Go to the documentation of this file.00001 #include "CondCore/ORA/interface/Exception.h"
00002 #include "MappingGenerator.h"
00003 #include "RelationalMapping.h"
00004 #include "MappingTree.h"
00005 #include "MappingRules.h"
00006
00007 #include "Reflex/Reflex.h"
00008
00009 ora::MappingGenerator::MappingGenerator( coral::ISchema& schema ):
00010 m_schema( schema ),
00011 m_tableRegister( schema ){
00012 }
00013
00014 ora::MappingGenerator::~MappingGenerator(){}
00015
00016 void ora::MappingGenerator::createNewMapping( const std::string& containerName,
00017 const Reflex::Type& classDictionary,
00018 MappingTree& destination ){
00019 std::string className = classDictionary.Name(Reflex::SCOPED);
00020
00021 size_t sz = RelationalMapping::sizeInColumns( classDictionary );
00022 if(sz > MappingRules::MaxColumnsPerTable){
00023 std::stringstream messg;
00024 messg << "Cannot process default mapping for class \"" << className+"\"";
00025 messg << " : number of columns ("<<sz<<") required exceedes maximum="<< MappingRules::MaxColumnsPerTable;
00026 throwException( messg.str(),"MappingGenerator::processClass");
00027 }
00028
00029 std::string tableName = ora::MappingRules::tableNameForItem( containerName );
00030 if(m_tableRegister.checkTable(tableName)){
00031 throwException( "Table \"" +tableName+ "\" already assigned, cannot be allocated for the mapping of class \""+className+"\"",
00032 "MappingGenerator::processClass");
00033 }
00034 m_tableRegister.insertTable(tableName);
00035
00036 MappingElement& topElement = destination.setTopElement( className, tableName );
00037 topElement.setColumnNames( std::vector< std::string >( 1, ora::MappingRules::columnNameForId() ) );
00038 m_tableRegister.insertColumns(tableName, topElement.columnNames() );
00039 RelationalMappingFactory mappingFactory( m_tableRegister );
00040 std::auto_ptr<IRelationalMapping> processor( mappingFactory.newProcessor( classDictionary ) );
00041 std::string nameForSchema = MappingRules::formatName( className, MappingRules::ClassNameLengthForSchema );
00042 std::string scope("");
00043 processor->process( topElement, className, nameForSchema, scope );
00044 }
00045
00046 void ora::MappingGenerator::createNewMapping( const std::string& containerName,
00047 const Reflex::Type& classDictionary,
00048 const MappingTree& baseMapping,
00049 MappingTree& destination ){
00050 createNewMapping( containerName, classDictionary, destination );
00051 if(baseMapping.className()!=destination.className()){
00052 throwException( "Mapping specified as base does not map the target class \"" + destination.className()+"\"",
00053 "MappingGenerator::createNewMapping" );
00054 }
00055 destination.override( baseMapping );
00056 }
00057
00058 void ora::MappingGenerator::createNewDependentMapping( const Reflex::Type& classDictionary,
00059 const MappingTree& parentClassMapping,
00060 MappingTree& destination ){
00061 std::string className = classDictionary.Name(Reflex::SCOPED);
00062
00063 size_t sz = RelationalMapping::sizeInColumns( classDictionary );
00064 if(sz > MappingRules::MaxColumnsPerTable){
00065 std::stringstream messg;
00066 messg << "Cannot process default mapping for class \"" << className+"\"";
00067 messg << " : number of columns ("<<sz<<") required exceedes maximum="<< MappingRules::MaxColumnsPerTable;
00068 throwException( messg.str(),"MappingGenerator::processClass");
00069 }
00070
00071 std::string mainTableName = parentClassMapping.topElement().tableName();
00072 std::string initialTable = mainTableName;
00073 std::string tableName = ora::MappingRules::newNameForDepSchemaObject( initialTable, 0, ora::MappingRules::MaxTableNameLength );
00074 unsigned int i=0;
00075 while(m_tableRegister.checkTable(tableName)){
00076 tableName = ora::MappingRules::newNameForDepSchemaObject( initialTable, i, ora::MappingRules::MaxTableNameLength );
00077 i++;
00078 }
00079 m_tableRegister.insertTable(tableName);
00080
00081 destination.setDependency( parentClassMapping );
00082 ora::MappingElement& classElement = destination.setTopElement( className, tableName, true );
00083
00084 std::vector<std::string> columns;
00085 columns.push_back( ora::MappingRules::columnNameForId() );
00086 columns.push_back( ora::MappingRules::columnNameForRefColumn() );
00087 classElement.setColumnNames( columns );
00088 m_tableRegister.insertColumns(tableName, columns );
00089 RelationalMappingFactory mappingFactory( m_tableRegister );
00090 std::auto_ptr<IRelationalMapping> processor( mappingFactory.newProcessor( classDictionary ) );
00091 std::string nameForSchema = MappingRules::formatName( className, MappingRules::ClassNameLengthForSchema );
00092 std::string scope("");
00093 processor->process( classElement, className, nameForSchema, scope );
00094 }
00095
00096 void ora::MappingGenerator::createNewDependentMapping( const Reflex::Type& classDictionary,
00097 const MappingTree& parentClassMapping,
00098 const MappingTree& dependentClassBaseMapping,
00099 MappingTree& destination ){
00100 createNewDependentMapping( classDictionary, parentClassMapping, destination );
00101 if(dependentClassBaseMapping.className()!=destination.className()){
00102 throwException( "Mapping specified as base does not map the target class \"" + destination.className()+"\"",
00103 "MappingGenerator::createNewDependentMapping" );
00104 }
00105 destination.override( dependentClassBaseMapping );
00106 }
00107