| 1 | <h2>Full example</h2> |
| 2 | <pre><code>#!/usr/bin/env qore |
| 3 | |
| 4 | # database test script |
| 5 | # databases users must be able to create and destroy tables and procedures, etc |
| 6 | # in order to execute all tests |
| 7 | |
| 8 | %require-our |
| 9 | %enable-all-warnings |
| 10 | |
| 11 | our ($o, $errors, $test_count); |
| 12 | |
| 13 | const opts = |
| 14 | ( "help" : "h,help", |
| 15 | "host" : "H,host=s", |
| 16 | "pass" : "p,pass=s", |
| 17 | "db" : "d,db=s", |
| 18 | "user" : "u,user=s", |
| 19 | "type" : "t,type=s", |
| 20 | "enc" : "e,encoding=s", |
| 21 | "verbose" : "v,verbose:i+", |
| 22 | "leave" : "l,leave" |
| 23 | ); |
| 24 | |
| 25 | sub usage() |
| 26 | { |
| 27 | printf("usage: %s [options] |
| 28 | -h,--help this help text |
| 29 | -u,--user=ARG set username |
| 30 | -p,--pass=ARG set password |
| 31 | -d,--db=ARG set database name |
| 32 | -e,--encoding=ARG set database character set encoding (i.e. \"utf8\") |
| 33 | -H,--host=ARG set hostname (for MySQL and PostgreSQL connections) |
| 34 | -t,--type set database driver (default mysql) |
| 35 | -v,--verbose more v's = more information |
| 36 | -l,--leave leave test tables in schema at end\n", |
| 37 | basename($ENV."_")); |
| 38 | exit(); |
| 39 | } |
| 40 | |
| 41 | const object_map = |
| 42 | ( "oracle" : |
| 43 | ( "tables" : ora_tables ), |
| 44 | "mysql" : |
| 45 | ( "tables" : mysql_tables ), |
| 46 | "pgsql" : |
| 47 | ( "tables" : pgsql_tables ), |
| 48 | "sybase" : |
| 49 | ( "tables" : syb_tables, |
| 50 | "procs" : sybase_procs ), |
| 51 | "freetds" : |
| 52 | ( "tables" : freetds_sybase_tables, |
| 53 | "procs" : sybase_procs ) ); |
| 54 | |
| 55 | const ora_tables = ( |
| 56 | "family" : "create table family ( |
| 57 | family_id int not null, |
| 58 | name varchar2(80) not null |
| 59 | )", |
| 60 | "people" : "create table people ( |
| 61 | person_id int not null, |
| 62 | family_id int not null, |
| 63 | name varchar2(250) not null, |
| 64 | dob date not null |
| 65 | )", |
| 66 | "attributes" : "create table attributes ( |
| 67 | person_id int not null, |
| 68 | attribute varchar2(80) not null, |
| 69 | value varchar2(160) not null |
| 70 | )" ); |
| 71 | |
| 72 | const mysql_tables = ( |
| 73 | "family" : "create table family ( |
| 74 | family_id int not null, |
| 75 | name varchar(80) not null |
| 76 | ) type = innodb", |
| 77 | "people" : "create table people ( |
| 78 | person_id int not null, |
| 79 | family_id int not null, |
| 80 | name varchar(250) not null, |
| 81 | dob date not null |
| 82 | ) type = innodb", |
| 83 | "attributes" : "create table attributes ( |
| 84 | person_id int not null, |
| 85 | attribute varchar(80) not null, |
| 86 | value varchar(160) not null |
| 87 | ) type = innodb" ); |
| 88 | |
| 89 | const pgsql_tables = ( |
| 90 | "family" : "create table family ( |
| 91 | family_id int not null, |
| 92 | name varchar(80) not null )", |
| 93 | "people" : "create table people ( |
| 94 | person_id int not null, |
| 95 | family_id int not null, |
| 96 | name varchar(250) not null, |
| 97 | dob date not null )", |
| 98 | "attributes" : "create table attributes ( |
| 99 | person_id int not null, |
| 100 | attribute varchar(80) not null, |
| 101 | value varchar(160) not null)", |
| 102 | "data_test" : "create table data_test ( |
| 103 | int2_f smallint not null, |
| 104 | int4_f integer not null, |
| 105 | int8_f int8 not null, |
| 106 | bool_f boolean not null, |
| 107 | |
| 108 | float4_f real not null, |
| 109 | float8_f double precision not null, |
| 110 | |
| 111 | number_f numeric(16,3) not null, |
| 112 | money_f money not null, |
| 113 | |
| 114 | text_f text not null, |
| 115 | varchar_f varchar(40) not null, |
| 116 | char_f char(40) not null, |
| 117 | name_f name not null, |
| 118 | |
| 119 | date_f date not null, |
| 120 | abstime_f abstime not null, |
| 121 | reltime_f reltime not null, |
| 122 | interval_f interval not null, |
| 123 | time_f time not null, |
| 124 | timetz_f time with time zone not null, |
| 125 | timestamp_f timestamp not null, |
| 126 | timestamptz_f timestamp with time zone not null, |
| 127 | tinterval_f tinterval not null, |
| 128 | |
| 129 | bytea_f bytea not null |
| 130 | --bit_f bit(11) not null, |
| 131 | --varbit_f bit varying(11) not null |
| 132 | )" ); |
| 133 | |
| 134 | const syb_tables = ( |
| 135 | "family" : "create table family ( |
| 136 | family_id int not null, |
| 137 | name varchar(80) not null |
| 138 | )", |
| 139 | "people" : "create table people ( |
| 140 | person_id int not null, |
| 141 | family_id int not null, |
| 142 | name varchar(250) not null, |
| 143 | dob date not null |
| 144 | )", |
| 145 | "attributes" : "create table attributes ( |
| 146 | person_id int not null, |
| 147 | attribute varchar(80) not null, |
| 148 | value varchar(160) not null |
| 149 | )", |
| 150 | "data_test" : "create table data_test ( |
| 151 | null_f char(1) null, |
| 152 | |
| 153 | varchar_f varchar(40) not null, |
| 154 | char_f char(40) not null, |
| 155 | unichar_f unichar(40) not null, |
| 156 | univarchar_f univarchar(40) not null, |
| 157 | text_f text not null, |
| 158 | unitext_f unitext not null, -- note that unitext is stored as 'image' |
| 159 | |
| 160 | bit_f bit not null, |
| 161 | tinyint_f tinyint not null, |
| 162 | smallint_f smallint not null, |
| 163 | int_f int not null, |
| 164 | int_f2 int not null, |
| 165 | |
| 166 | decimal_f decimal(10,4) not null, |
| 167 | |
| 168 | float_f float not null, -- 8-bytes |
| 169 | real_f real not null, -- 4-bytes |
| 170 | money_f money not null, |
| 171 | smallmoney_f smallmoney not null, |
| 172 | |
| 173 | date_f date not null, |
| 174 | time_f time not null, |
| 175 | datetime_f datetime not null, |
| 176 | smalldatetime_f smalldatetime not null, |
| 177 | |
| 178 | binary_f binary(4) not null, |
| 179 | varbinary_f varbinary(4) not null, |
| 180 | image_f image not null |
| 181 | )" ); |
| 182 | |
| 183 | const sybase_procs = ( |
| 184 | "find_family" : |
| 185 | "create procedure find_family @name varchar(80) |
| 186 | as |
| 187 | select * from family where name = @name |
| 188 | commit -- to maintain transaction count |
| 189 | ", |
| 190 | "get_values" : |
| 191 | "create procedure get_values @string varchar(80) output, @int int output |
| 192 | as |
| 193 | select @string = 'hello there' |
| 194 | select @int = 150 |
| 195 | commit -- to maintain transaction count |
| 196 | ", |
| 197 | "get_values_and_select" : |
| 198 | "create procedure get_values_and_select @string varchar(80) output, @int int output |
| 199 | as |
| 200 | select @string = 'hello there' |
| 201 | select @int = 150 |
| 202 | select * from family where family_id = 1 |
| 203 | commit -- to maintain transaction count |
| 204 | ", |
| 205 | "get_values_and_multiple_select" : |
| 206 | "create procedure get_values_and_multiple_select @string varchar(80) output, @int int output |
| 207 | as |
| 208 | select @string = 'hello there' |
| 209 | select @int = 150 |
| 210 | select * from family where family_id = 1 |
| 211 | select * from people where person_id = 1 |
| 212 | commit -- to maintain transaction count |
| 213 | ", |
| 214 | "just_select" : |
| 215 | "create procedure just_select |
| 216 | as |
| 217 | select * from family where family_id = 1 |
| 218 | commit -- to maintain transaction count |
| 219 | ", |
| 220 | "multiple_select" : |
| 221 | "create procedure multiple_select |
| 222 | as |
| 223 | select * from family where family_id = 1 |
| 224 | select * from people where person_id = 1 |
| 225 | commit -- to maintain transaction count |
| 226 | " |
| 227 | ); |
| 228 | |
| 229 | const freetds_sybase_tables = ( |
| 230 | "family" : "create table family ( |
| 231 | family_id int not null, |
| 232 | name varchar(80) not null |
| 233 | )", |
| 234 | "people" : "create table people ( |
| 235 | person_id int not null, |
| 236 | family_id int not null, |
| 237 | name varchar(250) not null, |
| 238 | dob date not null |
| 239 | )", |
| 240 | "attributes" : "create table attributes ( |
| 241 | person_id int not null, |
| 242 | attribute varchar(80) not null, |
| 243 | value varchar(160) not null |
| 244 | )", |
| 245 | "data_test" : "create table data_test ( |
| 246 | null_f char(1) null, |
| 247 | |
| 248 | varchar_f varchar(40) not null, |
| 249 | char_f char(40) not null, |
| 250 | text_f text not null, |
| 251 | unitext_f unitext not null, -- note that unitext is stored as 'image' |
| 252 | |
| 253 | bit_f bit not null, |
| 254 | tinyint_f tinyint not null, |
| 255 | smallint_f smallint not null, |
| 256 | int_f int not null, |
| 257 | int_f2 int not null, |
| 258 | |
| 259 | decimal_f decimal(10,4) not null, |
| 260 | |
| 261 | float_f float not null, -- 8-bytes |
| 262 | real_f real not null, -- 4-bytes |
| 263 | money_f money not null, |
| 264 | smallmoney_f smallmoney not null, |
| 265 | |
| 266 | date_f date not null, |
| 267 | time_f time not null, |
| 268 | datetime_f datetime not null, |
| 269 | smalldatetime_f smalldatetime not null, |
| 270 | |
| 271 | binary_f binary(4) not null, |
| 272 | varbinary_f varbinary(4) not null, |
| 273 | image_f image not null |
| 274 | )" ); |
| 275 | |
| 276 | const freetds_mssql_tables = ( |
| 277 | "family" : "create table family ( |
| 278 | family_id int not null, |
| 279 | name varchar(80) not null |
| 280 | )", |
| 281 | "people" : "create table people ( |
| 282 | person_id int not null, |
| 283 | family_id int not null, |
| 284 | name varchar(250) not null, |
| 285 | dob datetime not null |
| 286 | )", |
| 287 | "attributes" : "create table attributes ( |
| 288 | person_id int not null, |
| 289 | attribute varchar(80) not null, |
| 290 | value varchar(160) not null |
| 291 | )", |
| 292 | "data_test" : "create table data_test ( |
| 293 | null_f char(1) null, |
| 294 | |
| 295 | varchar_f varchar(40) not null, |
| 296 | char_f char(40) not null, |
| 297 | text_f text not null, |
| 298 | |
| 299 | bit_f bit not null, |
| 300 | tinyint_f tinyint not null, |
| 301 | smallint_f smallint not null, |
| 302 | int_f int not null, |
| 303 | int_f2 int not null, |
| 304 | |
| 305 | decimal_f decimal(10,4) not null, |
| 306 | |
| 307 | float_f float not null, -- 8-bytes |
| 308 | real_f real not null, -- 4-bytes |
| 309 | money_f money not null, |
| 310 | smallmoney_f smallmoney not null, |
| 311 | |
| 312 | datetime_f datetime not null, |
| 313 | smalldatetime_f smalldatetime not null, |
| 314 | |
| 315 | binary_f binary(4) not null, |
| 316 | varbinary_f varbinary(4) not null, |
| 317 | image_f image not null |
| 318 | )" ); |
| 319 | |
| 320 | sub parse_command_line() |
| 321 | { |
| 322 | my $g = new GetOpt(opts); |
| 323 | $o = $g.parse(\$ARGV); |
| 324 | if ($o.help) |
| 325 | usage(); |
| 326 | |
| 327 | if (!strlen($o.db)) |
| 328 | { |
| 329 | stderr.printf("set the login parameters with -u,-p,-d, etc (-h for help)\n"); |
| 330 | exit(1); |
| 331 | } |
| 332 | if (elements $ARGV) |
| 333 | { |
| 334 | stderr.printf("excess arguments on command-line (%n): -h for help\n", $ARGV); |
| 335 | exit(1); |
| 336 | } |
| 337 | if (!strlen($o.type)) |
| 338 | $o.type = "mysql"; |
| 339 | } |
| 340 | |
| 341 | sub create_datamodel($db) |
| 342 | { |
| 343 | drop_test_datamodel($db); |
| 344 | |
| 345 | my $driver = $db.getDriverName(); |
| 346 | # create tables |
| 347 | my $tables = object_map.$driver.tables; |
| 348 | if ($driver == "freetds") |
| 349 | if ($db.is_sybase) |
| 350 | $tables = freetds_sybase_tables; |
| 351 | else |
| 352 | $tables = freetds_mssql_tables; |
| 353 | |
| 354 | foreach my $table in (keys $tables) |
| 355 | { |
| 356 | tprintf(2, "creating table %n\n", $table); |
| 357 | $db.exec($tables.$table); |
| 358 | } |
| 359 | |
| 360 | # create procedures if any |
| 361 | foreach my $proc in (keys object_map.$driver.procs) |
| 362 | { |
| 363 | tprintf(2, "creating procedure %n\n", $proc); |
| 364 | $db.exec(object_map.$driver.procs.$proc); |
| 365 | } |
| 366 | |
| 367 | # create functions if any |
| 368 | foreach my $func in (keys object_map.$driver.funcs) |
| 369 | { |
| 370 | tprintf(2, "creating function %n\n", $func); |
| 371 | $db.exec(object_map.$driver.funcs.$func); |
| 372 | } |
| 373 | |
| 374 | $db.exec("insert into family values ( 1, 'Smith' )"); |
| 375 | $db.exec("insert into family values ( 2, 'Jones' )"); |
| 376 | |
| 377 | # we insert the dates here using binding by value so we don't have |
| 378 | # to worry about each database's specific date format |
| 379 | $db.exec("insert into people values ( 1, 1, 'Arnie', %v)", 1983-05-13); |
| 380 | $db.exec("insert into people values ( 2, 1, 'Sylvia', %v)", 1994-11-10); |
| 381 | $db.exec("insert into people values ( 3, 1, 'Carol', %v)", 2003-07-23); |
| 382 | $db.exec("insert into people values ( 4, 1, 'Bernard', %v)", 1979-02-27); |
| 383 | $db.exec("insert into people values ( 5, 1, 'Isaac', %v)", 2000-04-04); |
| 384 | $db.exec("insert into people values ( 6, 2, 'Alan', %v)", 1992-06-04); |
| 385 | $db.exec("insert into people values ( 7, 2, 'John', %v)", 1995-03-23); |
| 386 | |
| 387 | $db.exec("insert into attributes values ( 1, 'hair', 'blond' )"); |
| 388 | $db.exec("insert into attributes values ( 1, 'eyes', 'hazel' )"); |
| 389 | $db.exec("insert into attributes values ( 2, 'hair', 'blond' )"); |
| 390 | $db.exec("insert into attributes values ( 2, 'eyes', 'blue' )"); |
| 391 | $db.exec("insert into attributes values ( 3, 'hair', 'brown' )"); |
| 392 | $db.exec("insert into attributes values ( 3, 'eyes', 'grey')"); |
| 393 | $db.exec("insert into attributes values ( 4, 'hair', 'brown' )"); |
| 394 | $db.exec("insert into attributes values ( 4, 'eyes', 'brown' )"); |
| 395 | $db.exec("insert into attributes values ( 5, 'hair', 'red' )"); |
| 396 | $db.exec("insert into attributes values ( 5, 'eyes', 'green' )"); |
| 397 | $db.exec("insert into attributes values ( 6, 'hair', 'black' )"); |
| 398 | $db.exec("insert into attributes values ( 6, 'eyes', 'blue' )"); |
| 399 | $db.exec("insert into attributes values ( 7, 'hair', 'brown' )"); |
| 400 | $db.exec("insert into attributes values ( 7, 'eyes', 'brown' )"); |
| 401 | $db.commit(); |
| 402 | } |
| 403 | |
| 404 | sub drop_test_datamodel($db) |
| 405 | { |
| 406 | my $driver = $db.getDriverName(); |
| 407 | # drop the tables and ignore exceptions |
| 408 | # the commits are needed for databases like postgresql, where errors will prohibit and further |
| 409 | # actions from being taken on the Datasource |
| 410 | foreach my $table in (keys object_map.$driver.tables) |
| 411 | try { |
| 412 | $db.exec("drop table " + $table); |
| 413 | $db.commit(); |
| 414 | tprintf(2, "dropped table %n\n", $table); |
| 415 | } |
| 416 | catch () |
| 417 | { |
| 418 | $db.commit(); |
| 419 | } |
| 420 | |
| 421 | # drop procedures and ignore exceptions |
| 422 | foreach my $proc in (keys object_map.$driver.procs) |
| 423 | { |
| 424 | my $cmd = object_map.$driver.drop_proc_cmd; |
| 425 | if (!exists $cmd) |
| 426 | $cmd = "drop procedure"; |
| 427 | try { |
| 428 | $db.exec($cmd + " " + $proc); |
| 429 | $db.commit(); |
| 430 | tprintf(2, "dropped procedure %n\n", $proc); |
| 431 | } |
| 432 | catch () |
| 433 | { |
| 434 | $db.commit(); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | # drop functions and ignore exceptions |
| 439 | foreach my $func in (keys object_map.$driver.funcs) |
| 440 | { |
| 441 | my $cmd = object_map.$driver.drop_func_cmd; |
| 442 | if (!exists $cmd) |
| 443 | $cmd = "drop function"; |
| 444 | try { |
| 445 | $db.exec($cmd + " " + $func); |
| 446 | $db.commit(); |
| 447 | tprintf(2, "dropped function %n\n", $func); |
| 448 | } |
| 449 | catch () |
| 450 | { |
| 451 | $db.commit(); |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | sub getDS() |
| 457 | { |
| 458 | my $ds = new Datasource($o.type, $o.user, $o.pass, $o.db, $o.enc); |
| 459 | if (strlen($o.host)) |
| 460 | $ds.setHostName($o.host); |
| 461 | return $ds; |
| 462 | } |
| 463 | |
| 464 | sub tprintf($v, $msg) |
| 465 | { |
| 466 | if ($v <= $o.verbose) |
| 467 | vprintf($msg, $argv); |
| 468 | } |
| 469 | |
| 470 | sub test_value($v1, $v2, $msg) |
| 471 | { |
| 472 | ++$test_count; |
| 473 | if ($v1 == $v2) |
| 474 | tprintf(1, "OK: %s test\n", $msg); |
| 475 | else |
| 476 | { |
| 477 | tprintf(0, "ERROR: %s test failed! (%n != %n)\n", $msg, $v1, $v2); |
| 478 | $errors++; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | const family_hash = ( |
| 483 | "Jones" : ( |
| 484 | "people" : ( |
| 485 | "John" : ( |
| 486 | "dob" : 1995-03-23, |
| 487 | "eyes" : "brown", |
| 488 | "hair" : "brown" ), |
| 489 | "Alan" : ( |
| 490 | "dob" : 1992-06-04, |
| 491 | "eyes" : "blue", |
| 492 | "hair" : "black" ) ) ), |
| 493 | "Smith" : ( |
| 494 | "people" : ( |
| 495 | "Arnie" : ( |
| 496 | "dob" : 1983-05-13, |
| 497 | "eyes" : "hazel", |
| 498 | "hair" : "blond" ), |
| 499 | "Carol" : ( |
| 500 | "dob" : 2003-07-23, |
| 501 | "eyes" : "grey", |
| 502 | "hair" : "brown" ), |
| 503 | "Isaac" : ( |
| 504 | "dob" : 2000-04-04, |
| 505 | "eyes" : "green", |
| 506 | "hair" : "red" ), |
| 507 | "Bernard" : ( |
| 508 | "dob" : 1979-02-27, |
| 509 | "eyes" : "brown", |
| 510 | "hair" : "brown" ), |
| 511 | "Sylvia" : ( |
| 512 | "dob" : 1994-11-10, |
| 513 | "eyes" : "blue", |
| 514 | "hair" : "blond" ) ) ) ); |
| 515 | |
| 516 | sub context_test($db) |
| 517 | { |
| 518 | # first we select all the data from the tables and then use |
| 519 | # context statements to order the output hierarchically |
| 520 | |
| 521 | # context statements are most useful when a set of queries can be executed once |
| 522 | # and the results processed many times by creating "views" with context statements |
| 523 | |
| 524 | my $people = $db.select("select * from people"); |
| 525 | my $attributes = $db.select("select * from attributes"); |
| 526 | |
| 527 | my $today = format_date("YYYYMMDD", now()); |
| 528 | |
| 529 | # in this test, we create a big hash structure out of the queries executed above |
| 530 | # and compare it at the end to the expected result |
| 531 | |
| 532 | # display each family sorted by family name |
| 533 | my $fl; |
| 534 | context family ($db.select("select * from family")) sortBy (%name) |
| 535 | { |
| 536 | my $pl; |
| 537 | tprintf(2, "Family %d: %s\n", %family_id, %name); |
| 538 | |
| 539 | # display people, sorted by eye color, descending |
| 540 | context people ($people) |
| 541 | sortDescendingBy (find %value in $attributes |
| 542 | where (%attribute == "eyes" |
| 543 | && %person_id == %people:person_id)) |
| 544 | where (%family_id == %family:family_id) |
| 545 | { |
| 546 | my $al; |
| 547 | tprintf(2, " %s, born %s\n", %name, format_date("Month DD, YYYY", %dob)); |
| 548 | context ($attributes) sortBy (%attribute) where (%person_id == %people:person_id) |
| 549 | { |
| 550 | $al.%attribute = %value; |
| 551 | tprintf(2, " has %s %s\n", %value, %attribute); |
| 552 | } |
| 553 | # leave out the ID fields and name from hash under name; subtracting a |
| 554 | # string from a hash removes that key from the result |
| 555 | # this is "doing it the hard way", there is only one key left, |
| 556 | # "dob", then attributes are added directly into the person hash |
| 557 | $pl.%name = %% - "family_id" - "person_id" - "name" + $al; |
| 558 | } |
| 559 | # leave out family_id and name fields (leaving an empty hash) |
| 560 | $fl.%name = %% - "family_id" - "name" + ( "people" : $pl ); |
| 561 | } |
| 562 | |
| 563 | # test context ordering |
| 564 | test_value(keys $fl, ("Jones", "Smith"), "first context"); |
| 565 | test_value(keys $fl.Smith.people, ("Arnie", "Carol", "Isaac", "Bernard", "Sylvia"), "second context"); |
| 566 | # test entire context value |
| 567 | test_value($fl, family_hash, "third context"); |
| 568 | } |
| 569 | |
| 570 | |
| 571 | sub test_timeout($db, $c) |
| 572 | { |
| 573 | $db.setTransactionLockTimeout(1ms); |
| 574 | try { |
| 575 | # this should cause a TRANSACTION-LOCK-TIMEOUT exception to be thrown |
| 576 | $db.exec("insert into family values (3, 'Test')\n"); |
| 577 | test_value(True, False, "transaction timeout"); |
| 578 | $db.exec("delete from family where name = 'Test'"); |
| 579 | } |
| 580 | catch ($ex) |
| 581 | { |
| 582 | test_value(True, True, "transaction timeout"); |
| 583 | } |
| 584 | # signal parent thread to continue |
| 585 | $c.dec(); |
| 586 | } |
| 587 | |
| 588 | sub transaction_test($db) |
| 589 | { |
| 590 | my $ndb = getDS(); |
| 591 | my $r; |
| 592 | tprintf(2, "db.autocommit=%N, ndb.autocommit=%N\n", $db.getAutoCommit(), $ndb.getAutoCommit()); |
| 593 | |
| 594 | # first, we insert a new row into "family" but do not commit it |
| 595 | my $rows = $db.exec("insert into family values (3, 'Test')\n"); |
| 596 | if ($rows !== 1) |
| 597 | printf("FAILED INSERT, rows=%N\n", $rows); |
| 598 | |
| 599 | # now we verify that the new row is not visible to the other datasource |
| 600 | # unless it's a sybase/ms sql server datasource, in which case this would deadlock :-( |
| 601 | if ($o.type != "sybase" && $o.type != "freetds") |
| 602 | { |
| 603 | $r = $ndb.selectRow("select name from family where family_id = 3").name; |
| 604 | test_value($r, NOTHING, "first transaction"); |
| 605 | } |
| 606 | |
| 607 | # now we verify that the new row is visible to the inserting datasource |
| 608 | $r = $db.selectRow("select name from family where family_id = 3").name; |
| 609 | test_value($r, "Test", "second transaction"); |
| 610 | |
| 611 | # test datasource timeout |
| 612 | # this Counter variable will allow the parent thread to sleep |
| 613 | # until the child thread times out |
| 614 | my $c = new Counter(1); |
| 615 | background test_timeout($db, $c); |
| 616 | |
| 617 | # wait for child thread to time out |
| 618 | $c.waitForZero(); |
| 619 | |
| 620 | # now, we commit the transaction |
| 621 | $db.commit(); |
| 622 | |
| 623 | # now we verify that the new row is visible in the other datasource |
| 624 | $r = $ndb.selectRow("select name from family where family_id = 3").name; |
| 625 | test_value($r, "Test", "third transaction"); |
| 626 | |
| 627 | # now we delete the row we inserted (so we can repeat the test) |
| 628 | $r = $ndb.exec("delete from family where family_id = 3"); |
| 629 | test_value($r, 1, "delete row count"); |
| 630 | $ndb.commit(); |
| 631 | } |
| 632 | |
| 633 | sub oracle_test() |
| 634 | { |
| 635 | } |
| 636 | |
| 637 | # here we use a little workaround for modules that provide functions, |
| 638 | # namespace additions (constants, classes, etc) needed by test functions |
| 639 | # at parse time. To avoid parse errors (as database modules are loaded |
| 640 | # in this script at run-time when the Datasource class is instantiated) |
| 641 | # we use a Program object that we parse and run on demand to return the |
| 642 | # value required |
| 643 | sub get_val($code) |
| 644 | { |
| 645 | my $p = new Program(); |
| 646 | |
| 647 | my $str = sprintf("return %s;", $code); |
| 648 | $p.parse($str, "code"); |
| 649 | return $p.run(); |
| 650 | } |
| 651 | |
| 652 | sub pgsql_test($db) |
| 653 | { |
| 654 | my $args = ( "int2_f" : 258, |
| 655 | "int4_f" : 233932, |
| 656 | "int8_f" : 239392939458, |
| 657 | "bool_f" : True, |
| 658 | "float4_f" : 21.3444, |
| 659 | "float8_f" : 49394.23423491, |
| 660 | "number_f" : get_val("pgsql_bind(PG_TYPE_NUMERIC, '7235634215.3250')"), |
| 661 | "money_f" : get_val("pgsql_bind(PG_TYPE_CASH, \"400.56\")"), |
| 662 | "text_f" : 'some text ', |
| 663 | "varchar_f" : 'varchar ', |
| 664 | "char_f" : 'char text', |
| 665 | "name_f" : 'name', |
| 666 | "date_f" : 2004-01-05, |
| 667 | "abstime_f" : 2005-12-03T10:00:01, |
| 668 | "reltime_f" : 5M + 71D + 19h + 245m + 51s, |
| 669 | "interval_f" : 6M + 3D + 2h + 45m + 15s, |
| 670 | "time_f" : 11:35:00, |
| 671 | "timetz_f" : get_val("pgsql_bind(PG_TYPE_TIMETZ, \"11:38:21 CST\")"), |
| 672 | "timestamp_f" : 2005-04-01T11:35:26, |
| 673 | "timestamptz_f" : 2005-04-01T11:35:26.259, |
| 674 | "tinterval_f" : get_val("pgsql_bind(PG_TYPE_TINTERVAL, '[\"May 10, 1947 23:59:12\" \"Jan 14, 1973 03:14:21\"]')"), |
| 675 | "bytea_f" : <bead> |
| 676 | #bit_f : |
| 677 | #varbit_f : |
| 678 | ); |
| 679 | |
| 680 | $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)); |
| 681 | |
| 682 | my $q = $db.selectRow("select * from data_test"); |
| 683 | if ($o.verbose > 1) |
| 684 | foreach my $k in (keys $q) |
| 685 | tprintf(2, " %-16s= %-10s %N\n", $k, type($q.$k), $q.$k); |
| 686 | |
| 687 | # fix values where we know the return type is different |
| 688 | $args.money_f = 400.56; |
| 689 | $args.timetz_f = 11:38:21; |
| 690 | $args.tinterval_f = '["1947-05-10 21:59:12" "1973-01-14 02:14:21"]'; |
| 691 | $args.number_f = "7235634215.3250"; |
| 692 | $args.reltime_f = 19177551s; |
| 693 | $args.interval_f = 6M + 3D + 9915s; |
| 694 | |
| 695 | # rounding errors can happen in float4 |
| 696 | $q.float4_f = round($q.float4_f); |
| 697 | $args.float4_f = round($args.float4_f); |
| 698 | |
| 699 | # remove values where we know they won't match |
| 700 | # abstime and timestamptz are converted to GMT by the server |
| 701 | delete $q.abstime_f; |
| 702 | delete $q.timestamptz_f; |
| 703 | |
| 704 | # compare each value |
| 705 | foreach my $k in (keys $q) |
| 706 | test_value($q.$k, $args.$k, sprintf("%s bind and retrieve", $k)); |
| 707 | |
| 708 | $db.commit(); |
| 709 | } |
| 710 | |
| 711 | sub mysql_test() |
| 712 | { |
| 713 | } |
| 714 | |
| 715 | const family_q = ( "family_id" : 1, |
| 716 | "name" : "Smith" ); |
| 717 | const person_q = ( "person_id" : 1, |
| 718 | "family_id" : 1, |
| 719 | "name" : "Arnie", |
| 720 | "dob" : 1983-05-13 ); |
| 721 | const params = ( "string" : "hello there", |
| 722 | "int" : 150 ); |
| 723 | |
| 724 | sub sybase_test($db) |
| 725 | { |
| 726 | # simple stored proc test, bind by name |
| 727 | my $x = $db.exec("exec find_family %v", "Smith"); |
| 728 | test_value($x, ("name": list("Smith"), "family_id" : list(1)), "simple stored proc"); |
| 729 | |
| 730 | # stored proc execute with output params |
| 731 | $x = $db.exec("declare @string varchar(40), @int int |
| 732 | exec get_values :string output, :int output"); |
| 733 | test_value($x, params + ("rowcount":1), "get_values"); |
| 734 | |
| 735 | # we use Datasource::selectRows() in the following queries because we |
| 736 | # get hash results instead of a hash of lists as with exec in the queries |
| 737 | # normally we should not use selectRows to execute a stored procedure, |
| 738 | # as the Datasource::selectRows() method will not grab the transaction lock, |
| 739 | # but we already called Datasource::exec() above, so we have it already. |
| 740 | # the other alternative would be to call Datasource::beginTransaction() before |
| 741 | # Datasource::selectRows() |
| 742 | |
| 743 | # simple stored proc test, bind by name, returns hash |
| 744 | $x = $db.selectRows("exec find_family %v", "Smith"); |
| 745 | test_value($x, family_q, "simple stored proc"); |
| 746 | |
| 747 | # stored proc execute with output params and select results |
| 748 | $x = $db.selectRows("declare @string varchar(40), @int int |
| 749 | exec get_values_and_select :string output, :int output"); |
| 750 | test_value($x, ("query":family_q,"params":params), "get_values_and_select"); |
| 751 | |
| 752 | # stored proc execute with output params and multiple select results |
| 753 | $x = $db.selectRows("declare @string varchar(40), @int int |
| 754 | exec get_values_and_multiple_select :string output, :int output"); |
| 755 | test_value($x, ("query":("query0":family_q,"query1":person_q),"params":params), "get_values_and_multiple_select"); |
| 756 | |
| 757 | # stored proc execute with just select results |
| 758 | $x = $db.selectRows("exec just_select"); |
| 759 | test_value($x, family_q, "just_select"); |
| 760 | |
| 761 | # stored proc execute with multiple select results |
| 762 | $x = $db.selectRows("exec multiple_select"); |
| 763 | test_value($x, ("query0":family_q,"query1":person_q), "multiple_select"); |
| 764 | |
| 765 | my $args = ( "null_f" : NULL, |
| 766 | "varchar_f" : "varchar", |
| 767 | "char_f" : "char", |
| 768 | "unichar_f" : "unichar", |
| 769 | "univarchar_f" : "univarchar", |
| 770 | "text_f" : "test", |
| 771 | "unitext_f" : "test", |
| 772 | "bit_f" : True, |
| 773 | "tinyint_f" : 55, |
| 774 | "smallint_f" : 4285, |
| 775 | "int_f" : 405402, |
| 776 | "int_f2" : 214123498, |
| 777 | "decimal_f" : 500.1231, |
| 778 | "float_f" : 23443.234324234, |
| 779 | "real_f" : 213.123, |
| 780 | "money_f" : 3434234250.2034, |
| 781 | "smallmoney_f" : 211100.1012, |
| 782 | "date_f" : 2007-05-01, |
| 783 | "time_f" : 10:30:01, |
| 784 | "datetime_f" : 3459-01-01T11:15:02.250, |
| 785 | "smalldatetime_f" : 2007-12-01T12:01:00, |
| 786 | "binary_f" : <0badbeef>, |
| 787 | "varbinary_f" : <feedface>, |
| 788 | "image_f" : <cafebead> ); |
| 789 | |
| 790 | # insert data |
| 791 | 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)); |
| 792 | |
| 793 | my $q = $db.selectRow("select * from data_test"); |
| 794 | if ($o.verbose > 1) |
| 795 | foreach my $k in (keys $q) |
| 796 | tprintf(2, " %-16s= %-10s %N\n", $k, type($q.$k), $q.$k); |
| 797 | |
| 798 | # remove values where we know they won't match |
| 799 | # unitext_f is returned as IMAGE by the server |
| 800 | delete $q.unitext_f; |
| 801 | delete $args.unitext_f; |
| 802 | # rounding errors can happen in real |
| 803 | $q.real_f = round($q.real_f); |
| 804 | $args.real_f = round($args.real_f); |
| 805 | |
| 806 | # compare each value |
| 807 | foreach my $k in (keys $q) |
| 808 | test_value($q.$k, $args.$k, sprintf("%s bind and retrieve", $k)); |
| 809 | |
| 810 | $db.commit(); |
| 811 | } |
| 812 | |
| 813 | sub freetds_test($db) |
| 814 | { |
| 815 | # simple stored proc test, bind by name |
| 816 | my $x = $db.exec("exec find_family %v", "Smith"); |
| 817 | test_value($x, ("name": list("Smith"), "family_id" : list(1)), "simple stored proc"); |
| 818 | |
| 819 | # we cannot retrieve parameters from newer SQL Servers with the approach we use; |
| 820 | # Microsoft changed the handling of the protocol and require us to use RPC calls, |
| 821 | # this will be implemented in the next version of qore where the "freetds" driver will |
| 822 | # be able to add custom methods to the Datasource class. For now, we skip these tests |
| 823 | |
| 824 | if ($db.is_sybase) |
| 825 | { |
| 826 | $x = $db.exec("declare @string varchar(40), @int int |
| 827 | exec get_values :string output, :int output"); |
| 828 | test_value($x, params, "get_values"); |
| 829 | } |
| 830 | |
| 831 | # we use Datasource::selectRows() in the following queries because we |
| 832 | # get hash results instead of a hash of lists as with exec in the queries |
| 833 | # normally we should not use selectRows to execute a stored procedure, |
| 834 | # as the Datasource::selectRows() method will not grab the transaction lock, |
| 835 | # but we already called Datasource::exec() above, so we have it already. |
| 836 | # the other alternative would be to call Datasource::beginTransaction() before |
| 837 | # Datasource::selectRows() |
| 838 | |
| 839 | # simple stored proc test, bind by name, returns hash |
| 840 | $x = $db.selectRows("exec find_family %v", "Smith"); |
| 841 | test_value($x, family_q, "simple stored proc"); |
| 842 | |
| 843 | # stored proc execute with output params and select results |
| 844 | if ($db.is_sybase) |
| 845 | { |
| 846 | $x = $db.selectRows("declare @string varchar(40), @int int |
| 847 | exec get_values_and_select :string output, :int output"); |
| 848 | test_value($x, ("query":family_q,"params":params), "get_values_and_select"); |
| 849 | |
| 850 | # stored proc execute with output params and multiple select results |
| 851 | $x = $db.selectRows("declare @string varchar(40), @int int |
| 852 | exec get_values_and_multiple_select :string output, :int output"); |
| 853 | test_value($x, ("query":("query0":family_q,"query1":person_q),"params":params), "get_values_and_multiple_select"); |
| 854 | } |
| 855 | |
| 856 | # stored proc execute with just select results |
| 857 | $x = $db.selectRows("exec just_select"); |
| 858 | test_value($x, family_q, "just_select"); |
| 859 | |
| 860 | # stored proc execute with multiple select results |
| 861 | $x = $db.selectRows("exec multiple_select"); |
| 862 | test_value($x, ("query0":family_q,"query1":person_q), "multiple_select"); |
| 863 | |
| 864 | # the freetds driver does not work with the following sybase column types: |
| 865 | # unichar, univarchar |
| 866 | |
| 867 | my $args = ( "null_f" : NULL, |
| 868 | "varchar_f" : "test", |
| 869 | "char_f" : "test", |
| 870 | "text_f" : "test", |
| 871 | "unitext_f" : "test", |
| 872 | "bit_f" : True, |
| 873 | "tinyint_f" : 55, |
| 874 | "smallint_f" : 4285, |
| 875 | "int_f" : 405402, |
| 876 | "int_f2" : 214123498, |
| 877 | "decimal_f" : 500.1231, |
| 878 | "float_f" : 23443.234324234, |
| 879 | "real_f" : 213.123, |
| 880 | "money_f" : 3434234250.2034, |
| 881 | "smallmoney_f" : 211100.1012, |
| 882 | "date_f" : 2007-05-01, |
| 883 | "time_f" : 10:30:01, |
| 884 | "datetime_f" : 3459-01-01T11:15:02.250, |
| 885 | "smalldatetime_f" : 2007-12-01T12:01:00, |
| 886 | "binary_f" : <0badbeef>, |
| 887 | "varbinary_f" : <feedface>, |
| 888 | "image_f" : <cafebead> ); |
| 889 | |
| 890 | # remove fields not supported by sql server |
| 891 | if (!$db.is_sybase) |
| 892 | { |
| 893 | delete $args.unitext_f; |
| 894 | delete $args.date_f; |
| 895 | delete $args.time_f; |
| 896 | } |
| 897 | |
| 898 | my $sql = "insert into data_test values ("; |
| 899 | for (my $i; $i < elements $args; ++$i) |
| 900 | $sql += "%v, "; |
| 901 | $sql = substr($sql, 0, -2) + ")"; |
| 902 | |
| 903 | # insert data, using the values from the hash above |
| 904 | my $rows = $db.vexec($sql, hash_values($args)); |
| 905 | |
| 906 | my $q = $db.selectRow("select * from data_test"); |
| 907 | if ($o.verbose > 1) |
| 908 | foreach my $k in (keys $q) |
| 909 | tprintf(2, " %-16s= %-10s %N\n", $k, type($q.$k), $q.$k); |
| 910 | |
| 911 | # remove values where we know they won't match |
| 912 | # unitext_f is returned as IMAGE by the server |
| 913 | delete $q.unitext_f; |
| 914 | delete $args.unitext_f; |
| 915 | # rounding errors can happen in real |
| 916 | $q.real_f = round($q.real_f); |
| 917 | $args.real_f = round($args.real_f); |
| 918 | |
| 919 | # compare each value |
| 920 | foreach my $k in (keys $q) |
| 921 | test_value($q.$k, $args.$k, sprintf("%s bind and retrieve", $k)); |
| 922 | |
| 923 | $db.commit(); |
| 924 | } |
| 925 | |
| 926 | sub main() |
| 927 | { |
| 928 | my $test_map = |
| 929 | ( "sybase" : \sybase_test(), |
| 930 | "freetds" : \freetds_test(), |
| 931 | "mysql" : \mysql_test(), |
| 932 | "pgsql" : \pgsql_test(), |
| 933 | "oracle" : \oracle_test()); |
| 934 | |
| 935 | parse_command_line(); |
| 936 | my $db = getDS(); |
| 937 | |
| 938 | my $driver = $db.getDriverName(); |
| 939 | printf("testing %s driver\n", $driver); |
| 940 | my $sv = $db.getServerVersion(); |
| 941 | if ($o.verbose > 1) |
| 942 | tprintf(2, "client version=%n\nserver version=%n\n", $db.getClientVersion(), $sv); |
| 943 | |
| 944 | # determine if the server is a sybase or sql server dataserver |
| 945 | if ($driver == "freetds") |
| 946 | if ($sv !~ /microsoft/i) |
| 947 | $db.is_sybase = True; |
| 948 | |
| 949 | create_datamodel($db); |
| 950 | |
| 951 | context_test($db); |
| 952 | transaction_test($db); |
| 953 | my $test = $test_map.($db.getDriverName()); |
| 954 | if (exists $test) |
| 955 | $test($db); |
| 956 | |
| 957 | if (!$o.leave) |
| 958 | drop_test_datamodel($db); |
| 959 | printf("%d/%d tests OK\n", $test_count - $errors, $test_count); |
| 960 | } |
| 961 | |
| 962 | main();</code></pre> |