source: branches/Mars_McMismatchStudy/msql/MSqlInsertRun.cc@ 18796

Last change on this file since 18796 was 3328, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 13.1 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz 2/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2004
21!
22!
23\* ======================================================================== */
24
25////////////////////////////////////////////////////////////////////////
26//
27// MSqlInsertRun
28//
29// Input Containers:
30// MRawRunHeader
31//
32// Output Containers:
33// -/-
34//
35////////////////////////////////////////////////////////////////////////
36#include "MSqlInsertRun.h"
37
38#include <TSystem.h>
39
40#include <TSQLResult.h>
41#include <TSQLRow.h>
42
43#include "MLog.h"
44#include "MLogManip.h"
45
46#include "MParList.h"
47#include "MTaskList.h"
48
49#include "MRawFileRead.h"
50#include "MRawRunHeader.h"
51
52#include "MSQLServer.h"
53
54ClassImp(MSqlInsertRun);
55
56using namespace std;
57
58// --------------------------------------------------------------------------
59//
60MSqlInsertRun::MSqlInsertRun(const char *db, const char *user, const char *pw)
61 : fIsUpdate(kFALSE)
62{
63 fName = "MSqlInsertRun";
64 fTitle = "Write run into database";
65
66 *fLog << dbg << "Connecting to: " << db << " as " << user << " <" << pw << ">" << endl;
67
68 fSqlServer = new MSQLServer(db, user, pw);
69}
70
71// --------------------------------------------------------------------------
72//
73MSqlInsertRun::MSqlInsertRun(const char *u)
74 : fSqlServer(0), fIsUpdate(kFALSE)
75{
76 fName = "MSqlInsertRun";
77 fTitle = "Write run into database";
78
79 fSqlServer = new MSQLServer(u); //::Connect(url, user, pasw);
80}
81
82MSqlInsertRun::~MSqlInsertRun()
83{
84 if (fSqlServer)
85 delete fSqlServer;
86}
87
88Bool_t MSqlInsertRun::PrintError(const char *txt, const char *q) const
89{
90 *fLog << err;
91 *fLog << "Fatal error acessing database: " << txt << endl;
92 fLog->Underline();
93 *fLog << "Query:" << flush << " " << q << endl;
94 return kFALSE;
95}
96
97TString MSqlInsertRun::GetEntry(const char *table, const char *col, const char *where)
98{
99 return fSqlServer->GetEntry(where, col, table);
100}
101
102Bool_t MSqlInsertRun::IsRunExisting(MRawRunHeader *h, Bool_t &exist)
103{
104 exist = kFALSE;
105
106 const TString str(GetEntry("RunData", "fRunNumber", Form("fRunNumber=%d", h->GetRunNumber())));
107 if (str.IsNull())
108 return kFALSE;
109
110 exist = kTRUE;
111 return kTRUE;
112}
113
114Int_t MSqlInsertRun::GetKey(const char *table, const char *where)
115{
116 const TString str(GetEntry(table, Form("f%sKEY", table), where));
117
118 Int_t key;
119 return sscanf(str.Data(), "%d", &key)==1 ? key : -1;
120}
121
122TString MSqlInsertRun::GetKeyStr(const char *table, const char *where, Bool_t &rc)
123{
124 const TString str(GetEntry(table, Form("f%sKEY", table), where));
125 if (str.IsNull())
126 rc = kFALSE;
127
128 return str;
129}
130
131TString MSqlInsertRun::MagicNumber(MRawRunHeader *h, Bool_t &ok)
132{
133 Int_t key = GetKey("MagicNumber", Form("fMagicNumber=%d", h->GetMagicNumber()));
134 if (key<0)
135 key = GetKey("MagicNumber", "fMagicNumber=0xffff");
136 if (key<0)
137 {
138 ok = kFALSE;
139 return TString("");
140 }
141 return TString(Form("%d", key));
142}
143
144TString MSqlInsertRun::RunType(MRawRunHeader *h, Bool_t &ok)
145{
146 Int_t key = GetKey("RunType", Form("fRunType=%d", h->GetRunType()));
147 if (key<0)
148 key = GetKey("RunType", "fRunTye=0xffff");
149 if (key<0)
150 {
151 ok = kFALSE;
152 return TString("");
153 }
154 return TString(Form("%d", key));
155}
156
157TString MSqlInsertRun::Source(MRawRunHeader *h, Bool_t &ok)
158{
159 Int_t key = GetKey("Source", Form("fSourceName='%s'", h->GetSourceName()));
160 if (key<0)
161 {
162 const char *q = Form("INSERT Source (fSourceName, fSourceTxt) VALUES ('%s', 'MSqlInsert: %s')",
163 h->GetSourceName(), h->GetSourceName());
164
165 TSQLResult *res = fSqlServer->Query(q);
166 if (!res)
167 {
168 ok = kFALSE;
169 return TString("");
170 }
171 key = GetKey("Source", Form("fSourceName='%s'", h->GetSourceName()));
172 if (key>0)
173 *fLog << inf << "New Source '" << h->GetSourceName() << "' inserted into table Source of database." << endl;
174 }
175 else
176 {
177 // FIXME: check for consistency!
178 }
179 if (key<0)
180 {
181 ok = kFALSE;
182 return TString("");
183 }
184
185 return TString(Form("%d", key));
186}
187
188TString MSqlInsertRun::Project(MRawRunHeader *h, Bool_t &ok)
189{
190 Int_t key = GetKey("Project", Form("fProjectName='%s'", h->GetProjectName()));
191 if (key<0)
192 {
193 const char *q = Form("INSERT Project (fProjectName, fProjectTxt) VALUES ('%s', 'MSqlInsert: %s')",
194 h->GetProjectName(), h->GetProjectName());
195
196 TSQLResult *res = fSqlServer->Query(q);
197 if (!res)
198 {
199 ok = kFALSE;
200 return TString("");
201 }
202 key = GetKey("Project", Form("fProjectName='%s'", h->GetProjectName()));
203 if (key>0)
204 *fLog << inf << "New Project '" << h->GetProjectName() << "' inserted into table Project of database." << endl;
205 }
206 if (key<0)
207 {
208 ok = kFALSE;
209 return TString("");
210 }
211
212 return TString(Form("%d", key));
213}
214
215TString MSqlInsertRun::RawFilePath(const char *path, Bool_t &ok)
216{
217 Int_t key = GetKey("FilePath", Form("fFilePathName='%s'", path));
218 if (key<0)
219 {
220 const char *q = Form("INSERT FilePath (fFilePathName, fFilePath) VALUES ('%s', 'MSqlInsert: %s')",
221 path, path);
222
223 TSQLResult *res = fSqlServer->Query(q);
224 if (!res)
225 {
226 ok = kFALSE;
227 return TString("");
228 }
229 key = GetKey("FilePath", Form("fFilePathName='%s'", path));
230 if (key>0)
231 *fLog << inf << "New FilePath '" << path << "' inserted into table FilePath of database." << endl;
232
233 }
234 if (key<0)
235 {
236 ok = kFALSE;
237 return TString("");
238 }
239
240 return TString(Form("%d", key));
241}
242/*
243#include <TArrayI.h>
244
245void Line(const TArrayI &max)
246{
247 cout << "+" << setfill('-');
248 for (int i=0; i<max.GetSize(); i++)
249 cout << setw(max[i]+1) << "-" << "-+";
250 cout << endl;
251}
252
253void PrintResult(TSQLResult *res)
254{
255 Int_t n = res->GetFieldCount();
256
257 TArrayI max(n);
258
259 for (int i=0; i<n; i++)
260 max[i] = strlen(res->GetFieldName(i));
261
262 TSQLRow *row;
263
264 TList rows;
265 while ((row=res->Next()))
266 {
267 for (int i=0; i<n; i++)
268 max[i] = TMath::Max((ULong_t)max[i], row->GetFieldLength(i));
269 rows.Add(row);
270 }
271
272 Line(max);
273
274 cout << "|" << setfill(' ');
275 for (int i=0; i<n; i++)
276 cout << setw(max[i]+1) << res->GetFieldName(i) << " |";
277 cout << endl;
278
279 Line(max);
280
281 cout << setfill(' ');
282 TIter Next(&rows);
283 while ((row=(TSQLRow*)Next()))
284 {
285 cout << "|";
286 for (int i=0; i<n; i++)
287 {
288 const char *c = (*row)[i];
289 cout << setw(max[i]+1) << (c?c:"") << " |";
290 }
291 cout << endl;
292 }
293
294 Line(max);
295}
296*/
297Bool_t MSqlInsertRun::InsertRun(MRawRunHeader *h, Bool_t update)
298{
299 // FIXME: Insert input file (PreProcess), output file (PostProcess)
300
301 TString query(update ? "UPDATE" : "INSERT");
302
303 Bool_t ok=kTRUE;
304
305 query += " RunData SET fMagicNumberKEY='";
306 query += MagicNumber(h, ok);
307 query += "', fFormatVersion='";
308 query += h->GetFormatVersion();
309 query += "', fRunTypeKEY='";
310 query += RunType(h, ok);
311 query += "', fRunNumber='";
312 query += h->GetRunNumber();
313 query += "', fProjectKEY='";
314 query += Project(h, ok);
315 query += "', fSourceKEY='";
316 query += Source(h, ok);
317 query += "', fNumEvents='";
318 query += h->GetNumEvents();
319 query += "', fRunStart='";
320 query += h->GetRunStart().GetSqlDateTime();
321 query += "', fRunStop='";
322 query += h->GetRunEnd().GetSqlDateTime();
323 query += "'";
324
325 if (update)
326 {
327 query += " WHERE fRunNumber=";
328 query += h->GetRunNumber();
329 }
330
331 if (!ok)
332 {
333 *fLog << err << "ERROR - while concatenating query..." << endl;
334 *fLog << query << endl;
335 return kFALSE;
336 }
337
338 TSQLResult *res = fSqlServer->Query(query);
339 if (!res)
340 return PrintError("TSQLResult==NULL", query);
341
342 *fLog << inf << dec;
343 *fLog << "Run #" << h->GetRunNumber() << " ";
344 *fLog << (update ? "updated" : "inserted");
345 *fLog << " in database successfully." << endl;
346
347 return kTRUE;
348}
349
350Int_t MSqlInsertRun::GetIndex(MRawRunHeader *h)
351{
352 Bool_t ok=kTRUE;
353
354 TString query("SELECT fRunDataKEY from RunData WHERE fMagicNumberKEY='");
355
356 query += MagicNumber(h, ok);
357 query += "' AND fFormatVersion='";
358 query += h->GetFormatVersion();
359 query += "' AND fRunTypeKEY='";
360 query += RunType(h, ok);
361 query += "' AND fRunNumber='";
362 query += h->GetRunNumber();
363 query += "' AND fProjectKEY='";
364 query += Project(h, ok);
365 query += "' AND fSourceKEY='";
366 query += Source(h, ok);
367 query += "' AND fNumEvents='";
368 query += h->GetNumEvents();
369 query += "' AND fRunStart='";
370 query += h->GetRunStart().GetSqlDateTime();
371 query += "' AND fRunStop='";
372 query += h->GetRunEnd().GetSqlDateTime();
373 query += "'";
374
375 if (!ok)
376 {
377 *fLog << err << "ERROR - while concatenating query..." << endl;
378 *fLog << query << endl;
379 return -1;
380 }
381
382 TSQLResult *res = fSqlServer->Query(query);
383 if (!res)
384 return (PrintError("TSQLResult==NULL", query), -1);
385
386 if (res->GetFieldCount()!=1)
387 return (PrintError("Number of columns != 1", query), -1);
388
389 if (res->GetRowCount()>1)
390 return (PrintError("Number of rows > 1", query), -1);
391
392 if (res->GetRowCount()==0)
393 return 0;
394
395 const char *fld = res->Next()->GetField(0);
396 if (!fld)
397 return (PrintError("Entry is empty", query), -1);
398
399 return atoi(fld);
400}
401
402Bool_t MSqlInsertRun::InsertFile(MRawRunHeader *h, MParList *pList, Bool_t update)
403{
404 const Int_t key = GetIndex(h);
405 if (key<0)
406 return kFALSE;
407
408 if (key==0)
409 return kTRUE;
410
411 MTaskList *tasks = (MTaskList*)pList->FindObject("MTaskList");
412 if (!tasks)
413 {
414 *fLog << err << "MTaskList not found in parameter list... aborting." << endl;
415 return kFALSE;
416 }
417 MRawFileRead *read = (MRawFileRead*)tasks->FindObject("MRawFileRead");
418 if (!read)
419 {
420 *fLog << err << "MRawFileRead not found in task list... aborting." << endl;
421 return kFALSE;
422 }
423
424 const char *base = gSystem->BaseName(read->GetFileName());
425 const char *path = gSystem->DirName(read->GetFileName());
426
427 const TString str(GetEntry("RawFile", "fRawFileName", Form("fRawFileName='%s'", base)));
428 if (!update && !str.IsNull())
429 {
430 *fLog << err << "ERROR - fRawFileName=" << base << " already existing in RawFile... aborting." << endl;
431 return kFALSE;
432 }
433
434 Bool_t ok = kTRUE;
435
436 TString query(update ? "UPDATE" : "INSERT");
437 query += " RawFile SET fRawFileName='";
438 query += base;
439 query += "', fFilePathKEY=";
440 query += RawFilePath(path, ok);
441 query += ", fRawFileKEY=";
442 query += key;
443
444 if (!ok)
445 {
446 *fLog << err << "ERROR - while concatenating query..." << endl;
447 *fLog << query << endl;
448 return kFALSE;
449 }
450
451 TSQLResult *res = fSqlServer->Query(query);
452 if (!res)
453 return PrintError("TSQLResult==NULL", query);
454
455 *fLog << inf << dec;
456 *fLog << "File '" << base << "' ";
457 *fLog << (update ? "updated" : "inserted");
458 *fLog << " in database successfully." << endl;
459
460 return kTRUE;
461}
462
463// --------------------------------------------------------------------------
464//
465Int_t MSqlInsertRun::PreProcess(MParList *pList)
466{
467 if (!fSqlServer)
468 {
469 *fLog << err << "Connection to SQL server failed... aborting." << endl;
470 return kFALSE;
471 }
472
473 MRawRunHeader *header = (MRawRunHeader*)pList->FindObject("MRawRunHeader");
474 if (!header)
475 {
476 *fLog << err << "MRawRunHeader not found... aborting." << endl;
477 return kFALSE;
478 }
479/*
480 if (header->GetFormatVersion()<3)
481 {
482 *fLog << err << "MSqlInsertRun cannot be used for files with format <3... abort." << endl;
483 return kTRUE;
484 }
485 */
486 Bool_t update = kFALSE;
487
488 // Is run already in databae?
489 const TString str(GetEntry("MRawRunHeader", "fRunNumber", Form("fRunNumber=%d", header->GetRunNumber())));
490 if (!str.IsNull())
491 {
492 if (!fIsUpdate)
493 {
494 *fLog << err << "Run #" << dec << header->GetRunNumber() << " already in database... abort." << endl;
495 return kFALSE;
496 }
497 update=kTRUE;
498 }
499
500 if (!InsertRun(header, update))
501 return kFALSE;
502
503 if (!InsertFile(header, pList, update))
504 return kFALSE;
505
506 return kTRUE;
507}
508
509Int_t MSqlInsertRun::PostProcess()
510{
511 return kTRUE;
512}
Note: See TracBrowser for help on using the repository browser.