00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00033
00034
00044
00045
00046
00047 #include <stdio.h>
00048 #include <stdlib.h>
00049 #include <sys/types.h>
00050 #include "libpq-fe.h"
00051 #include <string>
00052 #include <iostream>
00053
00054 #include "database/MJDatabaseConnection.hh"
00055
00056 using namespace std;
00057
00058
00059
00060 #include "database/MJDatabasePsqlImpl.hh"
00061 #include "database/MJDatabaseMEGACrystal.hh"
00062 #include "database/MJDatabaseSEGACrystal.hh"
00063 #include "database/MJDatabaseCloverCrystal.hh"
00064 #include "database/MJDatabaseCloverDetector.hh"
00065 #include "database/MJDatabaseElement.hh"
00066 #include "database/MJDatabaseIsotope.hh"
00067 #include "database/MJDatabaseMaterial.hh"
00068 #include "database/MJDatabaseSimple800g.hh"
00069 #include "database/MJDatabaseClover4x50x80DetCalib.hh"
00070
00071 #include "io/MJLogger.hh"
00072
00073
00074 MJDatabasePsqlImpl::MJDatabasePsqlImpl()
00075 {
00076 fdbconn = new MJDatabaseConnection();
00077 }
00078 MJDatabasePsqlImpl::~MJDatabasePsqlImpl()
00079 {
00080 if(fdbconn)delete fdbconn;
00081 fdbconn=0;
00082 MJLog(trace) << "deleting connection" << endlog;
00083 }
00084 MJDatabaseMEGACrystal*
00085 MJDatabasePsqlImpl::MJDatabaseMegaPsqlImpl(string name)
00086 {
00087 MJDatabaseMEGACrystal* crystal = 0;
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 return crystal;
00101 }
00102 MJDatabaseMEGACrystal*
00103 MJDatabasePsqlImpl::MJDatabaseMegaPsqlImpl(int id)
00104 {
00105 return new MJDatabaseMEGACrystal();
00106 }
00107 MJDatabaseSEGACrystal*
00108 MJDatabasePsqlImpl::MJDatabaseSegaPsqlImpl(string name)
00109 {
00110 return new MJDatabaseSEGACrystal();
00111 }
00112 MJDatabaseSEGACrystal*
00113 MJDatabasePsqlImpl::MJDatabaseSegaPsqlImpl(int id)
00114 {
00115 return new MJDatabaseSEGACrystal();
00116 }
00117 MJDatabaseCloverCrystal*
00118 MJDatabasePsqlImpl::MJDatabaseCloverPsqlImpl(string name)
00119 {
00120 MJDatabaseCloverCrystal* crystal = new MJDatabaseCloverCrystal();
00121
00122 string cmd = "select * from clovercrystal where partid='" + name + "'";
00123 PGresult *res = fdbconn->qResult(cmd);
00124
00125
00126 if( PQntuples(res) == 0 ) {
00127 MJLog(error) << "Could not find " << name << " in the database " << endlog;
00128 return NULL;
00129 }
00130
00131 crystal->createCrystal(res);
00132
00133 fdbconn->qDone(res);
00134
00135
00136 return crystal;
00137 }
00138 MJDatabaseCloverCrystal*
00139 MJDatabasePsqlImpl::MJDatabaseCloverPsqlImpl(int id)
00140 {
00141 return new MJDatabaseCloverCrystal();
00142
00143 }
00144 MJDatabaseCloverDetector*
00145 MJDatabasePsqlImpl::MJDatabaseCloverDetectorPsqlImpl(string name)
00146 {
00147 MJDatabaseCloverDetector* detector = new MJDatabaseCloverDetector();
00148 string cmd = "select * from cloverdetector where serialno='" + name + "'";
00149 PGresult *res = fdbconn->qResult(cmd);
00150
00151
00152 if( PQntuples(res) == 0 ) {
00153 MJLog(error) << "Could not find " << name << " in the database " << endlog;
00154 return NULL;
00155 }
00156
00157 detector->createDetector(res, fdbconn->GetConnection());
00158
00159 fdbconn->qDone(res);
00160 return detector;
00161 }
00162 MJDatabaseCloverDetector*
00163 MJDatabasePsqlImpl::MJDatabaseCloverDetectorPsqlImpl(int id)
00164 {
00165 return new MJDatabaseCloverDetector();
00166 }
00167 MJDatabaseIsotope*
00168 MJDatabasePsqlImpl::MJDatabaseIsotopePsqlImpl(string name)
00169 {
00170
00171 MJDatabaseIsotope* iso = new MJDatabaseIsotope();
00172 string cmd = "select * from mjisotopes where isoname = '" + name + "'";
00173 PGresult *res = fdbconn->qResult(cmd);
00174
00175
00176 if( PQntuples(res) == 0 ) {
00177 MJLog(error) << "Could not find " << name << " in the database " << endlog;
00178 return NULL;
00179 }
00180
00181 iso->createIsotope(res);
00182 fdbconn->qDone(res);
00183
00184 return iso;
00185 }
00186 MJDatabaseMaterial*
00187 MJDatabasePsqlImpl::MJDatabaseMaterialPsqlImpl(string name)
00188 {
00189
00190 MJDatabaseMaterial *mat = new MJDatabaseMaterial();
00191 string cmd = "select mt.*, ct.matname, ct.noofatoms, ct.fracmass ";
00192 cmd += " from mjmaterials mt, mjcomponents ct ";
00193 cmd += " where mt.matname='" + name + "'";
00194 cmd += " AND mt.matname=ct.name";
00195
00196 PGresult *res = fdbconn->qResult(cmd);
00197
00198
00199 if( PQntuples(res) == 0 ) {
00200 MJLog(error) << "Could not find " << name << " in the database " << endlog;
00201 return NULL;
00202 }
00203
00204 mat->createMaterial(res, 0);
00205 fdbconn->qDone(res);
00206
00207
00208 return mat;
00209
00210 }
00211 MJDatabaseElement*
00212 MJDatabasePsqlImpl::MJDatabaseElementPsqlImpl(string name)
00213 {
00214
00215 MJDatabaseElement *ele= new MJDatabaseElement();
00216 string cmd = "select et.name, et.symbol, et.z, et.niostope, " ;
00217 cmd += " it.isoname, it.symbol, it.abundance" ;
00218 cmd += " from mjelements et, mjisotopesabundance it " ;
00219 cmd += " where et.name='" + name + "'" ;
00220 cmd += " AND et.name=it.elename";
00221 PGresult *res = fdbconn->qResult(cmd);
00222
00223
00224 if( PQntuples(res) == 0 ) {
00225 MJLog(error) << "Could not find " << name << " in the database " << endlog;
00226 return NULL;
00227 }
00228
00229 ele->createElement(res);
00230 fdbconn->qDone(res);
00231
00232 return ele;
00233 }
00234
00235 MJDatabaseSimple800g*
00236 MJDatabasePsqlImpl::MJDatabaseSimple800gPsqlImpl(string name)
00237 {
00238 MJDatabaseSimple800g* crystal = new MJDatabaseSimple800g();
00239
00240 string cmd = "select * from simple800gcrystal where partid='" + name + "'" ;
00241 PGresult *res = fdbconn->qResult(cmd);
00242
00243
00244 if( PQntuples(res) == 0 ) {
00245 MJLog(error) << "Could not find " << name << " in the database " << endlog;
00246 return NULL;
00247 }
00248
00249 crystal->createCrystal(res);
00250
00251 fdbconn->qDone(res);
00252
00253
00254 return crystal;
00255 }
00256 MJDatabaseClover4x50x80DetCalib*
00257 MJDatabasePsqlImpl::MJDatabaseClover4x50x80CalibPsqlImpl(string name,
00258 string beginT,
00259 string endT)
00260 {
00261 MJDatabaseClover4x50x80DetCalib* detCalib = new
00262 MJDatabaseClover4x50x80DetCalib();
00263 string cmd = "select * from CloverDetectorCalib where detector='" + name + "'";
00264 cmd += " and beginTime>='" + beginT;
00265 cmd += "' and endTime<='" + endT + "'";
00266 PGresult *res = fdbconn->qResult(cmd);
00267
00268
00269 if( PQntuples(res) == 0 ) {
00270 MJLog(error) << "Could not find calibration constants for " << name
00271 << " for time interval " << beginT
00272 << " and " << endT <<" in the database " << endlog;
00273 return NULL;
00274 }
00275
00276 detCalib->createCalib(res);
00277
00278 fdbconn->qDone(res);
00279
00280 return detCalib ;
00281 }
00282 vector<MJDatabaseMaterial>
00283 MJDatabasePsqlImpl::MJDatabaseAllMaterialsPsqlImpl()
00284 {
00285
00286 vector<MJDatabaseMaterial> matVec;
00287 string cmd = "select mt.*, ct.matname, ct.noofatoms, ct.fracmass ";
00288 cmd += " from mjmaterials mt, mjcomponents ct ";
00289 cmd += " where mt.matname=ct.name";
00290
00291 PGresult *res = fdbconn->qResult(cmd);
00292
00293
00294 if( PQntuples(res) == 0 ) {
00295 MJLog(error) << "Could not find materials in the database " << endlog;
00296
00297 }
00298 int n=0;
00299 for (int i = 0; i < PQntuples(res); i+=n){
00300 MJDatabaseMaterial* mat = new MJDatabaseMaterial();
00301 mat->createMaterial(res, i);
00302 matVec.push_back(*mat);
00303 n = *((int*)PQgetvalue(res, i, 4));
00304 }
00305 fdbconn->qDone(res);
00306
00307
00308 return matVec;
00309
00310 }
00311 vector<string>
00312 MJDatabasePsqlImpl::MJDatabaseMaterialNamesPsqlImpl()
00313 {
00314
00315 vector<string> nameVec;
00316 string cmd = "select matname from mjmaterials ";
00317
00318 PGresult *res = fdbconn->qResult(cmd);
00319
00320
00321 if( PQntuples(res) == 0 ) {
00322 MJLog(error) << "Could not find materials in the database " << endlog;
00323
00324 }
00325 for (int i = 0; i < PQntuples(res); i++){
00326 nameVec.push_back(PQgetvalue(res, i, 0));
00327 }
00328 fdbconn->qDone(res);
00329
00330
00331 return nameVec;
00332 }
00333
00334
00335
00336
00337
00338
00339