CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Private Member Functions | Private Attributes
ora::MappingToSchema Class Reference

#include <MappingToSchema.h>

Public Member Functions

void alter (const MappingTree &mapping)
 
bool check (const MappingTree &mapping)
 
void create (const MappingTree &mapping)
 
 MappingToSchema (coral::ISchema &schema)
 Constructor. More...
 
 ~MappingToSchema ()
 Destructor. More...
 

Private Member Functions

void createTable (const TableInfo &tableInfo)
 

Private Attributes

coral::ISchema & m_schema
 Reference to the schema in use. More...
 

Detailed Description

Helper class which is used for the creation of the tables which hold the object data, conforming to a given object/relational mapping.

Definition at line 19 of file MappingToSchema.h.

Constructor & Destructor Documentation

ora::MappingToSchema::MappingToSchema ( coral::ISchema &  schema)
explicit

Constructor.

Definition at line 16 of file MappingToSchema.cc.

16  :
17  m_schema( schema ){
18 }
coral::ISchema & m_schema
Reference to the schema in use.
ora::MappingToSchema::~MappingToSchema ( )

Destructor.

Definition at line 20 of file MappingToSchema.cc.

20  {
21 }

Member Function Documentation

void ora::MappingToSchema::alter ( const MappingTree mapping)

Definition at line 73 of file MappingToSchema.cc.

References funct::false, i, asciidump::table, ora::MappingTree::tables(), and ora::throwException().

Referenced by ora::ContainerSchema::evolve().

73  {
74  std::vector<TableInfo> tableList = mapping.tables();
75  for( std::vector<TableInfo>::iterator iT = tableList.begin();
76  iT != tableList.end(); ++iT ){
77  if( m_schema.existsTable( iT->m_tableName ) ){
78  std::set<std::string> allCols;
79  coral::ITable& table = m_schema.tableHandle( iT->m_tableName );
80  // check all of the columns
81  for( std::vector<std::string>::const_iterator iCol = iT->m_idColumns.begin();
82  iCol != iT->m_idColumns.end(); ++iCol ){
83  try {
84  table.description().columnDescription( *iCol );
85  } catch ( const coral::InvalidColumnNameException&){
86  // not recoverable: id columns cannot be added.
87  throwException("ID Column \""+*iCol+"\" has not been found in table \""+iT->m_tableName+"\" as required in the mapping.",
88  "MappingToSchema::alter");
89  }
90  allCols.insert( *iCol );
91  }
92  for( std::map<std::string,std::string>::const_iterator iDataCol = iT->m_dataColumns.begin();
93  iDataCol != iT->m_dataColumns.end(); ++iDataCol ){
94  try {
95  const coral::IColumn& colDescr = table.description().columnDescription( iDataCol->first );
96  // check the type
97  if( colDescr.type() != iDataCol->second ){
98  // not recoverable: column type cannot be changed.
99  throwException("ID Column \""+iDataCol->first+"\" in table \""+iT->m_tableName+"\" is type \""+colDescr.type()+
100  "\" while is required of type \""+iDataCol->second+"\" in the mapping.",
101  "MappingToSchema::alter");
102  }
103 
104  } catch ( const coral::InvalidColumnNameException&){
105  table.schemaEditor().insertColumn( iDataCol->first, iDataCol->second );
106  table.schemaEditor().setNotNullConstraint( iDataCol->first );
107  }
108  allCols.insert( iDataCol->first );
109  }
110  // then check the unused columns for not null constraint
111  int ncols = table.description().numberOfColumns();
112  for( int i=0;i<ncols;i++ ){
113  const coral::IColumn& colDescr = table.description().columnDescription( i );
114  std::set<std::string>::const_iterator iC = allCols.find( colDescr.name() );
115  if( iC == allCols.end() ){
116  table.schemaEditor().setNotNullConstraint( colDescr.name(), false );
117  }
118  }
119  } else {
120  createTable( *iT );
121  }
122  }
123 }
list table
Definition: asciidump.py:386
int i
Definition: DBlmapReader.cc:9
coral::ISchema & m_schema
Reference to the schema in use.
void createTable(const TableInfo &tableInfo)
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:10
bool ora::MappingToSchema::check ( const MappingTree mapping)

