~ubuntu-branches/ubuntu/quantal/mysql-workbench/quantal

« back to all changes in this revision

Viewing changes to library/dbc/unit-tests/dbc_statement_test.cpp

  • Committer: Package Import Robot
  • Author(s): Dmitry Smirnov
  • Date: 2012-03-01 21:57:30 UTC
  • Revision ID: package-import@ubuntu.com-20120301215730-o7y8av8y38n162ro
Tags: upstream-5.2.38+dfsg
ImportĀ upstreamĀ versionĀ 5.2.38+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public License as
 
6
 * published by the Free Software Foundation; version 2 of the
 
7
 * License.
 
8
 * 
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
12
 * GNU General Public License for more details.
 
13
 * 
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
17
 * 02110-1301  USA
 
18
 */
 
19
 
 
20
#include "tut_stdafx.h"
 
21
 
 
22
#include "connection_helpers.h"
 
23
#include "grtsqlparser/sql_facade.h"
 
24
 
 
25
#define DATABASE_TO_USE "USE test"
 
26
 
 
27
static bool populate_test_table(std::auto_ptr<sql::Statement> &stmt)
 
28
{
 
29
  stmt->execute(DATABASE_TO_USE);
 
30
  stmt->execute("DROP TABLE IF EXISTS test_function");
 
31
  if (true == stmt->execute("CREATE TABLE test_function (a integer, b integer, c integer default null)"))
 
32
    return false;
 
33
 
 
34
  if (true == stmt->execute("INSERT INTO test_function (a,b,c) VALUES(1, 111, NULL)"))
 
35
  {
 
36
    stmt->execute("DROP TABLE test_function");
 
37
    return false;
 
38
  }
 
39
  return true;
 
40
}
 
41
 
 
42
static bool populate_tx_test_table(std::auto_ptr<sql::Statement> &stmt)
 
43
{
 
44
  stmt->execute(DATABASE_TO_USE);
 
45
  stmt->execute("DROP TABLE IF EXISTS test_function_tx");
 
46
  if (true == stmt->execute("CREATE TABLE test_function_tx (a integer, b integer, c integer default null) engine = innodb"))
 
47
    return false;
 
48
 
 
49
  if (true == stmt->execute("INSERT INTO test_function_tx (a,b,c) VALUES(1, 111, NULL)"))
 
50
  {
 
51
    stmt->execute("DROP TABLE test_function_tx");
 
52
    return false;
 
53
  }
 
54
  stmt->getConnection()->commit();
 
55
  return true;
 
56
}
 
57
 
 
58
BEGIN_TEST_DATA_CLASS(module_dbc_statement_test)
 
59
public:
 
60
  GRTManagerTest grtm;
 
61
  SqlFacade::Ref sql_splitter;
 
62
  GRT *grt;
 
63
END_TEST_DATA_CLASS
 
64
 
 
65
TEST_MODULE(module_dbc_statement_test, "DBC: statement tests");
 
66
 
 
67
TEST_FUNCTION(1)
 
68
{
 
69
  sql_splitter = NULL;
 
70
  std::string path = "../../Bin/Debug";
 
71
  grtm.set_search_paths(path, path, path);
 
72
  grtm.initialize(path);
 
73
 
 
74
  grt= grtm.get_grt();
 
75
 
 
76
  // load structs
 
77
  grt->scan_metaclasses_in("../../res/grt/");
 
78
  grt->end_loading_metaclasses();
 
79
 
 
80
  ensure_equals("load structs", grt->get_metaclasses().size(), INT_METACLASS_COUNT);
 
81
 
 
82
  sql_splitter= SqlFacade::instance_for_rdbms_name(grt, "Mysql");
 
83
  ensure("failed to get sqlparser module", (NULL != sql_splitter));
 
84
}
 
85
 
 
86
// We don't test Statement::getWarnings() as such doesn't exist.
 
87
// We don't test Statement::clearWarnings() as such doesn't exist.
 
88
 
 
89
// Test construction of a statement object.
 
90
TEST_FUNCTION(2)
 
