| ... | ... |
@@ -1,7 +1,8 @@ |
| 1 | 1 |
use Test::More; |
| 2 | 2 |
use strict; |
| 3 | 3 |
use warnings; |
| 4 |
-use DBIx::Custom; |
|
| 4 |
+use DBIx::Custom; |
|
| 5 |
+use Encode qw/encode_utf8/; |
|
| 5 | 6 |
|
| 6 | 7 |
my $dbi; |
| 7 | 8 |
|
| ... | ... |
@@ -313,5 +314,448 @@ $result = $dbi->execute('select * from table1;');
|
| 313 | 314 |
$rows = $result->all; |
| 314 | 315 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
|
| 315 | 316 |
|
| 317 |
+test 'update'; |
|
| 318 |
+eval { $dbi->execute('drop table table1') };
|
|
| 319 |
+$dbi->execute($create_table1_2); |
|
| 320 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
|
|
| 321 |
+$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
|
|
| 322 |
+$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1});
|
|
| 323 |
+$result = $dbi->execute('select * from table1;');
|
|
| 324 |
+$rows = $result->all; |
|
| 325 |
+is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
|
|
| 326 |
+ {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}],
|
|
| 327 |
+ "basic"); |
|
| 328 |
+ |
|
| 329 |
+$dbi->execute("delete from table1");
|
|
| 330 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
|
|
| 331 |
+$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
|
|
| 332 |
+$dbi->update(table => 'table1', param => {key2 => 12}, where => {key2 => 2, key3 => 3});
|
|
| 333 |
+$result = $dbi->execute('select * from table1;');
|
|
| 334 |
+$rows = $result->all; |
|
| 335 |
+is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
|
|
| 336 |
+ {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}],
|
|
| 337 |
+ "update key same as search key"); |
|
| 338 |
+ |
|
| 339 |
+$dbi->update(table => 'table1', param => {key2 => [12]}, where => {key2 => 2, key3 => 3});
|
|
| 340 |
+$result = $dbi->execute('select * from table1;');
|
|
| 341 |
+$rows = $result->all; |
|
| 342 |
+is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
|
|
| 343 |
+ {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}],
|
|
| 344 |
+ "update key same as search key : param is array ref"); |
|
| 345 |
+ |
|
| 346 |
+$dbi->execute("delete from table1");
|
|
| 347 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
|
|
| 348 |
+$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
|
|
| 349 |
+$dbi->register_filter(twice => sub { $_[0] * 2 });
|
|
| 350 |
+$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1},
|
|
| 351 |
+ filter => {key2 => sub { $_[0] * 2 }});
|
|
| 352 |
+$result = $dbi->execute('select * from table1;');
|
|
| 353 |
+$rows = $result->all; |
|
| 354 |
+is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5},
|
|
| 355 |
+ {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}],
|
|
| 356 |
+ "filter"); |
|
| 357 |
+ |
|
| 358 |
+$result = $dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}, append => ' ');
|
|
| 359 |
+ |
|
| 360 |
+eval{$dbi->update(table => 'table1', where => {key1 => 1}, noexist => 1)};
|
|
| 361 |
+like($@, qr/noexist/, "invalid"); |
|
| 362 |
+ |
|
| 363 |
+eval{$dbi->update(table => 'table1')};
|
|
| 364 |
+like($@, qr/where/, "not contain where"); |
|
| 365 |
+ |
|
| 366 |
+eval { $dbi->execute('drop table table1') };
|
|
| 367 |
+$dbi->execute($create_table1); |
|
| 368 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 369 |
+$where = $dbi->where; |
|
| 370 |
+$where->clause(['and', 'key1 = :key1', 'key2 = :key2']); |
|
| 371 |
+$where->param({key1 => 1, key2 => 2});
|
|
| 372 |
+$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
|
|
| 373 |
+$result = $dbi->select(table => 'table1'); |
|
| 374 |
+is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
|
|
| 375 |
+ |
|
| 376 |
+eval { $dbi->execute('drop table table1') };
|
|
| 377 |
+$dbi->execute($create_table1); |
|
| 378 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 379 |
+$dbi->update( |
|
| 380 |
+ table => 'table1', |
|
| 381 |
+ param => {key1 => 3},
|
|
| 382 |
+ where => [ |
|
| 383 |
+ ['and', 'key1 = :key1', 'key2 = :key2'], |
|
| 384 |
+ {key1 => 1, key2 => 2}
|
|
| 385 |
+ ] |
|
| 386 |
+); |
|
| 387 |
+$result = $dbi->select(table => 'table1'); |
|
| 388 |
+is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
|
|
| 389 |
+ |
|
| 390 |
+eval { $dbi->execute('drop table table1') };
|
|
| 391 |
+$dbi->execute($create_table1); |
|
| 392 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 393 |
+$where = $dbi->where; |
|
| 394 |
+$where->clause(['and', 'key2 = :key2']); |
|
| 395 |
+$where->param({key2 => 2});
|
|
| 396 |
+$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
|
|
| 397 |
+$result = $dbi->select(table => 'table1'); |
|
| 398 |
+is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
|
|
| 399 |
+ |
|
| 400 |
+eval{$dbi->update(table => 'table1', param => {';' => 1})};
|
|
| 401 |
+like($@, qr/safety/); |
|
| 402 |
+ |
|
| 403 |
+eval{$dbi->update(table => 'table1', param => {'key1' => 1}, where => {';' => 1})};
|
|
| 404 |
+like($@, qr/safety/); |
|
| 405 |
+ |
|
| 406 |
+eval { $dbi->execute('drop table table1') };
|
|
| 407 |
+$dbi->quote('"');
|
|
| 408 |
+eval { $dbi->execute("drop table ${q}table$p") };
|
|
| 409 |
+$dbi->execute("create table ${q}table$p (${q}select$p, ${q}update$p)");
|
|
| 410 |
+$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
|
|
| 411 |
+$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
|
|
| 412 |
+$dbi->insert(table => 'table', param => {select => 1});
|
|
| 413 |
+$dbi->update(table => 'table', where => {select => 1}, param => {update => 2});
|
|
| 414 |
+$result = $dbi->execute("select * from ${q}table$p");
|
|
| 415 |
+$rows = $result->all; |
|
| 416 |
+is_deeply($rows, [{select => 2, update => 6}], "reserved word");
|
|
| 417 |
+ |
|
| 418 |
+eval {$dbi->update_all(table => 'table', param => {';' => 2}) };
|
|
| 419 |
+like($@, qr/safety/); |
|
| 420 |
+ |
|
| 421 |
+eval { $dbi->execute("drop table ${q}table$p") };
|
|
| 422 |
+$dbi->reserved_word_quote('"');
|
|
| 423 |
+$dbi->execute("create table ${q}table$p (${q}select$p, ${q}update$p)");
|
|
| 424 |
+$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
|
|
| 425 |
+$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
|
|
| 426 |
+$dbi->insert(table => 'table', param => {select => 1});
|
|
| 427 |
+$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
|
|
| 428 |
+$result = $dbi->execute("select * from ${q}table$p");
|
|
| 429 |
+$rows = $result->all; |
|
| 430 |
+is_deeply($rows, [{select => 2, update => 6}], "reserved word");
|
|
| 431 |
+ |
|
| 432 |
+eval { $dbi->execute('drop table table1') };
|
|
| 433 |
+$dbi->execute($create_table1_2); |
|
| 434 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
|
|
| 435 |
+$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
|
|
| 436 |
+$dbi->update({key2 => 11}, table => 'table1', where => {key1 => 1});
|
|
| 437 |
+$result = $dbi->execute('select * from table1;');
|
|
| 438 |
+$rows = $result->all; |
|
| 439 |
+is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
|
|
| 440 |
+ {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}],
|
|
| 441 |
+ "basic"); |
|
| 442 |
+ |
|
| 443 |
+eval { $dbi->execute('drop table table1') };
|
|
| 444 |
+$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
|
|
| 445 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 446 |
+$dbi->update(table => 'table1', param => {key2 => 4},
|
|
| 447 |
+ where => {key1 => 1}, prefix => 'or replace');
|
|
| 448 |
+$result = $dbi->execute('select * from table1;');
|
|
| 449 |
+$rows = $result->all; |
|
| 450 |
+is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
|
|
| 451 |
+ |
|
| 452 |
+eval { $dbi->execute('drop table table1') };
|
|
| 453 |
+$dbi->execute($create_table1_2); |
|
| 454 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
|
|
| 455 |
+$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
|
|
| 456 |
+$dbi->update(table => 'table1', param => {key2 => \"'11'"}, where => {key1 => 1});
|
|
| 457 |
+$result = $dbi->execute('select * from table1;');
|
|
| 458 |
+$rows = $result->all; |
|
| 459 |
+is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
|
|
| 460 |
+ {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}],
|
|
| 461 |
+ "basic"); |
|
| 462 |
+ |
|
| 463 |
+test 'update_all'; |
|
| 464 |
+eval { $dbi->execute('drop table table1') };
|
|
| 465 |
+$dbi->execute($create_table1_2); |
|
| 466 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
|
|
| 467 |
+$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
|
|
| 468 |
+$dbi->register_filter(twice => sub { $_[0] * 2 });
|
|
| 469 |
+$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'});
|
|
| 470 |
+$result = $dbi->execute('select * from table1;');
|
|
| 471 |
+$rows = $result->all; |
|
| 472 |
+is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
|
|
| 473 |
+ {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
|
|
| 474 |
+ "filter"); |
|
| 475 |
+ |
|
| 476 |
+ |
|
| 477 |
+test 'delete'; |
|
| 478 |
+eval { $dbi->execute('drop table table1') };
|
|
| 479 |
+$dbi->execute($create_table1); |
|
| 480 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 481 |
+$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
|
|
| 482 |
+$dbi->delete(table => 'table1', where => {key1 => 1});
|
|
| 483 |
+$result = $dbi->execute('select * from table1;');
|
|
| 484 |
+$rows = $result->all; |
|
| 485 |
+is_deeply($rows, [{key1 => 3, key2 => 4}], "basic");
|
|
| 486 |
+ |
|
| 487 |
+$dbi->execute("delete from table1;");
|
|
| 488 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 489 |
+$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
|
|
| 490 |
+$dbi->register_filter(twice => sub { $_[0] * 2 });
|
|
| 491 |
+$dbi->delete(table => 'table1', where => {key2 => 1}, filter => {key2 => 'twice'});
|
|
| 492 |
+$result = $dbi->execute('select * from table1;');
|
|
| 493 |
+$rows = $result->all; |
|
| 494 |
+is_deeply($rows, [{key1 => 3, key2 => 4}], "filter");
|
|
| 495 |
+ |
|
| 496 |
+$dbi->delete(table => 'table1', where => {key1 => 1}, append => ' ');
|
|
| 497 |
+ |
|
| 498 |
+$dbi->delete_all(table => 'table1'); |
|
| 499 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 500 |
+$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
|
|
| 501 |
+$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2});
|
|
| 502 |
+$rows = $dbi->select(table => 'table1')->all; |
|
| 503 |
+is_deeply($rows, [{key1 => 3, key2 => 4}], "delete multi key");
|
|
| 504 |
+ |
|
| 505 |
+eval{$dbi->delete(table => 'table1', where => {key1 => 1}, noexist => 1)};
|
|
| 506 |
+like($@, qr/noexist/, "invalid"); |
|
| 507 |
+ |
|
| 508 |
+eval { $dbi->execute('drop table table1') };
|
|
| 509 |
+$dbi->execute($create_table1); |
|
| 510 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 511 |
+$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
|
|
| 512 |
+$where = $dbi->where; |
|
| 513 |
+$where->clause(['and', 'key1 = :key1', 'key2 = :key2']); |
|
| 514 |
+$where->param({ke1 => 1, key2 => 2});
|
|
| 515 |
+$dbi->delete(table => 'table1', where => $where); |
|
| 516 |
+$result = $dbi->select(table => 'table1'); |
|
| 517 |
+is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
|
|
| 518 |
+ |
|
| 519 |
+eval { $dbi->execute('drop table table1') };
|
|
| 520 |
+$dbi->execute($create_table1); |
|
| 521 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 522 |
+$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
|
|
| 523 |
+$dbi->delete( |
|
| 524 |
+ table => 'table1', |
|
| 525 |
+ where => [ |
|
| 526 |
+ ['and', 'key1 = :key1', 'key2 = :key2'], |
|
| 527 |
+ {ke1 => 1, key2 => 2}
|
|
| 528 |
+ ] |
|
| 529 |
+); |
|
| 530 |
+$result = $dbi->select(table => 'table1'); |
|
| 531 |
+is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
|
|
| 532 |
+ |
|
| 533 |
+eval { $dbi->execute('drop table table1') };
|
|
| 534 |
+$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
|
|
| 535 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 536 |
+$dbi->delete(table => 'table1', where => {key1 => 1}, prefix => ' ');
|
|
| 537 |
+$result = $dbi->execute('select * from table1;');
|
|
| 538 |
+$rows = $result->all; |
|
| 539 |
+is_deeply($rows, [], "basic"); |
|
| 540 |
+ |
|
| 541 |
+test 'delete error'; |
|
| 542 |
+eval { $dbi->execute('drop table table1') };
|
|
| 543 |
+$dbi->execute($create_table1); |
|
| 544 |
+eval{$dbi->delete(table => 'table1')};
|
|
| 545 |
+like($@, qr/"where" must be specified/, |
|
| 546 |
+ "where key-value pairs not specified"); |
|
| 547 |
+ |
|
| 548 |
+eval{$dbi->delete(table => 'table1', where => {';' => 1})};
|
|
| 549 |
+like($@, qr/safety/); |
|
| 550 |
+ |
|
| 551 |
+$dbi = DBIx::Custom->connect; |
|
| 552 |
+$dbi->quote('"');
|
|
| 553 |
+eval { $dbi->execute("drop table ${q}table$p") };
|
|
| 554 |
+$dbi->execute("create table ${q}table$p (${q}select$p, ${q}update$p)");
|
|
| 555 |
+$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
|
|
| 556 |
+$dbi->insert(table => 'table', param => {select => 1});
|
|
| 557 |
+$dbi->delete(table => 'table', where => {select => 1});
|
|
| 558 |
+$result = $dbi->execute("select * from ${q}table$p");
|
|
| 559 |
+$rows = $result->all; |
|
| 560 |
+is_deeply($rows, [], "reserved word"); |
|
| 561 |
+ |
|
| 562 |
+test 'delete_all'; |
|
| 563 |
+eval { $dbi->execute('drop table table1') };
|
|
| 564 |
+$dbi->execute($create_table1); |
|
| 565 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 566 |
+$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
|
|
| 567 |
+$dbi->delete_all(table => 'table1'); |
|
| 568 |
+$result = $dbi->execute('select * from table1;');
|
|
| 569 |
+$rows = $result->all; |
|
| 570 |
+is_deeply($rows, [], "basic"); |
|
| 571 |
+ |
|
| 572 |
+ |
|
| 573 |
+test 'select'; |
|
| 574 |
+eval { $dbi->execute('drop table table1') };
|
|
| 575 |
+$dbi->execute($create_table1); |
|
| 576 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 577 |
+$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
|
|
| 578 |
+$rows = $dbi->select(table => 'table1')->all; |
|
| 579 |
+is_deeply($rows, [{key1 => 1, key2 => 2},
|
|
| 580 |
+ {key1 => 3, key2 => 4}], "table");
|
|
| 581 |
+ |
|
| 582 |
+$rows = $dbi->select(table => 'table1', column => ['key1'])->all; |
|
| 583 |
+is_deeply($rows, [{key1 => 1}, {key1 => 3}], "table and columns and where key");
|
|
| 584 |
+ |
|
| 585 |
+$rows = $dbi->select(table => 'table1', where => {key1 => 1})->all;
|
|
| 586 |
+is_deeply($rows, [{key1 => 1, key2 => 2}], "table and columns and where key");
|
|
| 587 |
+ |
|
| 588 |
+$rows = $dbi->select(table => 'table1', column => ['key1'], where => {key1 => 3})->all;
|
|
| 589 |
+is_deeply($rows, [{key1 => 3}], "table and columns and where key");
|
|
| 590 |
+ |
|
| 591 |
+$rows = $dbi->select(table => 'table1', append => "order by key1 desc limit 1")->all; |
|
| 592 |
+is_deeply($rows, [{key1 => 3, key2 => 4}], "append statement");
|
|
| 593 |
+ |
|
| 594 |
+$dbi->register_filter(decrement => sub { $_[0] - 1 });
|
|
| 595 |
+$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'})
|
|
| 596 |
+ ->all; |
|
| 597 |
+is_deeply($rows, [{key1 => 1, key2 => 2}], "filter");
|
|
| 598 |
+ |
|
| 599 |
+$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
|
|
| 600 |
+$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
|
|
| 601 |
+$rows = $dbi->select( |
|
| 602 |
+ table => [qw/table1 table2/], |
|
| 603 |
+ column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3', |
|
| 604 |
+ where => {'table1.key2' => 2},
|
|
| 605 |
+ relation => {'table1.key1' => 'table2.key1'}
|
|
| 606 |
+)->all; |
|
| 607 |
+is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : exists where");
|
|
| 608 |
+ |
|
| 609 |
+$rows = $dbi->select( |
|
| 610 |
+ table => [qw/table1 table2/], |
|
| 611 |
+ column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'], |
|
| 612 |
+ relation => {'table1.key1' => 'table2.key1'}
|
|
| 613 |
+)->all; |
|
| 614 |
+is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : no exists where");
|
|
| 615 |
+ |
|
| 616 |
+eval{$dbi->select(table => 'table1', noexist => 1)};
|
|
| 617 |
+like($@, qr/noexist/, "invalid"); |
|
| 618 |
+ |
|
| 619 |
+$dbi = DBIx::Custom->connect; |
|
| 620 |
+$dbi->quote('"');
|
|
| 621 |
+$dbi->execute("create table ${q}table$p (${q}select$p, ${q}update$p)");
|
|
| 622 |
+$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
|
|
| 623 |
+$dbi->insert(table => 'table', param => {select => 1, update => 2});
|
|
| 624 |
+$result = $dbi->select(table => 'table', where => {select => 1});
|
|
| 625 |
+$rows = $result->all; |
|
| 626 |
+is_deeply($rows, [{select => 2, update => 2}], "reserved word");
|
|
| 627 |
+ |
|
| 628 |
+test 'fetch filter'; |
|
| 629 |
+eval { $dbi->execute('drop table table1') };
|
|
| 630 |
+$dbi->register_filter( |
|
| 631 |
+ twice => sub { $_[0] * 2 },
|
|
| 632 |
+ three_times => sub { $_[0] * 3 }
|
|
| 633 |
+); |
|
| 634 |
+$dbi->default_fetch_filter('twice');
|
|
| 635 |
+$dbi->execute($create_table1); |
|
| 636 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 637 |
+$result = $dbi->select(table => 'table1'); |
|
| 638 |
+$result->filter({key1 => 'three_times'});
|
|
| 639 |
+$row = $result->one; |
|
| 640 |
+is_deeply($row, {key1 => 3, key2 => 4}, "default_fetch_filter and filter");
|
|
| 641 |
+ |
|
| 642 |
+test 'filters'; |
|
| 643 |
+$dbi = DBIx::Custom->new; |
|
| 644 |
+ |
|
| 645 |
+is($dbi->filters->{decode_utf8}->(encode_utf8('あ')),
|
|
| 646 |
+ 'あ', "decode_utf8"); |
|
| 647 |
+ |
|
| 648 |
+is($dbi->filters->{encode_utf8}->('あ'),
|
|
| 649 |
+ encode_utf8('あ'), "encode_utf8");
|
|
| 650 |
+ |
|
| 651 |
+test 'transaction'; |
|
| 652 |
+$dbi = DBIx::Custom->connect; |
|
| 653 |
+eval { $dbi->execute('drop table table1') };
|
|
| 654 |
+$dbi->execute($create_table1); |
|
| 655 |
+$dbi->dbh->begin_work; |
|
| 656 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 657 |
+$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
|
|
| 658 |
+$dbi->dbh->commit; |
|
| 659 |
+$result = $dbi->select(table => 'table1'); |
|
| 660 |
+is_deeply(scalar $result->all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}],
|
|
| 661 |
+ "commit"); |
|
| 662 |
+ |
|
| 663 |
+$dbi = DBIx::Custom->connect; |
|
| 664 |
+eval { $dbi->execute('drop table table1') };
|
|
| 665 |
+$dbi->execute($create_table1); |
|
| 666 |
+$dbi->dbh->begin_work(0); |
|
| 667 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 668 |
+$dbi->dbh->rollback; |
|
| 669 |
+ |
|
| 670 |
+$result = $dbi->select(table => 'table1'); |
|
| 671 |
+ok(! $result->fetch_first, "rollback"); |
|
| 672 |
+ |
|
| 673 |
+test 'cache'; |
|
| 674 |
+eval { $dbi->execute('drop table table1') };
|
|
| 675 |
+$dbi->cache(1); |
|
| 676 |
+$dbi->execute($create_table1); |
|
| 677 |
+$source = 'select * from table1 where key1 = :key1 and key2 = :key2;'; |
|
| 678 |
+$dbi->execute($source, {}, query => 1);
|
|
| 679 |
+is_deeply($dbi->{_cached}->{$source},
|
|
| 680 |
+ {sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2'], tables => []}, "cache");
|
|
| 681 |
+ |
|
| 682 |
+eval { $dbi->execute('drop table table1') };
|
|
| 683 |
+$dbi->execute($create_table1); |
|
| 684 |
+$dbi->{_cached} = {};
|
|
| 685 |
+$dbi->cache(0); |
|
| 686 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 687 |
+is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
|
|
| 688 |
+ |
|
| 689 |
+test 'execute'; |
|
| 690 |
+eval { $dbi->execute('drop table table1') };
|
|
| 691 |
+$dbi->execute($create_table1); |
|
| 692 |
+{
|
|
| 693 |
+ local $Carp::Verbose = 0; |
|
| 694 |
+ eval{$dbi->execute('select * frm table1')};
|
|
| 695 |
+ like($@, qr/\Qselect * frm table1;/, "fail prepare"); |
|
| 696 |
+ like($@, qr/\.t /, "fail : not verbose"); |
|
| 697 |
+} |
|
| 698 |
+{
|
|
| 699 |
+ local $Carp::Verbose = 1; |
|
| 700 |
+ eval{$dbi->execute('select * frm table1')};
|
|
| 701 |
+ like($@, qr/Custom.*\.t /s, "fail : verbose"); |
|
| 702 |
+} |
|
| 703 |
+ |
|
| 704 |
+eval{$dbi->execute('select * from table1', no_exists => 1)};
|
|
| 705 |
+like($@, qr/wrong/, "invald SQL"); |
|
| 706 |
+ |
|
| 707 |
+$query = $dbi->execute('select * from table1 where key1 = :key1', {}, query => 1);
|
|
| 708 |
+$dbi->dbh->disconnect; |
|
| 709 |
+eval{$dbi->execute($query, param => {key1 => {a => 1}})};
|
|
| 710 |
+ok($@, "execute fail"); |
|
| 711 |
+ |
|
| 712 |
+{
|
|
| 713 |
+ local $Carp::Verbose = 0; |
|
| 714 |
+ eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
|
|
| 715 |
+ like($@, qr/\Q.t /, "caller spec : not vebose"); |
|
| 716 |
+} |
|
| 717 |
+{
|
|
| 718 |
+ local $Carp::Verbose = 1; |
|
| 719 |
+ eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
|
|
| 720 |
+ like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose"); |
|
| 721 |
+} |
|
| 722 |
+ |
|
| 723 |
+ |
|
| 724 |
+test 'transaction'; |
|
| 725 |
+$dbi = DBIx::Custom->connect; |
|
| 726 |
+eval { $dbi->execute('drop table table1') };
|
|
| 727 |
+$dbi->execute($create_table1); |
|
| 728 |
+ |
|
| 729 |
+$dbi->begin_work; |
|
| 730 |
+ |
|
| 731 |
+eval {
|
|
| 732 |
+ $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 733 |
+ die "Error"; |
|
| 734 |
+ $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
|
|
| 735 |
+}; |
|
| 736 |
+ |
|
| 737 |
+$dbi->rollback if $@; |
|
| 738 |
+ |
|
| 739 |
+$result = $dbi->select(table => 'table1'); |
|
| 740 |
+$rows = $result->all; |
|
| 741 |
+is_deeply($rows, [], "rollback"); |
|
| 742 |
+ |
|
| 743 |
+$dbi->begin_work; |
|
| 744 |
+ |
|
| 745 |
+eval {
|
|
| 746 |
+ $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 747 |
+ $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
|
|
| 748 |
+}; |
|
| 749 |
+ |
|
| 750 |
+$dbi->commit unless $@; |
|
| 751 |
+ |
|
| 752 |
+$result = $dbi->select(table => 'table1'); |
|
| 753 |
+$rows = $result->all; |
|
| 754 |
+is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "commit");
|
|
| 755 |
+ |
|
| 756 |
+$dbi->dbh->{AutoCommit} = 0;
|
|
| 757 |
+eval{ $dbi->begin_work };
|
|
| 758 |
+ok($@, "exception"); |
|
| 759 |
+$dbi->dbh->{AutoCommit} = 1;
|
|
| 316 | 760 |
|
| 317 | 761 |
1; |