Definition at line 125 of file MappingToSchema.cc.

References convertSQLiteXML::ok, and ora::MappingTree::tables().

125  {
126  bool ok = true;
127  std::vector<TableInfo> tableList = mapping.tables();
128  for( std::vector<TableInfo>::iterator iT = tableList.begin();
129  iT != tableList.end(); ++iT ){
130  if( m_schema.existsTable( iT->m_tableName ) ){
131  ok = false;
132  }
133  }
134  return ok;
135 }
coral::ISchema & m_schema
Reference to the schema in use.
void ora::MappingToSchema::create ( const MappingTree mapping)

Definition at line 64 of file MappingToSchema.cc.

References ora::MappingTree::tables().

Referenced by ora::ContainerSchema::create().

64  {
65  std::vector<TableInfo> tableList = mapping.tables();
66  for( std::vector<TableInfo>::iterator iT = tableList.begin();
67  iT != tableList.end(); ++iT ){
68  createTable( *iT );
69  }
70 
71 }
void createTable(const TableInfo &tableInfo)
void ora::MappingToSchema::createTable ( const TableInfo tableInfo)
private

Definition at line 23 of file MappingToSchema.cc.

References idDealer::description, ora::MappingRules::fkNameForIdentity(), i, ora::TableInfo::m_dataColumns, ora::TableInfo::m_dependency, ora::TableInfo::m_idColumns, ora::TableInfo::m_parentTableName, ora::TableInfo::m_refColumns, and ora::TableInfo::m_tableName.

23  {
24  coral::TableDescription description("ORA");
25  description.setName(tableInfo.m_tableName);
26  std::vector<std::string> columnsForIndex;
27  std::vector<std::string> columnsForFk;
28  size_t i=0;
29  size_t cols = tableInfo.m_idColumns.size();
30  for( std::vector<std::string>::const_iterator iCol = tableInfo.m_idColumns.begin();
31  iCol != tableInfo.m_idColumns.end(); ++iCol ){
32  description.insertColumn( *iCol, coral::AttributeSpecification::typeNameForId( typeid(int) ) );
33  description.setNotNullConstraint( *iCol );
34  if( !tableInfo.m_dependency ) {
35  columnsForIndex.push_back( *iCol );
36  if( i< cols-1 ) columnsForFk.push_back( *iCol );
37  } else {
38  if( i>0 ) columnsForIndex.push_back( *iCol );
39  if( i>0 && i< cols-1 ) columnsForFk.push_back( *iCol );
40  }
41  ++i;
42  }
43  for( std::map<std::string,std::string>::const_iterator iDataCol = tableInfo.m_dataColumns.begin();
44  iDataCol != tableInfo.m_dataColumns.end(); ++iDataCol ){
45  description.insertColumn( iDataCol->first, iDataCol->second );
46  description.setNotNullConstraint( iDataCol->first );
47  }
48  description.setPrimaryKey( columnsForIndex );
49  if( !tableInfo.m_parentTableName.empty() ){
50  std::string fkName = MappingRules::fkNameForIdentity( tableInfo.m_tableName );
51 
52  if( !tableInfo.m_dependency ) {
53  description.createForeignKey( fkName, columnsForFk, tableInfo.m_parentTableName, tableInfo.m_refColumns );
54  } else {
55  std::vector<std::string> refCols;
56  for(size_t i=1;i<tableInfo.m_refColumns.size();i++) refCols.push_back( tableInfo.m_refColumns[i] );
57  if( !refCols.empty() ) description.createForeignKey( fkName, columnsForFk, tableInfo.m_parentTableName, refCols );
58  }
59  }
60  m_schema.createTable( description ).privilegeManager().grantToPublic( coral::ITablePrivilegeManager::Select );
61 
62 }
int i
Definition: DBlmapReader.cc:9
coral::ISchema & m_schema
Reference to the schema in use.
tuple description
Definition: idDealer.py:66
static std::string fkNameForIdentity(const std::string &tableName, int index=0)

Member Data Documentation

coral::ISchema& ora::MappingToSchema::m_schema
private

Reference to the schema in use.

Definition at line 39 of file MappingToSchema.h.