91
{
 
92
  db_mgmt_ConnectionRef connectionProperties(grt);
 
93
 
 
94
  setup_env(grt, connectionProperties);
 
95
 
 
96
  try {
 
97
    sql::DriverManager *dm= sql::DriverManager::getDriverManager();
 
98
    ensure("dm is NULL", dm != NULL);
 
99
 
 
100
    sql::ConnectionWrapper wrapper= dm->getConnection(connectionProperties);
 
101
    ensure("conn is NULL", wrapper.get() != NULL);
 
102
 
 
103
    sql::Connection* connection= wrapper.get();
 
104
    {
 
105
      /* Going out scope will free the object. We test this as there is no close() method */
 
106
      std::auto_ptr<sql::Statement> stmt(connection->createStatement());
 
107
    }
 
108
  } catch (sql::SQLException &) {
 
109
    printf("ERR: Caught sql::SQLException\n");
 
110
    throw;
 
111
  }
 
112
}
 
113
 
 
114
// Test simple update statement against statement object.
 
115
TEST_FUNCTION(3)
 
116
{
 
117
  db_mgmt_ConnectionRef connectionProperties(grt);
 
118
 
 
119
  setup_env(grt, connectionProperties);
 
120
 
 
121
  try {
 
122
    sql::DriverManager *dm= sql::DriverManager::getDriverManager();
 
123
    ensure("dm is NULL", dm != NULL);
 
124
 
 
125
    sql::ConnectionWrapper wrapper= dm->getConnection(connectionProperties);
 
126
    ensure("conn is NULL", wrapper.get() != NULL);
 
127
 
 
128
    sql::Connection* connection= wrapper.get();
 
129
 
 
130
    std::auto_ptr<sql::Statement> stmt(connection->createStatement());
 
131
    ensure("stmt is NULL", stmt.get() != NULL);
 
132
 
 
133
    ensure("Data not populated", true == populate_test_table(stmt));
 
134
    if (true == stmt->execute("UPDATE test_function SET a = 2, b = 222 where b = 111"))
 
135
      ensure("True returned for UPDATE", false);
 
136
 
 
137
    stmt->execute("DROP TABLE test_function");
 
138
  } catch (sql::SQLException &) {
 
139
    printf("ERR: Caught sql::SQLException\n");
 
140
    throw;
 
141
  } catch (...) {
 
142
    printf("ERR: Caught unknown exception\n");
 
143
    throw;
 
144
  }
 
145
}
 
146
 
 
147
// Test simple query against statement object.
 
148
TEST_FUNCTION(4)
 
149
{
 
150
  db_mgmt_ConnectionRef connectionProperties(grt);
 
151
 
 
152
  setup_env(grt, connectionProperties);
 
153
 
 
154
  try {
 
155
    sql::DriverManager *dm = sql::DriverManager::getDriverManager();
 
156
    ensure("dm is NULL", dm != NULL);
 
157
 
 
158
    sql::ConnectionWrapper wrapper= dm->getConnection(connectionProperties);
 
159
    ensure("conn is NULL", wrapper.get() != NULL);
 
160
 
 
161
    sql::Connection* connection= wrapper.get();
 
162
 
 
163
    std::auto_ptr<sql::Statement> stmt(connection->createStatement());
 
164
    ensure("stmt is NULL", stmt.get() != NULL);
 
165
 
 
166
    ensure("Data not populated", true == populate_test_table(stmt));
 
167
    if (false == stmt->execute("SELECT * FROM test_function"))
 
168
      ensure("False returned for SELECT", false);
 
169
 
 
170
    /* Clean */
 
171
    sql::ConnectionWrapper wrapper2= dm->getConnection(connectionProperties);
 
172
    ensure("conn2 is NULL", wrapper2.get() != NULL);
 
173
    sql::Connection* connection2= wrapper2.get();
 
174
    std::auto_ptr<sql::Statement> stmt2(connection2->createStatement());
 
175
    ensure("stmt is NULL", stmt2.get() != NULL);
 
176
    stmt2->execute(DATABASE_TO_USE);
 
177
    stmt2->execute("DROP TABLE test_function");
 
178
  } catch (sql::SQLException &) {
 
179
    printf("ERR: Caught sql::SQLException\n");
 
180
    throw;
 
181
  } catch (...) {
 
182
    printf("ERR: Caught unknown exception\n");
 
183
    throw;
 
184
  }
 
185
}
 
