Add regression test suite.
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>
Wed, 24 Apr 2013 15:05:29 +0000 (18:05 +0300)
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>
Wed, 24 Apr 2013 15:05:29 +0000 (18:05 +0300)
Two tests are currently failing, because they hit a real bug in the driver.
After the bug is fixed, the tests should pass.

This doesn't work on Windows yet.

37 files changed:
test/Makefile [new file with mode: 0644]
test/README.txt [new file with mode: 0644]
test/expected/alter.out [new file with mode: 0644]
test/expected/arraybinding.out [new file with mode: 0644]
test/expected/boolsaschar.out [new file with mode: 0644]
test/expected/connect.out [new file with mode: 0644]
test/expected/cvtnulldate.out [new file with mode: 0644]
test/expected/dataatexecution.out [new file with mode: 0644]
test/expected/getresult.out [new file with mode: 0644]
test/expected/insertreturning.out [new file with mode: 0644]
test/expected/notice.out [new file with mode: 0644]
test/expected/params.out [new file with mode: 0644]
test/expected/prepare.out [new file with mode: 0644]
test/expected/sampletables.out [new file with mode: 0644]
test/expected/select.out [new file with mode: 0644]
test/expected/stmthandles.out [new file with mode: 0644]
test/launcher [new file with mode: 0755]
test/odbc.ini [new file with mode: 0644]
test/odbcinst.ini [new file with mode: 0644]
test/sql/.gitignore [new file with mode: 0644]
test/sql/sampletables.sql [new file with mode: 0644]
test/src/.gitignore [new file with mode: 0644]
test/src/alter-test.c [new file with mode: 0644]
test/src/arraybinding-test.c [new file with mode: 0644]
test/src/boolsaschar-test.c [new file with mode: 0644]
test/src/common.c [new file with mode: 0644]
test/src/common.h [new file with mode: 0644]
test/src/connect-test.c [new file with mode: 0644]
test/src/cvtnulldate-test.c [new file with mode: 0644]
test/src/dataatexecution-test.c [new file with mode: 0644]
test/src/getresult-test.c [new file with mode: 0644]
test/src/insertreturning-test.c [new file with mode: 0644]
test/src/notice-test.c [new file with mode: 0644]
test/src/params-test.c [new file with mode: 0644]
test/src/prepare-test.c [new file with mode: 0644]
test/src/select-test.c [new file with mode: 0644]
test/src/stmthandles-test.c [new file with mode: 0644]

