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

Last change on this file since 8553 was 8061, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 7.9 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 delete res;
72 return rc;
73}
74
75// --------------------------------------------------------------------------
76//
77// Return the name corresponding to a key. If col starts with f or
78// end with KEY it is stripped.
79//
80// If the query fails an empty string is returned.
81//
82// On success the name of the key is returned.
83//
84TString MSQLMagic::QueryNameOfKey(TString col, const char *key)
85{
86 return QueryValOf(col, "Name", key);
87}
88
89// --------------------------------------------------------------------------
90//
91// Return the value corresponding to a key. If col starts with f or
92// end with KEY it is stripped.
93//
94// If the query fails an empty string is returned.
95//
96// On success the value of the key is returned.
97//
98TString MSQLMagic::QueryValOfKey(TString col, const char *key)
99{
100 return QueryValOf(col, "", key);
101}
102
103// --------------------------------------------------------------------------
104//
105// return the key of f[col]KEY where f[col][ext]=[val]
106//
107// return -1 if the query failed or the KEY was not found
108// return 0 if the KEY could not be determined after inserting
109// return the KEY in case of success
110//
111Int_t MSQLMagic::QueryKeyOf(const char *col, const char *ext, const char *val)
112{
113 const TString query1 = Form("SELECT f%sKEY FROM %s WHERE f%s%s='%s'",
114 col, col, col, ext, val);
115
116 TSQLResult *res1 = Query(query1);
117 if (!res1)
118 {
119 cout << "ERROR - Query has failed: " << query1 << endl;
120 return -1;
121 }
122
123 TSQLRow *row=res1->Next();
124
125 const Int_t rc1 = row && (*row)[0] ? atoi((*row)[0]) : -1;
126 delete res1;
127 return rc1;
128}
129
130// --------------------------------------------------------------------------
131//
132// return the key of f[col]KEY where f[col]=[val]
133//
134// return -1 if the query failed or the KEY was not found
135// return 0 if the KEY could not be determined after inserting
136// return the KEY in case of success
137//
138Int_t MSQLMagic::QueryKeyOfVal(const char *col, const char *val)
139{
140 return QueryKeyOf(col, "", val);
141}
142
143// --------------------------------------------------------------------------
144//
145// return the key of f[col]KEY where f[col]Name=[name]
146//
147// if value [name] is not existing, insert value (creates anew key)
148// and return the new key
149//
150// return -1 if the query failed or the KEY was not found (insert=kFALSE)
151// return 0 if the KEY could not be determined after inserting
152// return the KEY in case of success
153//
154Int_t MSQLMagic::QueryKeyOfName(const char *col, const char *name, Bool_t insert)
155{
156 const Int_t rc1 = QueryKeyOf(col, "Name", name);
157
158 if (rc1>=0)
159 return rc1;
160
161 if (!insert)
162 return -1;
163
164 //insert new value
165 const Int_t rc2 = Insert(col, Form("f%sName=\"%s\"", col, name));
166 if (rc2<0) // Dummy mode
167 return 0;
168 if (rc2==kFALSE) // Query failed
169 return -1;
170
171 const Int_t key = QueryKeyOfName(col, name, kFALSE);
172 if (key>0)
173 {
174 cout << " - New " << col << ": " << name << endl;
175 return key;
176 }
177
178 return 0;
179}
180
181Bool_t MSQLMagic::ExistStr(const char *column, const char *table, const char *test)
182{
183 TString query(Form("SELECT %s FROM %s WHERE %s='%s'", column, table, column, test));
184 TSQLResult *res = Query(query);
185 if (!res)
186 return kFALSE;
187
188 Bool_t rc = kFALSE;
189
190 TSQLRow *row=res->Next();
191 if (row && (*row)[0])
192 rc=kTRUE;
193
194 delete res;
195 return rc;
196}
197
198// --------------------------------------------------------------------------
199//
200// An abbreviation for an Insert-Query.
201//
202// It builds "INSERT table SET vars"
203// The whitespaces are already conatined.
204//
205// On success kTRUE is returned, kFALSE otherwise.
206// In Dummy mode no query is send an -1 is returned.
207//
208Int_t MSQLMagic::Insert(const char *table, const char *vars)
209{
210 // Build query
211 TString query("INSERT ");
212 query += table;
213 query += " SET ";
214 query += vars;
215
216 // Check for dummy mode
217 if (fIsDummy)
218 {
219 cout << "MSQLMagic - DUMMY: " << query << endl;
220 return -1;
221 }
222
223 // Execute query
224 if (Exec(query))
225 return kTRUE;
226
227 // Return error on failure
228 cout << "Error - Insert failed: " << query << endl;
229 return kFALSE;
230}
231
232// --------------------------------------------------------------------------
233//
234// An abbreviation for an Update-Query.
235//
236// It builds "UPDATE table SET vars WHERE where"
237// The whitespaces are already conatined.
238//
239// On success kTRUE is returned, kFALSE otherwise.
240// In Dummy mode no query is send an -1 is returned.
241//
242Int_t MSQLMagic::Update(const char *table, const char *vars, const char *where)
243{
244 // Build query
245 TString query("UPDATE ");
246 query += table;
247 query += " SET ";
248 query += vars;
249 query += " WHERE ";
250 query += where;
251
252 // Check for dummy mode
253 if (fIsDummy)
254 {
255 cout << "MSQLMagic - DUMMY: " << query << endl;
256 return -1;
257 }
258
259 // Execute Query
260 if (Exec(query))
261 return kTRUE;
262
263 // return kFALSE on failure
264 cout << "Error - Update failed: " << query << endl;
265 return kFALSE;
266}
267
268// --------------------------------------------------------------------------
269//
270// An abbreviation for a Dalete-Query.
271//
272// It builds "DELETE FROM table WHERE where"
273// The whitespaces are already conatined.
274//
275// On success kTRUE is returned, kFALSE otherwise.
276// In Dummy mode no query is send an -1 is returned.
277//
278Int_t MSQLMagic::Delete(const char *table, const char *where)
279{
280 // Build query
281 TString query("DELETE FROM ");
282 query += table;
283 query += " WHERE ";
284 query += where;
285
286 // Check for dummy mode
287 if (fIsDummy)
288 {
289 cout << "MSQLMagic - DUMMY: " << query << endl;
290 return -1;
291 }
292
293 // Execute query
294 if (Exec(query))
295 return kTRUE;
296
297 // return kFALSE on failure
298 cout << "Error - Delete failed: " << query << endl;
299 return kFALSE;
300}
301
Note: See TracBrowser for help on using the repository browser.