TDbi.cxx

Go to the documentation of this file.
00001 ///////////////////////////////////////////////////////////////////////
00002 // $Id: TDbi.cxx,v 1.1 2011/01/18 05:49:19 finch Exp $
00003 //
00004 // TDbi
00005 //
00006 // N. West 12/2000
00007 //
00008 // Purpose: TDbi consists entirely of static data and member functions
00009 //          used for package interface.
00010 //
00011 // General Remarks about TDbi
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 TDbi prefix
00025 //  is omitted on all but TDbi itself.
00026 //
00027 //  Validate
00028 //  ConfigStream
00029 //  Writer
00030 //  LogEntry
00031 //  ResultPtr
00032 //  SqlValPacket
00033 //  ValRecSet
00034 //  TableProxyTDbiRegistry
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 //  TDbi String Services
00056 //  TDbiAsciiDbImporter
00057 //  TDbiAsciiTablePreparer
00058 //  ExceptionLog
00059 //  Exception
00060 //
00061 ///////////////////////////////////////////////////////////////////////
00062 
00063 #include <stdio.h>
00064 #include <sstream>
00065 
00066 #include "TList.h"
00067 
00068 #include "DbiDetector.hxx"
00069 #include "DbiSimFlag.hxx"
00070 #include <TSK_DBI_Log.hxx>
00071 #include <MsgFormat.h>
00072 using std::endl;
00073 #include "TDbi.hxx"
00074 #include "TDbiException.hxx"
00075 #include "TDbiExceptionLog.hxx"
00076 
00077 
00078 //   Definition of static data members
00079 //   *********************************
00080 
00081 
00082 static std::map<std::string,Int_t> fgTimegateTable;
00083 
00084 // Definition of member functions (alphabetical order)
00085 // ***************************************************
00086 
00087 //.....................................................................
00088 
00089 Int_t TDbi::GetTimeGate(const std::string& tableName) {
00090 
00091 //  Purpose: Get a suitable time gate for table.
00092 
00093 //  Program Notes:-
00094 //  =============
00095 
00096 //  A time gate is applied to all primary (validity) queries to save time
00097 //  and ensure that gaps are not missed. Ideally the gate needs to be
00098 //  of order of a typical validity range.  Much longer and unneeded
00099 //  VLD records are processed, much shorter and the query results will
00100 //  expire too soon.  Both increase I/O.
00101 //
00102 //  The system provides a default for each table and TDbiValidityRecBuilder
00103 //  updates it if grossly wrong.
00104 
00105   // Set default if looking up table for the first time.
00106   std::map<std::string,Int_t>::iterator
00107       tablePtr = fgTimegateTable.find(tableName);
00108   if ( tablePtr == fgTimegateTable.end()
00109      ) TDbi::SetTimeGate(tableName,10*24*60*60);
00110 
00111   SK_DBI_Debug( "Returning time gate " << fgTimegateTable[tableName]
00112                           << " for " << tableName << "  ");
00113   return fgTimegateTable[tableName];
00114 
00115 }
00116 
00117 //.....................................................................
00118 
00119 std::string TDbi::GetVldDescr(const char* tableName,
00120                              Bool_t isTemporary /*= false*/) {
00121 //
00122 //
00123 //  Purpose:  Return string "create [temporary] table tableNameXXX" to make table xxxVLD
00124 //
00125 //  Program Notes:-
00126 //  =============
00127 
00128 //  If the format of Validity Tables is changed then this function must
00129 //  be updated as that format is hardwired into an SQL CREATE TABLE
00130 //  command in this function.
00131 
00132   string sql;
00133   sql += "create ";
00134   if ( isTemporary ) sql += "temporary ";
00135   sql += "table ";
00136   sql += tableName;
00137   sql += "VLD ( ";
00138   sql += "  SEQNO integer not null primary key,";
00139   sql += "  TIMESTART datetime not null,";
00140   sql += "  TIMEEND datetime not null,";
00141   sql += "  EPOCH tinyint,";
00142   sql += "  REALITY tinyint,";
00143   sql += "  DETECTORMASK tinyint,";
00144   sql += "  SIMMASK tinyint,";
00145   sql += "  TASK integer,";
00146   sql += "  AGGREGATENO integer,";
00147   sql += "  CREATIONDATE datetime not null,";
00148   sql += "  INSERTDATE datetime not null ) ";
00149   return sql;
00150 }
00151 
00152 //.....................................................................
00153 
00154 string TDbi::MakeDateTimeString(const TVldTimeStamp& timeStamp) {
00155 //
00156 //
00157 //  Purpose: Convert TVldTimeStamp to SQL DateTime string.
00158 //
00159 //  Arguments:
00160 //    timeStamp    in    TVldTimeStamp to be converted.
00161 //
00162 //  Return: SQL DateTime string corresponding to TVldTimeStamp.
00163 //
00164 //  Contact:   N. West
00165 //
00166 //  Specification:-
00167 //  =============
00168 //
00169 //  o Return SQL DateTime string corresponding to TVldTimeStamp.
00170 
00171 //  Program Notes:-
00172 //  =============
00173 
00174 //  The format of a  SQL DateTime string is:-
00175 //
00176 //            YYYY-MM-DD HH:MM:SS
00177 //    e.g.    2001-01-03 00:00:00
00178 //            0123456789012345678901234567890123"
00179 
00180  return timeStamp.AsString("s");
00181 
00182 }
00183 //.....................................................................
00184 
00185 TVldTimeStamp TDbi::MakeTimeStamp(const std::string& sqlDateTime,
00186                                 Bool_t* ok) {
00187 //
00188 //
00189 //  Purpose:  Convert SQL DateTime string to TVldTimeStamp.
00190 //
00191 //  Arguments:
00192 //    sqlDateTime  in   SQL DateTime string to be convered.
00193 //    ok           in    Optional return flag
00194 //                       If not supplied, report illegal dates
00195 //
00196 //  Return:  TVldTimeStamp corresponding to SQL DateTime string.
00197 //
00198 //  Contact:   N. West
00199 //
00200 //  Specification:-
00201 //  =============
00202 //
00203 //  o  Return TVldTimeStamp corresponding to SQL DateTime string.
00204 
00205 //  Program Notes:-
00206 //  =============
00207 
00208 //  Note that there is only white space between day and hour
00209 //  so no need for dummy separator.
00210 
00211   struct date {
00212     int year;
00213     int month;
00214     int day;
00215     int hour;
00216     int min;
00217     int sec;};
00218   char dummy;
00219 
00220   static string lo = "1970-01-01 00:00:00";
00221   static string hi = "2038-01-19 03:14:07";
00222 
00223   // Set up defaults from 0:0am today.
00224   TVldTimeStamp nowTS;
00225   int nowDate = nowTS.GetDate();
00226   date defaultDate = {nowDate/10000, nowDate/100%100, nowDate%100,0,0,0};
00227   date input       = defaultDate;
00228 
00229   istringstream in(sqlDateTime);
00230   in >> input.year >> dummy >> input.month >> dummy >> input.day
00231      >> input.hour >> dummy >> input.min   >> dummy >> input.sec;
00232 
00233   if ( ok ) *ok = kTRUE;
00234   if (  sqlDateTime < lo || sqlDateTime > hi ) {
00235     if ( ok ) *ok = kFALSE;
00236     else {
00237       static int bad_date_count = 0;
00238       if ( ++bad_date_count <= 20 ) {
00239         const char* last = (bad_date_count == 20) ? "..Last Message.. " : "";
00240         SK_DBI_Severe(  "Bad date string: " << sqlDateTime
00241                             << " parsed as "
00242                             << input.year  << " "
00243                             << input.month << " "
00244                             << input.day   << " "
00245                             << input.hour  << " "
00246                             << input.min   << " "
00247                             << input.sec
00248                             << "\n    Outside range " << lo
00249                             << " to " << hi << last << "  ");
00250       }
00251     }
00252 
00253     input = defaultDate;
00254   }
00255 
00256   return TVldTimeStamp(input.year,input.month,input.day,
00257                       input.hour,input.min,input.sec);
00258 
00259 }
00260 //.....................................................................
00261 
00262 Bool_t TDbi::NotGlobalSeqNo(UInt_t seqNo) {
00263   return seqNo <= kMAXLOCALSEQNO;
00264 }
00265 
00266 
00267 //.....................................................................
00268 
00269 void TDbi::SetLogLevel(int level) {
00270 //
00271 //
00272 //  Purpose:  Set MessageService log level for package.
00273 //
00274 //  Arguments:
00275 //    level        in    Log level.
00276 //
00277 //  Return:    None.
00278 //
00279 //  Contact:   N. West
00280 //
00281 //  Specification:-
00282 //  =============
00283 //
00284 //  o  Set MessageService log level for package.
00285 
00286 //  Program Notes:-
00287 //  =============
00288 
00289 //  None.
00290 
00291 
00292 
00293 }
00294 
00295 //.....................................................................
00296 
00297 void TDbi::SetTimeGate(const std::string& tableName, Int_t timeGate) {
00298 
00299 //  Purpose: Set a time gate for table.
00300 
00301 //  Program Notes:  See GetTimeGate.
00302 
00303   if ( timeGate > 15 && timeGate <= 100*24*60*60 ) {
00304     fgTimegateTable[tableName] = timeGate;
00305     SK_DBI_Debug( "Setting time gate " << timeGate
00306                             << " for " << tableName << "  ");
00307   }
00308   else {
00309     SK_DBI_Warn( "Ignoring  invalid time gate setting " << timeGate
00310                              << " for " << tableName << "  ");
00311   }
00312 }
00313 
00314 // ********************************************************************
00315 
00316 /*    Template for New Member Function
00317 
00318 //.....................................................................
00319 
00320 TDbi:: {
00321 //
00322 //
00323 //  Purpose:
00324 //
00325 //  Arguments:
00326 //    xxxxxxxxx    in    yyyyyy
00327 //
00328 //  Return:
00329 //
00330 //  Contact:   N. West
00331 //
00332 //  Specification:-
00333 //  =============
00334 //
00335 //  o
00336 
00337 //  Program Notes:-
00338 //  =============
00339 
00340 //  None.
00341 
00342 
00343 }
00344 
00345 */
00346 
00347 
00348 

Generated on 11 Aug 2013 for SKDatabase by  doxygen 1.6.1