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::QueryNameOfKey(TString col, 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%sName FROM %s WHERE f%sKEY=%s",
|
---|
61 | col.Data(), 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 key of f[col]KEY where f[col]Name=[name]
|
---|
78 | //
|
---|
79 | // if value [name] is not existing, insert value (creates anew key)
|
---|
80 | // and return the new key
|
---|
81 | //
|
---|
82 | // return -1 if the query failed or the KEY was not found (insert=kFALSE)
|
---|
83 | // return 0 if the KEY could not be determined after inserting
|
---|
84 | // return the KEY in case of success
|
---|
85 | //
|
---|
86 | Int_t MSQLMagic::QueryKeyOfName(const char *col, const char *name, Bool_t insert)
|
---|
87 | {
|
---|
88 | const TString query1 = Form("SELECT f%sKEY FROM %s WHERE f%sName='%s'",
|
---|
89 | col, col, col, name);
|
---|
90 |
|
---|
91 | TSQLResult *res1 = Query(query1);
|
---|
92 | if (!res1)
|
---|
93 | {
|
---|
94 | cout << "ERROR - Query has failed: " << query1 << endl;
|
---|
95 | return -1;
|
---|
96 | }
|
---|
97 |
|
---|
98 | TSQLRow *row=res1->Next();
|
---|
99 |
|
---|
100 | const Int_t rc1 = row && (*row)[0] ? atoi((*row)[0]) : -1;
|
---|
101 |
|
---|
102 | delete res1;
|
---|
103 |
|
---|
104 | if (rc1>=0)
|
---|
105 | return rc1;
|
---|
106 |
|
---|
107 | if (!insert)
|
---|
108 | return -1;
|
---|
109 |
|
---|
110 | //insert new value
|
---|
111 | const Int_t rc2 = Insert(col, Form("f%sName=\"%s\"", col, name));
|
---|
112 | if (rc2<0) // Dummy mode
|
---|
113 | return 0;
|
---|
114 | if (rc2==kFALSE) // Query failed
|
---|
115 | return -1;
|
---|
116 |
|
---|
117 | const Int_t key = QueryKeyOfName(col, name, kFALSE);
|
---|
118 | if (key>0)
|
---|
119 | {
|
---|
120 | cout << " - New " << col << ": " << name << endl;
|
---|
121 | return key;
|
---|
122 | }
|
---|
123 |
|
---|
124 | return 0;
|
---|
125 | }
|
---|
126 |
|
---|
127 | Bool_t MSQLMagic::ExistStr(const char *column, const char *table, const char *test)
|
---|
128 | {
|
---|
129 | TString query(Form("SELECT %s FROM %s WHERE %s='%s'", column, table, column, test));
|
---|
130 | TSQLResult *res = Query(query);
|
---|
131 | if (!res)
|
---|
132 | return kFALSE;
|
---|
133 |
|
---|
134 | Bool_t rc = kFALSE;
|
---|
135 |
|
---|
136 | TSQLRow *row=res->Next();
|
---|
137 | if (row && (*row)[0])
|
---|
138 | rc=kTRUE;
|
---|
139 |
|
---|
140 | delete res;
|
---|
141 | return rc;
|
---|
142 | }
|
---|
143 |
|
---|
144 | // --------------------------------------------------------------------------
|
---|
145 | //
|
---|
146 | // An abbreviation for an Insert-Query.
|
---|
147 | //
|
---|
148 | // It builds "INSERT table SET vars"
|
---|
149 | // The whitespaces are already conatined.
|
---|
150 | //
|
---|
151 | // On success kTRUE is returned, kFALSE otherwise.
|
---|
152 | // In Dummy mode no query is send an -1 is returned.
|
---|
153 | //
|
---|
154 | Int_t MSQLMagic::Insert(const char *table, const char *vars)
|
---|
155 | {
|
---|
156 | // Build query
|
---|
157 | TString query("INSERT ");
|
---|
158 | query += table;
|
---|
159 | query += " SET ";
|
---|
160 | query += vars;
|
---|
161 |
|
---|
162 | // Check for dummy mode
|
---|
163 | if (fIsDummy)
|
---|
164 | {
|
---|
165 | cout << "MSQLMagic - DUMMY: " << query << endl;
|
---|
166 | return -1;
|
---|
167 | }
|
---|
168 |
|
---|
169 | // Execute query
|
---|
170 | if (Exec(query))
|
---|
171 | return kTRUE;
|
---|
172 |
|
---|
173 | // Return error on failure
|
---|
174 | cout << "Error - Insert failed: " << query << endl;
|
---|
175 | return kFALSE;
|
---|
176 | }
|
---|
177 |
|
---|
178 | // --------------------------------------------------------------------------
|
---|
179 | //
|
---|
180 | // An abbreviation for an Update-Query.
|
---|
181 | //
|
---|
182 | // It builds "UPDATE table SET vars WHERE where"
|
---|
183 | // The whitespaces are already conatined.
|
---|
184 | //
|
---|
185 | // On success kTRUE is returned, kFALSE otherwise.
|
---|
186 | // In Dummy mode no query is send an -1 is returned.
|
---|
187 | //
|
---|
188 | Int_t MSQLMagic::Update(const char *table, const char *vars, const char *where)
|
---|
189 | {
|
---|
190 | // Build query
|
---|
191 | TString query("UPDATE ");
|
---|
192 | query += table;
|
---|
193 | query += " SET ";
|
---|
194 | query += vars;
|
---|
195 | query += " WHERE ";
|
---|
196 | query += where;
|
---|
197 |
|
---|
198 | // Check for dummy mode
|
---|
199 | if (fIsDummy)
|
---|
200 | {
|
---|
201 | cout << "MSQLMagic - DUMMY: " << query << endl;
|
---|
202 | return -1;
|
---|
203 | }
|
---|
204 |
|
---|
205 | // Execute Query
|
---|
206 | if (Exec(query))
|
---|
207 | return kTRUE;
|
---|
208 |
|
---|
209 | // return kFALSE on failure
|
---|
210 | cout << "Error - Update failed: " << query << endl;
|
---|
211 | return kFALSE;
|
---|
212 | }
|
---|
213 |
|
---|
214 | // --------------------------------------------------------------------------
|
---|
215 | //
|
---|
216 | // An abbreviation for a Dalete-Query.
|
---|
217 | //
|
---|
218 | // It builds "DELETE FROM table WHERE where"
|
---|
219 | // The whitespaces are already conatined.
|
---|
220 | //
|
---|
221 | // On success kTRUE is returned, kFALSE otherwise.
|
---|
222 | // In Dummy mode no query is send an -1 is returned.
|
---|
223 | //
|
---|
224 | Int_t MSQLMagic::Delete(const char *table, const char *where)
|
---|
225 | {
|
---|
226 | // Build query
|
---|
227 | TString query("DELETE FROM ");
|
---|
228 | query += table;
|
---|
229 | query += " WHERE ";
|
---|
230 | query += where;
|
---|
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 kFALSE on failure
|
---|
244 | cout << "Error - Delete failed: " << query << endl;
|
---|
245 | return kFALSE;
|
---|
246 | }
|
---|
247 |
|
---|