CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MappingToSchema.cc
Go to the documentation of this file.
2 #include "MappingToSchema.h"
3 #include "MappingTree.h"
4 #include "MappingRules.h"
5 //
6 // externals
7 #include "CoralBase/Blob.h"
8 #include "CoralBase/AttributeSpecification.h"
9 #include "RelationalAccess/IColumn.h"
10 #include "RelationalAccess/ISchema.h"
11 #include "RelationalAccess/ITable.h"
12 #include "RelationalAccess/ITablePrivilegeManager.h"
13 #include "RelationalAccess/SchemaException.h"
14 #include "RelationalAccess/TableDescription.h"
15 
17  m_schema( schema ){
18 }
19 
21 }
22 
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  // workaround (temporary?) for Blob. The metadata column has to be relaxed in the NOT NULL constraint (previous versions are not filling this column)
47  std::set<std::string>::const_iterator iNullable = tableInfo.m_nullableColumns.find( iDataCol->first );
48  if( iNullable == tableInfo.m_nullableColumns.end()){
49  description.setNotNullConstraint( iDataCol->first );
50  }
51  }
52  description.setPrimaryKey( columnsForIndex );
53  if( !tableInfo.m_parentTableName.empty() ){
55 
56  if( !tableInfo.m_dependency ) {
57  description.createForeignKey( fkName, columnsForFk, tableInfo.m_parentTableName, tableInfo.m_refColumns );
58  } else {
59  std::vector<std::string> refCols;
60  for(size_t i=1;i<tableInfo.m_refColumns.size();i++) refCols.push_back( tableInfo.m_refColumns[i] );
61  if( !refCols.empty() ) description.createForeignKey( fkName, columnsForFk, tableInfo.m_parentTableName, refCols );
62  }
63  }
64  m_schema.createTable( description );
65  //.privilegeManager().grantToPublic( coral::ITablePrivilegeManager::Select );
66 
67 }
68 
70  std::vector<TableInfo> tableList = mapping.tables();
71  for( std::vector<TableInfo>::iterator iT = tableList.begin();
72  iT != tableList.end(); ++iT ){
73  createTable( *iT );
74  }
75 
76 }
77 
79  std::vector<TableInfo> tableList = mapping.tables();
80  for( std::vector<TableInfo>::iterator iT = tableList.begin();
81  iT != tableList.end(); ++iT ){
82  if( m_schema.existsTable( iT->m_tableName ) ){
83  std::set<std::string> allCols;
84  coral::ITable& table = m_schema.tableHandle( iT->m_tableName );
85  // check all of the columns
86  for( std::vector<std::string>::const_iterator iCol = iT->m_idColumns.begin();
87  iCol != iT->m_idColumns.end(); ++iCol ){
88  try {
89  table.description().columnDescription( *iCol );
90  } catch ( const coral::InvalidColumnNameException&){
91  // not recoverable: id columns cannot be added.
92  throwException("ID Column \""+*iCol+"\" has not been found in table \""+iT->m_tableName+"\" as required in the mapping.",
93  "MappingToSchema::alter");
94  }
95  allCols.insert( *iCol );
96  }
97  for( std::map<std::string,std::string>::const_iterator iDataCol = iT->m_dataColumns.begin();
98  iDataCol != iT->m_dataColumns.end(); ++iDataCol ){
99  try {
100  const coral::IColumn& colDescr = table.description().columnDescription( iDataCol->first );
101  // check the type
102  if( colDescr.type() != iDataCol->second ){
103  // not recoverable: column type cannot be changed.
104  throwException("ID Column \""+iDataCol->first+"\" in table \""+iT->m_tableName+"\" is type \""+colDescr.type()+
105  "\" while is required of type \""+iDataCol->second+"\" in the mapping.",
106  "MappingToSchema::alter");
107  }
108 
109  } catch ( const coral::InvalidColumnNameException&){
110  table.schemaEditor().insertColumn( iDataCol->first, iDataCol->second );
111  table.schemaEditor().setNotNullConstraint( iDataCol->first );
112  }
113  allCols.insert( iDataCol->first );
114  }
115  // then check the unused columns for not null constraint
116  int ncols = table.description().numberOfColumns();
117  for( int i=0;i<ncols;i++ ){
118  const coral::IColumn& colDescr = table.description().columnDescription( i );
119  std::set<std::string>::const_iterator iC = allCols.find( colDescr.name() );
120  if( iC == allCols.end() ){
121  table.schemaEditor().setNotNullConstraint( colDescr.name(), false );
122  }
123  }
124  } else {
125  createTable( *iT );
126  }
127  }
128 }
129 
131  bool ok = true;
132  std::vector<TableInfo> tableList = mapping.tables();
133  for( std::vector<TableInfo>::iterator iT = tableList.begin();
134  iT != tableList.end(); ++iT ){
135  if( m_schema.existsTable( iT->m_tableName ) ){
136  ok = false;
137  }
138  }
139  return ok;
140 }
141 
142 
143 
list table
Definition: asciidump.py:386
int i
Definition: DBlmapReader.cc:9
std::set< std::string > m_nullableColumns
Definition: MappingTree.h:45
std::vector< std::string > m_refColumns
Definition: MappingTree.h:44
std::map< std::string, std::string > m_dataColumns
Definition: MappingTree.h:42
void createTable(const TableInfo &tableInfo)
std::string m_parentTableName
Definition: MappingTree.h:43
~MappingToSchema()
Destructor.
std::string m_tableName
Definition: MappingTree.h:40
bool check(const MappingTree &mapping)
std::vector< TableInfo > tables() const
Definition: MappingTree.cc:123
MappingToSchema(coral::ISchema &schema)
Constructor.
tuple description
Definition: idDealer.py:66
void alter(const MappingTree &mapping)
void create(const MappingTree &mapping)
std::vector< std::string > m_idColumns
Definition: MappingTree.h:41
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:10
static std::string fkNameForIdentity(const std::string &tableName, int index=0)