CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/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 ).privilegeManager().grantToPublic( coral::ITablePrivilegeManager::Select );
00061   
00062 }
00063 
00064 void ora::MappingToSchema::create( const MappingTree& mapping ){
00065   std::vector<TableInfo> tableList = mapping.tables();
00066   for( std::vector<TableInfo>::iterator iT = tableList.begin();
00067        iT != tableList.end(); ++iT ){
00068     createTable( *iT );
00069   }
00070   
00071 }
00072 
00073 void ora::MappingToSchema::alter( const MappingTree& mapping ){
00074   std::vector<TableInfo> tableList = mapping.tables();
00075   for( std::vector<TableInfo>::iterator iT = tableList.begin();
00076        iT != tableList.end(); ++iT ){
00077     if( m_schema.existsTable( iT->m_tableName ) ){
00078       std::set<std::string> allCols;
00079       coral::ITable& table = m_schema.tableHandle( iT->m_tableName );
00080       // check all of the columns
00081       for( std::vector<std::string>::const_iterator iCol = iT->m_idColumns.begin();
00082            iCol != iT->m_idColumns.end(); ++iCol ){
00083         try {
00084           table.description().columnDescription( *iCol );
00085         } catch ( const coral::InvalidColumnNameException&){
00086           // not recoverable: id columns cannot be added.
00087           throwException("ID Column \""+*iCol+"\" has not been found in table \""+iT->m_tableName+"\" as required in the mapping.",
00088                          "MappingToSchema::alter");
00089         }
00090         allCols.insert( *iCol );
00091       }
00092       for( std::map<std::string,std::string>::const_iterator iDataCol = iT->m_dataColumns.begin();
00093            iDataCol != iT->m_dataColumns.end(); ++iDataCol ){
00094         try {
00095           const coral::IColumn& colDescr = table.description().columnDescription( iDataCol->first );
00096           // check the type
00097           if( colDescr.type() != iDataCol->second ){
00098             // not recoverable: column type cannot be changed.
00099             throwException("ID Column \""+iDataCol->first+"\" in table \""+iT->m_tableName+"\" is type \""+colDescr.type()+
00100                            "\" while is required of type \""+iDataCol->second+"\" in the mapping.",
00101                            "MappingToSchema::alter");
00102           }
00103           
00104         } catch ( const coral::InvalidColumnNameException&){
00105           table.schemaEditor().insertColumn( iDataCol->first, iDataCol->second );
00106           table.schemaEditor().setNotNullConstraint( iDataCol->first );
00107         }        
00108         allCols.insert( iDataCol->first );
00109       }
00110       // then check the unused columns for not null constraint
00111       int ncols = table.description().numberOfColumns();
00112       for( int i=0;i<ncols;i++ ){
00113         const coral::IColumn& colDescr = table.description().columnDescription( i );
00114         std::set<std::string>::const_iterator iC = allCols.find( colDescr.name() );
00115         if( iC == allCols.end() ){
00116           table.schemaEditor().setNotNullConstraint( colDescr.name(), false );
00117         }
00118       }
00119     } else {
00120       createTable( *iT );
00121     }
00122   }
00123 }
00124 
00125 bool ora::MappingToSchema::check( const MappingTree& mapping ){
00126   bool ok = true;
00127   std::vector<TableInfo> tableList = mapping.tables();
00128   for( std::vector<TableInfo>::iterator iT = tableList.begin();
00129        iT != tableList.end(); ++iT ){
00130     if( m_schema.existsTable( iT->m_tableName ) ){
00131       ok = false;
00132     }
00133   }
00134   return ok;
00135 }
00136 
00137 
00138