Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Dbi.cxx

Go to the documentation of this file.
00001 
00002 // $Id: Dbi.cxx,v 1.46 2007/04/26 14:19:57 west Exp $
00003 //
00004 // Dbi
00005 //
00006 // N. West 12/2000
00007 //
00008 // Purpose: Dbi consists entirely of static data and member functions
00009 //          used for package interface.
00010 // 
00011 // General Remarks about Dbi
00012 // =========================
00013 // 
00014 // This primary purposes of this package are:-
00015 //
00016 //  o  To test out ideas.
00017 //
00018 //  o  To serve as an interim DBI for CalDet in 2001..
00019 // 
00020 // Internal Structure
00021 // ==================
00022 //
00023 //  Layered as follows, Higher levels can call those in lower levels
00024 //  Names on the same layer cannot call each other.  The Dbi prefix
00025 //  is omitted on all but Dbi itself.
00026 //  
00027 //  Validate
00028 //  ConfigStream
00029 //  Writer
00030 //  LogEntry
00031 //  ResultPtr
00032 //  SqlValPacket
00033 //  ValRecSet
00034 //  TableProxyRegistry
00035 //  TableProxy
00036 //  ResultAgg
00037 //  ValidityRecBuilder
00038 //  ResultNonAgg
00039 //  Cache  DBProxy
00040 //  Result
00041 //  Record
00042 //  ResultKey
00043 //  ConfigSet ValidityRec  DemoData1  DemoData2 DemoData3
00044 //  BinaryFile
00045 //  TableRow
00046 //  ResultSet OutRowStream
00047 //  RowStream
00048 //  ConnectionMaintainer
00049 //  Cascader
00050 //  SimFlagAssociation
00051 //  Statement
00052 //  Connection
00053 //  TableMetaData
00054 //  RollbackDates FieldType
00055 //  Dbi String Services
00056 //  DbiAsciiDbImporter
00057 //  DbiAsciiTablePreparer
00058 //  ExceptionLog
00059 //  Exception
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 //   Definition of static data members
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 // Definition of member functions (alphabetical order)
00084 // ***************************************************
00085 
00086 //.....................................................................
00087 
00088 Int_t Dbi::GetTimeGate(const std::string& tableName) {
00089 
00090 //  Purpose: Get a suitable time gate for table.
00091 
00092 //  Program Notes:-
00093 //  =============
00094 
00095 //  A time gate is applied to all primary (validity) queries to save time
00096 //  and ensure that gaps are not missed. Ideally the gate needs to be 
00097 //  of order of a typical validity range.  Much longer and unneeded
00098 //  VLD records are processed, much shorter and the query results will
00099 //  expire too soon.  Both increase I/O.
00100 //  
00101 //  The system provides a default for each table and DbiValidityRecBuilder
00102 //  updates it if grossly wrong.
00103 
00104   // Set default if looking up table for the first time.
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 /*= false*/) {
00120 //
00121 //
00122 //  Purpose:  Return string "create [temporary] table tableNameXXX" to make table xxxVLD
00123 //
00124 //  Program Notes:-
00125 //  =============
00126 
00127 //  If the format of Validity Tables is changed then this function must
00128 //  be updated as that format is hardwired into an SQL CREATE TABLE
00129 //  command in this function.
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 //  Purpose: Convert VldTimeStamp to SQL DateTime string.
00155 //
00156 //  Arguments: 
00157 //    timeStamp    in    VldTimeStamp to be converted.
00158 //
00159 //  Return: SQL DateTime string corresponding to VldTimeStamp.   
00160 //
00161 //  Contact:   N. West
00162 //
00163 //  Specification:-
00164 //  =============
00165 //
00166 //  o Return SQL DateTime string corresponding to VldTimeStamp.
00167 
00168 //  Program Notes:-
00169 //  =============
00170 
00171 //  The format of a  SQL DateTime string is:-
00172 // 
00173 //            YYYY-MM-DD HH:MM:SS
00174 //    e.g.    2001-01-03 00:00:00
00175 //            0123456789012345678901234567890123"
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 //  Purpose:  Convert SQL DateTime string to VldTimeStamp.
00187 //
00188 //  Arguments: 
00189 //    sqlDateTime  in   SQL DateTime string to be convered.
00190 //    ok           in    Optional return flag
00191 //                       If not supplied, report illegal dates
00192 //
00193 //  Return:  VldTimeStamp corresponding to SQL DateTime string.  
00194 //
00195 //  Contact:   N. West
00196 //
00197 //  Specification:-
00198 //  =============
00199 //
00200 //  o  Return VldTimeStamp corresponding to SQL DateTime string.
00201 
00202 //  Program Notes:-
00203 //  =============
00204 
00205 //  Note that there is only white space between day and hour
00206 //  so no need for dummy separator.
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   // Set up defaults from 0:0am today.
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 //  Purpose:  Set MessageService log level for package.
00271 //
00272 //  Arguments: 
00273 //    level        in    Log level. 
00274 //
00275 //  Return:    None.
00276 //
00277 //  Contact:   N. West
00278 //
00279 //  Specification:-
00280 //  =============
00281 //
00282 //  o  Set MessageService log level for package.
00283 
00284 //  Program Notes:-
00285 //  =============
00286 
00287 //  None.
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 //  Purpose: Set a time gate for table.
00298 
00299 //  Program Notes:  See GetTimeGate.
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 /*    Template for New Member Function
00315 
00316 //.....................................................................
00317 
00318 Dbi:: {
00319 //
00320 //
00321 //  Purpose:  
00322 //
00323 //  Arguments: 
00324 //    xxxxxxxxx    in    yyyyyy
00325 //
00326 //  Return:    
00327 //
00328 //  Contact:   N. West
00329 //
00330 //  Specification:-
00331 //  =============
00332 //
00333 //  o 
00334 
00335 //  Program Notes:-
00336 //  =============
00337 
00338 //  None.
00339 
00340 
00341 }
00342 
00343 */
00344 

Generated on Mon Feb 15 11:06:34 2010 for loon by  doxygen 1.3.9.1