TSQLMonitoring.cxx

Go to the documentation of this file.
00001 // @(#)root/proofplayer:$Id: TSQLMonitoring.cxx 38185 2011-02-21 14:55:16Z ganis $
00002 // Author: J.F. Grosse-Oetringhaus, G.Ganis
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // TSQLMonitoringWriter                                                 //
00015 //                                                                      //
00016 // SQL implementation of TVirtualMonitoringWriter.                      //
00017 //                                                                      //
00018 //////////////////////////////////////////////////////////////////////////
00019 
00020 #include "TList.h"
00021 #include "TParameter.h"
00022 #include "TEnv.h"
00023 #include "TSQLMonitoring.h"
00024 #include "TSQLServer.h"
00025 #include "TSQLResult.h"
00026 
00027 //______________________________________________________________________________
00028 TSQLMonitoringWriter::TSQLMonitoringWriter(const char *serv, const char *user,
00029                                            const char *pass, const char *table)
00030    : TVirtualMonitoringWriter("SQL", 0.0), fTable(table)
00031 {
00032    // Constructor.
00033 
00034    // Open connection to SQL server
00035    fDB = TSQLServer::Connect(serv, user, pass);
00036    if (!fDB || fDB->IsZombie()) {
00037       SafeDelete(fDB);
00038       // Invalid object
00039       MakeZombie();
00040    }
00041 }
00042 
00043 //______________________________________________________________________________
00044 TSQLMonitoringWriter::~TSQLMonitoringWriter()
00045 {
00046    // Destructor
00047 
00048    SafeDelete(fDB);
00049 }
00050 
00051 //______________________________________________________________________________
00052 Bool_t TSQLMonitoringWriter::SendParameters(TList *values, const char *)
00053 {
00054    // Register query log using the information in the list which is in the form
00055    // TParameter(<par>,<value>) or TNamed(<name>,<string>).
00056    // The first element in the list is a TNamed object called TABLE with the
00057    // table name in the title field. Of course the specified table must already
00058    // have been created in the DB.
00059 
00060    if (!fDB) {
00061       // Invalid instance
00062       return kFALSE;
00063    }
00064 
00065    // the list must contain something
00066    if (!values || values->GetSize() <= 1)
00067       return kFALSE;
00068 
00069    TIter nxi(values);
00070 
00071    // now prepare the strings
00072    TString sql = TString::Format("INSERT INTO %s", fTable.Data());
00073 
00074    // the column and values strings
00075    TObject *o = 0;
00076    char c = '(';
00077    TString cols, vals;
00078    while ((o = nxi())) {
00079       if (!strncmp(o->ClassName(), "TNamed", 6)) {
00080          cols += TString::Format("%c%s", c, ((TNamed *)o)->GetName());
00081          vals += TString::Format("%c'%s'", c, ((TNamed *)o)->GetTitle());
00082       } else if (!strcmp(o->ClassName(), "TParameter<Long64_t>")) {
00083          cols += TString::Format("%c%s", c, ((TParameter<Long64_t> *)o)->GetName());
00084          vals += TString::Format("%c%lld", c, ((TParameter<Long64_t> *)o)->GetVal());
00085       } else if (!strcmp(o->ClassName(), "TParameter<double>")) {
00086          cols += TString::Format("%c%s", c, ((TParameter<double> *)o)->GetName());
00087          vals += TString::Format("%c%f", c, ((TParameter<double> *)o)->GetVal());
00088       } else if (!strcmp(o->ClassName(), "TParameter<float>")) {
00089          cols += TString::Format("%c%s", c, ((TParameter<float> *)o)->GetName());
00090          vals += TString::Format("%c%f", c, ((TParameter<float> *)o)->GetVal());
00091       } else if (!strcmp(o->ClassName(), "TParameter<int>")) {
00092          cols += TString::Format("%c%s", c, ((TParameter<int> *)o)->GetName());
00093          vals += TString::Format("%c%d", c, ((TParameter<int> *)o)->GetVal());
00094       } else if (!strcmp(o->ClassName(), "TParameter<long>")) {
00095          cols += TString::Format("%c%s", c, ((TParameter<long> *)o)->GetName());
00096          vals += TString::Format("%c%ld", c, ((TParameter<long> *)o)->GetVal());
00097       }
00098       c = ',';
00099    }
00100    cols += ")";
00101    vals += ")";
00102 
00103    // Put everything together
00104    sql += TString::Format(" %s VALUES %s", cols.Data(), vals.Data());
00105 
00106    // Post query
00107    TSQLResult *res = fDB->Query(sql);
00108    if (!res) {
00109       Error("SendParameters", "insert into %s failed", fTable.Data());
00110       printf("%s\n", sql.Data());
00111       return kFALSE;
00112    }
00113    delete res;
00114 
00115    // Done successfully
00116    return kTRUE;
00117 }

Generated on Tue Jul 5 14:46:11 2011 for ROOT_528-00b_version by  doxygen 1.5.1