sqltables.C

Go to the documentation of this file.
00001 // This is an example illustrating how the TSQLFile class can be used.
00002 // Histogram, list of TBox and clones array of TBox objects are stored
00003 // to TSQLFile and read back.
00004 // Except for the specific TSQLFile configuration, the TSQLFile functionality
00005 // is absolutely similar to a normal root TFile
00006 // Author: S.Linev
00007 
00008 // example configuration for MySQL 4.1
00009 const char* dbname = "mysql://host.domain/test";
00010 const char* username = "user";
00011 const char* userpass = "pass";
00012 
00013 // example configuration for Oracle 9i
00014 //const char* dbname = "oracle://host.domain/db-test";
00015 //const char* username = "user";
00016 //const char* userpass = "pass";
00017 
00018 
00019 void sqltables() 
00020 {
00021    tables_write();
00022    tables_read();
00023 }
00024 
00025 void tables_write() 
00026 {
00027    // first connect to data base
00028    // "recreate" option delete all your tables !!!! 
00029    TSQLFile* f = new TSQLFile(dbname, "recreate", username, userpass);
00030    if (f->IsZombie()) { delete f; return; }
00031  
00032    // you can change configuration only until first object 
00033    // is writen to TSQLFile
00034    f->SetUseSuffixes(kFALSE);
00035    f->SetArrayLimit(1000);
00036    f->SetUseIndexes(1);
00037 //   f->SetTablesType("ISAM");
00038 //   f->SetUseTransactions(kFALSE);
00039 
00040    // lets first write histogram
00041    TH1I* h1 = new TH1I("histo1","histo title", 1000, -4., 4.);
00042    h1->FillRandom("gaus",10000);
00043    h1->Write("histo");
00044    h1->SetDirectory(0);
00045 
00046    // here we create list of objects and store them as single key
00047    // without kSingleKey all TBox objects will appear as separate keys
00048    TList* arr = new TList;
00049    for(Int_t n=0;n<10;n++) {
00050       TBox* b = new TBox(n*10,n*100,n*20,n*200);  
00051       arr->Add(b, Form("option_%d_option",n));
00052    }
00053    arr->Write("list",TObject::kSingleKey);
00054 
00055    // clones array is also strored as single key
00056    TClonesArray clones("TBox",10);
00057    for(int n=0;n<10;n++) 
00058        new (clones[n]) TBox(n*10,n*100,n*20,n*200);
00059    clones.Write("clones",TObject::kSingleKey);
00060 
00061    // close connection to database
00062    delete f;
00063 }
00064 
00065 
00066 void tables_read() 
00067 {
00068    // now open connection to database for read-only 
00069    TSQLFile* f = new TSQLFile(dbname, "open", username, userpass);
00070    if (f->IsZombie()) { delete f; return; }
00071    
00072    // see list of keys
00073    f->ls();
00074    
00075    // get histogram from DB and draw it
00076    TH1* h1 = (TH1*) f->Get("histo");
00077    if (h1!=0) {
00078        h1->SetDirectory(0);
00079        h1->Draw();
00080    }
00081    
00082    // get TList with other objects
00083    TObject* obj = f->Get("list");
00084    cout << "Printout of TList object" << endl;
00085    if (obj!=0) obj->Print("*");
00086    delete obj;
00087 
00088    // and get TClonesArray
00089    obj = f->Get("clones");
00090    cout << "Printout of TClonesArray object" << endl;
00091    if (obj!=0) obj->Print("*");
00092    delete obj;
00093 
00094    // this is query to select data of hole class from different tables
00095    cout << "================ TBox QUERY ================ " << endl;
00096    cout << f->MakeSelectQuery(TBox::Class()) << endl;
00097    cout << "================ END of TBox QUERY ================ " << endl;
00098 
00099    cout << "================== TH1I QUERY ================ " << endl;
00100    cout << f->MakeSelectQuery(TH1I::Class()) << endl;
00101    cout << "================ END of TH1I QUERY ================ " << endl;
00102    
00103    // close connection to database
00104    delete f;
00105 }

Generated on Tue Jul 5 15:45:10 2011 for ROOT_528-00b_version by  doxygen 1.5.1