00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "GaudiKernel/System.h"
00014 #include "GaudiKernel/MsgStream.h"
00015 #include "GaudiDb/IOODataBase.h"
00016 #include "GaudiDb/DbHandle.h"
00017 #include "GaudiDb/DbTypeInfo.h"
00018 #include "GaudiDb/DbContainer.h"
00019 #include "DbContainerObj.h"
00020
00021
00022 DbContainer::DbContainer(dbHandle<DbDataBase>& dbH, const std::string& name, const DbTypeInfo& info, DbAccessMode mode)
00023 : m_info(0), m_typeInfo(info)
00024 {
00025 m_isOpen = false;
00026 m_type = m_typeInfo.type();
00027 m_dbOOMs = dbH.db();
00028 m_msgSvc = dbH.msgSvc();
00029 if ( 0 != m_dbOOMs && dbH.isValid() && m_typeInfo.name().length() > 0 ) {
00030 if ( dbH.add( name, this) ) {
00031 m_database = dbH;
00032 m_name = name;
00033 m_mode = mode;
00034 if ( mode & DbOOMs::UPDATE ) {
00035 m_mode |= DbOOMs::CREATE;
00036 }
00037 m_info = m_dbOOMs->createContainer();
00038 m_database.addRef();
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 MsgStream log(m_msgSvc, dbH.name());
00049 log << MSG::DEBUG
00050 << "--> Access DbContainer " << DbOOMs::accessMode(m_mode) << " " << m_name
00051 << endreq;
00052 return;
00053 }
00054 }
00055 MsgStream err(m_msgSvc,System::moduleName());
00056 err << MSG::ERROR
00057 << "--> Access DbContainer "
00058 << " Mode:" << DbOOMs::accessMode(mode)
00059 << " " << name
00060 << " impossible. Cannot link to implementation."
00061 << endreq;
00062 }
00063
00064
00065 DbContainer::~DbContainer() {
00066 std::string identifier = m_database.isValid() ? m_database.name() : System::moduleName();
00067 clearEntries();
00068 if ( 0 != m_info ) {
00069 m_info->release();
00070 m_info = 0;
00071 }
00072 DbContainer* p = m_database.find(name());
00073 if ( p ) {
00074 m_database.remove ( p );
00075 }
00076 m_database.release();
00077 MsgStream log(m_msgSvc, identifier);
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 log << MSG::DEBUG
00088 << "--> Deaccess DbContainer " << DbOOMs::accessMode(m_mode) << " " << m_name
00089 << endreq;
00090 }
00091
00092
00093 long DbContainer::size() const {
00094 if ( 0 == m_info ) {
00095 std::string identifier = m_database.isValid() ? m_database.name() : System::moduleName();
00096 MsgStream log(m_msgSvc, identifier);
00097 log << MSG::ERROR
00098 << "--> Access DbContainer::size()"
00099 << " " << m_name
00100 << " impossible - invalid object!"
00101 << endreq;
00102 return -1;
00103 }
00104 return m_info->size();
00105 }
00106
00107
00108 DbResult DbContainer::open() const {
00109 if ( !m_isOpen ) {
00110 if ( 0 != m_info ) {
00111 if ( m_info->open(m_database, m_name, m_typeInfo, m_mode) ) {
00112 m_isOpen = true;
00113 return DbOOMs::DbSuccess;
00114 }
00115 }
00116 return DbOOMs::DbError;
00117 }
00118 return DbOOMs::DbSuccess;
00119 }
00120
00121
00122 DbResult DbContainer::close() const {
00123 if ( m_isOpen ) {
00124 if ( 0 != m_info ) {
00125
00126
00127
00128 if ( m_info->close() ) {
00129 m_isOpen = false;
00130 DbContainer* p = m_database.find(m_name);
00131 if ( p ) {
00132 m_database.remove ( p );
00133 }
00134
00135 return DbOOMs::DbSuccess;
00136 }
00137 }
00138 return DbOOMs::DbError;
00139 }
00140 else if ( m_database.isValid() ) {
00141 DbContainer* p = m_database.find(m_name);
00142 if ( p ) {
00143 m_database.remove ( p );
00144 }
00145 }
00146 return DbOOMs::DbSuccess;
00147 }
00148
00149
00150 DbResult DbContainer::addEntry(const dbHandle<DbObject>& objH) {
00151 objH.setType(type());
00152 if ( m_isOpen && m_info && m_mode != DbOOMs::READ ) {
00153 if ( m_info->addEntry(objH) ) {
00154 return m_database.addType(m_name, m_typeInfo);
00155 }
00156 }
00157 return DbOOMs::DbError;
00158 }
00159
00160
00161 DbResult DbContainer::updateEntry(const dbHandle<DbObject>& objH) {
00162 objH.setType(type());
00163 if ( m_isOpen && m_info && m_mode != DbOOMs::READ ) {
00164 return m_info->updateEntry(objH);
00165 }
00166 return DbOOMs::DbError;
00167 }
00168
00169
00170 DbResult DbContainer::find(const dbHandle<DbContainer>& cntH, dbHandle<DbObject>& objH, const dbHandle<DbLink>& linkH) const {
00171 if ( m_isOpen ) {
00172 objH.setType(m_database.type());
00173 if ( m_info ) {
00174 return m_info->find(cntH, objH, linkH);
00175 }
00176 }
00177 return DbOOMs::DbError;
00178 }
00179
00181 DbResult DbContainer::remove(dbHandle<DbObject>& element) {
00182 if ( element.isValid() && m_isOpen ) {
00183 DbObject* it = _Base::find(element.ptr());
00184 if ( it ) {
00185 element = dbHandle<DbObject>(m_database.type());
00186 return _Base::remove(it) ? DbOOMs::DbError : DbOOMs::DbSuccess;
00187 }
00188 }
00189 return DbOOMs::DbError;
00190 }
00191
00192
00193 DbResult DbContainer::select(ISelectStatement* sel) const {
00194 if ( m_info ) {
00195 return m_info->select(sel);
00196 }
00197 return DbOOMs::DbError;
00198 }
00199
00200
00201 DbResult DbContainer::unselect() const {
00202 if ( m_info ) {
00203 return m_info->unselect();
00204 }
00205 return DbOOMs::DbError;
00206 }
00207
00209 IDbCursor* DbContainer::makeCursor() const {
00210 if ( m_info ) {
00211 return m_info->makeCursor();
00212 }
00213 return 0;
00214 }