-
Notifications
You must be signed in to change notification settings - Fork 9
/
cxdatabase_oci.cpp
524 lines (447 loc) · 16.9 KB
/
cxdatabase_oci.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#include "cxdatabase.h"
#include "cxstring.h"
#include "cxcontainer.h"
#include "cxapplication.h"
#include "cxfile.h"
#include "cxinterinfo.h"
//#define OTL_ORA8I
#define OTL_ORA11G
#define OTL_STL // Compile OTL 4/ODBC
//#define OTL_ODBC // Compile OTL 4/ODBC
//#undef UNICODE
//#undef _UNICODE
#include "otl/otlv4.h" // include the OTL 4 header file
using namespace std;
struct OciColumnDesc
{
std::string columnName;
int ftype;
int fsize;
};
OciColumnDesc emptyOciColumnDesc;
class CxDatabaseOci : public CxDatabase
{
public:
static bool isMyDatabase(const std::string & sConnectSource)
{
return CxString::containCase(sConnectSource, "@");
}
static CxDatabase * createDatabase(void)
{
return new CxDatabaseOci();
}
private:
otl_connect _db;
string _sqlTableInfo;
public:
CxDatabaseOci()
{
}
~CxDatabaseOci()
{
if (_db.connected)
{
closeDatabaseImpl();
}
}
protected:
bool openDatabaseImpl(const std::string & sConnectSource, bool bCreate, const std::map<std::string, std::string> * oParams)
{
otl_connect::otl_initialize(); // initialize ODBC environment
cxDebug()<<"CxDatabaseOci->rlogon begin"<<_db.connected;
// _db.logoff();
if(!_db.connected)
{
try
{
cxDebug()<<"CxDatabaseOci->rlogon begin : "<<sConnectSource;
_db.rlogon(sConnectSource.c_str(),1); // connect to ODBC
bool bOk = execSqlImpl("SELECT user() LIMIT 1");
_sqlTableInfo = bOk ? "SELECT * FROM %s LIMIT 1;" : "SELECT TOP 1 * FROM %s;";
}
catch(otl_exception& p)
{ // intercept OTL exceptions
cxDebug()<<p.msg<<p.stm_text<<p.sqlstate<<p.var_info;
return -1;
}
}
cxDebug()<<"CxDatabaseOci->rlogon end"<<_db.connected;
return _db.connected;
}
void closeDatabaseImpl()
{
if(_db.connected)
{
_db.logoff(); // disconnect from Oracle
}
}
bool isOpenImpl() const
{
return _db.connected;
}
bool createTableImpl(const std::string & sTableName, const std::map<std::string, std::string> & columns)
{
string sSql = CxString::format("CREATE TABLE [%s] (", sTableName.c_str());
int iCount = 0;
for(std::map<string,string>::const_iterator it = columns.begin(); it != columns.end(); ++it)
{
string sColumn = "[" + it->first + "] " + it->second + ",";
sSql += sColumn;
++ iCount;
}
if (iCount > 0)
{
char * pch = (char *)sSql.data();
pch[sSql.size()-1] = ')';
return execSql(sSql);
}
return true;
}
bool alterPrimaryKeyAddImpl(const std::string & sTableName, const std::vector<std::string> & columns)
{
string sSql = CxString::format("ALTER TABLE ADD CONSTRAINT %s PRIMARY KEY (" , sTableName.c_str());
int iCount = 0;
for (size_t i = 0; i < columns.size(); ++i)
{
sSql += columns.at(i) + " ,";
++ iCount;
}
if (iCount > 0)
{
char * pch = (char *)sSql.data();
pch[sSql.size()-1] = ')';
return execSql(sSql);
}
return false;
}
int loadTableImpl(const std::string & sTableName, std::vector<std::vector<std::string> > & rows, std::vector<std::string> * oColumnNames = NULL, int iMaxRowCount = -1)
{
string sSql = "SELECT ";
if (oColumnNames && oColumnNames->size() > 0)
{
int iCount = 0;
for (size_t i = 0; i < oColumnNames->size(); ++i)
{
sSql += oColumnNames->at(i) + " ,";
++ iCount;
}
sSql.resize(sSql.size()-1);
}
else
{
sSql += "* ";
}
sSql += "FROM " + sTableName;
return loadSqlImpl(sSql, rows, oColumnNames);
}
int saveTableImpl(const std::string & sTableName, const std::vector<std::string> & columnNames, const std::vector<std::vector<std::string> > & rows, const std::vector<int> & columnTypes = std::vector<int>(), bool bTransaction = false)
{
if (! _db.connected)
{
string sErrorString = "error : db is not open sql!";
setLastError(-1, sErrorString);
return -1;
}
std::vector<OciColumnDesc> columnDescs;
getColumnDescs(sTableName, columnDescs);
// bool bBlod = false;
// map<string, string> columnTypeNames;
// for (size_t i = 0; i < columnNames.size(); ++i)
// {
// const string & sColumnName = columnNames.at(i);
// int ftype = CxString::findValueCase(ftypes, sColumnName, ci_int_zero);
// if (ftype == 0 && columnTypes.size()>=i)
// ftype = columnTypes.at(i);
// if (ftype == 0)
// return -2;
// string sTypeName = otl_var_type_name(ftype);
// if (sTypeName.empty())
// return -3;
// columnTypeNames[sColumnName] = sTypeName;
//// if (ftype == otl_var_blob || ftype == otl_var_clob)
// }
try{
cxDebug()<<"CxDatabaseOci->saveTableImpl begin : "<<connectSource();
string sSql = CxString::format("insert into %s ", sTableName.c_str());
string sColumnes = "(";
string sValues = "(";
int iCount = 0;
vector<int> columnTypes2;
for (size_t i = 0; i < columnNames.size(); ++i)
{
const string & sColumnName = columnNames.at(i);
OciColumnDesc columnDesc = findColumnDesc(columnDescs, sColumnName);
if (columnDesc.ftype == 0 && columnTypes.size()>=i)
columnDesc.ftype = columnTypes.at(i);
if (columnDesc.ftype < 1 || columnDesc.ftype > 29) // otl_var_char otl_var_bdouble
return -2;
columnTypes2.push_back(columnDesc.ftype);
sColumnes += sColumnName + " ,";
if (columnDesc.ftype == otl_var_char)
sValues += CxString::format(":%s<%s[%d]> ,", sColumnName.c_str(), otl_var_type_name(columnDesc.ftype), columnDesc.fsize);
else
sValues += CxString::format(":%s<%s> ,", sColumnName.c_str(), otl_var_type_name(columnDesc.ftype));
++ iCount;
}
if (iCount > 0)
{
char * pch = (char *)sColumnes.data();
pch[sColumnes.size()-1] = ')';
pch = (char *)sValues.data();
pch[sValues.size()-1] = ')';
}
sSql = sSql + sColumnes + " VALUES " + sValues + " ;";
// sSql = "insert into t1 (f1 ,f2 ,f3 ,f4 ,f5 ,f6 ,f7 ) VALUES (:f1<SHORT INT> ,:f2<INT> ,:f3<DOUBLE> ,:f4<DOUBLE> ,:f5<CHAR[31]> ,:f6<VARCHAR LONG> ,:f7<RAW LONG> )";
// cout<<"CxDatabaseOci->saveTableImpl sql : "<<sSql;
// referto : ex702_odbc.cpp
otl_stream o;
o.open(1, // buffer size has to be set to 1 for operations with LONGTEXTs
// "insert into test_tab values(:f1<int>,:f2<varchar_long>)",
sSql.c_str(),
// SQL statement
_db // connect object
);
if (bTransaction)
o.set_commit(0); // setting stream "auto-commit" to "off". It is required
for (size_t i = 0; i < rows.size(); ++i)
{
const vector<string> & row = rows.at(i);
if (row.size()<columnTypes2.size())
continue;
for (size_t j = 0; j < row.size(); ++j)
{
const string & sValue = row.at(j);
int iFtype = columnTypes2.at(j);
switch (iFtype)
{
case otl_var_short:
o << (short)CxString::toInt32(sValue);
break;
case otl_var_int:
case otl_var_unsigned_int:
o << CxString::toInt32(sValue);
break;
case otl_var_long_int:
case otl_var_bigint:
case otl_var_ubigint:
o << (long int)CxString::toInt64(sValue);
break;
case otl_var_float:
case otl_var_bfloat:
o << CxString::toFloat(sValue);
break;
case otl_var_double:
case otl_var_bdouble:
o << CxString::toDouble(sValue);
break;
case otl_var_raw:
case otl_var_char:
case otl_var_varchar_long:
case otl_var_raw_long:
case otl_var_blob:
case otl_var_clob:
o << sValue;
break;
default:
o << sValue;
}
}
}
if (bTransaction)
_db.commit(); // committing transaction.
}
catch(otl_exception& p){ // intercept OTL exceptions
cxDebug()<<p.msg<<p.stm_text<<p.sqlstate<<p.var_info;
return -9;
}
cxDebug()<<"CxDatabaseOci->saveTableImpl end"<<_db.connected;
return rows.size();
}
int execSqlImpl(const string &sSql)
{
int iResult = FALSE;
if(_db.connected)
{
try{
otl_stream o(1,sSql.c_str(),_db); // buffer size , SQL statement,connect object
iResult = TRUE;
}
catch(otl_exception& p)
{ // intercept OTL exceptions
cxDebug()<<"oci execSqlImpl : "<<p.msg<<p.stm_text<<p.sqlstate<<p.var_info<<cxEndLine;
}
}
return iResult;
}
int loadSqlImpl(const std::string & sSql, std::vector<std::vector<std::string> > & rows, std::vector<std::string> * oColumnNames = NULL, int iMaxRowCount = -1)
{
if (! _db.connected)
{
string sErrorString = "error : db is not open sql!";
setLastError(-1, sErrorString);
return -1;
}
vector<OciColumnDesc> columnDescs;
try
{
cxDebug()<<"CxDatabaseOci->saveTableImpl begin : "<<connectSource();
otl_stream o(1, // buffer size has to be set to 1 for operations with LONGTEXTs
// "insert into test_tab values(:f1<int>,:f2<varchar_long>)",
sSql.c_str(),
// SQL statement
_db // connect object
);
otl_column_desc* desc;
int desc_len;
desc=o.describe_select(desc_len);
OciColumnDesc columnDesc;
if (oColumnNames)
oColumnNames->clear();
for(int i=0;i<desc_len;++i)
{
if (oColumnNames)
oColumnNames->push_back(string(desc[i].name));
columnDesc.columnName = desc[i].name;
columnDesc.ftype = desc[i].otl_var_dbtype;
columnDesc.fsize = desc[i].dbsize;
columnDescs.push_back(columnDesc);
}
if (columnDescs.empty())
return -2;
while(!o.eof())
{
std::vector<std::string> row;
for(size_t i=0;i<columnDescs.size();i++)
{
OciColumnDesc & columnDesc = columnDescs.at(i);
switch (columnDesc.ftype)
{
case otl_var_short:
{
short value;
o >> value;
row.push_back(CxString::toString(value));
}
break;
case otl_var_int:
case otl_var_unsigned_int:
{
int value;
o >> value;
row.push_back(CxString::toString(value));
}
break;
case otl_var_long_int:
case otl_var_bigint:
case otl_var_ubigint:
{
long int value;
o >> value;
row.push_back(CxString::toString(value));
}
break;
case otl_var_float:
case otl_var_bfloat:
{
float value;
o >> value;
row.push_back(CxString::toString(value));
}
break;
case otl_var_double:
case otl_var_bdouble:
{
double value;
o >> value;
row.push_back(CxString::toString(value));
}
break;
case otl_var_raw:
case otl_var_char:
case otl_var_varchar_long:
case otl_var_raw_long:
case otl_var_blob:
case otl_var_clob:
{
string value;
o >> value;
row.push_back(CxString::toString(value));
}
break;
default:
{
string value;
o >> value;
row.push_back(CxString::toString(value));
}
}
}
rows.push_back(row);
if (rows.size() > iMaxRowCount)
break;
}
}
catch(otl_exception& p)
{ // intercept OTL exceptions
cxDebug()<<p.msg<<p.stm_text<<p.sqlstate<<p.var_info;
return -9;
}
cxDebug()<<"CxDatabaseOci->loadSqlImpl end "<<rows.size();
return rows.size();
}
private:
void getColumnDescs(const std::string &sTableName, std::vector<OciColumnDesc> & fieldTypes)
{
if(_db.connected)
{
string sSql = CxString::format(_sqlTableInfo.c_str(), sTableName.c_str());
try{
otl_stream o(1,sSql.c_str(),_db); // buffer size , SQL statement,connect object
otl_column_desc* desc;
int desc_len;
desc=o.describe_select(desc_len);
OciColumnDesc columnDesc;
for(int i=0;i<desc_len;++i)
{
columnDesc.columnName = string(desc[i].name);
columnDesc.ftype = desc[i].otl_var_dbtype;
columnDesc.fsize = desc[i].dbsize;
// cout<<"========== COLUMN #"<<i+1<<" ==========="<<endl;
// cout<<"name="<<desc[i].name<<endl;
// cout<<"dbtype="<<desc[i].dbtype<<endl;
// cout<<"otl_var_dbtype="<<desc[i].otl_var_dbtype<<endl;
// cout<<"dbsize="<<desc[i].dbsize<<endl;
// cout<<"scale="<<desc[i].scale<<endl;
// cout<<"prec="<<desc[i].prec<<endl;
// cout<<"nullok="<<desc[i].nullok<<endl;
fieldTypes.push_back(columnDesc);
// otl_var_type_name(columnDesc.ftype);
}
}
catch(otl_exception& p)
{ // intercept OTL exceptions
cxDebug()<<"oci getFieldTypes : "<<p.msg<<p.stm_text<<p.sqlstate<<p.var_info<<cxEndLine;
}
}
}
OciColumnDesc findColumnDesc(const vector<OciColumnDesc> & columnDescs, const std::string & sColumnName)
{
for (size_t i = 0; i < columnDescs.size(); ++i)
{
const OciColumnDesc & columnDesc = columnDescs.at(i);
if (CxString::equalCase(columnDesc.columnName, sColumnName))
return columnDesc;
}
return emptyOciColumnDesc;
}
};
CxDatabaseOci * fn_oDatabaseOci()
{
emptyOciColumnDesc.columnName.clear();
emptyOciColumnDesc.fsize = 0;
emptyOciColumnDesc.ftype = 0;
CxDatabaseManager::registDatabaseConstructor(CxDatabaseOci::isMyDatabase, CxDatabaseOci::createDatabase);
static CxDatabaseOci m;
return & m;
}
static CxDatabaseOci * f_oDatabaseOci = fn_oDatabaseOci();