CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MappingRules.cc
Go to the documentation of this file.
1 #include "MappingRules.h"
2 #include "ClassUtils.h"
3 //
4 #include <sstream>
5 #include <vector>
6 #include <cctype>
7 // externals
9 
11  static const std::string s_sequenceName("CONTAINER_ID");
12  return s_sequenceName;
13 }
14 
16  std::string ret("C_");
17  return ret+containerName;
18 }
19 
22  const std::string& className ){
23  std::string ret("D_");
24  ret+=containerName;
25  ret+="_";
26  ret+=className;
27  return ret;
28 }
29 
31  static const std::string s_mappingSequenceName("MAPPING_ELEMENT_ID");
32  return s_mappingSequenceName;
33 }
34 
37 {
38  static const std::string s_propertyName("mapping");
39  return s_propertyName;
40 }
41 
42 bool
44  return (mappingProperty == "Blob" || mappingProperty == "blob" || mappingProperty == "BLOB" );
45 }
46 
49  static const std::string s_propertyName("persistency");
50  return s_propertyName;
51 }
52 
53 bool
55  return (persistencyProperty == "loose_on_reading" || persistencyProperty == "LOOSE_ON_READING" );
56 }
57 
58 bool
60  return (persistencyProperty == "loose_on_writing" || persistencyProperty == "LOOSE_ON_WRITING" );
61 }
62 
65  const std::string& classVersion ){
66  return className+".V"+classVersion;
67 }
68 
70  std::string ret("");
71  size_t idx = classId.find('.');
72  if( idx != std::string::npos ){
73  ret = classId.substr( idx+2 );
74  }
75  return ret;
76 }
77 
80  return className+"."+baseClassVersion();
81 }
82 
85 {
86  static const std::string classVersion("BASE");
87  return classVersion;
88 }
89 
90 std::pair<bool,std::string> ora::MappingRules::classNameFromBaseId( const std::string& classId ){
91  std::pair<bool,std::string> ret(false,"");
92  size_t cut = classId.find("."+baseClassVersion() );
93  if( cut != std::string::npos ){
94  ret.first = true;
95  ret.second = classId.substr(0,cut);
96  }
97  return ret;
98 }
99 
102 {
103  std::string classVersion(className);
104  classVersion.append("_default");
105  return classVersion;
106 }
107 
110 {
111  static const std::string s_propertyName("class_version");
112  return s_propertyName;
113 
114 }
115 
118  int iteration,
119  char versionTrailer ){
120  std::ostringstream os;
121  os << itemName;
122  os << "_" << versionTrailer;
123  if ( iteration > 0 ) {
124  if ( iteration < 10 ) os << "0";
125  if ( iteration < 100 ) os << "0";
126  os << iteration;
127  } else {
128  os << "000";
129  }
130  return os.str();
131 }
132 
135  int iteration ){
136  return newMappingVersion( containerName, iteration, 'M' );
137 }
138 
141  const std::string& className,
142  int iteration )
143 {
144  std::string contDependencyName = containerName+"_"+className;
145  return newMappingVersion( contDependencyName, iteration, 'D' );
146 }
147 
150  const std::string& scope ){
151  std::stringstream scopedName;
152  if(!scope.empty()) scopedName << scope << "::";
153  scopedName << variableName;
154  return scopedName.str();
155 }
156 
159  unsigned int index ){
160  std::ostringstream arrayElementLabel;
161  arrayElementLabel << arrayVariable << "[" << index << "]";
162  return arrayElementLabel.str();
163 }
164 
167  std::ostringstream arrayElementLabel;
168  arrayElementLabel << "I" << arrayIndex;
169  return arrayElementLabel.str();
170 }
171 
174  std::stringstream contentTypeName;
175  size_t arraySize = ClassUtils::arrayLength( array );
176  contentTypeName << "A" << arraySize;
177  return contentTypeName.str();
178 }
179 
181  static const std::string s_cv("CV");
182  return s_cv;
183 }
184 
186  static const std::string s_ck("CK");
187  return s_ck;
188 }
189 
192  const std::string& scope ){
193  std::stringstream scopedName;
194  if(!scope.empty()) scopedName << formatName(scope, ClassNameLengthForSchema) << "_";
195  scopedName << variableName;
196  return scopedName.str();
197 }
198 
201 namespace ora {
202  static const std::string validChars("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-0123456789");
203  void checkString( const std::string& s, int code, bool thro=true){
204  for( size_t i=0;i<s.size();i++){
205  if( validChars.find( s[i])==std::string::npos ) {
206  std::stringstream mess;
207  mess <<" code="<<code<<" in string ["<<s<<"] found a wrong char=["<<s[i]<<"].";
208  if( thro ) throwException( mess.str(),"validChars");
209  }
210  }
211  }
212 
213 }
214 
217  unsigned int index,
218  size_t maxLength,
219  char indexTrailer){
220  unsigned int digitsForPostfix = 3;
221  if(index<10) digitsForPostfix = 2;
222  size_t newSize = initialName.size()+digitsForPostfix;
223  if(newSize > maxLength) newSize = maxLength;
224  unsigned int cutSize = newSize - digitsForPostfix;
225  std::stringstream newStr("");
226  if( initialName[cutSize-1]=='_' ) cutSize -= 1;
227  std::string cutString = initialName.substr(0, cutSize );
228  newStr << cutString << "_";
229  if( indexTrailer !=0 ) newStr<< indexTrailer;
230  newStr<< index;
231  //checkString( newStr.str(), 7 );
232  return newStr.str();
233 }
234 
237  unsigned int index,
238  size_t maxLength)
239 {
240  return newNameForSchemaObject( initialName, index, maxLength, 'D' );
241 }
242 
245  unsigned int index,
246  size_t maxL)
247 {
248  return newNameForSchemaObject( initialName, index, maxL, 'A' );
249 }
250 
253  std::string varName( variableName );
254  //first turn className into uppercase
255  MappingRules::ToUpper up(std::locale::classic());
256  std::transform(varName.begin(), varName.end(),varName.begin(),up);
257  size_t cutPoint = varName.size();
258  if(cutPoint && varName[varName.size()-1] == '_' ) cutPoint -= 1;
259  if(!cutPoint) return "";
260  size_t start = 0;
261  if(varName.size() && varName[0]== '_' ) start = 1;
262  return varName.substr(start,cutPoint);
263 }
264 
266  size_t maxL ){
267  if( !maxL ) return "";
268  if( className.size() < maxL ) return className;
269  std::vector< size_t> uppers;
270  for( size_t i=0;i<className.size();i++) {
271  if(::isupper(className[i]) || ::isdigit(className[i]) ) uppers.push_back(i);
272  }
273  std::stringstream shName;
274  size_t usize = uppers.size();
275  if( usize < maxL ){
276  size_t start = 0;
277  size_t cut = maxL-usize;
278  if( usize && (cut > uppers[0]) ) cut = uppers[0];
279  shName << className.substr( start, cut );
280  size_t left = maxL-cut-usize;
281  if( usize > 1) left = 0;
282  size_t curs = 0;
283  for( size_t i=0; i<usize;i++){
284  size_t st = uppers[i];
285  curs = st+left+1;
286  shName << className.substr(st,left+1);
287  left = 0;
288  }
289  size_t maxIndex = className.size();
290  if( shName.str().size()<maxL && curs < maxIndex ){
291  size_t more = maxL - shName.str().size();
292  size_t max = curs+more;
293  if(max > className.size()) max = className.size();
294  for( size_t j=curs;j<max-1;j++ ){
295  shName << className[j];
296  }
297  }
298 
299  //checkString( shName.str(), 0 );
300  } else {
301  shName << className[0];
302  for(size_t i=0 ;i<maxL-1;i++) {
303  if( uppers[i] != 0 ) shName << className[uppers[i]];
304  }
305  //checkString( shName.str(), 1 );
306  }
308  return shName.str();
309 }
310 
312  size_t maxLength ){
313  if( !maxLength ) return "";
314  std::string cn = scopedClassName;
315  std::string sn("");
316  size_t ns = cn.rfind("::");
317  if( ns!= std::string::npos ){
318  cn = scopedClassName.substr( ns+2 );
319  sn = scopedClassName.substr( 0, ns );
320  }
321  //
322  ns = cn.find(" ");
323  while( ns != std::string::npos ){
324  cn = cn.replace( ns, 1, "_");
325  ns = cn.find(" ");
326  }
327  //
328  ns = sn.find("::");
329  if( ns == 0 ){
330  sn = sn.substr( 2 );
331  ns = sn.find("::");
332  }
333  while( ns != std::string::npos ){
334  sn = sn.replace( ns, 2, "_");
335  ns = sn.find("::");
336  }
337  //
338  if( sn[sn.size()-1]=='_' ) sn = sn.substr(0,sn.size()-1);
339  // ignore if namespace==std
340  if( sn == "std" ) sn = "";
341 
342  size_t currSize = sn.size()+cn.size()+1;
343  if( currSize > maxLength+1 ){
344  // a cut is required...
345  size_t maxScopeLen = maxLength/3;
346  if( maxScopeLen ==0 ) maxScopeLen = 1;
347  if( maxScopeLen > 1 ) maxScopeLen -= 1;
348  if( sn.size() > maxScopeLen ){
349  sn = sn.substr( 0,maxScopeLen );
350  }
351  size_t availableSize = maxLength-sn.size();
352  if( sn.size() ) availableSize -= 1;
353  cn =shortNameByUpperCase( cn, availableSize );
354  }
355  std::string ret = sn;
356  if(!ret.empty()) ret += "_";
357  ret += cn;
358  //checkString( ret, 2 );
359  return ret;
360 }
361 
363  size_t maxLength ){
364  if( !maxLength ) return "";
365  std::string tempClassName(templateClassName);
366  size_t indComas = tempClassName.find(',',0);
367  std::vector<size_t> vComas;
368  while(indComas != std::string::npos) {
369  vComas.push_back(indComas);
370  indComas = tempClassName.find(',',indComas+1);
371  }
372  typedef std::vector<size_t>::const_iterator vSizeIter;
373  vSizeIter vBegin = vComas.begin();
374  vSizeIter vEnd = vComas.end();
375  for(vSizeIter i = vBegin; i != vEnd; ++i) {
376  tempClassName.replace(*i, 1, 1, '_');
377  }
378  std::string newName("");
379  size_t ind0 = tempClassName.find('<',0);
380  if( ind0 != std::string::npos ){
381  size_t ind1 = tempClassName.rfind('>');
382  std::string templArg = tempClassName.substr( ind0+1, ind1-ind0-1 );
383  std::string prefix = shortScopedName( tempClassName.substr( 0, ind0 ), maxLength );
384  size_t currSize = templArg.size()+prefix.size()+1;
385  if( currSize > maxLength+1 ){
386  // a cut is required...
387  size_t prefixL = maxLength/3;
388  if( prefixL == 0 ) prefixL = 1;
389  if( prefixL >1 ) prefixL -=1;
390  prefix = shortScopedName( prefix, prefixL );
391  }
392  size_t templMaxSize = maxLength-prefix.size()-1;
393  templArg = nameFromTemplate( templArg, templMaxSize );
394  newName = prefix+"_"+ templArg;
395  } else {
396  newName = shortScopedName( tempClassName, maxLength );
397  }
398  //checkString( newName, 3 );
399  return newName;
400 }
401 
404 {
405  return "ORA_C_"+nameForSchema(formatName( itemName, MaxTableNameLength-5 ));
406 }
407 
410 {
411  return std::string("ID");
412 }
413 
416 {
417  return std::string("REF_ID");
418 }
419 
422  const std::string& scopeName,
423  bool forData )
424 {
425  std::ostringstream totalString;
426  int scopeMaxSize = MaxColumnNameLength/4-1;
427  int extraSize = MaxColumnNameLength-variableName.size()-2;
428  if( extraSize>0 && extraSize>scopeMaxSize ) scopeMaxSize = extraSize;
429  if( !scopeName.empty() ) {
430  size_t scopeCut = scopeName.size();
431  if( scopeCut> (size_t)scopeMaxSize ) scopeCut = scopeMaxSize;
432  totalString << scopeName.substr(0,scopeCut);
433  totalString << "_";
434  }
435  size_t varMaxSize = MaxColumnNameLength-totalString.str().size();
436 
437  size_t fp = variableName.find('[');
438  if( fp != std::string::npos ){
439  // process for c-arrays
440  std::string arrayVar = variableName.substr(0,fp);
441  std::string indexVar = variableName.substr(fp);
442  for( size_t ind = 0; ind!=std::string::npos; ind=indexVar.find('[',ind+1 ) ){
443  indexVar = indexVar.replace(ind,1,"I");
444  }
445  for( size_t ind = indexVar.find(']'); ind!=std::string::npos; ind=indexVar.find(']',ind+1 ) ){
446  indexVar = indexVar.replace(ind,1,"_");
447  }
448  size_t arrayVarCut = 0;
449  size_t varCut = variableName.size()+1;
450  if( varCut>varMaxSize ) varCut = varMaxSize;
451  if( varCut>(indexVar.size()+1) ) arrayVarCut = varCut-indexVar.size()-1;
452  totalString << arrayVar.substr(0,arrayVarCut);
453  totalString << "_" << indexVar;
454  } else {
455  size_t varCut = variableName.size();
456  if( varCut>varMaxSize ) varCut = varMaxSize;
457  // other types
458  totalString << variableName.substr(0,varCut);
459  }
460 
461  std::stringstream ret;
462  if(forData) ret << "D";
463  ret << nameForSchema(totalString.str());
464  //return formatForSchema(ret.str(),MaxColumnNameLength);
465  return formatName(ret.str(),MaxColumnNameLength);
466 }
467 
470  const std::string& scope,
471  unsigned int index )
472 {
473  std::stringstream ret;
474  ret << "R" << columnNameForVariable( variableName, scope, false )<<"_OID"<<index;
475  return ret.str();
476 }
477 
480  const std::string& scope )
481 {
482  std::stringstream ret;
483  ret << "R" << columnNameForVariable( variableName, scope, false )<<"_NAME";
484  return ret.str();
485 }
486 
489  const std::string& scope )
490 {
491  std::stringstream ret;
492  ret << "M" << columnNameForVariable( variableName, scope, false );
493  return ret.str();
494 }
495 
498  const std::string& scope )
499 {
500  std::stringstream ret;
501  ret << "RID_" << columnNameForVariable( variableName, scope, false );
502  return ret.str();
503 }
504 
507 {
508  return std::string("POS");
509 }
510 
513  std::string prefix("M");
514  return prefix + dataColumnName.substr(1);
515 }
516 
519 {
520  std::stringstream ret;
521  ret << tableName <<"_ID_FK";
522  if( index ) ret << "_"<<index;
523  return ret.str();
524 }
525 
527  size_t maxLength ){
528  return nameFromTemplate( variableName, maxLength );
529 }
530 
Definition: BitonicSort.h:8
int i
Definition: DBlmapReader.cc:9
tuple start
Check for commandline option errors.
Definition: dqm_diff.py:58
static std::string columnNameForRefId(const std::string &variableName, const std::string &scope)
static std::string variableNameForArrayIndex(const std::string &arrayVariable, unsigned int index)
static std::string scopedVariableName(const std::string &variableName, const std::string &scope)
variable name manipulation
static std::string newMappingVersionForContainer(const std::string &containerName, int iteration)
mapping versions
static std::string variableNameForContainerKey()
static std::string variableNameForArrayColumn(unsigned int arrayIndex)
static std::string variableNameForContainerValue()
static std::string persistencyPropertyNameInDictionary()
Definition: MappingRules.cc:48
static std::string newNameForDepSchemaObject(const std::string &initialName, unsigned int index, size_t maxLength)
static std::string baseIdForClass(const std::string &className)
Definition: MappingRules.cc:79
static std::string scopedVariableForSchemaObjects(const std::string &variableName, const std::string &scope)
static std::string shortScopedName(const std::string &scopedClassName, size_t maxLength)
static std::string columnNameForRefColumn()
static std::string columnNameForVariable(const std::string &variableName, const std::string &scope, bool forData=true)
static std::string mappingPropertyNameInDictionary()
class related parameters
Definition: MappingRules.cc:36
static std::string columnNameForPosition()
tuple iteration
Definition: align_cfg.py:5
static std::string shortNameByUpperCase(const std::string &className, size_t maxLength)
static std::string sequenceNameForMapping()
Definition: MappingRules.cc:30
static std::string defaultClassVersion(const std::string &className)
static std::string newMappingVersion(const std::string &itemName, int iteration, char versionTrailer)
int j
Definition: DBlmapReader.cc:9
static std::string columnNameForOID(const std::string &variableName, const std::string &scope, unsigned int index)
static bool isLooseOnReading(const std::string &persistencyProperty)
Definition: MappingRules.cc:54
static const std::string validChars("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-0123456789")
static std::string columnNameForBlobMetadata(const std::string &dataColumnName)
static std::string newNameForArraySchemaObject(const std::string &initialName, unsigned int index, size_t maxLength)
static bool isMappedToBlob(const std::string &mappingProperty)
Definition: MappingRules.cc:43
static std::string columnNameForId()
static std::string classVersionFromId(const std::string &classId)
Definition: MappingRules.cc:69
static std::pair< bool, std::string > classNameFromBaseId(const std::string &classId)
Definition: MappingRules.cc:90
void checkString(const std::string &s, int code, bool thro=true)
static bool isLooseOnWriting(const std::string &persistencyProperty)
Definition: MappingRules.cc:59
static std::string columnNameForRefMetadata(const std::string &variableName, const std::string &scope)
tuple idx
DEBUGGING if hasattr(process,&quot;trackMonIterativeTracking2012&quot;): print &quot;trackMonIterativeTracking2012 D...
size_t arrayLength(const edm::TypeWithDict &typ)
Definition: ClassUtils.cc:493
void throwException(const std::string &message, const std::string &methodName) __attribute__((noreturn))
Definition: Exception.cc:10
static std::string nameFromTemplate(const std::string &templateClassName, size_t maxLength)
static std::string baseClassVersion()
Definition: MappingRules.cc:84
static std::string columnNameForNamedReference(const std::string &variableName, const std::string &scope)
static std::string nameForSchema(const std::string &variableName)
static std::string classId(const std::string &className, const std::string &classVersion)
Definition: MappingRules.cc:64
static std::string sequenceNameForContainer(const std::string &containerName)
Definition: MappingRules.cc:15
static std::string tableNameForItem(const std::string &itemName)
schema object naming
static std::string sequenceNameForContainerId()
sequence names
Definition: MappingRules.cc:10
static std::string newMappingVersionForDependentClass(const std::string &containerName, const std::string &className, int iteration)
static std::string sequenceNameForDependentClass(const std::string &containerName, const std::string &className)
Definition: MappingRules.cc:21
static std::string classVersionPropertyNameInDictionary()
static std::string newNameForSchemaObject(const std::string &initialName, unsigned int index, size_t maxLength, char indexTrailer=0)
functions for new schema object name generation
static std::string formatName(const std::string &variableName, size_t maxLength)
formatting for variable names to schema object names
static std::string fkNameForIdentity(const std::string &tableName, int index=0)
std::string className(const T &t)
Definition: ClassName.h:30