| 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 | // setstatus.C | 
|---|
| 28 | // =========== | 
|---|
| 29 | // | 
|---|
| 30 | // This macro is a key part of the automation concept. | 
|---|
| 31 | // It sets the status for dates/runs/sequences/datasets in the database, if a | 
|---|
| 32 | // certain step has been done. | 
|---|
| 33 | // | 
|---|
| 34 | // Usage: | 
|---|
| 35 | //  .x setstatus.C+("primary","table","column","statustime","returncode","failedcode","failedcodeadd","starttime","failedtime",kTRUE) | 
|---|
| 36 | // The first argument is the primary (date/run/sequence/dataset), the second | 
|---|
| 37 | // and third argument give the table and column of the step. The fourth | 
|---|
| 38 | // column is giving the statustime, to which the column has to be set. The | 
|---|
| 39 | // fifth argument is giving the error code (in case of failure), the sixth | 
|---|
| 40 | // an error comment, the seventh the starttime and the eigth the stoptime | 
|---|
| 41 | // (only in case of failure). | 
|---|
| 42 | // The last argument is indicating if the columns of all influenced steps | 
|---|
| 43 | // shall be reset to NULL. The default is kTRUE, which means, that the | 
|---|
| 44 | // columns are reset. | 
|---|
| 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 <MTime.h> | 
|---|
| 60 |  | 
|---|
| 61 | #include <MSQLServer.h> | 
|---|
| 62 | #include <TSQLRow.h> | 
|---|
| 63 | #include <TSQLResult.h> | 
|---|
| 64 |  | 
|---|
| 65 | using namespace std; | 
|---|
| 66 |  | 
|---|
| 67 |  | 
|---|
| 68 | Bool_t CheckReset(TEnv &rc, TString table, TString column, TString statustime) | 
|---|
| 69 | { | 
|---|
| 70 | if (statustime!="NULL") | 
|---|
| 71 | { | 
|---|
| 72 | cout << "everything ok - no reset needed" << endl; | 
|---|
| 73 | return kTRUE; | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | //some columns can't be reseted, this is given in steps.rc | 
|---|
| 77 | TString reset=rc.GetValue(table+"."+column+".Reset", "no"); | 
|---|
| 78 | if (reset.Contains("no")) | 
|---|
| 79 | { | 
|---|
| 80 | cout << "you can't reset " << column << "." << endl; | 
|---|
| 81 | return kFALSE; | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | cout << "reset is possible" << endl; | 
|---|
| 85 | return kTRUE; | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | TString CheckDefault(MSQLServer &serv, TEnv &rc, TString primary, TString table, TString influence) | 
|---|
| 89 | { | 
|---|
| 90 | //check if the value in the database is the default | 
|---|
| 91 | TString query(Form("SELECT %s FROM %s WHERE %s=%s", | 
|---|
| 92 | influence.Data(), table.Data(), | 
|---|
| 93 | rc.GetValue(table+".Primary", ""), primary.Data())); | 
|---|
| 94 |  | 
|---|
| 95 | TSQLResult *res = serv.Query(query); | 
|---|
| 96 | if (!res) | 
|---|
| 97 | cout << "Error - no run to check" << endl; | 
|---|
| 98 |  | 
|---|
| 99 | MTime t, t0; | 
|---|
| 100 | //set time t to 'no-version-value', | 
|---|
| 101 | //which is in this case equivalent to NULL | 
|---|
| 102 | t.Set(1970,1,1,0,0,0); | 
|---|
| 103 | t0.Set(1970,1,1,0,0,0); | 
|---|
| 104 |  | 
|---|
| 105 | TSQLRow *row=0; | 
|---|
| 106 | while ((row = res->Next())) | 
|---|
| 107 | { | 
|---|
| 108 | if (row && (*row)[0]) | 
|---|
| 109 | t.SetSqlDateTime((*row)[0]); | 
|---|
| 110 |  | 
|---|
| 111 | cout << "t : " << t  << endl; | 
|---|
| 112 | cout << "t0: " << t0 << endl; | 
|---|
| 113 |  | 
|---|
| 114 | return t()-t0()>0 ? "no" : "yes"; | 
|---|
| 115 | } | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | Int_t SetInfluences(MSQLServer &serv, TEnv &rc, TString primary, TString table, TString column, TString statustime, TString returncode, TString failedcode, TString failedcodeadd, TString starttime, TString failedtime, Bool_t resetall) | 
|---|
| 119 | { | 
|---|
| 120 | cout << "set influences for " << table << "." << column << endl; | 
|---|
| 121 |  | 
|---|
| 122 | //build query and set step and influences for the step | 
|---|
| 123 | TString influences  = rc.GetValue(table+"."+column+".Influences", ""); | 
|---|
| 124 |  | 
|---|
| 125 | TString query(Form("UPDATE %s SET %s=%s, fReturnCode=%s, fFailedCode=%s, fFailedCodeAdd=%s, fFailedTime=%s ", | 
|---|
| 126 | table.Data(), column.Data(), statustime.Data(), returncode.Data(), failedcode.Data(), | 
|---|
| 127 | failedcodeadd.Data(), failedtime.Data())); | 
|---|
| 128 | if (starttime.CompareTo("noreset")) | 
|---|
| 129 | query += Form(", fStartTime=%s", starttime.Data()); | 
|---|
| 130 |  | 
|---|
| 131 | while (!influences.IsNull() && resetall) | 
|---|
| 132 | { | 
|---|
| 133 | influences = influences.Strip(TString::kBoth); | 
|---|
| 134 |  | 
|---|
| 135 | Int_t idx = influences.First(' '); | 
|---|
| 136 | if (idx<0) | 
|---|
| 137 | idx = influences.Length(); | 
|---|
| 138 |  | 
|---|
| 139 | TString influence = influences(0, idx); | 
|---|
| 140 | influences.Remove(0, idx); | 
|---|
| 141 |  | 
|---|
| 142 | TString deflt  = rc.GetValue(influence+".Default", ""); | 
|---|
| 143 |  | 
|---|
| 144 | //for some columns there may be a different default | 
|---|
| 145 | // in the file steps.rc they are marked with Default: check | 
|---|
| 146 | // in this case, the value in the database has to be checked | 
|---|
| 147 | if (deflt=="check") | 
|---|
| 148 | deflt=CheckDefault(serv, rc, primary, table, influence); | 
|---|
| 149 | //if it is the default value (1970-01-01 00:00:00), nothing is inserted into the database | 
|---|
| 150 | if (deflt=="yes") | 
|---|
| 151 | continue; | 
|---|
| 152 |  | 
|---|
| 153 | query+=Form(", %s=NULL", influence.Data()); | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | query+=Form(" WHERE %s='%s'", rc.GetValue(table+".Primary", ""), primary.Data()); | 
|---|
| 157 |  | 
|---|
| 158 | TSQLResult *res = serv.Query(query); | 
|---|
| 159 | if (!res) | 
|---|
| 160 | return 0; | 
|---|
| 161 |  | 
|---|
| 162 | return 1; | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 165 | int setstatus(TString primary, TString table, TString column, TString statustime, TString returncode, TString failedcode, TString failedcodeadd, TString starttime, TString failedtime, Bool_t resetall=kTRUE) | 
|---|
| 166 | { | 
|---|
| 167 | TEnv env("sql.rc"); | 
|---|
| 168 |  | 
|---|
| 169 | MSQLServer serv(env); | 
|---|
| 170 | if (!serv.IsConnected()) | 
|---|
| 171 | { | 
|---|
| 172 | cout << "ERROR - Connection to database failed." << endl; | 
|---|
| 173 | return 0; | 
|---|
| 174 | } | 
|---|
| 175 | cout << "setstatus" << endl; | 
|---|
| 176 | cout << "---------" << endl; | 
|---|
| 177 | cout << endl; | 
|---|
| 178 | cout << "Connected to " << serv.GetName() << endl; | 
|---|
| 179 | cout << endl; | 
|---|
| 180 |  | 
|---|
| 181 | TEnv rc("steps.rc"); | 
|---|
| 182 |  | 
|---|
| 183 | //if reset is needed and/or can be done, set new values | 
|---|
| 184 | return !CheckReset(rc, table, column, statustime) ? 0 : SetInfluences(serv, rc, primary, table, column, statustime, returncode, failedcode, failedcodeadd, starttime, failedtime, resetall); | 
|---|
| 185 |  | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|