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

Last change on this file was 8709, checked in by tbretz, 17 years ago
*** empty log message ***
File size: 8.4 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
189Bool_t MSQLMagic::ExistStr(const char *column, const char *table, const char *test)
190{
191 TString query(Form("SELECT %s FROM %s WHERE %s='%s'", column, table, column, test));
192 TSQLResult *res = Query(query);
193 if (!res)
194 return kFALSE;
195
196 Bool_t rc = kFALSE;
197
198 TSQLRow *row=res->Next();
199 if (row && (*row)[0])
200 rc=kTRUE;
201
202 if (row)
203 delete row;
204
205 delete res;
206 return rc;
207}
208
209// --------------------------------------------------------------------------
210//
211// An abbreviation for an Insert-Query.
212//
213// It builds "INSERT table SET vars"
214// The whitespaces are already conatined.
215//
216// On success kTRUE is returned, kFALSE otherwise.
217// In Dummy mode no query is send an -1 is returned.
218//
219Int_t MSQLMagic::Insert(const char *table, const char *vars)
220{
221 // Build query
222 TString query("INSERT ");
223 query += table;
224 query += " SET ";
225 query += vars;
226
227 // Check for dummy mode
228 if (fIsDummy)
229 {
230 cout << "MSQLMagic - DUMMY: " << query << endl;
231 return -1;
232 }
233
234 // Execute query
235 if (Exec(query))
236 return kTRUE;
237
238 // Return error on failure
239 cout << "Error - Insert failed: " << query << endl;
240 return kFALSE;
241}
242
243// --------------------------------------------------------------------------
244//
245// An abbreviation for an Update-Query.
246//
247// It builds "UPDATE table SET vars WHERE where"
248// The whitespaces are already conatined.
249//
250// On success kTRUE is returned, kFALSE otherwise.
251// In Dummy mode no query is send an -1 is returned.
252//
253Int_t MSQLMagic::Update(const char *table, const char *vars, const char *where)
254{
255 // Build query
256 TString query("UPDATE ");
257 query += table;
258 query += " SET ";
259 query += vars;
260 query += " WHERE ";
261 query += where;
262
263 // Check for dummy mode
264 if (fIsDummy)
265 {
266 cout << "MSQLMagic - DUMMY: " << query << endl;
267 return -1;
268 }
269
270 // Execute Query
271 if (Exec(query))
272 return kTRUE;
273
274 // return kFALSE on failure
275 cout << "Error - Update failed: " << query << endl;
276 return kFALSE;
277}
278
279// --------------------------------------------------------------------------
280//
281// An abbreviation for checking the existance with ExistStr and
282// calling insert or update respectively.
283//
284Int_t MSQLMagic::InsertUpdate(const char *table, const char *col, const char *val, const char *vars)
285{
286 return ExistStr(col, table, val) ?
287 Update(table, vars, Form("%s=%s", col, val)) :
288 Insert(table, vars);
289}
290
291// --------------------------------------------------------------------------
292//
293// An abbreviation for a Dalete-Query.
294//
295// It builds "DELETE FROM table WHERE where"
296// The whitespaces are already conatined.
297//
298// On success kTRUE is returned, kFALSE otherwise.
299// In Dummy mode no query is send an -1 is returned.
300//
301Int_t MSQLMagic::Delete(const char *table, const char *where)
302{
303 // Build query
304 TString query("DELETE FROM ");
305 query += table;
306 query += " WHERE ";
307 query += where;
308
309 // Check for dummy mode
310 if (fIsDummy)
311 {
312 cout << "MSQLMagic - DUMMY: " << query << endl;
313 return -1;
314 }
315
316 // Execute query
317 if (Exec(query))
318 return kTRUE;
319
320 // return kFALSE on failure
321 cout << "Error - Delete failed: " << query << endl;
322 return kFALSE;
323}
324
Note: See TracBrowser for help on using the repository browser.