186
 
 
187
// Test executeQuery() - returning a result set.
 
188
TEST_FUNCTION(5)
 
189
{
 
190
  db_mgmt_ConnectionRef connectionProperties(grt);
 
191
 
 
192
  setup_env(grt, connectionProperties);
 
193
 
 
194
  try {
 
195
    sql::DriverManager *dm = sql::DriverManager::getDriverManager();
 
196
    ensure("dm is NULL", dm != NULL);
 
197
 
 
198
    sql::ConnectionWrapper wrapper= dm->getConnection(connectionProperties);
 
199
    ensure("conn is NULL", wrapper.get() != NULL);
 
200
 
 
201
    sql::Connection* connection= wrapper.get();
 
202
 
 
203
    std::auto_ptr<sql::Statement> stmt(connection->createStatement());
 
204
    ensure("stmt is NULL", stmt.get() != NULL);
 
205
 
 
206
    ensure("Data not populated", true == populate_test_table(stmt));
 
207
    /* Get a result set */
 
208
    try {
 
209
      std::auto_ptr<sql::ResultSet> rset(stmt->executeQuery("SELECT * FROM test_function"));
 
210
      ensure("NULL returned for result set", rset.get() != NULL);
 
211
    } catch (sql::SQLException &) {
 
212
      printf("ERR: sql::SQLException caught\n");
 
213
      throw;
 
214
    }
 
215
 
 
216
    /* Clean */
 
217
    sql::ConnectionWrapper wrapper2= dm->getConnection(connectionProperties);
 
218
    ensure("wrapper2 is NULL", wrapper2.get() != NULL);
 
219
    sql::Connection* connection2= wrapper2.get();
 
220
    std::auto_ptr<sql::Statement> stmt2(connection2->createStatement());
 
221
    ensure("stmt is NULL", stmt2.get() != NULL);
 
222
    stmt2->execute(DATABASE_TO_USE);
 
223
    stmt2->execute("DROP TABLE test_function");
 
224
  } catch (sql::SQLException &) {
 
225
    printf("ERR: Caught sql::SQLException\n");
 
226
    throw;
 
227
  } catch (...) {
 
228
    printf("ERR: Caught unknown exception\n");
 
229
    throw;
 
230
  }
 
231
}
 
232
 
 
233
// Test executeQuery() - returning empty result set.
 
234
TEST_FUNCTION(6)
 
235
{
 
236
  db_mgmt_ConnectionRef connectionProperties(grt);
 
237
 
 
238
  setup_env(grt, connectionProperties);
 
239
 
 
240
  try {
 
241
    sql::DriverManager *dm = sql::DriverManager::getDriverManager();
 
242
    ensure("dm is NULL", dm != NULL);
 
243
 
 
244
    sql::ConnectionWrapper wrapper= dm->getConnection(connectionProperties);
 
245
    ensure("conn is NULL", wrapper.get() != NULL);
 
246
 
 
247
    sql::Connection* connection= wrapper.get();
 
248
 
 
249
    std::auto_ptr<sql::Statement> stmt(connection->createStatement());
 
250
    ensure("stmt is NULL", stmt.get() != NULL);
 
251
 
 
252
    ensure("Data not populated", true == populate_test_table(stmt));
 
253
    /* Get a result set */
 
254
    try {
 
255
      std::auto_ptr<sql::ResultSet> rset(stmt->executeQuery("SELECT * FROM test_function WHERE 1=2"));
 
256
      ensure("NULL returned for result set", rset.get() != NULL);
 
257
      ensure("Non-empty result set", false == rset->next());
 
258
 
 
259
    } catch (sql::SQLException &) {
 
260
      printf("ERR: Caught sql::SQLException\n");
 
261
      throw;
 
262
    }
 
263
 
 
264
    /* Clean */
 
265
    sql::ConnectionWrapper wrapper2= dm->getConnection(connectionProperties);
 
266
    ensure("wrapper2 is NULL", wrapper2.get() != NULL);
 
267
    sql::Connection* connection2= wrapper2.get();
 
268
    std::auto_ptr<sql::Statement> stmt2(connection2->createStatement());
 
269
    ensure("stmt is NULL", stmt2.get() != NULL);
 
270
    stmt2->execute(DATABASE_TO_USE);
 
271
    stmt2->execute("DROP TABLE test_function");
 
272
  } catch (sql::SQLException &) {
 
273
    printf("ERR: Caught sql::SQLException\n");
 
274
    throw;
 
275
  } catch (...) {
 
276
    printf("ERR: Caught unknown exception\n");
 
277
    throw;
 
278
  }
 
279
}
 
