Hash :
c3ed5b52
Author :
Date :
2018-03-11T11:11:05
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
<h2>Full example</h2>
<pre><code>#!/usr/bin/env qore
# database test script
# databases users must be able to create and destroy tables and procedures, etc
# in order to execute all tests
%require-our
%enable-all-warnings
our ($o, $errors, $test_count);
const opts =
( "help" : "h,help",
"host" : "H,host=s",
"pass" : "p,pass=s",
"db" : "d,db=s",
"user" : "u,user=s",
"type" : "t,type=s",
"enc" : "e,encoding=s",
"verbose" : "v,verbose:i+",
"leave" : "l,leave"
);
sub usage()
{
printf("usage: %s [options]
-h,--help this help text
-u,--user=ARG set username
-p,--pass=ARG set password
-d,--db=ARG set database name
-e,--encoding=ARG set database character set encoding (i.e. \"utf8\")
-H,--host=ARG set hostname (for MySQL and PostgreSQL connections)
-t,--type set database driver (default mysql)
-v,--verbose more v's = more information
-l,--leave leave test tables in schema at end\n",
basename($ENV."_"));
exit();
}
const object_map =
( "oracle" :
( "tables" : ora_tables ),
"mysql" :
( "tables" : mysql_tables ),
"pgsql" :
( "tables" : pgsql_tables ),
"sybase" :
( "tables" : syb_tables,
"procs" : sybase_procs ),
"freetds" :
( "tables" : freetds_sybase_tables,
"procs" : sybase_procs ) );
const ora_tables = (
"family" : "create table family (
family_id int not null,
name varchar2(80) not null
)",
"people" : "create table people (
person_id int not null,
family_id int not null,
name varchar2(250) not null,
dob date not null
)",
"attributes" : "create table attributes (
person_id int not null,
attribute varchar2(80) not null,
value varchar2(160) not null
)" );
const mysql_tables = (
"family" : "create table family (
family_id int not null,
name varchar(80) not null
) type = innodb",
"people" : "create table people (
person_id int not null,
family_id int not null,
name varchar(250) not null,
dob date not null
) type = innodb",
"attributes" : "create table attributes (
person_id int not null,
attribute varchar(80) not null,
value varchar(160) not null
) type = innodb" );
const pgsql_tables = (
"family" : "create table family (
family_id int not null,
name varchar(80) not null )",
"people" : "create table people (
person_id int not null,
family_id int not null,
name varchar(250) not null,
dob date not null )",
"attributes" : "create table attributes (
person_id int not null,
attribute varchar(80) not null,
value varchar(160) not null)",
"data_test" : "create table data_test (
int2_f smallint not null,
int4_f integer not null,
int8_f int8 not null,
bool_f boolean not null,
float4_f real not null,
float8_f double precision not null,
number_f numeric(16,3) not null,
money_f money not null,
text_f text not null,
varchar_f varchar(40) not null,
char_f char(40) not null,
name_f name not null,
date_f date not null,
abstime_f abstime not null,
reltime_f reltime not null,
interval_f interval not null,
time_f time not null,
timetz_f time with time zone not null,
timestamp_f timestamp not null,
timestamptz_f timestamp with time zone not null,
tinterval_f tinterval not null,
bytea_f bytea not null
--bit_f bit(11) not null,
--varbit_f bit varying(11) not null
)" );
const syb_tables = (
"family" : "create table family (
family_id int not null,
name varchar(80) not null
)",
"people" : "create table people (
person_id int not null,
family_id int not null,
name varchar(250) not null,
dob date not null
)",
"attributes" : "create table attributes (
person_id int not null,
attribute varchar(80) not null,
value varchar(160) not null
)",
"data_test" : "create table data_test (
null_f char(1) null,
varchar_f varchar(40) not null,
char_f char(40) not null,
unichar_f unichar(40) not null,
univarchar_f univarchar(40) not null,
text_f text not null,
unitext_f unitext not null, -- note that unitext is stored as 'image'
bit_f bit not null,
tinyint_f tinyint not null,
smallint_f smallint not null,
int_f int not null,
int_f2 int not null,
decimal_f decimal(10,4) not null,
float_f float not null, -- 8-bytes
real_f real not null, -- 4-bytes
money_f money not null,
smallmoney_f smallmoney not null,
date_f date not null,
time_f time not null,
datetime_f datetime not null,
smalldatetime_f smalldatetime not null,
binary_f binary(4) not null,
varbinary_f varbinary(4) not null,
image_f image not null
)" );
const sybase_procs = (
"find_family" :
"create procedure find_family @name varchar(80)
as
select * from family where name = @name
commit -- to maintain transaction count
",
"get_values" :
"create procedure get_values @string varchar(80) output, @int int output
as
select @string = 'hello there'
select @int = 150
commit -- to maintain transaction count
",
"get_values_and_select" :
"create procedure get_values_and_select @string varchar(80) output, @int int output
as
select @string = 'hello there'
select @int = 150
select * from family where family_id = 1
commit -- to maintain transaction count
",
"get_values_and_multiple_select" :
"create procedure get_values_and_multiple_select @string varchar(80) output, @int int output
as
select @string = 'hello there'
select @int = 150
select * from family where family_id = 1
select * from people where person_id = 1
commit -- to maintain transaction count
",
"just_select" :
"create procedure just_select
as
select * from family where family_id = 1
commit -- to maintain transaction count
",
"multiple_select" :
"create procedure multiple_select
as
select * from family where family_id = 1
select * from people where person_id = 1
commit -- to maintain transaction count
"
);
const freetds_sybase_tables = (
"family" : "create table family (
family_id int not null,
name varchar(80) not null
)",
"people" : "create table people (
person_id int not null,
family_id int not null,
name varchar(250) not null,
dob date not null
)",
"attributes" : "create table attributes (
person_id int not null,
attribute varchar(80) not null,
value varchar(160) not null
)",
"data_test" : "create table data_test (
null_f char(1) null,
varchar_f varchar(40) not null,
char_f char(40) not null,
text_f text not null,
unitext_f unitext not null, -- note that unitext is stored as 'image'
bit_f bit not null,
tinyint_f tinyint not null,
smallint_f smallint not null,
int_f int not null,
int_f2 int not null,
decimal_f decimal(10,4) not null,
float_f float not null, -- 8-bytes
real_f real not null, -- 4-bytes
money_f money not null,
smallmoney_f smallmoney not null,
date_f date not null,
time_f time not null,
datetime_f datetime not null,
smalldatetime_f smalldatetime not null,
binary_f binary(4) not null,
varbinary_f varbinary(4) not null,
image_f image not null
)" );
const freetds_mssql_tables = (
"family" : "create table family (
family_id int not null,
name varchar(80) not null
)",
"people" : "create table people (
person_id int not null,
family_id int not null,
name varchar(250) not null,
dob datetime not null
)",
"attributes" : "create table attributes (
person_id int not null,
attribute varchar(80) not null,
value varchar(160) not null
)",
"data_test" : "create table data_test (
null_f char(1) null,
varchar_f varchar(40) not null,
char_f char(40) not null,
text_f text not null,
bit_f bit not null,
tinyint_f tinyint not null,
smallint_f smallint not null,
int_f int not null,
int_f2 int not null,
decimal_f decimal(10,4) not null,
float_f float not null, -- 8-bytes
real_f real not null, -- 4-bytes
money_f money not null,
smallmoney_f smallmoney not null,
datetime_f datetime not null,
smalldatetime_f smalldatetime not null,
binary_f binary(4) not null,
varbinary_f varbinary(4) not null,
image_f image not null
)" );
sub parse_command_line()
{
my $g = new GetOpt(opts);
$o = $g.parse(\$ARGV);
if ($o.help)
usage();
if (!strlen($o.db))
{
stderr.printf("set the login parameters with -u,-p,-d, etc (-h for help)\n");
exit(1);
}
if (elements $ARGV)
{
stderr.printf("excess arguments on command-line (%n): -h for help\n", $ARGV);
exit(1);
}
if (!strlen($o.type))
$o.type = "mysql";
}
sub create_datamodel($db)
{
drop_test_datamodel($db);
my $driver = $db.getDriverName();
# create tables
my $tables = object_map.$driver.tables;
if ($driver == "freetds")
if ($db.is_sybase)
$tables = freetds_sybase_tables;
else
$tables = freetds_mssql_tables;
foreach my $table in (keys $tables)
{
tprintf(2, "creating table %n\n", $table);
$db.exec($tables.$table);
}
# create procedures if any
foreach my $proc in (keys object_map.$driver.procs)
{
tprintf(2, "creating procedure %n\n", $proc);
$db.exec(object_map.$driver.procs.$proc);
}
# create functions if any
foreach my $func in (keys object_map.$driver.funcs)
{
tprintf(2, "creating function %n\n", $func);
$db.exec(object_map.$driver.funcs.$func);
}
$db.exec("insert into family values ( 1, 'Smith' )");
$db.exec("insert into family values ( 2, 'Jones' )");
# we insert the dates here using binding by value so we don't have
# to worry about each database's specific date format
$db.exec("insert into people values ( 1, 1, 'Arnie', %v)", 1983-05-13);
$db.exec("insert into people values ( 2, 1, 'Sylvia', %v)", 1994-11-10);
$db.exec("insert into people values ( 3, 1, 'Carol', %v)", 2003-07-23);
$db.exec("insert into people values ( 4, 1, 'Bernard', %v)", 1979-02-27);
$db.exec("insert into people values ( 5, 1, 'Isaac', %v)", 2000-04-04);
$db.exec("insert into people values ( 6, 2, 'Alan', %v)", 1992-06-04);
$db.exec("insert into people values ( 7, 2, 'John', %v)", 1995-03-23);
$db.exec("insert into attributes values ( 1, 'hair', 'blond' )");
$db.exec("insert into attributes values ( 1, 'eyes', 'hazel' )");
$db.exec("insert into attributes values ( 2, 'hair', 'blond' )");
$db.exec("insert into attributes values ( 2, 'eyes', 'blue' )");
$db.exec("insert into attributes values ( 3, 'hair', 'brown' )");
$db.exec("insert into attributes values ( 3, 'eyes', 'grey')");
$db.exec("insert into attributes values ( 4, 'hair', 'brown' )");
$db.exec("insert into attributes values ( 4, 'eyes', 'brown' )");
$db.exec("insert into attributes values ( 5, 'hair', 'red' )");
$db.exec("insert into attributes values ( 5, 'eyes', 'green' )");
$db.exec("insert into attributes values ( 6, 'hair', 'black' )");
$db.exec("insert into attributes values ( 6, 'eyes', 'blue' )");
$db.exec("insert into attributes values ( 7, 'hair', 'brown' )");
$db.exec("insert into attributes values ( 7, 'eyes', 'brown' )");
$db.commit();
}
sub drop_test_datamodel($db)
{
my $driver = $db.getDriverName();
# drop the tables and ignore exceptions
# the commits are needed for databases like postgresql, where errors will prohibit and further
# actions from being taken on the Datasource
foreach my $table in (keys object_map.$driver.tables)
try {
$db.exec("drop table " + $table);
$db.commit();
tprintf(2, "dropped table %n\n", $table);
}
catch ()
{
$db.commit();
}
# drop procedures and ignore exceptions
foreach my $proc in (keys object_map.$driver.procs)
{
my $cmd = object_map.$driver.drop_proc_cmd;
if (!exists $cmd)
$cmd = "drop procedure";
try {
$db.exec($cmd + " " + $proc);
$db.commit();
tprintf(2, "dropped procedure %n\n", $proc);
}
catch ()
{
$db.commit();
}
}
# drop functions and ignore exceptions
foreach my $func in (keys object_map.$driver.funcs)
{
my $cmd = object_map.$driver.drop_func_cmd;
if (!exists $cmd)
$cmd = "drop function";
try {
$db.exec($cmd + " " + $func);
$db.commit();
tprintf(2, "dropped function %n\n", $func);
}
catch ()
{
$db.commit();
}
}
}
sub getDS()
{
my $ds = new Datasource($o.type, $o.user, $o.pass, $o.db, $o.enc);
if (strlen($o.host))
$ds.setHostName($o.host);
return $ds;
}
sub tprintf($v, $msg)
{
if ($v <= $o.verbose)
vprintf($msg, $argv);
}
sub test_value($v1, $v2, $msg)
{
++$test_count;
if ($v1 == $v2)
tprintf(1, "OK: %s test\n", $msg);
else
{
tprintf(0, "ERROR: %s test failed! (%n != %n)\n", $msg, $v1, $v2);
$errors++;
}
}
const family_hash = (
"Jones" : (
"people" : (
"John" : (
"dob" : 1995-03-23,
"eyes" : "brown",
"hair" : "brown" ),
"Alan" : (
"dob" : 1992-06-04,
"eyes" : "blue",
"hair" : "black" ) ) ),
"Smith" : (
"people" : (
"Arnie" : (
"dob" : 1983-05-13,
"eyes" : "hazel",
"hair" : "blond" ),
"Carol" : (
"dob" : 2003-07-23,
"eyes" : "grey",
"hair" : "brown" ),
"Isaac" : (
"dob" : 2000-04-04,
"eyes" : "green",
"hair" : "red" ),
"Bernard" : (
"dob" : 1979-02-27,
"eyes" : "brown",
"hair" : "brown" ),
"Sylvia" : (
"dob" : 1994-11-10,
"eyes" : "blue",
"hair" : "blond" ) ) ) );
sub context_test($db)
{
# first we select all the data from the tables and then use
# context statements to order the output hierarchically
# context statements are most useful when a set of queries can be executed once
# and the results processed many times by creating "views" with context statements
my $people = $db.select("select * from people");
my $attributes = $db.select("select * from attributes");
my $today = format_date("YYYYMMDD", now());
# in this test, we create a big hash structure out of the queries executed above
# and compare it at the end to the expected result
# display each family sorted by family name
my $fl;
context family ($db.select("select * from family")) sortBy (%name)
{
my $pl;
tprintf(2, "Family %d: %s\n", %family_id, %name);
# display people, sorted by eye color, descending
context people ($people)
sortDescendingBy (find %value in $attributes
where (%attribute == "eyes"
&& %person_id == %people:person_id))
where (%family_id == %family:family_id)
{
my $al;
tprintf(2, " %s, born %s\n", %name, format_date("Month DD, YYYY", %dob));
context ($attributes) sortBy (%attribute) where (%person_id == %people:person_id)
{
$al.%attribute = %value;
tprintf(2, " has %s %s\n", %value, %attribute);
}
# leave out the ID fields and name from hash under name; subtracting a
# string from a hash removes that key from the result
# this is "doing it the hard way", there is only one key left,
# "dob", then attributes are added directly into the person hash
$pl.%name = %% - "family_id" - "person_id" - "name" + $al;
}
# leave out family_id and name fields (leaving an empty hash)
$fl.%name = %% - "family_id" - "name" + ( "people" : $pl );
}
# test context ordering
test_value(keys $fl, ("Jones", "Smith"), "first context");
test_value(keys $fl.Smith.people, ("Arnie", "Carol", "Isaac", "Bernard", "Sylvia"), "second context");
# test entire context value
test_value($fl, family_hash, "third context");
}
sub test_timeout($db, $c)
{
$db.setTransactionLockTimeout(1ms);
try {
# this should cause a TRANSACTION-LOCK-TIMEOUT exception to be thrown
$db.exec("insert into family values (3, 'Test')\n");
test_value(True, False, "transaction timeout");
$db.exec("delete from family where name = 'Test'");
}
catch ($ex)
{
test_value(True, True, "transaction timeout");
}
# signal parent thread to continue
$c.dec();
}
sub transaction_test($db)
{
my $ndb = getDS();
my $r;
tprintf(2, "db.autocommit=%N, ndb.autocommit=%N\n", $db.getAutoCommit(), $ndb.getAutoCommit());
# first, we insert a new row into "family" but do not commit it
my $rows = $db.exec("insert into family values (3, 'Test')\n");
if ($rows !== 1)
printf("FAILED INSERT, rows=%N\n", $rows);
# now we verify that the new row is not visible to the other datasource
# unless it's a sybase/ms sql server datasource, in which case this would deadlock :-(
if ($o.type != "sybase" && $o.type != "freetds")
{
$r = $ndb.selectRow("select name from family where family_id = 3").name;
test_value($r, NOTHING, "first transaction");
}
# now we verify that the new row is visible to the inserting datasource
$r = $db.selectRow("select name from family where family_id = 3").name;
test_value($r, "Test", "second transaction");
# test datasource timeout
# this Counter variable will allow the parent thread to sleep
# until the child thread times out
my $c = new Counter(1);
background test_timeout($db, $c);
# wait for child thread to time out
$c.waitForZero();
# now, we commit the transaction
$db.commit();
# now we verify that the new row is visible in the other datasource
$r = $ndb.selectRow("select name from family where family_id = 3").name;
test_value($r, "Test", "third transaction");
# now we delete the row we inserted (so we can repeat the test)
$r = $ndb.exec("delete from family where family_id = 3");
test_value($r, 1, "delete row count");
$ndb.commit();
}
sub oracle_test()
{
}
# here we use a little workaround for modules that provide functions,
# namespace additions (constants, classes, etc) needed by test functions
# at parse time. To avoid parse errors (as database modules are loaded
# in this script at run-time when the Datasource class is instantiated)
# we use a Program object that we parse and run on demand to return the
# value required
sub get_val($code)
{
my $p = new Program();
my $str = sprintf("return %s;", $code);
$p.parse($str, "code");
return $p.run();
}
sub pgsql_test($db)
{
my $args = ( "int2_f" : 258,
"int4_f" : 233932,
"int8_f" : 239392939458,
"bool_f" : True,
"float4_f" : 21.3444,
"float8_f" : 49394.23423491,
"number_f" : get_val("pgsql_bind(PG_TYPE_NUMERIC, '7235634215.3250')"),
"money_f" : get_val("pgsql_bind(PG_TYPE_CASH, \"400.56\")"),
"text_f" : 'some text ',
"varchar_f" : 'varchar ',
"char_f" : 'char text',
"name_f" : 'name',
"date_f" : 2004-01-05,
"abstime_f" : 2005-12-03T10:00:01,
"reltime_f" : 5M + 71D + 19h + 245m + 51s,
"interval_f" : 6M + 3D + 2h + 45m + 15s,
"time_f" : 11:35:00,
"timetz_f" : get_val("pgsql_bind(PG_TYPE_TIMETZ, \"11:38:21 CST\")"),
"timestamp_f" : 2005-04-01T11:35:26,
"timestamptz_f" : 2005-04-01T11:35:26.259,
"tinterval_f" : get_val("pgsql_bind(PG_TYPE_TINTERVAL, '[\"May 10, 1947 23:59:12\" \"Jan 14, 1973 03:14:21\"]')"),
"bytea_f" : <bead>
#bit_f :
#varbit_f :
);
$db.vexec("insert into data_test values (%v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v)", hash_values($args));
my $q = $db.selectRow("select * from data_test");
if ($o.verbose > 1)
foreach my $k in (keys $q)
tprintf(2, " %-16s= %-10s %N\n", $k, type($q.$k), $q.$k);
# fix values where we know the return type is different
$args.money_f = 400.56;
$args.timetz_f = 11:38:21;
$args.tinterval_f = '["1947-05-10 21:59:12" "1973-01-14 02:14:21"]';
$args.number_f = "7235634215.3250";
$args.reltime_f = 19177551s;
$args.interval_f = 6M + 3D + 9915s;
# rounding errors can happen in float4
$q.float4_f = round($q.float4_f);
$args.float4_f = round($args.float4_f);
# remove values where we know they won't match
# abstime and timestamptz are converted to GMT by the server
delete $q.abstime_f;
delete $q.timestamptz_f;
# compare each value
foreach my $k in (keys $q)
test_value($q.$k, $args.$k, sprintf("%s bind and retrieve", $k));
$db.commit();
}
sub mysql_test()
{
}
const family_q = ( "family_id" : 1,
"name" : "Smith" );
const person_q = ( "person_id" : 1,
"family_id" : 1,
"name" : "Arnie",
"dob" : 1983-05-13 );
const params = ( "string" : "hello there",
"int" : 150 );
sub sybase_test($db)
{
# simple stored proc test, bind by name
my $x = $db.exec("exec find_family %v", "Smith");
test_value($x, ("name": list("Smith"), "family_id" : list(1)), "simple stored proc");
# stored proc execute with output params
$x = $db.exec("declare @string varchar(40), @int int
exec get_values :string output, :int output");
test_value($x, params + ("rowcount":1), "get_values");
# we use Datasource::selectRows() in the following queries because we
# get hash results instead of a hash of lists as with exec in the queries
# normally we should not use selectRows to execute a stored procedure,
# as the Datasource::selectRows() method will not grab the transaction lock,
# but we already called Datasource::exec() above, so we have it already.
# the other alternative would be to call Datasource::beginTransaction() before
# Datasource::selectRows()
# simple stored proc test, bind by name, returns hash
$x = $db.selectRows("exec find_family %v", "Smith");
test_value($x, family_q, "simple stored proc");
# stored proc execute with output params and select results
$x = $db.selectRows("declare @string varchar(40), @int int
exec get_values_and_select :string output, :int output");
test_value($x, ("query":family_q,"params":params), "get_values_and_select");
# stored proc execute with output params and multiple select results
$x = $db.selectRows("declare @string varchar(40), @int int
exec get_values_and_multiple_select :string output, :int output");
test_value($x, ("query":("query0":family_q,"query1":person_q),"params":params), "get_values_and_multiple_select");
# stored proc execute with just select results
$x = $db.selectRows("exec just_select");
test_value($x, family_q, "just_select");
# stored proc execute with multiple select results
$x = $db.selectRows("exec multiple_select");
test_value($x, ("query0":family_q,"query1":person_q), "multiple_select");
my $args = ( "null_f" : NULL,
"varchar_f" : "varchar",
"char_f" : "char",
"unichar_f" : "unichar",
"univarchar_f" : "univarchar",
"text_f" : "test",
"unitext_f" : "test",
"bit_f" : True,
"tinyint_f" : 55,
"smallint_f" : 4285,
"int_f" : 405402,
"int_f2" : 214123498,
"decimal_f" : 500.1231,
"float_f" : 23443.234324234,
"real_f" : 213.123,
"money_f" : 3434234250.2034,
"smallmoney_f" : 211100.1012,
"date_f" : 2007-05-01,
"time_f" : 10:30:01,
"datetime_f" : 3459-01-01T11:15:02.250,
"smalldatetime_f" : 2007-12-01T12:01:00,
"binary_f" : <0badbeef>,
"varbinary_f" : <feedface>,
"image_f" : <cafebead> );
# insert data
my $rows = $db.vexec("insert into data_test values (%v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %d, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v)", hash_values($args));
my $q = $db.selectRow("select * from data_test");
if ($o.verbose > 1)
foreach my $k in (keys $q)
tprintf(2, " %-16s= %-10s %N\n", $k, type($q.$k), $q.$k);
# remove values where we know they won't match
# unitext_f is returned as IMAGE by the server
delete $q.unitext_f;
delete $args.unitext_f;
# rounding errors can happen in real
$q.real_f = round($q.real_f);
$args.real_f = round($args.real_f);
# compare each value
foreach my $k in (keys $q)
test_value($q.$k, $args.$k, sprintf("%s bind and retrieve", $k));
$db.commit();
}
sub freetds_test($db)
{
# simple stored proc test, bind by name
my $x = $db.exec("exec find_family %v", "Smith");
test_value($x, ("name": list("Smith"), "family_id" : list(1)), "simple stored proc");
# we cannot retrieve parameters from newer SQL Servers with the approach we use;
# Microsoft changed the handling of the protocol and require us to use RPC calls,
# this will be implemented in the next version of qore where the "freetds" driver will
# be able to add custom methods to the Datasource class. For now, we skip these tests
if ($db.is_sybase)
{
$x = $db.exec("declare @string varchar(40), @int int
exec get_values :string output, :int output");
test_value($x, params, "get_values");
}
# we use Datasource::selectRows() in the following queries because we
# get hash results instead of a hash of lists as with exec in the queries
# normally we should not use selectRows to execute a stored procedure,
# as the Datasource::selectRows() method will not grab the transaction lock,
# but we already called Datasource::exec() above, so we have it already.
# the other alternative would be to call Datasource::beginTransaction() before
# Datasource::selectRows()
# simple stored proc test, bind by name, returns hash
$x = $db.selectRows("exec find_family %v", "Smith");
test_value($x, family_q, "simple stored proc");
# stored proc execute with output params and select results
if ($db.is_sybase)
{
$x = $db.selectRows("declare @string varchar(40), @int int
exec get_values_and_select :string output, :int output");
test_value($x, ("query":family_q,"params":params), "get_values_and_select");
# stored proc execute with output params and multiple select results
$x = $db.selectRows("declare @string varchar(40), @int int
exec get_values_and_multiple_select :string output, :int output");
test_value($x, ("query":("query0":family_q,"query1":person_q),"params":params), "get_values_and_multiple_select");
}
# stored proc execute with just select results
$x = $db.selectRows("exec just_select");
test_value($x, family_q, "just_select");
# stored proc execute with multiple select results
$x = $db.selectRows("exec multiple_select");
test_value($x, ("query0":family_q,"query1":person_q), "multiple_select");
# the freetds driver does not work with the following sybase column types:
# unichar, univarchar
my $args = ( "null_f" : NULL,
"varchar_f" : "test",
"char_f" : "test",
"text_f" : "test",
"unitext_f" : "test",
"bit_f" : True,
"tinyint_f" : 55,
"smallint_f" : 4285,
"int_f" : 405402,
"int_f2" : 214123498,
"decimal_f" : 500.1231,
"float_f" : 23443.234324234,
"real_f" : 213.123,
"money_f" : 3434234250.2034,
"smallmoney_f" : 211100.1012,
"date_f" : 2007-05-01,
"time_f" : 10:30:01,
"datetime_f" : 3459-01-01T11:15:02.250,
"smalldatetime_f" : 2007-12-01T12:01:00,
"binary_f" : <0badbeef>,
"varbinary_f" : <feedface>,
"image_f" : <cafebead> );
# remove fields not supported by sql server
if (!$db.is_sybase)
{
delete $args.unitext_f;
delete $args.date_f;
delete $args.time_f;
}
my $sql = "insert into data_test values (";
for (my $i; $i < elements $args; ++$i)
$sql += "%v, ";
$sql = substr($sql, 0, -2) + ")";
# insert data, using the values from the hash above
my $rows = $db.vexec($sql, hash_values($args));
my $q = $db.selectRow("select * from data_test");
if ($o.verbose > 1)
foreach my $k in (keys $q)
tprintf(2, " %-16s= %-10s %N\n", $k, type($q.$k), $q.$k);
# remove values where we know they won't match
# unitext_f is returned as IMAGE by the server
delete $q.unitext_f;
delete $args.unitext_f;
# rounding errors can happen in real
$q.real_f = round($q.real_f);
$args.real_f = round($args.real_f);
# compare each value
foreach my $k in (keys $q)
test_value($q.$k, $args.$k, sprintf("%s bind and retrieve", $k));
$db.commit();
}
sub main()
{
my $test_map =
( "sybase" : \sybase_test(),
"freetds" : \freetds_test(),
"mysql" : \mysql_test(),
"pgsql" : \pgsql_test(),
"oracle" : \oracle_test());
parse_command_line();
my $db = getDS();
my $driver = $db.getDriverName();
printf("testing %s driver\n", $driver);
my $sv = $db.getServerVersion();
if ($o.verbose > 1)
tprintf(2, "client version=%n\nserver version=%n\n", $db.getClientVersion(), $sv);
# determine if the server is a sybase or sql server dataserver
if ($driver == "freetds")
if ($sv !~ /microsoft/i)
$db.is_sybase = True;
create_datamodel($db);
context_test($db);
transaction_test($db);
my $test = $test_map.($db.getDriverName());
if (exists $test)
$test($db);
if (!$o.leave)
drop_test_datamodel($db);
printf("%d/%d tests OK\n", $test_count - $errors, $test_count);
}
main();</code></pre>