source: tags/Mars-V2.1.1/msql/MSQLMagic.cc

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