CMS 3D CMS Logo

Public Member Functions | Private Member Functions | Private Attributes

ora::MappingToSchema Class Reference

#include <MappingToSchema.h>

List of all members.

Public Member Functions

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

Private Member Functions

void createTable (const TableInfo &tableInfo)

Private Attributes

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

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.

                                                         :
  m_schema( schema ){
}
ora::MappingToSchema::~MappingToSchema ( )

Destructor.

Definition at line 20 of file MappingToSchema.cc.

                                    {
}

Member Function Documentation

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

Definition at line 78 of file MappingToSchema.cc.

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

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

                                                          {
  std::vector<TableInfo> tableList = mapping.tables();
  for( std::vector<TableInfo>::iterator iT = tableList.begin();
       iT != tableList.end(); ++iT ){
    if( m_schema.existsTable( iT->m_tableName ) ){
      std::set<std::string> allCols;
      coral::ITable& table = m_schema.tableHandle( iT->m_tableName );
      // check all of the columns
      for( std::vector<std::string>::const_iterator iCol = iT->m_idColumns.begin();
           iCol != iT->m_idColumns.end(); ++iCol ){
        try {
          table.description().columnDescription( *iCol );
        } catch ( const coral::InvalidColumnNameException&){
          // not recoverable: id columns cannot be added.
          throwException("ID Column \""+*iCol+"\" has not been found in table \""+iT->m_tableName+"\" as required in the mapping.",
                         "MappingToSchema::alter");
        }
        allCols.insert( *iCol );
      }
      for( std::map<std::string,std::string>::const_iterator iDataCol = iT->m_dataColumns.begin();
           iDataCol != iT->m_dataColumns.end(); ++iDataCol ){
        try {
          const coral::IColumn& colDescr = table.description().columnDescription( iDataCol->first );
          // check the type
          if( colDescr.type() != iDataCol->second ){
            // not recoverable: column type cannot be changed.
            throwException("ID Column \""+iDataCol->first+"\" in table \""+iT->m_tableName+"\" is type \""+colDescr.type()+
                           "\" while is required of type \""+iDataCol->second+"\" in the mapping.",
                           "MappingToSchema::alter");
          }
          
        } catch ( const coral::InvalidColumnNameException&){
          table.schemaEditor().insertColumn( iDataCol->first, iDataCol->second );
          table.schemaEditor().setNotNullConstraint( iDataCol->first );
        }        
        allCols.insert( iDataCol->first );
      }
      // then check the unused columns for not null constraint
      int ncols = table.description().numberOfColumns();
      for( int i=0;i<ncols;i++ ){
        const coral::IColumn& colDescr = table.description().columnDescription( i );
        std::set<std::string>::const_iterator iC = allCols.find( colDescr.name() );
        if( iC == allCols.end() ){
          table.schemaEditor().setNotNullConstraint( colDescr.name(), false );
        }
      }
    } else {
      createTable( *iT );
    }
  }
}
bool ora::MappingToSchema::check ( const MappingTree mapping)

Definition at line 130 of file MappingToSchema.cc.

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

                                                          {
  bool ok = true;
  std::vector<TableInfo> tableList = mapping.tables();
  for( std::vector<TableInfo>::iterator iT = tableList.begin();
       iT != tableList.end(); ++iT ){
    if( m_schema.existsTable( iT->m_tableName ) ){
      ok = false;
    }
  }
  return ok;
}
void ora::MappingToSchema::create ( const MappingTree mapping)

Definition at line 69 of file MappingToSchema.cc.

References ora::MappingTree::tables().

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

                                                           {
  std::vector<TableInfo> tableList = mapping.tables();
  for( std::vector<TableInfo>::iterator iT = tableList.begin();
       iT != tableList.end(); ++iT ){
    createTable( *iT );
  }
  
}
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_nullableColumns, ora::TableInfo::m_parentTableName, ora::TableInfo::m_refColumns, and ora::TableInfo::m_tableName.

                                                                {
  coral::TableDescription description("ORA");
  description.setName(tableInfo.m_tableName);
  std::vector<std::string> columnsForIndex;
  std::vector<std::string> columnsForFk;
  size_t i=0;
  size_t cols = tableInfo.m_idColumns.size();
  for( std::vector<std::string>::const_iterator iCol = tableInfo.m_idColumns.begin();
       iCol != tableInfo.m_idColumns.end(); ++iCol ){
    description.insertColumn( *iCol, coral::AttributeSpecification::typeNameForId( typeid(int) ) );
    description.setNotNullConstraint( *iCol );
    if( !tableInfo.m_dependency ) {
      columnsForIndex.push_back( *iCol );
      if( i< cols-1 ) columnsForFk.push_back( *iCol );
    } else {
      if( i>0 ) columnsForIndex.push_back( *iCol );
      if( i>0 && i< cols-1 ) columnsForFk.push_back( *iCol );
    }
    ++i;
  }
  for( std::map<std::string,std::string>::const_iterator iDataCol = tableInfo.m_dataColumns.begin();
       iDataCol != tableInfo.m_dataColumns.end(); ++iDataCol ){
    description.insertColumn( iDataCol->first, iDataCol->second );
    // workaround (temporary?) for Blob. The metadata column has to be relaxed in the NOT NULL constraint (previous versions are not filling this column)
    std::set<std::string>::const_iterator iNullable = tableInfo.m_nullableColumns.find( iDataCol->first );
    if( iNullable == tableInfo.m_nullableColumns.end()){
      description.setNotNullConstraint( iDataCol->first );
    }
  }
  description.setPrimaryKey( columnsForIndex );
  if( !tableInfo.m_parentTableName.empty() ){
    std::string fkName = MappingRules::fkNameForIdentity( tableInfo.m_tableName );
    
    if( !tableInfo.m_dependency ) {
      description.createForeignKey( fkName, columnsForFk, tableInfo.m_parentTableName, tableInfo.m_refColumns );
    } else {
      std::vector<std::string> refCols;
      for(size_t i=1;i<tableInfo.m_refColumns.size();i++) refCols.push_back( tableInfo.m_refColumns[i] );
      if( !refCols.empty() ) description.createForeignKey( fkName, columnsForFk, tableInfo.m_parentTableName, refCols );
    }
  }
  m_schema.createTable( description );
  //.privilegeManager().grantToPublic( coral::ITablePrivilegeManager::Select );
  
}

Member Data Documentation

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

Reference to the schema in use.

Definition at line 39 of file MappingToSchema.h.