CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/CondCore/ORA/src/MappingToSchema.cc

Go to the documentation of this file.
00001 #include "CondCore/ORA/interface/Exception.h"
00002 #include "MappingToSchema.h"
00003 #include "MappingTree.h"
00004 #include "MappingRules.h"
00005 //
00006 // externals
00007 #include "CoralBase/Blob.h"
00008 #include "CoralBase/AttributeSpecification.h"
00009 #include "RelationalAccess/IColumn.h"
00010 #include "RelationalAccess/ISchema.h"
00011 #include "RelationalAccess/ITable.h"
00012 #include "RelationalAccess/ITablePrivilegeManager.h"
00013 #include "RelationalAccess/SchemaException.h"
00014 #include "RelationalAccess/TableDescription.h"
00015 
00016 ora::MappingToSchema::MappingToSchema( coral::ISchema& schema ):
00017   m_schema( schema ){
00018 }
00019 
00020 ora::MappingToSchema::~MappingToSchema(){
00021 }
00022 
00023 void ora::MappingToSchema::createTable( const TableInfo& tableInfo ){
00024   coral::TableDescription description("ORA");
00025   description.setName(tableInfo.m_tableName);
00026   std::vector<std::string> columnsForIndex;
00027   std::vector<std::string> columnsForFk;
00028   size_t i=0;
00029   size_t cols = tableInfo.m_idColumns.size();
00030   for( std::vector<std::string>::const_iterator iCol = tableInfo.m_idColumns.begin();
00031        iCol != tableInfo.m_idColumns.end(); ++iCol ){
00032     description.insertColumn( *iCol, coral::AttributeSpecification::typeNameForId( typeid(int) ) );
00033     description.setNotNullConstraint( *iCol );
00034     if( !tableInfo.m_dependency ) {
00035       columnsForIndex.push_back( *iCol );
00036       if( i< cols-1 ) columnsForFk.push_back( *iCol );
00037     } else {
00038       if( i>0 ) columnsForIndex.push_back( *iCol );
00039       if( i>0 && i< cols-1 ) columnsForFk.push_back( *iCol );
00040     }
00041     ++i;
00042   }
00043   for( std::map<std::string,std::string>::const_iterator iDataCol = tableInfo.m_dataColumns.begin();
00044        iDataCol != tableInfo.m_dataColumns.end(); ++iDataCol ){
00045     description.insertColumn( iDataCol->first, iDataCol->second );
00046     description.setNotNullConstraint( iDataCol->first );
00047   }
00048   description.setPrimaryKey( columnsForIndex );
00049   if( !tableInfo.m_parentTableName.empty() ){
00050     std::string fkName = MappingRules::fkNameForIdentity( tableInfo.m_tableName );
00051     
00052     if( !tableInfo.m_dependency ) {
00053       description.createForeignKey( fkName, columnsForFk, tableInfo.m_parentTableName, tableInfo.m_refColumns );
00054     } else {
00055       std::vector<std::string> refCols;
00056       for(size_t i=1;i<tableInfo.m_refColumns.size();i++) refCols.push_back( tableInfo.m_refColumns[i] );
00057       if( !refCols.empty() ) description.createForeignKey( fkName, columnsForFk, tableInfo.m_parentTableName, refCols );
00058     }
00059   }
00060   m_schema.createTable( description );
00061   //.privilegeManager().grantToPublic( coral::ITablePrivilegeManager::Select );
00062   
00063 }
00064 
00065 void ora::MappingToSchema::create( const MappingTree& mapping ){
00066   std::vector<TableInfo> tableList = mapping.tables();
00067   for( std::vector<TableInfo>::iterator iT = tableList.begin();
00068        iT != tableList.end(); ++iT ){
00069     createTable( *iT );
00070   }
00071   
00072 }
00073 
00074 void ora::MappingToSchema::alter( const MappingTree& mapping ){
00075   std::vector<TableInfo> tableList = mapping.tables();
00076   for( std::vector<TableInfo>::iterator iT = tableList.begin();
00077        iT != tableList.end(); ++iT ){
00078     if( m_schema.existsTable( iT->m_tableName ) ){
00079       std::set<std::string> allCols;
00080       coral::ITable& table = m_schema.tableHandle( iT->m_tableName );
00081       // check all of the columns
00082       for( std::vector<std::string>::const_iterator iCol = iT->m_idColumns.begin();
00083            iCol != iT->m_idColumns.end(); ++iCol ){
00084         try {
00085           table.description().columnDescription( *iCol );
00086         } catch ( const coral::InvalidColumnNameException&){
00087           // not recoverable: id columns cannot be added.
00088           throwException("ID Column \""+*iCol+"\" has not been found in table \""+iT->m_tableName+"\" as required in the mapping.",
00089                          "MappingToSchema::alter");
00090         }
00091         allCols.insert( *iCol );
00092       }
00093       for( std::map<std::string,std::string>::const_iterator iDataCol = iT->m_dataColumns.begin();
00094            iDataCol != iT->m_dataColumns.end(); ++iDataCol ){
00095         try {
00096           const coral::IColumn& colDescr = table.description().columnDescription( iDataCol->first );
00097           // check the type
00098           if( colDescr.type() != iDataCol->second ){
00099             // not recoverable: column type cannot be changed.
00100             throwException("ID Column \""+iDataCol->first+"\" in table \""+iT->m_tableName+"\" is type \""+colDescr.type()+
00101                            "\" while is required of type \""+iDataCol->second+"\" in the mapping.",
00102                            "MappingToSchema::alter");
00103           }
00104           
00105         } catch ( const coral::InvalidColumnNameException&){
00106           table.schemaEditor().insertColumn( iDataCol->first, iDataCol->second );
00107           table.schemaEditor().setNotNullConstraint( iDataCol->first );
00108         }        
00109         allCols.insert( iDataCol->first );
00110       }
00111       // then check the unused columns for not null constraint
00112       int ncols = table.description().numberOfColumns();
00113       for( int i=0;i<ncols;i++ ){
00114         const coral::IColumn& colDescr = table.description().columnDescription( i );
00115         std::set<std::string>::const_iterator iC = allCols.find( colDescr.name() );
00116         if( iC == allCols.end() ){
00117           table.schemaEditor().setNotNullConstraint( colDescr.name(), false );
00118         }
00119       }
00120     } else {
00121       createTable( *iT );
00122     }
00123   }
00124 }
00125 
00126 bool ora::MappingToSchema::check( const MappingTree& mapping ){
00127   bool ok = true;
00128   std::vector<TableInfo> tableList = mapping.tables();
00129   for( std::vector<TableInfo>::iterator iT = tableList.begin();
00130        iT != tableList.end(); ++iT ){
00131     if( m_schema.existsTable( iT->m_tableName ) ){
00132       ok = false;
00133     }
00134   }
00135   return ok;
00136 }
00137 
00138 
00139