CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MappingElement.cc
Go to the documentation of this file.
2 #include "MappingElement.h"
3 //
4 #include <set>
5 #include <sstream>
6 
7 std::string
9 {
10  static std::string s_classMappingElementType = "Class";
11  return s_classMappingElementType;
12 }
13 
14 std::string
16 {
17  static std::string s_objectMappingElementType = "Object";
18  return s_objectMappingElementType;
19 }
20 
21 std::string
23 {
24  static std::string s_dependencyMappingElementType = "Dependency";
25  return s_dependencyMappingElementType;
26 }
27 
28 std::string
30 {
31  static std::string s_primitiveMappingElementType = "Primitive";
32  return s_primitiveMappingElementType;
33 }
34 
35 std::string
37 {
38  static std::string s_arrayMappingElementType = "Array";
39  return s_arrayMappingElementType;
40 }
41 
42 std::string
44 {
45  static std::string s_CArrayMappingElementType = "CArray";
46  return s_CArrayMappingElementType;
47 }
48 
49 std::string
51 {
52  static std::string s_inlineCArrayMappingElementType = "InlineCArray";
53  return s_inlineCArrayMappingElementType;
54 }
55 
56 std::string
58 {
59  static std::string s_oraReferenceMappingElementType = "OraReference";
60  return s_oraReferenceMappingElementType;
61 }
62 
63 std::string
65 {
66  static std::string s_oraPointerMappingElementType = "OraPointer";
67  return s_oraPointerMappingElementType;
68 }
69 
70 std::string
72 {
73  static std::string s_oraUniqueReferenceMappingElementType = "UniqueReference";
74  return s_oraUniqueReferenceMappingElementType;
75 }
76 
77 std::string
79 {
80  static std::string s_oraArrayMappingElementType = "OraArray";
81  return s_oraArrayMappingElementType;
82 }
83 
84 std::string
86 {
87  static std::string s_pointerMappingElementType = "Pointer";
88  return s_pointerMappingElementType;
89 }
90 
91 std::string
93 {
94  static std::string s_referenceMappingElementType = "Reference";
95  return s_referenceMappingElementType;
96 }
97 
98 std::string
100 {
101  static std::string s_blobMappingElementType = "Blob";
102  return s_blobMappingElementType;
103 }
104 
105 std::string
107 {
108  static std::string s_namedReferenceMappingElementType = "NamedReference";
109  return s_namedReferenceMappingElementType;
110 }
111 
112 bool
113 ora::MappingElement::isValidMappingElementType( const std::string& elementType )
114 {
115  return ( elementType == classMappingElementType() ||
116  elementType == objectMappingElementType() ||
117  elementType == dependencyMappingElementType() ||
118  elementType == primitiveMappingElementType() ||
119  elementType == arrayMappingElementType() ||
120  elementType == CArrayMappingElementType() ||
121  elementType == inlineCArrayMappingElementType() ||
122  elementType == OraReferenceMappingElementType() ||
123  elementType == OraPointerMappingElementType() ||
124  elementType == uniqueReferenceMappingElementType() ||
125  elementType == OraArrayMappingElementType() ||
126  elementType == pointerMappingElementType() ||
127  elementType == referenceMappingElementType() ||
128  elementType == blobMappingElementType() ||
129  elementType == namedReferenceMappingElementType() );
130 }
131 
132 std::string
134 {
135  switch ( elementType ) {
136  case Class :
137  return classMappingElementType();
138  break;
139  case Object :
140  return objectMappingElementType();
141  break;
142  case Dependency :
143  return dependencyMappingElementType();
144  break;
145  case Primitive :
146  return primitiveMappingElementType();
147  break;
148  case Array :
149  return arrayMappingElementType();
150  break;
151  case CArray :
152  return CArrayMappingElementType();
153  break;
154  case InlineCArray :
155  return inlineCArrayMappingElementType();
156  break;
157  case OraReference :
158  return OraReferenceMappingElementType();
159  break;
160  case OraPointer :
161  return OraPointerMappingElementType();
162  break;
163  case UniqueReference :
164  return uniqueReferenceMappingElementType();
165  break;
166  case OraArray :
167  return OraArrayMappingElementType();
168  break;
169  case Reference :
170  return referenceMappingElementType();
171  break;
172  case Blob :
173  return blobMappingElementType();
174  break;
175  case NamedReference :
176  return namedReferenceMappingElementType();
177  break;
178  case Pointer :
179  return pointerMappingElementType();
180  break;
181  case Undefined :
182  // This should never appear
183  break;
184  };
185 
186  std::stringstream ms;
187  ms <<"Undefined mapping element type code="<< elementType;
188  throwException( ms.str(),
189  "MappingElement::elementTypeAsString" );
190  return "";
191 }
192 
193 
195 ora::MappingElement::elementTypeFromString( const std::string& elementType )
196 {
197  // Check here the element type
198  if ( ! isValidMappingElementType( elementType ) ) {
199  throwException( "\"" + elementType + "\" is not a supported mapping element type",
200  "MappingElement::elementTypeFromString" );
201  }
202 
204 
205  if ( elementType == classMappingElementType() ) {
206  return Class;
207  }
208  if ( elementType == objectMappingElementType() ) {
209  return Object;
210  }
211  if ( elementType == dependencyMappingElementType() ) {
212  return Dependency;
213  }
214  if ( elementType == primitiveMappingElementType() ) {
215  return Primitive;
216  }
217  else if ( elementType == arrayMappingElementType() ) {
218  return Array;
219  }
220  else if ( elementType == CArrayMappingElementType() ) {
221  return CArray;
222  }
223  else if ( elementType == inlineCArrayMappingElementType() ) {
224  return InlineCArray;
225  }
226  else if ( elementType == pointerMappingElementType() ) {
227  return Pointer;
228  }
229  else if ( elementType == referenceMappingElementType() ) {
230  return Reference;
231  }
232  else if ( elementType == OraReferenceMappingElementType() ) {
233  return OraReference;
234  }
235  else if ( elementType == OraPointerMappingElementType() ) {
236  return OraPointer;
237  }
238  else if ( elementType == uniqueReferenceMappingElementType() ) {
239  return UniqueReference;
240  }
241  else if ( elementType == OraArrayMappingElementType() ) {
242  return OraArray;
243  }
244  else if ( elementType == blobMappingElementType() ) {
245  return Blob;
246  }
247  else if ( elementType == namedReferenceMappingElementType() ) {
248  return NamedReference;
249  }
250 
251  return result;
252 }
253 
254 ora::MappingElement::MappingElement( const std::string& elementType,
255  const std::string& variableName,
256  const std::string& variableType,
257  const std::string& tableName ):
258  m_elementType( Undefined ),
259  m_isDependentTree(false),
260  m_scopeName( "" ),
261  m_variableName( variableName ),
262  m_variableNameForSchema( "" ),
263  m_variableType( variableType ),
264  m_tableName( tableName ),
265  m_columnNames(),
266  m_subElements()
267 {
268  // Check here the element type
269  m_elementType = elementTypeFromString( elementType );
271 }
272 
274 }
275 
276 namespace ora {
277  void processTableHierarchy( const MappingElement& element,
278  std::set<std::string>& tableRegister,
279  std::vector<std::pair<std::string, std::string> >& tableList ){
280  const std::string& tableName = element.tableName();
281  std::set<std::string>::iterator iT = tableRegister.find( tableName );
282  if( iT == tableRegister.end() ){
283  if( element.columnNames().empty() ){
284  throwException( "Mapping element for variable \""+element.variableName()+"\" does not specify column names.",
285  "MappingElement::tableHierarchy");
286  }
287  tableRegister.insert( tableName );
288  tableList.push_back( std::make_pair(tableName,element.columnNames()[0]) );
289  }
290  for(MappingElement::const_iterator iEl = element.begin();
291  iEl != element.end(); ++iEl ){
292  processTableHierarchy( iEl->second, tableRegister, tableList );
293  }
294  }
295 }
296 
297 std::vector<std::pair<std::string,std::string> >
299  std::set<std::string> involvedTables;
300  std::vector<std::pair<std::string,std::string> > tableList;
301  processTableHierarchy( *this, involvedTables,tableList );
302  return tableList;
303 }
304 
305 std::string ora::MappingElement::idColumn() const {
306  if( m_columnNames.empty() ) throwException( "No column names found in the mapping element.",
307  "MappingElement::idColumn");
308  return m_columnNames.front();
309 }
310 
311 std::string ora::MappingElement::pkColumn() const {
312  size_t ind = 0;
313  if( m_isDependentTree ) ind = 1;
314  if( m_columnNames.size() < ind+1 )
315  throwException( "Column names not found as expected in the mapping element.",
316  "MappingElement::idColumn");
317  return m_columnNames.at( ind );
318 }
319 
320 std::vector<std::string> ora::MappingElement::recordIdColumns() const {
321  size_t ind = 0;
322  if( m_isDependentTree ) ind = 1;
323  size_t cols = m_columnNames.size();
324  if( cols < ind+2 ){
325  std::stringstream message;
326  message <<"Column names for variable=\""<< m_variableName<<"\" of type=\""<<elementTypeAsString( m_elementType )<<"\" are not as expected.";
327  throwException( message.str(),
328  "MappingElement::recordIdColumns");
329  }
330  std::vector<std::string> ret;
331  for( size_t i=ind+1;i<cols;i++){
332  ret.push_back( m_columnNames[i] );
333  }
334  return ret;
335 }
336 
337 std::string ora::MappingElement::posColumn() const {
338  size_t ind = 0;
339  if( m_isDependentTree ) ind = 1;
340  if( m_columnNames.size() < ind+2 )
341  throwException( "Column names not found as expected in the mapping element.",
342  "MappingElement::posColumn");
343  return m_columnNames.back();
344 }
345 
347 ora::MappingElement::appendSubElement( const std::string& elementType,
348  const std::string& variableName,
349  const std::string& variableType,
350  const std::string& tableName )
351 {
352  // Check whether the current element type supports other subelements
353  if ( ! ( m_elementType == Class ||
354  m_elementType == Object ||
355  m_elementType == Dependency ||
356  m_elementType == Array ||
357  m_elementType == CArray ||
358  m_elementType == InlineCArray ||
359  m_elementType == OraPointer ||
360  m_elementType == OraArray ) ) {
361  throwException( "Attempted to insert a sub-element under an element of type \"" +
362  ora::MappingElement::elementTypeAsString( m_elementType ) + "\"",
363  "MappingElement::appendSubElement" );
364  }
365  if(m_subElements.find( variableName )!=m_subElements.end()){
366  throwException("Attribute name \""+variableName+"\" is already defined in the mapping element of variable \""+
367  m_variableName+"\".",
368  "MappingElement::appendSubElement");
369  }
370 
371  MappingElement& newElement =
372  m_subElements.insert( std::make_pair( variableName,ora::MappingElement( elementType,
373  variableName,
374  variableType,
375  tableName ) ) ).first->second;
376  newElement.m_isDependentTree = m_isDependentTree;
377  if ( m_scopeName.empty() ) {
378  newElement.m_scopeName = m_variableName;
379  } else {
380  newElement.m_scopeName = m_scopeName + "::" + m_variableName;
381  }
382 
383  return newElement;
384 }
385 
386 void
387 ora::MappingElement::alterType( const std::string& elementType )
388 {
389  // Check here the element type
390  if ( ! isValidMappingElementType( elementType ) ) {
391  throwException( "\"" + elementType + "\" is not a supported mapping element type",
392  "MappingElement::alterType");
393  }
394  ElementType elementTypeCode = elementTypeFromString( elementType );
395  if(m_elementType != elementTypeCode){
396  m_elementType = elementTypeCode;
397  // clear sub elements when no supported by the new type specified
398  if ( ! ( m_elementType == Class ||
399  m_elementType == Object ||
400  m_elementType == Dependency ||
401  m_elementType == Array ||
402  m_elementType == CArray ||
403  m_elementType == InlineCArray ||
404  m_elementType == OraPointer ||
405  m_elementType == OraArray ) ) {
406  m_subElements.clear();
407  }
408  }
409 }
410 
411 void
412 ora::MappingElement::alterTableName( const std::string& tableName )
413 {
414  if ( tableName == m_tableName ) return;
415 
416  for ( iterator iElement = this->begin();
417  iElement != this->end(); ++iElement ) {
418  ora::MappingElement& subElement = iElement->second;
419  if ( subElement.elementType() != Array &&
420  subElement.elementType() != CArray &&
421  subElement.elementType() != OraArray ) {
422  subElement.alterTableName( tableName );
423  }
424  }
425  m_tableName = tableName;
426 }
427 
428 void
429 ora::MappingElement::setColumnNames( const std::vector< std::string >& columns )
430 {
431  m_columnNames = columns;
432  if ( m_subElements.empty() ) return;
433 }
434 
436  if(variableType()==source.variableType() || elementType()==source.elementType())
437  {
439  alterTableName(source.tableName());
440  setColumnNames(source.columnNames());
441  for(iterator iel=begin();
442  iel!=end();++iel){
443  const_iterator iTarg = source.find(iel->first);
444  if(iTarg!=source.end()){
445  iel->second.override(iTarg->second);
446  }
447  }
448  }
449 
450 }
451 
452 void ora::MappingElement::printXML( std::ostream& outputStream, std::string indentation ) const {
453  outputStream << indentation << "<"<<elementTypeString()<<" name=\""<<m_variableName<<"\" type=\""<<m_variableType<<
454  "\" table=\""<<m_tableName<<"\"";
455  if( !m_columnNames.empty()) {
456  outputStream << " columns=\"";
457  for( std::vector<std::string>::const_iterator iC=m_columnNames.begin(); iC != m_columnNames.end(); ++iC ){
458  if( iC != m_columnNames.begin() ) outputStream << ",";
459  outputStream << *iC;
460  }
461  outputStream << "\"";
462  }
463  outputStream << " >"<< std::endl;
464  for( std::map< std::string, MappingElement >::const_iterator iM = m_subElements.begin();
465  iM != m_subElements.end(); ++iM ){
466  iM->second.printXML( outputStream, indentation+" " );
467  }
468  outputStream << indentation << "</"<<elementTypeString()<<">"<< std::endl;
469 }
470 
int i
Definition: DBlmapReader.cc:9
static std::string namedReferenceMappingElementType()
Returns the name of the named reference element type.
static std::string blobMappingElementType()
Returns the name of the blob mapping element type.
static std::string arrayMappingElementType()
Returns the name of the array mapping element type.
const std::string & variableName() const
static std::string primitiveMappingElementType()
Returns the name of the primitive mapping element type.
static std::string dependencyMappingElementType()
Returns the name of the dependent class mapping element type.
std::string idColumn() const
static std::string OraReferenceMappingElementType()
Returns the name of the ORA reference mapping element type.
ElementType elementType() const
MappingElement & appendSubElement(const std::string &elementType, const std::string &variableName, const std::string &variableType, const std::string &tableName)
MappingElement()
Empty Constructor.
void alterTableName(const std::string &tableName)
iterator begin()
Returns an iterator in the beginning of the sequence.
static std::string pointerMappingElementType()
Returns the name of the pointer mapping element type.
std::vector< std::string > recordIdColumns() const
std::string posColumn() const
tuple result
Definition: query.py:137
void alterType(const std::string &elementType)
static std::string elementTypeAsString(ElementType elementType)
Converts the enumeration type to a string.
static std::string objectMappingElementType()
Returns the name of the object mapping element type.
#define end
Definition: vmac.h:38
std::vector< std::pair< std::string, std::string > > tableHierarchy() const
static std::string uniqueReferenceMappingElementType()
Returns the name of the ORA polymorphic pointer mapping element type.
iterator find(const std::string &key)
Retrieves a sub-element.
std::string pkColumn() const
std::map< std::string, MappingElement >::iterator iterator
Iterator definition.
const std::vector< std::string > & columnNames() const
void printXML(std::ostream &outputStream, std::string indentation="") const
static std::string referenceMappingElementType()
Returns the name of the reference mapping element type.
~MappingElement()
Destructor.
static std::string inlineCArrayMappingElementType()
Returns the name of the inline array mapping element type.
void processTableHierarchy(const MappingElement &element, std::set< std::string > &tableRegister, std::vector< std::pair< std::string, std::string > > &tableList)
void override(const MappingElement &source)
const std::string & variableType() const
std::map< std::string, MappingElement >::const_iterator const_iterator
static ElementType elementTypeFromString(const std::string &elementType)
Converts a string into an element type.
iterator end()
Returns an iterator in the end of the sequence.
std::string m_scopeName
#define begin
Definition: vmac.h:31
static std::string OraPointerMappingElementType()
Returns the name of the ORA pointer mapping element type.
static std::string OraArrayMappingElementType()
Returns the name of the ORA array mapping element type.
const std::string & tableName() const
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:10
static bool isValidMappingElementType(const std::string &elementType)
Checks if the provided element type is valid.
static std::string classMappingElementType()
Returns the name of the class mapping element type.
static std::string CArrayMappingElementType()
Returns the name of the array mapping element type.
void setColumnNames(const std::vector< std::string > &columns)
ElementType m_elementType