280
 
 
281
// Test executeQuery() - use it for inserting, should generate an exception.
 
282
TEST_FUNCTION(7)
 
283
{
 
284
  db_mgmt_ConnectionRef connectionProperties(grt);
 
285
 
 
286
  setup_env(grt, connectionProperties);
 
287
 
 
288
  try {
 
289
    sql::DriverManager *dm = sql::DriverManager::getDriverManager();
 
290
    ensure("dm is NULL", dm != NULL);
 
291
 
 
292
    sql::ConnectionWrapper wrapper= dm->getConnection(connectionProperties);
 
293
    ensure("conn is NULL", wrapper.get() != NULL);
 
294
 
 
295
    sql::Connection* connection= wrapper.get();
 
296
 
 
297
    std::auto_ptr<sql::Statement> stmt(connection->createStatement());
 
298
    ensure("stmt is NULL", stmt.get() != NULL);
 
299
 
 
300
    ensure("Data not populated", true == populate_test_table(stmt));
 
301
    /* Get a result set */
 
302
    try {
 
303
      std::auto_ptr<sql::ResultSet> rset(stmt->executeQuery("INSERT INTO test_function VALUES(2,200)"));
 
304
      ensure("NULL returned for result set", rset.get() == NULL);
 
305
      ensure("Non-empty result set", false == rset->next());
 
306
    } catch (sql::SQLException &) {
 
307
    } catch (...) {
 
308
      printf("ERR: Incorrectly sql::SQLException ist not thrown\n");
 
309
      throw;
 
310
    }
 
311
    /* Clean */
 
312
    sql::ConnectionWrapper wrapper2= dm->getConnection(connectionProperties);
 
313
    ensure("wrapper2 is NULL", wrapper2.get() != NULL);
 
314
    sql::Connection* connection2= wrapper2.get();
 
315
    std::auto_ptr<sql::Statement> stmt2(connection2->createStatement());
 
316
    ensure("stmt is NULL", stmt2.get() != NULL);
 
317
    stmt2->execute(DATABASE_TO_USE);
 
318
    stmt2->execute("DROP TABLE test_function");
 
319
  } catch (sql::SQLException &) {
 
320
    printf("ERR: Caught sql::SQLException\n");
 
321
    throw;
 
322
  } catch (...) {
 
323
    printf("ERR: Caught unknown exception\n");
 
324
    throw;
 
325
  }
 
326
}
 
327
 
 
328
// Test executeUpdate() - check the returned value.
 
329
TEST_FUNCTION(8)
 
