Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
sqltables.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_sql
3 /// This is an example illustrating how the TSQLFile class can be used.
4 /// Histogram, list of TBox and clones array of TBox objects are stored
5 /// to TSQLFile and read back.
6 /// Except for the specific TSQLFile configuration, the TSQLFile functionality
7 /// is absolutely similar to a normal root TFile
8 ///
9 /// \macro_code
10 ///
11 /// \author Sergey Linev
12 
13 // example configuration for MySQL 4.1
14 const char* dbname = "mysql://host.domain/test";
15 const char* username = "user";
16 const char* userpass = "pass";
17 
18 // example configuration for Oracle 9i
19 //const char* dbname = "oracle://host.domain/db-test";
20 //const char* username = "user";
21 //const char* userpass = "pass";
22 
23 
24 void sqltables()
25 {
26  tables_write();
27  tables_read();
28 }
29 
30 void tables_write()
31 {
32  // first connect to data base
33  // "recreate" option delete all your tables !!!!
34  TSQLFile* f = new TSQLFile(dbname, "recreate", username, userpass);
35  if (f->IsZombie()) { delete f; return; }
36 
37  // you can change configuration only until first object
38  // is written to TSQLFile
39  f->SetUseSuffixes(kFALSE);
40  f->SetArrayLimit(1000);
41  f->SetUseIndexes(1);
42 // f->SetTablesType("ISAM");
43 // f->SetUseTransactions(kFALSE);
44 
45  // lets first write histogram
46  TH1I* h1 = new TH1I("histo1","histo title", 1000, -4., 4.);
47  h1->FillRandom("gaus",10000);
48  h1->Write("histo");
49  h1->SetDirectory(0);
50 
51  // here we create list of objects and store them as single key
52  // without kSingleKey all TBox objects will appear as separate keys
53  TList* arr = new TList;
54  for(Int_t n=0;n<10;n++) {
55  TBox* b = new TBox(n*10,n*100,n*20,n*200);
56  arr->Add(b, Form("option_%d_option",n));
57  }
58  arr->Write("list",TObject::kSingleKey);
59 
60  // clones array is also stored as single key
61  TClonesArray clones("TBox",10);
62  for(int n=0;n<10;n++)
63  new (clones[n]) TBox(n*10,n*100,n*20,n*200);
64  clones.Write("clones",TObject::kSingleKey);
65 
66  // close connection to database
67  delete f;
68 }
69 
70 
71 void tables_read()
72 {
73  // now open connection to database for read-only
74  TSQLFile* f = new TSQLFile(dbname, "open", username, userpass);
75  if (f->IsZombie()) { delete f; return; }
76 
77  // see list of keys
78  f->ls();
79 
80  // get histogram from DB and draw it
81  TH1* h1 = (TH1*) f->Get("histo");
82  if (h1!=0) {
83  h1->SetDirectory(0);
84  h1->Draw();
85  }
86 
87  // get TList with other objects
88  TObject* obj = f->Get("list");
89  cout << "Printout of TList object" << endl;
90  if (obj!=0) obj->Print("*");
91  delete obj;
92 
93  // and get TClonesArray
94  obj = f->Get("clones");
95  cout << "Printout of TClonesArray object" << endl;
96  if (obj!=0) obj->Print("*");
97  delete obj;
98 
99  // this is query to select data of hole class from different tables
100  cout << "================ TBox QUERY ================ " << endl;
101  cout << f->MakeSelectQuery(TBox::Class()) << endl;
102  cout << "================ END of TBox QUERY ================ " << endl;
103 
104  cout << "================== TH1I QUERY ================ " << endl;
105  cout << f->MakeSelectQuery(TH1I::Class()) << endl;
106  cout << "================ END of TH1I QUERY ================ " << endl;
107 
108  // close connection to database
109  delete f;
110 }