HARPO  5.1.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
harpodbtest.cxx
Go to the documentation of this file.
1 //
2 // Demo code for harpo database manipulation
3 // Write access to harpo database was required !!!
4 // Can be run on harpo servers ONLY !!!
5 //
6 
7 #include "HarpoDB.h"
8 #include "TString.h"
9 #include <iostream>
10 #include <stdlib.h>
11 #include <unistd.h>
12 
13 
14 int main(int argc, char **argv)
15 {
16  HarpoDB *db;
17  Long_t run = 0;
18  if (argc == 2) {
19  // Lookup for given run
20  run = atol(argv[1]);
21  // char *pw = getpass("Enter Password: ");
22  }
23  db = new HarpoDB;
24  db->SetUser("harpo","harpo");
25  if ( ! db->Open() ) return 1;
26  db->list();
27 
28  // Clenup test table
29  db->ExecQuery("DELETE FROM `runtest`;");
30 
31  if ( run < 0 ) return run;
32 
33  HarpoDBTable *t0 = new HarpoDBTable();
34  HarpoDBTable *t1 = new HarpoDBTable();
35 
36  db->GetTable("runconfig",t0);
37  db->GetTable("runtest",t1);
38 
39  t0->list();
40  t1->list();
41 
42  // copy runs 1675 - 1680
43 
44  HarpoDBRow runinfo;
45  Bool_t ok;
46  for (Long_t i=1675;i<=1680;i++) {
47  ok = t0->GetRunRow(i,runinfo);
48  if (ok) {
49  runinfo.list();
50  t1->InsertRunRow(runinfo);
51  }
52  }
53 
54  t0->DeleteRun(1676);
55 
56  // Insert with some row exist
57  for (Long_t i=1678;i<=1682;i++) {
58  ok = t0->GetRunRow(i,runinfo);
59  if (ok) {
60  runinfo.list();
61  t1->InsertRunRow(runinfo);
62  }
63  }
64 
65 
66  // Narrow query to "intresting" fileds
67  dbFields fields = t0->GetFiledsTypes();
68  for (dbFields::iterator it=fields.begin(); it!=fields.end(); ++it) {
69  if (!( it->first.Contains("run") || it->first.Contains("Duration")
70  || it->first.Contains("TriggerType")) )
71  { // Other, we not need
72  t0->Erase(it->first);
73  }
74  }
75 
76  // 3 field query for runs
77  for (Long_t i=2000;i<=2010;i++) {
78  ok = t0->GetRunRow(i,runinfo);
79  if (ok) {
80  // runinfo.list();
81  t1->InsertRunRow(runinfo);
82  }
83  }
84 
85  // Update Duration to 2 hour
86 
87  Long_t duration = 200;
88  for (Long_t i=2005;i<=2010;i++) {
89  ok = t1->GetRunRow(i,runinfo);
90  if (ok) {
91  runinfo.SetVal("Duration",duration);
92  t1->UpdateRunRow(runinfo);
93  }
94  }
95 
96  delete t1;
97  delete t0;
98  db->Close();
99  delete db;
100  return 0;
101 }
102 
103 
104 
Bool_t UpdateRunRow(HarpoDBRow row, const TString option="")
Definition: HarpoDB.cxx:408
Bool_t GetRunRow(Long_t nrun, HarpoDBRow &row)
Query Table for given run information.
Definition: HarpoDB.cxx:206
static void SetUser(TString u, TString p)
Definition: HarpoDB.cxx:60
void list()
Definition: HarpoDB.cxx:65
Bool_t Open(const TString *DbUrl=NULL)
Definition: HarpoDB.cxx:77
void list()
Definition: HarpoDB.cxx:286
Bool_t GetTable(const TString name, HarpoDBTable *table)
Definition: HarpoDB.h:122
Bool_t InsertRunRow(HarpoDBRow row, const TString option="")
Definition: HarpoDB.cxx:477
int main(int argc, char **argv)
Definition: harpodbtest.cxx:14
Bool_t SetVal(const TString field, TString val)
Definition: HarpoDB.cxx:322
Bool_t DeleteRun(Long_t run)
Delete run from table.
Definition: HarpoDB.cxx:383
dbFields & GetFiledsTypes()
Definition: HarpoDB.h:94
Harpo Database access class. The class query MySQL database. Deal with (single !) Harpo Database and ...
Definition: HarpoDB.h:112
Bool_t ExecQuery(const TString sql, dbResults *list=NULL)
Definition: HarpoDB.cxx:170
void Erase(const TString &field)
Definition: HarpoDB.h:97
void list()
list all fields with values
Definition: HarpoDB.cxx:274
std::map< TString, TString > dbFields
Definition: HarpoDB.h:16
Bool_t Close()
Definition: HarpoDB.cxx:111