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"
26 std::vector<std::string> columnsForIndex;
27 std::vector<std::string> columnsForFk;
30 for( std::vector<std::string>::const_iterator iCol = tableInfo.
m_idColumns.begin();
32 description.insertColumn( *iCol, coral::AttributeSpecification::typeNameForId(
typeid(
int) ) );
33 description.setNotNullConstraint( *iCol );
35 columnsForIndex.push_back( *iCol );
36 if( i< cols-1 ) columnsForFk.push_back( *iCol );
38 if( i>0 ) columnsForIndex.push_back( *iCol );
39 if( i>0 && i< cols-1 ) columnsForFk.push_back( *iCol );
43 for( std::map<std::string,std::string>::const_iterator iDataCol = tableInfo.
m_dataColumns.begin();
45 description.insertColumn( iDataCol->first, iDataCol->second );
47 std::set<std::string>::const_iterator iNullable = tableInfo.
m_nullableColumns.find( iDataCol->first );
49 description.setNotNullConstraint( iDataCol->first );
52 description.setPrimaryKey( columnsForIndex );
59 std::vector<std::string> refCols;
61 if( !refCols.empty() ) description.createForeignKey( fkName, columnsForFk, tableInfo.
m_parentTableName, refCols );
64 m_schema.createTable( description );
70 std::vector<TableInfo> tableList = mapping.
tables();
71 for( std::vector<TableInfo>::iterator iT = tableList.begin();
72 iT != tableList.end(); ++iT ){
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 );
86 for( std::vector<std::string>::const_iterator iCol = iT->m_idColumns.begin();
87 iCol != iT->m_idColumns.end(); ++iCol ){
89 table.description().columnDescription( *iCol );
90 }
catch (
const coral::InvalidColumnNameException&){
92 throwException(
"ID Column \""+*iCol+
"\" has not been found in table \""+iT->m_tableName+
"\" as required in the mapping.",
93 "MappingToSchema::alter");
95 allCols.insert( *iCol );
97 for( std::map<std::string,std::string>::const_iterator iDataCol = iT->m_dataColumns.begin();
98 iDataCol != iT->m_dataColumns.end(); ++iDataCol ){
100 const coral::IColumn& colDescr = table.description().columnDescription( iDataCol->first );
102 if( colDescr.type() != iDataCol->second ){
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");
109 }
catch (
const coral::InvalidColumnNameException&){
110 table.schemaEditor().insertColumn( iDataCol->first, iDataCol->second );
111 table.schemaEditor().setNotNullConstraint( iDataCol->first );
113 allCols.insert( iDataCol->first );
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 );
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 ) ){
std::set< std::string > m_nullableColumns
std::vector< std::string > m_refColumns
std::map< std::string, std::string > m_dataColumns
void createTable(const TableInfo &tableInfo)
std::string m_parentTableName
~MappingToSchema()
Destructor.
bool check(const MappingTree &mapping)
std::vector< TableInfo > tables() const
MappingToSchema(coral::ISchema &schema)
Constructor.
void alter(const MappingTree &mapping)
void create(const MappingTree &mapping)
std::vector< std::string > m_idColumns
void throwException(const std::string &message, const std::string &methodName)
static std::string fkNameForIdentity(const std::string &tableName, int index=0)