source: tags/Mars-V0.9.4.3/datacenter/macros/setupdb.C

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