00001 #include "CondCore/ORA/interface/Exception.h"
00002 #include "CondCore/ORA/interface/PoolToken.h"
00003 #include "PoolDatabaseSchema.h"
00004 #include "MappingRules.h"
00005 #include "MappingElement.h"
00006
00007 #include <memory>
00008
00009 #include "RelationalAccess/ISchema.h"
00010 #include "RelationalAccess/ITable.h"
00011 #include "RelationalAccess/IQuery.h"
00012 #include "RelationalAccess/ICursor.h"
00013 #include "RelationalAccess/TableDescription.h"
00014 #include "RelationalAccess/ITablePrivilegeManager.h"
00015 #include "RelationalAccess/ITableDataEditor.h"
00016 #include "RelationalAccess/IBulkOperation.h"
00017 #include "CoralBase/Attribute.h"
00018
00019 namespace ora {
00020 }
00021
00022 std::string ora::PoolMainTable::tableName(){
00023 static std::string s_name("POOL_RSS_DB");
00024 return s_name;
00025 }
00026
00027 ora::PoolMainTable::PoolMainTable( coral::ISchema& dbSchema ):
00028 m_schema( dbSchema ){
00029 }
00030
00031 ora::PoolMainTable::~PoolMainTable(){
00032 }
00033
00034 bool ora::PoolMainTable::getParameters( std::map<std::string,std::string>& ){
00035 return false;
00036 }
00037
00038 std::string ora::PoolMainTable::schemaVersion(){
00039 return poolSchemaVersion();
00040 }
00041
00042 bool ora::PoolMainTable::exists(){
00043 return m_schema.existsTable( tableName() );
00044 }
00045
00046 void ora::PoolMainTable::create(){
00047 if( m_schema.existsTable( tableName() )){
00048 throwException( "POOL database main table already exists in this schema.",
00049 "PoolMainTable::create");
00050 }
00051 throwException( "POOL database cannot be created.","PoolMainTable::create");
00052 }
00053
00054 void ora::PoolMainTable::drop(){
00055 m_schema.dropIfExistsTable( tableName() );
00056 }
00057
00058 std::string ora::PoolSequenceTable::tableName(){
00059 static std::string s_name("POOL_RSS_SEQ");
00060 return s_name;
00061 }
00062
00063 std::string ora::PoolSequenceTable::sequenceNameColumn(){
00064 static std::string s_column("NAME");
00065 return s_column;
00066 }
00067
00068 std::string ora::PoolSequenceTable::sequenceValueColumn(){
00069 static std::string s_column("VALUE");
00070 return s_column;
00071 }
00072
00073 ora::PoolSequenceTable::PoolSequenceTable( coral::ISchema& schema ):
00074 m_schema( schema),
00075 m_dbCache( 0 ){
00076 }
00077
00078 ora::PoolSequenceTable::~PoolSequenceTable(){
00079 }
00080
00081 void ora::PoolSequenceTable::init( PoolDbCache& dbCache ){
00082 m_dbCache = &dbCache;
00083 }
00084
00085 bool
00086 ora::PoolSequenceTable::add( const std::string& sequenceName ){
00087
00088 coral::AttributeList insertData;
00089 insertData.extend<std::string>(sequenceNameColumn());
00090 insertData.extend<int>(sequenceValueColumn());
00091 coral::AttributeList::iterator iAttribute = insertData.begin();
00092 iAttribute->data< std::string >() = sequenceName;
00093 ++iAttribute;
00094 iAttribute->data< int >() = 0;
00095 m_schema.tableHandle( tableName() ).dataEditor().insertRow( insertData );
00096 return true;
00097 }
00098
00099 bool
00100 ora::PoolSequenceTable::getLastId( const std::string& sequenceName,
00101 int& lastId ) {
00102 if(!m_dbCache){
00103 throwException("Sequence Table handle has not been initialized.","PoolSequenceTable::getLastId");
00104 }
00105
00106
00107 std::map<std::string,PoolDbCacheData*>& seq = m_dbCache->sequences();
00108 std::map<std::string,PoolDbCacheData*>::iterator iS = seq.find( sequenceName );
00109 if( iS != seq.end()){
00110 if( iS->second->m_nobjWr == 0 ) return false;
00111 lastId = iS->second->m_nobjWr-1;
00112 return true;
00113 }
00114
00115
00116 std::auto_ptr< coral::IQuery > query( m_schema.tableHandle( tableName() ).newQuery() );
00117 query->limitReturnedRows( 1, 0 );
00118 query->addToOutputList( sequenceValueColumn() );
00119 query->defineOutputType( sequenceValueColumn(), coral::AttributeSpecification::typeNameForType<int>() );
00120 query->setForUpdate();
00121 std::string whereClause( sequenceNameColumn() + " = :" + sequenceNameColumn() );
00122 coral::AttributeList rowData;
00123 rowData.extend<std::string>(sequenceNameColumn());
00124 rowData.begin()->data< std::string >() = sequenceName;
00125 query->setCondition( whereClause, rowData );
00126 coral::ICursor& cursor = query->execute();
00127 if ( cursor.next() ) {
00128 lastId = cursor.currentRow().begin()->data<int >();
00129 return true;
00130 }
00131 return false;
00132 }
00133
00134 void ora::PoolSequenceTable::sinchronize( const std::string& sequenceName,
00135 int lastValue ){
00136 if(!m_dbCache){
00137 throwException("Sequence Table handle has not been initialized.","PoolSequenceTable::sinchronize");
00138 }
00139
00140 std::map<std::string,PoolDbCacheData*>& seq = m_dbCache->sequences();
00141 std::map<std::string,PoolDbCacheData*>::iterator iS = seq.find( sequenceName );
00142 if( iS != seq.end()){
00143 iS->second->m_nobjWr = lastValue+1;
00144 return;
00145 }
00146
00147 coral::AttributeList updateData;
00148 updateData.extend<std::string>(sequenceNameColumn());
00149 updateData.extend<int>(sequenceValueColumn());
00150 std::string setClause( sequenceValueColumn() + " = :" + sequenceValueColumn() );
00151 std::string whereClause( sequenceNameColumn() + " = :" + sequenceNameColumn() );
00152
00153 coral::AttributeList::iterator iAttribute = updateData.begin();
00154 iAttribute->data< std::string >() = sequenceName;
00155 ++iAttribute;
00156 iAttribute->data< int >() = lastValue;
00157 m_schema.tableHandle( tableName() ).dataEditor().updateRows( setClause,whereClause,updateData );
00158 }
00159
00160 void ora::PoolSequenceTable::erase( const std::string& sequenceName ){
00161 coral::AttributeList whereData;
00162 whereData.extend<std::string>(sequenceNameColumn());
00163 whereData[ sequenceNameColumn() ].data<std::string>() = sequenceName;
00164 std::string whereClause( sequenceNameColumn() + " = :" + sequenceNameColumn() );
00165 m_schema.tableHandle( tableName() ).dataEditor().deleteRows( whereClause, whereData );
00166 }
00167
00168 bool ora::PoolSequenceTable::exists(){
00169 if(!m_dbCache){
00170 throwException("Sequence Table handle has not been initialized.","PoolSequenceTable::exists");
00171 }
00172
00173 return m_schema.existsTable( tableName() );
00174 }
00175
00176 void ora::PoolSequenceTable::create(){
00177 if( m_schema.existsTable( tableName() )){
00178 throwException( "POOL database sequence table already exists in this schema.",
00179 "PoolSequenceTable::create");
00180 }
00181 throwException( "POOL database cannot be created.","PoolSequenceTable::create");
00182 }
00183
00184 void ora::PoolSequenceTable::drop(){
00185 m_schema.dropIfExistsTable( tableName() );
00186 }
00187
00188 std::string ora::PoolMappingVersionTable::tableName(){
00189 static std::string s_table("POOL_OR_MAPPING_VERSIONS");
00190 return s_table;
00191 }
00192
00193 std::string ora::PoolMappingVersionTable::mappingVersionColumn(){
00194 static std::string s_col("MAPPING_VERSION");
00195 return s_col;
00196 }
00197
00198 std::string ora::PoolMappingVersionTable::containerNameColumn(){
00199 static std::string s_col("CONTAINER_ID");
00200 return s_col;
00201 }
00202
00203 ora::PoolMappingVersionTable::PoolMappingVersionTable( coral::ISchema& dbSchema ):
00204 m_schema( dbSchema ){
00205 }
00206
00207 ora::PoolMappingVersionTable::~PoolMappingVersionTable(){
00208 }
00209
00210 bool ora::PoolMappingVersionTable::exists(){
00211 return m_schema.existsTable( tableName() );
00212 }
00213
00214 void ora::PoolMappingVersionTable::create(){
00215 if( m_schema.existsTable( tableName() )){
00216 throwException( "POOL database mapping version table already exists in this schema.",
00217 "PoolMappingVersionTable::create");
00218 }
00219 throwException( "POOL database cannot be created.","PoolMappingVersionTable::create");
00220 }
00221
00222 void ora::PoolMappingVersionTable::drop(){
00223 m_schema.dropIfExistsTable( tableName() );
00224 }
00225
00226
00227 std::string ora::PoolMappingElementTable::tableName(){
00228 static std::string s_table("POOL_OR_MAPPING_ELEMENTS");
00229 return s_table;
00230 }
00231
00232 std::string ora::PoolMappingElementTable::mappingVersionColumn(){
00233 static std::string s_col("MAPPING_VERSION");
00234 return s_col;
00235 }
00236
00237 std::string ora::PoolMappingElementTable::elementIdColumn(){
00238 static std::string s_col("ELEMENT_ID");
00239 return s_col;
00240 }
00241
00242 std::string ora::PoolMappingElementTable::elementTypeColumn(){
00243 static std::string s_col("ELEMENT_TYPE");
00244 return s_col;
00245 }
00246
00247 std::string ora::PoolMappingElementTable::scopeNameColumn(){
00248 static std::string s_col("VARIABLE_SCOPE");
00249 return s_col;
00250 }
00251
00252 std::string ora::PoolMappingElementTable::variableNameColumn(){
00253 static std::string s_col("VARIABLE_NAME");
00254 return s_col;
00255 }
00256
00257 std::string ora::PoolMappingElementTable::variableParIndexColumn(){
00258 static std::string s_col("VARIABLE_PAR_INDEX");
00259 return s_col;
00260 }
00261
00262 std::string ora::PoolMappingElementTable::variableTypeColumn(){
00263 static std::string s_col("VARIABLE_TYPE");
00264 return s_col;
00265 }
00266
00267 std::string ora::PoolMappingElementTable::tableNameColumn(){
00268 static std::string s_col("TABLE_NAME");
00269 return s_col;
00270 }
00271
00272 std::string ora::PoolMappingElementTable::columnNameColumn(){
00273 static std::string s_col("COLUMN_NAME");
00274 return s_col;
00275 }
00276
00277 ora::PoolMappingElementTable::PoolMappingElementTable( coral::ISchema& dbSchema ):
00278 m_schema( dbSchema ){
00279 }
00280
00281 ora::PoolMappingElementTable::~PoolMappingElementTable(){
00282 }
00283
00284 bool ora::PoolMappingElementTable::exists(){
00285 return m_schema.existsTable( tableName() );
00286 }
00287
00288 void ora::PoolMappingElementTable::create(){
00289 if( m_schema.existsTable( tableName() )){
00290 throwException( "POOL database mapping element table already exists in this schema.",
00291 "PoolMappingElementTable::create");
00292 }
00293 throwException( "POOL database cannot be created.","PoolMappingElementTable::create");
00294 }
00295
00296 void ora::PoolMappingElementTable::drop(){
00297 m_schema.dropIfExistsTable( tableName() );
00298 }
00299
00300 ora::PoolDbCacheData::PoolDbCacheData():
00301 m_id( 0 ),
00302 m_name("" ),
00303 m_className("" ),
00304 m_mappingVersion( "" ),
00305 m_nobjWr( 0 ){
00306 }
00307
00308 ora::PoolDbCacheData::PoolDbCacheData( int id,
00309 const std::string& name,
00310 const std::string& className,
00311 const std::string& mappingVersion,
00312 unsigned int nobjWr ):
00313 m_id( id ),
00314 m_name( name ),
00315 m_className( className ),
00316 m_mappingVersion( mappingVersion ),
00317 m_nobjWr( nobjWr ){
00318 }
00319
00320 ora::PoolDbCacheData::~PoolDbCacheData(){
00321 }
00322
00323 ora::PoolDbCacheData::PoolDbCacheData( const ora::PoolDbCacheData& rhs ):
00324 m_id( rhs.m_id ),
00325 m_name( rhs.m_name ),
00326 m_className( rhs.m_className ),
00327 m_mappingVersion( rhs.m_mappingVersion ),
00328 m_nobjWr( rhs.m_nobjWr ){
00329 }
00330
00331 ora::PoolDbCacheData& ora::PoolDbCacheData::operator=( const ora::PoolDbCacheData& rhs ){
00332 m_id = rhs.m_id;
00333 m_name = rhs.m_name;
00334 m_className = rhs.m_className;
00335 m_mappingVersion = rhs.m_mappingVersion;
00336 m_nobjWr = rhs.m_nobjWr;
00337 return *this;
00338 }
00339
00340 ora::PoolDbCache::PoolDbCache():
00341 m_databaseData(),
00342 m_mappingData(),
00343 m_idMap(),
00344 m_sequences(){
00345 m_databaseData.m_nobjWr = 1;
00346 m_mappingData.m_nobjWr = 1;
00347 }
00348
00349 ora::PoolDbCache::~PoolDbCache(){
00350 }
00351
00352 void ora::PoolDbCache::add( int id, const PoolDbCacheData& data ){
00353 std::map<int,PoolDbCacheData >::iterator iData = m_idMap.insert( std::make_pair( id, data )).first;
00354 std::map<std::string,PoolDbCacheData*>::iterator iS = m_sequences.find( MappingRules::sequenceNameForContainerId() );
00355 if( iS == m_sequences.end() ){
00356 throwException( "ContainerId Sequence is empty","PoolDbCache::add");
00357 }
00358 if( id > (int)iS->second->m_nobjWr ){
00359 iS->second->m_nobjWr = id;
00360 }
00361 m_sequences.insert( std::make_pair( MappingRules::sequenceNameForContainer( data.m_name ),&iData->second ) );
00362 }
00363
00364 const std::string& ora::PoolDbCache::nameById( int id ){
00365 PoolDbCacheData& data = find( id );
00366 return data.m_name;
00367 }
00368
00369 int ora::PoolDbCache::idByName( const std::string& name ){
00370 int ret = -1;
00371 for(std::map<int,PoolDbCacheData >::const_iterator iData = m_idMap.begin();
00372 iData != m_idMap.end(); iData++ ){
00373 if( iData->second.m_name == name ){
00374 ret = iData->first;
00375 break;
00376 }
00377 }
00378 return ret;
00379 }
00380
00381 ora::PoolDbCacheData& ora::PoolDbCache::find( int id ){
00382 std::map<int,PoolDbCacheData >::iterator iC = m_idMap.find( id );
00383 if( iC == m_idMap.end() ){
00384 throwException("Container has not been found in the cache.","PoolDbCache::find");
00385 }
00386 return iC->second;
00387 }
00388
00389 void ora::PoolDbCache::remove( int id ){
00390 std::string name = find( id ).m_name;
00391 m_sequences.erase( MappingRules::sequenceNameForContainer( name ) );
00392 m_idMap.erase( id );
00393 }
00394
00395 std::map<std::string,ora::PoolDbCacheData*>& ora::PoolDbCache::sequences(){
00396 return m_sequences;
00397 }
00398
00399 void ora::PoolDbCache::clear(){
00400 m_sequences.clear();
00401 m_idMap.clear();
00402 m_sequences.insert(std::make_pair( MappingRules::sequenceNameForContainerId(), &m_databaseData ) );
00403 m_sequences.insert(std::make_pair( MappingRules::sequenceNameForMapping(), &m_mappingData ) );
00404 }
00405
00406 std::string ora::PoolContainerHeaderTable::tableName(){
00407 static std::string s_name("POOL_RSS_CONTAINERS");
00408 return s_name;
00409 }
00410
00411
00412 std::string ora::PoolContainerHeaderTable::containerIdColumn(){
00413 static std::string s_column("CONTAINER_ID");
00414 return s_column;
00415 }
00416
00417
00418 std::string ora::PoolContainerHeaderTable::containerNameColumn(){
00419 static std::string s_column("CONTAINER_NAME");
00420 return s_column;
00421 }
00422
00423 std::string ora::PoolContainerHeaderTable::containerTypeColumn(){
00424 static std::string s_column("CONTAINER_TYPE");
00425 return s_column;
00426 }
00427
00428 std::string ora::PoolContainerHeaderTable::tableNameColumn(){
00429 static std::string s_column("TABLE_NAME");
00430 return s_column;
00431 }
00432
00433 std::string ora::PoolContainerHeaderTable::classNameColumn(){
00434 static std::string s_column("CLASS_NAME");
00435 return s_column;
00436 }
00437
00438 std::string ora::PoolContainerHeaderTable::baseMappingVersionColumn(){
00439 static std::string s_column("MAPPING_VERSION");
00440 return s_column;
00441 }
00442
00443 std::string ora::PoolContainerHeaderTable::numberOfWrittenObjectsColumn(){
00444 static std::string s_column("NUMBER_OF_WRITTEN_OBJECTS");
00445 return s_column;
00446 }
00447
00448 std::string ora::PoolContainerHeaderTable::numberOfDeletedObjectsColumn(){
00449 static std::string s_column("NUMBER_OF_DELETED_OBJECTS");
00450 return s_column;
00451 }
00452
00453 std::string ora::PoolContainerHeaderTable::homogeneousContainerType(){
00454 static std::string s_type("Homogeneous");
00455 return s_type;
00456 }
00457
00458 ora::PoolContainerHeaderTable::PoolContainerHeaderTable( coral::ISchema& dbSchema ):
00459 m_schema(dbSchema),
00460 m_dbCache( 0 ){
00461 }
00462
00463 ora::PoolContainerHeaderTable::~PoolContainerHeaderTable(){
00464 }
00465
00466 void ora::PoolContainerHeaderTable::init( PoolDbCache& dbCache ){
00467 m_dbCache = &dbCache;
00468 }
00469
00470 bool ora::PoolContainerHeaderTable::getContainerData( std::map<std::string,
00471 ora::ContainerHeaderData>& dest ){
00472 if(!m_dbCache){
00473 throwException("Container Table handle has not been initialized.","PoolContainerHeaderTable::getContainerData");
00474 }
00475 bool ret = false;
00476 m_dbCache->clear();
00477 coral::ITable& containerTable = m_schema.tableHandle( tableName() );
00478 std::auto_ptr<coral::IQuery> query( containerTable.newQuery());
00479 coral::AttributeList outputBuffer;
00480 outputBuffer.extend<int>( containerIdColumn() );
00481 outputBuffer.extend<std::string>( containerNameColumn() );
00482 outputBuffer.extend<std::string>( classNameColumn() );
00483 outputBuffer.extend<std::string>( baseMappingVersionColumn() );
00484 outputBuffer.extend<unsigned int>( numberOfWrittenObjectsColumn() );
00485 outputBuffer.extend<unsigned int>( numberOfDeletedObjectsColumn() );
00486 query->defineOutput( outputBuffer );
00487 query->addToOutputList( containerIdColumn() );
00488 query->addToOutputList( containerNameColumn() );
00489 query->addToOutputList( classNameColumn() );
00490 query->addToOutputList( baseMappingVersionColumn() );
00491 query->addToOutputList( numberOfWrittenObjectsColumn() );
00492 query->addToOutputList( numberOfDeletedObjectsColumn() );
00493 std::stringstream condition;
00494 condition << containerTypeColumn()<<" = :"<<containerTypeColumn();
00495 coral::AttributeList condData;
00496 condData.extend<std::string>( containerTypeColumn() );
00497 condData[ containerTypeColumn() ].data<std::string>()=homogeneousContainerType();
00498 query->setCondition( condition.str(), condData );
00499 coral::ICursor& cursor = query->execute();
00500 while ( cursor.next() ) {
00501 ret = true;
00502 const coral::AttributeList& row = cursor.currentRow();
00503 int containerId = row[ containerIdColumn() ].data< int >() - 1;
00504 std::string containerName = row[ containerNameColumn()].data< std::string >();
00505 std::string className = row[ classNameColumn()].data< std::string >();
00506 std::string baseMappingVersion = row[ baseMappingVersionColumn()].data< std::string >();
00507 unsigned int numberOfWrittenObjects = row[ numberOfWrittenObjectsColumn()].data< unsigned int >();
00508 unsigned int numberOfDeletedObjects = row[ numberOfDeletedObjectsColumn()].data< unsigned int >();
00509
00510 dest.insert( std::make_pair( containerName, ContainerHeaderData( containerId, className,
00511 numberOfWrittenObjects-numberOfDeletedObjects ) )) ;
00512 m_dbCache->add( containerId, PoolDbCacheData(containerId, containerName, className, baseMappingVersion, numberOfWrittenObjects) );
00513 }
00514 return ret;
00515 }
00516
00517 void ora::PoolContainerHeaderTable::addContainer( int containerId,
00518 const std::string& containerName,
00519 const std::string& className ){
00545 throwException( "Cannot create new Containers into POOL database.","PoolContainerHeaderTable::addContainer");
00546 }
00547
00548 void ora::PoolContainerHeaderTable::removeContainer( int id ){
00549 if(!m_dbCache){
00550 throwException("Container Table handle has not been initialized.","PoolContainerHeaderTable::removeContainer");
00551 }
00552 m_dbCache->remove( id );
00553 std::stringstream whereClause;
00554 whereClause << containerIdColumn() << "= :" <<containerIdColumn();
00555 coral::AttributeList whereData;
00556 whereData.extend< int >( containerIdColumn() );
00557 whereData.begin()->data< int >() = id + 1;
00558 coral::ITable& containerTable = m_schema.tableHandle( tableName() );
00559 containerTable.dataEditor().deleteRows(whereClause.str(),whereData);
00560 }
00561
00562 void ora::PoolContainerHeaderTable::incrementNumberOfObjects( int containerId ){
00563 throwException( "Operation not supported into POOL database.","PoolContainerHeaderTable::incrementNumberOfObjects");
00564 }
00565
00566 void ora::PoolContainerHeaderTable::decrementNumberOfObjects( int containerId ){
00567 throwException( "Operation not supported into POOL database.","PoolContainerHeaderTable::decrementNumberOfObjects");
00568 }
00569
00570 void ora::PoolContainerHeaderTable::updateNumberOfObjects( const std::map<int,unsigned int>& numberOfObjectsForContainerIds ){
00571 if( numberOfObjectsForContainerIds.size() ){
00572
00573 if(!m_dbCache){
00574 throwException("Container Table handle has not been initialized.","PoolContainerHeaderTable::updateNumberOfObjects");
00575 }
00576
00577 std::stringstream whereClause;
00578 whereClause << containerIdColumn() << " = :" <<containerIdColumn();
00579 std::stringstream setClause;
00580 setClause << numberOfWrittenObjectsColumn()<< " = :"<<numberOfWrittenObjectsColumn();
00581 setClause << " , "<< numberOfDeletedObjectsColumn()<< " = :"<<numberOfDeletedObjectsColumn();
00582 coral::AttributeList updateData;
00583 updateData.extend<unsigned int>( numberOfWrittenObjectsColumn() );
00584 updateData.extend<unsigned int>( numberOfDeletedObjectsColumn() );
00585 updateData.extend<int>( containerIdColumn() );
00586
00587 coral::ITable& containerTable = m_schema.tableHandle( tableName() );
00588 std::auto_ptr<coral::IBulkOperation> bulkUpdate( containerTable.dataEditor().bulkUpdateRows( setClause.str(), whereClause.str(), updateData,(int)numberOfObjectsForContainerIds.size()+1));
00589
00590 for( std::map<int,unsigned int>::const_iterator iCont = numberOfObjectsForContainerIds.begin();
00591 iCont != numberOfObjectsForContainerIds.end(); ++iCont ){
00592
00593 PoolDbCacheData& contData = m_dbCache->find( iCont->first );
00594 unsigned int nwrt = contData.m_nobjWr;
00595 unsigned int ndel = nwrt-iCont->second;
00596
00597 updateData[containerIdColumn()].data<int>() = iCont->first + 1;
00598 updateData[numberOfWrittenObjectsColumn()].data<unsigned int>() = nwrt;
00599 updateData[numberOfDeletedObjectsColumn()].data<unsigned int>() = ndel;
00600 bulkUpdate->processNextIteration();
00601
00602 }
00603 bulkUpdate->flush();
00604 }
00605 }
00606
00607 bool ora::PoolContainerHeaderTable::exists(){
00608 return m_schema.existsTable( tableName() );
00609 }
00610
00611 void ora::PoolContainerHeaderTable::create(){
00612 if( m_schema.existsTable( tableName() )){
00613 throwException( "POOL database container header table already exists in this schema.",
00614 "PoolContainerHeaderTable::create");
00615 }
00616 throwException( "POOL database cannot be created.","PoolContainerHeaderTable::create");
00617 }
00618
00619 void ora::PoolContainerHeaderTable::drop(){
00620 m_schema.dropIfExistsTable( tableName() );
00621 }
00622
00623 std::string ora::PoolClassVersionTable::tableName(){
00624 static std::string s_table("POOL_OR_CLASS_VERSIONS");
00625 return s_table;
00626 }
00627
00628 std::string ora::PoolClassVersionTable::classVersionColumn(){
00629 static std::string s_col("CLASS_VERSION");
00630 return s_col;
00631 }
00632
00633 std::string ora::PoolClassVersionTable::containerNameColumn(){
00634 static std::string s_col("CONTAINER_ID");
00635 return s_col;
00636
00637 }
00638
00639 std::string ora::PoolClassVersionTable::mappingVersionColumn(){
00640 static std::string s_col("MAPPING_VERSION");
00641 return s_col;
00642 }
00643
00644 ora::PoolClassVersionTable::PoolClassVersionTable( coral::ISchema& dbSchema ):
00645 m_schema( dbSchema ){
00646 }
00647
00648 ora::PoolClassVersionTable::~PoolClassVersionTable(){
00649 }
00650
00651 bool ora::PoolClassVersionTable::exists(){
00652 return m_schema.existsTable( tableName() );
00653 }
00654
00655 void ora::PoolClassVersionTable::create(){
00656 if( m_schema.existsTable( tableName() )){
00657 throwException( "POOL database class version table already exists in this schema.",
00658 "PoolClassVersionTable::create");
00659 }
00660 throwException( "POOL database cannot be created.","PoolClassVersionTable::create");
00661 }
00662
00663 void ora::PoolClassVersionTable::drop(){
00664 m_schema.dropIfExistsTable( tableName() );
00665 }
00666
00667 namespace ora {
00668 std::string mappingTypeFromPool( const std::string& mappingType ){
00669 if( mappingType == "PoolArray" ) return MappingElement::OraArrayMappingElementType();
00670 return mappingType;
00671 }
00672
00673 std::string variableNameFromPool( const std::string& variableName ){
00674 size_t ind = variableName.find("pool::PVector");
00675 if( ind != std::string::npos ){
00676 return "ora::PVector"+variableName.substr(13);
00677 }
00678 return variableName;
00679 }
00680 }
00681
00682 std::string ora::PoolMappingSchema::emptyScope(){
00683 static std::string s_scope(" ");
00684 return s_scope;
00685 }
00686
00687 ora::PoolMappingSchema::PoolMappingSchema( coral::ISchema& dbSchema ):
00688 m_schema( dbSchema ),
00689 m_dbCache( 0 ){
00690 }
00691
00692 ora::PoolMappingSchema::~PoolMappingSchema(){
00693 }
00694
00695 void ora::PoolMappingSchema::init( PoolDbCache& dbCache ){
00696 m_dbCache = &dbCache;
00697 }
00698
00699 bool ora::PoolMappingSchema::getVersionList( std::set<std::string>& dest ){
00700 bool ret = false;
00701 std::auto_ptr<coral::IQuery> query( m_schema.tableHandle( PoolMappingVersionTable::tableName() ).newQuery() );
00702 query->addToOutputList( PoolMappingVersionTable::mappingVersionColumn() );
00703 coral::ICursor& cursor = query->execute();
00704 while ( cursor.next() ) {
00705 ret = true;
00706 const coral::AttributeList& currentRow = cursor.currentRow();
00707 std::string mappingVersion = currentRow[ PoolMappingVersionTable::mappingVersionColumn()].data<std::string>();
00708 dest.insert( mappingVersion );
00709 }
00710 return ret;
00711 }
00712
00713 namespace ora {
00714
00715 void rebuildPoolMapping( const std::string& scope, const std::string& extraScope, const std::map<std::string, std::vector<MappingRawElement> >& elementsByScope, ora::MappingRawData& dest, int& counter ){
00716 std::map<std::string, std::vector<MappingRawElement> >::const_iterator iSc = elementsByScope.find( scope );
00717 if( iSc != elementsByScope.end() ){
00718 for( std::vector<MappingRawElement>::const_iterator iMap = iSc->second.begin();
00719 iMap != iSc->second.end(); ++iMap ){
00720 MappingRawElement& elem = dest.addElement( counter ) = *iMap;
00721 elem.scopeName = extraScope+"::"+iMap->scopeName;
00722 counter++;
00723 rebuildPoolMapping( scope+"::"+iMap->variableName, extraScope, elementsByScope, dest, counter );
00724 }
00725 }
00726 }
00727 }
00728
00729 bool ora::PoolMappingSchema::getMapping( const std::string& version,
00730 ora::MappingRawData& dest ){
00731 bool ret = false;
00732 coral::ITable& mappingTable = m_schema.tableHandle( PoolMappingElementTable::tableName() );
00733 std::auto_ptr<coral::IQuery> query(mappingTable.newQuery());
00734 coral::AttributeList outputBuffer;
00735 outputBuffer.extend<std::string>( PoolMappingElementTable::elementTypeColumn() );
00736 outputBuffer.extend<std::string>( PoolMappingElementTable::scopeNameColumn() );
00737 outputBuffer.extend<std::string>( PoolMappingElementTable::variableNameColumn() );
00738 outputBuffer.extend<std::string>( PoolMappingElementTable::variableTypeColumn() );
00739 outputBuffer.extend<std::string>( PoolMappingElementTable::tableNameColumn() );
00740 outputBuffer.extend<std::string>( PoolMappingElementTable::columnNameColumn() );
00741 query->defineOutput( outputBuffer );
00742 query->addToOutputList( PoolMappingElementTable::elementTypeColumn() );
00743 query->addToOutputList( PoolMappingElementTable::scopeNameColumn() );
00744 query->addToOutputList( PoolMappingElementTable::variableNameColumn() );
00745 query->addToOutputList( PoolMappingElementTable::variableTypeColumn() );
00746 query->addToOutputList( PoolMappingElementTable::tableNameColumn() );
00747 query->addToOutputList( PoolMappingElementTable::columnNameColumn() );
00748 std::ostringstream condition;
00749 condition << PoolMappingElementTable::mappingVersionColumn()<<"= :"<< PoolMappingElementTable::mappingVersionColumn();
00750 coral::AttributeList condData;
00751 condData.extend<std::string>( PoolMappingElementTable::mappingVersionColumn() );
00752 coral::AttributeList::iterator iAttribute = condData.begin();
00753 iAttribute->data< std::string >() = version;
00754 query->setCondition( condition.str(), condData );
00755 query->addToOrderList( PoolMappingElementTable::scopeNameColumn() );
00756 query->addToOrderList( PoolMappingElementTable::variableNameColumn() );
00757
00758 query->addToOrderList( PoolMappingElementTable::variableParIndexColumn() );
00759 coral::ICursor& cursor = query->execute();
00760 std::set<std::string> topElements;
00761 std::map<std::string,MappingRawElement> elementsByVarName;
00762 while ( cursor.next() ) {
00763 ret = true;
00764 const coral::AttributeList& currentRow = cursor.currentRow();
00765 std::string scope = currentRow[ PoolMappingElementTable::scopeNameColumn() ].data<std::string>();
00766 std::string varName = currentRow[ PoolMappingElementTable::variableNameColumn() ].data<std::string>();
00767 std::string elemId = scope+"::"+varName;
00768 std::map<std::string,MappingRawElement>::iterator iE = elementsByVarName.find( elemId );
00769 if( iE == elementsByVarName.end() ) {
00770 iE = elementsByVarName.insert( std::make_pair( elemId, MappingRawElement())).first;
00771 MappingRawElement& elem = iE->second;
00772 elem.elementType = mappingTypeFromPool( currentRow[ PoolMappingElementTable::elementTypeColumn() ].data<std::string>() );
00773 elem.scopeName = scope;
00774 elem.variableName = variableNameFromPool( varName );
00775 elem.variableType = currentRow[ PoolMappingElementTable::variableTypeColumn() ].data<std::string>();
00776 elem.tableName = currentRow[ PoolMappingElementTable::tableNameColumn() ].data<std::string>();
00777 if(elem.scopeName == emptyScope()) {
00778 if( elem.elementType == MappingElement::objectMappingElementType() ){
00779 if( topElements.find( elemId ) == topElements.end() ){
00780 topElements.insert( elemId );
00781 }
00782 }
00783 }
00784 }
00785 iE->second.columns.push_back( currentRow[ PoolMappingElementTable::columnNameColumn() ].data<std::string>() );
00786 }
00787
00788 std::map<std::string, std::vector<MappingRawElement> > elementsByScope;
00789 for( std::map<std::string,MappingRawElement>::iterator iEl = elementsByVarName.begin();
00790 iEl != elementsByVarName.end(); ++iEl ){
00791
00792 std::vector<std::string> reverseCols;
00793 for( std::vector<std::string>::reverse_iterator iR = iEl->second.columns.rbegin();
00794 iR != iEl->second.columns.rend(); ++iR ){
00795 reverseCols.push_back( *iR );
00796 }
00797 iEl->second.columns = reverseCols;
00798 std::string scope = iEl->second.scopeName;
00799 if( scope != emptyScope() ){
00800 std::map<std::string, std::vector<MappingRawElement> >::iterator iS = elementsByScope.find( scope );
00801 if( iS == elementsByScope.end() ){
00802 elementsByScope.insert( std::make_pair( scope, std::vector<MappingRawElement>(1,iEl->second ) ));
00803 } else {
00804 iS->second.push_back( iEl->second );
00805 }
00806 }
00807 }
00808
00809 int eid = 0;
00810 for( std::set<std::string>::const_iterator iEl = topElements.begin();
00811 iEl != topElements.end(); ++iEl ){
00812
00813 std::map<std::string,MappingRawElement>::iterator iE = elementsByVarName.find( *iEl );
00814 MappingRawElement classElement = iE->second;
00815 classElement.elementType = MappingElement::classMappingElementType();
00816 classElement.scopeName = MappingRawElement::emptyScope();
00817 dest.addElement( eid ) = classElement;
00818 eid++;
00819 MappingRawElement firstElement = iE->second;
00820 firstElement.scopeName = iE->second.variableName;
00821 dest.addElement( eid ) = firstElement;
00822 eid++;
00823
00824 rebuildPoolMapping( iE->second.variableName, iE->second.variableName, elementsByScope, dest, eid );
00825 }
00826 return ret;
00827 }
00828
00829 void ora::PoolMappingSchema::storeMapping( const MappingRawData& mapping ){
00830
00831 coral::ITable& mappingVersionTable = m_schema.tableHandle( PoolMappingVersionTable::tableName() );
00832 coral::AttributeList rowBuffer;
00833 rowBuffer.extend< std::string >( PoolMappingVersionTable::mappingVersionColumn() );
00834 rowBuffer[ PoolMappingVersionTable::mappingVersionColumn() ].data<std::string>()= mapping.version;
00835 mappingVersionTable.dataEditor().insertRow( rowBuffer );
00836
00837
00838 coral::ITable& mappingElementTable = m_schema.tableHandle( PoolMappingElementTable::tableName() );
00839 coral::AttributeList dataBuffer;
00840 dataBuffer.extend< std::string >( PoolMappingElementTable::mappingVersionColumn() );
00841 dataBuffer.extend< std::string >( PoolMappingElementTable::elementIdColumn() );
00842 dataBuffer.extend< std::string >( PoolMappingElementTable::elementTypeColumn() );
00843 dataBuffer.extend< std::string >( PoolMappingElementTable::scopeNameColumn() );
00844 dataBuffer.extend< std::string >( PoolMappingElementTable::variableNameColumn() );
00845 dataBuffer.extend< unsigned int >( PoolMappingElementTable::variableParIndexColumn() );
00846 dataBuffer.extend< std::string >( PoolMappingElementTable::variableTypeColumn() );
00847 dataBuffer.extend< std::string >( PoolMappingElementTable::tableNameColumn() );
00848 dataBuffer.extend< std::string >( PoolMappingElementTable::columnNameColumn() );
00849 dataBuffer[ PoolMappingElementTable::mappingVersionColumn() ].data<std::string>()= mapping.version;
00850
00851 for( std::map < int, MappingRawElement >::const_iterator iElem = mapping.elements.begin();
00852 iElem != mapping.elements.end(); iElem++ ){
00853 for( size_t iParamIndex = 0; iParamIndex < iElem->second.columns.size(); iParamIndex++ ){
00854 std::stringstream elemIdx;
00855 elemIdx << iElem->first;
00856 std::string scopeName = iElem->second.scopeName;
00857 if( scopeName == MappingRawElement::emptyScope() ) scopeName = std::string(" ");
00858 dataBuffer[ PoolMappingElementTable::elementIdColumn() ].data<std::string>() = elemIdx.str();
00859 dataBuffer[ PoolMappingElementTable::elementTypeColumn()].data<std::string>()= iElem->second.elementType;
00860 dataBuffer[ PoolMappingElementTable::scopeNameColumn() ].data<std::string>()= scopeName;
00861 dataBuffer[ PoolMappingElementTable::variableNameColumn() ].data<std::string>()= iElem->second.variableName;
00862 dataBuffer[ PoolMappingElementTable::variableParIndexColumn() ].data<unsigned int>() = iParamIndex;
00863 dataBuffer[ PoolMappingElementTable::variableTypeColumn() ].data<std::string>()= iElem->second.variableType;
00864 dataBuffer[ PoolMappingElementTable::tableNameColumn() ].data<std::string>()= iElem->second.tableName;
00865 dataBuffer[ PoolMappingElementTable::columnNameColumn() ].data<std::string>()= iElem->second.columns[iParamIndex];
00866 mappingElementTable.dataEditor().insertRow( dataBuffer );
00867 }
00868 }
00869 }
00870
00871 void ora::PoolMappingSchema::removeMapping( const std::string& version ){
00872
00873 coral::AttributeList whereData;
00874 whereData.extend<std::string>( PoolMappingVersionTable::mappingVersionColumn() );
00875 whereData.begin()->data<std::string>() = version;
00876
00877 std::string condition = PoolMappingVersionTable::mappingVersionColumn() + " = :" + PoolMappingVersionTable::mappingVersionColumn();
00878 m_schema.tableHandle( PoolClassVersionTable::tableName() ).dataEditor().deleteRows( condition, whereData );
00879 m_schema.tableHandle( PoolMappingElementTable::tableName() ).dataEditor().deleteRows( condition, whereData );
00880 m_schema.tableHandle( PoolMappingVersionTable::tableName() ).dataEditor().deleteRows( condition, whereData );
00881 }
00882
00883 bool ora::PoolMappingSchema::getContainerTableMap( std::map<std::string, int>&){
00884
00885 return false;
00886 }
00887
00888 bool ora::PoolMappingSchema::getMappingVersionListForContainer( int containerId,
00889 std::set<std::string>& dest,
00890 bool onlyDependency ){
00891 bool ret = false;
00892 std::auto_ptr<coral::IQuery> query( m_schema.newQuery() );
00893 query->addToTableList( PoolClassVersionTable::tableName(), "T0" );
00894 query->addToTableList( PoolContainerHeaderTable::tableName(), "T1" );
00895 query->addToTableList( PoolMappingElementTable::tableName(), "T2" );
00896 query->addToOutputList( "T0."+PoolClassVersionTable::mappingVersionColumn() );
00897 query->setDistinct();
00898 std::ostringstream condition;
00899 condition <<"T0."<<PoolClassVersionTable::containerNameColumn()<<" = "<<"T1."<<PoolContainerHeaderTable::containerNameColumn();
00900 condition <<" AND T0."<<PoolClassVersionTable::mappingVersionColumn()<<" = "<<"T2."<<PoolMappingElementTable::mappingVersionColumn();
00901 condition <<" AND T1."<<PoolContainerHeaderTable::containerIdColumn()<<" =:"<<PoolContainerHeaderTable::containerIdColumn();
00902 coral::AttributeList condData;
00903 condData.extend< int >( PoolContainerHeaderTable::containerIdColumn() );
00904 condData[ PoolContainerHeaderTable::containerIdColumn() ].data< int >() = containerId + 1;
00905 if( onlyDependency ){
00906 condition <<" AND T2."<<PoolMappingElementTable::elementTypeColumn() <<" = :"<<PoolMappingElementTable::elementTypeColumn();
00907 condData.extend< std::string >( PoolMappingElementTable::elementTypeColumn() );
00908 condData[PoolMappingElementTable::elementTypeColumn() ].data<std::string>() = MappingElement::dependencyMappingElementType();
00909 }
00910 query->setCondition(condition.str(),condData);
00911 coral::ICursor& cursor = query->execute();
00912 while ( cursor.next() ) {
00913 ret = true;
00914 const coral::AttributeList& currentRow = cursor.currentRow();
00915 std::string mappingVersion = currentRow[ "T0."+PoolClassVersionTable::mappingVersionColumn() ].data<std::string>();
00916 dest.insert( mappingVersion );
00917 }
00918 return ret;
00919 }
00920
00921
00922 bool ora::PoolMappingSchema::getDependentClassesInContainerMapping( int,
00923 std::set<std::string>& ){
00924
00925 return false;
00926 }
00927
00928 bool ora::PoolMappingSchema::getClassVersionListForMappingVersion( const std::string& mappingVersion,
00929 std::set<std::string>& destination ){
00930
00931 bool ret = false;
00932 coral::ITable& classVersionTable = m_schema.tableHandle( PoolClassVersionTable::tableName() );
00933 std::auto_ptr<coral::IQuery> query( classVersionTable.newQuery() );
00934 query->setDistinct();
00935 query->addToOutputList( PoolClassVersionTable::classVersionColumn() );
00936 std::ostringstream condition;
00937 condition <<PoolClassVersionTable::mappingVersionColumn()<<" =:"<<PoolClassVersionTable::mappingVersionColumn();
00938 coral::AttributeList condData;
00939 condData.extend<std::string>(PoolClassVersionTable::mappingVersionColumn());
00940 condData[ PoolClassVersionTable::mappingVersionColumn() ].data< std::string >() = mappingVersion;
00941 query->setCondition(condition.str(),condData);
00942 coral::ICursor& cursor = query->execute();
00943 while ( cursor.next() ) {
00944 ret = true;
00945 const coral::AttributeList& currentRow = cursor.currentRow();
00946 std::string classVersion = currentRow[ PoolClassVersionTable::classVersionColumn() ].data<std::string>();
00947 destination.insert( classVersion );
00948 }
00949 return ret;
00950 }
00951
00952 bool ora::PoolMappingSchema::getClassVersionListForContainer( int containerId,
00953 std::map<std::string,std::string>& versionMap ){
00954
00955 bool ret = false;
00956 std::auto_ptr<coral::IQuery> query( m_schema.newQuery() );
00957 query->addToTableList( PoolClassVersionTable::tableName(), "T0" );
00958 query->addToTableList( PoolContainerHeaderTable::tableName(), "T1" );
00959 query->addToOutputList( "T0."+PoolClassVersionTable::classVersionColumn() );
00960 query->addToOutputList( "T0."+PoolClassVersionTable::mappingVersionColumn() );
00961 query->setDistinct();
00962 std::ostringstream condition;
00963 condition <<"T0."<<PoolClassVersionTable::containerNameColumn()<<" = "<<"T1."<<PoolContainerHeaderTable::containerNameColumn();
00964 condition <<" AND T1."<<PoolContainerHeaderTable::containerIdColumn()<<" =:"<<PoolContainerHeaderTable::containerIdColumn();
00965 coral::AttributeList condData;
00966 condData.extend< int >( PoolContainerHeaderTable::containerIdColumn() );
00967 condData[ PoolContainerHeaderTable::containerIdColumn() ].data< int >() = containerId + 1;
00968 query->setCondition(condition.str(),condData);
00969 coral::ICursor& cursor = query->execute();
00970 while ( cursor.next() ) {
00971 ret = true;
00972 const coral::AttributeList& currentRow = cursor.currentRow();
00973 std::string classVersion = currentRow[ "T0."+PoolClassVersionTable::classVersionColumn() ].data<std::string>();
00974 std::string mappingVersion = currentRow[ "T0."+PoolClassVersionTable::mappingVersionColumn() ].data<std::string>();
00975 versionMap.insert( std::make_pair(classVersion, mappingVersion ) );
00976 }
00977 return ret;
00978 }
00979
00980 bool ora::PoolMappingSchema::getMappingVersionListForTable( const std::string&,
00981 std::set<std::string>& ){
00982
00983 return false;
00984 }
00985
00986 bool ora::PoolMappingSchema::selectMappingVersion( const std::string& classId,
00987 int containerId,
00988 std::string& destination ){
00989 bool ret = false;
00990 destination.clear();
00991
00992 std::pair<bool,std::string> isBaseId = MappingRules::classNameFromBaseId( classId );
00993 if( !isBaseId.first ){
00994 std::auto_ptr<coral::IQuery> query( m_schema.newQuery() );
00995 query->addToTableList( PoolClassVersionTable::tableName(), "T0" );
00996 query->addToTableList( PoolContainerHeaderTable::tableName(), "T1" );
00997 query->addToOutputList( "T0."+PoolClassVersionTable::mappingVersionColumn() );
00998 std::ostringstream condition;
00999 condition <<"T0."<<PoolClassVersionTable::containerNameColumn()<<" = "<<"T1."<<PoolContainerHeaderTable::containerNameColumn();
01000 condition << " AND T0."<<PoolClassVersionTable::classVersionColumn() << " =:" <<PoolClassVersionTable::classVersionColumn();
01001 condition << " AND T1."<<PoolContainerHeaderTable::containerIdColumn() << " =:" <<PoolContainerHeaderTable::containerIdColumn();
01002 coral::AttributeList condData;
01003 condData.extend<std::string>( PoolClassVersionTable::classVersionColumn() );
01004 condData.extend<int>( PoolContainerHeaderTable::containerIdColumn() );
01005 coral::AttributeList::iterator iAttribute = condData.begin();
01006 iAttribute->data< std::string >() = MappingRules::classVersionFromId( classId );
01007 ++iAttribute;
01008 iAttribute->data< int >() = containerId + 1;
01009 query->setCondition( condition.str(), condData );
01010 coral::ICursor& cursor = query->execute();
01011 while ( cursor.next() ) {
01012 ret = true;
01013 const coral::AttributeList& currentRow = cursor.currentRow();
01014 destination = currentRow["T0."+PoolClassVersionTable::mappingVersionColumn()].data<std::string>();
01015 }
01016 } else {
01017 PoolDbCacheData& containerData = m_dbCache->find( containerId );
01018
01019 if( containerData.m_className == isBaseId.second ){
01020 destination = containerData.m_mappingVersion;
01021 ret = true;
01022 }
01023 }
01024
01025 return ret;
01026 }
01027
01028 bool ora::PoolMappingSchema::containerForMappingVersion( const std::string&,
01029 int& ){
01030
01031 return false;
01032 }
01033
01034 void ora::PoolMappingSchema::insertClassVersion( const std::string&,
01035 const std::string& classVersion,
01036 const std::string&,
01037 int,
01038 int containerId,
01039 const std::string& mappingVersion ){
01040 if(!m_dbCache){
01041 throwException("MappingSchema handle has not been initialized.","PoolMappingSchema::insertClassVersion");
01042 }
01043
01044 coral::ITable& classVersionTable = m_schema.tableHandle( PoolClassVersionTable::tableName() );
01045 coral::AttributeList inputData;
01046 inputData.extend<std::string>( PoolClassVersionTable::mappingVersionColumn());
01047 inputData.extend<std::string>( PoolClassVersionTable::classVersionColumn());
01048 inputData.extend<std::string>( PoolClassVersionTable::containerNameColumn());
01049
01050 std::string containerName = m_dbCache->nameById( containerId );
01051 coral::AttributeList::iterator iInAttr = inputData.begin();
01052 iInAttr->data< std::string >() = mappingVersion;
01053 ++iInAttr;
01054 iInAttr->data< std::string >() = classVersion;
01055 ++iInAttr;
01056 iInAttr->data< std::string >() = containerName;
01057 classVersionTable.dataEditor().insertRow( inputData );
01058 }
01059
01060 void ora::PoolMappingSchema::setMappingVersion( const std::string& classId,
01061 int containerId,
01062 const std::string& mappingVersion ){
01063 if(!m_dbCache){
01064 throwException("MappingSchema handle has not been initialized.","PoolMappingSchema::setMappingVersion");
01065 }
01066 coral::ITable& classVersionTable = m_schema.tableHandle( PoolClassVersionTable::tableName() );
01067 coral::AttributeList inputData;
01068 inputData.extend<std::string>( PoolClassVersionTable::mappingVersionColumn());
01069 inputData.extend<std::string>( PoolClassVersionTable::classVersionColumn());
01070 inputData.extend<std::string>( PoolClassVersionTable::containerNameColumn());
01071 std::string classVersion = MappingRules::classVersionFromId( classId );
01072 std::string containerName = m_dbCache->nameById( containerId );
01073 coral::AttributeList::iterator iInAttr = inputData.begin();
01074 iInAttr->data< std::string >() = mappingVersion;
01075 ++iInAttr;
01076 iInAttr->data< std::string >() = classVersion;
01077 ++iInAttr;
01078 iInAttr->data< std::string >() = containerName;
01079 std::string setClause = PoolClassVersionTable::mappingVersionColumn()+" =:"+ PoolClassVersionTable::mappingVersionColumn();
01080 std::string whereClause = PoolClassVersionTable::classVersionColumn()+" =:"+ PoolClassVersionTable::classVersionColumn()+" AND "+
01081 PoolClassVersionTable::containerNameColumn()+" =:"+ PoolClassVersionTable::containerNameColumn();
01082 classVersionTable.dataEditor().updateRows( setClause,whereClause, inputData );
01083 }
01084
01085 std::string& ora::CondMetadataTable::tableName(){
01086 static std::string s_name("METADATA");
01087 return s_name;
01088 }
01089
01090 std::string& ora::CondMetadataTable::objectNameColumn(){
01091 static std::string s_column("NAME");
01092 return s_column;
01093 }
01094
01095 std::string& ora::CondMetadataTable::tokenColumn(){
01096 static std::string s_column("TOKEN");
01097 return s_column;
01098 }
01099
01100 std::string& ora::CondMetadataTable::timetypeColumn(){
01101 static std::string s_column("TIMETYPE");
01102 return s_column;
01103 }
01104
01105 ora::CondMetadataTable::CondMetadataTable( coral::ISchema& dbSchema,
01106 PoolDbCache& dbCache ):
01107 m_schema( dbSchema ),
01108 m_dbCache( dbCache ){
01109 }
01110
01111 ora::CondMetadataTable::~CondMetadataTable(){
01112 }
01113
01114 void ora::CondMetadataTable::setObjectName( const std::string& name,
01115 int contId, int itemId ){
01116 coral::AttributeList dataToInsert;
01117 dataToInsert.extend<std::string>( objectNameColumn() );
01118 dataToInsert.extend<std::string>( tokenColumn());
01119 dataToInsert.extend<int>( timetypeColumn());
01120 ora::PoolDbCacheData& contData = m_dbCache.find( contId );
01121 std::string token = cond::writeToken( contData.m_name, contId, itemId, contData.m_className );
01122 dataToInsert[ objectNameColumn() ].data<std::string>() = name;
01123 dataToInsert[ tokenColumn() ].data<std::string>() = token;
01124 dataToInsert[ timetypeColumn() ].data<int>() = -1;
01125 coral::ITable& containerTable = m_schema.tableHandle( tableName() );
01126 containerTable.dataEditor().insertRow( dataToInsert );
01127 }
01128
01129 bool ora::CondMetadataTable::eraseObjectName( const std::string& name ){
01130 coral::AttributeList whereData;
01131 whereData.extend<std::string>( objectNameColumn() );
01132 whereData.begin()->data<std::string>() = name;
01133 std::string condition = objectNameColumn() + " = :" + objectNameColumn();
01134 return m_schema.tableHandle( tableName() ).dataEditor().deleteRows( condition, whereData )>0;
01135 }
01136
01137 bool ora::CondMetadataTable::eraseAllNames(){
01138 std::string condition("");
01139 coral::AttributeList whereData;
01140 return m_schema.tableHandle( tableName() ).dataEditor().deleteRows( condition, whereData )>0;
01141 }
01142
01143 bool ora::CondMetadataTable::getObjectByName( const std::string& name,
01144 std::pair<int,int>& destination ){
01145 bool ret = false;
01146 coral::ITable& containerTable = m_schema.tableHandle( tableName() );
01147 std::auto_ptr<coral::IQuery> query( containerTable.newQuery());
01148 coral::AttributeList outputBuffer;
01149 outputBuffer.extend<std::string>( tokenColumn() );
01150 query->defineOutput( outputBuffer );
01151 query->addToOutputList( tokenColumn() );
01152 std::ostringstream condition;
01153 condition << objectNameColumn()<<"= :"<< objectNameColumn();
01154 coral::AttributeList condData;
01155 condData.extend<std::string>( objectNameColumn() );
01156 coral::AttributeList::iterator iAttribute = condData.begin();
01157 iAttribute->data< std::string >() = name;
01158 query->setCondition( condition.str(), condData );
01159 coral::ICursor& cursor = query->execute();
01160 while ( cursor.next() ) {
01161 ret = true;
01162 const coral::AttributeList& row = cursor.currentRow();
01163 std::string token = row[ tokenColumn() ].data< std::string >();
01164 std::pair<std::string,int> tokData = cond::parseToken( token );
01165 destination.first = m_dbCache.idByName( tokData.first );
01166 destination.second = tokData.second;
01167 }
01168 return ret;
01169 }
01170
01171 bool ora::CondMetadataTable::getNamesForObject( int contId,
01172 int itemId,
01173 std::vector<std::string>& destination ){
01174 bool ret = false;
01175 coral::ITable& containerTable = m_schema.tableHandle( tableName() );
01176 std::auto_ptr<coral::IQuery> query( containerTable.newQuery());
01177 coral::AttributeList outputBuffer;
01178 outputBuffer.extend<std::string>( objectNameColumn() );
01179 query->defineOutput( outputBuffer );
01180 query->addToOutputList( objectNameColumn() );
01181 std::ostringstream condition;
01182 condition << tokenColumn()<<"= :"<< tokenColumn();
01183 ora::PoolDbCacheData& contData = m_dbCache.find( contId );
01184 std::string token = cond::writeToken( contData.m_name, contId, itemId, contData.m_className );
01185 coral::AttributeList condData;
01186 condData.extend<std::string>( tokenColumn() );
01187 coral::AttributeList::iterator iAttribute = condData.begin();
01188 iAttribute->data< std::string >() = token;
01189 query->setCondition( condition.str(), condData );
01190 coral::ICursor& cursor = query->execute();
01191 while ( cursor.next() ) {
01192 ret = true;
01193 const coral::AttributeList& row = cursor.currentRow();
01194 std::string name = row[ objectNameColumn() ].data< std::string >();
01195 destination.push_back( name );
01196 }
01197 return ret;
01198 }
01199
01200 bool ora::CondMetadataTable::getNamesForContainer( int contId,
01201 std::vector<std::string>& destination ){
01202
01203 bool ret = false;
01204 coral::ITable& containerTable = m_schema.tableHandle( tableName() );
01205 std::auto_ptr<coral::IQuery> query( containerTable.newQuery());
01206 coral::AttributeList outputBuffer;
01207 outputBuffer.extend<std::string>( objectNameColumn() );
01208 query->defineOutput( outputBuffer );
01209 query->addToOutputList( objectNameColumn() );
01210 std::ostringstream condition;
01211 condition << tokenColumn()<<" LIKE :"<< tokenColumn();
01212 ora::PoolDbCacheData& contData = m_dbCache.find( contId );
01213 std::string tokenFragment = cond::writeTokenContainerFragment( contData.m_name, contData.m_className )+"%";
01214 coral::AttributeList condData;
01215 condData.extend<std::string>( tokenColumn() );
01216 coral::AttributeList::iterator iAttribute = condData.begin();
01217 iAttribute->data< std::string >() = tokenFragment;
01218 query->setCondition( condition.str(), condData );
01219 coral::ICursor& cursor = query->execute();
01220 while ( cursor.next() ) {
01221 ret = true;
01222 const coral::AttributeList& row = cursor.currentRow();
01223 std::string name = row[ objectNameColumn() ].data< std::string >();
01224 destination.push_back( name );
01225 }
01226 return ret;
01227 }
01228
01229 bool ora::CondMetadataTable::getAllNames( std::vector<std::string>& destination ){
01230
01231 bool ret = false;
01232 coral::ITable& containerTable = m_schema.tableHandle( tableName() );
01233 std::auto_ptr<coral::IQuery> query( containerTable.newQuery());
01234 coral::AttributeList outputBuffer;
01235 outputBuffer.extend<std::string>( objectNameColumn() );
01236 query->defineOutput( outputBuffer );
01237 query->addToOutputList( objectNameColumn() );
01238 coral::ICursor& cursor = query->execute();
01239 while ( cursor.next() ) {
01240 ret = true;
01241 const coral::AttributeList& row = cursor.currentRow();
01242 std::string name = row[ objectNameColumn() ].data< std::string >();
01243 destination.push_back( name );
01244 }
01245 return ret;
01246 }
01247
01248 bool ora::CondMetadataTable::exists(){
01249 return m_schema.existsTable( tableName() );
01250 }
01251
01252 void ora::CondMetadataTable::create(){
01253 if( m_schema.existsTable( tableName() )){
01254 throwException( "Metadata table already exists in this schema.",
01255 "CondMetadataTable::create");
01256 }
01257 throwException( "Cond Metadata table cannot be created.","CondMetadataTable::create");
01258 }
01259
01260 void ora::CondMetadataTable::drop(){
01261 if( !m_schema.existsTable( tableName() )){
01262 throwException( "Metadata table does not exists in this schema.",
01263 "CondMetadataTable::drop");
01264 }
01265 throwException( "Cond Metadata table cannot be dropped.","CondMetadataTable::drop");
01266 }
01267
01268 bool ora::PoolDatabaseSchema::existsMainTable( coral::ISchema& dbSchema ){
01269 PoolMainTable tmp( dbSchema );
01270 return tmp.exists();
01271 }
01272
01273 ora::PoolDatabaseSchema::PoolDatabaseSchema( coral::ISchema& dbSchema ):
01274 IDatabaseSchema( dbSchema ),
01275 m_schema( dbSchema ),
01276 m_dbCache(),
01277 m_mainTable( dbSchema ),
01278 m_sequenceTable( dbSchema ),
01279 m_mappingVersionTable( dbSchema ),
01280 m_mappingElementTable( dbSchema ),
01281 m_containerHeaderTable( dbSchema ),
01282 m_classVersionTable( dbSchema ),
01283 m_mappingSchema( dbSchema ),
01284 m_metadataTable( dbSchema, m_dbCache ){
01285 m_sequenceTable.init( m_dbCache );
01286 m_containerHeaderTable.init( m_dbCache );
01287 m_mappingSchema.init( m_dbCache );
01288 }
01289
01290 ora::PoolDatabaseSchema::~PoolDatabaseSchema(){
01291 }
01292
01293 bool ora::PoolDatabaseSchema::exists(){
01294 if(!m_mainTable.exists()){
01295 return false;
01296 }
01297
01298 if(!m_sequenceTable.exists() ||
01299 !m_mappingVersionTable.exists() ||
01300 !m_mappingElementTable.exists() ||
01301 !m_containerHeaderTable.exists() ||
01302 !m_classVersionTable.exists()){
01303 throwException( "POOL database is corrupted..",
01304 "PoolDatabaseSchema::exists");
01305 }
01306 if( !m_metadataTable.exists()){
01307 throwException( "Metadata table has not been found.",
01308 "PoolDatabaseSchema::exists");
01309 }
01310 return true;
01311 }
01312
01313 void ora::PoolDatabaseSchema::create(){
01314 throwException( "POOL database cannot be created.","PoolDatabaseSchema::create");
01315 }
01316
01317 void ora::PoolDatabaseSchema::drop(){
01318 m_classVersionTable.drop();
01319 m_mappingElementTable.drop();
01320 m_sequenceTable.drop();
01321 m_containerHeaderTable.drop();
01322 m_mappingVersionTable.drop();
01323 m_mainTable.drop();
01324 }
01325
01326 ora::IMainTable& ora::PoolDatabaseSchema::mainTable(){
01327 return m_mainTable;
01328 }
01329
01330 ora::ISequenceTable& ora::PoolDatabaseSchema::sequenceTable(){
01331 return m_sequenceTable;
01332 }
01333
01334 ora::IDatabaseTable& ora::PoolDatabaseSchema::mappingVersionTable(){
01335 return m_mappingVersionTable;
01336 }
01337
01338 ora::IDatabaseTable& ora::PoolDatabaseSchema::mappingElementTable(){
01339 return m_mappingElementTable;
01340 }
01341
01342 ora::IContainerHeaderTable& ora::PoolDatabaseSchema::containerHeaderTable(){
01343 return m_containerHeaderTable;
01344 }
01345
01346 ora::IDatabaseTable& ora::PoolDatabaseSchema::classVersionTable(){
01347 return m_classVersionTable;
01348 }
01349
01350 ora::IMappingSchema& ora::PoolDatabaseSchema::mappingSchema(){
01351 return m_mappingSchema;
01352 }
01353
01354 ora::INamingServiceTable& ora::PoolDatabaseSchema::namingServiceTable(){
01355 return m_metadataTable;
01356 }
01357
01358