HARPO  5.1.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
HarpoRunConfig.cxx
Go to the documentation of this file.
1 //
2 // File HarpoRunConfig.cxx
3 //
41 #include "HarpoRunConfig.h"
42 #include "HarpoDebug.h"
43 #include <TSQLServer.h>
44 #include <TSQLResult.h>
45 #include <TSQLRow.h>
46 #include <TSystem.h>
47 #include <strings.h>
48 #include <stdlib.h>
49 #include <iostream>
50 
52 
53 
54 ClassImp(dbField)
55 
56 ClassImp(HarpoRunConfig)
57 
58 
59 Bool_t HarpoRunConfig::GetRunConfig(Long_t nrun, const TString *DbUrl)
60 {
61  const char *tables[] = {
62  "runconfig",
63  "NSBeam",
64  NULL
65  };
66 
67  const char *dburl_default = "mysql://llrharpo.in2p3.fr/harpo";
68  if(gHarpoDebug>0)
69  printf("GetRunConfig %ld\n",nrun);
70 
71  TSQLServer *db = NULL;
72  if ( DbUrl == NULL ) {
73  const char *dburl = gSystem->Getenv("HARPO_DB_URL");
74  if ( dburl == NULL || strlen(dburl) == 0 )
75  dburl = dburl_default;
76 
77  db = TSQLServer::Connect(dburl,"reader", "ro");
78  } else if ( DbUrl->IsNull() ) {
79  std::cout << " NO database" << std::endl;
80  return false;
81  } else {
82  db = TSQLServer::Connect(DbUrl->Data(),"reader", "ro");
83  }
84  if ( db == NULL ) {
85  std::cout << " Error acess a database" << std::endl;
86  return rec_found;
87  }
88  // printf("Server info: %s\n", db->ServerInfo());
89 
90  TSQLRow *row;
91  TSQLResult *res;
92 
93  // // list databases available on server
94  // printf("\nList all databases on server %s\n", db->GetHost());
95  // res = db->GetDataBases();
96  // while ((row = res->Next())) {
97  // printf("%s\n", row->GetField(0));
98  // delete row;
99  // }
100  // delete res;
101 
102  // // list tables in database "test" (the permission tables)
103  // printf("\nList all tables in database \"harpo\" on server %s\n",
104  // db->GetHost());
105  // res = db->GetTables("harpo");
106 
107  // while ((row = res->Next())) {
108  // printf("%s\n", row->GetField(0));
109  // delete row;
110  // }
111  // delete res;
112 
113  // //////
114  // // list columns in table "runcatalog" in database "harpo"
115  // //////
116 
117  // printf("\nList all columns in table \"runcatalog\" in database \"harpo\" on server %s\n",
118  // db->GetHost());
119 
120  int itbl = 0;
121  do {
122  res = db->GetColumns("harpo", tables[itbl]);
123  // printf("No of field in GetColomns %d\n",res->GetFieldCount());
124  while ((row = res->Next())) {
125  // !!!!!hire 6 filed by program crached after 4 !!!!!!!!!!!!
126  // for(int i=0;i<4;i++)
127  // {
128  // std::cout << " f " << i << " v " << row->GetField(i);
129  // }
130  //std::cout << std::endl;
131  if ( fields.find(row->GetField(0)) == fields.end() ) {
132  dbField *pf = new dbField;
133  pf->ftype = row->GetField(1);
134  fields[row->GetField(0)] = *pf;
135  } else {
136  if(gHarpoDebug>0)
137  std::cout << "key " << row->GetField(0) << " exist" << std::endl;
138  }
139  delete row;
140  }
141  delete res;
142  itbl++;
143  } while (tables[itbl] != NULL );
144  // query database and print results
145  char sql[4096];
146  sprintf(sql,"select * from %s t0 left join %s t1 on t0.run = t1.run where t0.run = %ld;",tables[0],tables[1],nrun);
147 
148  if ( db->SelectDataBase("harpo") != 0 ) return rec_found;
149  res = db->Query(sql);
150 
151  int nrows = res->GetRowCount();
152 
153  // printf("\nGot %d rows in result\n", nrows);
154  if (nrows != 1) return rec_found; // Bad query
155 
156  rec_found = true;
157  run = nrun;
158  int nfields = res->GetFieldCount();
159 
160  // std::cout << "N Fields in runconfig " << nfields
161  // << "found "<< rec_found<< std::endl;
162 
163  row = res->Next();
164 
165  for (int j = 0; j < nfields; j++) {
166 
167  if (row->GetFieldLength(j) > 0 )
168  fields[res->GetFieldName(j)].fval = row->GetField(j);
169 
170  // case 0: /* Long_t */ run = atol(row->GetField(j)); break;
171  // case 1: /* char */ /* mysql date */ date = strdup(row->GetField(j)); break;
172  // case 7: /* Double_t */ Temperature = atof(row->GetField(j)); break;
173  }
174  delete row;
175  delete res;
176 
177  //close DB
178  delete db;
179 
180  return rec_found;
181 }
182 
184 {
185  if ( !rec_found ) {
186  printf("No run info or DB read error\n");
187  } else {
188  for (dbFields::iterator it=fields.begin(); it!=fields.end(); ++it) {
189  std::cout << it->first << " ("<< it->second.ftype << ")" << std::endl;
190  }
191  }
192 }
193 
195 {
196  if ( !rec_found ) {
197  printf("No run info or DB read error\n");
198  } else {
199  for (dbFields::iterator it=fields.begin(); it!=fields.end(); ++it) {
200  std::cout << it->first << " = " << it->second.fval << std::endl;
201  }
202  }
203 
204 }
205 
206 //Look if filed exist
207 Bool_t HarpoRunConfig::Lookup(const TString field)
208 {
209  return fields.find(field) != fields.end();
210 }
211 
212 Bool_t HarpoRunConfig::Lookup(const char * field)
213 {
214  return Lookup(TString(field));
215 }
216 
217 Bool_t HarpoRunConfig::GetVal(const TString field, TString &val)
218 {
219  dbFields::iterator it = fields.find(field);
220  if ( it != fields.end() ) {
221  val = it->second.fval;
222  return true;
223  }
224  return false;
225 }
226 
227 Bool_t HarpoRunConfig::GetVal(const char * field, TString &val)
228 {
229  return GetVal(TString(field),val);
230 }
231 
232 Bool_t HarpoRunConfig::GetType(const TString field, TString &type)
233 {
234  dbFields::iterator it = fields.find(field);
235  if ( it != fields.end() ) {
236  type = it->second.ftype;
237  return true;
238  }
239  return false;
240 }
241 
242 Bool_t HarpoRunConfig::GetType(const char * field, TString &type)
243 {
244  return GetType(TString(field),type);
245 }
246 
247 
248 // Update DB record base on class data
250 {
251  //Bool_t res );
252  printf("UpdateRunConfig NO IMPLEMENTED\n");
253  return kFALSE;
254 }
void list()
list all filds found which his types
Get Run Configuration info by run number. The class query MySQL databe use GetRunConfig Method Only t...
TString ftype
void print()
Print values of all founded fields.
static int type
Long64_t gHarpoDebug
Definition: HarpoDebug.cxx:9
Bool_t Lookup(const TString field)
Lookup filed exist.
Bool_t GetVal(const TString field, TString &val)
Return value of existing field in Tstring.
Bool_t GetType(const TString field, TString &type)
Return Type of field in Tstring.
unsigned int res
Definition: Pmm2Status.h:428
Bool_t UpdateRunConfig()