| 1 | /* ======================================================================== *\
|
|---|
| 2 | !
|
|---|
| 3 | ! *
|
|---|
| 4 | ! * This file is part of MARS, the MAGIC Analysis and Reconstruction
|
|---|
| 5 | ! * Software. It is distributed to you in the hope that it can be a useful
|
|---|
| 6 | ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
|
|---|
| 7 | ! * It is distributed WITHOUT ANY WARRANTY.
|
|---|
| 8 | ! *
|
|---|
| 9 | ! * Permission to use, copy, modify and distribute this software and its
|
|---|
| 10 | ! * documentation for any purpose is hereby granted without fee,
|
|---|
| 11 | ! * provided that the above copyright notice appear in all copies and
|
|---|
| 12 | ! * that both that copyright notice and this permission notice appear
|
|---|
| 13 | ! * in supporting documentation. It is provided "as is" without express
|
|---|
| 14 | ! * or implied warranty.
|
|---|
| 15 | ! *
|
|---|
| 16 | !
|
|---|
| 17 | !
|
|---|
| 18 | ! Author(s): Daniela Dorner, 01/2005 <mailto:dorner@astro.uni-wuerzburg.de>
|
|---|
| 19 | !
|
|---|
| 20 | ! Copyright: MAGIC Software Development, 2000-2005
|
|---|
| 21 | !
|
|---|
| 22 | !
|
|---|
| 23 | \* ======================================================================== */
|
|---|
| 24 |
|
|---|
| 25 | /////////////////////////////////////////////////////////////////////////////
|
|---|
| 26 | //
|
|---|
| 27 | // InsertDataset.C
|
|---|
| 28 | // ===============
|
|---|
| 29 | //
|
|---|
| 30 | /////////////////////////////////////////////////////////////////////////////
|
|---|
| 31 |
|
|---|
| 32 | #include <iostream>
|
|---|
| 33 | #include <iomanip>
|
|---|
| 34 | #include <fstream>
|
|---|
| 35 |
|
|---|
| 36 | #include <TEnv.h>
|
|---|
| 37 |
|
|---|
| 38 | #include <MSQLServer.h>
|
|---|
| 39 | #include <TSQLRow.h>
|
|---|
| 40 | #include <TSQLResult.h>
|
|---|
| 41 |
|
|---|
| 42 | using namespace std;
|
|---|
| 43 |
|
|---|
| 44 | Bool_t ExistStr(MSQLServer &serv, const char *column, const char *table, const char *test)
|
|---|
| 45 | {
|
|---|
| 46 | TString query(Form("SELECT %s FROM %s WHERE %s='%s'", column, table, column, test));
|
|---|
| 47 | TSQLResult *res = serv.Query(query);
|
|---|
| 48 | if (!res)
|
|---|
| 49 | return kFALSE;
|
|---|
| 50 |
|
|---|
| 51 | Bool_t rc = kFALSE;
|
|---|
| 52 |
|
|---|
| 53 | TSQLRow *row=res->Next();
|
|---|
| 54 | if (row && (*row)[0])
|
|---|
| 55 | rc=kTRUE;
|
|---|
| 56 |
|
|---|
| 57 | delete res;
|
|---|
| 58 | return rc;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | Int_t QueryNameKEY(MSQLServer &serv, Bool_t dummy, const char *col, const char *name, Bool_t insert=kTRUE)
|
|---|
| 62 | {
|
|---|
| 63 | TString query;
|
|---|
| 64 |
|
|---|
| 65 | query = Form("SELECT f%sKEY FROM %s WHERE f%sName='%s'", col, col, col, name);
|
|---|
| 66 | TSQLResult *res = serv.Query(query);
|
|---|
| 67 | if (!res)
|
|---|
| 68 | return -1;
|
|---|
| 69 |
|
|---|
| 70 | TSQLRow *row=res->Next();
|
|---|
| 71 |
|
|---|
| 72 | Int_t rc = row && (*row)[0] ? atoi((*row)[0]) : -1;
|
|---|
| 73 |
|
|---|
| 74 | delete res;
|
|---|
| 75 |
|
|---|
| 76 | if (rc>=0)
|
|---|
| 77 | return rc;
|
|---|
| 78 |
|
|---|
| 79 | if (!insert)
|
|---|
| 80 | return -1;
|
|---|
| 81 |
|
|---|
| 82 | query = Form("INSERT %s (f%sName) VALUES (\"%s\");", col, col, name);
|
|---|
| 83 |
|
|---|
| 84 | if (dummy)
|
|---|
| 85 | {
|
|---|
| 86 | cout << query << endl;
|
|---|
| 87 | return 0;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | res=serv.Query(query);
|
|---|
| 91 | if (!res)
|
|---|
| 92 | return -1;
|
|---|
| 93 |
|
|---|
| 94 | delete res;
|
|---|
| 95 |
|
|---|
| 96 | Int_t key = QueryNameKEY(serv, dummy, col, name, kFALSE);
|
|---|
| 97 | if (key>0)
|
|---|
| 98 | {
|
|---|
| 99 | cout << "New " << col << ": " << name << endl;
|
|---|
| 100 | return key;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | cout << "ERROR: " << query << endl;
|
|---|
| 104 | return kFALSE;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | int insertdataset(TString number, TString source, TString wobble, Bool_t dummy=kTRUE)
|
|---|
| 108 | {
|
|---|
| 109 | TEnv env("sql.rc");
|
|---|
| 110 |
|
|---|
| 111 | MSQLServer serv(env);
|
|---|
| 112 | if (!serv.IsConnected())
|
|---|
| 113 | {
|
|---|
| 114 | cout << "ERROR - Connection to database failed." << endl;
|
|---|
| 115 | return 0;
|
|---|
| 116 | }
|
|---|
| 117 | cout << "insertdataset" << endl;
|
|---|
| 118 | cout << "-------------" << endl;
|
|---|
| 119 | cout << endl;
|
|---|
| 120 | cout << "Connected to " << serv.GetName() << endl;
|
|---|
| 121 | cout << endl;
|
|---|
| 122 |
|
|---|
| 123 | Int_t sourcekey = QueryNameKEY(serv, dummy, "Source", source.Data(), kFALSE);
|
|---|
| 124 | if (sourcekey<0)
|
|---|
| 125 | {
|
|---|
| 126 | cout << "Error - could not get sourcename from DB -> " << flush;
|
|---|
| 127 | cout << "maybe you have the wrong sourcename in your datasetfile" << endl;
|
|---|
| 128 | return 0;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | cout << "no:" << number << endl;
|
|---|
| 132 |
|
|---|
| 133 | if (!ExistStr(serv, "fDataSetNumber", "DataSets", number.Data())) // Form("%d", number)
|
|---|
| 134 | {
|
|---|
| 135 | TString query=Form("INSERT DataSets SET fDataSetNumber='%s', "
|
|---|
| 136 | " fSourceKEY=%d, fWobble='%s' ",
|
|---|
| 137 | number.Data(), sourcekey, wobble.Data());
|
|---|
| 138 |
|
|---|
| 139 | if (dummy)
|
|---|
| 140 | {
|
|---|
| 141 | cout << query << endl;
|
|---|
| 142 | return 0;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | TSQLResult *res = serv.Query(query);
|
|---|
| 146 | if (!res)
|
|---|
| 147 | {
|
|---|
| 148 | cout << "Error - could not insert dataset" << endl;
|
|---|
| 149 | return 0;
|
|---|
| 150 | }
|
|---|
| 151 | delete res;
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 | query=Form("INSERT DataSetProcessStatus SET fDataSetNumber='%s', "
|
|---|
| 155 | " fDataSetInserted=Now() ",
|
|---|
| 156 | number.Data());
|
|---|
| 157 |
|
|---|
| 158 | res = serv.Query(query);
|
|---|
| 159 | if (!res)
|
|---|
| 160 | {
|
|---|
| 161 | cout << "Error - could not insert dataset" << endl;
|
|---|
| 162 | return 0;
|
|---|
| 163 | }
|
|---|
| 164 | delete res;
|
|---|
| 165 | }
|
|---|
| 166 | else
|
|---|
| 167 | {
|
|---|
| 168 | cout << number << " already exists... " << endl;
|
|---|
| 169 | return 0;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | return 1;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|