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

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