demo.cxx

Go to the documentation of this file.
00001 //----------------------------------------------------------------------------------------------
00002 //
00003 // To run this demo:-
00004 // 
00005 // 1) Set up a database (say t2k_db) on a mysql server (say mysqlserver.t2k.com) 
00006 //    with a write access account (say t2k_writer,t2k_writer_psw) using the 
00007 //    following environmental variables:-
00008 // 
00009 //    export SK_TSQL_URL="mysql://neut12.triumf.ca/testskcalib"
00010 //    export SK_TSQL_USER="skcaldb_writer"
00011 //    export SK_TSQL_PSWD="sk222wrtr"
00012 //    
00013 //    You need to have added ROOT to your LD_LIBRARY_PATH as well
00014 // 
00015 //    There are scripts (setup_writer.*sh) that do this for you
00016 //    
00017 // 2) Prime it:-
00018 //
00019 //    app/database_updater.py apply_local_update demo/demo_db_table.update
00020 // 
00021 // 3) Run the demo:-
00022 // 
00023 //    linux/demo.exe 
00024 //
00025 //----------------------------------------------------------------------------------------------
00026 #include "TVldTimeStamp.hxx"
00027 #include "TDbi.hxx"
00028 
00029 Int_t DateTimeToUnixTime(const std::string& dateTime) {
00030   return TDbi::MakeTimeStamp(dateTime).GetSec();
00031 }
00032 
00033 std::string UnixTimeToDateTime(Int_t unixSecs) {
00034   return TDbi::MakeDateTimeString(TVldTimeStamp(unixSecs,0));
00035 }
00036 
00037 
00038 
00039 #include "TSK_DBI_Context.hxx"
00040 #include "TDemo_DB_Table.hxx"
00041 #include "TResultSetHandle.hxx"
00042 
00043 #include <iostream>
00044 #include <string>
00045 
00046 int main() {
00047 
00048 
00049   // Examine the Database table DEMO_DB_TABLE every 5 days between the start
00050   // and end date.
00051 
00052   std::string start_date_time = "2008-12-30 00:00:00";
00053   std::string end_date_time   = "2009-03-01 00:00:00";
00054 
00055   Int_t start_unix_time = DateTimeToUnixTime(start_date_time);
00056   Int_t end_unix_time   = DateTimeToUnixTime(end_date_time);
00057 
00058   UInt_t required_id      = 1;
00059 
00060   for (Int_t current_unix_time = start_unix_time; current_unix_time <= end_unix_time; current_unix_time += 5*24*60*60) {
00061     TSK_DBI_Context context;
00062     context.SetTimeStamp(current_unix_time);
00063     TResultSetHandle<TDemo_DB_Table> rs(context);
00064     SK_DBI_Log("_______________________________________________________________________________");
00065     Int_t numRows(rs.GetNumRows());
00066     if ( ! numRows )  SK_DBI_Log("Applying query at " << UnixTimeToDateTime(current_unix_time) << " ... failed to find any results.");
00067     else {
00068       SK_DBI_Log("Applying query at " << UnixTimeToDateTime(current_unix_time) << " ... result set contains " 
00069                                     << numRows << " rows as follows:-");
00070       for (Int_t irow = 0; irow<numRows; ++irow) rs.GetRow(irow)->Print();
00071       const TDemo_DB_Table* required_row = rs.GetRowByIndex(required_id);
00072       if ( required_row ) {
00073         SK_DBI_Log("  required row " << 1);
00074         required_row->Print();
00075       }
00076       else SK_DBI_Log("  cannot find required row " << 1);      
00077     }
00078   }
00079 
00080   return 0;
00081 }

Generated on 11 Aug 2013 for SKDatabase by  doxygen 1.6.1