330
{
 
331
  db_mgmt_ConnectionRef connectionProperties(grt);
 
332
 
 
333
  setup_env(grt, connectionProperties);
 
334
 
 
335
  try {
 
336
    sql::DriverManager *dm= sql::DriverManager::getDriverManager();
 
337
    ensure("dm is NULL", dm != NULL);
 
338
 
 
339
    sql::ConnectionWrapper wrapper= dm->getConnection(connectionProperties);
 
340
    ensure("conn is NULL", wrapper.get() != NULL);
 
341
 
 
342
    sql::Connection* connection= wrapper.get();
 
343
 
 
344
    std::auto_ptr<sql::Statement> stmt(connection->createStatement());
 
345
    ensure("stmt is NULL", stmt.get() != NULL);
 
346
 
 
347
    ensure("Data not populated", true == populate_test_table(stmt));
 
348
    /* Get a result set */
 
349
    try {
 
350
      ensure_equals("Number of updated rows",
 
351
                    stmt->executeUpdate("UPDATE test_function SET a = 123"),
 
352
                    1);
 
353
    } catch (sql::SQLException &) {
 
354
      printf("ERR: Caught sql::SQLException\n");
 
355
      throw;
 
356
    }
 
357
 
 
358
    /* Clean */
 
359
    sql::ConnectionWrapper wrapper2= dm->getConnection(connectionProperties);
 
360
    ensure("wrapper2 is NULL", wrapper2.get() != NULL);
 
361
    sql::Connection* connection2= wrapper2.get();
 
362
    std::auto_ptr<sql::Statement> stmt2(connection2->createStatement());
 
363
    ensure("stmt is NULL", stmt2.get() != NULL);
 
364
    stmt2->execute(DATABASE_TO_USE);
 
365
    stmt2->execute("DROP TABLE test_function");
 
366
  } catch (sql::SQLException &) {
 
367
    printf("ERR: Caught sql::SQLException\n");
 
368
    throw;
 
369
  } catch (...) {
 
370
    printf("ERR: Caught unknown exception\n");
 
371
    throw;
 
372
  }
 
373
}
 
374
 
 
375
// Test executeUpdate() - execute a SELECT, should get an exception
 
376
TEST_FUNCTION(9)
 
377
{
 
378
  db_mgmt_ConnectionRef connectionProperties(grt);
 
379
 
 
380
  setup_env(grt, connectionProperties);
 
381
 
 
382
  try {
 
383
    sql::DriverManager *dm = sql::DriverManager::getDriverManager();
 
384
    ensure("dm is NULL", dm != NULL);
 
385
 
 
386
    sql::ConnectionWrapper wrapper= dm->getConnection(connectionProperties);
 
387
    ensure("conn is NULL", wrapper.get() != NULL);
 
388
 
 
389
    sql::Connection* connection= wrapper.get();
 
390
 
 
391
    std::auto_ptr<sql::Statement> stmt(connection->createStatement());
 
392
    ensure("stmt is NULL", stmt.get() != NULL);
 
393
 
 
394
    ensure("Data not populated", populate_test_table(stmt));
 
395
    /* Get a result set */
 
396
    try {
 
397
      stmt->executeUpdate("SELECT * FROM test_function");
 
398
      // TODO: executing a query which returns a result set should throw an exception.
 
399
      // fail("No exception thrown");
 
400
    } catch (sql::SQLException &) {}
 
401
 
 
402
    /* Clean */
 
403
    sql::ConnectionWrapper wrapper2= dm->getConnection(connectionProperties);
 
404
    ensure("wrapper2 is NULL", wrapper2.get() != NULL);
 
405
    sql::Connection* connection2= wrapper2.get();
 
406
    std::auto_ptr<sql::Statement> stmt2(connection2->createStatement());
 
407
    ensure("stmt is NULL", stmt2.get() != NULL);
 
408
    stmt2->execute(DATABASE_TO_USE);
 
409
    stmt2->execute("DROP TABLE test_function");
 
410
  } catch (sql::SQLException &) {
 
411
    printf("ERR: Caught sql::SQLException\n");
 
412
    throw;
 
413
  }
 
414
}
 
415
 
 
416
// Test getFetchSize() - should return int value.
 
417
TEST_FUNCTION(10)
 
