source: trunk/Mars/msql/MSQLMagic.cc@ 10498

Last change on this file since 10498 was 9195, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 9.7 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz 7/2006 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2006
21!
22!
23\* ======================================================================== */
24
25////////////////////////////////////////////////////////////////////////
26//
27// MSQLMagic
28//
29// This is an enhancement of MSQLServer especially made the feature
30// the interfaction with our database.
31//
32////////////////////////////////////////////////////////////////////////
33#include "MSQLMagic.h"
34
35#include <stdlib.h> // atoi (Ubuntu 8.10)
36
37#include <iostream>
38
39#include <TSQLRow.h>
40#include <TSQLResult.h>
41
42ClassImp(MSQLMagic);
43
44using namespace std;
45
46// --------------------------------------------------------------------------
47//
48// Return the name corresponding to a key. If col starts with f or
49// end with KEY it is stripped.
50//
51// If the query fails an empty string is returned.
52//
53// On success the name of the key is returned.
54//
55TString MSQLMagic::QueryValOf(TString col, const char *ext, const char *key)
56{
57 if (col.EndsWith("KEY"))
58 col.Remove(col.Length()-3);
59 if (col.BeginsWith("f"))
60 col.Remove(0, 1);
61
62 const TString query=Form("SELECT f%s%s FROM %s WHERE f%sKEY=%s",
63 col.Data(), ext, col.Data(), col.Data(), key);
64
65 TSQLResult *res = Query(query);
66 if (!res)
67 return "";
68
69 TSQLRow *row=res->Next();
70
71 const TString rc = row ? (*row)[0] : "";
72
73 if (row)
74 delete row;
75
76 delete res;
77 return rc;
78}
79
80// --------------------------------------------------------------------------
81//
82// Return the name corresponding to a key. If col starts with f or
83// end with KEY it is stripped.
84//
85// If the query fails an empty string is returned.
86//
87// On success the name of the key is returned.
88//
89TString MSQLMagic::QueryNameOfKey(TString col, const char *key)
90{
91 return QueryValOf(col, "Name", key);
92}
93
94// --------------------------------------------------------------------------
95//
96// Return the value corresponding to a key. If col starts with f or
97// end with KEY it is stripped.
98//
99// If the query fails an empty string is returned.
100//
101// On success the value of the key is returned.
102//
103TString MSQLMagic::QueryValOfKey(TString col, const char *key)
104{
105 return QueryValOf(col, "", key);
106}
107
108// --------------------------------------------------------------------------
109//
110// return the key of f[col]KEY where f[col][ext]=[val]
111//
112// return -1 if the query failed or the KEY was not found
113// return 0 if the KEY could not be determined after inserting
114// return the KEY in case of success
115//
116Int_t MSQLMagic::QueryKeyOf(const char *col, const char *ext, const char *val)
117{
118 const TString query1 = Form("SELECT f%sKEY FROM %s WHERE f%s%s='%s'",
119 col, col, col, ext, val);
120
121 TSQLResult *res1 = Query(query1);
122 if (!res1)
123 {
124 cout << "ERROR - Query has failed: " << query1 << endl;
125 return -1;
126 }
127
128 TSQLRow *row=res1->Next();
129
130 const Int_t rc1 = row && (*row)[0] ? atoi((*row)[0]) : -1;
131
132 if (row)
133 delete row;
134
135 delete res1;
136
137 return rc1;
138}
139
140// --------------------------------------------------------------------------
141//
142// return the key of f[col]KEY where f[col]=[val]
143//
144// return -1 if the query failed or the KEY was not found
145// return 0 if the KEY could not be determined after inserting
146// return the KEY in case of success
147//
148Int_t MSQLMagic::QueryKeyOfVal(const char *col, const char *val)
149{
150 return QueryKeyOf(col, "", val);
151}
152
153// --------------------------------------------------------------------------
154//
155// return the key of f[col]KEY where f[col]Name=[name]
156//
157// if value [name] is not existing, insert value (creates anew key)
158// and return the new key
159//
160// return -1 if the query failed or the KEY was not found (insert=kFALSE)
161// return 0 if the KEY could not be determined after inserting
162// return the KEY in case of success
163//
164Int_t MSQLMagic::QueryKeyOfName(const char *col, const char *name, Bool_t insert)
165{
166 const Int_t rc1 = QueryKeyOf(col, "Name", name);
167
168 if (rc1>=0)
169 return rc1;
170
171 if (!insert)
172 return -1;
173
174 //insert new value
175 const Int_t rc2 = Insert(col, Form("f%sName=\"%s\"", col, name));
176 if (rc2<0) // Dummy mode
177 return 0;
178 if (rc2==kFALSE) // Query failed
179 return -1;
180
181 const Int_t key = QueryKeyOfName(col, name, kFALSE);
182 if (key>0)
183 {
184 cout << " - New " << col << ": " << name << endl;
185 return key;
186 }
187
188 return 0;
189}
190
191// --------------------------------------------------------------------------
192//
193// Check if the column of an entry in a table is existing and not null.
194// The entry is defined by column=test (column name) and a optional
195// WHERE statement.
196//
197Bool_t MSQLMagic::ExistStr(const char *column, const char *table, const char *test, const char *where)
198{
199 TString query = test ?
200 Form("SELECT %s FROM %s WHERE %s='%s' %s %s", column, table, column, test, where?"AND":"", where?where:"") :
201 Form("SELECT %s FROM %s WHERE %s", column, table, where);
202
203 TSQLResult *res = Query(query);
204 if (!res)
205 return kFALSE;
206
207 Bool_t rc = kFALSE;
208
209 TSQLRow *row=res->Next();
210 if (row && (*row)[0])
211 rc=kTRUE;
212
213 if (row)
214 delete row;
215
216 delete res;
217 return rc;
218}
219
220// --------------------------------------------------------------------------
221//
222// Check if at least one row with one field exists in table
223// defined by where
224//
225Bool_t MSQLMagic::ExistRow(const char *table, const char *where)
226{
227 return ExistStr("*", table, 0, where);
228}
229
230// --------------------------------------------------------------------------
231//
232// An abbreviation for an Insert-Query.
233//
234// It builds "INSERT table SET vars"
235// The whitespaces are already conatined.
236//
237// On success kTRUE is returned, kFALSE otherwise.
238// In Dummy mode no query is send an -1 is returned.
239//
240Int_t MSQLMagic::Insert(const char *table, const char *vars, const char *where)
241{
242 // Build query
243 TString query("INSERT ");
244 query += table;
245 query += " SET ";
246 query += vars;
247 if (!TString(where).IsNull())
248 {
249 query += ", ";
250 query += where;
251 }
252
253 // Check for dummy mode
254 if (fIsDummy)
255 {
256 cout << "MSQLMagic - DUMMY: " << query << endl;
257 return -1;
258 }
259
260 // Execute query
261 if (Exec(query))
262 return kTRUE;
263
264 // Return error on failure
265 cout << "Error - Insert failed: " << query << endl;
266 return kFALSE;
267}
268
269// --------------------------------------------------------------------------
270//
271// An abbreviation for an Update-Query.
272//
273// It builds "UPDATE table SET vars WHERE where"
274// The whitespaces are already conatined.
275//
276// On success kTRUE is returned, kFALSE otherwise.
277// In Dummy mode no query is send an -1 is returned.
278//
279Int_t MSQLMagic::Update(const char *table, const char *vars, const char *where)
280{
281 // Build query
282 TString query("UPDATE ");
283 query += table;
284 query += " SET ";
285 query += vars;
286
287 if (!TString(where).IsNull())
288 {
289 query += " WHERE ";
290 query += where;
291 }
292
293 // Check for dummy mode
294 if (fIsDummy)
295 {
296 cout << "MSQLMagic - DUMMY: " << query << endl;
297 return -1;
298 }
299
300 // Execute Query
301 if (Exec(query))
302 return kTRUE;
303
304 // return kFALSE on failure
305 cout << "Error - Update failed: " << query << endl;
306 return kFALSE;
307}
308
309// --------------------------------------------------------------------------
310//
311// An abbreviation for checking the existance with ExistStr and
312// calling insert or update respectively.
313//
314Int_t MSQLMagic::InsertUpdate(const char *table, const char *col, const char *val, const char *vars)
315{
316 return ExistStr(col, table, val) ?
317 Update(table, vars, Form("%s='%s'", col, val)) :
318 Insert(table, vars, Form("%s='%s'", col, val));
319}
320
321// --------------------------------------------------------------------------
322//
323// An abbreviation for checking whether a row with the condition where
324// exists. If no such row exist Insert vars into table, otherwise update
325// vars in table at row(s) defined by where.
326//
327Int_t MSQLMagic::InsertUpdate(const char *table, const char *vars, const char *where)
328{
329 return ExistRow(table, where) ?
330 Update(table, vars, where) :
331 Insert(table, vars);
332}
333
334// --------------------------------------------------------------------------
335//
336// An abbreviation for a Dalete-Query.
337//
338// It builds "DELETE FROM table WHERE where"
339// The whitespaces are already conatined.
340//
341// On success kTRUE is returned, kFALSE otherwise.
342// In Dummy mode no query is send an -1 is returned.
343//
344Int_t MSQLMagic::Delete(const char *table, const char *where)
345{
346 // Build query
347 TString query("DELETE FROM ");
348 query += table;
349 query += " WHERE ";
350 query += where;
351
352 // Check for dummy mode
353 if (fIsDummy)
354 {
355 cout << "MSQLMagic - DUMMY: " << query << endl;
356 return -1;
357 }
358
359 // Execute query
360 if (Exec(query))
361 return kTRUE;
362
363 // return kFALSE on failure
364 cout << "Error - Delete failed: " << query << endl;
365 return kFALSE;
366}
367
Note: See TracBrowser for help on using the repository browser.