forked from pocoproject/poco
/
SQLExecutor.h
546 lines (465 loc) · 15.7 KB
/
SQLExecutor.h
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
//
// SQLExecutor.h
//
// $Id: //poco/Main/Data/ODBC/testsuite/src/SQLExecutor.h#4 $
//
// Definition of the SQLExecutor class.
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#ifndef SQLExecutor_INCLUDED
#define SQLExecutor_INCLUDED
#include "Poco/Data/ODBC/ODBC.h"
#include "Poco/Data/ODBC/Utility.h"
#include "Poco/Data/ODBC/ODBCException.h"
#include "Poco/Data/Session.h"
#include "Poco/Data/BulkExtraction.h"
#include "Poco/Data/BulkBinding.h"
#include "Poco/NumberFormatter.h"
#include "Poco/String.h"
#include "Poco/Exception.h"
#include <iostream>
#define poco_odbc_check_env(r, h) \
if (!SQL_SUCCEEDED(r)) \
{ \
Poco::Data::ODBC::EnvironmentException ee(h); \
std::cout << ee.toString() << std::endl; \
} \
assert (SQL_SUCCEEDED(r))
#define poco_odbc_check_dbc(r, h) \
if (!SQL_SUCCEEDED(r)) \
{ \
Poco::Data::ODBC::ConnectionException ce(h); \
std::cout << ce.toString() << std::endl; \
} \
assert (SQL_SUCCEEDED(r))
#define poco_odbc_check_stmt(r, h) \
if (!SQL_SUCCEEDED(r)) \
{ \
Poco::Data::ODBC::StatementException se(h); \
std::cout << se.toString() << std::endl; \
} \
assert (SQL_SUCCEEDED(r))
#define poco_odbc_check_desc(r, h) \
if (!SQL_SUCCEEDED(r)) \
{ \
Poco::Data::ODBC::DescriptorException de(h); \
std::cout << de.toString() << std::endl; \
} \
assert (SQL_SUCCEEDED(r))
#define poco_data_using_statements using Poco::Data::Keywords::now; \
using Poco::Data::Keywords::into; \
using Poco::Data::Keywords::use; \
using Poco::Data::Keywords::bulk; \
using Poco::Data::Keywords::limit; \
using Poco::Data::CLOB; \
using Poco::Data::ODBC::ConnectionException; \
using Poco::Data::ODBC::StatementException
class SQLExecutor: public CppUnit::TestCase
{
public:
enum DataBinding
{
PB_IMMEDIATE,
PB_AT_EXEC
};
enum DataExtraction
{
DE_MANUAL,
DE_BOUND
};
SQLExecutor(const std::string& name, Poco::Data::Session* _pSession);
~SQLExecutor();
void execute(const std::string& sql);
/// Execute a query.
void bareboneODBCTest(const std::string& dbConnString,
const std::string& tableCreateString,
DataBinding bindMode,
DataExtraction extractMode,
bool doTime=true,
const std::string& blobPlaceholder="?");
void bareboneODBCMultiResultTest(const std::string& dbConnString,
const std::string& tableCreateString,
SQLExecutor::DataBinding bindMode,
SQLExecutor::DataExtraction extractMode,
const std::string& insert = MULTI_INSERT,
const std::string& select = MULTI_SELECT);
/// The above two functions use "bare bone" ODBC API calls
/// (i.e. calls are not "wrapped" in PocoData framework structures).
/// The purpose of the functions is to verify that a driver behaves
/// correctly as well as to determine its capabilities
/// (e.g. SQLGetData() restrictions relaxation policy, if any).
/// If these test pass, subsequent tests failures are likely ours.
void simpleAccess();
void complexType();
void complexTypeTuple();
void simpleAccessVector();
void complexTypeVector();
void sharedPtrComplexTypeVector();
void autoPtrComplexTypeVector();
void insertVector();
void insertEmptyVector();
void simpleAccessList();
void complexTypeList();
void insertList();
void insertEmptyList();
void simpleAccessDeque();
void complexTypeDeque();
void insertDeque();
void insertEmptyDeque();
void affectedRows(const std::string& whereClause = "");
void insertSingleBulk();
void insertSingleBulkVec();
void limits();
void limitOnce();
void limitPrepare();
void limitZero();
void prepare();
template <typename C1, typename C2, typename C3, typename C4, typename C5, typename C6>
void doBulkWithBool(Poco::UInt32 size, const std::string& blobPlaceholder="?")
{
poco_data_using_statements;
std::string funct = "doBulkWithBool()";
C1 ints;
C2 strings;
C3 blobs;
C4 floats;
C5 dateTimes(size);
C6 bools;
for (int i = 0; i < size; ++i)
{
ints.push_back(i);
strings.push_back(std::string("xyz" + Poco::NumberFormatter::format(i)));
blobs.push_back(std::string("abc") + Poco::NumberFormatter::format(i));
floats.push_back(i + .5);
bools.push_back(0 == i % 2);
}
try
{
session() <<
Poco::format("INSERT INTO MiscTest VALUES (?,%s,?,?,?,?)", blobPlaceholder),
use(strings),
use(blobs),
use(ints),
use(floats),
use(dateTimes),
use(bools), now;
} catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
try { session() << "DELETE FROM MiscTest", now; }
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
try
{
session() <<
Poco::format("INSERT INTO MiscTest VALUES (?,%s,?,?,?,?)", blobPlaceholder),
use(strings, bulk),
use(blobs, bulk),
use(ints, bulk),
use(floats, bulk),
use(dateTimes, bulk),
use(bools, bulk), now;
} catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
ints.clear();
strings.clear();
blobs.clear();
floats.clear();
dateTimes.clear();
bools.clear();
try
{
session() << "SELECT * FROM MiscTest ORDER BY Third",
into(strings),
into(blobs),
into(ints),
into(floats),
into(dateTimes),
into(bools),
now;
} catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
std::string number = Poco::NumberFormatter::format(size - 1);
assert (size == ints.size());
assert (0 == ints.front());
assert (size - 1 == ints.back());
assert (std::string("xyz0") == strings.front());
assert (std::string("xyz") + number == strings.back());
assert (CLOB("abc0") == blobs.front());
CLOB blob("abc");
blob.appendRaw(number.c_str(), number.size());
assert (blob == blobs.back());
assert (.5 == floats.front());
assert (floats.size() - 1 + .5 == floats.back());
assert (bools.front());
assert (((0 == ((bools.size() - 1) % 2)) == bools.back()));
ints.clear();
try
{
session() << "SELECT First FROM MiscTest", into(ints, bulk(size)), limit(size+1), now;
fail ("must fail");
}
catch(Poco::InvalidArgumentException&){ }
try
{
session() << "SELECT First FROM MiscTest", into(ints), bulk(size), now;
fail ("must fail");
}
catch(Poco::InvalidAccessException&){ }
ints.clear();
strings.clear();
strings.resize(size);
blobs.clear();
floats.clear();
floats.resize(size);
dateTimes.clear();
bools.clear();
bools.resize(size);
try
{
session() << "SELECT First, Second, Third, Fourth, Fifth, Sixth FROM MiscTest ORDER BY Third",
into(strings, bulk),
into(blobs, bulk(size)),
into(ints, bulk(size)),
into(floats, bulk),
into(dateTimes, bulk(size)),
into(bools, bulk),
now;
} catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
assert (size == ints.size());
assert (0 == ints.front());
assert (size - 1 == ints.back());
assert (std::string("xyz0") == strings.front());
assert (std::string("xyz") + number == strings.back());
assert (CLOB("abc0") == blobs.front());
blob.assignRaw("abc", 3);
blob.appendRaw(number.c_str(), number.size());
assert (blob == blobs.back());
assert (.5 == floats.front());
assert (floats.size() - 1 + .5 == floats.back());
assert (bools.front());
assert (((0 == ((bools.size() - 1) % 2)) == bools.back()));
}
void doBulkPerformance(Poco::UInt32 size);
template <typename C1, typename C2, typename C3, typename C4, typename C5>
void doBulk(Poco::UInt32 size)
{
poco_data_using_statements;
std::string funct = "doBulk()";
C1 ints;
C2 strings;
C3 blobs;
C4 floats;
C5 dateTimes(size);
for (int i = 0; i < size; ++i)
{
ints.push_back(i);
strings.push_back(std::string("xyz" + Poco::NumberFormatter::format(i)));
blobs.push_back(std::string("abc") + Poco::NumberFormatter::format(i));
floats.push_back(i + .5);
}
try
{
session() << "INSERT INTO MiscTest VALUES (?,?,?,?,?)",
use(strings),
use(blobs),
use(ints),
use(floats),
use(dateTimes), now;
} catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
try { session() << "DELETE FROM MiscTest", now; }
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
try
{
session() << "INSERT INTO MiscTest VALUES (?,?,?,?,?)",
use(strings, bulk),
use(blobs, bulk),
use(ints, bulk),
use(floats, bulk),
use(dateTimes, bulk), now;
} catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
ints.clear();
strings.clear();
blobs.clear();
floats.clear();
dateTimes.clear();
try
{
session() << "SELECT * FROM MiscTest ORDER BY First",
into(strings),
into(blobs),
into(ints),
into(floats),
into(dateTimes),
now;
} catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
std::string number = Poco::NumberFormatter::format(size - 1);
assert (size == ints.size());
assert (0 == ints.front());
assert (size - 1 == ints.back());
assert (std::string("xyz0") == strings.front());
assert (std::string("xyz") + number == strings.back());
assert (CLOB("abc0") == blobs.front());
CLOB blob("abc");
blob.appendRaw(number.c_str(), number.size());
assert (blob == blobs.back());
assert (.5 == floats.front());
assert (floats.size() - 1 + .5 == floats.back());
ints.clear();
try
{
session() << "SELECT First FROM MiscTest", into(ints, bulk(size)), limit(size+1), now;
fail ("must fail");
}
catch(Poco::InvalidArgumentException&){ }
try
{
session() << "SELECT First FROM MiscTest", into(ints), bulk(size), now;
fail ("must fail");
}
catch(Poco::InvalidAccessException&){ }
ints.clear();
strings.clear();
blobs.clear();
floats.clear();
dateTimes.clear();
try
{
session() << "SELECT * FROM MiscTest ORDER BY First",
into(strings, bulk(size)),
into(blobs, bulk(size)),
into(ints, bulk(size)),
into(floats, bulk(size)),
into(dateTimes, bulk(size)),
now;
} catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
assert (size == ints.size());
assert (0 == ints.front());
assert (size - 1 == ints.back());
assert (std::string("xyz0") == strings.front());
assert (std::string("xyz") + number == strings.back());
assert (CLOB("abc0") == blobs.front());
blob.assignRaw("abc", 3);
blob.appendRaw(number.c_str(), number.size());
assert (blob == blobs.back());
assert (.5 == floats.front());
assert (floats.size() - 1 + .5 == floats.back());
}
void setSimple();
void setComplex();
void setComplexUnique();
void multiSetSimple();
void multiSetComplex();
void mapComplex();
void mapComplexUnique();
void multiMapComplex();
void selectIntoSingle();
void selectIntoSingleStep();
void selectIntoSingleFail();
void lowerLimitOk();
void lowerLimitFail();
void combinedLimits();
void combinedIllegalLimits();
void ranges();
void illegalRange();
void singleSelect();
void emptyDB();
void blob(int bigSize = 1024, const std::string& blobPlaceholder = "?");
template <typename C1, typename C2>
void blobContainer(int size)
{
poco_data_using_statements;
std::string funct = "blobContainer()";
C1 lastName(size, "lastname");
C1 firstName(size, "firstname");
C1 address(size, "Address");
C2 img(size, CLOB("0123456789", 10));
int count = 0;
try { session() << "INSERT INTO Person VALUES (?,?,?,?)", use(lastName), use(firstName), use(address), use(img), now; }
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
try { session() << "SELECT COUNT(*) FROM Person", into(count), now; }
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
assert (count == size);
C2 res;
try { session() << "SELECT Image FROM Person", into(res), now; }
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
assert (res.size() == img.size());
assert (res == img);
}
void blobStmt();
void dateTime();
void date();
void time();
void floats();
void doubles();
void tuples();
void tupleVector();
void internalExtraction();
void filter(const std::string& query =
"SELECT * FROM Vectors ORDER BY int0 ASC",
const std::string& intFldName = "int0");
void internalBulkExtraction();
void internalStorageType();
void nulls();
void notNulls(const std::string& sqlState = "23502");
void rowIterator();
void stdVectorBool();
void asynchronous(int rowCount = 500);
void any();
void dynamicAny();
void multipleResults(const std::string& sql =
"SELECT * FROM Person WHERE Age = ?; "
"SELECT Age FROM Person WHERE FirstName = 'Bart'; "
"SELECT * FROM Person WHERE Age = ? OR Age = ? ORDER BY Age;");
void sqlChannel(const std::string& connect);
void sqlLogger(const std::string& connect);
void sessionTransaction(const std::string& connect);
void transaction(const std::string& connect);
void transactor();
void nullable();
void reconnect();
private:
static const std::string MULTI_INSERT;
static const std::string MULTI_SELECT;
void setTransactionIsolation(Poco::Data::Session& session, Poco::UInt32 ti);
Poco::Data::Session& session();
Poco::Data::Session* _pSession;
};
inline Poco::Data::Session& SQLExecutor::session()
{
poco_check_ptr (_pSession);
return *_pSession;
}
#endif // SQLExecutor_INCLUDED