418
{
 
419
  db_mgmt_ConnectionRef connectionProperties(grt);
 
420
 
 
421
  setup_env(grt, connectionProperties);
 
422
 
 
423
  try {
 
424
    sql::DriverManager *dm = sql::DriverManager::getDriverManager();
 
425
    ensure("dm is NULL", dm != NULL);
 
426
 
 
427
    sql::ConnectionWrapper wrapper= dm->getConnection(connectionProperties);
 
428
    ensure("conn is NULL", wrapper.get() != NULL);
 
429
 
 
430
    sql::Connection* connection= wrapper.get();
 
431
 
 
432
    std::auto_ptr<sql::Statement> stmt(connection->createStatement());
 
433
    ensure("stmt is NULL", stmt.get() != NULL);
 
434
 
 
435
    // TODO: implement and test getFetchSize() and getFechtDirection()
 
436
    // ensure("fetchSize not > 0", stmt->getFetchSize() > 0);
 
437
 
 
438
  } catch (sql::SQLException &) {
 
439
    printf("ERR: Caught sql::SQLException\n");
 
440
    throw;
 
441
  }
 
442
}
 
443
 
 
444
// We don't test Statement::getMaxRows() as such doesn't exist.
 
445
// We don't test Statement::getMoreResults() as such doesn't exist.
 
446
// We don't test Statement::getQueryTimeout() as such doesn't exist.
 
447
 
 
448
// Test getResultSet() - execute() a query and get the result set.
 
449
TEST_FUNCTION(11)
 
450
{
 
451
  db_mgmt_ConnectionRef connectionProperties(grt);
 
452
 
 
453
  setup_env(grt, connectionProperties);
 
454
 
 
455
  try {
 
456
    sql::DriverManager *dm = sql::DriverManager::getDriverManager();
 
457
    ensure("dm is NULL", dm != NULL);
 
458
 
 
459
    sql::ConnectionWrapper wrapper= dm->getConnection(connectionProperties);
 
460
    ensure("conn is NULL", wrapper.get() != NULL);
 
461
 
 
462
    sql::Connection* connection= wrapper.get();
 
463
 
 
464
    std::auto_ptr<sql::Statement> stmt(connection->createStatement());
 
465
    ensure("stmt is NULL", stmt.get() != NULL);
 
466
 
 
467
    ensure("Data not populated", true == populate_test_table(stmt));
 
468
    ensure("Statement::execute returned false", true == stmt->execute("SELECT * FROM test_function"));
 
469
    
 
470
    std::auto_ptr<sql::ResultSet> rset(stmt->getResultSet());
 
471
    ensure("rset is NULL", rset.get() != NULL);
 
472
 
 
473
    /* Clean */
 
474
    sql::ConnectionWrapper wrapper2= dm->getConnection(connectionProperties);
 
475
    ensure("wrapper2 is NULL", wrapper2.get() != NULL);
 
476
    sql::Connection* connection2= wrapper2.get();
 
477
    std::auto_ptr<sql::Statement> stmt2(connection2->createStatement());
 
478
    ensure("stmt is NULL", stmt2.get() != NULL);
 
479
    stmt2->execute(DATABASE_TO_USE);
 
480
    stmt2->execute("DROP TABLE test_function");
 
481
  } catch (sql::SQLException &) {
 
482
    printf("ERR: Caught sql::SQLException\n");
 
483
    throw;
 
484
  }
 
485
}
 
486
 
 
487
// Test getResultSet() - execute() an update query and get the result set - should be empty.
 
488
// TODO: Doesn't test much as stmt::getResultSet() is not implemented.
 
489
TEST_FUNCTION(12)
 
