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