Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

CdbSQLApiDatabase.cc

Go to the documentation of this file.
00001 // File and Version Information:
00002 //      $Id: CdbSQLApiDatabase.cc,v 1.2 2005/10/22 00:40:54 gapon Exp $
00003 
00004 /// The implementation of the CdbSQLApiDatabase class.
00005 /**
00006   * @see CdbSQLApiDatabase
00007   */
00008 
00009 #include "BaBar/BaBar.hh"
00010 
00011 #include "CdbSQL/CdbSQLApiDatabase.hh"
00012 #include "CdbSQL/CdbSQLApiView.hh"
00013 #include "CdbSQL/CdbSQLApiCondition.hh"
00014 #include "CdbSQL/CdbSQL.hh"
00015 #include "CdbSQL/CdbSQLOrigin.hh"
00016 #include "CdbSQL/CdbSQLView.hh"
00017 #include "CdbSQL/CdbSQLPartition.hh"
00018 #include "CdbSQL/CdbSQLCondition.hh"
00019 #include "CdbSQL/CdbSQLConditionAtFolder.hh"
00020 
00021 #include "CdbBase/Cdb.hh"
00022 #include "CdbBase/CdbOrigin.hh"
00023 #include "CdbBase/CdbView.hh"
00024 #include "CdbBase/CdbId.hh"
00025 #include "CdbBase/CdbCompositeName.hh"
00026 #include "CdbBase/CdbEnvironment.hh"
00027 #include "CdbBase/CdbProperty.hh"
00028 #include "CdbBase/CdbFolder.hh"
00029 #include "CdbBase/CdbCondition.hh"
00030 #include "CdbBase/CdbPartition.hh"
00031 #include "CdbBase/CdbVectorAdapterItr.hh"
00032 #include "CdbBase/CdbEmptyItr.hh"
00033 #include "CdbBase/CdbSimpleVectorAdapterItr.hh"
00034 #include "CdbBase/CdbSTD2CStrAdapterItr.hh"
00035 
00036 #include <iostream>
00037 using std::cout;
00038 using std::endl;
00039 
00040 namespace {
00041 
00042   /////////////////////////////////////
00043   // Class CdbSQLApiPartitionItr //
00044   /////////////////////////////////////
00045 
00046     class CdbSQLApiPartitionItr : public CdbVectorAdapterItr<unsigned short, CdbCPtr<CdbSQLPartition> > {
00047 
00048     private:
00049 
00050       /// The default constructor (NOT IMPLEMENTED)
00051 
00052         CdbSQLApiPartitionItr( );
00053 
00054       /// The assignment operator (NOT IMPLEMENTED)
00055 
00056         CdbSQLApiPartitionItr& operator=( const CdbSQLApiPartitionItr& );
00057 
00058     protected:
00059 
00060       /// The copy constructor
00061 
00062         CdbSQLApiPartitionItr( const CdbSQLApiPartitionItr& theItr ) :
00063             CdbVectorAdapterItr<unsigned short, CdbCPtr<CdbSQLPartition> >( theItr )
00064         { }
00065 
00066     public:
00067 
00068       /// The normal constructor
00069       /**
00070         * @see CdbVectorAdapterItr::CdbVectorAdapterItr()
00071         */
00072         CdbSQLApiPartitionItr( const std::vector< CdbCPtr<CdbSQLPartition> >& theList ) :
00073             CdbVectorAdapterItr<unsigned short, CdbCPtr<CdbSQLPartition> >( theList )
00074         { }
00075 
00076       /// Make a clone of itself
00077       /**
00078         * @see CdbIItr::clone()
00079         */
00080         virtual CdbIItr<unsigned short>* clone( ) const
00081         {
00082             return new CdbSQLApiPartitionItr( *this );
00083         }
00084 
00085     protected:
00086 
00087       /// User defined translation for the currently reffered value
00088       /**
00089         * @see CdbVectorAdapterItr::CdbVectorAdapterItr()
00090         */
00091         virtual ValueType toValue( const CdbCPtr<CdbSQLPartition>& thePtr ) const
00092         {
00093             return thePtr->id( );
00094         }
00095     };
00096 
00097   //////////////////////////////////
00098   // Class CdbSQLApiPartition //
00099   //////////////////////////////////
00100 
00101     class CdbSQLApiPartition : public CdbPartition {
00102 
00103     private:
00104 
00105       /// The default constructor (NOT IMPLEMENTED)
00106       /**
00107         */
00108         CdbSQLApiPartition( );
00109 
00110       /// The copy constructor (NOT IMPLEMENTED)
00111       /**
00112         * More details to come...
00113         */
00114         CdbSQLApiPartition( const CdbSQLApiPartition& thePartition );
00115 
00116       /// The assignment operator (NOT IMPLEMENTED)
00117       /**
00118         * More details to come...
00119         */
00120         CdbSQLApiPartition& operator=( const CdbSQLApiPartition& thePartition );
00121 
00122     public:
00123 
00124       /// The normal constructor
00125       /**
00126         */
00127         CdbSQLApiPartition( const CdbDatabasePtr&           theDatabasePtr,                      
00128                             const CdbCPtr<CdbSQLPartition>& thePartitionPtr,
00129                             const std::string&              theOwnerName );
00130 
00131       /// The destructor
00132       /**
00133         * Details...
00134         */
00135         virtual ~CdbSQLApiPartition( );
00136 
00137       /// The begin time in the "insertion" dimension
00138       /**
00139         * Implements the corresponding method of its base class or an interface.
00140         *
00141         * @see CdbPartition::endInsertion()
00142         */
00143         virtual BdbTime endInsertion( ) const;
00144 
00145       /// The recent modification time of the partition
00146       /**
00147         * Implements the corresponding method of its base class or an interface.
00148         *
00149         * @see CdbPartition::modified()
00150         */
00151         virtual BdbTime modified( ) const;
00152 
00153       /// The status of the partition
00154       /**
00155         * Implements the corresponding method of its base class or an interface.
00156         *
00157         * @see CdbPartition::isClosed()
00158         */
00159         virtual bool isClosed( ) const;
00160 
00161       /// The status of the partition
00162       /**
00163         * Implements the corresponding method of its base class or an interface.
00164         *
00165         * @see CdbPartition::isInstantiated()
00166         */
00167         virtual bool isInstantiated( ) const;
00168 
00169     private:
00170 
00171         CdbCPtr<CdbSQLPartition> _partitionPtr;
00172     };
00173 
00174     CdbSQLApiPartition::CdbSQLApiPartition( const CdbDatabasePtr&           theDatabasePtr,
00175                                             const CdbCPtr<CdbSQLPartition>& thePartitionPtr,
00176                                             const std::string&              theOwnerName ) :
00177         CdbPartition( theDatabasePtr,
00178                       thePartitionPtr->id( ),
00179                       thePartitionPtr->originId( ),
00180                       theOwnerName.c_str( ),
00181                       thePartitionPtr->description( ).c_str( ),
00182                       thePartitionPtr->created( ),
00183                       thePartitionPtr->beginValidity( ),
00184                       thePartitionPtr->endValidity( ),
00185                       thePartitionPtr->beginInsertion( )),
00186        _partitionPtr(thePartitionPtr)
00187     { }
00188 
00189     CdbSQLApiPartition::~CdbSQLApiPartition( )
00190     { }
00191 
00192     BdbTime 
00193     CdbSQLApiPartition::endInsertion( ) const
00194     {
00195         return _partitionPtr->endInsertion( );
00196     }
00197 
00198     BdbTime
00199     CdbSQLApiPartition::modified( ) const
00200     {
00201         return _partitionPtr->modified( );
00202     }
00203 
00204     bool
00205     CdbSQLApiPartition::isClosed( ) const
00206     {
00207         return _partitionPtr->isClosed( );
00208     }
00209 
00210     bool
00211     CdbSQLApiPartition::isInstantiated( ) const
00212     {
00213         return _partitionPtr->isInstantiated( );
00214     }
00215 
00216   //////////////////////////////////
00217   // Class CdbSQLApiOriginItr //
00218   //////////////////////////////////
00219 
00220     class CdbSQLApiOriginItr : public CdbVectorAdapterItr<unsigned short, CdbCPtr<CdbSQLOrigin> > {
00221 
00222     private:
00223 
00224       /// The default constructor (NOT IMPLEMENTED)
00225 
00226         CdbSQLApiOriginItr( );
00227 
00228       /// The assignment operator (NOT IMPLEMENTED)
00229 
00230         CdbSQLApiOriginItr& operator=( const CdbSQLApiOriginItr& );
00231 
00232     protected:
00233 
00234       /// The copy constructor
00235 
00236         CdbSQLApiOriginItr( const CdbSQLApiOriginItr& theItr ) :
00237             CdbVectorAdapterItr<unsigned short, CdbCPtr<CdbSQLOrigin> >( theItr )
00238         { }
00239 
00240     public:
00241 
00242       /// The normal constructor
00243       /**
00244         * @see CdbVectorAdapterItr::CdbVectorAdapterItr()
00245         */
00246         CdbSQLApiOriginItr( const std::vector< CdbCPtr<CdbSQLOrigin> >& theList ) :
00247             CdbVectorAdapterItr<unsigned short, CdbCPtr<CdbSQLOrigin> >( theList )
00248         { }
00249 
00250       /// Make a clone of itself
00251       /**
00252         * @see CdbIItr::clone()
00253         */
00254         virtual CdbIItr<unsigned short>* clone( ) const
00255         {
00256             return new CdbSQLApiOriginItr( *this );
00257         }
00258 
00259     protected:
00260 
00261       /// User defined translation for the currently reffered value
00262       /**
00263         * @see CdbVectorAdapterItr::CdbVectorAdapterItr()
00264         */
00265         virtual ValueType toValue( const CdbCPtr<CdbSQLOrigin>& thePtr ) const
00266         {
00267             return thePtr->id( );
00268         }
00269     };
00270 
00271   ///////////////////////////////
00272   // Class CdbSQLApiOrigin //
00273   ///////////////////////////////
00274 
00275   /// An implementation of a transient origin
00276   /**
00277     * More details to come...
00278     */
00279     class CdbSQLApiOrigin : public CdbOrigin {
00280 
00281     private:
00282 
00283       /// The constructor (NOT IMPLEMENTED)
00284       /**
00285         * Is disabled...
00286         */
00287         CdbSQLApiOrigin( );
00288 
00289       /// The copy constructor (NOT IMPLEMENTED)
00290       /**
00291         * Is disabled...
00292         */
00293         CdbSQLApiOrigin( const CdbSQLApiOrigin& theOrigin );
00294 
00295       /// The assignment operator (NOT IMPLEMENTED)
00296       /**
00297         * Is disabled...
00298         */
00299         CdbSQLApiOrigin& operator=( const CdbSQLApiOrigin& theOrigin );
00300 
00301     public:
00302 
00303       /// The normal constructor.
00304       /**
00305         * Initialize context with specified parameters.
00306         *
00307         * @see CdbDatabase
00308         * @see CdbDatabasePtr
00309         */
00310         CdbSQLApiOrigin( const CdbDatabasePtr&        theDatabasePtr,   /**< the smart pointer providing a back back link to the parent database object */
00311                          const CdbCPtr<CdbSQLOrigin>& theOriginPtr      /**< the persistent origin */
00312                        );
00313 
00314       /// The destructor.
00315       /**
00316         * More details...
00317         */
00318         virtual ~CdbSQLApiOrigin( );
00319 
00320       /// Obtain the creation time of the origin
00321       /**
00322         * This implements the corresponding method of the base class.
00323         *
00324         * @see CdbOrigin::created()
00325         */
00326         virtual BdbTime created( );
00327 
00328       /// Obtain the origin description
00329       /**
00330         * This implements the corresponding method of the base class.
00331         *
00332         * @see CdbOrigin::description()
00333         */
00334         virtual std::string description( );
00335 
00336       /// Check if this origin is the local one
00337       /**
00338         * This implements the corresponding method of the base class.
00339         *
00340         * @see CdbOrigin::isLocal()
00341         */
00342         virtual bool isLocal( ) const;
00343 
00344       /// Check if data corresponding to this origin are available in the local database
00345       /**
00346         * This implements the corresponding method of the base class.
00347         *
00348         * @see CdbOrigin::isInstantiated()
00349         */
00350         virtual bool isInstantiated( ) const;
00351 
00352       /// Check if this origin is the "master" one
00353       /**
00354         * This implements the corresponding method of the base class.
00355         *
00356         * @see CdbOrigin::isMaster()
00357         */
00358         virtual bool isMaster( ) const;
00359 
00360       /// Check if this origin is a "slave" one
00361       /**
00362         * This implements the corresponding method of the base class.
00363         *
00364         * @see CdbOrigin::isSlave()
00365         */
00366         virtual bool isSlave( ) const;
00367 
00368       /// Check if this origin is a "test" one
00369       /**
00370         * This implements the corresponding method of the base class.
00371         *
00372         * @see CdbOrigin::isTest()
00373         */
00374         virtual bool isTest( ) const;
00375 
00376       /// Set up an iterator of properties
00377       /**
00378         * This implements the corresponding method of the base class.
00379         *
00380         * @see CdbOrigin::propertyIterator()
00381         */
00382         virtual CdbStatus propertyIterator( CdbItr<CdbProperty>& theItr );
00383 
00384     private:
00385 
00386         CdbCPtr<CdbSQLOrigin> _originPtr;
00387     };
00388 
00389     CdbSQLApiOrigin::CdbSQLApiOrigin( const CdbDatabasePtr&        theDatabasePtr,
00390                                       const CdbCPtr<CdbSQLOrigin>& theOriginPtr ) :
00391         CdbOrigin( theDatabasePtr,
00392                    theOriginPtr->name( ).c_str( ),
00393                    theOriginPtr->id( )),
00394         _originPtr(theOriginPtr)
00395     { }
00396 
00397     CdbSQLApiOrigin::~CdbSQLApiOrigin( )
00398     { }
00399 
00400     BdbTime
00401     CdbSQLApiOrigin::created( )
00402     {
00403         return  _originPtr->created( );
00404     }
00405 
00406     std::string
00407     CdbSQLApiOrigin::description( )
00408     {
00409         return _originPtr->description( );
00410     }
00411 
00412     bool
00413     CdbSQLApiOrigin::isLocal( ) const
00414     {
00415       // Use parent object to get the local origin's identifier.
00416 
00417         return parent( )->localOrigin( )->id( ) == _originPtr->id( );
00418     }
00419 
00420     bool
00421     CdbSQLApiOrigin::isInstantiated( ) const
00422     {
00423         return _originPtr->isInstantiated( );
00424     }
00425 
00426     bool
00427     CdbSQLApiOrigin::isMaster( ) const
00428     {
00429         return _originPtr->type( ) == CdbSQLOrigin::TYPE_MASTER;
00430     }
00431 
00432     bool
00433     CdbSQLApiOrigin::isSlave( ) const
00434     {
00435         return _originPtr->type( ) == CdbSQLOrigin::TYPE_SLAVE;
00436     }
00437 
00438     bool
00439     CdbSQLApiOrigin::isTest( ) const
00440     {
00441         return _originPtr->type( ) == CdbSQLOrigin::TYPE_TEST;
00442     }
00443 
00444     CdbStatus
00445     CdbSQLApiOrigin::propertyIterator( CdbItr<CdbProperty>& theItr )
00446     {
00447         theItr = CdbItr<CdbProperty>( new CdbEmptyItr<CdbProperty>( ));
00448         return CdbStatus::Success;
00449     }
00450 }
00451 
00452 /////////////////////////////////
00453 // Class CdbSQLApiDatabase //
00454 /////////////////////////////////
00455 
00456 CdbDatabase*
00457 CdbSQLApiDatabase::clone( ) const
00458 {
00459   // Get rid of the "const" to access non-const methods
00460 
00461     CdbSQLApiDatabase* mySelf = const_cast<CdbSQLApiDatabase*>( this );
00462     return new CdbSQLApiDatabase( mySelf->parent( ),
00463                                   mySelf->name( ),
00464                                   _mySQLPtr );
00465 }
00466 
00467 CdbSQLApiDatabase::CdbSQLApiDatabase( const CdbPtr&          theCdbPtr,
00468                                       const char*            theName,
00469                                       const CdbCPtr<CdbSQL>& theSQLPtr ) :
00470     CdbDatabase( theCdbPtr,
00471                  theName ),
00472     _isValid                (true),
00473     _isOpen                 (false),
00474     _cachedLocalOriginAPIPtr(0),
00475     _mySQLPtr               (theSQLPtr),
00476     _myMasterSQLOriginPtr   (0),
00477     _myLocalSQLOriginPtr    (0)
00478 { }
00479 
00480 CdbSQLApiDatabase::~CdbSQLApiDatabase( )
00481 { }
00482 
00483 bool
00484 CdbSQLApiDatabase::isValid( )
00485 {
00486     return _isValid;
00487 }
00488 
00489 bool
00490 CdbSQLApiDatabase::isOpen( )
00491 {
00492     return _isOpen;
00493 }
00494 
00495 CdbStatus
00496 CdbSQLApiDatabase::open( )
00497 {
00498     const char* errorStr = "CdbSQLApiDatabase::open() - ERROR.";
00499 
00500     CdbStatus result = CdbStatus::Error;
00501     do {
00502 
00503         if( ! _isOpen ) {
00504 
00505           // Find and cache local SQL origin
00506 
00507             if( CdbStatus::Success != _mySQLPtr->find_origin( _myLocalSQLOriginPtr, "<local>" )) {
00508                 cout << errorStr << endl
00509                      << "    Failed to find the local origin. The database was not properly" << endl
00510                      << "    initialized/loaded." << endl;
00511                 break;
00512             }
00513 
00514           // Find and cache the master SQL origin
00515 
00516             if( _myLocalSQLOriginPtr->type( ) == CdbSQLOrigin::TYPE_MASTER ) {
00517                 _myMasterSQLOriginPtr = _myLocalSQLOriginPtr;
00518             } else {
00519                 if( CdbStatus::Success != _mySQLPtr->find_origin( _myMasterSQLOriginPtr, "<master>" )) {
00520                     cout << errorStr << endl
00521                          << "    Failed to find the master origin. The database was not properly" << endl
00522                          << "    initialized/loaded." << endl;
00523                     break;
00524                 }
00525             }
00526             _isOpen = true;
00527         }
00528 
00529       // Done
00530 
00531         result = CdbStatus::Success;
00532 
00533     } while( false );
00534 
00535     return result;
00536 }
00537 
00538 CdbStatus
00539 CdbSQLApiDatabase::close( )
00540 {
00541     if( _isOpen ) {
00542         _isOpen = false;
00543 
00544         _cachedLocalOriginAPIPtr = 0;
00545 
00546         _myMasterSQLOriginPtr = 0;
00547         _myLocalSQLOriginPtr  = 0;
00548     }
00549     return CdbStatus::Success;
00550 }
00551 
00552 BdbTime
00553 CdbSQLApiDatabase::id( )
00554 {
00555 
00556   // Make sure the database is open
00557 
00558     if( CdbStatus::Success != open( )) {
00559         assert( 0 );
00560         return BdbTime::plusInfinity;
00561     }
00562 
00563   // IMPLEMENTATION NOTE: It has to be a unique identifier of the distributed CDB
00564   //                      in here. This identifier is most likely a creation time
00565   //                      of the MASTER CDB.
00566   //
00567   //                      Unfortunatelly the current design of SQL tables does not
00568   //                      provide this information. This issues will be resolved in
00569   //                      the future.
00570 
00571     return BdbTime::minusInfinity;
00572 }
00573 
00574 BdbTime
00575 CdbSQLApiDatabase::created( )
00576 {
00577   // Make sure the database is open
00578 
00579     if( CdbStatus::Success != open( )) {
00580         assert( 0 );
00581         return BdbTime::plusInfinity;
00582     }
00583 
00584   // IMPLEMENTATION NOTE: It has to be a creation time of the local database installation
00585   //                      in here.
00586   //
00587   //                      Unfortunatelly the current design of SQL tables does not
00588   //                      provide this information. This issues will be resolved in
00589   //                      the future.
00590 
00591     return BdbTime::minusInfinity;
00592 }
00593 
00594 std::string
00595 CdbSQLApiDatabase::description( )
00596 {
00597   // Make sure the database is open
00598 
00599     if( CdbStatus::Success != open( )) {
00600         assert( 0 );
00601         return std::string( );
00602     }
00603 
00604   // IMPLEMENTATION NOTE: It has to be a description of the local database installation
00605   //                      in here.
00606   //
00607   //                      Unfortunatelly the current design of SQL tables does not
00608   //                      provide this information. This issues will be resolved in
00609   //                      the future.
00610 
00611     return "";
00612 }
00613 
00614 std::string
00615 CdbSQLApiDatabase::defaultView( ) const
00616 {
00617     return CdbEnvironment::defaultView( parent( )->technologyName( ),
00618                                         parent( )->implementationName( ),
00619                                         name( ));
00620 }
00621 
00622 CdbStatus
00623 CdbSQLApiDatabase::findView( CdbViewPtr& thePtr,
00624                              const char* theName )
00625 {
00626     const char* errorStr = "CdbSQLApiDatabase::findView(name) -- ERROR" ;
00627 
00628     CdbStatus result = CdbStatus::Error;
00629     do {
00630 
00631       // Make sure the database is open
00632 
00633         if( CdbStatus::Success != open( )) break;
00634 
00635       // Verify and correct parameters if 0 pointer is passed to indicate
00636       // that default database is needed.
00637 
00638         std::string viewName = defaultView( );
00639         if( 0 != theName ) viewName = theName;
00640 
00641         CdbCompositeName vName( viewName.c_str( ));
00642         if( !vName.isValid( )) {
00643             cout << errorStr << endl
00644                  << "    The view name passed to the procedure has invalid format: \"" << viewName.c_str( ) << "\"" << endl;
00645             break;
00646         }
00647 
00648       // Extract origin name and local view names
00649 
00650         std::string originName    = vName.first( );
00651         std::string localViewName = vName.last( );
00652 
00653       // Find a persistent origin where the specified view is supposed to belong to.
00654 
00655         CdbCPtr<CdbSQLOrigin> originPtr;
00656         if( CdbStatus::Success != findOriginObject( originPtr,
00657                                                     originName )) {
00658             cout << errorStr << endl
00659                  << "    An owner origin for the specified view was not found." << endl
00660                  << "        VIEW NAME : \"" << theName << "\"." << endl;
00661             break;
00662         }
00663 
00664       // Find the view by its name in the scope of the origin.
00665       //
00666       // NOTE: Watch for side track in case if view nicknames are used.
00667 
00668         CdbCPtr<CdbSQLView> viewPtr;
00669 
00670         if( "<recent>" == localViewName ) {
00671 
00672             CdbStatus status = originPtr->find_view( viewPtr );
00673             if( CdbStatus::Success != status ) {
00674                 result = status;
00675                 break;
00676             }
00677 
00678         } else {
00679 
00680             CdbStatus status = originPtr->find_view( viewPtr,
00681                                                      localViewName );
00682             if( CdbStatus::Success != status ) {
00683                 result = status;
00684                 break;
00685             }
00686         }
00687 
00688       // Construct the transient API object
00689 
00690         thePtr = new CdbSQLApiView( CdbDatabasePtr( this->clone( )),
00691                                     viewName.c_str( ),
00692                                     CdbId( originPtr->id( ),
00693                                            viewPtr->id( )),
00694                                     viewPtr,
00695                                     _myMasterSQLOriginPtr,
00696                                     _myLocalSQLOriginPtr );
00697 
00698         result = CdbStatus::Success;
00699 
00700     } while( false );
00701 
00702     return result;
00703 }
00704 
00705 CdbStatus
00706 CdbSQLApiDatabase::findView( CdbViewPtr&  thePtr,
00707                              const CdbId& theId )
00708 {
00709     const char* errorStr = "CdbSQLApiDatabase::findView(id) -- ERROR" ;
00710 
00711     CdbStatus result = CdbStatus::Error;
00712     do {
00713 
00714       // Make sure the database is open
00715 
00716         if( CdbStatus::Success != open( )) break;
00717 
00718       // Find a persistent origin where the specified view is supposed to belong to.
00719 
00720         CdbCPtr<CdbSQLOrigin> originPtr;
00721         if( CdbStatus::Success != findOriginObject( originPtr,
00722                                                     theId.origin )) {
00723             cout << errorStr << endl
00724                  << "    An owner origin for the specified view was not found." << endl
00725                  << "        VIEW ID : " << theId << "." << endl;
00726             break;
00727         }
00728 
00729       // Find the view by its identifier in the scope of the origin.
00730 
00731         CdbCPtr<CdbSQLView> viewPtr;
00732         {
00733             CdbStatus status = originPtr->find_view( viewPtr,
00734                                                      theId.local );
00735             if( CdbStatus::Success != status ) {
00736                 result = status;
00737                 break;
00738             }
00739         }
00740 
00741       // Build the full name of the view
00742 
00743         std::string viewName = originPtr->name( ) + "::" + viewPtr->name( );
00744 
00745       // Construct the transient CDB API object
00746 
00747         thePtr = new CdbSQLApiView( CdbDatabasePtr( this->clone( )),
00748                                     viewName.c_str( ),
00749                                     theId,
00750                                     viewPtr,
00751                                     _myMasterSQLOriginPtr,
00752                                     _myLocalSQLOriginPtr );
00753 
00754         result = CdbStatus::Success;
00755 
00756     } while( false );
00757 
00758     return result;
00759 }
00760 
00761 CdbStatus
00762 CdbSQLApiDatabase::viewIterator( CdbViewItr& theItr )
00763 {
00764     const char* errorStr = "CdbSQLApiDatabase::viewIterator() -- ERROR" ;
00765 
00766     CdbStatus status = CdbStatus::Error;
00767 
00768    // Make sure the database is open
00769 
00770     if( CdbStatus::Success != open( )) return CdbStatus::Error;
00771 
00772   // Get a list of all origins where the view will be extracted from
00773 
00774     std::vector< CdbCPtr<CdbSQLOrigin> > vectorOfOrigins;
00775 
00776     if( CdbStatus::Success != ( status = _mySQLPtr->origins( vectorOfOrigins ))) {
00777         ErrMsg(error) << errorStr << endl
00778                       << "    Failed to get a list of origins in a scope of the database." << endl
00779                       << "        DATABASE : \"" << name( ) << "\"" << endmsg;
00780         return status;
00781     }
00782 
00783   // Buuild a list of full names of all views in the found origins.
00784 
00785     std::vector< std::string > vectorOfAllViews;
00786 
00787     for( std::vector< CdbCPtr<CdbSQLOrigin> >::const_iterator originItr = vectorOfOrigins.begin( );
00788                                                               originItr != vectorOfOrigins.end( );
00789                                                             ++originItr ) {
00790         CdbCPtr<CdbSQLOrigin> originPtr = *originItr;
00791 
00792       // Skip the origin if it's not instantiated yet
00793 
00794         if( !originPtr->isInstantiated( )) continue;
00795 
00796         std::vector< CdbCPtr<CdbSQLView> > vectorOfViews;
00797 
00798         if( CdbStatus::Success != ( status = originPtr->views( vectorOfViews ))) {
00799             ErrMsg(error) << errorStr << endl
00800                           << "    Failed to get a list of views in a scope of the origin." << endl
00801                           << "        DATABASE : \"" << name( ) << "\"" << endl
00802                           << "        ORIGIN   : \"" << originPtr->name( ) << "\"" << endmsg;
00803             return status;
00804         }
00805         for( std::vector< CdbCPtr<CdbSQLView> >::const_iterator viewItr = vectorOfViews.begin( );
00806                                                                 viewItr != vectorOfViews.end( );
00807                                                               ++viewItr ) {
00808             CdbCPtr<CdbSQLView> viewPtr = *viewItr;
00809 
00810             vectorOfAllViews.push_back( CdbCompositeName( originPtr->name( ).c_str( ),
00811                                                           viewPtr->name( ).c_str( )).getName( ));
00812         }
00813     }
00814 
00815   // Turn the vector into the iterator
00816 
00817     theItr = CdbViewItr( new CdbSTD2CStrAdapterItr( new CdbSimpleVectorAdapterItr< std::string >( vectorOfAllViews )));
00818 
00819     return CdbStatus::Success;
00820 }
00821 
00822 
00823 CdbStatus
00824 CdbSQLApiDatabase::physicalConditionIterator( CdbItr<CdbId>& theItr )
00825 {
00826     const char* errorStr = "CdbSQLApiDatabase::physicalConditionIterator() -- ERROR" ;
00827 
00828     CdbStatus result = CdbStatus::Error;
00829 
00830   // Make sure the database is open
00831 
00832     if( CdbStatus::Success != ( result = open( ))) return result;
00833 
00834   // Find all physical conditions and put their 'global physical identifiers'
00835   // into thsi vector.
00836 
00837     std::vector< CdbId > vectorOfIds;
00838 
00839     std::vector< CdbCPtr<CdbSQLOrigin> > vectorOfOrigins;
00840 
00841     if( CdbStatus::Success != ( result = _mySQLPtr->origins( vectorOfOrigins ))) {
00842         ErrMsg(error) << errorStr << endl
00843                       << "    Failed to obtain a list of origins." << endmsg;
00844         return result;
00845     }
00846     for( std::vector< CdbCPtr<CdbSQLOrigin> >::const_iterator originItr = vectorOfOrigins.begin( );
00847                                                               originItr != vectorOfOrigins.end( );
00848                                                             ++originItr ) {
00849 
00850         CdbCPtr<CdbSQLOrigin> originPtr = *originItr;
00851 
00852       // Skip the origin if it's not instantiated yet
00853 
00854         if( !originPtr->isInstantiated( )) continue;
00855 
00856       // Get all conditions in that origin.
00857 
00858         std::vector< CdbCPtr<CdbSQLCondition> > vectorOfConditions;
00859 
00860         if( CdbStatus::Success != ( result = originPtr->conditions( vectorOfConditions ))) {
00861             ErrMsg(error) << errorStr << endl
00862                           << "    Failed to obtain a list of 'physical' conditions in the origin" << endl
00863                           << "        ORIGIN ID:" << originPtr->id( ) << "." << endmsg;
00864             return result;
00865         }
00866         for( std::vector< CdbCPtr<CdbSQLCondition> >::const_iterator conditionItr = vectorOfConditions.begin( );
00867                                                                      conditionItr != vectorOfConditions.end( );
00868                                                                    ++conditionItr ) {
00869             CdbCPtr<CdbSQLCondition> conditionPtr = *conditionItr;
00870 
00871           // Create a record
00872 
00873             vectorOfIds.push_back( CdbId( originPtr->id( ),
00874                                           conditionPtr->id( )));
00875         }
00876     }
00877 
00878   // Build an iterator out of the vector
00879 
00880     theItr = CdbItr<CdbId>( new CdbSimpleVectorAdapterItr<CdbId>( vectorOfIds ));
00881 
00882     return CdbStatus::Success;
00883 }
00884 
00885 CdbStatus
00886 CdbSQLApiDatabase::findCondition( CdbConditionPtr& thePtr,
00887                                   const CdbId&     theId )
00888 {
00889     const char* errorStr = "CdbSQLApiDatabase::findCondition('physical address') -- ERROR" ;
00890 
00891     CdbStatus result = CdbStatus::Error;
00892     do {
00893 
00894       // Make sure the database is open
00895 
00896         if( CdbStatus::Success != open( )) break;
00897 
00898       // Find a persistent origin where the specified physical condition
00899       // is supposed to belong to.
00900 
00901         CdbCPtr<CdbSQLOrigin> originPtr;
00902         if( CdbStatus::Success != findOriginObject( originPtr,
00903                                                     theId.origin )) {
00904             cout << errorStr << endl
00905                  << "    An owner origin for the specified condition was not found." << endl
00906                  << "        VIEW ID : " << theId << "." << endl;
00907             break;
00908         }
00909 
00910       // Find the condition by its identifier in the scope of the origin.
00911 
00912         CdbCPtr<CdbSQLCondition> conditionPtr;
00913         {
00914             CdbStatus status = originPtr->find_condition( conditionPtr,
00915                                                           theId.local );
00916             if( CdbStatus::Success != status ) {
00917                 result = status;
00918                 break;
00919             }
00920         }
00921 
00922       // Build the full name of the condition
00923 
00924         std::string conditionName = originPtr->name( ) + "::" + conditionPtr->name( );
00925 
00926       // DESIGN NOTE: The condition API component is going to be disjoined from
00927       //              any view or a folder. The direct database pointer will be added instead.
00928 
00929         CdbFolderPtr                     parentFolderPtr      = 0;
00930         CdbCPtr<CdbSQLConditionAtFolder> conditionAtFolderPtr = 0;
00931 
00932       // Construct the transient CDB API object
00933 
00934         thePtr = new CdbSQLApiCondition( parentFolderPtr,
00935                                          CdbDatabasePtr( this->clone( )),
00936                                          conditionName.c_str( ),
00937                                          conditionAtFolderPtr,
00938                                          conditionPtr,
00939                                          _myMasterSQLOriginPtr,
00940                                          _myLocalSQLOriginPtr );
00941 
00942         result = CdbStatus::Success;
00943 
00944     } while( false );
00945 
00946     return result;
00947 }
00948 
00949 CdbStatus
00950 CdbSQLApiDatabase::findCondition( CdbConditionPtr&        thePtr,
00951                                   const CdbCompositeName& thePhysicalName )
00952 {
00953     const char* errorStr = "CdbSQLApiDatabase::findCondition('physical name') -- ERROR" ;
00954 
00955     CdbStatus result = CdbStatus::Error;
00956     do {
00957 
00958       // Make sure the database is open
00959 
00960         if( CdbStatus::Success != open( )) break;
00961 
00962       // Verify parameters
00963 
00964         if( !thePhysicalName.isValid( )) {
00965             cout << errorStr << endl
00966                  << "    The 'physical name' name passed to the procedure has invalid format: \"" << thePhysicalName.getName( ).c_str( ) << "\"" << endl;
00967             break;
00968         }
00969 
00970       // Extract origin name and local condition names
00971 
00972         std::string originName         = thePhysicalName.first( );
00973         std::string localConditionName = thePhysicalName.last( );
00974 
00975       // Find a persistent origin where the specified 'physical' condition
00976       // is supposed to belong to.
00977 
00978         CdbCPtr<CdbSQLOrigin> originPtr;
00979         if( CdbStatus::Success != findOriginObject( originPtr,
00980                                                     originName )) {
00981             cout << errorStr << endl
00982                  << "    An owner origin for the specified 'physical' condition was not found." << endl
00983                  << "        CONDITION NAME : \"" << thePhysicalName.getName( ) << "\"." << endl;
00984             break;
00985         }
00986 
00987       // Find the condition by its name in the scope of the origin.
00988 
00989         CdbCPtr<CdbSQLCondition> conditionPtr;
00990         {
00991             CdbStatus status = originPtr->find_condition( conditionPtr,
00992                                                           localConditionName );
00993             if( CdbStatus::Success != status ) {
00994                 result = status;
00995                 break;
00996             }
00997         }
00998 
00999       // DESIGN NOTE: The condition API component is going to be disjoined from
01000       //              any view or a folder. The direct database pointer will be added instead.
01001 
01002         CdbFolderPtr                     parentFolderPtr      = 0;
01003         CdbCPtr<CdbSQLConditionAtFolder> conditionAtFolderPtr = 0;
01004 
01005       // Construct the transient CDB API object
01006 
01007         thePtr = new CdbSQLApiCondition( parentFolderPtr,
01008                                          CdbDatabasePtr( this->clone( )),
01009                                          thePhysicalName.getName( ).c_str( ),
01010                                          conditionAtFolderPtr,
01011                                          conditionPtr,
01012                                          _myMasterSQLOriginPtr,
01013                                          _myLocalSQLOriginPtr );
01014 
01015         result = CdbStatus::Success;
01016 
01017     } while( false );
01018 
01019     return result;
01020 }
01021 
01022 CdbStatus
01023 CdbSQLApiDatabase::historyEventIterator( CdbHistoryEventItr& theItr,
01024                                          const BdbTime&      theBeginTime,
01025                                          const BdbTime&      theEndTime,
01026                                          const char**        theEventsToSelect )
01027 {
01028     return CdbStatus::NotImplemented;
01029 }
01030 
01031 CdbStatus
01032 CdbSQLApiDatabase::historyEventTypeIterator( CdbHistoryEventTypeItr& theItr )
01033 {
01034     return CdbStatus::NotImplemented;
01035 }
01036 
01037 CdbStatus
01038 CdbSQLApiDatabase::partitionIterator( CdbItr<unsigned short>& theItr )
01039 {
01040     const char* errorStr = "CdbSQLApiDatabase::partitionIterator() -- ERROR" ;
01041 
01042     CdbStatus result = CdbStatus::Error;
01043     do {
01044 
01045       // Make sure the database is open
01046 
01047         if( CdbStatus::Success != open( )) break;
01048 
01049       // Get a list of all known partition from the MASTER origin. This list will be used
01050       // by an iterator to be constructed at the next stage.
01051 
01052         std::vector< CdbCPtr<CdbSQLPartition> > partitions;
01053         if( CdbStatus::Success != _myMasterSQLOriginPtr->partitions( partitions )) {
01054             cout << errorStr << endl
01055                  << "    Failed to obtain a list of known partitions from MASTER." << endl;
01056             return CdbStatus::Error;
01057         }
01058 
01059       // ATTENTION: This is not a memory leak - the created object
01060       //            will be destroyed by the iterator.
01061 
01062         theItr = CdbItr<unsigned short>( new CdbSQLApiPartitionItr( partitions ));
01063 
01064       // Done
01065 
01066         result = CdbStatus::Success;
01067 
01068     } while( false );
01069 
01070     return result;
01071 }
01072 
01073 CdbStatus
01074 CdbSQLApiDatabase::partitionIterator( CdbItr<unsigned short>& theItr,
01075                                       unsigned int            theOriginId )
01076 {
01077     const char* errorStr = "CdbSQLApiDatabase::partitionIterator(by origin id) - ERROR.";
01078 
01079     CdbStatus result = CdbStatus::Error;
01080     do {
01081 
01082       // Make sure the database is open
01083 
01084         if( CdbStatus::Success != open( )) break;
01085 
01086       // Find the corresponding persistent origin by its identifier
01087 
01088         CdbCPtr<CdbSQLOrigin> originPtr;
01089         if( CdbStatus::Success != findOriginObject( originPtr,
01090                                                     theOriginId )) {
01091             cout << errorStr << endl
01092                  << "    Failed to find the specified origin." << endl
01093                  << "        ORIGIN ID : " << theOriginId << "." << endl;
01094             break;
01095         }
01096 
01097       // Get a list of all known partition from the found origin. This list will be used
01098       // by an iterator to be constructed at the next stage.
01099 
01100         std::vector< CdbCPtr<CdbSQLPartition> > partitions;
01101         if( CdbStatus::Success != originPtr->partitions( partitions )) {
01102             cout << errorStr << endl
01103                  << "    Failed to obtain a list of known partitions from the specified origin." << endl
01104                  << "        ORIGIN ID : " << theOriginId << "." << endl;
01105             return CdbStatus::Error;
01106         }
01107 
01108       // ATTENTION: This is not a memory leak - the created object
01109       //            will be destroyed by the iterator.
01110 
01111         theItr = CdbItr<unsigned short>( new CdbSQLApiPartitionItr( partitions ));
01112 
01113       // Done
01114 
01115         result = CdbStatus::Success;
01116 
01117     } while( false );
01118 
01119     return result;
01120 }
01121 
01122 CdbStatus
01123 CdbSQLApiDatabase::partitionIterator( CdbItr<unsigned short>& theItr,
01124                                       const char*             theOriginName )
01125 {
01126     const char* errorStr = "CdbSQLApiDatabase::partitionIterator(by origin name) - ERROR.";
01127 
01128     CdbStatus result = CdbStatus::Error;
01129     do {
01130 
01131       // Make sure the database is open
01132 
01133         if( CdbStatus::Success != open( )) break;
01134 
01135       // Find the corresponding persistent origin by its name
01136 
01137         CdbCPtr<CdbSQLOrigin> originPtr;
01138         if( CdbStatus::Success != findOriginObject( originPtr,
01139                                                     theOriginName )) {
01140             cout << errorStr << endl
01141                  << "    Failed to find the specified origin." << endl
01142                  << "        ORIGIN NAME : \"" << theOriginName << "\"." << endl;
01143             break;
01144         }
01145 
01146       // Get a list of all known partition from the found origin. This list will be used
01147       // by an iterator to be constructed at the next stage.
01148 
01149         std::vector< CdbCPtr<CdbSQLPartition> > partitions;
01150         if( CdbStatus::Success != originPtr->partitions( partitions )) {
01151             cout << errorStr << endl
01152                  << "    Failed to obtain a list of known partitions from the specified origin." << endl
01153                  << "        ORIGIN NAME : \"" << theOriginName << "\"." << endl;
01154             return CdbStatus::Error;
01155         }
01156 
01157       // ATTENTION: This is not a memory leak - the created object
01158       //            will be destroyed by the iterator.
01159 
01160         theItr = CdbItr<unsigned short>( new CdbSQLApiPartitionItr( partitions ));
01161 
01162       // Done
01163 
01164         result = CdbStatus::Success;
01165 
01166     } while( false );
01167 
01168     return result;
01169 }
01170 
01171 CdbStatus
01172 CdbSQLApiDatabase::findPartition( CdbPartitionPtr& thePtr,
01173                                   unsigned short   thePartitionId )
01174 {
01175     const char* errorStr = "CdbSQLApiDatabase::findPartition() -- ERROR" ;
01176 
01177     CdbStatus status = CdbStatus::Error;
01178 
01179   // Make sure the database is open
01180 
01181     if( CdbStatus::Success != open( )) return CdbStatus::Error;
01182 
01183   // Find specified partition
01184   //
01185   // NOTE: This algorithm will use partition object from the local
01186   //       origin if it will discover that the partition belongs to the current
01187   //       database. This is a typical scenario for the SLAVE type databases.
01188 
01189     CdbCPtr<CdbSQLPartition> partitionPtr;
01190     {
01191       // First step is to find the partition in MASTER where all partitions
01192       // are described.
01193 
01194         if( CdbStatus::Success != ( status = _myMasterSQLOriginPtr->find_partition( partitionPtr,
01195                                                                                     thePartitionId ))) {
01196             ErrMsg(error) << errorStr << endl
01197                           << "    Failed to find the specified partition in the MASTER Registry." << endl
01198                           << "        PARTITION ID: " << thePartitionId << endmsg;
01199             return status;
01200         }
01201 
01202       // Refind the search if it's a local partition and the local origin is
01203       // not the MASTER one.
01204 
01205         if( partitionPtr->originId( ) != _myMasterSQLOriginPtr->id( )) {
01206             if( partitionPtr->originId( ) == _myLocalSQLOriginPtr->id( )) {
01207 
01208                 if( CdbStatus::Success != ( status = _myLocalSQLOriginPtr->find_partition( partitionPtr,
01209                                                                                            thePartitionId ))) {
01210                     ErrMsg(error) << errorStr << endl
01211                                   << "    Failed to find partition with ID=" << thePartitionId << " at the current" << endl
01212                                   << "    database to whom the partition is supposed to belong to. The database" << endl
01213                                   << "    may not be properly initialized/loaded." << endl
01214                                   << "        ORIGIN NAME  : \"" << _myLocalSQLOriginPtr->name( ) << "\"" << endl
01215                                   << "        PARTITION ID : " << thePartitionId << endmsg;
01216                     return status;
01217                 }
01218             }
01219         }
01220     }
01221 
01222   // Get the owner's origin name of this partition.
01223 
01224     std::string ownerOriginName;
01225     {
01226         CdbCPtr<CdbSQLOrigin> originPtr;
01227         if( CdbStatus::Success != ( status = findOriginObject( originPtr,
01228                                                                partitionPtr->originId( )))) {
01229             ErrMsg(error) << errorStr << endl
01230                           << "    There is no information on the origin ID=" << partitionPtr->originId( ) << endl
01231                           << "    in the corresponding collection of the MASTER's registry." << endl
01232                           << "    The database may be corrupted or in an inconsistent state." << endmsg;
01233             return status;
01234         }
01235         ownerOriginName = originPtr->name( );
01236     }
01237 
01238   // Construct the transient CDB API object
01239 
01240     thePtr = new CdbSQLApiPartition( CdbDatabasePtr( this->clone( )),
01241                                      partitionPtr,
01242                                      ownerOriginName );
01243     return CdbStatus::Success;
01244 }
01245 
01246 CdbStatus
01247 CdbSQLApiDatabase::findPartition( CdbPartitionPtr& thePtr,
01248                                   unsigned short   thePartitionId,
01249                                   unsigned int     theOriginId )
01250 {
01251     const char* errorStr = "CdbSQLApiDatabase::findPartition(by origin id) -- ERROR" ;
01252 
01253     CdbStatus status = CdbStatus::Error;
01254 
01255   // Make sure the database is open
01256 
01257     if( CdbStatus::Success != open( )) return CdbStatus::Error;
01258 
01259   // Find the corresponding persistent origin
01260 
01261     CdbCPtr<CdbSQLOrigin> originPtr;
01262     if( CdbStatus::Success != ( status = findOriginObject( originPtr,
01263                                                            theOriginId ))) {
01264         ErrMsg(error) << errorStr << endl
01265                       << "    Failed to find the origin." << endl
01266                       << "        ORIGIN    : " << theOriginId << endmsg;
01267         return status;
01268     }
01269 
01270   // Find the specified partition in the scope of the found origin
01271 
01272     CdbCPtr<CdbSQLPartition> partitionPtr;
01273     if( CdbStatus::Success != ( status = originPtr->find_partition( partitionPtr,
01274                                                                     thePartitionId ))) {
01275         ErrMsg(error) << errorStr << endl
01276                       << "    Failed to find the partition in a scope of the specified origin." << endl
01277                       << "        ORIGIN    : " << theOriginId << endl
01278                       << "        PARTITION : " << thePartitionId << endmsg;
01279         return status;
01280     }
01281 
01282   // Get the owner's origin name of this partition.
01283 
01284     std::string ownerOriginName;
01285     {
01286         CdbCPtr<CdbSQLOrigin> originPtr;
01287         if( CdbStatus::Success != ( status = findOriginObject( originPtr,
01288                                                                partitionPtr->originId( )))) {
01289             ErrMsg(error) << errorStr << endl
01290                           << "    There is no information on the origin ID=" << partitionPtr->originId( ) << endl
01291                           << "    in the corresponding collection of the MASTER's registry." << endl
01292                           << "    The database may be corrupted or in an inconsistent state." << endmsg;
01293             return status;
01294         }
01295         ownerOriginName = originPtr->name( );
01296     }
01297 
01298   // Construct the transient CDB API object
01299 
01300     thePtr = new CdbSQLApiPartition( CdbDatabasePtr( this->clone( )),
01301                                      partitionPtr,
01302                                      ownerOriginName );
01303     return CdbStatus::Success;
01304 }
01305 
01306 CdbStatus
01307 CdbSQLApiDatabase::findPartition( CdbPartitionPtr& thePtr,
01308                                   unsigned short   thePartitionId,
01309                                   const char*      theOriginName )
01310 {
01311     const char* errorStr = "CdbSQLApiDatabase::findPartition(by origin name) -- ERROR" ;
01312 
01313     CdbStatus result = CdbStatus::Error;
01314     do {
01315 
01316       // Make sure the database is open
01317 
01318         if( CdbStatus::Success != open( )) break;
01319 
01320       // Find the corresponding persistent origin
01321 
01322         CdbCPtr<CdbSQLOrigin> originPtr;
01323         if( CdbStatus::Success != findOriginObject( originPtr,
01324                                                     theOriginName )) {
01325             cout << errorStr << endl
01326                  << "    Failed to find the origin with NAME=\"" << theOriginName << "\"." << endl;
01327             break;
01328         }
01329 
01330       // Delegate the rest of the procedure to a similar method using
01331       // the identifier.
01332 
01333         result = findPartition( thePtr,
01334                                 thePartitionId,
01335                                 originPtr->id( ));
01336     } while( false );
01337 
01338     return result;
01339 }
01340 
01341 CdbOriginPtr
01342 CdbSQLApiDatabase::localOrigin( )
01343 {
01344 
01345   // Make sure the database is open
01346 
01347     if( CdbStatus::Success != open( )) {
01348         assert( 0 );
01349         return 0;
01350     }
01351 
01352   // Use cached object pointer if possible
01353 
01354     if( _cachedLocalOriginAPIPtr.isNull( )) {
01355         if( CdbStatus::Success != findOrigin( _cachedLocalOriginAPIPtr,
01356                                               _myLocalSQLOriginPtr->id( ))) {
01357             assert( 0 );
01358             return 0;
01359         }
01360     }
01361     return _cachedLocalOriginAPIPtr;
01362 }
01363 
01364 CdbStatus
01365 CdbSQLApiDatabase::originIterator( CdbItr<unsigned short>& theItr )
01366 {
01367     const char* errorStr = "CdbSQLApiDatabase::originIterator() -- ERROR" ;
01368 
01369     CdbStatus result = CdbStatus::Error;
01370     do {
01371 
01372       // Make sure the database is open
01373 
01374         if( CdbStatus::Success != open( )) break;
01375 
01376       // Get a list of all known origins
01377 
01378         std::vector<CdbCPtr<CdbSQLOrigin> > origins;
01379         if( CdbStatus::Success != _mySQLPtr->origins( origins )) {
01380             cout << errorStr << endl
01381                  << "    Failed to obtain a list of all known origins from the database." << endl;
01382             break;
01383         }
01384 
01385       // ATTENTION: This is not a memory leak - the created object
01386       //            will be destroyed by the iterator.
01387 
01388         theItr = CdbItr<unsigned short>( new CdbSQLApiOriginItr( origins ));
01389 
01390       // Done
01391 
01392         result = CdbStatus::Success;
01393 
01394     } while( false );
01395 
01396     return result;
01397 }
01398 
01399 CdbStatus
01400 CdbSQLApiDatabase::findOrigin( CdbOriginPtr& thePtr,
01401                                const char*   theName )
01402 {
01403     const char* errorStr = "CdbSQLApiDatabase::findOrigin(name) -- ERROR" ;
01404 
01405     assert( 0 != theName );
01406 
01407     CdbStatus status = CdbStatus::Error;
01408 
01409   // Make sure the database is open
01410 
01411     if( CdbStatus::Success != open( )) return CdbStatus::Error;
01412 
01413   // Find the corresponding persistent origin
01414 
01415     CdbCPtr<CdbSQLOrigin> originPtr;
01416     if( CdbStatus::Success != ( status = findOriginObject( originPtr,
01417                                                            theName ))) {
01418         ErrMsg(error) << errorStr << endl
01419                       << "    Failed to find the specified origin." << endl
01420                       << "        ORIGIN NAME : \"" << theName << "\"." << endmsg;
01421         return status;
01422     }
01423 
01424   // Construct the transient CDB API object
01425 
01426     thePtr = new CdbSQLApiOrigin( CdbDatabasePtr( this->clone( )),
01427                                   originPtr );
01428     return CdbStatus::Success;
01429 }
01430 
01431 CdbStatus
01432 CdbSQLApiDatabase::findOrigin( CdbOriginPtr&  thePtr,
01433                                unsigned short theId )
01434 {
01435     const char* errorStr = "CdbSQLApiDatabase::findOrigin(id) -- ERROR" ;
01436 
01437     CdbStatus status = CdbStatus::Error;
01438 
01439   // Make sure the database is open
01440 
01441     if( CdbStatus::Success != open( )) return CdbStatus::Error;
01442 
01443   // Find the corresponding persistent origin
01444 
01445     CdbCPtr<CdbSQLOrigin> originPtr;
01446     if( CdbStatus::Success != ( status = findOriginObject( originPtr,
01447                                                            theId ))) {
01448         ErrMsg(error) << errorStr << endl
01449                       << "    Failed to find the specified origin." << endl
01450                       << "        ORIGIN ID : " << theId << endmsg;
01451         return status;
01452     }
01453 
01454   // Construct the transient CDB API object
01455 
01456     thePtr = new CdbSQLApiOrigin( CdbDatabasePtr( this->clone( )),
01457                                   originPtr );
01458     return CdbStatus::Success;
01459 }
01460 
01461 CdbStatus
01462 CdbSQLApiDatabase::findOriginObject( CdbCPtr<CdbSQLOrigin>& thePtr,
01463                                      const std::string&     theName )
01464 {
01465     assert( _isValid );
01466     assert( _isOpen );
01467     assert( !theName.empty( ));
01468 
01469   // A little optimization to use already found and cached MASTER and local
01470   // origin objects in case if one of them gets requested.
01471  
01472     CdbCPtr<CdbSQLOrigin> originPtr;
01473 
01474     if(      theName == _myMasterSQLOriginPtr->name( )) originPtr = _myMasterSQLOriginPtr;
01475     else if( theName == _myLocalSQLOriginPtr->name( ))  originPtr = _myLocalSQLOriginPtr;
01476     else if( theName == "<master>" )                    originPtr = _myMasterSQLOriginPtr;
01477     else if( theName == "<local>"  )                    originPtr = _myLocalSQLOriginPtr;
01478     else {
01479 
01480         CdbStatus status = _mySQLPtr->find_origin( originPtr, theName );
01481         if( CdbStatus::Success != status ) return status;
01482     }
01483 
01484   // Done
01485 
01486     thePtr = originPtr;
01487 
01488     return CdbStatus::Success;
01489 }
01490 
01491 CdbStatus
01492 CdbSQLApiDatabase::findOriginObject( CdbCPtr<CdbSQLOrigin>& thePtr,
01493                                      unsigned short         theId )
01494 {
01495     assert( _isValid );
01496     assert( _isOpen );
01497 
01498   // A little optimization to use already found and cached MASTER and local
01499   // origin objects in case if one of them gets requested.
01500  
01501     CdbCPtr<CdbSQLOrigin> originPtr;
01502 
01503     if(      theId == _myMasterSQLOriginPtr->id( )) originPtr = _myMasterSQLOriginPtr;
01504     else if( theId == _myLocalSQLOriginPtr->id( ))  originPtr = _myLocalSQLOriginPtr;
01505     else {
01506 
01507         CdbStatus status = _mySQLPtr->find_origin( originPtr, theId );
01508         if( CdbStatus::Success != status ) return status;
01509     }
01510 
01511   // Done
01512 
01513     thePtr = originPtr;
01514 
01515     return CdbStatus::Success;
01516 }
01517 
01518 /////////////////
01519 // End Of File //
01520 /////////////////

Generated on Mon Dec 5 18:22:11 2005 for CDB by doxygen1.3-rc3