source: trunk/MagicSoft/Mars/msql/MSQLServer.cc@ 4450

Last change on this file since 4450 was 3415, checked in by tonello, 21 years ago
*** empty log message ***
File size: 12.4 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// MSQLServer
28//
29// Using this instead of a TSQLServer gives the possibility to
30// browse a database server through the TBrowser and it will offer
31// many features usefull working with relational tables.
32//
33////////////////////////////////////////////////////////////////////////
34#include "MSQLServer.h"
35
36#include <iostream>
37#include <iomanip>
38#include <stdlib.h>
39
40#include <TROOT.h>
41
42#include <TH1.h>
43
44#include <TSQLResult.h>
45#include <TSQLServer.h>
46#include <TSQLRow.h>
47
48#include <TBrowser.h>
49
50ClassImp(MSQLServer);
51
52using namespace std;
53
54// --------------------------------------------------------------------------
55//
56// Used in virtual function TObject::Browse() to create the
57//
58void MSQLServer::BrowseColumn(TBrowser *b) /*FOLD00*/
59{
60 const TString query0(Form("EXPLAIN %s.%s %s", (const char*)fDataBase, (const char*)fTable, (const char*)fColumn));
61 const TString query1(Form("SELECT %s FROM %s.%s", (const char*)fColumn, (const char*)fDataBase, (const char*)fTable));
62
63 //cout << query0 << endl;
64 TSQLResult *res = fServ->Query(query0);
65 if (!res)
66 {
67 cout << "query - failed: " << query0 << endl;
68 return;
69 }
70
71 TSQLRow *row=res->Next();
72 const TString desc((*row)[1]);
73
74 //cout << "Column Type: " << desc << endl;
75
76 const Bool_t isnum =
77 !desc.Contains("char", TString::kIgnoreCase) &&
78 !desc.Contains("text", TString::kIgnoreCase);
79
80 cout << query1 << endl;
81 res = fServ->Query(query1);
82 if (!res)
83 {
84 cout << "query - failed: " << query1 << endl;
85 return;
86 }
87
88 TArrayD arr(2);
89 Int_t num=0;
90 Double_t max=0;
91 Double_t min=0;
92 Double_t sum=0;
93 Double_t sqr=0;
94
95 while ((row=res->Next()))
96 {
97 const TString row0((*row)[0]);
98
99 if (!isnum)
100 {
101 cout << row0 << endl;
102 continue;
103 }
104
105 if (num==arr.GetSize())
106 arr.Set(arr.GetSize()*2);
107
108 arr[num] = atof(row0.Data());
109
110 if (num==0)
111 min=max=arr[0];
112
113 if (arr[num]>max) max = arr[num];
114 if (arr[num]<min) min = arr[num];
115
116 sum += arr[num];
117 sqr += arr[num]*arr[num];
118
119 num++;
120 }
121
122 if (!isnum)
123 return;
124
125 if (max==min) max += 1;
126
127 Int_t num0 = 1;
128
129 if (num>0)
130 {
131 /*
132 cout << "Num: " << num << endl;
133 cout << "Mean: " << sum/num << endl;
134 cout << "Range: " << max-min << endl;
135 cout << "RMS: " << TMath::Sqrt(sqr/num-sum*sum/num/num) << endl;
136 */
137
138 num0 = (Int_t)((max-min)*40/TMath::Sqrt(sqr/num-sum*sum/num/num));
139 }
140
141 const TString title(Form("#splitline{%s}{<%s>}", (const char*)query1, (const char*)desc));
142
143 TH1F *hist=new TH1F(fColumn, title, num0, min, max);
144 for (int i=0; i<num; i++)
145 hist->Fill(arr[i]);
146
147 //cout << "Done." << endl;
148
149 hist->Draw();
150 hist->SetBit(kCanDelete);
151}
152
153void MSQLServer::BrowseTable(TBrowser *b) /*FOLD00*/
154{
155 TSQLResult *res = fServ->GetColumns(fDataBase, fTable);
156 if (!res)
157 return;
158
159 TSQLRow *row;
160 while ((row=res->Next()))
161 {
162 TString row0((*row)[0]);
163
164 MSQLServer *sql = (MSQLServer*)fList.FindObject(Form("%s/%s/%s", (const char*)fDataBase, (const char*)fTable, (const char*)row0));
165 if (!sql)
166 {
167 sql = new MSQLColumn(fServ, fDataBase, fTable, row0);
168 fList.Add(sql);
169 }
170 b->Add(sql, row0);
171 }
172}
173
174void MSQLServer::BrowseDataBase(TBrowser *b) /*FOLD00*/
175{
176 TSQLResult *res = fServ->GetTables(fDataBase);
177 if (!res)
178 return;
179
180 TSQLRow *row;
181 while ((row=res->Next()))
182 {
183 TString row0((*row)[0]);
184
185 MSQLServer *sql = (MSQLServer*)fList.FindObject(Form("%s/%s", (const char*)fDataBase, (const char*)row0));
186 if (!sql)
187 {
188 sql = new MSQLServer(fServ, fDataBase, row0);
189 fList.Add(sql);
190 }
191 b->Add(sql, row0);
192 }
193}
194
195void MSQLServer::BrowseServer(TBrowser *b) /*FOLD00*/
196{
197 TSQLResult *res = fServ->GetDataBases();
198 if (!res)
199 return;
200
201 TSQLRow *row;
202 while ((row=res->Next()))
203 {
204 const TString row0((*row)[0]);
205
206 MSQLServer *sql = (MSQLServer*)fList.FindObject(row0);
207 if (!sql)
208 {
209 sql = new MSQLServer(fServ, row0);
210 fList.Add(sql);
211 }
212 b->Add(sql, row0);
213 }
214}
215
216void MSQLServer::PrintLine(const TArrayI &max) const /*FOLD00*/
217{
218 cout << "+" << setfill('-');
219 for (int i=0; i<max.GetSize(); i++)
220 cout << setw(max[i]+1) << "-" << "-+";
221 cout << endl;
222}
223
224void MSQLServer::PrintTable(TSQLResult *res) const /*FOLD00*/
225{
226 Int_t n = res->GetFieldCount();
227
228 TArrayI max(n);
229
230 for (int i=0; i<n; i++)
231 max[i] = strlen(res->GetFieldName(i));
232
233 TSQLRow *row;
234
235 TList rows;
236 while ((row=res->Next()))
237 {
238 for (int i=0; i<n; i++)
239 max[i] = TMath::Max((ULong_t)max[i], row->GetFieldLength(i));
240 rows.Add(row);
241 }
242
243 cout << endl;
244
245 PrintLine(max);
246
247 cout << "|" << setfill(' ');
248 for (int i=0; i<n; i++)
249 cout << setw(max[i]+1) << res->GetFieldName(i) << " |";
250 cout << endl;
251
252 PrintLine(max);
253
254 cout << setfill(' ');
255 TIter Next(&rows);
256 while ((row=(TSQLRow*)Next()))
257 {
258 cout << "|";
259 for (int i=0; i<n; i++)
260 {
261 const char *c = (*row)[i];
262 cout << setw(max[i]+1) << (c?c:"") << " |";
263 }
264 cout << endl;
265 }
266
267 PrintLine(max);
268}
269
270TString MSQLServer::GetFields() const /*FOLD00*/
271{
272 TSQLResult *res = fServ->GetColumns(fDataBase, fTable);
273 if (!res)
274 return "";
275
276 TString fields;
277
278 TSQLRow *row;
279
280 TList rows;
281 while ((row=res->Next()))
282 rows.Add(row);
283
284 TIter Next(&rows);
285 while ((row=(TSQLRow*)Next()))
286 {
287 fields += (*row)[0];
288 if (row!=rows.Last())
289 fields += ", ";
290 }
291
292 return fields;
293}
294
295void MSQLServer::PrintQuery(const char *query) const /*FOLD00*/
296{
297 TSQLResult *res = fServ->Query(query);
298 if (res)
299 PrintTable(res);
300 else
301 cout << "Query failed: " << query << endl;
302}
303
304void MSQLServer::Print(Option_t *o) const /*FOLD00*/
305{
306 switch (fType)
307 {
308 case kIsServer:
309 PrintQuery("SHOW DATABASES");
310 break;
311
312 case kIsDataBase:
313 PrintQuery(Form("SHOW TABLES FROM %s", (const char*)fDataBase));
314 break;
315
316 case kIsTable:
317 PrintQuery(Form("SELECT * FROM %s.%s", (const char*)fDataBase, (const char*)fTable));
318 break;
319
320 case kIsColumn:
321 PrintQuery(Form("SELECT %s FROM %s.%s", (const char*)fColumn, (const char*)fDataBase, (const char*)fTable));
322 break;
323
324 default:
325 break;
326 }
327}
328
329void MSQLServer::ShowColumns() const /*FOLD00*/
330{
331 switch (fType)
332 {
333 case kIsTable:
334 PrintQuery(Form("SHOW FULl COLUMNS FROM %s.%s", (const char*)fDataBase, (const char*)fTable));
335 break;
336
337 case kIsColumn:
338 PrintQuery(Form("SHOW FULl COLUMNS FROM %s.%s LIKE %s", (const char*)fDataBase, (const char*)fTable, (const char*)fColumn));
339 break;
340
341 default:
342 //Print();
343 break;
344 }
345}
346
347void MSQLServer::ShowStatus() const /*FOLD00*/
348{
349 switch (fType)
350 {
351 case kIsServer:
352 PrintQuery("SHOW STATUS");
353 break;
354
355 case kIsDataBase:
356 PrintQuery(Form("SHOW TABLE STATUS FROM %s", (const char*)fDataBase));
357 break;
358
359 case kIsTable:
360 PrintQuery(Form("SHOW TABLE STATUS FROM %s LIKE %s", (const char*)fDataBase, (const char*)fTable));
361 break;
362
363 default:
364 break;
365 }
366}
367
368void MSQLServer::ShowTableIndex() const /*FOLD00*/
369{
370 switch (fType)
371 {
372 case kIsTable:
373 case kIsColumn:
374 PrintQuery(Form("SHOW INDEX FROM %s.%s", (const char*)fDataBase, (const char*)fTable));
375 break;
376
377 default:
378 break;
379 }
380}
381
382void MSQLServer::ShowTableCreate() const /*FOLD00*/
383{
384 switch (fType)
385 {
386 case kIsTable:
387 case kIsColumn:
388 PrintQuery(Form("SHOW CREATE TABLE %s.%s", (const char*)fDataBase, (const char*)fTable));
389 break;
390
391 default:
392 break;
393 }
394}
395
396void MSQLServer::Close(Option_t *option) /*FOLD00*/
397{
398 if (fType==kIsServer)
399 fServ->Close(option);
400}
401
402TSQLResult *MSQLServer::Query(const char *sql) /*FOLD00*/
403{
404 return fType==kIsServer ? fServ->Query(sql) : NULL;
405}
406
407Int_t MSQLServer::SelectDataBase(const char *dbname) /*FOLD00*/
408{
409 return fType==kIsServer ? fServ->SelectDataBase(dbname) : 0;
410}
411
412TSQLResult *MSQLServer::GetDataBases(const char *wild) /*FOLD00*/
413{
414 return fType==kIsServer ? fServ->GetDataBases(wild) : NULL;
415}
416
417TSQLResult *MSQLServer::GetTables(const char *dbname, const char *wild) /*FOLD00*/
418{
419 return fType==kIsServer ? fServ->GetTables(dbname, wild) : NULL;
420}
421
422TSQLResult *MSQLServer::GetColumns(const char *dbname, const char *table, const char *wild) /*FOLD00*/
423{
424 return fType==kIsServer ? fServ->GetColumns(dbname, table, wild) : NULL;
425}
426
427Int_t MSQLServer::CreateDataBase(const char *dbname) /*FOLD00*/
428{
429 return fType==kIsServer ? fServ->CreateDataBase(dbname) : 0;
430}
431
432Int_t MSQLServer::DropDataBase(const char *dbname) /*FOLD00*/
433{
434 return fType==kIsServer ? fServ->DropDataBase(dbname) : 0;
435}
436
437Int_t MSQLServer::Reload() /*FOLD00*/
438{
439 return fType==kIsServer ? fServ->Reload() : 0;
440}
441
442Int_t MSQLServer::Shutdown() /*FOLD00*/
443{
444 return fType==kIsServer ? fServ->Shutdown() : 0;
445}
446
447const char *MSQLServer::ServerInfo() /*FOLD00*/
448{
449 return fType==kIsServer ? fServ->ServerInfo() : "";
450}
451
452void MSQLServer::Init(const char *connection, const char *user, const char *password) /*FOLD00*/
453{
454 fServ = TSQLServer::Connect(connection, user, password);
455 if (fServ)
456 {
457 gROOT->GetListOfBrowsables()->Add(this, connection);
458 fType = kIsServer;
459 }
460 else
461 fType = kIsZombie;
462
463 fList.SetOwner();
464}
465
466MSQLServer::MSQLServer(const char *connection, const char *user, const char *password) /*FOLD00*/
467{
468 Init(connection, user, password);
469}
470
471MSQLServer::MSQLServer(const char *u) /*FOLD00*/
472{
473 TString url(u);
474
475 const Ssiz_t pos1 = url.First("://")+3;
476 const Ssiz_t pos2 = url.Last(':') +1;
477 const Ssiz_t pos3 = url.First('@');
478
479 if (pos1<0 || pos2<0 || pos3<0 || pos1>pos2 || pos2>pos3)
480 return;
481
482 const TString user = url(pos1, pos2-pos1-1);
483 const TString pasw = url(pos2, pos3-pos2);
484
485 url.Remove(pos1, pos3+1-pos1);
486
487 Init(url, user, pasw);
488
489}
490
491MSQLServer::~MSQLServer() /*FOLD00*/
492{
493 Close();
494 cout << "Delete: " << GetName() << endl;
495}
496
497Bool_t MSQLServer::PrintError(const char *txt, const char *q) const /*FOLD00*/
498{
499 cout << "Fatal error acessing database: " << txt << endl;
500 cout << "Query: " << q << endl;
501 return kFALSE;
502}
503
504TString MSQLServer::GetEntry(const char *where, const char *col, const char *table) const /*FOLD00*/
505{
506 if (!fServ)
507 return "";
508
509 if (table==0)
510 table = Form("%s.%s", (const char *)fDataBase, (const char*)fTable);
511 if (col==0)
512 col = (const char *)fColumn;
513
514 const TString query(Form("SELECT %s FROM %s WHERE %s", col, table, where));
515
516 TSQLResult *res = fServ->Query(query);
517 if (!res)
518 return (PrintError("GetEntry - TSQLResult==NULL", query), "");
519
520 if (res->GetFieldCount()!=1)
521 return (PrintError("GetEntry - Number of columns != 1", query), "");
522
523 if (res->GetRowCount()>1)
524 return (PrintError("GetEntry - Number of rows > 1", query), "");
525
526 if (res->GetRowCount()==0)
527 return "";
528
529 const char *fld = res->Next()->GetField(0);
530 if (!fld)
531 return (PrintError("GetEntry - Entry is empty", query), "");
532
533 return TString(fld);
534}
Note: See TracBrowser for help on using the repository browser.