16 fDbType(conDb.GetDbType()),
19 fConDb.ConnectStatement();
35 if ( stmt->Process() ) {
return stmt; }
48 std::list<TString>::const_iterator itr(sqlList.begin()), itrEnd(sqlList.end());
50 TSQLStatement* stmt = 0;
51 while (itr != itrEnd) {
52 const TString& sql = *itr++;
53 cout <<
"-I- FairDbStatement SQL:" <<
fConDb.
GetDbName() <<
":" << sql << endl;
56 if ( ! stmt ) {
return 0; }
60 if ( stmt && ! stmt->StoreResult() ) {
77 oss <<
"Unknown failure (no execption but no TSQLStatement either executing " << sql;
91 std::list<TString>::const_iterator itr(sqlList.begin()), itrEnd(sqlList.end());
93 while (itr != itrEnd) {
94 const TString& sql = *itr++;
95 cout <<
"-I- FairDbStatement::ExecuteUpdate SQL:" <<
fConDb.
GetDbName() <<
":" << sql << endl;
110 if ( el.
IsEmpty() ) {
return false; }
161 std::list<TString> sqlTransList;
164 sqlTransList.push_back(sql);
168 Bool_t translated = kFALSE;
170 Bool_t modified = kFALSE;
172 TString sqlTrans(sql);
176 sqlTransList.push_back(
"ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD hh24:mi:ss'");
179 if ( sqlTrans.Index(
"NOW()") != kNPOS ) {
180 sqlTrans.ReplaceAll(
"NOW()",
"SYSDATE");
185 if (sqlTrans ==
"SHOW TABLES" ) {
186 sqlTrans =
"SELECT TABLE_NAME FROM ALL_TABLES";
187 sqlTransList.push_back(sqlTrans);
194 Ssiz_t createTableIndex = sqlTrans.Index(
"CREATE TABLE",0,TString::kIgnoreCase );
195 if ( ! translated && createTableIndex != kNPOS ) {
198 std::vector<std::string> sql_list;
200 std::vector<std::string>::const_iterator itr(sql_list.begin()), itrEnd(sql_list.end());
201 while ( itr != itrEnd ) {
202 sqlTransList.push_back(*itr);
210 Ssiz_t dropTableIndex = sqlTrans.Index(
"DROP TABLE",0,TString::kIgnoreCase );
211 if ( ! translated && dropTableIndex != kNPOS ) {
213 sqlTrans.ReplaceAll(
" IF EXISTS ",
" ");
216 sqlTransList.push_back(sqlTrans);
219 Ssiz_t startIndex = dropTableIndex + 10;
220 while ( isspace(sqlTrans[startIndex]) ) { ++startIndex; }
221 Ssiz_t endIndex = startIndex + 1;
222 Ssiz_t endIndexMax = sqlTrans.Length();
223 while ( endIndex < endIndexMax
224 && (isalnum(sqlTrans[endIndex]) || sqlTrans[endIndex] ==
'_')
226 TString
name(sqlTrans.Data()+startIndex,endIndex-startIndex);
231 TString sqlIncant(
"DROP PUBLIC SYNONYM ");
233 sqlTransList.push_back(sqlIncant);
238 Ssiz_t whereStart = sqlTrans.Index(
"INSERT INTO",0,TString::kIgnoreCase );
239 if ( whereStart == kNPOS ) { whereStart = 0; }
245 Ssiz_t whereStartMax = sqlTrans.Length();
246 whereStart = sqlTrans.Index(
"(",whereStart,TString::kIgnoreCase );
247 if ( whereStart == kNPOS ) { whereStart = whereStartMax; }
248 else { ++whereStart; }
250 while ( whereStart < whereStartMax && level ) {
251 char chr = sqlTrans[whereStart++];
252 if ( chr ==
'(' ) { ++level; }
253 if ( chr ==
')' ) { --level; }
257 Ssiz_t whereIndex = sqlTrans.Index(
" WHERE ",whereStart,TString::kIgnoreCase );
258 if ( ! translated && whereIndex != kNPOS ) {
262 Ssiz_t whereEnd = sqlTrans.Length();
263 std::string whereDelim[] = {
"GROUP BY",
"HAVING",
"ORDER BY",
"LIMIT" };
264 int numDelims =
sizeof(whereDelim)/
sizeof(
string);
265 for (
int iDelim = 0; iDelim < numDelims; ++iDelim ) {
266 const string& delimName = whereDelim[iDelim];
267 Ssiz_t delimIndex = sqlTrans.Index(delimName.c_str(), delimName.size(), whereIndex + 5, TString::kIgnoreCase );
268 if (delimIndex != kNPOS && delimIndex < whereEnd) { whereEnd = delimIndex; }
273 TString whereClause(sql.Data()+whereIndex,whereEnd-whereIndex);
275 whereClause.ReplaceAll(
"\n",
" ");
277 std::vector<std::string> tokens;
279 int numTokens = tokens.size();
280 for (
int ithToken = 1; ithToken < numTokens-1; ++ithToken ) {
281 if ( tokens[ithToken] ==
"&" ) {
282 string tmp(
"bitand(");
283 tmp += tokens[ithToken-1] +
"," + tokens[ithToken+1] +
") != 0";
284 tokens[ithToken] = tmp;
285 tokens[ithToken-1] =
"";
286 tokens[ithToken+1] =
"";
290 sqlTrans = sql(0,whereIndex);
291 for (
int ithToken = 0; ithToken < numTokens; ++ithToken ) {
293 sqlTrans += tokens[ithToken].c_str();
295 sqlTrans +=
" " + sql(whereEnd,999999);
300 if ( ! translated && sqlTrans.Index(
"\\\'") != kNPOS ) {
303 if ( not modified ) { sqlTrans = sql; }
304 sqlTrans.ReplaceAll(
"\\\'",
"\'\'");
308 if ( modified && ! translated ) {
309 sqlTransList.push_back(sqlTrans);
314 cout <<
"-I- FairDbStatement::TranslateSQL sql: " << sql << endl
315 <<
"translates to " << sqlTransList.size()
316 <<
" statements:- \n";
317 std::list<TString>::const_iterator itr(sqlTransList.begin()), itrEnd(sqlTransList.end());
318 while (itr != itrEnd) { cout <<
"-I- FairDbStatement::TranslateSQL " <<
" " << *itr << endl; ++itr;}
320 sqlTransList.push_back(sql);