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