ROOT logo
// @(#)root/proofplayer:$Id: TSQLMonitoring.cxx 23487 2008-04-23 17:13:12Z brun $
// Author: J.F. Grosse-Oetringhaus, G.Ganis

/*************************************************************************
 * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TSQLMonitoringWriter                                                 //
//                                                                      //
// SQL implementation of TVirtualMonitoringWriter.                      //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include "TList.h"
#include "TParameter.h"
#include "TEnv.h"
#include "TSQLMonitoring.h"
#include "TSQLServer.h"
#include "TSQLResult.h"

//______________________________________________________________________________
TSQLMonitoringWriter::TSQLMonitoringWriter(const char *serv, const char *user,
                                           const char *pass, const char *table)
   : TVirtualMonitoringWriter("SQL", 0.0), fTable(table)
{
   // Constructor.

   // Open connection to SQL server
   fDB = TSQLServer::Connect(serv, user, pass);
   if (!fDB || fDB->IsZombie()) {
      SafeDelete(fDB);
      // Invalid object
      MakeZombie();
   }
}

//______________________________________________________________________________
TSQLMonitoringWriter::~TSQLMonitoringWriter()
{
   // Destructor

   SafeDelete(fDB);
}

//______________________________________________________________________________
Bool_t TSQLMonitoringWriter::SendParameters(TList *values, const char *)
{
   // Register query log using the information in the list which is in the form
   // TParameter(<par>,<value>) or TNamed(<name>,<string>).
   // The first element in the list is a TNamed object called TABLE with the
   // table name in the title field. Of course the specified table must already
   // have been created in the DB.

   if (!fDB) {
      // Invalid instance
      return kFALSE;
   }

   // the list must contain something
   if (!values || values->GetSize() <= 1)
      return kFALSE;

   TIter nxi(values);

   // now prepare the strings
   TString sql = Form("INSERT INTO %s", fTable.Data());

   // the column and values strings
   TObject *o = 0;
   char c = '(';
   TString cols, vals;
   while ((o = nxi())) {
      if (!strncmp(o->ClassName(), "TNamed", 6)) {
         cols += Form("%c'%s'", c, ((TNamed *)o)->GetName());
         vals += Form("%c'%s'", c, ((TNamed *)o)->GetTitle());
      } else if (!strcmp(o->ClassName(), "TParameter<Long64_t>")) {
         cols += Form(",'%s'", c, ((TParameter<Long64_t> *)o)->GetName());
         vals += Form("%c%lld", c, ((TParameter<Long64_t> *)o)->GetVal());
      } else if (!strcmp(o->ClassName(), "TParameter<double>")) {
         cols += Form("%c'%s'", c, ((TParameter<double> *)o)->GetName());
         vals += Form("%c%f", c, ((TParameter<double> *)o)->GetVal());
      } else if (!strcmp(o->ClassName(), "TParameter<float>")) {
         cols += Form("%c'%s'", c, ((TParameter<float> *)o)->GetName());
         vals += Form("%c%f", c, ((TParameter<float> *)o)->GetVal());
      } else if (!strcmp(o->ClassName(), "TParameter<int>")) {
         cols += Form("%c'%s'", c, ((TParameter<int> *)o)->GetName());
         vals += Form("%c%d", c, ((TParameter<int> *)o)->GetVal());
      } else if (!strcmp(o->ClassName(), "TParameter<long>")) {
         cols += Form("%c'%s'", c, ((TParameter<long> *)o)->GetName());
         vals += Form("%c%ld", c, ((TParameter<long> *)o)->GetVal());
      }
      c = ',';
   }
   cols += ")";
   vals += ")";

   // Put everything together
   sql += Form(" %s VALUES %s", cols.Data(), vals.Data());

   // Post query
   TSQLResult *res = fDB->Query(sql);
   if (!res) {
      Error("SendParameters", "insert into %s failed", fTable.Data());
      printf("%s\n", sql.Data());
      return kFALSE;
   }
   delete res;

   // Done successfully
   return kTRUE;
}
 TSQLMonitoring.cxx:1
 TSQLMonitoring.cxx:2
 TSQLMonitoring.cxx:3
 TSQLMonitoring.cxx:4
 TSQLMonitoring.cxx:5
 TSQLMonitoring.cxx:6
 TSQLMonitoring.cxx:7
 TSQLMonitoring.cxx:8
 TSQLMonitoring.cxx:9
 TSQLMonitoring.cxx:10
 TSQLMonitoring.cxx:11
 TSQLMonitoring.cxx:12
 TSQLMonitoring.cxx:13
 TSQLMonitoring.cxx:14
 TSQLMonitoring.cxx:15
 TSQLMonitoring.cxx:16
 TSQLMonitoring.cxx:17
 TSQLMonitoring.cxx:18
 TSQLMonitoring.cxx:19
 TSQLMonitoring.cxx:20
 TSQLMonitoring.cxx:21
 TSQLMonitoring.cxx:22
 TSQLMonitoring.cxx:23
 TSQLMonitoring.cxx:24
 TSQLMonitoring.cxx:25
 TSQLMonitoring.cxx:26
 TSQLMonitoring.cxx:27
 TSQLMonitoring.cxx:28
 TSQLMonitoring.cxx:29
 TSQLMonitoring.cxx:30
 TSQLMonitoring.cxx:31
 TSQLMonitoring.cxx:32
 TSQLMonitoring.cxx:33
 TSQLMonitoring.cxx:34
 TSQLMonitoring.cxx:35
 TSQLMonitoring.cxx:36
 TSQLMonitoring.cxx:37
 TSQLMonitoring.cxx:38
 TSQLMonitoring.cxx:39
 TSQLMonitoring.cxx:40
 TSQLMonitoring.cxx:41
 TSQLMonitoring.cxx:42
 TSQLMonitoring.cxx:43
 TSQLMonitoring.cxx:44
 TSQLMonitoring.cxx:45
 TSQLMonitoring.cxx:46
 TSQLMonitoring.cxx:47
 TSQLMonitoring.cxx:48
 TSQLMonitoring.cxx:49
 TSQLMonitoring.cxx:50
 TSQLMonitoring.cxx:51
 TSQLMonitoring.cxx:52
 TSQLMonitoring.cxx:53
 TSQLMonitoring.cxx:54
 TSQLMonitoring.cxx:55
 TSQLMonitoring.cxx:56
 TSQLMonitoring.cxx:57
 TSQLMonitoring.cxx:58
 TSQLMonitoring.cxx:59
 TSQLMonitoring.cxx:60
 TSQLMonitoring.cxx:61
 TSQLMonitoring.cxx:62
 TSQLMonitoring.cxx:63
 TSQLMonitoring.cxx:64
 TSQLMonitoring.cxx:65
 TSQLMonitoring.cxx:66
 TSQLMonitoring.cxx:67
 TSQLMonitoring.cxx:68
 TSQLMonitoring.cxx:69
 TSQLMonitoring.cxx:70
 TSQLMonitoring.cxx:71
 TSQLMonitoring.cxx:72
 TSQLMonitoring.cxx:73
 TSQLMonitoring.cxx:74
 TSQLMonitoring.cxx:75
 TSQLMonitoring.cxx:76
 TSQLMonitoring.cxx:77
 TSQLMonitoring.cxx:78
 TSQLMonitoring.cxx:79
 TSQLMonitoring.cxx:80
 TSQLMonitoring.cxx:81
 TSQLMonitoring.cxx:82
 TSQLMonitoring.cxx:83
 TSQLMonitoring.cxx:84
 TSQLMonitoring.cxx:85
 TSQLMonitoring.cxx:86
 TSQLMonitoring.cxx:87
 TSQLMonitoring.cxx:88
 TSQLMonitoring.cxx:89
 TSQLMonitoring.cxx:90
 TSQLMonitoring.cxx:91
 TSQLMonitoring.cxx:92
 TSQLMonitoring.cxx:93
 TSQLMonitoring.cxx:94
 TSQLMonitoring.cxx:95
 TSQLMonitoring.cxx:96
 TSQLMonitoring.cxx:97
 TSQLMonitoring.cxx:98
 TSQLMonitoring.cxx:99
 TSQLMonitoring.cxx:100
 TSQLMonitoring.cxx:101
 TSQLMonitoring.cxx:102
 TSQLMonitoring.cxx:103
 TSQLMonitoring.cxx:104
 TSQLMonitoring.cxx:105
 TSQLMonitoring.cxx:106
 TSQLMonitoring.cxx:107
 TSQLMonitoring.cxx:108
 TSQLMonitoring.cxx:109
 TSQLMonitoring.cxx:110
 TSQLMonitoring.cxx:111
 TSQLMonitoring.cxx:112
 TSQLMonitoring.cxx:113
 TSQLMonitoring.cxx:114
 TSQLMonitoring.cxx:115
 TSQLMonitoring.cxx:116
 TSQLMonitoring.cxx:117