| 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-2006 | 
|---|
| 21 | ! | 
|---|
| 22 | ! | 
|---|
| 23 | \* ======================================================================== */ | 
|---|
| 24 |  | 
|---|
| 25 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 26 | // | 
|---|
| 27 | // insertdataset.C | 
|---|
| 28 | // =============== | 
|---|
| 29 | // | 
|---|
| 30 | // This macro inserts datasets into the database. | 
|---|
| 31 | // If a new dataset file has been stored in the dataset directory, the | 
|---|
| 32 | // information file is read by a script and the information is inserted with | 
|---|
| 33 | // this macro into the database in the tables DataSets and | 
|---|
| 34 | // DataSetProcessStatus, where in the information about datasets is stored. | 
|---|
| 35 | // | 
|---|
| 36 | // Usage: | 
|---|
| 37 | //   .x insertdataset.C+("number","source,"wobble","comment",kTRUE) | 
|---|
| 38 | // The first argument is the dataset number, the second is giving the source | 
|---|
| 39 | // name, the third the observation mode (wobble/on-off), the fourth a comment | 
|---|
| 40 | // about the dataset. This arguments are extracted by a script from the | 
|---|
| 41 | // dataset file. | 
|---|
| 42 | // The last argument is the 'dummy-mode'. If it is kTRUE dummy-mode is | 
|---|
| 43 | // switched on and nothing will be written into the database. This is usefull | 
|---|
| 44 | // for tests. | 
|---|
| 45 | // | 
|---|
| 46 | // Make sure, that database and password are corretly set in a resource | 
|---|
| 47 | // file called sql.rc and the resource file is found. | 
|---|
| 48 | // | 
|---|
| 49 | // Returns 0 in case of failure and 1 in case of success. | 
|---|
| 50 | // | 
|---|
| 51 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 52 |  | 
|---|
| 53 | #include <iostream> | 
|---|
| 54 | #include <iomanip> | 
|---|
| 55 | #include <fstream> | 
|---|
| 56 |  | 
|---|
| 57 | #include <TEnv.h> | 
|---|
| 58 |  | 
|---|
| 59 | #include <MSQLServer.h> | 
|---|
| 60 | #include <TSQLRow.h> | 
|---|
| 61 | #include <TSQLResult.h> | 
|---|
| 62 |  | 
|---|
| 63 | using namespace std; | 
|---|
| 64 |  | 
|---|
| 65 | Bool_t ExistStr(MSQLServer &serv, const char *column, const char *table, const char *test) | 
|---|
| 66 | { | 
|---|
| 67 | TString query(Form("SELECT %s FROM %s WHERE %s='%s'", column, table, column, test)); | 
|---|
| 68 | TSQLResult *res = serv.Query(query); | 
|---|
| 69 | if (!res) | 
|---|
| 70 | return kFALSE; | 
|---|
| 71 |  | 
|---|
| 72 | Bool_t rc = kFALSE; | 
|---|
| 73 |  | 
|---|
| 74 | TSQLRow *row=res->Next(); | 
|---|
| 75 | if (row && (*row)[0]) | 
|---|
| 76 | rc=kTRUE; | 
|---|
| 77 |  | 
|---|
| 78 | delete res; | 
|---|
| 79 | return rc; | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | //get key of a name | 
|---|
| 83 | // insert value, if it is not yet existing | 
|---|
| 84 | Int_t QueryNameKEY(MSQLServer &serv, Bool_t dummy, const char *col, const char *name, Bool_t insert=kTRUE) | 
|---|
| 85 | { | 
|---|
| 86 | TString query; | 
|---|
| 87 |  | 
|---|
| 88 | query = Form("SELECT f%sKEY FROM %s WHERE f%sName='%s'", col, col, col, name); | 
|---|
| 89 | TSQLResult *res = serv.Query(query); | 
|---|
| 90 | if (!res) | 
|---|
| 91 | return -1; | 
|---|
| 92 |  | 
|---|
| 93 | TSQLRow *row=res->Next(); | 
|---|
| 94 |  | 
|---|
| 95 | Int_t rc = row && (*row)[0] ? atoi((*row)[0]) : -1; | 
|---|
| 96 |  | 
|---|
| 97 | delete res; | 
|---|
| 98 |  | 
|---|
| 99 | if (rc>=0) | 
|---|
| 100 | return rc; | 
|---|
| 101 |  | 
|---|
| 102 | if (!insert) | 
|---|
| 103 | return -1; | 
|---|
| 104 |  | 
|---|
| 105 | query = Form("INSERT %s (f%sName) VALUES (\"%s\");", col, col, name); | 
|---|
| 106 |  | 
|---|
| 107 | if (dummy) | 
|---|
| 108 | { | 
|---|
| 109 | cout << query << endl; | 
|---|
| 110 | return 0; | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | res=serv.Query(query); | 
|---|
| 114 | if (!res) | 
|---|
| 115 | return -1; | 
|---|
| 116 |  | 
|---|
| 117 | delete res; | 
|---|
| 118 |  | 
|---|
| 119 | Int_t key = QueryNameKEY(serv, dummy, col, name, kFALSE); | 
|---|
| 120 | if (key>0) | 
|---|
| 121 | { | 
|---|
| 122 | cout << "New " << col << ": " << name << endl; | 
|---|
| 123 | return key; | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 | cout << "ERROR: " << query << endl; | 
|---|
| 127 | return kFALSE; | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | int insertdataset(TString number, TString source, TString wobble, TString comment, Bool_t dummy=kTRUE) | 
|---|
| 131 | { | 
|---|
| 132 | TEnv env("sql.rc"); | 
|---|
| 133 |  | 
|---|
| 134 | MSQLServer serv(env); | 
|---|
| 135 | if (!serv.IsConnected()) | 
|---|
| 136 | { | 
|---|
| 137 | cout << "ERROR - Connection to database failed." << endl; | 
|---|
| 138 | return 0; | 
|---|
| 139 | } | 
|---|
| 140 | cout << "insertdataset" << endl; | 
|---|
| 141 | cout << "-------------" << endl; | 
|---|
| 142 | cout << endl; | 
|---|
| 143 | cout << "Connected to " << serv.GetName() << endl; | 
|---|
| 144 | cout << endl; | 
|---|
| 145 |  | 
|---|
| 146 | //get source key | 
|---|
| 147 | Int_t sourcekey = QueryNameKEY(serv, dummy, "Source", source.Data(), kFALSE); | 
|---|
| 148 | if (sourcekey<0) | 
|---|
| 149 | { | 
|---|
| 150 | cout << "Error - could not get sourcename from DB -> " << flush; | 
|---|
| 151 | cout << "maybe you have the wrong sourcename in your datasetfile" << endl; | 
|---|
| 152 | return 0; | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | cout << "no:" << number << endl; | 
|---|
| 156 |  | 
|---|
| 157 | //if dataset is not yet in database, insert the information | 
|---|
| 158 | if (!ExistStr(serv, "fDataSetNumber", "DataSets", number.Data())) // Form("%d", number) | 
|---|
| 159 | { | 
|---|
| 160 | TString query=Form("INSERT DataSets SET fDataSetNumber='%s', " | 
|---|
| 161 | " fSourceKEY=%d, fWobble='%s', fComment='%s' ", | 
|---|
| 162 | number.Data(), sourcekey, wobble.Data(), comment.Data()); | 
|---|
| 163 |  | 
|---|
| 164 | if (dummy) | 
|---|
| 165 | { | 
|---|
| 166 | cout << query << endl; | 
|---|
| 167 | return 0; | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | TSQLResult *res = serv.Query(query); | 
|---|
| 171 | if (!res) | 
|---|
| 172 | { | 
|---|
| 173 | cout << "Error - could not insert dataset" << endl; | 
|---|
| 174 | return 0; | 
|---|
| 175 | } | 
|---|
| 176 | delete res; | 
|---|
| 177 |  | 
|---|
| 178 | //add also entry in the table DataSetProcessStatus | 
|---|
| 179 | query=Form("INSERT DataSetProcessStatus SET fDataSetNumber='%s', " | 
|---|
| 180 | " fDataSetInserted=Now() ", | 
|---|
| 181 | number.Data()); | 
|---|
| 182 |  | 
|---|
| 183 | res = serv.Query(query); | 
|---|
| 184 | if (!res) | 
|---|
| 185 | { | 
|---|
| 186 | cout << "Error - could not insert dataset" << endl; | 
|---|
| 187 | return 0; | 
|---|
| 188 | } | 
|---|
| 189 | delete res; | 
|---|
| 190 | } | 
|---|
| 191 | else | 
|---|
| 192 | { | 
|---|
| 193 | cout << number << " already exists... " << endl; | 
|---|
| 194 | return 0; | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|
| 197 | return 1; | 
|---|
| 198 | } | 
|---|
| 199 |  | 
|---|
| 200 |  | 
|---|