CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/CondCore/ORA/src/OraDatabaseSchema.cc

Go to the documentation of this file.
00001 #include "CondCore/ORA/interface/Exception.h"
00002 #include "OraDatabaseSchema.h"
00003 //
00004 #include <memory>
00005 // externals
00006 #include "RelationalAccess/ISchema.h"
00007 #include "RelationalAccess/ITable.h"
00008 #include "RelationalAccess/IQuery.h"
00009 #include "RelationalAccess/ICursor.h"
00010 #include "RelationalAccess/TableDescription.h"
00011 #include "RelationalAccess/ITablePrivilegeManager.h"
00012 #include "RelationalAccess/ITableDataEditor.h"
00013 #include "RelationalAccess/IBulkOperation.h"
00014 #include "CoralBase/Attribute.h"
00015 
00016 std::string ora::OraMainTable::version(){
00017   static std::string s_version("V1.0");
00018   return s_version;
00019 }
00020 
00021 std::string ora::OraMainTable::tableName(){
00022   static std::string s_name("ORA_DB");
00023   return s_name;
00024 }
00025 
00026 std::string ora::OraMainTable::parameterNameColumn(){
00027   static std::string s_column("PARAMETER_NAME");
00028   return s_column;
00029 }
00030 
00031 std::string ora::OraMainTable::parameterValueColumn(){
00032   static std::string s_column("PARAMETER_VALUE");
00033   return s_column;
00034 }
00035 
00036 ora::OraMainTable::OraMainTable( coral::ISchema& dbSchema ):
00037   m_schema( dbSchema ){
00038 }
00039 
00040 ora::OraMainTable::~OraMainTable(){
00041 }
00042 
00043 bool ora::OraMainTable::getParameters( std::map<std::string,std::string>& dest ){
00044   bool ret = false;
00045   coral::ITable& mainTable = m_schema.tableHandle( tableName() );
00046   std::auto_ptr<coral::IQuery> query(mainTable.newQuery());
00047   coral::ICursor& cursor = query->execute();
00048   while ( cursor.next() ) {
00049     ret = true;
00050     const coral::AttributeList& row = cursor.currentRow();
00051     std::string paramName = row[ parameterNameColumn()].data< std::string >();
00052     std::string paramValue = row[ parameterValueColumn()].data< std::string >();
00053     dest.insert( std::make_pair( paramName, paramValue ) );
00054   }
00055   return ret;
00056 }
00057 
00058 std::string ora::OraMainTable::schemaVersion(){
00059   // could be replaced by a call to getParameters in case of needs to distinguish between ora db schema versions...
00060   return version();
00061 }
00062 
00063 bool ora::OraMainTable::exists(){
00064   return m_schema.existsTable( tableName() );
00065 }
00066 
00067 void ora::OraMainTable::create(){
00068   if( m_schema.existsTable( tableName() )){
00069     throwException( "ORA database main table already exists in this schema.",
00070                     "OraMainTable::create");
00071   }
00072   
00073   coral::TableDescription descr( "OraDb" );
00074   descr.setName( tableName() );
00075   descr.insertColumn( parameterNameColumn(),
00076                       coral::AttributeSpecification::typeNameForType<std::string>() );
00077   descr.insertColumn( parameterValueColumn(),
00078                       coral::AttributeSpecification::typeNameForType<std::string>() );
00079   descr.setNotNullConstraint( parameterNameColumn() );
00080   descr.setNotNullConstraint( parameterValueColumn() );
00081   descr.setPrimaryKey( std::vector<std::string>( 1, parameterNameColumn() ) );
00082 
00083   coral::ITable& table = m_schema.createTable( descr );
00084   table.privilegeManager().grantToPublic( coral::ITablePrivilegeManager::Select );
00085 
00086   coral::AttributeList dataToInsert;
00087   dataToInsert.extend<std::string>( parameterNameColumn());
00088   dataToInsert.extend<std::string>( parameterValueColumn());
00089   dataToInsert[ parameterNameColumn() ].data<std::string>() = IMainTable::versionParameterName();
00090   dataToInsert[ parameterValueColumn() ].data<std::string>() = version();
00091   table.dataEditor().insertRow( dataToInsert );
00092 }
00093 
00094 void ora::OraMainTable::drop(){
00095   m_schema.dropIfExistsTable( tableName() );
00096 }
00097 
00098 std::string ora::OraSequenceTable::tableName(){
00099   static std::string s_name("ORA_SEQUENCE");
00100   return s_name;
00101 }
00102 
00103 std::string ora::OraSequenceTable::sequenceNameColumn(){
00104   static std::string s_column("NAME");
00105   return s_column;
00106 }
00107 
00108 std::string ora::OraSequenceTable::sequenceValueColumn(){
00109   static std::string s_column("VALUE");
00110   return s_column;
00111 }
00112 
00113 ora::OraSequenceTable::OraSequenceTable( coral::ISchema& schema ):
00114   m_schema( schema){
00115 }
00116 
00117 ora::OraSequenceTable::~OraSequenceTable(){
00118 }
00119 
00120 bool
00121 ora::OraSequenceTable::add( const std::string& sequenceName ){
00122   // Create the entry in the table 
00123   coral::AttributeList insertData;
00124   insertData.extend<std::string>(sequenceNameColumn());
00125   insertData.extend<int>(sequenceValueColumn());
00126   coral::AttributeList::iterator iAttribute = insertData.begin();
00127   iAttribute->data< std::string >() = sequenceName;
00128   ++iAttribute;
00129   iAttribute->data< int >() = 0;
00130   m_schema.tableHandle( tableName() ).dataEditor().insertRow( insertData );
00131   return true;
00132 }
00133 
00134 bool
00135 ora::OraSequenceTable::getLastId( const std::string& sequenceName,
00136                                   int& lastId ) {
00137   std::auto_ptr< coral::IQuery > query( m_schema.tableHandle( tableName() ).newQuery() );
00138   query->limitReturnedRows( 1, 0 );
00139   query->addToOutputList( sequenceValueColumn() );
00140   query->defineOutputType( sequenceValueColumn(), coral::AttributeSpecification::typeNameForType<int>() );
00141   query->setForUpdate();
00142   std::string whereClause( sequenceNameColumn() + " = :" + sequenceNameColumn() );
00143   coral::AttributeList rowData;
00144   rowData.extend<std::string>(sequenceNameColumn());
00145   rowData.begin()->data< std::string >() = sequenceName;
00146   query->setCondition( whereClause, rowData );
00147   coral::ICursor& cursor = query->execute();
00148   if ( cursor.next() ) {
00149     lastId = cursor.currentRow().begin()->data<int >();
00150     return true;
00151   }
00152   return false;
00153 }
00154 
00155 void ora::OraSequenceTable::sinchronize( const std::string& sequenceName,
00156                                          int lastValue ){
00157   coral::AttributeList updateData;
00158   updateData.extend<std::string>(sequenceNameColumn());
00159   updateData.extend<int>(sequenceValueColumn());
00160   std::string setClause( sequenceValueColumn() + " = :" +  sequenceValueColumn() );
00161   std::string whereClause( sequenceNameColumn() + " = :" + sequenceNameColumn() );
00162   // Increment the oid in the database as well
00163   coral::AttributeList::iterator iAttribute = updateData.begin();
00164   iAttribute->data< std::string >() = sequenceName;
00165   ++iAttribute;
00166   iAttribute->data< int >() = lastValue;
00167   m_schema.tableHandle( tableName() ).dataEditor().updateRows( setClause,whereClause,updateData );
00168 }
00169 
00170 void ora::OraSequenceTable::erase( const std::string& name ){
00171   coral::AttributeList whereData;
00172   whereData.extend<std::string>(sequenceNameColumn());
00173   whereData[ sequenceNameColumn() ].data<std::string>() = name;
00174   std::string whereClause( sequenceNameColumn() + " = :" + sequenceNameColumn() );
00175   m_schema.tableHandle( tableName() ).dataEditor().deleteRows( whereClause, whereData );
00176 }
00177 
00178 bool ora::OraSequenceTable::exists(){
00179   return m_schema.existsTable( tableName() );
00180 }
00181 
00182 void ora::OraSequenceTable::create(){
00183   if( m_schema.existsTable( tableName() )){
00184     throwException( "ORA database sequence table already exists in this schema.",
00185                     "OraSequenceTable::create");
00186   }
00187   
00188   coral::TableDescription description( "OraDb" );
00189   description.setName( tableName() );
00190 
00191   description.insertColumn( sequenceNameColumn(), coral::AttributeSpecification::typeNameForType<std::string>() );
00192   description.setNotNullConstraint( sequenceNameColumn() );
00193 
00194   description.insertColumn( sequenceValueColumn(),coral::AttributeSpecification::typeNameForType<int>() );
00195   description.setNotNullConstraint( sequenceValueColumn() );
00196 
00197   description.setPrimaryKey( std::vector< std::string >( 1, sequenceNameColumn() ) );
00198   m_schema.createTable( description ).privilegeManager().grantToPublic( coral::ITablePrivilegeManager::Select );
00199 }
00200 
00201 void ora::OraSequenceTable::drop(){
00202   m_schema.dropIfExistsTable( tableName() );
00203 }
00204 
00205 std::string ora::OraMappingVersionTable::tableName(){
00206   static std::string s_table("ORA_MAPPING_VERSION");
00207   return s_table;
00208 }
00209 
00210 std::string ora::OraMappingVersionTable::mappingVersionColumn(){
00211   static std::string s_col("MAPPING_VERSION");
00212   return s_col;
00213 }
00214 
00215 ora::OraMappingVersionTable::OraMappingVersionTable( coral::ISchema& dbSchema  ):
00216   m_schema( dbSchema ){
00217 }
00218 
00219 ora::OraMappingVersionTable::~OraMappingVersionTable(){
00220 }
00221 
00222 bool ora::OraMappingVersionTable::exists(){
00223   return m_schema.existsTable( tableName() );
00224 }
00225 
00226 void ora::OraMappingVersionTable::create(){
00227   if( m_schema.existsTable( tableName() )){
00228     throwException( "ORA database mapping version table already exists in this schema.",
00229                     "OraMappingVersionTable::create");
00230   }
00231   // version table
00232   coral::TableDescription  description0( "OraDb" );
00233   description0.setName( tableName() );
00234   description0.insertColumn( mappingVersionColumn(),
00235                              coral::AttributeSpecification::typeNameForType<std::string>(), 1000, false);
00236   description0.setNotNullConstraint( mappingVersionColumn() );
00237   description0.setPrimaryKey( mappingVersionColumn() );
00238   m_schema.createTable( description0 ).privilegeManager().grantToPublic( coral::ITablePrivilegeManager::Select );
00239 }
00240 
00241 void ora::OraMappingVersionTable::drop(){
00242   m_schema.dropIfExistsTable( tableName() );
00243 }
00244 
00245 
00246 std::string ora::OraMappingElementTable::tableName(){
00247   static std::string s_table("ORA_MAPPING_ELEMENT");
00248   return s_table;  
00249 }
00250 
00251 std::string ora::OraMappingElementTable::mappingVersionColumn(){
00252   static std::string s_col("MAPPING_VERSION");
00253   return s_col;  
00254 }
00255 
00256 std::string ora::OraMappingElementTable::elementIdColumn(){
00257   static std::string s_col("ELEMENT_ID");
00258   return s_col;
00259 }
00260 
00261 std::string ora::OraMappingElementTable::elementTypeColumn(){
00262   static std::string s_col("ELEMENT_TYPE");
00263   return s_col;  
00264 }
00265 
00266 std::string ora::OraMappingElementTable::scopeNameColumn(){
00267   static std::string s_col("VARIABLE_SCOPE");
00268   return s_col;
00269 }
00270 
00271 std::string ora::OraMappingElementTable::variableNameColumn(){
00272   static std::string s_col("VARIABLE_NAME");
00273   return s_col;
00274 }
00275 
00276 std::string ora::OraMappingElementTable::variableParIndexColumn(){
00277   static std::string s_col("VARIABLE_PAR_INDEX");
00278   return s_col;
00279 }
00280 
00281 std::string ora::OraMappingElementTable::variableTypeColumn(){
00282   static std::string s_col("VARIABLE_TYPE");
00283   return s_col;
00284 
00285 }
00286 
00287 std::string ora::OraMappingElementTable::tableNameColumn(){
00288   static std::string s_col("TABLE_NAME");
00289   return s_col;  
00290 }
00291 
00292 std::string ora::OraMappingElementTable::columnNameColumn(){
00293   static std::string s_col("COLUMN_NAME");
00294   return s_col;  
00295 }
00296 
00297 ora::OraMappingElementTable::OraMappingElementTable( coral::ISchema& dbSchema  ):
00298   m_schema( dbSchema ){
00299 }
00300 
00301 ora::OraMappingElementTable::~OraMappingElementTable(){
00302 }
00303 
00304 bool ora::OraMappingElementTable::exists(){
00305   return m_schema.existsTable( tableName() );
00306 }
00307 
00308 void ora::OraMappingElementTable::create(){
00309   if( m_schema.existsTable( tableName() )){
00310     throwException( "ORA database mapping element table already exists in this schema.",
00311                     "OraMappingElementTable::create");
00312   }
00313 
00314   // mapping elements table
00315   coral::TableDescription description2( "OraDb" );
00316   description2.setName( tableName() );
00317   description2.insertColumn(  mappingVersionColumn(),
00318                               coral::AttributeSpecification::typeNameForType<std::string>(), 1000, false );
00319   description2.setNotNullConstraint(  mappingVersionColumn() );
00320   description2.insertColumn(  elementIdColumn(),
00321                               coral::AttributeSpecification::typeNameForType<int>() );
00322   description2.setNotNullConstraint(  elementIdColumn() );
00323   description2.insertColumn(  elementTypeColumn(),
00324                               coral::AttributeSpecification::typeNameForType<std::string>() );
00325   description2.setNotNullConstraint(  elementTypeColumn() );
00326   description2.insertColumn(  scopeNameColumn(),
00327                               coral::AttributeSpecification::typeNameForType<std::string>() );
00328   description2.setNotNullConstraint(  scopeNameColumn() );
00329   description2.insertColumn( variableNameColumn(),
00330                              coral::AttributeSpecification::typeNameForType<std::string>() );
00331   description2.setNotNullConstraint( variableNameColumn() );
00332   description2.insertColumn( variableParIndexColumn(),
00333                               coral::AttributeSpecification::typeNameForType<unsigned int>() );
00334   description2.setNotNullConstraint(  variableParIndexColumn() );
00335   description2.insertColumn(  variableTypeColumn(),
00336                               coral::AttributeSpecification::typeNameForType<std::string>() );
00337   description2.setNotNullConstraint(  variableTypeColumn() );
00338   description2.insertColumn(  tableNameColumn(),
00339                               coral::AttributeSpecification::typeNameForType<std::string>() );
00340   description2.setNotNullConstraint(  tableNameColumn() );
00341   description2.insertColumn(  columnNameColumn(),
00342                               coral::AttributeSpecification::typeNameForType<std::string>() );
00343   description2.setNotNullConstraint(  columnNameColumn() );
00344   std::vector<std::string> cols2;
00345   cols2.push_back(  elementIdColumn() );
00346   cols2.push_back(  variableParIndexColumn() );
00347   description2.setPrimaryKey( cols2 );
00348   std::string fkName20 = mappingVersionColumn()+"_FK_1";
00349   description2.createForeignKey( fkName20, mappingVersionColumn(),
00350                                  OraMappingVersionTable::tableName(),OraMappingVersionTable::mappingVersionColumn());
00351   m_schema.createTable( description2 ).privilegeManager().grantToPublic( coral::ITablePrivilegeManager::Select );
00352 }
00353 
00354 void ora::OraMappingElementTable::drop(){
00355   m_schema.dropIfExistsTable( tableName() );
00356 }
00357 
00358 std::string ora::OraContainerHeaderTable::tableName(){
00359   static std::string s_name("ORA_CONTAINER");
00360   return s_name;
00361 }
00362 
00363 
00364 std::string ora::OraContainerHeaderTable::containerIdColumn(){
00365   static std::string s_column("CONTAINER_ID");
00366   return s_column;
00367 }
00368 
00369 
00370 std::string ora::OraContainerHeaderTable::containerNameColumn(){
00371   static std::string s_column("CONTAINER_NAME");
00372   return s_column;  
00373 }
00374 
00375 std::string ora::OraContainerHeaderTable::classNameColumn(){
00376   static std::string s_column("CLASS_NAME");
00377   return s_column;
00378 }
00379 
00380 std::string ora::OraContainerHeaderTable::numberOfObjectsColumn(){
00381   static std::string s_column("NUMBER_OF_OBJECTS");
00382   return s_column;  
00383 }
00384 
00385 ora::OraContainerHeaderTable::OraContainerHeaderTable( coral::ISchema& dbSchema ):
00386   m_schema(dbSchema){
00387 }
00388 
00389 ora::OraContainerHeaderTable::~OraContainerHeaderTable(){
00390 }
00391 
00392 bool ora::OraContainerHeaderTable::getContainerData( std::map<std::string,
00393                                                      ora::ContainerHeaderData>& dest ){
00394   bool ret = false;
00395   coral::ITable& containerTable = m_schema.tableHandle( tableName() );
00396   std::auto_ptr<coral::IQuery> query( containerTable.newQuery());
00397   coral::AttributeList outputBuffer;
00398   outputBuffer.extend<int>( containerIdColumn() );
00399   outputBuffer.extend<std::string>( containerNameColumn() );
00400   outputBuffer.extend<std::string>( classNameColumn() );
00401   outputBuffer.extend<unsigned int>( numberOfObjectsColumn() );
00402   query->defineOutput( outputBuffer );
00403   coral::ICursor& cursor = query->execute();
00404   while ( cursor.next() ) {
00405     ret = true;
00406     const coral::AttributeList& row = cursor.currentRow();
00407     int containerId = row[ containerIdColumn() ].data< int >();
00408     std::string containerName = row[ containerNameColumn()].data< std::string >();
00409     std::string className = row[ classNameColumn()].data< std::string >();
00410     unsigned int numberOfObjects = row[ numberOfObjectsColumn()].data< unsigned int >();
00411     dest.insert( std::make_pair( containerName, ContainerHeaderData( containerId, className, numberOfObjects ) )) ;
00412   }
00413   return ret;
00414 }
00415 
00416 void ora::OraContainerHeaderTable::addContainer( int containerId,
00417                                                  const std::string& containerName,
00418                                                  const std::string& className ){
00419   coral::AttributeList dataToInsert;
00420   unsigned int numberOfObjects = 0;
00421   dataToInsert.extend<int>( containerIdColumn());
00422   dataToInsert.extend<std::string>( containerNameColumn());
00423   dataToInsert.extend<std::string>( classNameColumn());
00424   dataToInsert.extend<unsigned int>( numberOfObjectsColumn());
00425   dataToInsert[ containerIdColumn() ].data<int>() = containerId;
00426   dataToInsert[ containerNameColumn() ].data<std::string>() = containerName;
00427   dataToInsert[ classNameColumn() ].data<std::string>() = className;
00428   dataToInsert[ numberOfObjectsColumn() ].data<unsigned int>() = numberOfObjects;
00429   coral::ITable& containerTable = m_schema.tableHandle( tableName() );
00430   containerTable.dataEditor().insertRow( dataToInsert );
00431 }
00432 
00433 void ora::OraContainerHeaderTable::removeContainer( int id ){
00434   std::stringstream whereClause;
00435   whereClause << containerIdColumn() << "= :" <<containerIdColumn();
00436   coral::AttributeList whereData;
00437   whereData.extend< int >( containerIdColumn() );
00438   whereData.begin()->data< int >() = id;
00439   coral::ITable& containerTable = m_schema.tableHandle( tableName() );
00440   containerTable.dataEditor().deleteRows(whereClause.str(),whereData);
00441 }
00442 
00443 void ora::OraContainerHeaderTable::updateContainer( int containerId,
00444                                                     const std::string& setClause ){
00445   std::stringstream whereClause;
00446   whereClause << containerIdColumn() << "= :" <<containerIdColumn();
00447   coral::AttributeList updateData;
00448   updateData.extend<int>( containerIdColumn() );
00449   updateData.begin()->data<int>() = containerId;
00450   coral::ITable& containerTable = m_schema.tableHandle( tableName() );
00451   containerTable.dataEditor().updateRows(setClause,whereClause.str(),updateData);
00452 }
00453 
00454 void ora::OraContainerHeaderTable::incrementNumberOfObjects( int containerId  ){
00455   std::stringstream setClause;
00456   setClause << numberOfObjectsColumn() << " = " <<numberOfObjectsColumn() << " + 1";
00457   return updateContainer( containerId, setClause.str() );
00458 }
00459 
00460 void ora::OraContainerHeaderTable::decrementNumberOfObjects( int containerId  ){
00461   std::stringstream setClause;
00462   setClause << numberOfObjectsColumn() << " = " <<numberOfObjectsColumn() << " - 1";
00463   return updateContainer( containerId, setClause.str() );
00464 }
00465 
00466 void ora::OraContainerHeaderTable::updateNumberOfObjects( const std::map<int,unsigned int>& numberOfObjectsForContainerIds ){
00467   if( numberOfObjectsForContainerIds.size() ){
00468 
00469     std::stringstream whereClause;
00470     whereClause << containerIdColumn() << "= :" <<containerIdColumn();
00471     std::stringstream setClause;
00472     setClause << numberOfObjectsColumn() << " = :" <<numberOfObjectsColumn();
00473     coral::AttributeList updateData;
00474     updateData.extend<unsigned int>( numberOfObjectsColumn() );
00475     updateData.extend<int>( containerIdColumn() );
00476 
00477     coral::ITable& containerTable = m_schema.tableHandle( tableName() );
00478     std::auto_ptr<coral::IBulkOperation> bulkUpdate( containerTable.dataEditor().bulkUpdateRows( setClause.str(), whereClause.str(), updateData,(int)numberOfObjectsForContainerIds.size()));
00479 
00480     for( std::map<int,unsigned int>::const_iterator iCont = numberOfObjectsForContainerIds.begin();
00481          iCont != numberOfObjectsForContainerIds.end(); ++iCont ){
00482       updateData[containerIdColumn()].data<int>() = iCont->first;
00483       updateData[numberOfObjectsColumn()].data<unsigned int>() = iCont->second;
00484       bulkUpdate->processNextIteration();
00485     }
00486     bulkUpdate->flush();
00487   }
00488 }
00489 
00490 bool ora::OraContainerHeaderTable::exists(){
00491   return m_schema.existsTable( tableName() );
00492 }
00493 
00494 void ora::OraContainerHeaderTable::create(){
00495   if( m_schema.existsTable( tableName() )){
00496     throwException( "ORA database container header table already exists in this schema.",
00497                     "OraContainerHeaderTable::create");
00498   }
00499   
00500   coral::TableDescription descr( "OraDb" );
00501   descr.setName( tableName() );
00502   descr.insertColumn( containerIdColumn(),
00503                       coral::AttributeSpecification::typeNameForType<int>() );
00504   descr.insertColumn( containerNameColumn(),
00505                       coral::AttributeSpecification::typeNameForType<std::string>() );
00506   descr.insertColumn( classNameColumn(),
00507                       coral::AttributeSpecification::typeNameForType<std::string>() );
00508   descr.insertColumn( numberOfObjectsColumn(),
00509                       coral::AttributeSpecification::typeNameForType<unsigned int>() );
00510   descr.setNotNullConstraint( containerIdColumn() );
00511   descr.setNotNullConstraint( containerNameColumn() );
00512   descr.setNotNullConstraint( classNameColumn() );
00513   descr.setNotNullConstraint( numberOfObjectsColumn() );
00514   descr.setPrimaryKey( std::vector<std::string>( 1, containerIdColumn() ) );
00515   descr.setUniqueConstraint( containerNameColumn() );
00516   coral::ITable& table = m_schema.createTable( descr );
00517   table.privilegeManager().grantToPublic( coral::ITablePrivilegeManager::Select );
00518 }
00519 
00520 void ora::OraContainerHeaderTable::drop(){
00521   m_schema.dropIfExistsTable( tableName() );
00522 }
00523 
00524 std::string ora::OraClassVersionTable::tableName(){
00525   static std::string s_table("ORA_CLASS_VERSION");
00526   return s_table;
00527 }
00528 
00529 std::string ora::OraClassVersionTable::classNameColumn(){
00530   static std::string s_col("CLASS_NAME");
00531   return s_col;
00532 }
00533 
00534 std::string ora::OraClassVersionTable::classVersionColumn(){
00535   static std::string s_col("CLASS_VERSION");
00536   return s_col;
00537 }
00538 
00539 std::string ora::OraClassVersionTable::classIdColumn(){
00540   static std::string s_col("CLASS_ID");
00541   return s_col;
00542 }
00543 
00544 std::string ora::OraClassVersionTable::dependencyIndexColumn(){
00545   static std::string s_col("DEPENDENCY_INDEX");
00546   return s_col;
00547 }
00548 
00549 std::string ora::OraClassVersionTable::containerIdColumn(){
00550   static std::string s_col("CONTAINER_ID");
00551   return s_col;
00552 
00553 }
00554  
00555 std::string ora::OraClassVersionTable::mappingVersionColumn(){
00556   static std::string s_col("MAPPING_VERSION");
00557   return s_col;
00558 }
00559 
00560 
00561 ora::OraClassVersionTable::OraClassVersionTable( coral::ISchema& dbSchema  ):
00562   m_schema( dbSchema ){
00563 }
00564 
00565 ora::OraClassVersionTable::~OraClassVersionTable(){
00566 }
00567 
00568 bool ora::OraClassVersionTable::exists(){
00569   return m_schema.existsTable( tableName() );
00570 }
00571 
00572 void ora::OraClassVersionTable::create(){
00573   if( m_schema.existsTable( tableName() )){
00574     throwException( "ORA database class version table already exists in this schema.",
00575                     "OraClassVersionTable::create");
00576   }
00577   // class version table
00578   coral::TableDescription description1( "OraDb" );
00579   description1.setName( tableName() );
00580   description1.insertColumn( classNameColumn(),
00581                              coral::AttributeSpecification::typeNameForType<std::string>(), 1000, false);
00582   description1.setNotNullConstraint( classNameColumn() );
00583   description1.insertColumn( classVersionColumn(),
00584                              coral::AttributeSpecification::typeNameForType<std::string>(), 1000, false);
00585   description1.setNotNullConstraint( classVersionColumn() );
00586   description1.insertColumn( classIdColumn(),
00587                              coral::AttributeSpecification::typeNameForType<std::string>(), 1000, false);
00588   description1.setNotNullConstraint( classIdColumn() );
00589   description1.insertColumn( dependencyIndexColumn(),
00590                              coral::AttributeSpecification::typeNameForType<int>() );
00591   description1.setNotNullConstraint(  dependencyIndexColumn() );
00592   description1.insertColumn( containerIdColumn(),
00593                              coral::AttributeSpecification::typeNameForType<int>() );
00594   description1.setNotNullConstraint(  containerIdColumn() );
00595   description1.insertColumn( mappingVersionColumn(),
00596                              coral::AttributeSpecification::typeNameForType<std::string>(), 1000, false);
00597   description1.setNotNullConstraint( mappingVersionColumn() );
00598   std::vector<std::string> cols1;
00599   cols1.push_back( classIdColumn() );
00600   cols1.push_back( containerIdColumn() );
00601   description1.setPrimaryKey( cols1 );
00602   std::string fk10Name = mappingVersionColumn()+"_FK10";
00603   description1.createForeignKey( fk10Name, mappingVersionColumn(),
00604                                  ora::OraMappingVersionTable::tableName(),ora::OraMappingVersionTable::mappingVersionColumn());
00605   std::string fk11Name = containerIdColumn()+"_FK11";
00606   description1.createForeignKey( fk11Name, containerIdColumn(),
00607                                  ora::OraContainerHeaderTable::tableName(),ora::OraContainerHeaderTable::containerIdColumn());
00608   m_schema.createTable( description1 ).privilegeManager().grantToPublic( coral::ITablePrivilegeManager::Select );
00609 }
00610 
00611 void ora::OraClassVersionTable::drop(){
00612   m_schema.dropIfExistsTable( tableName() );
00613 }
00614 
00615 ora::OraMappingSchema::OraMappingSchema( coral::ISchema& dbSchema  ):
00616   m_schema( dbSchema ){
00617 }
00618 
00619 ora::OraMappingSchema::~OraMappingSchema(){
00620 }
00621 
00622 bool ora::OraMappingSchema::getVersionList( std::set<std::string>& dest ){
00623   bool ret = false;
00624   std::auto_ptr<coral::IQuery> query( m_schema.tableHandle( OraMappingVersionTable::tableName() ).newQuery() );
00625   query->addToOutputList( OraMappingVersionTable::mappingVersionColumn() );
00626   coral::ICursor& cursor = query->execute();
00627   while ( cursor.next() ) {
00628     ret = true;
00629     const coral::AttributeList& currentRow = cursor.currentRow();
00630     std::string mappingVersion = currentRow[ OraMappingVersionTable::mappingVersionColumn()].data<std::string>();
00631     dest.insert( mappingVersion );
00632   }
00633   return ret;
00634 }
00635 
00636 bool ora::OraMappingSchema::getMapping( const std::string& version,
00637                                         ora::MappingRawData& dest ){
00638   bool ret = false;
00639   coral::ITable& mappingTable = m_schema.tableHandle( OraMappingElementTable::tableName() );
00640   std::auto_ptr<coral::IQuery> query(mappingTable.newQuery());
00641   coral::AttributeList outputBuffer;
00642   outputBuffer.extend<int>( OraMappingElementTable::elementIdColumn() );
00643   outputBuffer.extend<std::string>( OraMappingElementTable::elementTypeColumn() );
00644   outputBuffer.extend<std::string>( OraMappingElementTable::scopeNameColumn() );
00645   outputBuffer.extend<std::string>( OraMappingElementTable::variableNameColumn() );
00646   outputBuffer.extend<std::string>( OraMappingElementTable::variableTypeColumn() );
00647   outputBuffer.extend<std::string>( OraMappingElementTable::tableNameColumn() );
00648   outputBuffer.extend<std::string>( OraMappingElementTable::columnNameColumn() );
00649   query->defineOutput( outputBuffer );
00650   query->addToOutputList( OraMappingElementTable::elementIdColumn() );
00651   query->addToOutputList( OraMappingElementTable::elementTypeColumn() );
00652   query->addToOutputList( OraMappingElementTable::scopeNameColumn() );
00653   query->addToOutputList( OraMappingElementTable::variableNameColumn() );
00654   query->addToOutputList( OraMappingElementTable::variableTypeColumn() );
00655   query->addToOutputList( OraMappingElementTable::tableNameColumn() );
00656   query->addToOutputList( OraMappingElementTable::columnNameColumn() );
00657   std::ostringstream condition;
00658   condition << OraMappingElementTable::mappingVersionColumn()<<"= :"<< OraMappingElementTable::mappingVersionColumn();
00659   coral::AttributeList condData;
00660   condData.extend<std::string>( OraMappingElementTable::mappingVersionColumn() );
00661   coral::AttributeList::iterator iAttribute = condData.begin();
00662   iAttribute->data< std::string >() = version;
00663   query->setCondition( condition.str(), condData );
00664   query->addToOrderList( OraMappingElementTable::scopeNameColumn() );
00665   query->addToOrderList( OraMappingElementTable::variableNameColumn() );
00666   query->addToOrderList( OraMappingElementTable::variableParIndexColumn() );
00667   coral::ICursor& cursor = query->execute();
00668   while ( cursor.next() ) {
00669     ret = true;
00670     const coral::AttributeList& currentRow = cursor.currentRow();
00671     int elementId = currentRow[ OraMappingElementTable::elementIdColumn() ].data<int>();
00672     MappingRawElement& elem = dest.addElement( elementId );
00673     elem.elementType = currentRow[ OraMappingElementTable::elementTypeColumn() ].data<std::string>();
00674     elem.scopeName = currentRow[ OraMappingElementTable::scopeNameColumn() ].data<std::string>();
00675     elem.variableName = currentRow[ OraMappingElementTable::variableNameColumn() ].data<std::string>();
00676     elem.variableType = currentRow[ OraMappingElementTable::variableTypeColumn() ].data<std::string>();
00677     elem.tableName = currentRow[ OraMappingElementTable::tableNameColumn() ].data<std::string>();
00678     elem.columns.push_back( currentRow[ OraMappingElementTable::columnNameColumn() ].data<std::string>() );
00679   }
00680   return ret;
00681 }
00682 
00683 void ora::OraMappingSchema::storeMapping( const MappingRawData& mapping ){
00684   // first update the version table
00685   coral::ITable& mappingVersionTable = m_schema.tableHandle( OraMappingVersionTable::tableName() );
00686   coral::AttributeList  rowBuffer;
00687   rowBuffer.extend< std::string >( OraMappingVersionTable::mappingVersionColumn() );
00688   rowBuffer[ OraMappingVersionTable::mappingVersionColumn() ].data<std::string>()= mapping.version;
00689   mappingVersionTable.dataEditor().insertRow( rowBuffer );
00690 
00691   // then update the element tables
00692   coral::ITable& mappingElementTable = m_schema.tableHandle( OraMappingElementTable::tableName() );
00693   coral::AttributeList  dataBuffer;
00694   dataBuffer.extend< std::string >( OraMappingElementTable::mappingVersionColumn() );
00695   dataBuffer.extend< int >( OraMappingElementTable::elementIdColumn() );
00696   dataBuffer.extend< std::string >( OraMappingElementTable::elementTypeColumn() );
00697   dataBuffer.extend< std::string >( OraMappingElementTable::scopeNameColumn() );
00698   dataBuffer.extend< std::string >( OraMappingElementTable::variableNameColumn() );
00699   dataBuffer.extend< unsigned int >( OraMappingElementTable::variableParIndexColumn() );
00700   dataBuffer.extend< std::string >( OraMappingElementTable::variableTypeColumn() );
00701   dataBuffer.extend< std::string >( OraMappingElementTable::tableNameColumn() );
00702   dataBuffer.extend< std::string >( OraMappingElementTable::columnNameColumn() );
00703   dataBuffer[ OraMappingElementTable::mappingVersionColumn() ].data<std::string>()= mapping.version;
00704 
00705   for( std::map < int, MappingRawElement >::const_iterator iElem = mapping.elements.begin();
00706        iElem != mapping.elements.end(); iElem++ ){
00707     for( size_t iParamIndex = 0; iParamIndex < iElem->second.columns.size(); iParamIndex++ ){
00708       dataBuffer[ OraMappingElementTable::elementIdColumn() ].data<int>() = iElem->first;
00709       dataBuffer[ OraMappingElementTable::elementTypeColumn()].data<std::string>()=  iElem->second.elementType;
00710       dataBuffer[ OraMappingElementTable::scopeNameColumn() ].data<std::string>()= iElem->second.scopeName;
00711       dataBuffer[ OraMappingElementTable::variableNameColumn() ].data<std::string>()= iElem->second.variableName;
00712       dataBuffer[ OraMappingElementTable::variableParIndexColumn() ].data<unsigned int>() = iParamIndex;
00713       dataBuffer[ OraMappingElementTable::variableTypeColumn() ].data<std::string>()= iElem->second.variableType;
00714       dataBuffer[ OraMappingElementTable::tableNameColumn() ].data<std::string>()= iElem->second.tableName;
00715       dataBuffer[ OraMappingElementTable::columnNameColumn() ].data<std::string>()= iElem->second.columns[iParamIndex];
00716       mappingElementTable.dataEditor().insertRow( dataBuffer );
00717     }
00718   }
00719 }
00720 
00721 void ora::OraMappingSchema::removeMapping( const std::string& version ){
00722   // Remove all rows in the tables with the version.
00723   coral::AttributeList whereData;
00724   whereData.extend<std::string>( OraMappingVersionTable::mappingVersionColumn() );
00725   whereData.begin()->data<std::string>() = version;
00726 
00727   std::string condition = OraMappingVersionTable::mappingVersionColumn() + " = :" + OraMappingVersionTable::mappingVersionColumn();
00728   m_schema.tableHandle( OraClassVersionTable::tableName() ).dataEditor().deleteRows( condition, whereData );
00729   m_schema.tableHandle( OraMappingElementTable::tableName() ).dataEditor().deleteRows( condition, whereData );
00730   m_schema.tableHandle( OraMappingVersionTable::tableName() ).dataEditor().deleteRows( condition, whereData );
00731 }
00732 
00733 bool ora::OraMappingSchema::getContainerTableMap( std::map<std::string, int>& dest ){
00734   bool ret = false;
00735   std::auto_ptr<coral::IQuery> query(m_schema.newQuery());
00736   query->addToTableList( OraMappingElementTable::tableName(),"T0");
00737   query->addToTableList( OraClassVersionTable::tableName(), "T1");
00738   query->setDistinct();
00739   query->addToOutputList( "T0."+ OraMappingElementTable::tableNameColumn() );
00740   query->addToOutputList( "T1."+ OraClassVersionTable::containerIdColumn());
00741   std::ostringstream condition;
00742   condition << "T0."<<OraMappingElementTable::mappingVersionColumn()<<"="<< "T1."<< OraClassVersionTable::mappingVersionColumn();
00743   coral::AttributeList condData;
00744   query->setCondition(condition.str(),condData);
00745   coral::ICursor& cursor = query->execute();
00746   while ( cursor.next() ) {
00747     ret = true;
00748     const coral::AttributeList& currentRow = cursor.currentRow();
00749     std::string tableName = currentRow[ "T0."+ OraMappingElementTable::tableNameColumn()].data<std::string>();
00750     int containerId = currentRow[ "T1."+ OraClassVersionTable::containerIdColumn()].data<int>();
00751     dest.insert(std::make_pair(tableName,containerId));
00752   }
00753   return ret;
00754 }
00755 
00781 bool ora::OraMappingSchema::getDependentClassesInContainerMapping( int containerId,
00782                                                                    std::set<std::string>& destination ){
00783   bool ret = false;
00784   std::auto_ptr<coral::IQuery> query( m_schema.tableHandle( OraClassVersionTable::tableName() ).newQuery() );
00785   query->setDistinct();
00786   query->addToOutputList( OraClassVersionTable::classNameColumn() );
00787   std::ostringstream condition;
00788   condition <<OraClassVersionTable::containerIdColumn()<<" =:"<<OraClassVersionTable::containerIdColumn();
00789   condition << " AND "<< OraClassVersionTable::dependencyIndexColumn()<<" > 0";
00790   coral::AttributeList condData;
00791   condData.extend< int >( OraClassVersionTable::containerIdColumn() );
00792   condData[ OraClassVersionTable::containerIdColumn() ].data< int >() = containerId;
00793   query->setCondition(condition.str(),condData);
00794   coral::ICursor& cursor = query->execute();
00795   while ( cursor.next() ) {
00796     ret = true;
00797     const coral::AttributeList& currentRow = cursor.currentRow();
00798     std::string className = currentRow[ OraClassVersionTable::classNameColumn() ].data<std::string>();
00799     destination.insert( className );
00800   }
00801   return ret;
00802 }
00803 
00804 bool ora::OraMappingSchema::getClassVersionListForMappingVersion( const std::string& mappingVersion,
00805                                                                   std::set<std::string>& destination ){
00806   
00807   bool ret = false;
00808   std::auto_ptr<coral::IQuery> query( m_schema.tableHandle( OraClassVersionTable::tableName() ).newQuery() );
00809   query->setDistinct();
00810   query->addToOutputList( OraClassVersionTable::classVersionColumn() );
00811   std::ostringstream condition;
00812   condition <<OraClassVersionTable::mappingVersionColumn()<<" =:"<<OraClassVersionTable::mappingVersionColumn();
00813   coral::AttributeList condData;
00814   condData.extend< std::string >( OraClassVersionTable::mappingVersionColumn() );  
00815   condData[ OraClassVersionTable::mappingVersionColumn() ].data< std::string >() = mappingVersion;
00816   query->setCondition(condition.str(),condData);
00817   coral::ICursor& cursor = query->execute();
00818   while ( cursor.next() ) {
00819     ret = true;
00820     const coral::AttributeList& currentRow = cursor.currentRow();
00821     std::string classVersion = currentRow[ OraClassVersionTable::classVersionColumn() ].data<std::string>();
00822     destination.insert( classVersion );
00823   }
00824   return ret;
00825 }
00826 
00827 bool ora::OraMappingSchema::getMappingVersionListForContainer( int containerId,
00828                                                                std::set<std::string>& dest,
00829                                                                bool onlyDependency ){
00830   bool ret = false;
00831   std::auto_ptr<coral::IQuery> query( m_schema.tableHandle( OraClassVersionTable::tableName() ).newQuery() );
00832   query->setDistinct();
00833   query->addToOutputList( OraClassVersionTable::mappingVersionColumn() );
00834   std::ostringstream condition;
00835   condition <<OraClassVersionTable::containerIdColumn()<<" =:"<<OraClassVersionTable::containerIdColumn();
00836   coral::AttributeList condData;
00837   condData.extend< int >( OraClassVersionTable::containerIdColumn() );
00838   if( onlyDependency ){
00839     condition << " AND "<<OraClassVersionTable::dependencyIndexColumn()<<" > 0";
00840   }
00841   condData[ OraClassVersionTable::containerIdColumn() ].data< int >() = containerId;
00842   query->setCondition(condition.str(),condData);
00843   coral::ICursor& cursor = query->execute();
00844   while ( cursor.next() ) {
00845     ret = true;
00846     const coral::AttributeList& currentRow = cursor.currentRow();
00847     std::string mappingVersion = currentRow[ OraClassVersionTable::mappingVersionColumn() ].data<std::string>();
00848     dest.insert( mappingVersion );
00849   }
00850   return ret;
00851 }
00852 
00853 bool ora::OraMappingSchema::getClassVersionListForContainer( int containerId,
00854                                                              std::map<std::string,std::string>& versionMap ){
00855   bool ret = false;
00856   std::auto_ptr<coral::IQuery> query( m_schema.tableHandle( OraClassVersionTable::tableName() ).newQuery() );
00857   query->setDistinct();
00858   query->addToOutputList( OraClassVersionTable::classVersionColumn() );
00859   query->addToOutputList( OraClassVersionTable::mappingVersionColumn() );
00860   std::ostringstream condition;
00861   condition <<OraClassVersionTable::containerIdColumn()<<" =:"<<OraClassVersionTable::containerIdColumn();
00862   coral::AttributeList condData;
00863   condData.extend< int >( OraClassVersionTable::containerIdColumn() );
00864   condData[ OraClassVersionTable::containerIdColumn() ].data< int >() = containerId;
00865   query->setCondition(condition.str(),condData);
00866   coral::ICursor& cursor = query->execute();
00867   while ( cursor.next() ) {
00868     ret = true;
00869     const coral::AttributeList& currentRow = cursor.currentRow();
00870     std::string classVersion = currentRow[ OraClassVersionTable::classVersionColumn() ].data<std::string>();
00871     std::string mappingVersion = currentRow[ OraClassVersionTable::mappingVersionColumn() ].data<std::string>();
00872     versionMap.insert( std::make_pair(classVersion,mappingVersion ) );
00873   }
00874   return ret;
00875 }
00876 
00877 bool ora::OraMappingSchema::getMappingVersionListForTable( const std::string& tableName,
00878                                                            std::set<std::string>& destination )
00879 {
00880   bool ret = false;
00881   destination.clear();
00882   std::auto_ptr<coral::IQuery> query( m_schema.tableHandle( OraMappingElementTable::tableName() ).newQuery() );
00883   query->setDistinct();
00884   query->addToOutputList( OraMappingElementTable::mappingVersionColumn() );
00885   std::ostringstream condition;
00886   condition << OraMappingElementTable::tableNameColumn()<<" = :"<< OraMappingElementTable::tableNameColumn();
00887   coral::AttributeList condData;
00888   condData.extend< std::string >( OraMappingElementTable::tableNameColumn() );
00889   condData.begin()->data<std::string>() = tableName;
00890   query->setCondition(condition.str(),condData);
00891   coral::ICursor& cursor = query->execute();
00892   while ( cursor.next() ) {
00893     ret = true;
00894     const coral::AttributeList& currentRow = cursor.currentRow();
00895     std::string mappingVersion = currentRow[ OraMappingElementTable::mappingVersionColumn()].data<std::string>();
00896     destination.insert( mappingVersion );
00897   }
00898   return ret;
00899 }
00900 
00901 bool ora::OraMappingSchema::selectMappingVersion( const std::string& classId,
00902                                                   int containerId,
00903                                                   std::string& destination ){
00904   bool ret = false;
00905   destination.clear();
00906   std::auto_ptr<coral::IQuery> query( m_schema.tableHandle( OraClassVersionTable::tableName() ).newQuery() );
00907   query->addToOutputList( OraClassVersionTable::mappingVersionColumn() );
00908   std::ostringstream condition;
00909   condition << OraClassVersionTable::classIdColumn() << " =:" << OraClassVersionTable::classIdColumn() << " AND ";
00910   condition << OraClassVersionTable::containerIdColumn() << " =:" << OraClassVersionTable::containerIdColumn();
00911   coral::AttributeList condData;
00912   condData.extend<std::string>( OraClassVersionTable::classIdColumn() );
00913   condData.extend<int>( OraClassVersionTable::containerIdColumn() );
00914   coral::AttributeList::iterator iAttribute = condData.begin();
00915   iAttribute->data< std::string >() = classId;
00916   ++iAttribute;
00917   iAttribute->data< int >() = containerId;
00918   query->setCondition( condition.str(), condData );
00919   coral::ICursor& cursor = query->execute();
00920   while ( cursor.next() ) {
00921     ret = true;
00922     const coral::AttributeList& currentRow = cursor.currentRow();
00923     destination = currentRow[OraClassVersionTable::mappingVersionColumn()].data<std::string>();
00924   }
00925   return ret;  
00926 }
00927 
00928 bool ora::OraMappingSchema::containerForMappingVersion( const std::string& mappingVersion,
00929                                                         int& destination ){
00930   bool ret = false;
00931   std::auto_ptr<coral::IQuery> query( m_schema.tableHandle( OraClassVersionTable::tableName() ).newQuery() );
00932   query->addToOutputList( OraClassVersionTable::containerIdColumn() );
00933   std::ostringstream condition;
00934   condition << OraClassVersionTable::mappingVersionColumn() << " =:"<< OraClassVersionTable::mappingVersionColumn();
00935   coral::AttributeList condData;
00936   condData.extend<std::string>( OraClassVersionTable::mappingVersionColumn() );
00937   coral::AttributeList::iterator iAttribute = condData.begin();
00938   iAttribute->data< std::string >() = mappingVersion;
00939   query->setCondition( condition.str(), condData );
00940   coral::ICursor& cursor = query->execute();
00941   while ( cursor.next() ) {
00942     ret = true;
00943     const coral::AttributeList& currentRow = cursor.currentRow();
00944     destination = currentRow[ OraClassVersionTable::containerIdColumn() ].data<int>();
00945   }
00946   return ret;
00947 }
00948 
00949 void ora::OraMappingSchema::insertClassVersion( const std::string& className,
00950                                                 const std::string& classVersion,
00951                                                 const std::string& classId,
00952                                                 int dependencyIndex,
00953                                                 int containerId,
00954                                                 const std::string& mappingVersion ){
00955   coral::ITable& classVersionTable = m_schema.tableHandle( OraClassVersionTable::tableName() );
00956   coral::AttributeList inputData;
00957   inputData.extend<std::string>( OraClassVersionTable::mappingVersionColumn());
00958   inputData.extend<std::string>( OraClassVersionTable::classNameColumn());
00959   inputData.extend<std::string>( OraClassVersionTable::classVersionColumn());
00960   inputData.extend<std::string>( OraClassVersionTable::classIdColumn());
00961   inputData.extend<int>( OraClassVersionTable::dependencyIndexColumn());
00962   inputData.extend<int>( OraClassVersionTable::containerIdColumn());
00963   coral::AttributeList::iterator iInAttr = inputData.begin();
00964   iInAttr->data< std::string >() = mappingVersion;
00965   ++iInAttr;
00966   iInAttr->data< std::string >() = className;
00967   ++iInAttr;
00968   iInAttr->data< std::string >() = classVersion;
00969   ++iInAttr;
00970   iInAttr->data< std::string >() = classId;
00971   ++iInAttr;
00972   iInAttr->data< int >() = dependencyIndex;
00973   ++iInAttr;
00974   iInAttr->data< int >() = containerId;
00975   classVersionTable.dataEditor().insertRow( inputData );
00976 }
00977 
00978 void ora::OraMappingSchema::setMappingVersion( const std::string& classId,
00979                                                int containerId,
00980                                                const std::string& mappingVersion ){
00981   coral::ITable& classVersionTable = m_schema.tableHandle( OraClassVersionTable::tableName() );
00982   coral::AttributeList inputData;
00983   inputData.extend<std::string>( OraClassVersionTable::mappingVersionColumn());
00984   inputData.extend<std::string>( OraClassVersionTable::classIdColumn());
00985   inputData.extend<int>( OraClassVersionTable::containerIdColumn());
00986   coral::AttributeList::iterator iInAttr = inputData.begin();
00987   iInAttr->data< std::string >() = mappingVersion;
00988   ++iInAttr;
00989   iInAttr->data< std::string >() = classId;
00990   ++iInAttr;
00991   iInAttr->data< int >() = containerId;
00992   std::string setClause = OraClassVersionTable::mappingVersionColumn()+" =:"+ OraClassVersionTable::mappingVersionColumn();
00993   std::string whereClause = OraClassVersionTable::classIdColumn()+" =:"+ OraClassVersionTable::classIdColumn()+" AND "+
00994     OraClassVersionTable::containerIdColumn()+" =:"+ OraClassVersionTable::containerIdColumn();
00995   classVersionTable.dataEditor().updateRows( setClause,whereClause, inputData  );
00996 }
00997 
00998 bool ora::OraDatabaseSchema::existsMainTable( coral::ISchema& dbSchema ){
00999   OraMainTable tmp( dbSchema );
01000   return tmp.exists();
01001 }
01002 
01003 std::string& ora::OraNamingServiceTable::tableName(){
01004   static std::string s_table("ORA_NAMING_SERVICE" );
01005   return s_table;
01006 }
01007 
01008 std::string& ora::OraNamingServiceTable::objectNameColumn(){
01009   static std::string s_column("OBJECT_NAME");
01010   return s_column;  
01011 }
01012 
01013 std::string& ora::OraNamingServiceTable::containerIdColumn(){
01014   static std::string s_column("CONTAINER_ID");
01015   return s_column;  
01016 }
01017 
01018 std::string& ora::OraNamingServiceTable::itemIdColumn(){
01019   static std::string s_column("ITEM_ID");
01020   return s_column;  
01021 }
01022 
01023 ora::OraNamingServiceTable::OraNamingServiceTable( coral::ISchema& dbSchema ): m_schema( dbSchema ){
01024 }
01025 
01026 ora::OraNamingServiceTable::~OraNamingServiceTable(){
01027 }
01028 
01029 bool ora::OraNamingServiceTable::exists(){
01030   return m_schema.existsTable( tableName() );
01031 }
01032 
01033 void ora::OraNamingServiceTable::create(){
01034   if( m_schema.existsTable( tableName() )){
01035     throwException( "ORA naming service table already exists in this schema.",
01036                     "OraNameTable::create");
01037   }
01038   
01039   coral::TableDescription descr( "OraDb" );
01040   descr.setName( tableName() );
01041   descr.insertColumn( objectNameColumn(),
01042                       coral::AttributeSpecification::typeNameForType<std::string>() );
01043   descr.insertColumn( containerIdColumn(),
01044                       coral::AttributeSpecification::typeNameForType<int>() );
01045   descr.insertColumn( itemIdColumn(),
01046                       coral::AttributeSpecification::typeNameForType<int>() );
01047   descr.setNotNullConstraint( objectNameColumn() );
01048   descr.setNotNullConstraint( containerIdColumn() );
01049   descr.setNotNullConstraint( itemIdColumn() );
01050   descr.setPrimaryKey( std::vector<std::string>( 1, objectNameColumn() ) );
01051 
01052   coral::ITable& table = m_schema.createTable( descr );
01053   table.privilegeManager().grantToPublic( coral::ITablePrivilegeManager::Select );
01054 }
01055 
01056 void ora::OraNamingServiceTable::drop(){
01057   m_schema.dropIfExistsTable( tableName() );
01058 }
01059 
01060 void ora::OraNamingServiceTable::setObjectName( const std::string& name, 
01061                                                 int contId, 
01062                                                 int itemId ){
01063   coral::AttributeList dataToInsert;
01064   dataToInsert.extend<std::string>( objectNameColumn() );
01065   dataToInsert.extend<int>( containerIdColumn());
01066   dataToInsert.extend<int>( itemIdColumn());
01067   dataToInsert[ objectNameColumn() ].data<std::string>() = name;
01068   dataToInsert[ containerIdColumn() ].data<int>()  = contId;
01069   dataToInsert[ itemIdColumn() ].data<int>()  = itemId;
01070   coral::ITable& containerTable = m_schema.tableHandle( tableName() );
01071   containerTable.dataEditor().insertRow( dataToInsert );  
01072 }
01073 
01074 bool ora::OraNamingServiceTable::eraseObjectName( const std::string& name ){
01075   coral::AttributeList whereData;
01076   whereData.extend<std::string>( objectNameColumn() );
01077   whereData.begin()->data<std::string>() = name;
01078   std::string condition = objectNameColumn() + " = :" + objectNameColumn();
01079   return m_schema.tableHandle( tableName() ).dataEditor().deleteRows( condition, whereData )>0;
01080 }
01081 
01082 bool ora::OraNamingServiceTable::eraseAllNames(){
01083   std::string condition("");
01084   coral::AttributeList whereData;
01085   return m_schema.tableHandle( tableName() ).dataEditor().deleteRows( condition, whereData )>0;
01086 }
01087 
01088 bool ora::OraNamingServiceTable::getObjectByName( const std::string& name, 
01089                                                   std::pair<int,int>& destination ){
01090   bool ret = false;
01091   coral::ITable& containerTable = m_schema.tableHandle( tableName() );
01092   std::auto_ptr<coral::IQuery> query( containerTable.newQuery());
01093   coral::AttributeList outputBuffer;
01094   outputBuffer.extend<int>( containerIdColumn() );
01095   outputBuffer.extend<int>( itemIdColumn() );
01096   query->defineOutput( outputBuffer );
01097   query->addToOutputList( containerIdColumn() );
01098   query->addToOutputList( itemIdColumn() );
01099   std::ostringstream condition;
01100   condition << objectNameColumn()<<"= :"<< objectNameColumn();
01101   coral::AttributeList condData;
01102   condData.extend<std::string>( objectNameColumn() );
01103   coral::AttributeList::iterator iAttribute = condData.begin();
01104   iAttribute->data< std::string >() = name;
01105   query->setCondition( condition.str(), condData );
01106   coral::ICursor& cursor = query->execute();
01107   while ( cursor.next() ) {
01108     ret = true;
01109     const coral::AttributeList& row = cursor.currentRow();
01110     int containerId = row[ containerIdColumn() ].data< int >();
01111     int itemId = row[ itemIdColumn() ].data< int >();
01112     destination.first = containerId;
01113     destination.second = itemId;
01114   }
01115   return ret;
01116 }
01117 
01118 bool ora::OraNamingServiceTable::getNamesForObject( int contId, 
01119                                                     int itemId, 
01120                                                     std::vector<std::string>& destination ){
01121   bool ret = false;
01122   coral::ITable& containerTable = m_schema.tableHandle( tableName() );
01123   std::auto_ptr<coral::IQuery> query( containerTable.newQuery());
01124   coral::AttributeList outputBuffer;
01125   outputBuffer.extend<std::string>( objectNameColumn() );
01126   query->defineOutput( outputBuffer );
01127   query->addToOutputList( objectNameColumn() );
01128   std::ostringstream condition;
01129   condition << containerIdColumn()<<"= :"<< containerIdColumn();
01130   condition << " AND ";
01131   condition << itemIdColumn()<<"= :"<< itemIdColumn();
01132   coral::AttributeList condData;
01133   condData.extend<int>( containerIdColumn() );
01134   condData.extend<int>( itemIdColumn() );
01135   coral::AttributeList::iterator iAttribute = condData.begin();
01136   iAttribute->data< int >() = contId;
01137   ++iAttribute;
01138   iAttribute->data< int >() = itemId;
01139   query->setCondition( condition.str(), condData );
01140   coral::ICursor& cursor = query->execute();
01141   while ( cursor.next() ) {
01142     ret = true;
01143     const coral::AttributeList& row = cursor.currentRow();
01144     std::string name = row[ objectNameColumn() ].data< std::string >();
01145     destination.push_back( name );
01146   }
01147   return ret;
01148 }
01149 
01150 bool ora::OraNamingServiceTable::getNamesForContainer( int contId, 
01151                                                        std::vector<std::string>& destination ){
01152   bool ret = false;
01153   coral::ITable& containerTable = m_schema.tableHandle( tableName() );
01154   std::auto_ptr<coral::IQuery> query( containerTable.newQuery());
01155   coral::AttributeList outputBuffer;
01156   outputBuffer.extend<std::string>( objectNameColumn() );
01157   query->defineOutput( outputBuffer );
01158   query->addToOutputList( objectNameColumn() );
01159   std::ostringstream condition;
01160   condition << containerIdColumn()<<"= :"<< containerIdColumn();
01161   coral::AttributeList condData;
01162   condData.extend<int>( containerIdColumn() );
01163   coral::AttributeList::iterator iAttribute = condData.begin();
01164   iAttribute->data< int >() = contId;
01165   query->setCondition( condition.str(), condData );
01166   coral::ICursor& cursor = query->execute();
01167   while ( cursor.next() ) {
01168     ret = true;
01169     const coral::AttributeList& row = cursor.currentRow();
01170     std::string name = row[ objectNameColumn() ].data< std::string >();
01171     destination.push_back( name );
01172   }
01173   return ret;
01174 }
01175 
01176 bool ora::OraNamingServiceTable::getAllNames( std::vector<std::string>& destination ){
01177   bool ret = false;
01178   coral::ITable& containerTable = m_schema.tableHandle( tableName() );
01179   std::auto_ptr<coral::IQuery> query( containerTable.newQuery());
01180   coral::AttributeList outputBuffer;
01181   outputBuffer.extend<std::string>( objectNameColumn() );
01182   query->defineOutput( outputBuffer );
01183   query->addToOutputList( objectNameColumn() );
01184   coral::ICursor& cursor = query->execute();
01185   while ( cursor.next() ) {
01186     ret = true;
01187     const coral::AttributeList& row = cursor.currentRow();
01188     std::string name = row[ objectNameColumn() ].data< std::string >();
01189     destination.push_back( name );
01190   }
01191   return ret;
01192 }
01193 
01194 ora::OraDatabaseSchema::OraDatabaseSchema( coral::ISchema& dbSchema ):
01195   IDatabaseSchema( dbSchema ),
01196   m_schema( dbSchema ),
01197   m_mainTable( dbSchema ),
01198   m_sequenceTable( dbSchema ),
01199   m_mappingVersionTable( dbSchema ),
01200   m_mappingElementTable( dbSchema ),
01201   m_containerHeaderTable( dbSchema ),
01202   m_classVersionTable( dbSchema ),
01203   m_mappingSchema( dbSchema ),
01204   m_namingServiceTable( dbSchema ){
01205 }
01206 
01207 ora::OraDatabaseSchema::~OraDatabaseSchema(){
01208 }
01209 
01210 bool ora::OraDatabaseSchema::exists(){
01211   if(!m_mainTable.exists()){
01212     return false;
01213   }
01214   if(!m_sequenceTable.exists() ||
01215      !m_mappingVersionTable.exists() ||
01216      !m_mappingElementTable.exists() ||
01217      !m_containerHeaderTable.exists() ||
01218      !m_classVersionTable.exists() || 
01219      !m_namingServiceTable.exists()){
01220     throwException( "ORA database is corrupted..",
01221                     "OraDatabaseSchema::exists");
01222   }
01223   return true;
01224 }
01225 
01226 void ora::OraDatabaseSchema::create(){
01227   m_mainTable.create();
01228   m_sequenceTable.create();
01229   m_mappingVersionTable.create();
01230   m_mappingElementTable.create();
01231   m_containerHeaderTable.create();
01232   m_classVersionTable.create();
01233   m_namingServiceTable.create();
01234 }
01235 
01236 void ora::OraDatabaseSchema::drop(){
01237   m_namingServiceTable.drop();
01238   m_classVersionTable.drop();
01239   m_containerHeaderTable.drop();
01240   m_mappingElementTable.drop();
01241   m_mappingVersionTable.drop();
01242   m_sequenceTable.drop();
01243   m_mainTable.drop(); 
01244 }
01245 
01246 ora::IMainTable& ora::OraDatabaseSchema::mainTable(){
01247   return m_mainTable;
01248 }
01249 
01250 ora::ISequenceTable& ora::OraDatabaseSchema::sequenceTable(){
01251   return m_sequenceTable;
01252 }
01253 
01254 ora::IDatabaseTable& ora::OraDatabaseSchema::mappingVersionTable(){
01255   return m_mappingVersionTable;  
01256 }
01257 
01258 ora::IDatabaseTable& ora::OraDatabaseSchema::mappingElementTable(){
01259   return m_mappingElementTable;  
01260 }
01261 
01262 ora::IContainerHeaderTable& ora::OraDatabaseSchema::containerHeaderTable(){
01263   return m_containerHeaderTable;
01264 }
01265 
01266 ora::IDatabaseTable& ora::OraDatabaseSchema::classVersionTable(){
01267   return m_classVersionTable;  
01268 }
01269 
01270 ora::IMappingSchema& ora::OraDatabaseSchema::mappingSchema(){
01271   return m_mappingSchema;  
01272 }
01273 
01274 ora::INamingServiceTable& ora::OraDatabaseSchema::namingServiceTable(){
01275   return m_namingServiceTable;
01276 }
01277   
01278