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