source: trunk/MagicSoft/Mars/datacenter/macros/setupdb.C@ 7648

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