490
{
 
491
  db_mgmt_ConnectionRef connectionProperties(grt);
 
492
 
 
493
  setup_env(grt, connectionProperties);
 
494
 
 
495
  try {
 
496
    sql::DriverManager *dm = sql::DriverManager::getDriverManager();
 
497
    ensure("dm is NULL", dm != NULL);
 
498
 
 
499
    sql::ConnectionWrapper wrapper= dm->getConnection(connectionProperties);
 
500
    ensure("conn is NULL", wrapper.get() != NULL);
 
501
 
 
502
    sql::Connection* connection= wrapper.get();
 
503
 
 
504
    std::auto_ptr<sql::Statement> stmt(connection->createStatement());
 
505
    ensure("stmt is NULL", stmt.get() != NULL);
 
506
 
 
507
    ensure("Data not populated", true == populate_test_table(stmt));
 
508
 
 
509
    ensure("Statement::execute returned true", false == stmt->execute("UPDATE test_function SET a = 222"));
 
510
    
 
511
    try
 
512
    {
 
513
      std::auto_ptr<sql::ResultSet> rset(stmt->getResultSet());
 
514
                        if (NULL == rset.get())
 
515
                                throw sql::SQLException();
 
516
      fail("Got result set for an update operation.");
 
517
    }
 
518
    catch (sql::SQLException &) {}
 
519
 
 
520
    /* Clean */
 
521
    sql::ConnectionWrapper wrapper2= dm->getConnection(connectionProperties);
 
522
    ensure("wrapper2 is NULL", wrapper2.get() != NULL);
 
523
    sql::Connection* connection2= wrapper2.get();
 
524
    std::auto_ptr<sql::Statement> stmt2(connection2->createStatement());
 
525
    ensure("stmt is NULL", stmt2.get() != NULL);
 
526
    stmt2->execute(DATABASE_TO_USE);
 
527
    stmt2->execute("DROP TABLE test_function");
 
528
  } catch (sql::SQLException &) {
 
529
    printf("ERR: Caught sql::SQLException\n");
 
530
    throw;
 
531
  }
 
532
}
 
533
 
 
534
// We don't test Statement::getResultSetConcurrency() as such doesn't exist.
 
535
// We don't test Statement::getResultSetType() as such doesn't exist.
 
536
// We don't test Statement::getUpdateCount() as such doesn't exist.
 
537
// We don't test Statement::getWarnings() as such doesn't exist.
 
538
 
 
539
// Test setFetchSize() - set and get the value.
 
540
// TODO: Doesn't pass because setFetchSize() is unimplemented.
 
541
TEST_FUNCTION(13)
 
542
{
 
543
  db_mgmt_ConnectionRef connectionProperties(grt);
 
544
 
 
545
  setup_env(grt, connectionProperties);
 
546
 
 
547
  try {
 
548
    sql::DriverManager *dm = sql::DriverManager::getDriverManager();
 
549
    ensure("dm is NULL", dm != NULL);
 
550
 
 
551
    sql::ConnectionWrapper wrapper= dm->getConnection(connectionProperties);
 
552
    ensure("conn is NULL", wrapper.get() != NULL);
 
553
 
 
554
    sql::Connection* connection= wrapper.get();
 
555
 
 
556
    std::auto_ptr<sql::Statement> stmt(connection->createStatement());
 
557
    ensure("stmt is NULL", stmt.get() != NULL);
 
558
/*
 
559
    int setFetchSize = 50;
 
560
 
 
561
    stmt->setFetchSize(setFetchSize);
 
562
    
 
563
    ensure_equals("Non-equal", setFetchSize, stmt->getFetchSize());
 
564
*/
 
565
  } catch (sql::SQLException &) {
 
566
    printf("ERR: Caught sql::SQLException\n");
 
567
    throw;
 
568
  }
 
569
}
 
570
 
 
571
// Test setFetchSize() - set negative value and expect an exception.
 
572
// TODO: Doesn't pass because setFetchSize() is unimplemented.
 
573
TEST_FUNCTION(14)
 
574
{
 
575
  db_mgmt_ConnectionRef connectionProperties(grt);
 
576
 
 
577
  setup_env(grt, connectionProperties);
 
578
 
 
579
  try {
 
580
    sql::DriverManager *dm = sql::DriverManager::getDriverManager();
 
581
    ensure("dm is NULL", dm != NULL);
 
582
 
 
583
    sql::ConnectionWrapper wrapper= dm->getConnection(connectionProperties);
 
584
    ensure("conn is NULL", wrapper.get() != NULL);
 
585
 
 
586
    sql::Connection* connection= wrapper.get();
 
587
 
 
588
    std::auto_ptr<sql::Statement> stmt(connection->createStatement());
 
589
    ensure("stmt is NULL", stmt.get() != NULL);
 
590
/*
 
591
    try {
 
592
      stmt->setFetchSize(-1);
 
593
      ensure("No exception", false);
 
594
    } catch (sql::InvalidArgumentException) {
 
595
      printf("INFO: Caught sql::InvalidArgumentException\n");
 
596
    }
 
597
*/
 
598
  } catch (sql::SQLException &) {
 
599
    printf("ERR: Caught sql::SQLException\n");
 
600
    throw;
 
601
  }
 
602
}
 
