Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TSQLImporter.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file TSQLImporter.cxx
1 // $Id: TSQLImporter.cxx,v 1.1.1.1 2004/02/18 20:58:02 dave Exp $
2 //*-- Author : Valeriy Onuchin 20/03/2001
3 //
4 
6 //
7 // TSQLImporter
8 //
10 #include <RDBC/TSQLImporter.h>
11 #include <RDBC/TSQLImportClient.h>
12 #include <RDBC/TSQLConnection.h>
13 #include <RDBC/TSQLUrl.h>
14 #include <RDBC/TSQLStatement.h>
15 #include <RDBC/TSQLResultSet.h>
16 #include <RDBC/TSQLDriverManager.h>
17 
20 //___________________________________________________________________
21 TSQLImporter::TSQLImporter():TQObject()
22 {
23  // ctor
24 
25  fConnection = 0;
26  fClient = 0;
27  fStatus = 400; // not OK
28 }
29 
30 //___________________________________________________________________
31 void TSQLImporter::LoadTable(const TString& url)
32 {
33  //
34 
35  fStatus = 400;
36  TSQLStatement* stmt = 0;
37 
38  fClient = new TSQLImportClient(url);
39 
40  // catch exceptions from client and re-emmit exception
41  fClient->Init();
42 
43  if(!fClient->IsValid()) { // failed to create client
46  if(e) {
47  // Create a copy of the exception; the owning client is about to be destroyed.
48  TSQLException* clientException = new TSQLException();
49  *clientException = *e;
50  Throw(clientException);
51  }
52  if(fClient) delete fClient;
53  fClient = 0;
54  return;
55  }
56 
57  TString cols = fClient->GetColumns(); // read column names,types
58  TString table = fClient->GetTableName();
59  TString file = fClient->GetLocalFile();
60 
61  TString query;
62  TString query1;
63  Int_t update_count;
64 
65  stmt = fConnection->CreateStatement();
66 
67  if(!stmt) {
68  Throw(new TSQLException("Failed to create SQL statement","",HTTP_NOT_ACCEPTABLE));
69  if(fClient) delete fClient;
70  Destroyed();
71  fClient = 0;
73  return;
74  }
75 
76  query1 = "CREATE TEMPORARY TABLE ";
77  query1 += table + "(" + cols + ")";
78 
79  update_count = stmt->ExecuteUpdate(query1);
80 
81  query = "LOAD DATA ";
82  query += fClient->GetLocal() + " INFILE '";
83  query += file;
84  query += "' INTO TABLE ";
85  query += table;
86  query += " FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '";
87  query += '\"';
88  query += "'";
89 // query += " ESCAPED BY '\\'";
90 // query += " LINES TERMINATED BY '\n'";
91 
92  if(fClient->GetSkipLines()) {
93  query += " IGNORE ";
94  query += Form("%d LINES",fClient->GetSkipLines());
95  }
96 
97  update_count = stmt->ExecuteUpdate(query);
98 
99  if(!update_count) { //
100  Throw(new TSQLException(Form("Wrong format:\n%s\n%s",query1.Data(),query.Data()),"",HTTP_NOT_ACCEPTABLE));
101  Destroyed();
102  SafeDelete(fClient);
104  }
105 
106  fStatus = HTTP_OK;
107  if(stmt) delete stmt;
108  return;
109 }
110 
111 //___________________________________________________________________
112 void TSQLImporter::LoadCatalog(const TString& url)
113 {
114  //
115 
116  LoadTable(url); // load
117 
118  if( (fStatus!=HTTP_OK) || !fClient ) {
119  return;
120  }
121 
123  TString table = fClient->GetTableName();
124  TString query;
125 
126 // query = "ALTER TABLE " + table;
127 // query += " ADD table_name varchar(64)";
128 
129  query = "SELECT * FROM " + table;
130 
131  TSQLResultSet* rs = stmt->ExecuteQuery(query);
132 
133  if(!rs) {
135  return;
136  }
137 
138  if(fClient) {
139  delete fClient;
140  fClient = 0;
141  }
142 
143  while(rs->Next()) {
144  table = rs->GetString(1); // first column is URL/file
145  LoadTable(table);
146  }
147 
148  if(fClient) {
149  delete fClient;
150  fClient = 0;
151  }
152 
153  fStatus = HTTP_OK;
154  if(stmt) delete stmt;
155  return;
156 }
157 
158 //___________________________________________________________________
160 {
161  // import data from url to con
162 
163  fStatus = 400;
164 
165  if(!con) {
166  fConnection = 0;
167  //Throw(new TSQLException("Connection not defined","",HTTP_FORBIDDEN));
168  return fStatus = HTTP_FORBIDDEN;
169  }
170 
171  fConnection = con;
172  TString ext = strrchr(url.Data(),'.'); // get file extention
173 
174  if( (ext==".cat") || (ext==".db") ) LoadCatalog(url);
175  else LoadTable(url);
176 
177  return fStatus = HTTP_OK;
178 }
179 
180 //___________________________________________________________________
181 Bool_t TSQLImporter::IsValid() const
182 {
183  //
184 
185  return (fStatus < 400);
186 }
187 
188 //___________________________________________________________________
190 {
191  // dtor
192 
193  SafeDelete(fClient);
194 }
195