1 | #include <iomanip.h>
|
---|
2 | /*
|
---|
3 | Statements
|
---|
4 |
|
---|
5 | SELECT [ DISTINCT ] * | LIST OF COLUMNS, FUNCTIONS, CONSTANTS
|
---|
6 | FROM LIST OF TABLES OR VIEWS
|
---|
7 | [ WHERE CONDITION(S) ]
|
---|
8 | [ ORDER BY ORDERING COLUMN(S) [ ASC | DESC ] ]
|
---|
9 | [ GROUP BY GROUPING COLUMN(S) ]
|
---|
10 | [ HAVING CONDITION(S) ]
|
---|
11 |
|
---|
12 | DELETE FROM TABLE NAME
|
---|
13 | [ WHERE CONDITION(S) ]
|
---|
14 |
|
---|
15 | INSERT INTO TABLE NAME
|
---|
16 | [ (COLUMN LIST) ]
|
---|
17 | VALUES (VALUE LIST)
|
---|
18 |
|
---|
19 | UPDATE TABLE NAME
|
---|
20 | SET COLUMN NAME = VALUE
|
---|
21 | [ WHERE CONDITION ]
|
---|
22 |
|
---|
23 | Functions
|
---|
24 |
|
---|
25 | Function Purpose
|
---|
26 | SUM Total of the values in a field.
|
---|
27 | AVG Average of the values in a field.
|
---|
28 | MIN Lowest value in a field.
|
---|
29 | MAX Highest value in a field.
|
---|
30 | COUNT Number of values in a field, not counting Null (blank) values.
|
---|
31 |
|
---|
32 | Predicates
|
---|
33 |
|
---|
34 | Predicate Description
|
---|
35 | BETWEEN ... AND Compares a value to a range formed by two values.
|
---|
36 | IN Determines whether a value exists in a list of values or a table.
|
---|
37 | LIKE Compares, in part or in whole, one value with another.
|
---|
38 | JOIN Joins two tables.
|
---|
39 |
|
---|
40 | Data Definition
|
---|
41 |
|
---|
42 | CREATE TABLE TABLE_NAME
|
---|
43 | ( COLUMN_NAME DATA_TYPE [(SIZE)] COLUMN_CONSTRAINT,
|
---|
44 | [, other column definitions,...]
|
---|
45 | [, primary key constraint]
|
---|
46 | )
|
---|
47 |
|
---|
48 | ALTER TABLE TABLE_NAME ADD | DROP | MODIFY
|
---|
49 | ( COLUMN_NAME DATA_TYPE [(SIZE)] COLUMN_CONSTRAINT,
|
---|
50 | [, other column definitions,...]
|
---|
51 | )
|
---|
52 |
|
---|
53 | DROP TABLE TABLE_NAME
|
---|
54 |
|
---|
55 | CREATE [UNIQUE] [ASC | DESC] INDEX INDEX_NAME
|
---|
56 | ON TABLE_NAME ( COLUMN_LIST )
|
---|
57 |
|
---|
58 | DROP INDEX INDEX_NAME ON TABLE_NAME
|
---|
59 |
|
---|
60 | CREATE VIEW VIEW_NAME AS QUERY_NAME
|
---|
61 |
|
---|
62 | CONSTRAINT CONSTRAINT_NAME
|
---|
63 | {PRIMARY KEY | UNIQUE | NOT NULL |
|
---|
64 | REFERENCES FOREIGN_TABLE [(FIELD_LIST)]}
|
---|
65 |
|
---|
66 | */
|
---|
67 |
|
---|
68 | void Line(const TArrayI &max)
|
---|
69 | {
|
---|
70 | cout << "+" << setfill('-');
|
---|
71 | for (int i=0; i<max.GetSize(); i++)
|
---|
72 | cout << setw(max[i]+1) << "-" << "-+";
|
---|
73 | cout << endl;
|
---|
74 | }
|
---|
75 |
|
---|
76 | void Print(TSQLResult *res)
|
---|
77 | {
|
---|
78 | Int_t n = res->GetFieldCount();
|
---|
79 |
|
---|
80 | TArrayI max(n);
|
---|
81 |
|
---|
82 | for (int i=0; i<n; i++)
|
---|
83 | max[i] = strlen(res->GetFieldName(i));
|
---|
84 |
|
---|
85 | TSQLRow *row;
|
---|
86 |
|
---|
87 | TList rows;
|
---|
88 | while (row=res->Next())
|
---|
89 | {
|
---|
90 | for (int i=0; i<n; i++)
|
---|
91 | max[i] = TMath::Max(max[i], row->GetFieldLength(i));
|
---|
92 | rows.Add(row);
|
---|
93 | }
|
---|
94 |
|
---|
95 | Line(max);
|
---|
96 |
|
---|
97 | cout << "|" << setfill(' ');
|
---|
98 | for (int i=0; i<n; i++)
|
---|
99 | cout << setw(max[i]+1) << res->GetFieldName(i) << " |";
|
---|
100 | cout << endl;
|
---|
101 |
|
---|
102 | Line(max);
|
---|
103 |
|
---|
104 | cout << setfill(' ');
|
---|
105 | TIter Next(&rows);
|
---|
106 | while (row=(TSQLRow*)Next())
|
---|
107 | {
|
---|
108 | cout << "|";
|
---|
109 | for (int i=0; i<n; i++)
|
---|
110 | {
|
---|
111 | char *c = (*row)[i];
|
---|
112 | cout << setw(max[i]+1) << (c?c:"") << " |";
|
---|
113 | }
|
---|
114 | cout << endl;
|
---|
115 | }
|
---|
116 |
|
---|
117 | Line(max);
|
---|
118 | }
|
---|
119 |
|
---|
120 | TString GetFields(TSQLServer *serv, const char *db, const char *table)
|
---|
121 | {
|
---|
122 | res = serv->GetColumns(db, table);
|
---|
123 | if (!res)
|
---|
124 | return "";
|
---|
125 |
|
---|
126 | TString fields;
|
---|
127 |
|
---|
128 | TList rows;
|
---|
129 | while (row=res->Next())
|
---|
130 | rows.Add(row);
|
---|
131 |
|
---|
132 | TIter Next(&rows);
|
---|
133 | while (row=(TSQLRow*)Next())
|
---|
134 | {
|
---|
135 | fields += (*row)[0];
|
---|
136 | if (row!=rows.Last())
|
---|
137 | fields += ", ";
|
---|
138 | }
|
---|
139 |
|
---|
140 | return fields;
|
---|
141 | }
|
---|
142 |
|
---|
143 | void PrintContents(TSQLServer *serv, const char *db, const char *table)
|
---|
144 | {
|
---|
145 | TString fields=GetFields(serv, db, table);
|
---|
146 |
|
---|
147 | TSQLResult *res = serv->Query(Form("SELECT %s FROM %s", fields.Data(), table));
|
---|
148 | if (res)
|
---|
149 | {
|
---|
150 | Print(res);
|
---|
151 | delete res;
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | /*
|
---|
156 | GetTables(db):
|
---|
157 | \u "db"
|
---|
158 | SHOW TABLES;
|
---|
159 |
|
---|
160 | GetDataBases();
|
---|
161 | SHOW DATABASES;
|
---|
162 |
|
---|
163 | GetColumns(db, table);
|
---|
164 | EXPLAIN table;
|
---|
165 | DESCRIBE table;
|
---|
166 |
|
---|
167 | SHOW VARIABLES;
|
---|
168 |
|
---|
169 | PRIMARY_KEY impliziert NOT NULL
|
---|
170 |
|
---|
171 | */
|
---|
172 |
|
---|
173 | void CreatePrimaryEntries()
|
---|
174 | {
|
---|
175 | TList list;
|
---|
176 | list.SetOwner();
|
---|
177 |
|
---|
178 | // Trigger tables
|
---|
179 | list.Add(new TObjString(
|
---|
180 | "INSERT MyMagic.L1TriggerTable (fL1TriggerTableKEY, fL1TriggerTableName, fL1TriggerTable) VALUES "
|
---|
181 | " (1, 'n/a', 'Not available')"));
|
---|
182 |
|
---|
183 | list.Add(new TObjString(
|
---|
184 | "INSERT MyMagic.L2TriggerTable (fL2TriggerTableKEY, fL2TriggerTableName, fL2TriggerTable) VALUES "
|
---|
185 | " (1, 'n/a', 'Not available')"));
|
---|
186 |
|
---|
187 | // File path
|
---|
188 | list.Add(new TObjString(
|
---|
189 | "INSERT MyMagic.FilePath (fFilePathName, fFilePath) VALUES "
|
---|
190 | " ('n/a', 'Not available in the Data Center')"));
|
---|
191 |
|
---|
192 | // Magic number
|
---|
193 | list.Add(new TObjString(
|
---|
194 | "INSERT MyMagic.MagicNumber (fMagicNumber, fMagicNumberName) VALUES "
|
---|
195 | " (0x0000, 'Not available'),"
|
---|
196 | " (0xc0c0, 'Ok'),"
|
---|
197 | " (0xc0c1, 'Not closed'),"
|
---|
198 | " (0xffff, 'Wrong')"));
|
---|
199 |
|
---|
200 | // Run type
|
---|
201 | list.Add(new TObjString(
|
---|
202 | "INSERT MyMagic.RunType (fRunType, fRunTypeName) VALUES "
|
---|
203 | " (0xffff, 'Not available'),"
|
---|
204 | " (0x0000, 'Data'),"
|
---|
205 | " (0x0001, 'Pedestal'),"
|
---|
206 | " (0x0002, 'Calibration'),"
|
---|
207 | " (0x0007, 'Point Run'),"
|
---|
208 | " (0x0100, 'Monte Carlo')"));
|
---|
209 |
|
---|
210 | // HvSettings
|
---|
211 | list.Add(new TObjString(
|
---|
212 | "INSERT MyMagic.HvSettings (fHvSettingsKEY, fHvSettingsName, fHvSettings) VALUES "
|
---|
213 | " (1, 'n/a', 'Not available')"));
|
---|
214 |
|
---|
215 | //Excluded From DataAnalysis
|
---|
216 | list.Add(new TObjString(
|
---|
217 | "INSERT MyMagic.ExcludedFDA (fExcludedFDAKEY, fExcludedFDAName, fExcludedFDA) VALUES "
|
---|
218 | " (1, \"Not excluded\", \"Not excluded from Data Analysis\")"));
|
---|
219 |
|
---|
220 | //Manually Changed
|
---|
221 | list.Add(new TObjString(
|
---|
222 | "INSERT MyMagic.ManuallyChanged (fManuallyChangedKEY, fManuallyChangedName, fManuallyChanged) VALUES "
|
---|
223 | " (1, \"Automatic\", \"This entry was created automatically without user intervention\")");
|
---|
224 |
|
---|
225 | // Source type
|
---|
226 | list.Add(new TObjString(
|
---|
227 | "INSERT MyMagic.SourceType (fSourceTypeName) VALUES ('Unknown')"));
|
---|
228 |
|
---|
229 | // LightConditions
|
---|
230 | list.Add(new TObjString(
|
---|
231 | "INSERT MyMagic.LightConditions (fLightConditionsKEY, fLightConditionsName, fLightConditions) VALUES "
|
---|
232 | " (1, 'n/a', 'Light conditions are not available')"));
|
---|
233 |
|
---|
234 | // Testflag
|
---|
235 | list.Add(new TObjString(
|
---|
236 | "INSERT MyMagic.TestFlag (fTestFlagKEY, fTestFlagName, fTestFlag) VALUES "
|
---|
237 | " (1, 'n/a', 'Testflag is not available')"));
|
---|
238 |
|
---|
239 | // Trigger delay table
|
---|
240 | list.Add(new TObjString(
|
---|
241 | "INSERT MyMagic.TriggerDelayTable (fTriggerDelayTableKEY, fTriggerDelayTableName, fTriggerDelayTable) VALUES "
|
---|
242 | " (1, 'n/a', 'Trigger delay table is not available')"));
|
---|
243 |
|
---|
244 | // Calibration script
|
---|
245 | list.Add(new TObjString(
|
---|
246 | "INSERT MyMagic.CalibrationScript (fCalibrationScriptKEY, fCalibrationScriptName, fCalibrationScript) VALUES "
|
---|
247 | " (1, 'n/a', 'Calibration script is not available')"));
|
---|
248 |
|
---|
249 | // discriminator threshold table
|
---|
250 | list.Add(new TObjString(
|
---|
251 | "INSERT MyMagic.DiscriminatorThresholdTable (fDiscriminatorThresholdTableKEY, fDiscriminatorThresholdTableName, fDiscriminatorThresholdTable) VALUES "
|
---|
252 | " (1, 'n/a', 'Discriminator threshold table is not available')"));
|
---|
253 |
|
---|
254 |
|
---|
255 | TSQLServer *serv = TSQLServer::Connect("mysql://localhost:3306", "hercules", "d99swMT!");
|
---|
256 | if (!serv)
|
---|
257 | return;
|
---|
258 |
|
---|
259 | TIter Next(&list);
|
---|
260 | TObjString *str;
|
---|
261 |
|
---|
262 | cout << "Filling tables..." << endl;
|
---|
263 |
|
---|
264 | while ((str=(TObjString*)Next()))
|
---|
265 | {
|
---|
266 | TString q(str->GetString());
|
---|
267 |
|
---|
268 | Int_t f = q.First('(');
|
---|
269 | TString out = q(0, f);
|
---|
270 | cout << " - " << out << "... " << flush;
|
---|
271 | TSQLResult *res = serv->Query(q);
|
---|
272 | cout << (res==0 ? "ERROR!" : "Ok.") << endl;
|
---|
273 | if (res)
|
---|
274 | delete res;
|
---|
275 | }
|
---|
276 |
|
---|
277 | serv->Close();
|
---|
278 | delete serv;
|
---|
279 | }
|
---|
280 |
|
---|
281 | TObjString *CreateKeyTable(TString name)
|
---|
282 | {
|
---|
283 | return new TObjString(Form(
|
---|
284 | "CREATE TABLE MyMagic.%s ("
|
---|
285 | " f%sKEY SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,"
|
---|
286 | " f%sName VARCHAR(255) NOT NULL UNIQUE,"
|
---|
287 | " f%s VARCHAR(255) NULL"
|
---|
288 | ") MAX_ROWS=65536", name.Data(), name.Data(), name.Data(), name.Data()));
|
---|
289 | }
|
---|
290 |
|
---|
291 | void CreateMagicDataBase()
|
---|
292 | {
|
---|
293 | TList list;
|
---|
294 | list.SetOwner();
|
---|
295 |
|
---|
296 | TString query =
|
---|
297 | "CREATE TABLE MyMagic.RunData"
|
---|
298 | "("
|
---|
299 | " fRunDataKEY INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,"// COMMENT('The unique key for each run-file'),";
|
---|
300 | " fRunNumber INT UNSIGNED NOT NULL UNIQUE," // PACK_KEYS=1,";// COMMENT('Run number'),";
|
---|
301 | " fMagicNumberKEY TINYINT UNSIGNED NOT NULL," // PACK_KEYS=1,";// COMMENT('The MAGIC Key (first two bytes of a raw data file)'),";
|
---|
302 | " fFormatVersion SMALLINT UNSIGNED NOT NULL," // PACK_KEYS=1,";// COMMENT('File format version of the raw data file)'),";
|
---|
303 | " fRunTypeKEY TINYINT UNSIGNED NOT NULL," // PACK_KEYS=1,";// COMMENT('Run type'),";
|
---|
304 | " fProjectKEY SMALLINT UNSIGNED NOT NULL," // PACK_KEYS=1,";// COMMENT('Name of the project (assigned by the physics comittee)'),";
|
---|
305 | " fSourceKEY SMALLINT UNSIGNED NOT NULL," // PACK_KEYS=1,";// COMMENT('Name of the source observed'),";
|
---|
306 | " fNumEvents MEDIUMINT UNSIGNED NOT NULL," // COMMENT('Number of events in this run-file'),";
|
---|
307 | " fRunStart DATETIME NOT NULL," // COMMENT('Time when the run was started'),";
|
---|
308 | " fRunStop DATETIME NULL,"; // COMMENT('Time when the run has stopped')";
|
---|
309 | query +=
|
---|
310 | " fZenithDistance TINYINT NULL," // COMMENT('Time of last change'),";
|
---|
311 | " fAzimuth SMALLINT NULL," // COMMENT('Time of last change'),";
|
---|
312 | " fL1TriggerTableKEY SMALLINT UNSIGNED NOT NULL," // COMMENT('Time of last change'),";
|
---|
313 | " fL2TriggerTableKEY SMALLINT UNSIGNED NOT NULL," // COMMENT('Time of last change'),";
|
---|
314 | " fHvSettingsKEY SMALLINT UNSIGNED NOT NULL," // COMMENT('Time of last change'),";
|
---|
315 | " fDaqStoreRate SMALLINT UNSIGNED NULL," // COMMENT('Time of last change'),";
|
---|
316 | " fDaqTriggerRate SMALLINT UNSIGNED NULL," // COMMENT('Time of last change'),";
|
---|
317 | " fMeanTriggerRate SMALLINT UNSIGNED NULL," // COMMENT('Time of last change'),";
|
---|
318 | " fL2RatePresc SMALLINT UNSIGNED NULL," // COMMENT('Time of last change'),";
|
---|
319 | " fL2RateUnpresc SMALLINT UNSIGNED NULL," // COMMENT('Time of last change'),";
|
---|
320 | " fExcludedFDAKEY SMALLINT UNSIGNED NOT NULL,"
|
---|
321 | " fSequenceFirst INT UNSIGNED NOT NULL,";
|
---|
322 | query +=
|
---|
323 | " fLastUpdate TIMESTAMP" // COMMENT('Time of last change'),";
|
---|
324 | " fTestFlagKEY SMALLINT UNSIGNED NOT NULL,"
|
---|
325 | " fLightConditionsKEY SMALLINT UNSIGNED NOT NULL,"
|
---|
326 | " fCalibrationScriptKEY SMALLINT UNSIGNED NOT NULL,"
|
---|
327 | " fDiscriminatorThresholdTableKEY SMALLINT UNSIGNED NOT NULL,"
|
---|
328 | " fTriggerDelayTableKEY SMALLINT UNSIGNED NOT NULL,"
|
---|
329 | ")";
|
---|
330 |
|
---|
331 | list.Add(new TObjString(query));
|
---|
332 |
|
---|
333 | list.Add(new TObjString(
|
---|
334 | "CREATE TABLE MyMagic.RunType ("
|
---|
335 | " fRunTypeKEY TINYINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,"
|
---|
336 | " fRunType SMALLINT UNSIGNED NOT NULL UNIQUE,"
|
---|
337 | " fRunTypeName VARCHAR(255) NOT NULL UNIQUE,"
|
---|
338 | ") MAX_ROWS=256"));
|
---|
339 |
|
---|
340 | list.Add(new TObjString(
|
---|
341 | "CREATE TABLE MyMagic.MagicNumber ("
|
---|
342 | " fMagicNumberKEY TINYINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,"
|
---|
343 | " fMagicNumber SMALLINT UNSIGNED NOT NULL UNIQUE,"
|
---|
344 | " fMagicNumberName VARCHAR(255) NOT NULL UNIQUE"
|
---|
345 | ") MAX_ROWS=256"));
|
---|
346 |
|
---|
347 | list.Add(new TObjString(
|
---|
348 | "CREATE TABLE MyMagic.Project ("
|
---|
349 | " fProjectKEY SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,"
|
---|
350 | " fProjectName CHAR(22) NOT NULL UNIQUE,"
|
---|
351 | " fProject VARCHAR(255) NOT NULL"
|
---|
352 | ") MAX_ROWS=65536"));
|
---|
353 |
|
---|
354 | list.Add(new TObjString(
|
---|
355 | "CREATE TABLE MyMagic.Source ("
|
---|
356 | " fSourceKEY SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,"
|
---|
357 | " fSourceName CHAR(12) NOT NULL UNIQUE,"
|
---|
358 | " fSourceTypeKEY SMALLINT UNSIGNED NULL,"
|
---|
359 | " fRightAscension DOUBLE NULL,"
|
---|
360 | " fDeclination DOUBLE NULL,"
|
---|
361 | " fEpochChar CHAR NULL,"// DEFAULT='J'
|
---|
362 | " fEpochDate SMALLINT(4) UNSIGNED NULL,"// DEFAULT=2000
|
---|
363 | " fMagnitude SMALLINT NULL" // 82=8.2
|
---|
364 | ") MAX_ROWS=65536"));
|
---|
365 |
|
---|
366 | list.Add(new TObjString(
|
---|
367 | "CREATE TABLE MyMagic.SourceType ("
|
---|
368 | " fSourceTypeKEY SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,"
|
---|
369 | " fSourceTypeName VARCHAR(255) NOT NULL UNIQUE"
|
---|
370 | ") MAX_ROWS=65536"));
|
---|
371 |
|
---|
372 | list.Add(new TObjString(
|
---|
373 | "CREATE TABLE MyMagic.Files ("
|
---|
374 | " fRawFileKEY INT UNSIGNED NOT NULL PRIMARY KEY,"
|
---|
375 | " fRepFileKEY INT UNSIGNED NULL UNIQUE,"
|
---|
376 | " fDccFileKEY INT UNSIGNED NULL UNIQUE"
|
---|
377 | ")"));
|
---|
378 |
|
---|
379 | list.Add(new TObjString(
|
---|
380 | "CREATE TABLE MyMagic.RawFile ("
|
---|
381 | " fRawFileKEY INT UNSIGNED PRIMARY KEY,"
|
---|
382 | " fRawFileName VARCHAR(64) NOT NULL UNIQUE," // NULL means - Not in Wuerzburg!
|
---|
383 | " fFilePathKEY SMALLINT UNSIGNED NOT NULL"
|
---|
384 | ")"));
|
---|
385 |
|
---|
386 | list.Add(new TObjString(
|
---|
387 | "CREATE TABLE MyMagic.RepFile ("
|
---|
388 | " fRepFileKEY INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,"
|
---|
389 | " fRepFileName VARCHAR(64) NOT NULL UNIQUE," // NULL means - Not in Wuerzburg!
|
---|
390 | " fFilePathKEY SMALLINT UNSIGNED NOT NULL"
|
---|
391 | ")"));
|
---|
392 |
|
---|
393 | list.Add(new TObjString(
|
---|
394 | "CREATE TABLE MyMagic.DccFile ("
|
---|
395 | " fDccFileKEY INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,"
|
---|
396 | " fDccFileName VARCHAR(64) NOT NULL UNIQUE," // NULL means - Not in Wuerzburg!
|
---|
397 | " fFilePathKEY SMALLINT UNSIGNED NOT NULL"
|
---|
398 | ")"));
|
---|
399 |
|
---|
400 | list.Add(new TObjString(
|
---|
401 | "CREATE TABLE MyMagic.FilePath ("
|
---|
402 | " fFilePathKEY SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,"
|
---|
403 | " fFilePathName VARCHAR(255) NOT NULL UNIQUE,"
|
---|
404 | " fFilePath VARCHAR(255) NOT NULL UNIQUE"
|
---|
405 | ") MAX_ROWS=65536"));
|
---|
406 |
|
---|
407 | list.Add(CreateKeyTable("L1TriggerTable"));
|
---|
408 | list.Add(CreateKeyTable("L2TriggerTable"));
|
---|
409 | list.Add(CreateKeyTable("HvSettings"));
|
---|
410 | // list.Add(CreateKeyTable("ExcludedFDA"));
|
---|
411 | list.Add(CreateKeyTable("ManuallyChanged"));
|
---|
412 | list.Add(CreateKeyTable("TestFlag"));
|
---|
413 | list.Add(CreateKeyTable("LightConditions"));
|
---|
414 | list.Add(CreateKeyTable("CalibrationScript"));
|
---|
415 | list.Add(CreateKeyTable("DiscriminatorThresholdTable"));
|
---|
416 | list.Add(CreateKeyTable("TriggerDelayTable"));
|
---|
417 |
|
---|
418 | list.Add(new TObjString(
|
---|
419 | "CREATE TABLE MyMagic.%s ("
|
---|
420 | " fExcludedFDAKEY SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,"
|
---|
421 | " fExcludedFDAImportance SMALLINT UNSIGNED NULL,"
|
---|
422 | " fExcludedFDAAutomatic ENUM("yes","no") NULL,"
|
---|
423 | " fExcludedFDAName VARCHAR(255) NOT NULL UNIQUE,"
|
---|
424 | " fExcludedFDA VARCHAR(255) NULL"
|
---|
425 | ") MAX_ROWS=65536"));
|
---|
426 |
|
---|
427 |
|
---|
428 | /*
|
---|
429 | list.Add(new TObjString(
|
---|
430 | "CREATE TABLE MyMagic.TriggerTable ("
|
---|
431 | " fTriggerTableKEY SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,"
|
---|
432 | " fTriggerTableName VARCHAR(255) NOT NULL UNIQUE,"
|
---|
433 | " fTriggerTable VARCHAR(255) NULL"
|
---|
434 | ") MAX_ROWS=65536"));
|
---|
435 |
|
---|
436 | list.Add(new TObjString(
|
---|
437 | "CREATE TABLE MyMagic.HvSettings ("
|
---|
438 | " fHvSettingsKEY SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,"
|
---|
439 | " fHvSettingsName VARCHAR(255) NOT NULL UNIQUE,"
|
---|
440 | " fHvSettings VARCHAR(255) NULL"
|
---|
441 | ") MAX_ROWS=65536"));
|
---|
442 |
|
---|
443 | list.Add(new TObjString(
|
---|
444 | "CREATE TABLE MyMagic.ExcludedFDA ("
|
---|
445 | " fExcludedFDAKEY SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,"
|
---|
446 | " fExcludedFDAName VARCHAR(255) NOT NULL UNIQUE,"
|
---|
447 | " fExcludedFDA VARCHAR(255) NULL"
|
---|
448 | ") MAX_ROWS=65536"));
|
---|
449 |
|
---|
450 | list.Add(new TObjString(
|
---|
451 | "CREATE TABLE MyMagic.ManuallyChanged ("
|
---|
452 | " fManuallyChangedKEY SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,"
|
---|
453 | " fManuallyChangedName VARCHAR(255) NOT NULL UNIQUE,"
|
---|
454 | " fManuallyChanged VARCHAR(255) NULL"
|
---|
455 | ") MAX_ROWS=65536"));
|
---|
456 | */
|
---|
457 | list.Add(new TObjString(
|
---|
458 | "CREATE TABLE MyMagic.Changes ("
|
---|
459 | " fChangesKEY INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,"
|
---|
460 | " fTimeStamp TIMESTAMP,"
|
---|
461 | " fTableName VARCHAR(64) NOT NULL,"
|
---|
462 | " fRowKEY INT UNSIGNED NOT NULL,"
|
---|
463 | " fColumnName VARCHAR(64) NOT NULL,"
|
---|
464 | " fDescription VARCHAR(255) NOT NULL"
|
---|
465 | ")"));
|
---|
466 |
|
---|
467 | list.Add(new TObjString(
|
---|
468 | "CREATE TABLE MyMagic.Comments ("
|
---|
469 | " fCommentsKEY INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,"
|
---|
470 | " fCommentsName VARCHAR(255) NOT NULL,",
|
---|
471 | " fRunDataKEY INT UNSIGNED NOT NULL"
|
---|
472 | ")"));
|
---|
473 |
|
---|
474 | list.Add(new TObjString(
|
---|
475 | "CREATE TABLE MyMagic.RunBook ("
|
---|
476 | " fRunBookKEY INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,"
|
---|
477 | " fRunBookDate DATETIME NOT NULL,",
|
---|
478 | " fRunBookText TEXT NOT NULL"
|
---|
479 | ")"));
|
---|
480 |
|
---|
481 | list.Add(new TObjString(
|
---|
482 | "CREATE TABLE MyMagic.Sequences ("
|
---|
483 | " fSequenceFirst INT UNSIGNED PRIMARY KEY, "
|
---|
484 | " fSequenceLast INT UNSIGNED NOT NULL UNIQUE, "
|
---|
485 | " fProjectKEY SMALLINT UNSIGNED NOT NULL,"
|
---|
486 | " fSourceKEY SMALLINT UNSIGNED NOT NULL,"
|
---|
487 | " fNumEvents MEDIUMINT UNSIGNED NOT NULL,"
|
---|
488 | " fRunTime SMALLINT UNSIGNED NOT NULL,"
|
---|
489 | " fRunStart DATETIME NOT NULL,"
|
---|
490 | " fZenithDistanceMin TINYINT NULL,"
|
---|
491 | " fZenithDistanceMax TINYINT NULL,"
|
---|
492 | " fAzimuthMin SMALLINT NULL,"
|
---|
493 | " fAzimuthMax SMALLINT NULL,"
|
---|
494 | " fL1TriggerTableKEY SMALLINT UNSIGNED NOT NULL,";
|
---|
495 | query +=
|
---|
496 | " fL2TriggerTableKEY SMALLINT UNSIGNED NOT NULL,"
|
---|
497 | " fHvSettingsKEY SMALLINT UNSIGNED NOT NULL,"
|
---|
498 | " fManuallyChanged SMALLINT UNSIGNED NOT NULL,"
|
---|
499 | " fLastUpdate TIMESTAMP" // COMMENT('Time of last change'),";
|
---|
500 | " fTestFlagKEY SMALLINT UNSIGNED NOT NULL,"
|
---|
501 | " fLightConditionsKEY SMALLINT UNSIGNED NOT NULL,"
|
---|
502 | " fDiscriminatorThresholdTableKEY SMALLINT UNSIGNED NOT NULL,"
|
---|
503 | " fTriggerDelayTableKEY SMALLINT UNSIGNED NOT NULL,"
|
---|
504 | ")";
|
---|
505 |
|
---|
506 | list.Add(new TObjString(
|
---|
507 | "CREATE TABLE MyMagic.Calibration ("
|
---|
508 | " fSequenceFirst INT UNSIGNED PRIMARY KEY, "
|
---|
509 | " fUnsuitableInner SMALLINT UNSIGNED NOT NULL, "
|
---|
510 | " fUnsuitableOuter SMALLINT UNSIGNED NOT NULL, "
|
---|
511 | " fUnreliableInner SMALLINT UNSIGNED NOT NULL,"
|
---|
512 | " fUnreliableOuter SMALLINT UNSIGNED NOT NULL,"
|
---|
513 | " fIsolatedInner SMALLINT UNSIGNED NOT NULL,"
|
---|
514 | " fIsolatedOuter SMALLINT UNSIGNED NOT NULL,"
|
---|
515 | " fIsolatedMaxCluster SMALLINT UNSIGNED NOT NULL,"
|
---|
516 | " fMeanPedRmsInner FLOAT(5,1) NOT NULL,"
|
---|
517 | " fMeanPedRmsOuter FLOAT(5,1) NOT NULL,"
|
---|
518 | " fArrTimeMeanInner FLOAT(5,1) NOT NULL,"
|
---|
519 | " fArrTimeRmsInner FLOAT(5,1) NOT NULL,"
|
---|
520 | " fArrTimeMeanOuter FLOAT(5,1) NOT NULL,"
|
---|
521 | " fArrTimeRmsOuter FLOAT(5,1) NOT NULL,"
|
---|
522 | " fConvFactorInner FLOAT(6,3) NOT NULL,"
|
---|
523 | " fConvFactorOuter FLOAT(6,3) NOT NULL,"
|
---|
524 | " fLastUpdate TIMESTAMP"
|
---|
525 | ")"));
|
---|
526 |
|
---|
527 | list.Add(new TObjString(
|
---|
528 | "CREATE TABLE MyMagic.Star ("
|
---|
529 | " fSequenceFirst INT UNSIGNED PRIMARY KEY, "
|
---|
530 | " fMeanNumberIslands FLOAT(5,1) NOT NULL,"
|
---|
531 | " fPSF FLOAT(5,1) NOT NULL,"
|
---|
532 | " fRatio FLOAT(5,1) NOT NULL,"
|
---|
533 | " fMuonRate FLOAT(6,2) NOT NULL,"
|
---|
534 | " fMuonNumber INT UNSIGNED NOT NULL,"
|
---|
535 | " fEffOnTime INT UNSIGNED NOT NULL,"
|
---|
536 | " fDataRate INT UNSIGNED NOT NULL,"
|
---|
537 | " fMaxHumidity FLOAT(6,1) NOT NULL,"
|
---|
538 | " fLastUpdate TIMESTAMP"
|
---|
539 | ")"));
|
---|
540 |
|
---|
541 | list.Add(new TObjString(
|
---|
542 | "CREATE TABLE MyMagic.DataSets ("
|
---|
543 | " fDataSetNumber INT UNSIGNED PRIMARY KEY, "
|
---|
544 | " fSourceKEY SMALLINT UNSIGNED NOT NULL,"
|
---|
545 | " fWobble ENUM('Y','N') NULL,"
|
---|
546 | " fLastUpdate TIMESTAMP"
|
---|
547 | ")"));
|
---|
548 |
|
---|
549 | list.Add(new TObjString(
|
---|
550 | "CREATE TABLE MyMagic.DataSetProcessStatus ("
|
---|
551 | " fDataSetNumber INT UNSIGNED PRIMARY KEY, "
|
---|
552 | " fDataSetInserted DATETIME NULL,"
|
---|
553 | " fStarFilesAvail DATETIME NULL,"
|
---|
554 | " fGanymed DATETIME NULL"
|
---|
555 | ")";
|
---|
556 |
|
---|
557 | list.Add(new TObjString(
|
---|
558 | "CREATE TABLE MyMagic.SequenceBuildStatus ("
|
---|
559 | " fDate DATE PRIMARY KEY, "
|
---|
560 | " fCCFilled DATETIME NULL,"
|
---|
561 | " fExclusionsDone DATETIME NULL,"
|
---|
562 | " fSequenceEntriesBuilt DATETIME NULL"
|
---|
563 | ")";
|
---|
564 |
|
---|
565 | list.Add(new TObjString(
|
---|
566 | "CREATE TABLE MyMagic.RunProcessStatus ("
|
---|
567 | " fRunNumber INT UNSIGNED PRIMARY KEY, "
|
---|
568 | " fCCFileAvail DATETIME NULL,"
|
---|
569 | " fCaCoFileAvail DATETIME NULL,"
|
---|
570 | " fCaCoFileFound INT UNSIGNED NULL,"
|
---|
571 | " fRawFileAvail DATETIME NULL,"
|
---|
572 | " fFillDotRaw DATETIME NULL,"
|
---|
573 | " fTimingCorrection DATETIME NULL,"
|
---|
574 | " fMerpp DATETIME NULL,"
|
---|
575 | " fMerppCCUpdate DATETIME NULL,"
|
---|
576 | " fMerppCaCoUpdate DATETIME NULL"
|
---|
577 | ")";
|
---|
578 |
|
---|
579 | list.Add(new TObjString(
|
---|
580 | "CREATE TABLE MyMagic.SequenceProcessStatus ("
|
---|
581 | " fSequenceFirst INT UNSIGNED PRIMARY KEY, "
|
---|
582 | " fSequenceFileWritten DATETIME NULL,"
|
---|
583 | " fAllFilesAvail DATETIME NULL,"
|
---|
584 | " fCallisto DATETIME NULL,"
|
---|
585 | " fFillCallisto DATETIME NULL,"
|
---|
586 | " fStar DATETIME NULL,"
|
---|
587 | " fFillStar DATETIME NULL"
|
---|
588 | ")";
|
---|
589 |
|
---|
590 | list.Add(new TObjString(
|
---|
591 | "CREATE TABLE MyMagic.MarsVersion ("
|
---|
592 | " fMarsVersion SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,"
|
---|
593 | " fStartDate DATETIME NULL ,"
|
---|
594 | " fMarsVersionName VARCHAR(12) NOT NULL UNIQUE"
|
---|
595 | ") MAX_ROWS=256"));
|
---|
596 |
|
---|
597 | TSQLResult *res;
|
---|
598 |
|
---|
599 | TSQLServer *serv = TSQLServer::Connect("mysql://localhost:3306", "hercules", "d99swMT!");
|
---|
600 | if (!serv)
|
---|
601 | return;
|
---|
602 |
|
---|
603 | res = serv->DropDataBase("MyMagic");
|
---|
604 | res = serv->CreateDataBase("MyMagic");
|
---|
605 | if (res)
|
---|
606 | {
|
---|
607 | cout << "Error creating Data Base!" << endl;
|
---|
608 | return;
|
---|
609 | }
|
---|
610 |
|
---|
611 | TIter Next(&list);
|
---|
612 | TObjString *str;
|
---|
613 |
|
---|
614 | cout << "Creating tables..." << endl;
|
---|
615 |
|
---|
616 | while ((str=(TObjString*)Next()))
|
---|
617 | {
|
---|
618 | TString q(str->GetString());
|
---|
619 |
|
---|
620 | Int_t f = q.First('(');
|
---|
621 | TString out = q(0, f);
|
---|
622 | cout << " - " << out << "... " << flush;
|
---|
623 | res = serv->Query(q);
|
---|
624 | cout << (res==0 ? "ERROR!" : "Ok.") << endl;
|
---|
625 | if (res)
|
---|
626 | delete res;
|
---|
627 | }
|
---|
628 |
|
---|
629 | serv->Close();
|
---|
630 | delete serv;
|
---|
631 | }
|
---|
632 |
|
---|
633 | void DisplayMagicDataBase()
|
---|
634 | {
|
---|
635 | TSQLServer *serv = TSQLServer::Connect("mysql://localhost:3306", "hercules", "d99swMT!");
|
---|
636 | if (!serv)
|
---|
637 | return;
|
---|
638 |
|
---|
639 | res = serv->GetColumns("MyMagic", "RunData");
|
---|
640 | if (res)
|
---|
641 | Print(res);
|
---|
642 | res = serv->GetColumns("MyMagic", "RunType");
|
---|
643 | if (res)
|
---|
644 | Print(res);
|
---|
645 | res = serv->GetColumns("MyMagic", "MagicNumber");
|
---|
646 | if (res)
|
---|
647 | Print(res);
|
---|
648 | res = serv->GetColumns("MyMagic", "Project");
|
---|
649 | if (res)
|
---|
650 | Print(res);
|
---|
651 | res = serv->GetColumns("MyMagic", "Source");
|
---|
652 | if (res)
|
---|
653 | Print(res);
|
---|
654 | res = serv->GetColumns("MyMagic", "TriggerTable");
|
---|
655 | if (res)
|
---|
656 | Print(res);
|
---|
657 | res = serv->GetColumns("MyMagic", "HvSettings");
|
---|
658 | if (res)
|
---|
659 | Print(res);
|
---|
660 |
|
---|
661 | serv->Close();
|
---|
662 | }
|
---|
663 |
|
---|
664 | Bool_t ExistStr(TSQLServer *serv, const char *column, const char *table, const char *test)
|
---|
665 | {
|
---|
666 | TString query(Form("SELECT %s FROM %s WHERE %s='%s'", column, table, column, test));
|
---|
667 | TSQLResult *res = serv->Query(query);
|
---|
668 | if (!res)
|
---|
669 | return kFALSE;
|
---|
670 | delete res;
|
---|
671 |
|
---|
672 | TSQLRow *row;
|
---|
673 |
|
---|
674 | while (row=res->Next())
|
---|
675 | {
|
---|
676 | if ((*row)[0])
|
---|
677 | return kTRUE;
|
---|
678 | }
|
---|
679 |
|
---|
680 | return kFALSE;
|
---|
681 | }
|
---|
682 | /*
|
---|
683 | void LoadSourceNames(const char *fname)
|
---|
684 | {
|
---|
685 | TSQLServer *serv = TSQLServer::Connect("mysql://localhost:3306", "hercules", "d99swMT!");
|
---|
686 | if (!serv)
|
---|
687 | return;
|
---|
688 |
|
---|
689 | ifstream fin(fname);
|
---|
690 | if (!fin)
|
---|
691 | {
|
---|
692 | cout << "Cannot open " << fname << endl;
|
---|
693 | return;
|
---|
694 | }
|
---|
695 |
|
---|
696 | while (1)
|
---|
697 | {
|
---|
698 | TString query, name;
|
---|
699 |
|
---|
700 | name.ReadLine(fin);
|
---|
701 | if (!fin)
|
---|
702 | break;
|
---|
703 |
|
---|
704 | if (ExistStr(serv, "fSourceName", "MyMagic.Source", name))
|
---|
705 | {
|
---|
706 | cout << "Entry " << name << " exists." << endl;
|
---|
707 | continue;
|
---|
708 | }
|
---|
709 |
|
---|
710 | query = "INSERT MyMagic.Source SET ";
|
---|
711 | query += " fSourceName='";
|
---|
712 | query += name;
|
---|
713 | query += "', fSourceTypeKEY=1";
|
---|
714 | if (!serv->Query(query))
|
---|
715 | {
|
---|
716 | cout << query << " - FAILED!" << endl;
|
---|
717 | return;
|
---|
718 | }
|
---|
719 | }
|
---|
720 |
|
---|
721 | serv->Close();
|
---|
722 | }
|
---|
723 |
|
---|
724 | void LoadProjectNames(const char *fname)
|
---|
725 | {
|
---|
726 | TSQLServer *serv = TSQLServer::Connect("mysql://localhost:3306", "hercules", "d99swMT!");
|
---|
727 | if (!serv)
|
---|
728 | return;
|
---|
729 |
|
---|
730 | ifstream fin(fname);
|
---|
731 | if (!fin)
|
---|
732 | {
|
---|
733 | cout << "Cannot open " << fname << endl;
|
---|
734 | return;
|
---|
735 | }
|
---|
736 |
|
---|
737 | while (1)
|
---|
738 | {
|
---|
739 | TString query, name;
|
---|
740 |
|
---|
741 | name.ReadLine(fin);
|
---|
742 | if (!fin)
|
---|
743 | break;
|
---|
744 |
|
---|
745 | if (ExistStr(serv, "fProjectName", "MyMagic.Project", name))
|
---|
746 | {
|
---|
747 | cout << "Entry " << name << " exists." << endl;
|
---|
748 | continue;
|
---|
749 | }
|
---|
750 |
|
---|
751 | query = "INSERT MyMagic.Project SET ";
|
---|
752 | query += " fProjectName='";
|
---|
753 | query += name;
|
---|
754 | query += "', fProject='AUTO: ";
|
---|
755 | query += name;
|
---|
756 | query += "'";
|
---|
757 | if (!serv->Query(query))
|
---|
758 | {
|
---|
759 | cout << query << " - FAILED!" << endl;
|
---|
760 | return;
|
---|
761 | }
|
---|
762 | }
|
---|
763 |
|
---|
764 | serv->Close();
|
---|
765 | }
|
---|
766 |
|
---|
767 | void LoadTriggerTableNames(const char *fname)
|
---|
768 | {
|
---|
769 | TSQLServer *serv = TSQLServer::Connect("mysql://localhost:3306", "hercules", "d99swMT!");
|
---|
770 | if (!serv)
|
---|
771 | return;
|
---|
772 |
|
---|
773 | ifstream fin(fname);
|
---|
774 | if (!fin)
|
---|
775 | {
|
---|
776 | cout << "Cannot open " << fname << endl;
|
---|
777 | return;
|
---|
778 | }
|
---|
779 |
|
---|
780 | while (1)
|
---|
781 | {
|
---|
782 | TString query, name;
|
---|
783 |
|
---|
784 | name.ReadLine(fin);
|
---|
785 | if (!fin)
|
---|
786 | break;
|
---|
787 |
|
---|
788 | if (ExistStr(serv, "fTriggerTableName", "MyMagic.TriggerTable", name))
|
---|
789 | {
|
---|
790 | cout << "Entry " << name << " exists." << endl;
|
---|
791 | continue;
|
---|
792 | }
|
---|
793 |
|
---|
794 | query = "INSERT MyMagic.TriggerTable SET ";
|
---|
795 | query += " fTriggerTableName='";
|
---|
796 | query += name;
|
---|
797 | query += "'";
|
---|
798 | if (!serv->Query(query))
|
---|
799 | {
|
---|
800 | cout << query << " - FAILED!" << endl;
|
---|
801 | return;
|
---|
802 | }
|
---|
803 | }
|
---|
804 |
|
---|
805 | serv->Close();
|
---|
806 | }
|
---|
807 | */
|
---|
808 | void setupdb()
|
---|
809 | {
|
---|
810 | CreateMagicDataBase();
|
---|
811 | CreatePrimaryEntries();
|
---|
812 | //LoadSourceNames("sourcenames.txt");
|
---|
813 | //LoadProjectNames("projectnames.txt");
|
---|
814 | //LoadTriggerTableNames("triggertablenames.txt");
|
---|
815 |
|
---|
816 |
|
---|
817 | return;
|
---|
818 |
|
---|
819 | //TSQLServer *serv = TSQLServer::Connect("mysql://magic:3306", "root", "marWin");
|
---|
820 | //TSQLServer *serv = TSQLServer::Connect("mysql://localhost:3306", "database", "ImM9G1CD8");
|
---|
821 | TSQLServer *serv = TSQLServer::Connect("mysql://localhost:3306", "hercules", "d99swMT!");
|
---|
822 | if (!serv)
|
---|
823 | return;
|
---|
824 |
|
---|
825 | cout << "ServerInfo: " << serv->ServerInfo() << endl;
|
---|
826 | cout << "DBMS: " << serv->GetDBMS() << endl;
|
---|
827 | cout << "Host: " << serv->GetHost() << endl;
|
---|
828 | cout << "Port: " << serv->GetPort() << endl;
|
---|
829 |
|
---|
830 | TSQLResult *res;
|
---|
831 |
|
---|
832 | cout << endl;
|
---|
833 |
|
---|
834 | res = serv->GetDataBases();
|
---|
835 | if (res)
|
---|
836 | Print(res);
|
---|
837 |
|
---|
838 | serv->Close();
|
---|
839 | /*
|
---|
840 | TList list;
|
---|
841 | if (!list.FindObject(triggertablename))
|
---|
842 | {
|
---|
843 | TNamed *name = new TNamed(triggertablename, "");
|
---|
844 | list.Add(name);
|
---|
845 | }
|
---|
846 |
|
---|
847 | list.Print();
|
---|
848 | */
|
---|
849 | }
|
---|