603
 
 
604
// Test setQueryTimeout() - set negative value and expect an exception.
 
605
// TODO: Doesn't pass because setQueryTimeout() is unimplemented.
 
606
TEST_FUNCTION(15)
 
607
{
 
608
  db_mgmt_ConnectionRef connectionProperties(grt);
 
609
 
 
610
  setup_env(grt, connectionProperties);
 
611
 
 
612
  try {
 
613
    sql::DriverManager *dm = sql::DriverManager::getDriverManager();
 
614
    ensure("dm is NULL", dm != NULL);
 
615
 
 
616
    sql::ConnectionWrapper wrapper= dm->getConnection(connectionProperties);
 
617
    ensure("conn is NULL", wrapper.get() != NULL);
 
618
 
 
619
    sql::Connection* connection= wrapper.get();
 
620
 
 
621
    std::auto_ptr<sql::Statement> stmt(connection->createStatement());
 
622
    ensure("stmt is NULL", stmt.get() != NULL);
 
623
/*
 
624
    try {
 
625
      stmt->setQueryTimeout(-1);
 
626
      printf("ERR: No exception\n");
 
627
    } catch (sql::InvalidArgumentException &e) {
 
628
      delete e;
 
629
    }
 
630
*/
 
631
  } catch (sql::SQLException &) {
 
632
    printf("ERR: Caught sql::SQLException\n");
 
633
    throw;
 
634
  }
 
635
}
 
636
 
 
637
// Test addBatch()/executeBatch() (includes a test against the 'out of sync' error).
 
638
TEST_FUNCTION(16)
 
639
{
 
640
  db_mgmt_ConnectionRef connectionProperties(grt);
 
641
 
 
642
  setup_env(grt, connectionProperties);
 
643
 
 
644
  try {
 
645
    sql::DriverManager *dm = sql::DriverManager::getDriverManager();
 
646
    ensure("dm is NULL", dm != NULL);
 
647
 
 
648
    sql::ConnectionWrapper wrapper= dm->getConnection(connectionProperties);
 
649
    ensure("conn is NULL", wrapper.get() != NULL);
 
650
 
 
651
    sql::Connection* connection= wrapper.get();
 
652
 
 
653
    std::auto_ptr<sql::Statement> stmt(connection->createStatement());
 
654
    ensure("stmt is NULL", stmt.get() != NULL);
 
655
 
 
656
                std::string sql_script=
 
657
                        "DROP DATABASE IF EXISTS dbc_statement_test_15;"
 
658
                        "CREATE DATABASE dbc_statement_test_15;"
 
659
                        "CREATE TABLE dbc_statement_test_15.table1 (id int);"
 
660
                        "SELECT 1;"
 
661
                        "CREATE TABLE dbc_statement_test_15.table2 (id int);"
 
662
                        "SELECT 1;"
 
663
                        "CREATE TABLE dbc_statement_test_15.table3 (id int);"
 
664
                        "SELECT 1;";
 
665
                std::list<std::string> statements;
 
666
        ensure("failed to get sqlparser module", (NULL != sql_splitter));
 
667
                sql_splitter->splitSqlScript(sql_script, statements);
 
668
                sql::SqlBatchExec()(stmt.get(), statements);
 
669
 
 
670
  } catch (sql::SQLException &) {
 
671
    printf("ERR: Caught sql::SQLException\n");
 
672
    throw;
 
673
  } 
 
674
}
 
675
 
 
676
END_TESTS