31 TSQLMonitoringWriter::TSQLMonitoringWriter(
const char *serv,
const char *user,
32 const char *pass,
const char *table)
33 : TVirtualMonitoringWriter(
"SQL", 0.0), fTable(table), fVerbose(kFALSE)
36 fDB = TSQLServer::Connect(serv, user, pass);
37 if (!fDB || fDB->IsZombie()) {
43 fMaxBulkSize = 16 * 1024 * 1024;
44 TString smx = gEnv->GetValue(
"SQLMonitoringWriter.MaxBulkSize",
"16M");
46 if (smx.EndsWith(
"K", TString::kIgnoreCase)) {
47 smx.Remove(smx.Length()-1);
48 if (smx.IsDigit()) fMaxBulkSize = smx.Atoi() * 1024;
49 }
else if (smx.EndsWith(
"M", TString::kIgnoreCase)) {
50 smx.Remove(smx.Length()-1);
51 if (smx.IsDigit()) fMaxBulkSize = smx.Atoi() * 1024 * 1024;
52 }
else if (smx.EndsWith(
"G", TString::kIgnoreCase)) {
53 smx.Remove(smx.Length()-1);
54 if (smx.IsDigit()) fMaxBulkSize = smx.Atoi() * 1024 * 1024 * 1024;
57 fMaxBulkSize = smx.Atoi();
64 TSQLMonitoringWriter::~TSQLMonitoringWriter()
87 Bool_t TSQLMonitoringWriter::SendParameters(TList *values,
const char *opt)
95 if (!values || (values && values->GetSize() < 1))
99 TString table(fTable), op, ops(opt);
101 Bool_t bulk = kFALSE;
102 while (ops.Tokenize(op, from,
",")) {
105 }
else if (op.BeginsWith(
"table=")) {
106 op.ReplaceAll(
"table=",
"");
108 Ssiz_t idot = table.Index(
'.');
109 if (idot != kNPOS && op.Index(
'.') == kNPOS) {
110 table.Remove(idot+1);
123 TString sql = TString::Format(
"INSERT INTO %s", table.Data());
131 while ((o = nxi())) {
132 if (!strncmp(o->ClassName(),
"TNamed", 6)) {
133 cols += TString::Format(
"%c%s", c, ((TNamed *)o)->GetName());
134 vals += TString::Format(
"%c'%s'", c, ((TNamed *)o)->GetTitle());
135 }
else if (!strcmp(o->ClassName(),
"TParameter<Long64_t>")) {
136 cols += TString::Format(
"%c%s", c, ((TParameter<Long64_t> *)o)->GetName());
137 vals += TString::Format(
"%c%lld", c, ((TParameter<Long64_t> *)o)->GetVal());
138 }
else if (!strcmp(o->ClassName(),
"TParameter<double>")) {
139 cols += TString::Format(
"%c%s", c, ((TParameter<double> *)o)->GetName());
140 vals += TString::Format(
"%c%f", c, ((TParameter<double> *)o)->GetVal());
141 }
else if (!strcmp(o->ClassName(),
"TParameter<float>")) {
142 cols += TString::Format(
"%c%s", c, ((TParameter<float> *)o)->GetName());
143 vals += TString::Format(
"%c%f", c, ((TParameter<float> *)o)->GetVal());
144 }
else if (!strcmp(o->ClassName(),
"TParameter<int>")) {
145 cols += TString::Format(
"%c%s", c, ((TParameter<int> *)o)->GetName());
146 vals += TString::Format(
"%c%d", c, ((TParameter<int> *)o)->GetVal());
147 }
else if (!strcmp(o->ClassName(),
"TParameter<long>")) {
148 cols += TString::Format(
"%c%s", c, ((TParameter<long> *)o)->GetName());
149 vals += TString::Format(
"%c%ld", c, ((TParameter<long> *)o)->GetVal());
157 sql += TString::Format(
" %s VALUES %s", cols.Data(), vals.Data());
160 if (fVerbose) Info(
"SendParameters",
"sending: '%s'", sql.Data());
161 if (!(res = fDB->Query(sql))) {
162 Error(
"SendParameters",
"insert into %s failed", table.Data());
163 if (sql.Length() > 1024) {
164 TString head(sql(0,508)), tail(sql(sql.Length()-512,512));
165 Printf(
"%s...%s", head.Data(), tail.Data());
167 Printf(
"%s", sql.Data());
176 TObjString *os =
dynamic_cast<TObjString *
>(o);
178 Error(
"SendParameters",
"bulk insert: first entry in list is not 'TObjString' but '%s'", o->ClassName() );
182 sql += TString::Format(
" (%s) VALUES ", os->GetName());
184 if (fVerbose) Info(
"SendParameters",
"sending: '%s' (bulk of %d nplets)", head.Data(), values->GetSize() - 1);
186 while ((o = nxi())) {
187 if ((os = dynamic_cast<TObjString *>(o))) {
188 sql += TString::Format(
"%c(%s)", c, os->GetName());
191 Warning(
"SendParameters",
"bulk insert: ignoring not 'TObjString' entry ('%s')", o->ClassName() );
194 if (sql.Length() > 0.9 * fMaxBulkSize) {
195 if (!(res = fDB->Query(sql))) {
196 Error(
"SendParameters",
"bulk insert into %s failed", table.Data());
197 if (sql.Length() > 1024) {
198 TString hd(sql(0,508)), tl(sql(sql.Length()-512,512));
199 Printf(
"%s...%s", hd.Data(), tl.Data());
201 Printf(
"%s", sql.Data());
211 if (sql.Length() > head.Length()) {
212 if (!(res = fDB->Query(sql))) {
213 Error(
"SendParameters",
"bulk insert into %s failed", table.Data());
214 if (sql.Length() > 1024) {
215 TString hd(sql(0,508)), tl(sql(sql.Length()-512,512));
216 Printf(
"%s...%s", hd.Data(), tl.Data());
218 Printf(
"%s", sql.Data());