CMS 3D CMS Logo

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