00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
00033
00034
00035 fDB = TSQLServer::Connect(serv, user, pass);
00036 if (!fDB || fDB->IsZombie()) {
00037 SafeDelete(fDB);
00038
00039 MakeZombie();
00040 }
00041 }
00042
00043
00044 TSQLMonitoringWriter::~TSQLMonitoringWriter()
00045 {
00046
00047
00048 SafeDelete(fDB);
00049 }
00050
00051
00052 Bool_t TSQLMonitoringWriter::SendParameters(TList *values, const char *)
00053 {
00054
00055
00056
00057
00058
00059
00060 if (!fDB) {
00061
00062 return kFALSE;
00063 }
00064
00065
00066 if (!values || values->GetSize() <= 1)
00067 return kFALSE;
00068
00069 TIter nxi(values);
00070
00071
00072 TString sql = TString::Format("INSERT INTO %s", fTable.Data());
00073
00074
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
00104 sql += TString::Format(" %s VALUES %s", cols.Data(), vals.Data());
00105
00106
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
00116 return kTRUE;
00117 }