ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
sqltables.C
Go to the documentation of this file.
1 // This is an example illustrating how the TSQLFile class can be used.
2 // Histogram, list of TBox and clones array of TBox objects are stored
3 // to TSQLFile and read back.
4 // Except for the specific TSQLFile configuration, the TSQLFile functionality
5 // is absolutely similar to a normal root TFile
6 // Author: S.Linev
7 
8 // example configuration for MySQL 4.1
9 const char* dbname = "mysql://host.domain/test";
10 const char* username = "user";
11 const char* userpass = "pass";
12 
13 // example configuration for Oracle 9i
14 //const char* dbname = "oracle://host.domain/db-test";
15 //const char* username = "user";
16 //const char* userpass = "pass";
17 
18 
19 void sqltables()
20 {
21  tables_write();
22  tables_read();
23 }
24 
26 {
27  // first connect to data base
28  // "recreate" option delete all your tables !!!!
29  TSQLFile* f = new TSQLFile(dbname, "recreate", username, userpass);
30  if (f->IsZombie()) { delete f; return; }
31 
32  // you can change configuration only until first object
33  // is writen to TSQLFile
35  f->SetArrayLimit(1000);
36  f->SetUseIndexes(1);
37 // f->SetTablesType("ISAM");
38 // f->SetUseTransactions(kFALSE);
39 
40  // lets first write histogram
41  TH1I* h1 = new TH1I("histo1","histo title", 1000, -4., 4.);
42  h1->FillRandom("gaus",10000);
43  h1->Write("histo");
44  h1->SetDirectory(0);
45 
46  // here we create list of objects and store them as single key
47  // without kSingleKey all TBox objects will appear as separate keys
48  TList* arr = new TList;
49  for(Int_t n=0;n<10;n++) {
50  TBox* b = new TBox(n*10,n*100,n*20,n*200);
51  arr->Add(b, Form("option_%d_option",n));
52  }
53  arr->Write("list",TObject::kSingleKey);
54 
55  // clones array is also strored as single key
56  TClonesArray clones("TBox",10);
57  for(int n=0;n<10;n++)
58  new (clones[n]) TBox(n*10,n*100,n*20,n*200);
59  clones.Write("clones",TObject::kSingleKey);
60 
61  // close connection to database
62  delete f;
63 }
64 
65 
67 {
68  // now open connection to database for read-only
69  TSQLFile* f = new TSQLFile(dbname, "open", username, userpass);
70  if (f->IsZombie()) { delete f; return; }
71 
72  // see list of keys
73  f->ls();
74 
75  // get histogram from DB and draw it
76  TH1* h1 = (TH1*) f->Get("histo");
77  if (h1!=0) {
78  h1->SetDirectory(0);
79  h1->Draw();
80  }
81 
82  // get TList with other objects
83  TObject* obj = f->Get("list");
84  cout << "Printout of TList object" << endl;
85  if (obj!=0) obj->Print("*");
86  delete obj;
87 
88  // and get TClonesArray
89  obj = f->Get("clones");
90  cout << "Printout of TClonesArray object" << endl;
91  if (obj!=0) obj->Print("*");
92  delete obj;
93 
94  // this is query to select data of hole class from different tables
95  cout << "================ TBox QUERY ================ " << endl;
96  cout << f->MakeSelectQuery(TBox::Class()) << endl;
97  cout << "================ END of TBox QUERY ================ " << endl;
98 
99  cout << "================== TH1I QUERY ================ " << endl;
100  cout << f->MakeSelectQuery(TH1I::Class()) << endl;
101  cout << "================ END of TH1I QUERY ================ " << endl;
102 
103  // close connection to database
104  delete f;
105 }
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TObject.cxx:823
const char * dbname
Definition: sqltables.C:9
virtual void ls(Option_t *option="") const
List file contents.
Definition: TFile.cxx:1361
virtual void SetDirectory(TDirectory *dir)
By default when an histogram is created, it is added to the list of histogram objects in the current ...
Definition: TH1.cxx:8266
Create a Box.
Definition: TBox.h:44
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
Bool_t IsZombie() const
Definition: TObject.h:141
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: Rtypes.h:92
void sqltables()
Definition: sqltables.C:19
TFile * f
void Class()
Definition: Class.C:29
TH1F * h1
Definition: legend1.C:5
virtual void Print(Option_t *option="") const
This method must be overridden when a class wants to print itself.
Definition: TObject.cxx:594
A doubly linked list.
Definition: TList.h:47
virtual void FillRandom(const char *fname, Int_t ntimes=5000)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3330
Access an SQL db via the TFile interface.
Definition: TSQLFile.h:32
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
void tables_write()
Definition: sqltables.C:25
1-D histogram with a int per channel (see TH1 documentation)}
Definition: TH1.h:529
char * Form(const char *fmt,...)
void SetUseIndexes(Int_t use_type=kIndexesBasic)
Specify usage of indexes for data tables Index Description kIndexesNone = 0 no indexes are used kInd...
Definition: TSQLFile.cxx:641
TString MakeSelectQuery(TClass *cl)
Produce SELECT statement which can be used to get all data of class cl in one SELECT statement...
Definition: TSQLFile.cxx:1244
const char * username
Definition: sqltables.C:10
void SetArrayLimit(Int_t limit=20)
Defines maximum number of columns for array representation If array size bigger than limit...
Definition: TSQLFile.cxx:542
The TH1 histogram class.
Definition: TH1.h:80
Mother of all ROOT objects.
Definition: TObject.h:58
void tables_read()
Definition: sqltables.C:66
An array of clone (identical) objects.
Definition: TClonesArray.h:32
virtual void Add(TObject *obj)
Definition: TList.h:81
const char * userpass
Definition: sqltables.C:11
void SetUseSuffixes(Bool_t on=kTRUE)
enable/disable uasge of suffixes in columns names can be changed before first object is saved into fi...
Definition: TSQLFile.cxx:526
TObject * obj
const Int_t n
Definition: legend1.C:16
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write all objects in this collection.