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-2008
|
---|
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 | #include <iostream>
|
---|
53 | #include <iomanip>
|
---|
54 | #include <fstream>
|
---|
55 |
|
---|
56 | #include <TEnv.h>
|
---|
57 | #include <TSQLRow.h>
|
---|
58 | #include <TSQLResult.h>
|
---|
59 |
|
---|
60 | #include "MTime.h"
|
---|
61 | #include "MSQLServer.h"
|
---|
62 |
|
---|
63 | using namespace std;
|
---|
64 |
|
---|
65 |
|
---|
66 | Bool_t CheckReset(TEnv &rc, TString table, TString column, TString statustime)
|
---|
67 | {
|
---|
68 | if (statustime!="NULL")
|
---|
69 | {
|
---|
70 | cout << "everything ok - no reset needed" << endl;
|
---|
71 | return kTRUE;
|
---|
72 | }
|
---|
73 |
|
---|
74 | //some columns can't be reseted, this is given in steps.rc
|
---|
75 | TString reset=rc.GetValue(table+"."+column+".Reset", "no");
|
---|
76 | if (reset.Contains("no"))
|
---|
77 | {
|
---|
78 | cout << "you can't reset " << column << "." << endl;
|
---|
79 | return kFALSE;
|
---|
80 | }
|
---|
81 |
|
---|
82 | cout << "reset is possible" << endl;
|
---|
83 | return kTRUE;
|
---|
84 | }
|
---|
85 |
|
---|
86 | TString CheckDefault(MSQLServer &serv, TEnv &rc, TString primary, TString table, TString influence)
|
---|
87 | {
|
---|
88 | //check if the value in the database is the default
|
---|
89 | TString query(Form("SELECT %s FROM %s WHERE %s=%s",
|
---|
90 | influence.Data(), table.Data(),
|
---|
91 | rc.GetValue(table+".Primary", ""), primary.Data()));
|
---|
92 |
|
---|
93 | TSQLResult *res = serv.Query(query);
|
---|
94 | if (!res)
|
---|
95 | cout << "Error - no run to check" << endl;
|
---|
96 |
|
---|
97 | MTime t, t0;
|
---|
98 | //set time t to 'no-version-value',
|
---|
99 | //which is in this case equivalent to NULL
|
---|
100 | t.Set(1970,1,1,0,0,0);
|
---|
101 | t0.Set(1970,1,1,0,0,0);
|
---|
102 |
|
---|
103 | TSQLRow *row=0;
|
---|
104 | while ((row = res->Next()))
|
---|
105 | {
|
---|
106 | if (row && (*row)[0])
|
---|
107 | t.SetSqlDateTime((*row)[0]);
|
---|
108 |
|
---|
109 | cout << "t : " << t << endl;
|
---|
110 | cout << "t0: " << t0 << endl;
|
---|
111 |
|
---|
112 | return t()-t0()>0 ? "no" : "yes";
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | 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)
|
---|
117 | {
|
---|
118 | cout << "set influences for " << table << "." << column << endl;
|
---|
119 |
|
---|
120 | //build query and set step and influences for the step
|
---|
121 | TString influences = rc.GetValue(table+"."+column+".Influences", "");
|
---|
122 |
|
---|
123 | TString query(Form("UPDATE %s SET %s=%s, fReturnCode=%s, fFailedCode=%s, fFailedCodeAdd=%s, fFailedTime=%s ",
|
---|
124 | table.Data(), column.Data(), statustime.Data(), returncode.Data(), failedcode.Data(),
|
---|
125 | failedcodeadd.Data(), failedtime.Data()));
|
---|
126 | if (starttime.CompareTo("noreset"))
|
---|
127 | query += Form(", fStartTime=%s", starttime.Data());
|
---|
128 |
|
---|
129 | while (!influences.IsNull() && resetall)
|
---|
130 | {
|
---|
131 | influences = influences.Strip(TString::kBoth);
|
---|
132 |
|
---|
133 | Int_t idx = influences.First(' ');
|
---|
134 | if (idx<0)
|
---|
135 | idx = influences.Length();
|
---|
136 |
|
---|
137 | TString influence = influences(0, idx);
|
---|
138 | influences.Remove(0, idx);
|
---|
139 |
|
---|
140 | TString deflt = rc.GetValue(influence+".Default", "");
|
---|
141 |
|
---|
142 | //for some columns there may be a different default
|
---|
143 | // in the file steps.rc they are marked with Default: check
|
---|
144 | // in this case, the value in the database has to be checked
|
---|
145 | if (deflt=="check")
|
---|
146 | deflt=CheckDefault(serv, rc, primary, table, influence);
|
---|
147 | //if it is the default value (1970-01-01 00:00:00), nothing is inserted into the database
|
---|
148 | if (deflt=="yes")
|
---|
149 | continue;
|
---|
150 |
|
---|
151 | query+=Form(", %s=NULL", influence.Data());
|
---|
152 | }
|
---|
153 |
|
---|
154 | query+=Form(" WHERE %s='%s'", rc.GetValue(table+".Primary", ""), primary.Data());
|
---|
155 |
|
---|
156 | TSQLResult *res = serv.Query(query);
|
---|
157 | if (!res)
|
---|
158 | return 0;
|
---|
159 |
|
---|
160 | return 1;
|
---|
161 | }
|
---|
162 |
|
---|
163 | int setstatus(TString primary, TString table, TString column, TString statustime, TString returncode, TString failedcode, TString failedcodeadd, TString starttime, TString failedtime, Bool_t resetall=kTRUE)
|
---|
164 | {
|
---|
165 | TEnv env("sql.rc");
|
---|
166 |
|
---|
167 | MSQLServer serv(env);
|
---|
168 | if (!serv.IsConnected())
|
---|
169 | {
|
---|
170 | cout << "ERROR - Connection to database failed." << endl;
|
---|
171 | return 0;
|
---|
172 | }
|
---|
173 | cout << "setstatus" << endl;
|
---|
174 | cout << "---------" << endl;
|
---|
175 | cout << endl;
|
---|
176 | cout << "Connected to " << serv.GetName() << endl;
|
---|
177 | cout << endl;
|
---|
178 |
|
---|
179 | TEnv rc("steps.rc");
|
---|
180 |
|
---|
181 | //if reset is needed and/or can be done, set new values
|
---|
182 | return !CheckReset(rc, table, column, statustime) ? 0 : SetInfluences(serv, rc, primary, table, column, statustime, returncode, failedcode, failedcodeadd, starttime, failedtime, resetall);
|
---|
183 |
|
---|
184 | }
|
---|
185 |
|
---|