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 MyMagic.%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 MyMagic.%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());
|
---|
124 | if (sourcekey<0)
|
---|
125 | {
|
---|
126 | cout << "Error - could not get sourcename from DB" << endl;
|
---|
127 | return 0;
|
---|
128 | }
|
---|
129 |
|
---|
130 | cout << "no:" << number << endl;
|
---|
131 |
|
---|
132 | if (!ExistStr(serv, "fDataSetNumber", "MyMagic.DataSets", number.Data())) // Form("%d", number)
|
---|
133 | {
|
---|
134 | TString query=Form("INSERT MyMagic.DataSets SET fDataSetNumber='%s', "
|
---|
135 | " fSourceKEY=%d, fWobble='%s' ",
|
---|
136 | number.Data(), sourcekey, wobble.Data());
|
---|
137 |
|
---|
138 | if (dummy)
|
---|
139 | {
|
---|
140 | cout << query << endl;
|
---|
141 | return 0;
|
---|
142 | }
|
---|
143 |
|
---|
144 | TSQLResult *res = serv.Query(query);
|
---|
145 | if (!res)
|
---|
146 | {
|
---|
147 | cout << "Error - could not insert dataset" << endl;
|
---|
148 | return 0;
|
---|
149 | }
|
---|
150 | delete res;
|
---|
151 |
|
---|
152 |
|
---|
153 | query=Form("INSERT MyMagic.DataSetProcessStatus SET fDataSetNumber='%s', "
|
---|
154 | " fDataSetInserted=Now() ",
|
---|
155 | number.Data());
|
---|
156 |
|
---|
157 | res = serv.Query(query);
|
---|
158 | if (!res)
|
---|
159 | {
|
---|
160 | cout << "Error - could not insert dataset" << endl;
|
---|
161 | return 0;
|
---|
162 | }
|
---|
163 | delete res;
|
---|
164 | }
|
---|
165 | else
|
---|
166 | {
|
---|
167 | cout << number << " already exists... " << endl;
|
---|
168 | return 0;
|
---|
169 | }
|
---|
170 |
|
---|
171 | return 1;
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|