diff --git a/test/Makefile b/test/Makefile
new file mode 100644 (file)
index 0000000..30dd5df
--- /dev/null
@@ -0,0 +1,31 @@
+TESTS = connect stmthandles select getresult prepare params notice \
+   arraybinding insertreturning dataatexecution boolsaschar cvtnulldate \
+   alter
+
+TESTBINS = $(patsubst %,src/%-test, $(TESTS))
+TESTSQLS = $(patsubst %,sql/%.sql, $(TESTS))
+
+REGRESS = sampletables $(TESTS)
+
+all: $(TESTBINS) $(TESTSQLS)
+
+installcheck: all
+
+override CFLAGS += -Wno-pointer-sign
+
+src/common.o: src/common.c
+
+# For each test file, compile the .c file, and create a .sql file that
+# when executed from psql, just runs the binary.
+src/%-test sql/%.sql: src/%-test.c src/common.o
+   $(CC) $(CFLAGS) src/$*-test.c src/common.o -o src/$*-test -lodbc
+   echo "\! ./src/$*-test" > sql/$*.sql
+
+EXTRA_CLEAN = $(TESTBINS) $(TESTSQLS)
+
+REGRESS_OPTS = --launcher=./launcher
+
+PG_CONFIG = pg_config
+PGXS := $(shell $(PG_CONFIG) --pgxs)
+include $(PGXS)
+
diff --git a/test/README.txt b/test/README.txt
new file mode 100644 (file)
index 0000000..80c9fac
--- /dev/null
@@ -0,0 +1,37 @@
+This directory contains a regression test suite for the psqlODBC driver.
+
+The Makefile is written for Unix-like systems, there is no script to run the
+tests on Windows at the moment.
+
+Prerequisites
+-------------
+
+To run the regression tests, you must have a PostgreSQL server running and
+accepting connections at port 5432. You must have the PostgreSQL server
+binaries, including the regression test driver pg_regress, in your $PATH.
+
+By default, the regression tests use the driver built from the parent directory,
+../.libs/psqlodbcw.so, for the tests. You can edit odbcinst.ini in this
+directory to test a different version.
+
+Running the tests
+-----------------
+
+To run the test suite, type:
+
+  make installcheck
+
+The PostgreSQL username used for the test is determined by the normal ODBC /
+libpq rules. You can set the PGUSER environment variable or .pgpass to
+override.
+
+Development
+-----------
+
+To add a test, add a *-test.c file to src/ directory, using one of the
+existing tests as example. Also add the test to the TESTS list in the Makefile,
+and create an expected output file in expected/ directory.
+
+The current test suite only tests a small fraction of the codebase. Whenever
+you add a new feature, or fix a non-trivial bug, please add a test case to
+cover it.
diff --git a/test/expected/alter.out b/test/expected/alter.out
new file mode 100644 (file)
index 0000000..c44486e
--- /dev/null
@@ -0,0 +1,7 @@
+\! ./src/alter-test
+connected
+Result set metadata:
+t: VARCHAR(40) digits: 0, nullable
+Result set metadata:
+t: VARCHAR(80) digits: 0, nullable
+disconnecting
diff --git a/test/expected/arraybinding.out b/test/expected/arraybinding.out
new file mode 100644 (file)
index 0000000..53700ce
--- /dev/null
@@ -0,0 +1,30 @@
+\! ./src/arraybinding-test
+connected
+Parameter  Status
+Result set:
+10000
+Result set:
+0  columnwise 0
+1  columnwise 1
+100    columnwise 100
+9999   columnwise 9999
+Parameter  Status
+Fetching result sets for array bound (5 results expected)
+1: Result set:
+columnwise 100
+2: Result set:
+columnwise 101
+3: Result set:
+columnwise 102
+4: Result set:
+columnwise 103
+5: Result set:
+columnwise 104
+Number of rows in table:
+Result set:
+9995
+Result set:
+0  columnwise 0
+1  columnwise 1
+9999   columnwise 9999
+disconnecting
diff --git a/test/expected/boolsaschar.out b/test/expected/boolsaschar.out
new file mode 100644 (file)
index 0000000..566dd50
--- /dev/null
@@ -0,0 +1,17 @@
+\! ./src/boolsaschar-test
+connected
+Result set metadata:
+id: INTEGER(10) digits: 0, nullable
+t: VARCHAR(5) digits: 0, nullable
+b: VARCHAR(5) digits: 0, nullable
+Result set:
+2  yes 1
+Result set metadata:
+id: INTEGER(10) digits: 0, nullable
+t: VARCHAR(5) digits: 0, nullable
+b: VARCHAR(5) digits: 0, nullable
+Result set:
+1  yeah    1
+2  yes 1
+3  true    1
+disconnecting
diff --git a/test/expected/connect.out b/test/expected/connect.out
new file mode 100644 (file)
index 0000000..3762ffe
--- /dev/null
@@ -0,0 +1,3 @@
+\! ./src/connect-test
+connected
+disconnecting
diff --git a/test/expected/cvtnulldate.out b/test/expected/cvtnulldate.out
new file mode 100644 (file)
index 0000000..f02f312
--- /dev/null
@@ -0,0 +1,5 @@
+\! ./src/cvtnulldate-test
+connected
+Result set:
+1
+disconnecting
diff --git a/test/expected/dataatexecution.out b/test/expected/dataatexecution.out
new file mode 100644 (file)
index 0000000..9adc1c9
--- /dev/null
@@ -0,0 +1,12 @@
+\! ./src/dataatexecution-test
+connected
+Result set:
+2
+3
+Parameter  Status
+Fetching result sets for array bound (2 results expected)
+1: Result set:
+4
+2: Result set:
+5
+disconnecting
diff --git a/test/expected/getresult.out b/test/expected/getresult.out
new file mode 100644 (file)
index 0000000..e400929
--- /dev/null
@@ -0,0 +1,9 @@
+\! ./src/getresult-test
+connected
+Result set:
+varcharcol: foo
+integercol: 123
+intervalyears: 10
+intervalmonths: 11
+intervaldays: 12
+disconnecting
diff --git a/test/expected/insertreturning.out b/test/expected/insertreturning.out
new file mode 100644 (file)
index 0000000..01617a8
--- /dev/null
@@ -0,0 +1,303 @@
+\! ./src/insertreturning-test
+connected
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 0
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 1
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 2
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 3
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 4
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 5
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 6
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 7
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 8
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 9
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 10
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 11
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 12
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 13
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 14
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 15
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 16
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 17
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 18
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 19
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 20
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 21
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 22
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 23
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 24
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 25
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 26
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 27
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 28
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 29
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 30
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 31
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 32
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 33
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 34
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 35
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 36
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 37
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 38
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 39
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 40
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 41
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 42
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 43
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 44
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 45
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 46
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 47
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 48
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 49
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 50
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 51
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 52
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 53
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 54
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 55
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 56
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 57
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 58
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 59
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 60
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 61
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 62
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 63
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 64
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 65
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 66
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 67
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 68
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 69
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 70
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 71
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 72
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 73
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 74
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 75
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 76
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 77
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 78
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 79
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 80
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 81
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 82
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 83
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 84
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 85
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 86
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 87
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 88
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 89
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 90
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 91
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 92
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 93
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 94
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 95
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 96
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 97
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 98
+# of result cols before SQLExecute: 0, after: 1
+Result set:
+foobar 99
+disconnecting
diff --git a/test/expected/notice.out b/test/expected/notice.out
new file mode 100644 (file)
index 0000000..ad449aa
--- /dev/null
@@ -0,0 +1,7 @@
+\! ./src/notice-test
+connected
+got SUCCESS_WITH_INFO
+00000=NOTICE: test notice: foo
+got SUCCESS_WITH_INFO
+00000=
+disconnecting
diff --git a/test/expected/params.out b/test/expected/params.out
new file mode 100644 (file)
index 0000000..3a9267c
--- /dev/null
@@ -0,0 +1,6 @@
+\! ./src/params-test
+connected
+# of result cols: 2
+Result set:
+1  0102030405060708
+disconnecting
diff --git a/test/expected/prepare.out b/test/expected/prepare.out
new file mode 100644 (file)
index 0000000..d89e5c3
--- /dev/null
@@ -0,0 +1,14 @@
+\! ./src/prepare-test
+connected
+# of result cols: 2
+Result set:
+2  bar
+Result set:
+3  foobar
+Result set:
+1  @ 1 day one day
+2  @ 10 secs   ten secs
+# of result cols: 2
+Result set:
+2  bar
+disconnecting
diff --git a/test/expected/sampletables.out b/test/expected/sampletables.out
new file mode 100644 (file)
index 0000000..51b9073
--- /dev/null
@@ -0,0 +1,21 @@
+-- This file creates some tables to be used in the tests
+CREATE TABLE testtab1 (id integer, t varchar(20));
+INSERT INTO testtab1 VALUES (1, 'foo');
+INSERT INTO testtab1 VALUES (2, 'bar');
+INSERT INTO testtab1 VALUES (3, 'foobar');
+CREATE TABLE byteatab (id integer, t bytea);
+INSERT INTO byteatab VALUES (1, E'\\001\\002\\003\\004\\005\\006\\007\\010'::bytea);
+INSERT INTO byteatab VALUES (2, 'bar');
+INSERT INTO byteatab VALUES (3, 'foobar');
+INSERT INTO byteatab VALUES (4, 'foo');
+INSERT INTO byteatab VALUES (5, 'barf');
+CREATE TABLE intervaltable(id integer, iv interval, d varchar(100));
+INSERT INTO intervaltable VALUES (1, '1 day', 'one day');
+INSERT INTO intervaltable VALUES (2, '10 seconds', 'ten secs');
+INSERT INTO intervaltable VALUES (3, '100 years', 'hundred years');
+CREATE TABLE booltab (id integer, t varchar(5), b boolean);
+INSERT INTO booltab VALUES (1, 'yeah', true);
+INSERT INTO booltab VALUES (2, 'yes', true);
+INSERT INTO booltab VALUES (3, 'true', true);
+INSERT INTO booltab VALUES (4, 'false', false);
+INSERT INTO booltab VALUES (5, 'not', false);
diff --git a/test/expected/select.out b/test/expected/select.out
new file mode 100644 (file)
index 0000000..2fe6fe3
--- /dev/null
@@ -0,0 +1,8 @@
+\! ./src/select-test
+connected
+Result set:
+1
+2
+Result set:
+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 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000    1001    1002    1003    1004    1005    1006    1007    1008    1009    1010    1011    1012    1013    1014    1015    1016    1017    1018    1019    1020    1021    1022    1023    1024    1025    1026    1027    1028    1029    1030    1031    1032    1033    1034    1035    1036    1037    1038    1039    1040    1041    1042    1043    1044    1045    1046    1047    1048    1049    1050    1051    1052    1053    1054    1055    1056    1057    1058    1059    1060    1061    1062    1063    1064    1065    1066    1067    1068    1069    1070    1071    1072    1073    1074    1075    1076    1077    1078    1079    1080    1081    1082    1083    1084    1085    1086    1087    1088    1089    1090    1091    1092    1093    1094    1095    1096    1097    1098    1099    1100    1101    1102    1103    1104    1105    1106    1107    1108    1109    1110    1111    1112    1113    1114    1115    1116    1117    1118    1119    1120    1121    1122    1123    1124    1125    1126    1127    1128    1129    1130    1131    1132    1133    1134    1135    1136    1137    1138    1139    1140    1141    1142    1143    1144    1145    1146    1147    1148    1149    1150    1151    1152    1153    1154    1155    1156    1157    1158    1159    1160    1161    1162    1163    1164    1165    1166    1167    1168    1169    1170    1171    1172    1173    1174    1175    1176    1177    1178    1179    1180    1181    1182    1183    1184    1185    1186    1187    1188    1189    1190    1191    1192    1193    1194    1195    1196    1197    1198    1199    1200    1201    1202    1203    1204    1205    1206    1207    1208    1209    1210    1211    1212    1213    1214    1215    1216    1217    1218    1219    1220    1221    1222    1223    1224    1225    1226    1227    1228    1229    1230    1231    1232    1233    1234    1235    1236    1237    1238    1239    1240    1241    1242    1243    1244    1245    1246    1247    1248    1249    1250    1251    1252    1253    1254    1255    1256    1257    1258    1259    1260    1261    1262    1263    1264    1265    1266    1267    1268    1269    1270    1271    1272    1273    1274    1275    1276    1277    1278    1279    1280    1281    1282    1283    1284    1285    1286    1287    1288    1289    1290    1291    1292    1293    1294    1295    1296    1297    1298    1299    1300    1301    1302    1303    1304    1305    1306    1307    1308    1309    1310    1311    1312    1313    1314    1315    1316    1317    1318    1319    1320    1321    1322    1323    1324    1325    1326    1327    1328    1329    1330    1331    1332    1333    1334    1335    1336    1337    1338    1339    1340    1341    1342    1343    1344    1345    1346    1347    1348    1349    1350    1351    1352    1353    1354    1355    1356    1357    1358    1359    1360    1361    1362    1363    1364    1365    1366    1367    1368    1369    1370    1371    1372    1373    1374    1375    1376    1377    1378    1379    1380    1381    1382    1383    1384    1385    1386    1387    1388    1389    1390    1391    1392    1393    1394    1395    1396    1397    1398    1399    1400    1401    1402    1403    1404    1405    1406    1407    1408    1409    1410    1411    1412    1413    1414    1415    1416    1417    1418    1419    1420    1421    1422    1423    1424    1425    1426    1427    1428    1429    1430    1431    1432    1433    1434    1435    1436    1437    1438    1439    1440    1441    1442    1443    1444    1445    1446    1447    1448    1449    1450    1451    1452    1453    1454    1455    1456    1457    1458    1459    1460    1461    1462    1463    1464    1465    1466    1467    1468    1469    1470    1471    1472    1473    1474    1475    1476    1477    1478    1479    1480    1481    1482    1483    1484    1485    1486    1487    1488    1489    1490    1491    1492    1493    1494    1495    1496    1497    1498    1499    1500    1501    1502    1503    1504    1505    1506    1507    1508    1509    1510    1511    1512    1513    1514    1515    1516    1517    1518    1519    1520    1521    1522    1523    1524    1525    1526    1527    1528    1529    1530    1531    1532    1533    1534    1535    1536    1537    1538    1539    1540    1541    1542    1543    1544    1545    1546    1547    1548    1549    1550    1551    1552    1553    1554    1555    1556    1557    1558    1559    1560    1561    1562    1563    1564    1565    1566    1567    1568    1569    1570    1571    1572    1573    1574    1575    1576    1577    1578    1579    1580    1581    1582    1583    1584    1585    1586    1587    1588    1589    1590    1591    1592    1593    1594    1595    1596    1597    1598    1599    1600
+disconnecting
diff --git a/test/expected/stmthandles.out b/test/expected/stmthandles.out
new file mode 100644 (file)
index 0000000..b0b10ba
--- /dev/null
@@ -0,0 +1,43 @@
+\! ./src/stmthandles-test
+connected
+10 statements allocated...
+20 statements allocated...
+30 statements allocated...
+40 statements allocated...
+50 statements allocated...
+60 statements allocated...
+70 statements allocated...
+80 statements allocated...
+90 statements allocated...
+100 statements allocated...
+10 statements executed...
+20 statements executed...
+30 statements executed...
+40 statements executed...
+50 statements executed...
+60 statements executed...
+70 statements executed...
+80 statements executed...
+90 statements executed...
+100 statements executed...
+Result set:
+stmt no 1
+Result set:
+stmt no 11
+Result set:
+stmt no 21
+Result set:
+stmt no 31
+Result set:
+stmt no 41
+Result set:
+stmt no 51
+Result set:
+stmt no 61
+Result set:
+stmt no 71
+Result set:
+stmt no 81
+Result set:
+stmt no 91
+disconnecting
diff --git a/test/launcher b/test/launcher
new file mode 100755 (executable)
index 0000000..08d00f2
--- /dev/null
@@ -0,0 +1,5 @@
+#!/bin/bash
+
+# Pass ODBCSYSINI env variable to psql, so that it finds the odbc.ini and
+# odbcinst.ini files from the current dir.
+ODBCSYSINI=. $*
\ No newline at end of file
diff --git a/test/odbc.ini b/test/odbc.ini
new file mode 100644 (file)
index 0000000..2068306
--- /dev/null
@@ -0,0 +1,17 @@
+[psqlodbc_test_dsn]
+Description             = psqlodbc regression test DSN
+Driver          = psqlodbc test driver
+Trace           = No
+TraceFile               = 
+Database                = contrib_regression
+Servername              = localhost
+Username                = 
+Password                = 
+Port            = 5432
+Protocol                = 6.4
+ReadOnly                = No
+RowVersioning           = No
+ShowSystemTables                = No
+ShowOidColumn           = No
+FakeOidIndex            = No
+ConnSettings            = 
diff --git a/test/odbcinst.ini b/test/odbcinst.ini
new file mode 100644 (file)
index 0000000..bf2648c
--- /dev/null
@@ -0,0 +1,5 @@
+[psqlodbc test driver]
+Description     = PostgreSQL ODBC driver (Unicode version), for regression tests
+Driver          = ../.libs/psqlodbcw.so
+Debug           = 0
+CommLog         = 1
diff --git a/test/sql/.gitignore b/test/sql/.gitignore
new file mode 100644 (file)
index 0000000..7739451
--- /dev/null
@@ -0,0 +1,2 @@
+*.sql
+!sampletables.sql
diff --git a/test/sql/sampletables.sql b/test/sql/sampletables.sql
new file mode 100644 (file)
index 0000000..78a7eb6
--- /dev/null
@@ -0,0 +1,25 @@
+-- This file creates some tables to be used in the tests
+
+CREATE TABLE testtab1 (id integer, t varchar(20));
+INSERT INTO testtab1 VALUES (1, 'foo');
+INSERT INTO testtab1 VALUES (2, 'bar');
+INSERT INTO testtab1 VALUES (3, 'foobar');
+
+CREATE TABLE byteatab (id integer, t bytea);
+INSERT INTO byteatab VALUES (1, E'\\001\\002\\003\\004\\005\\006\\007\\010'::bytea);
+INSERT INTO byteatab VALUES (2, 'bar');
+INSERT INTO byteatab VALUES (3, 'foobar');
+INSERT INTO byteatab VALUES (4, 'foo');
+INSERT INTO byteatab VALUES (5, 'barf');
+
+CREATE TABLE intervaltable(id integer, iv interval, d varchar(100));
+INSERT INTO intervaltable VALUES (1, '1 day', 'one day');
+INSERT INTO intervaltable VALUES (2, '10 seconds', 'ten secs');
+INSERT INTO intervaltable VALUES (3, '100 years', 'hundred years');
+
+CREATE TABLE booltab (id integer, t varchar(5), b boolean);
+INSERT INTO booltab VALUES (1, 'yeah', true);
+INSERT INTO booltab VALUES (2, 'yes', true);
+INSERT INTO booltab VALUES (3, 'true', true);
+INSERT INTO booltab VALUES (4, 'false', false);
+INSERT INTO booltab VALUES (5, 'not', false);
diff --git a/test/src/.gitignore b/test/src/.gitignore
new file mode 100644 (file)
index 0000000..c5674c9
--- /dev/null
@@ -0,0 +1 @@
+*-test
diff --git a/test/src/alter-test.c b/test/src/alter-test.c
new file mode 100644 (file)
index 0000000..b21a0b9
--- /dev/null
@@ -0,0 +1,51 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "common.h"
+
+int main(int argc, char **argv)
+{
+   SQLRETURN rc;
+   HSTMT hstmt = SQL_NULL_HSTMT;
+
+   test_connect();
+
+   rc = SQLAllocStmt(conn, &hstmt);
+   if (!SQL_SUCCEEDED(rc))
+   {
+       print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn);
+       exit(1);
+   }
+
+   /* Create a table to test with */
+   rc = SQLExecDirect(hstmt, (SQLCHAR *) "CREATE TEMPORARY TABLE testtbl(t varchar(40))", SQL_NTS);
+   CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
+
+   /**** A simple query against the table, fetch column info ****/
+
+   rc = SQLExecDirect(hstmt, "SELECT * FROM testtbl", SQL_NTS);
+   CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
+
+   /* Get column metadata */
+   print_result_meta(hstmt);
+
+   rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+   CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
+
+   /* Alter the table */
+   rc = SQLExecDirect(hstmt, (SQLCHAR *) "ALTER TABLE testtbl ALTER COLUMN t TYPE varchar(80)", SQL_NTS);
+   CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
+
+   /* Run the query again, check if the metadata was updated */
+
+   rc = SQLExecDirect(hstmt, "SELECT * FROM testtbl", SQL_NTS);
+   CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
+
+   /* Get column metadata */
+   print_result_meta(hstmt);
+
+   /* Clean up */
+   test_disconnect();
+
+   return 0;
+}
diff --git a/test/src/arraybinding-test.c b/test/src/arraybinding-test.c
new file mode 100644 (file)
index 0000000..9fcbe97
--- /dev/null
@@ -0,0 +1,231 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "common.h"
+
+#define ARRAY_SIZE 10000
+#define ARRAY_SIZE_SMALL 5
+
+int main(int argc, char **argv)
+{
+   SQLRETURN rc;
+   HSTMT hstmt = SQL_NULL_HSTMT;
+   char *sql;
+
+   int i;
+
+   SQLUINTEGER int_array[ARRAY_SIZE];
+   SQLCHAR str_array[ARRAY_SIZE][30];
+   SQLCHAR str_array2[ARRAY_SIZE][6];
+   SQLLEN int_ind_array[ARRAY_SIZE];
+   SQLLEN str_ind_array[ARRAY_SIZE];
+   SQLUSMALLINT status_array[ARRAY_SIZE];
+   SQLULEN nprocessed;
+
+   test_connect();
+
+   rc = SQLAllocStmt(conn, &hstmt);
+   if (!SQL_SUCCEEDED(rc))
+   {
+       print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn);
+       exit(1);
+   }
+
+   sql = "CREATE TEMPORARY TABLE tmptable (i int4, t text)";
+   rc = SQLExecDirect(hstmt, (SQLCHAR *) sql, SQL_NTS);
+   CHECK_STMT_RESULT(rc, "SQLExecDirect failed while creating temp table", hstmt);
+
+   rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+   CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
+
+   /****
+    * 1. Test column-wise binding
+    */
+   for (i = 0; i < ARRAY_SIZE; i++)
+   {
+       int_array[i] = i;
+       int_ind_array[i] = 0;
+       sprintf(str_array[i], "columnwise %d", i);
+       str_ind_array[i] = SQL_NTS;
+   }
+
+   SQLSetStmtAttr(hstmt, SQL_ATTR_PARAM_BIND_TYPE, SQL_PARAM_BIND_BY_COLUMN, 0);
+   SQLSetStmtAttr(hstmt, SQL_ATTR_PARAM_STATUS_PTR, status_array, 0);
+   SQLSetStmtAttr(hstmt, SQL_ATTR_PARAMS_PROCESSED_PTR, &nprocessed, 0);
+   SQLSetStmtAttr(hstmt, SQL_ATTR_PARAMSET_SIZE, (SQLPOINTER) ARRAY_SIZE, 0);
+
+   /* Bind the parameter arrays. */
+   SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_INTEGER, 5, 0,
+                    int_array, 0, int_ind_array);
+   SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 29, 0,
+                    str_array, 30, str_ind_array);
+
+   /* Execute */
+   sql = "INSERT INTO tmptable VALUES (?, ?)";
+   rc = SQLExecDirect(hstmt, (SQLCHAR *) sql, SQL_NTS);
+   CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
+
+   /* Fetch results */
+   printf("Parameter   Status\n");
+   for (i = 0; i < nprocessed; i++)
+   {
+       switch (status_array[i])
+       {
+           case SQL_PARAM_SUCCESS:
+           case SQL_PARAM_SUCCESS_WITH_INFO:
+               break;
+
+           case SQL_PARAM_ERROR:
+               printf("%d\tError\n", i);
+               break;
+
+           case SQL_PARAM_UNUSED:
+               printf("%d\tUnused\n", i);
+               break;
+
+           case SQL_PARAM_DIAG_UNAVAILABLE:
+               printf("%d\tDiag unavailable\n", i);
+               break;
+       }
+   }
+
+   rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+   CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
+
+   /*
+    * Free and allocate a new handle for the next SELECT statement, as we don't
+    * want to array bind that one. The parameters set with SQLSetStmtAttr
+    * survive SQLFreeStmt.
+    */
+   rc = SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
+   CHECK_STMT_RESULT(rc, "SQLFreeHandle failed", hstmt);
+
+   rc = SQLAllocStmt(conn, &hstmt);
+   if (!SQL_SUCCEEDED(rc))
+   {
+       print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn);
+       exit(1);
+   }
+
+   /* Check that all the rows were inserted */
+   sql = "SELECT COUNT(*) FROM tmptable";
+   rc = SQLExecDirect(hstmt, (SQLCHAR *) sql, SQL_NTS);
+   CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
+   print_result(hstmt);
+
+   rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+   CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
+
+   /* Check the contents of a few rows */
+   sql = "SELECT * FROM tmptable WHERE i IN (0, 1, 100, 9999, 10000) ORDER BY i";
+   rc = SQLExecDirect(hstmt, (SQLCHAR *) sql, SQL_NTS);
+   CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
+   print_result(hstmt);
+
+   rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+   CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
+
+   /****
+    * 2. Test column-wise binding. With a column_size=5 VARCHAR param - that
+    * causes the driver to do a server-side prepare, assuming BoolsAsChar=1.
+    */
+
+   /* a small array will do for this test */
+   for (i = 0; i < ARRAY_SIZE_SMALL; i++)
+   {
+       sprintf(str_array2[i], "%d", 100+i);
+       str_ind_array[i] = SQL_NTS;
+   }
+
+   SQLSetStmtAttr(hstmt, SQL_ATTR_PARAM_BIND_TYPE, SQL_PARAM_BIND_BY_COLUMN, 0);
+   SQLSetStmtAttr(hstmt, SQL_ATTR_PARAM_STATUS_PTR, status_array, 0);
+   SQLSetStmtAttr(hstmt, SQL_ATTR_PARAMS_PROCESSED_PTR, &nprocessed, 0);
+   SQLSetStmtAttr(hstmt, SQL_ATTR_PARAMSET_SIZE, (SQLPOINTER) ARRAY_SIZE_SMALL, 0);
+
+   /* Bind the parameter array. */
+   SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, 5, 0,
+                    str_array2, 6, str_ind_array);
+
+   /* Execute */
+   sql = "DELETE FROM tmptable WHERE i = ? RETURNING (t)";
+   rc = SQLExecDirect(hstmt, (SQLCHAR *) sql, SQL_NTS);
+   CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
+
+   /* Fetch results */
+   printf("Parameter   Status\n");
+   for (i = 0; i < nprocessed; i++)
+   {
+       switch (status_array[i])
+       {
+           case SQL_PARAM_SUCCESS:
+           case SQL_PARAM_SUCCESS_WITH_INFO:
+               break;
+
+           case SQL_PARAM_ERROR:
+               printf("%d\tError\n", i);
+               break;
+
+           case SQL_PARAM_UNUSED:
+               printf("%d\tUnused\n", i);
+               break;
+
+           case SQL_PARAM_DIAG_UNAVAILABLE:
+               printf("%d\tDiag unavailable\n", i);
+               break;
+       }
+   }
+
+   printf ("Fetching result sets for array bound (%d results expected)\n",
+           nprocessed);
+   for (i = 1; rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO; i++)
+   {
+       printf("%d: ", i);
+       print_result(hstmt);
+
+       rc = SQLMoreResults(hstmt);
+   }
+   if (rc != SQL_NO_DATA)
+       CHECK_STMT_RESULT(rc, "SQLMoreResults failed", hstmt);
+
+   rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+   CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
+
+   /*
+    * Free and allocate a new handle for the next SELECT statement, as we don't
+    * want to array bind that one. The parameters set with SQLSetStmtAttr
+    * survive SQLFreeStmt.
+    */
+   rc = SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
+   CHECK_STMT_RESULT(rc, "SQLFreeHandle failed", hstmt);
+
+   rc = SQLAllocStmt(conn, &hstmt);
+   if (!SQL_SUCCEEDED(rc))
+   {
+       print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn);
+       exit(1);
+   }
+
+   /* Check that all the rows were inserted */
+   printf("Number of rows in table:\n");
+   sql = "SELECT COUNT(*) FROM tmptable";
+   rc = SQLExecDirect(hstmt, (SQLCHAR *) sql, SQL_NTS);
+   CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
+   print_result(hstmt);
+
+   rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+   CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
+
+   /* Check the contents of a few rows */
+   sql = "SELECT * FROM tmptable WHERE i IN (0, 1, 100, 9999, 10000) ORDER BY i";
+   rc = SQLExecDirect(hstmt, (SQLCHAR *) sql, SQL_NTS);
+   CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
+   print_result(hstmt);
+
+   rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+   CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
+
+   /* Clean up */
+   test_disconnect();
+
+   return 0;
+}
diff --git a/test/src/boolsaschar-test.c b/test/src/boolsaschar-test.c
new file mode 100644 (file)
index 0000000..cd660f4
--- /dev/null
@@ -0,0 +1,86 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "common.h"
+
+int main(int argc, char **argv)
+{
+   SQLRETURN rc;
+   HSTMT hstmt = SQL_NULL_HSTMT;
+   char *param1;
+   SQLLEN cbParam1;
+   long longparam;
+   SQL_INTERVAL_STRUCT intervalparam;
+   SQLSMALLINT colcount;
+
+   /* BoolsAsChar is the default, but just in case.. */
+   test_connect_ext("BoolsAsChar=1;UseServerSidePrepare=1");
+
+   rc = SQLAllocStmt(conn, &hstmt);
+   if (!SQL_SUCCEEDED(rc))
+   {
+       print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn);
+       exit(1);
+   }
+
+   /**** A simple query with one text param ****/
+
+   /* Prepare a statement */
+   rc = SQLPrepare(hstmt, (SQLCHAR *) "SELECT id, t, b FROM booltab WHERE t = ?", SQL_NTS);
+   CHECK_STMT_RESULT(rc, "SQLPrepare failed", hstmt);
+
+   /* bind param  */
+   param1 = "yes";
+   cbParam1 = SQL_NTS;
+   rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT,
+                         SQL_C_CHAR,   /* value type */
+                         SQL_VARCHAR,  /* param type */
+                         5,            /* column size */
+                         0,            /* dec digits */
+                         param1,       /* param value ptr */
+                         0,            /* buffer len */
+                         &cbParam1     /* StrLen_or_IndPtr */);
+   CHECK_STMT_RESULT(rc, "SQLBindParameter failed", hstmt);
+
+   /* Execute */
+   rc = SQLExecute(hstmt);
+   CHECK_STMT_RESULT(rc, "SQLExecute failed", hstmt);
+
+   /* Fetch result */
+   print_result_meta(hstmt);
+   print_result(hstmt);
+
+   rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+   CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
+
+   /**** A simple query with one boolean param (passed as varchar) ****/
+
+   /* bind param  */
+   param1 = "true";
+   cbParam1 = SQL_NTS;
+   rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT,
+                         SQL_C_CHAR,   /* value type */
+                         SQL_VARCHAR,  /* param type */
+                         5,            /* column size */
+                         0,            /* dec digits */
+                         param1,       /* param value ptr */
+                         0,            /* buffer len */
+                         &cbParam1     /* StrLen_or_IndPtr */);
+   CHECK_STMT_RESULT(rc, "SQLBindParameter failed", hstmt);
+
+   /* Execute */
+   rc = SQLExecDirect(hstmt, (SQLCHAR *) "SELECT id, t, b FROM booltab WHERE b = ?", SQL_NTS);
+   CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
+
+   /* Fetch result */
+   print_result_meta(hstmt);
+   print_result(hstmt);
+
+   rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+   CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
+
+   /* Clean up */
+   test_disconnect();
+
+   return 0;
+}
diff --git a/test/src/common.c b/test/src/common.c
new file mode 100644 (file)
index 0000000..57c9e4e
--- /dev/null
@@ -0,0 +1,253 @@
+#include "common.h"
+
+SQLHENV env;
+SQLHDBC conn;
+
+void
+print_diag(char *msg, SQLSMALLINT htype, SQLHANDLE handle)
+{
+   char sqlstate[32];
+   char message[1000];
+   SQLINTEGER nativeerror;
+   SQLSMALLINT textlen;
+   SQLRETURN ret;
+
+   if (msg)
+       printf("%s\n", msg);
+
+   ret = SQLGetDiagRec(htype, handle, 1, sqlstate, &nativeerror,
+                       message, 256, &textlen);
+
+   if (ret != SQL_ERROR)
+       printf("%s=%s\n", (CHAR *)sqlstate, (CHAR *)message);
+}
+
+void
+test_connect_ext(char *extraparams)
+{
+   SQLRETURN ret;
+   SQLCHAR str[1024];
+   SQLSMALLINT strl;
+   SQLCHAR dsn[1024];
+
+   snprintf(dsn, sizeof(dsn), "DSN=psqlodbc_test_dsn;%s",
+            extraparams ? extraparams : "");
+
+   SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
+
+   SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
+
+   SQLAllocHandle(SQL_HANDLE_DBC, env, &conn);
+   ret = SQLDriverConnect(conn, NULL, dsn, SQL_NTS,
+                          str, sizeof(str), &strl,
+                          SQL_DRIVER_COMPLETE);
+   if (SQL_SUCCEEDED(ret)) {
+       printf("connected\n");
+   } else {
+       print_diag("SQLDriverConnect failed.", SQL_HANDLE_DBC, conn);
+       exit(1);
+   }
+}
+
+void
+test_connect(void)
+{
+   test_connect_ext(NULL);
+}
+
+void
+test_disconnect(void)
+{
+   SQLRETURN rc;
+
+   printf("disconnecting\n");
+   rc = SQLDisconnect(conn);
+   if (!SQL_SUCCEEDED(rc))
+   {
+       print_diag("SQLDisconnect failed", SQL_HANDLE_DBC, conn);
+       exit(1);
+   }
+
+   rc = SQLFreeConnect(conn);
+   if (!SQL_SUCCEEDED(rc))
+   {
+       print_diag("SQLFreeConnect failed", SQL_HANDLE_DBC, conn);
+       exit(1);
+   }
+   conn = NULL;
+
+   rc = SQLFreeEnv(env);
+   if (!SQL_SUCCEEDED(rc))
+   {
+       print_diag("SQLFreeEnv failed", SQL_HANDLE_ENV, env);
+       exit(1);
+   }
+   env = NULL;
+}
+
+static const char *
+datatype_str(SQLSMALLINT datatype)
+{
+   static char buf[100];
+
+   switch (datatype)
+   {
+       case SQL_CHAR:
+           return "CHAR";
+       case SQL_VARCHAR:
+           return "VARCHAR";
+       case SQL_LONGVARCHAR:
+           return "LONGVARCHAR";
+       case SQL_WCHAR:
+           return "WCHAR";
+       case SQL_WVARCHAR:
+           return "WVARCHAR";
+       case SQL_WLONGVARCHAR:
+           return "WLONGVARCHAR";
+       case SQL_DECIMAL:
+           return "DECIMAL";
+       case SQL_NUMERIC:
+           return "NUMERIC";
+       case SQL_SMALLINT:
+           return "SMALLINT";
+       case SQL_INTEGER:
+           return "INTEGER";
+       case SQL_REAL:
+           return "REAL";
+       case SQL_FLOAT:
+           return "FLOAT";
+       case SQL_DOUBLE:
+           return "DOUBLE";
+       case SQL_BIT:
+           return "BIT";
+       case SQL_TINYINT:
+           return "TINYINT";
+       case SQL_BIGINT:
+           return "BIGINT";
+       case SQL_BINARY:
+           return "BINARY";
+       case SQL_VARBINARY:
+           return "VARBINARY";
+       case SQL_LONGVARBINARY:
+           return "LONGVARBINARY";
+       case SQL_TYPE_DATE:
+           return "TYPE_DATE";
+       case SQL_TYPE_TIME:
+           return "TYPE_TIME";
+       case SQL_TYPE_TIMESTAMP:
+           return "TYPE_TIMESTAMP";
+       case SQL_GUID:
+           return "GUID";
+       default:
+           snprintf(buf, sizeof(buf), "unknown sql type %d", datatype);
+           return buf;
+   }
+}
+
+static const char *nullable_str(SQLSMALLINT nullable)
+{
+   static char buf[100];
+
+   switch(nullable)
+   {
+       case SQL_NO_NULLS:
+           return "not nullable";
+       case SQL_NULLABLE:
+           return "nullable";
+       case SQL_NULLABLE_UNKNOWN:
+           return "nullable_unknown";
+       default:
+           snprintf(buf, sizeof(buf), "unknown nullable value %d", nullable);
+           return buf;
+   }
+}
+
+void
+print_result_meta(HSTMT hstmt)
+{
+   SQLRETURN rc;
+   SQLSMALLINT numcols;
+   int i;
+
+   rc = SQLNumResultCols(hstmt, &numcols);
+   if (!SQL_SUCCEEDED(rc))
+   {
+       print_diag("SQLNumResultCols failed", SQL_HANDLE_STMT, hstmt);
+       return;
+   }
+
+   printf("Result set metadata:\n");
+
+   for (i = 1; i <= numcols; i++)
+   {
+       SQLCHAR colname[50];
+       SQLSMALLINT colnamelen;
+       SQLSMALLINT datatype;
+       SQLULEN colsize;
+       SQLSMALLINT decdigits;
+       SQLSMALLINT nullable;
+
+       rc = SQLDescribeCol(hstmt, i,
+                           colname, sizeof(colname),
+                           &colnamelen,
+                           &datatype,
+                           &colsize,
+                           &decdigits,
+                           &nullable);
+       if (!SQL_SUCCEEDED(rc))
+       {
+           print_diag("SQLDescribeCol failed", SQL_HANDLE_STMT, hstmt);
+           return;
+       }
+       printf("%s: %s(%d) digits: %d, %s\n",
+              colname, datatype_str(datatype), colsize,
+              decdigits, nullable_str(nullable));
+   }
+}
+
+void
+print_result(HSTMT hstmt)
+{
+   SQLRETURN rc;
+   SQLSMALLINT numcols;
+
+   rc = SQLNumResultCols(hstmt, &numcols);
+   if (!SQL_SUCCEEDED(rc))
+   {
+       print_diag("SQLNumResultCols failed", SQL_HANDLE_STMT, hstmt);
+       return;
+   }
+
+   printf("Result set:\n");
+   while(1)
+   {
+       rc = SQLFetch(hstmt);
+       if (rc == SQL_NO_DATA) 
+           break;
+       if (rc == SQL_SUCCESS)
+       {
+           char buf[40];
+           int i;
+           SQLLEN ind;
+
+           for (i = 1; i <= numcols; i++)
+           {
+               rc = SQLGetData(hstmt,i, SQL_C_CHAR, buf, sizeof(buf), &ind);
+               if (!SQL_SUCCEEDED(rc))
+               {
+                   print_diag("SQLGetData failed", SQL_HANDLE_STMT, hstmt);
+                   return;
+               }
+               if (ind == SQL_NULL_DATA)
+                   strcpy(buf, "NULL");
+               printf("%s%s", (i > 1) ? "\t" : "", buf);
+           }
+           printf("\n");
+       }
+       else
+       {
+           print_diag("SQLFetch failed", SQL_HANDLE_STMT, hstmt);
+           exit(1);
+       }
+   }
+}
diff --git a/test/src/common.h b/test/src/common.h
new file mode 100644 (file)
index 0000000..7c47f5b
--- /dev/null
@@ -0,0 +1,31 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+#ifdef WIN32
+#include <windows.h>
+#endif
+
+#include <sql.h>
+#include <sqlext.h>
+
+#ifdef WIN32
+#define snprintf _snprintf
+#endif
+
+extern SQLHENV env;
+extern SQLHDBC conn;
+
+#define CHECK_STMT_RESULT(rc, msg, hstmt)  \
+   if (!SQL_SUCCEEDED(rc)) \
+   { \
+       print_diag(msg, SQL_HANDLE_STMT, hstmt);    \
+       exit(1);                                    \
+    }
+
+extern void print_diag(char *msg, SQLSMALLINT htype, SQLHANDLE handle);
+extern void test_connect_ext(char *extraparams);
+extern void test_connect(void);
+extern void test_disconnect(void);
+extern void print_result_meta(HSTMT hstmt);
+extern void print_result(HSTMT hstmt);
diff --git a/test/src/connect-test.c b/test/src/connect-test.c
new file mode 100644 (file)
index 0000000..7f59cc6
--- /dev/null
@@ -0,0 +1,10 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "common.h"
+
+int main(int argc, char **argv)
+{
+   test_connect();
+   test_disconnect();
+}
diff --git a/test/src/cvtnulldate-test.c b/test/src/cvtnulldate-test.c
new file mode 100644 (file)
index 0000000..3e1e178
--- /dev/null
@@ -0,0 +1,63 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "common.h"
+
+int main(int argc, char **argv)
+{
+   SQLRETURN rc;
+   HSTMT hstmt = SQL_NULL_HSTMT;
+   char *param1;
+   SQLLEN cbParam1;
+   long longparam;
+   SQL_INTERVAL_STRUCT intervalparam;
+   SQLSMALLINT colcount;
+
+   test_connect_ext("AB=0x08;UseServerSidePrepare=1");
+
+   rc = SQLAllocStmt(conn, &hstmt);
+   if (!SQL_SUCCEEDED(rc))
+   {
+       print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn);
+       exit(1);
+   }
+
+   /**** A simple query with one text param ****/
+
+   rc = SQLExecDirect(hstmt, (SQLCHAR *) "CREATE TEMPORARY TABLE nulldate (d date)", SQL_NTS);
+   CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
+
+   /*
+    * Bind empty string to the date param. In cvt_null_date mode, the driver
+    * maps it to NULL. (In normal mode, the driver fills in the current date)
+    */
+   param1 = "";
+   cbParam1 = SQL_NTS;
+   rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT,
+                         SQL_C_CHAR,   /* value type */
+                         SQL_CHAR,     /* param type */
+                         5,            /* column size */
+                         0,            /* dec digits */
+                         param1,       /* param value ptr */
+                         0,            /* buffer len */
+                         &cbParam1     /* StrLen_or_IndPtr */);
+   CHECK_STMT_RESULT(rc, "SQLBindParameter failed", hstmt);
+
+   /* Execute */
+   rc = SQLExecDirect(hstmt, (SQLCHAR *) "INSERT INTO nulldate VALUES (?)", SQL_NTS);
+   CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
+
+   /* Check the resulting table */
+   rc = SQLExecDirect(hstmt, (SQLCHAR *) "SELECT d IS NULL FROM nulldate", SQL_NTS);
+   CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
+
+   print_result(hstmt);
+
+   rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+   CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
+
+   /* Clean up */
+   test_disconnect();
+
+   return 0;
+}
diff --git a/test/src/dataatexecution-test.c b/test/src/dataatexecution-test.c
new file mode 100644 (file)
index 0000000..4b91daa
--- /dev/null
@@ -0,0 +1,200 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "common.h"
+
+int main(int argc, char **argv)
+{
+   SQLRETURN rc;
+   HSTMT hstmt = SQL_NULL_HSTMT;
+   char *param1, *param2;
+   SQLLEN cbParam1, cbParam2;
+   SQLLEN param1bytes, param2bytes;
+   SQLSMALLINT colcount;
+   PTR paramid;
+   char buf[40];
+   SQLLEN lenOrInd;
+   SQLCHAR str_array2[2][6];
+   SQLLEN str_ind_array[2];
+   SQLUSMALLINT status_array[2];
+   SQLULEN nprocessed = 0;
+   int i;
+
+   test_connect();
+
+   rc = SQLAllocStmt(conn, &hstmt);
+   if (!SQL_SUCCEEDED(rc))
+   {
+       print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn);
+       exit(1);
+   }
+
+   /****
+    * Bind with data-at-execution params. (VARBINARY)
+    */
+
+   /* Prepare a statement */
+   rc = SQLPrepare(hstmt, (SQLCHAR *) "SELECT id FROM byteatab WHERE t = ? OR t = ?", SQL_NTS);
+   CHECK_STMT_RESULT(rc, "SQLPrepare failed", hstmt);
+
+   /* prepare the parameter values */
+   param1 = "bar";
+   param1bytes = strlen(param1);
+   cbParam1 = SQL_DATA_AT_EXEC;
+   param2 = "foobar";
+   param2bytes = strlen(param2);
+   cbParam2 = SQL_DATA_AT_EXEC;
+
+   /* bind them. */
+   rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT,
+                         SQL_C_BINARY, /* value type */
+                         SQL_VARBINARY, /* param type */
+                         param1bytes,  /* column size */
+                         0,            /* dec digits */
+                         (VOID *) 1,   /* param value ptr. For a data-at-exec
+                                        * param, this is a "parameter id" */
+                         0,            /* buffer len */
+                         &cbParam1     /* StrLen_or_IndPtr */);
+   CHECK_STMT_RESULT(rc, "SQLBindParameter failed", hstmt);
+
+   rc = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT,
+                         SQL_C_BINARY, /* value type */
+                         SQL_VARBINARY, /* param type */
+                         param2bytes,  /* column size */
+                         0,            /* dec digits */
+                         (VOID *) 2,   /* param value ptr. For a data-at-exec
+                                        * param, this is a "parameter id" */
+                         0,            /* buffer len */
+                         &cbParam2     /* StrLen_or_IndPtr */);
+   CHECK_STMT_RESULT(rc, "SQLBindParameter failed", hstmt);
+
+   /* Execute */
+   rc = SQLExecute(hstmt);
+   if (rc != SQL_NEED_DATA)
+       CHECK_STMT_RESULT(rc, "SQLExecute failed", hstmt);
+
+   /* set parameters */
+   paramid = 0;
+   while ((rc = SQLParamData(hstmt, &paramid)) == SQL_NEED_DATA)
+   {
+     if (paramid == (VOID *) 1)
+     {
+         rc = SQLPutData(hstmt, param1, param1bytes);
+         CHECK_STMT_RESULT(rc, "SQLPutData failed", hstmt);
+     }
+     else if (paramid == (VOID *) 2)
+     {
+         rc = SQLPutData(hstmt, param2, param2bytes);
+         CHECK_STMT_RESULT(rc, "SQLPutData failed", hstmt);
+     }
+     else
+     {
+         printf("unexpected parameter id returned by SQLParamData: %d\n", paramid);
+         exit(1);
+     }
+   }
+   CHECK_STMT_RESULT(rc, "SQLParamData failed", hstmt);
+
+   /* Fetch result */
+   print_result(hstmt);
+
+   rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+   CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
+
+
+   /****
+    * Array binding with data-at-execution params.
+    */
+
+   /* prepare the parameter values */
+   str_ind_array[0] = SQL_DATA_AT_EXEC;
+   str_ind_array[1] = SQL_DATA_AT_EXEC;
+
+   /* Prepare a statement */
+   rc = SQLPrepare(hstmt, (SQLCHAR *) "SELECT id FROM byteatab WHERE t = ?", SQL_NTS);
+   CHECK_STMT_RESULT(rc, "SQLPrepare failed", hstmt);
+
+   SQLSetStmtAttr(hstmt, SQL_ATTR_PARAM_BIND_TYPE, SQL_PARAM_BIND_BY_COLUMN, 0);
+   SQLSetStmtAttr(hstmt, SQL_ATTR_PARAM_STATUS_PTR, status_array, 0);
+   SQLSetStmtAttr(hstmt, SQL_ATTR_PARAMS_PROCESSED_PTR, &nprocessed, 0);
+   SQLSetStmtAttr(hstmt, SQL_ATTR_PARAMSET_SIZE, (SQLPOINTER) 2, 0);
+
+   /* bind the array. */
+   rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT,
+                         SQL_C_BINARY, /* value type */
+                         SQL_VARBINARY, /* param type */
+                         5,            /* column size */
+                         0,            /* dec digits */
+                         (VOID *) 1,   /* param value ptr. For a data-at-exec
+                                        * param, this is "parameter id" */
+                         0,            /* buffer len */
+                         str_ind_array /* StrLen_or_IndPtr */);
+   CHECK_STMT_RESULT(rc, "SQLBindParameter failed", hstmt);
+
+   /* Execute */
+   rc = SQLExecute(hstmt);
+   if (rc != SQL_NEED_DATA)
+       CHECK_STMT_RESULT(rc, "SQLExecute failed", hstmt);
+
+   /* set parameters */
+   paramid = 0;
+   while ((rc = SQLParamData(hstmt, &paramid)) == SQL_NEED_DATA)
+   {
+       if (nprocessed == 1)
+           rc = SQLPutData(hstmt, "foo", strlen("foo"));
+       else if (nprocessed == 2)
+           rc = SQLPutData(hstmt, "barf", strlen("barf"));
+       else
+       {
+           printf("unexpected # of rows processed after SQL_NEED_DATA: %d\n", nprocessed);
+           exit(1);
+       }
+       CHECK_STMT_RESULT(rc, "SQLPutData failed", hstmt);
+   }
+   CHECK_STMT_RESULT(rc, "SQLParamData failed", hstmt);
+
+   /* Fetch results */
+   printf("Parameter   Status\n");
+   for (i = 0; i < nprocessed; i++)
+   {
+       switch (status_array[i])
+       {
+           case SQL_PARAM_SUCCESS:
+           case SQL_PARAM_SUCCESS_WITH_INFO:
+               break;
+
+           case SQL_PARAM_ERROR:
+               printf("%d\tError\n", i);
+               break;
+
+           case SQL_PARAM_UNUSED:
+               printf("%d\tUnused\n", i);
+               break;
+
+           case SQL_PARAM_DIAG_UNAVAILABLE:
+               printf("%d\tDiag unavailable\n", i);
+               break;
+       }
+   }
+
+   printf ("Fetching result sets for array bound (%d results expected)\n",
+           nprocessed);
+   for (i = 1; rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO; i++)
+   {
+       printf("%d: ", i);
+       print_result(hstmt);
+
+       rc = SQLMoreResults(hstmt);
+   }
+   if (rc != SQL_NO_DATA)
+       CHECK_STMT_RESULT(rc, "SQLMoreResults failed", hstmt);
+
+   rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+   CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
+
+   /* Clean up */
+   test_disconnect();
+
+   return 0;
+}
diff --git a/test/src/getresult-test.c b/test/src/getresult-test.c
new file mode 100644 (file)
index 0000000..b7ec67c
--- /dev/null
@@ -0,0 +1,80 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "common.h"
+
+int main(int argc, char **argv)
+{
+   SQLRETURN rc;
+   HSTMT hstmt = SQL_NULL_HSTMT;
+   char *param1;
+   SQLLEN cbParam1;
+   long longparam;
+   SQL_INTERVAL_STRUCT intervalval;
+   char *sql;
+
+   char buf[40];
+   SQLINTEGER ld;
+
+   test_connect();
+
+   rc = SQLAllocStmt(conn, &hstmt);
+   if (!SQL_SUCCEEDED(rc))
+   {
+       print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn);
+       exit(1);
+   }
+
+   /*
+    * The interval stuff requires intervalstyle=postgres at the momemnt.
+    * Someone should fix the driver to understand other formats,
+    * postgres_verbose in particular...
+    */
+   rc = SQLExecDirect(hstmt, (SQLCHAR *) "SET intervalstyle=postgres", SQL_NTS);
+   CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
+   print_result(hstmt);
+
+   rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+   CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
+
+   /* Run a query with a result set with all kinds of values */
+   sql = "SELECT "
+       "'foo'::varchar(10) AS varcharcol,\n"
+       "123::integer as integercol,\n"
+       "'10 years'::interval AS intervalyears,\n"
+       "'11 months'::interval AS intervalmonths,\n"
+       "'12 days'::interval AS intervaldays\n";
+   rc = SQLExecDirect(hstmt, (SQLCHAR *) sql, SQL_NTS);
+   CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
+
+   /* Fetch result */
+
+   rc = SQLFetch(hstmt);
+   CHECK_STMT_RESULT(rc, "SQLFetch failed", hstmt);
+
+   rc = SQLGetData(hstmt, 1, SQL_C_CHAR, buf, sizeof(buf), NULL);
+   CHECK_STMT_RESULT(rc, "SQLGetData failed", hstmt);
+   printf("varcharcol: %s\n", buf);
+
+   rc = SQLGetData(hstmt, 2, SQL_C_LONG, &ld, sizeof(ld), NULL);
+   CHECK_STMT_RESULT(rc, "SQLGetData failed", hstmt);
+   printf("integercol: %ld\n", ld);
+
+   rc = SQLGetData(hstmt, 3, SQL_C_INTERVAL_YEAR, &intervalval, sizeof(intervalval), NULL);
+   CHECK_STMT_RESULT(rc, "SQLGetData failed", hstmt);
+   printf("intervalyears: %ld\n", intervalval.intval.year_month.year);
+
+   rc = SQLGetData(hstmt, 4, SQL_C_INTERVAL_MONTH, &intervalval, sizeof(intervalval), NULL);
+   CHECK_STMT_RESULT(rc, "SQLGetData failed", hstmt);
+   printf("intervalmonths: %ld\n", intervalval.intval.year_month.month);
+
+   rc = SQLGetData(hstmt, 5, SQL_C_INTERVAL_DAY, &intervalval, sizeof(intervalval), NULL);
+   CHECK_STMT_RESULT(rc, "SQLGetData failed", hstmt);
+   printf("intervaldays: %ld\n", intervalval.intval.day_second.day);
+
+   rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+   CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
+
+   /* Clean up */
+   test_disconnect();
+}
diff --git a/test/src/insertreturning-test.c b/test/src/insertreturning-test.c
new file mode 100644 (file)
index 0000000..e0f8ef5
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * INSERT RETURNING tests.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "common.h"
+
+int main(int argc, char **argv)
+{
+   SQLRETURN rc;
+   HSTMT hstmt = SQL_NULL_HSTMT;
+   char param1[100];
+   SQLLEN cbParam1;
+   SQLSMALLINT colcount1, colcount2;
+   SQLCHAR *sql;
+   int i;
+
+   test_connect();
+
+   rc = SQLAllocStmt(conn, &hstmt);
+   if (!SQL_SUCCEEDED(rc))
+   {
+       print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn);
+       exit(1);
+   }
+
+   sql = "CREATE TEMPORARY TABLE tmptable (i int4, t text)";
+   rc = SQLExecDirect(hstmt, (SQLCHAR *) sql, SQL_NTS);
+   CHECK_STMT_RESULT(rc, "SQLExecDirect failed while creating temp table", hstmt);
+
+   /*
+    * We used to have a memory leak when SQLNumResultCols() was called on an
+    * INSERT statement. It's been fixed, but this test case was useful to find
+    * it, when you crank up the number of iterations.
+    */
+   for (i = 0; i < 100; i++)
+   {
+       /* Prepare a statement */
+       rc = SQLPrepare(hstmt, (SQLCHAR *) "INSERT INTO tmptable VALUES (1, ?) RETURNING (t)", SQL_NTS);
+       CHECK_STMT_RESULT(rc, "SQLPrepare failed", hstmt);
+
+       /* bind param  */
+       snprintf(param1, sizeof(param1), "foobar %d", i);
+       cbParam1 = SQL_NTS;
+       rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT,
+                             SQL_C_CHAR,   /* value type */
+                             SQL_CHAR, /* param type */
+                             20,       /* column size */
+                             0,        /* dec digits */
+                             param1,       /* param value ptr */
+                             0,        /* buffer len */
+                             &cbParam1 /* StrLen_or_IndPtr */);
+       CHECK_STMT_RESULT(rc, "SQLBindParameter failed", hstmt);
+
+       /* Test SQLNumResultCols, called before SQLExecute() */
+       rc = SQLNumResultCols(hstmt, &colcount1);
+       CHECK_STMT_RESULT(rc, "SQLNumResultCols failed", hstmt);
+
+       /* Execute */
+       rc = SQLExecute(hstmt);
+       CHECK_STMT_RESULT(rc, "SQLExecute failed", hstmt);
+
+       /* Call SQLNumResultCols again, after SQLExecute() */
+       rc = SQLNumResultCols(hstmt, &colcount2);
+       CHECK_STMT_RESULT(rc, "SQLNumResultCols failed", hstmt);
+
+       printf("# of result cols before SQLExecute: %d, after: %d\n",
+              colcount1, colcount2);
+
+       /* Fetch result */
+       print_result(hstmt);
+
+       rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+       CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
+   }
+
+   rc = SQLFreeStmt(hstmt, SQL_DROP);
+   if (!SQL_SUCCEEDED(rc))
+   {
+       print_diag("SQLFreeStmt failed", SQL_HANDLE_STMT, hstmt);
+       exit(1);
+   }
+
+   /* Clean up */
+   test_disconnect();
+
+   return 0;
+}
diff --git a/test/src/notice-test.c b/test/src/notice-test.c
new file mode 100644 (file)
index 0000000..777c7a8
--- /dev/null
@@ -0,0 +1,85 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "common.h"
+
+int main(int argc, char **argv)
+{
+   int rc;
+   HSTMT hstmt = SQL_NULL_HSTMT;
+   char *sql;
+
+   test_connect();
+
+   rc = SQLAllocStmt(conn, &hstmt);
+   if (!SQL_SUCCEEDED(rc))
+   {
+       print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn);
+       exit(1);
+   }
+
+   sql =
+       "CREATE FUNCTION raisenotice(s text) RETURNS void AS $$"
+       "begin\n"
+       "  raise notice 'test notice: %',s;\n"
+       "end;\n"
+       "$$ LANGUAGE plpgsql";
+
+   rc = SQLExecDirect(hstmt, (SQLCHAR *) sql, SQL_NTS);
+   if (!SQL_SUCCEEDED(rc))
+   {
+       print_diag("SQLExecDirect failed", SQL_HANDLE_STMT, hstmt);
+       exit(1);
+   }
+
+   rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+   if (!SQL_SUCCEEDED(rc))
+   {
+       print_diag("SQLFreeStmt failed", SQL_HANDLE_STMT, hstmt);
+       exit(1);
+   }
+
+   /* Call the function that gives a NOTICE */
+   sql = "SELECT raisenotice('foo')";
+   rc = SQLExecDirect(hstmt, (SQLCHAR *) sql, SQL_NTS);
+   if (!SQL_SUCCEEDED(rc))
+   {
+       print_diag("SQLExecDirect failed", SQL_HANDLE_STMT, hstmt);
+       exit(1);
+   }
+
+   if (rc == SQL_SUCCESS_WITH_INFO)
+       print_diag("got SUCCESS_WITH_INFO", SQL_HANDLE_STMT, hstmt);
+
+   rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+   if (!SQL_SUCCEEDED(rc))
+   {
+       print_diag("SQLFreeStmt failed", SQL_HANDLE_STMT, hstmt);
+       exit(1);
+   }
+
+   /* The same, with a really long notice. XXX: At the moment, this returns
+    * an empty string, as the driver has a built-in limit on the error/notice
+    * size */
+
+   sql = "SELECT raisenotice(repeat('foo', 100))";
+   rc = SQLExecDirect(hstmt, (SQLCHAR *) sql, SQL_NTS);
+   if (!SQL_SUCCEEDED(rc))
+   {
+       print_diag("SQLExecDirect failed", SQL_HANDLE_STMT, hstmt);
+       exit(1);
+   }
+
+   if (rc == SQL_SUCCESS_WITH_INFO)
+       print_diag("got SUCCESS_WITH_INFO", SQL_HANDLE_STMT, hstmt);
+
+   rc = SQLFreeStmt(hstmt, SQL_DROP);
+   if (!SQL_SUCCEEDED(rc))
+   {
+       print_diag("SQLFreeStmt failed", SQL_HANDLE_STMT, hstmt);
+       exit(1);
+   }
+
+   /* Clean up */
+   test_disconnect();
+}
diff --git a/test/src/params-test.c b/test/src/params-test.c
new file mode 100644 (file)
index 0000000..837c98e
--- /dev/null
@@ -0,0 +1,62 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "common.h"
+
+int main(int argc, char **argv)
+{
+   SQLRETURN rc;
+   HSTMT hstmt = SQL_NULL_HSTMT;
+   char param1[20] = { 1, 2, 3, 4, 5, 6, 7, 8 };
+   SQLLEN cbParam1;
+   long longparam;
+   SQL_INTERVAL_STRUCT intervalparam;
+   SQLSMALLINT colcount;
+
+   test_connect();
+
+   rc = SQLAllocStmt(conn, &hstmt);
+   if (!SQL_SUCCEEDED(rc))
+   {
+       print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn);
+       exit(1);
+   }
+
+   /**** Query with a bytea param ****/
+
+   /* Prepare a statement */
+   rc = SQLPrepare(hstmt, (SQLCHAR *) "SELECT id, t FROM byteatab WHERE t = ?", SQL_NTS);
+   CHECK_STMT_RESULT(rc, "SQLPrepare failed", hstmt);
+
+   /* bind param  */
+   cbParam1 = 8;
+   rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT,
+                         SQL_C_BINARY, /* value type */
+                         SQL_BINARY,   /* param type */
+                         20,           /* column size */
+                         0,            /* dec digits */
+                         param1,       /* param value ptr */
+                         0,            /* buffer len */
+                         &cbParam1     /* StrLen_or_IndPtr */);
+   CHECK_STMT_RESULT(rc, "SQLBindParameter failed", hstmt);
+
+   /* Test SQLNumResultCols, called before SQLExecute() */
+   rc = SQLNumResultCols(hstmt, &colcount);
+   CHECK_STMT_RESULT(rc, "SQLNumResultCols failed", hstmt);
+   printf("# of result cols: %d\n", colcount);
+
+   /* Execute */
+   rc = SQLExecute(hstmt);
+   CHECK_STMT_RESULT(rc, "SQLExecute failed", hstmt);
+
+   /* Fetch result */
+   print_result(hstmt);
+
+   rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+   CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
+
+   /* Clean up */
+   test_disconnect();
+
+   return 0;
+}
diff --git a/test/src/prepare-test.c b/test/src/prepare-test.c
new file mode 100644 (file)
index 0000000..c6d618a
--- /dev/null
@@ -0,0 +1,171 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "common.h"
+
+int main(int argc, char **argv)
+{
+   SQLRETURN rc;
+   HSTMT hstmt = SQL_NULL_HSTMT;
+   char *param1;
+   SQLLEN cbParam1;
+   long longparam;
+   SQL_INTERVAL_STRUCT intervalparam;
+   SQLSMALLINT colcount;
+
+   test_connect();
+
+   rc = SQLAllocStmt(conn, &hstmt);
+   if (!SQL_SUCCEEDED(rc))
+   {
+       print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn);
+       exit(1);
+   }
+
+   /**** A simple query with one text param ****/
+
+   /* Prepare a statement */
+   rc = SQLPrepare(hstmt, (SQLCHAR *) "SELECT id, t FROM testtab1 WHERE t = ?", SQL_NTS);
+   CHECK_STMT_RESULT(rc, "SQLPrepare failed", hstmt);
+
+   /* bind param  */
+   param1 = "bar";
+   cbParam1 = SQL_NTS;
+   rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT,
+                         SQL_C_CHAR,   /* value type */
+                         SQL_CHAR,     /* param type */
+                         20,           /* column size */
+                         0,            /* dec digits */
+                         param1,       /* param value ptr */
+                         0,            /* buffer len */
+                         &cbParam1     /* StrLen_or_IndPtr */);
+   CHECK_STMT_RESULT(rc, "SQLBindParameter failed", hstmt);
+
+   /* Test SQLNumResultCols, called before SQLExecute() */
+   rc = SQLNumResultCols(hstmt, &colcount);
+   CHECK_STMT_RESULT(rc, "SQLNumResultCols failed", hstmt);
+   printf("# of result cols: %d\n", colcount);
+
+   /* Execute */
+   rc = SQLExecute(hstmt);
+   CHECK_STMT_RESULT(rc, "SQLExecute failed", hstmt);
+
+   /* Fetch result */
+   print_result(hstmt);
+
+   rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+   CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
+
+   /**** A query with an integer param ****/
+
+   /* Prepare a statement */
+   rc = SQLPrepare(hstmt, (SQLCHAR *) "SELECT id, t FROM testtab1 WHERE id = ?", SQL_NTS);
+   CHECK_STMT_RESULT(rc, "SQLPrepare failed", hstmt);
+
+   /* bind param  */
+   longparam = 3;
+   cbParam1 = sizeof(longparam);
+   rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT,
+                         SQL_C_SLONG,  /* value type */
+                         SQL_INTEGER,  /* param type */
+                         0,            /* column size (ignored for SQL_INTEGER) */
+                         0,            /* dec digits */
+                         &longparam,   /* param value ptr */
+                         sizeof(longparam), /* buffer len (ignored for SQL_INTEGER) */
+                         &cbParam1     /* StrLen_or_IndPtr (ignored for SQL_INTEGER) */);
+   CHECK_STMT_RESULT(rc, "SQLBindParameter failed", hstmt);
+
+   /* Execute */
+   rc = SQLExecute(hstmt);
+   CHECK_STMT_RESULT(rc, "SQLExecute failed", hstmt);
+
+   /* Fetch result */
+   print_result(hstmt);
+
+   rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+   CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
+
+   /**** A query with an interval param (SQL_C_INTERVAL_SECOND) ****/
+
+   /* Prepare a statement */
+   rc = SQLPrepare(hstmt, (SQLCHAR *) "SELECT id, iv, d FROM intervaltable WHERE iv < ?", SQL_NTS);
+   CHECK_STMT_RESULT(rc, "SQLPrepare failed", hstmt);
+
+   /* bind param  */
+   intervalparam.interval_type = SQL_IS_SECOND;
+   intervalparam.interval_sign = 0;
+   intervalparam.intval.day_second.day = 1;
+   intervalparam.intval.day_second.hour = 2;
+   intervalparam.intval.day_second.minute = 3;
+   intervalparam.intval.day_second.second = 4;
+   intervalparam.intval.day_second.fraction = 5;
+
+   cbParam1 = sizeof(intervalparam);
+   rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT,
+                         SQL_C_INTERVAL_SECOND,    /* value type */
+                         SQL_INTERVAL_SECOND,  /* param type */
+                         0,            /* column size (ignored for SQL_INTERVAL_SECOND) */
+                         0,            /* dec digits */
+                         &intervalparam, /* param value ptr */
+                         sizeof(intervalparam), /* buffer len (ignored for SQL_C_INTERVAL_SECOND) */
+                         &cbParam1 /* StrLen_or_IndPtr (ignored for SQL_C_INTERVAL_SECOND) */);
+   CHECK_STMT_RESULT(rc, "SQLBindParameter failed", hstmt);
+
+   /* Execute */
+   rc = SQLExecute(hstmt);
+   CHECK_STMT_RESULT(rc, "SQLExecute failed", hstmt);
+
+   /* Fetch result */
+   print_result(hstmt);
+
+   rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+   CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
+
+
+   /****
+    * With BoolsAsChar=1, a varchar param with column_size=5 forces a
+    * server-side Prepare. So test that.
+    */
+
+   /* Prepare a statement */
+   rc = SQLPrepare(hstmt, (SQLCHAR *) "SELECT id, t FROM testtab1 WHERE id = ?", SQL_NTS);
+   CHECK_STMT_RESULT(rc, "SQLPrepare failed", hstmt);
+
+   /* bind param  */
+   param1 = "2";
+   cbParam1 = SQL_NTS;
+   rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT,
+                         SQL_C_CHAR,   /* value type */
+                         SQL_VARCHAR,  /* param type */
+                         5,            /* column size. 5 Triggers special
+                                        * behavior with BoolsAsChar=1 */
+                         0,            /* dec digits */
+                         param1,       /* param value ptr */
+                         0,            /* buffer len */
+                         &cbParam1     /* StrLen_or_IndPtr */);
+   CHECK_STMT_RESULT(rc, "SQLBindParameter failed", hstmt);
+
+   /* Test SQLNumResultCols, called before SQLExecute() */
+   rc = SQLNumResultCols(hstmt, &colcount);
+   CHECK_STMT_RESULT(rc, "SQLNumResultCols failed", hstmt);
+   printf("# of result cols: %d\n", colcount);
+
+   /* Execute */
+   rc = SQLExecute(hstmt);
+   CHECK_STMT_RESULT(rc, "SQLExecute failed", hstmt);
+
+   /* Fetch result */
+   print_result(hstmt);
+
+   rc = SQLFreeStmt(hstmt, SQL_DROP);
+   if (!SQL_SUCCEEDED(rc))
+   {
+       print_diag("SQLFreeStmt failed", SQL_HANDLE_STMT, hstmt);
+       exit(1);
+   }
+
+   /* Clean up */
+   test_disconnect();
+
+   return 0;
+}
diff --git a/test/src/select-test.c b/test/src/select-test.c
new file mode 100644 (file)
index 0000000..e4f0912
--- /dev/null
@@ -0,0 +1,47 @@
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "common.h"
+
+int main(int argc, char **argv)
+{
+   int rc;
+   HSTMT hstmt = SQL_NULL_HSTMT;
+   char sql[100000];
+   char *sqlend;
+   int i;
+
+   test_connect();
+
+   rc = SQLAllocStmt(conn, &hstmt);
+   if (!SQL_SUCCEEDED(rc))
+   {
+       print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn);
+       exit(1);
+   }
+
+   rc = SQLExecDirect(hstmt, (SQLCHAR *) "SELECT 1 UNION ALL SELECT 2", SQL_NTS);
+   CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
+   print_result(hstmt);
+
+   rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+   CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
+
+   /* Result set with 1600 cols */
+   strcpy(sql, "SELECT 1");
+   sqlend = &sql[strlen(sql)];
+   for (i = 2; i <= 1600; i++)
+   {
+       sprintf(sqlend, ",%d", i);
+       sqlend += strlen(sqlend);
+   }
+   *sqlend = '\0';
+
+   rc = SQLExecDirect(hstmt, (SQLCHAR *) sql, SQL_NTS);
+   CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
+   print_result(hstmt);
+
+   /* Clean up */
+   test_disconnect();
+}
diff --git a/test/src/stmthandles-test.c b/test/src/stmthandles-test.c
new file mode 100644 (file)
index 0000000..3f93b1c
--- /dev/null
@@ -0,0 +1,69 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "common.h"
+
+/*
+ * This test case tests that things still work when you have a lot of
+ * statements open simultaneously.
+ *
+ * As of writing this, there's an internal limit of just below 2^15 stmt
+ * handles. Also, unixodbc's method of checking if a handle is valid, by
+ * scanning a linked list of statements, grinds to a halt as you have
+ * a lot of statements. If you want to test those limits, increase
+ * NUM_STMT_HANDLES value.
+ */
+#define NUM_STMT_HANDLES 100
+
+int main(int argc, char **argv)
+{
+   HSTMT hstmt[NUM_STMT_HANDLES];
+   SQLRETURN rc;
+   int i, nhandles;
+
+   test_connect();
+
+   /* Allocate a lot of stmt handles */
+   for (i = 0; i < NUM_STMT_HANDLES; i++)
+   {
+       rc = SQLAllocStmt(conn, &hstmt[i]);
+       if (!SQL_SUCCEEDED(rc))
+       {
+           printf("failed to allocate stmt handle %d\n", i + 1);
+           print_diag(NULL, SQL_HANDLE_DBC, conn);
+           break;
+       }
+       if ((i + 1) % (NUM_STMT_HANDLES / 10) == 0)
+       {
+           printf("%d statements allocated...\n", i + 1);
+           fflush(stdout);
+       }
+   }
+   nhandles = i;
+
+   /* Execute a query using each of them to verify they all work */
+   for (i = 0; i < nhandles; i++)
+   {
+       char sqlbuf[100];
+       snprintf(sqlbuf, sizeof(sqlbuf), "SELECT 'stmt no %d'", i + 1);
+       rc = SQLExecDirect(hstmt[i], (SQLCHAR *) sqlbuf, SQL_NTS);
+       if (!SQL_SUCCEEDED(rc))
+       {
+           print_diag("SQLExecDirect failed", SQL_HANDLE_STMT, hstmt);
+           exit(1);
+       }
+       if ((i + 1) % (NUM_STMT_HANDLES / 10) == 0)
+       {
+           printf("%d statements executed...\n", i + 1);
+           fflush(stdout);
+       }
+   }
+
+   for (i = 0; i < nhandles; i += (NUM_STMT_HANDLES / 10))
+   {
+       print_result(hstmt[i]);
+       fflush(stdout);
+   }
+
+   test_disconnect();
+}