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
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00062
00063 #include <stdio.h>
00064 #include <sstream>
00065
00066 #include "TList.h"
00067
00068 #include "Conventions/Detector.h"
00069 #include "Conventions/SimFlag.h"
00070 #include "MessageService/MsgService.h"
00071 #include "DatabaseInterface/Dbi.h"
00072 #include "DatabaseInterface/DbiException.h"
00073 #include "DatabaseInterface/DbiExceptionLog.h"
00074
00075
00076
00077
00078
00079 CVSID("$Id: Dbi.cxx,v 1.46 2007/04/26 14:19:57 west Exp $");
00080
00081 static std::map<std::string,Int_t> fgTimegateTable;
00082
00083
00084
00085
00086
00087
00088 Int_t Dbi::GetTimeGate(const std::string& tableName) {
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105 std::map<std::string,Int_t>::iterator
00106 tablePtr = fgTimegateTable.find(tableName);
00107 if ( tablePtr == fgTimegateTable.end()
00108 ) Dbi::SetTimeGate(tableName,10*24*60*60);
00109
00110 MSG("Dbi",Msg::kDebug) << "Returning time gate " << fgTimegateTable[tableName]
00111 << " for " << tableName << endl;
00112 return fgTimegateTable[tableName];
00113
00114 }
00115
00116
00117
00118 std::string Dbi::GetVldDescr(const char* tableName,
00119 Bool_t isTemporary ) {
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131 string sql;
00132 sql += "create ";
00133 if ( isTemporary ) sql += "temporary ";
00134 sql += "table ";
00135 sql += tableName;
00136 sql += "VLD ( ";
00137 sql += " SEQNO integer not null primary key,";
00138 sql += " TIMESTART datetime not null,";
00139 sql += " TIMEEND datetime not null,";
00140 sql += " DETECTORMASK tinyint,";
00141 sql += " SIMMASK tinyint,";
00142 sql += " TASK integer,";
00143 sql += " AGGREGATENO integer,";
00144 sql += " CREATIONDATE datetime not null,";
00145 sql += " INSERTDATE datetime not null ) ";
00146 return sql;
00147 }
00148
00149
00150
00151 string Dbi::MakeDateTimeString(const VldTimeStamp& timeStamp) {
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 return timeStamp.AsString("s");
00178
00179 }
00180
00181
00182 VldTimeStamp Dbi::MakeTimeStamp(const std::string& sqlDateTime,
00183 Bool_t* ok) {
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208 struct date {
00209 int year;
00210 int month;
00211 int day;
00212 int hour;
00213 int min;
00214 int sec;};
00215 char dummy;
00216
00217 static string lo = "1970-01-01 00:00:00";
00218 static string hi = "2038-01-19 03:14:07";
00219
00220
00221 VldTimeStamp nowTS;
00222 int nowDate = nowTS.GetDate();
00223 date defaultDate = {nowDate/10000, nowDate/100%100, nowDate%100,0,0,0};
00224 date input = defaultDate;
00225
00226 istringstream in(sqlDateTime);
00227 in >> input.year >> dummy >> input.month >> dummy >> input.day
00228 >> input.hour >> dummy >> input.min >> dummy >> input.sec;
00229
00230 if ( ok ) *ok = kTRUE;
00231 if ( sqlDateTime < lo || sqlDateTime > hi ) {
00232 if ( ok ) *ok = kFALSE;
00233 else {
00234 static int bad_date_count = 0;
00235 if ( ++bad_date_count <= 20 ) {
00236 const char* last = (bad_date_count == 20) ? "..Last Message.. " : "";
00237 MSG("Dbi",Msg::kError)
00238 << "Bad date string: " << sqlDateTime
00239 << " parsed as "
00240 << input.year << " "
00241 << input.month << " "
00242 << input.day << " "
00243 << input.hour << " "
00244 << input.min << " "
00245 << input.sec
00246 << "\n Outside range " << lo
00247 << " to " << hi << last << endl;
00248 }
00249 }
00250
00251 input = defaultDate;
00252 }
00253
00254 return VldTimeStamp(input.year,input.month,input.day,
00255 input.hour,input.min,input.sec);
00256
00257 }
00258
00259
00260 Bool_t Dbi::NotGlobalSeqNo(UInt_t seqNo) {
00261 return seqNo <= kMAXLOCALSEQNO;
00262 }
00263
00264
00265
00266
00267 void Dbi::SetLogLevel(int level) {
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289 MsgService::Instance()->GetStream("Dbi")->SetLogLevel(level);
00290
00291 }
00292
00293
00294
00295 void Dbi::SetTimeGate(const std::string& tableName, Int_t timeGate) {
00296
00297
00298
00299
00300
00301 if ( timeGate > 15 && timeGate <= 100*24*60*60 ) {
00302 fgTimegateTable[tableName] = timeGate;
00303 MSG("Dbi",Msg::kDebug) << "Setting time gate " << timeGate
00304 << " for " << tableName << endl;
00305 }
00306 else {
00307 MSG("Dbi",Msg::kWarning) << "Ignoring invalid time gate setting " << timeGate
00308 << " for " << tableName << endl;
00309 }
00310 }
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344