source: trunk/MagicSoft/Mars/msql/MSQLMagic.cc@ 8987

Last change on this file since 8987 was 8987, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 8.5 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, const char *where)
220{
221 // Build query
222 TString query("INSERT ");
223 query += table;
224 query += " SET ";
225 query += vars;
226 if (!TString(where).IsNull())
227 {
228 query += ", ";
229 query += where;
230 }
231
232 // Check for dummy mode
233 if (fIsDummy)
234 {
235 cout << "MSQLMagic - DUMMY: " << query << endl;
236 return -1;
237 }
238
239 // Execute query
240 if (Exec(query))
241 return kTRUE;
242
243 // Return error on failure
244 cout << "Error - Insert failed: " << query << endl;
245 return kFALSE;
246}
247
248// --------------------------------------------------------------------------
249//
250// An abbreviation for an Update-Query.
251//
252// It builds "UPDATE table SET vars WHERE where"
253// The whitespaces are already conatined.
254//
255// On success kTRUE is returned, kFALSE otherwise.
256// In Dummy mode no query is send an -1 is returned.
257//
258Int_t MSQLMagic::Update(const char *table, const char *vars, const char *where)
259{
260 // Build query
261 TString query("UPDATE ");
262 query += table;
263 query += " SET ";
264 query += vars;
265 query += " WHERE ";
266 query += where;
267
268 // Check for dummy mode
269 if (fIsDummy)
270 {
271 cout << "MSQLMagic - DUMMY: " << query << endl;
272 return -1;
273 }
274
275 // Execute Query
276 if (Exec(query))
277 return kTRUE;
278
279 // return kFALSE on failure
280 cout << "Error - Update failed: " << query << endl;
281 return kFALSE;
282}
283
284// --------------------------------------------------------------------------
285//
286// An abbreviation for checking the existance with ExistStr and
287// calling insert or update respectively.
288//
289Int_t MSQLMagic::InsertUpdate(const char *table, const char *col, const char *val, const char *vars)
290{
291 return ExistStr(col, table, val) ?
292 Update(table, vars, Form("%s='%s'", col, val)) :
293 Insert(table, vars, Form("%s='%s'", col, val));
294}
295
296// --------------------------------------------------------------------------
297//
298// An abbreviation for a Dalete-Query.
299//
300// It builds "DELETE FROM table WHERE where"
301// The whitespaces are already conatined.
302//
303// On success kTRUE is returned, kFALSE otherwise.
304// In Dummy mode no query is send an -1 is returned.
305//
306Int_t MSQLMagic::Delete(const char *table, const char *where)
307{
308 // Build query
309 TString query("DELETE FROM ");
310 query += table;
311 query += " WHERE ";
312 query += where;
313
314 // Check for dummy mode
315 if (fIsDummy)
316 {
317 cout << "MSQLMagic - DUMMY: " << query << endl;
318 return -1;
319 }
320
321 // Execute query
322 if (Exec(query))
323 return kTRUE;
324
325 // return kFALSE on failure
326 cout << "Error - Delete failed: " << query << endl;
327 return kFALSE;
328}
329
Note: See TracBrowser for help on using the repository browser.