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
|
use strict;
use warnings;
no warnings 'qw';
use SQL::Abstract::More;
use Test::More;
use SQL::Abstract::Test import => [qw/is_same_sql_bind/];
diag( "Testing SQL::Abstract::More $SQL::Abstract::More::VERSION, "
."extends @SQL::Abstract::More::ISA, Perl $], $^X" );
use constant N_DBI_MOCK_TESTS => 2;
my $sqla = SQL::Abstract::More->new;
my ($sql, @bind, $join);
#----------------------------------------------------------------------
# various forms of select()
#----------------------------------------------------------------------
# old API transmitted to parent
($sql, @bind) = $sqla->select('Foo', 'bar', {bar => {">" => 123}}, ['bar']);
is_same_sql_bind(
$sql, \@bind,
"SELECT bar FROM Foo WHERE bar > ? ORDER BY bar", [123],
"old API (positional parameters)",
);
# idem, new API
($sql, @bind) = $sqla->select(
-columns => [qw/bar/],
-from => 'Foo',
-where => {bar => {">" => 123}},
-order_by => ['bar']
);
is_same_sql_bind(
$sql, \@bind,
"SELECT bar FROM Foo WHERE bar > ? ORDER BY bar", [123],
"new API : named parameters",
);
# pass one table as array
($sql, @bind) = $sqla->select(
-columns => [qw/bar/],
-from => ['Foo'],
-where => {bar => {">" => 123}},
-order_by => ['bar']
);
is_same_sql_bind(
$sql, \@bind,
"SELECT bar FROM Foo WHERE bar > ? ORDER BY bar", [123],
"-from => arrayref (1 table)",
);
# pass several tables as array
($sql, @bind) = $sqla->select(
-columns => [qw/bar/],
-from => [qw/Foo Bar Buz/],
-where => {bar => {">" => 123}},
);
is_same_sql_bind(
$sql, \@bind,
"SELECT bar FROM Foo, Bar, Buz WHERE bar > ?", [123],
"-from => arrayref (several tables)",
);
($sql, @bind) = $sqla->select(
-columns => [qw/bar/],
-from => \ 'Foo',
-where => {bar => {">" => 123}},
);
is_same_sql_bind(
$sql, \@bind,
"SELECT bar FROM Foo WHERE bar > ?", [123],
"-from => scalarref",
);
# -from with alias
($sql, @bind) = $sqla->select(
-columns => [qw/bar/],
-from => 'Foo|f',
-where => {"f.bar" => 123},
);
is_same_sql_bind(
$sql, \@bind,
"SELECT bar FROM Foo AS f WHERE f.bar = ?", [123],
"-from with alias"
);
# -distinct
($sql, @bind) = $sqla->select(
-columns => [-DISTINCT => qw/foo bar/],
-from => 'Foo',
);
is_same_sql_bind(
$sql, \@bind,
"SELECT DISTINCT foo, bar FROM Foo", [],
);
# other minus signs
($sql, @bind) = $sqla->select(
-columns => [-DISTINCT => -STRAIGHT_JOIN => qw/foo bar/],
-from => 'Foo',
);
is_same_sql_bind(
$sql, \@bind,
"SELECT DISTINCT STRAIGHT_JOIN foo, bar FROM Foo", [],
);
($sql, @bind) = $sqla->select(
-columns => [-SQL_SMALL_RESULT => qw/foo bar/],
-from => 'Foo',
);
is_same_sql_bind(
$sql, \@bind,
"SELECT SQL_SMALL_RESULT foo, bar FROM Foo", [],
);
($sql, @bind) = $sqla->select(
-columns => ["-/*+ FIRST_ROWS (100) */" => qw/foo bar/],
-from => 'Foo',
);
is_same_sql_bind(
$sql, \@bind,
"SELECT /*+ FIRST_ROWS (100) */ foo, bar FROM Foo", [],
);
# subquery as column, simple example
($sql, @bind) = $sqla->select(
-columns => ["col1", \ [ "(SELECT max(bar) FROM Bar WHERE bar < ?)|col2", 123], "col3"],
-from => 'Foo',
-where => {foo => 456},
);
is_same_sql_bind(
$sql, \@bind,
"SELECT col1, (SELECT max(bar) FROM Bar WHERE bar < ?) AS col2, col3 FROM Foo WHERE foo = ?", [123, 456],
"subquery in select list",
);
# subquery as column, example from the doc
my ($subq_sql, @subq_bind) = $sqla->select(
-columns => 'COUNT(*)',
-from => 'Foo',
-where => {bar_id => {-ident => 'Bar.bar_id'},
height => {-between => [100, 200]}},
);
my $subquery = ["($subq_sql)|col3", @subq_bind];
($sql, @bind) = $sqla->select(
-from => 'Bar',
-columns => ['col1', 'col2', \$subquery, , 'col4'], # reference to an arrayref !
-where => {color => 'green'},
);
is_same_sql_bind(
$sql, \@bind,
"SELECT col1, col2,
(SELECT COUNT(*) FROM Foo WHERE bar_id=Bar.bar_id and height BETWEEN ? AND ?) AS col3,
col4
FROM Bar WHERE color = ?", [100, 200, 'green'],
"subquery, example from the doc");
# subquery in the -from arg
($subq_sql, @subq_bind) = $sqla->select(
-columns => [qw/a b c/],
-from => 'Foo',
-where => {foo => 123},
);
$subquery = ["($subq_sql)|subq", @subq_bind];
($sql, @bind) = $sqla->select(
-from => \$subquery,
-columns => ['subq.*', 'count(*)|nb_a'],
-where => {b => 456},
-group_by => 'a',
);
is_same_sql_bind(
$sql, \@bind,
"SELECT subq.*, count(*) AS nb_a
FROM (SELECT a, b, c FROM Foo WHERE foo = ?) AS subq
WHERE b = ?
GROUP BY a", [123, 456],
"subquery in -from");
# subq example from the synopsis
my $subq1 = [ $sqla->select(-columns => 'f|x', -from => 'Foo',
-union => [-columns => 'b|x',
-from => 'Bar',
-where => {barbar => 123}],
-as => 'Foo_union_Bar',
) ];
my $subq2 = [ $sqla->select(-columns => 'MAX(amount)',
-from => 'Expenses',
-where => {exp_id => {-ident => 'x'}, date => {">" => '01.01.2024'}},
-as => 'max_amount',
) ];
($sql, @bind) = $sqla->select(
-columns => ['x', \$subq2],
-from => \$subq1,
-order_by => 'x',
);
is_same_sql_bind(
$sql, \@bind,
" SELECT x, (SELECT MAX(amount) FROM Expenses WHERE ( date > ? AND exp_id = x)) AS max_amount
FROM (SELECT f AS x FROM Foo UNION SELECT b AS x FROM Bar WHERE barbar = ?) AS Foo_union_Bar
ORDER BY x", ['01.01.2024', 123],
"subqueries in column list and in source");
# subquery with -in
my $subq = [ $sqla->select(-columns => 'x',
-from => 'Bar',
-where => {y => {"<" => 100}}) ];
($sql, @bind) = $sqla->select(
-from => 'Foo',
-where => {x => {-in => \$subq}},
);
is_same_sql_bind(
$sql, \@bind,
"SELECT * FROM Foo WHERE (x IN (SELECT x FROM Bar WHERE ( y < ? )))",
[100],
"select -in => subquery",
);
# -join
($sql, @bind) = $sqla->select(
-from => [-join => qw/Foo fk=pk Bar/]
);
is_same_sql_bind(
$sql, \@bind,
"SELECT * FROM Foo INNER JOIN Bar ON Foo.fk=Bar.pk", [],
"select from join",
);
# -join with bind values
($sql, @bind) = $sqla->select(
-from => [-join => qw/Foo {fk=pk,other='abc'} Bar/]
);
is_same_sql_bind(
$sql, \@bind,
"SELECT * FROM Foo INNER JOIN Bar ON Foo.fk=Bar.pk and Foo.other = ?", ['abc'],
"select from join with bind value",
);
# set operators
($sql, @bind) = $sqla->select(
-columns => [qw/col1 col2/],
-from => 'Foo',
-where => {col1 => 123},
-intersect => [ -columns => [qw/col3 col4/],
-from => 'Bar',
-where => {col3 => 456},
],
);
is_same_sql_bind(
$sql, \@bind,
"SELECT col1, col2 FROM Foo WHERE col1 = ? "
." INTERSECT SELECT col3, col4 FROM Bar WHERE col3 = ?",
[123, 456],
"from q1 intersect q2",
);
($sql, @bind) = $sqla->select(
-columns => [qw/col1 col2/],
-from => 'Foo',
-where => {col1 => 123},
-union_all => [ -where => {col2 => 456},
-union_all => [-columns => [qw/col1 col3/],
-where => {col3 => 789}, ],
],
-order_by => [qw/col1 col2/],
);
is_same_sql_bind(
$sql, \@bind,
"SELECT col1, col2 FROM Foo WHERE col1 = ? "
." UNION ALL SELECT col1, col2 FROM Foo WHERE col2 = ?"
." UNION ALL SELECT col1, col3 FROM Foo WHERE col3 = ?"
." ORDER BY col1, col2",
[123, 456, 789],
"from q1 union_all q2",
);
#-order_by
($sql, @bind) = $sqla->select(
-from => 'Foo',
-order_by => [qw/-foo +bar buz/],
);
is_same_sql_bind(
$sql, \@bind,
"SELECT * FROM Foo ORDER BY foo DESC, bar ASC, buz", [],
);
#-group_by / -having
($sql, @bind) = $sqla->select(
-columns => [qw/foo SUM(bar)|sum_bar/],
-from => 'Foo',
-group_by => [qw/foo/],
-having => {sum_bar => {">" => 10}},
);
is_same_sql_bind(
$sql, \@bind,
"SELECT foo, SUM(bar) AS sum_bar FROM Foo GROUP BY foo HAVING sum_bar > ?", [10],
"group by / having",
);
#-having
($sql, @bind) = $sqla->select(
-columns => [qw/SUM(bar)|sum_bar/],
-from => 'Foo',
-where => { foo => 1 },
-having => {sum_bar => {">" => 10}},
);
is_same_sql_bind(
$sql, \@bind,
"SELECT SUM(bar) AS sum_bar FROM Foo WHERE ( foo = ? ) HAVING ( sum_bar > ? )", [1,10],
"group by / having (2)",
);
#-limit alone
($sql, @bind) = $sqla->select(
-from => 'Foo',
-limit => 100
);
is_same_sql_bind(
$sql, \@bind,
"SELECT * FROM Foo LIMIT ? OFFSET ?", [100, 0],
);
($sql, @bind) = $sqla->select(
-from => 'Foo',
-limit => 0,
);
is_same_sql_bind(
$sql, \@bind,
"SELECT * FROM Foo LIMIT ? OFFSET ?", [0, 0],
"limit 0",
);
#-limit / -offset
($sql, @bind) = $sqla->select(
-from => 'Foo',
-limit => 100,
-offset => 300,
);
is_same_sql_bind(
$sql, \@bind,
"SELECT * FROM Foo LIMIT ? OFFSET ?", [100, 300],
);
#-page_size / page_index
($sql, @bind) = $sqla->select(
-from => 'Foo',
-page_size => 50,
-page_index => 2,
);
is_same_sql_bind(
$sql, \@bind,
"SELECT * FROM Foo LIMIT ? OFFSET ?", [50, 50],
);
# -for
($sql, @bind) = $sqla->select(
-from => 'Foo',
-for => "UPDATE",
);
is_same_sql_bind(
$sql, \@bind,
"SELECT * FROM Foo FOR UPDATE", [],
);
# -want_details
my $details = $sqla->select(
-columns => [ qw/f.col1|c1 b.col2|c2 /],
-from => [-join => qw/Foo|f fk=pk Bar|b /],
-want_details => 1,
);
is_same_sql_bind(
$details->{sql}, $details->{bind},
"SELECT f.col1 AS c1, b.col2 AS c2 FROM Foo AS f INNER JOIN Bar AS b ON f.fk=b.pk", [],
);
is_deeply($details->{aliased_tables}, {f => 'Foo', b => 'Bar'},
"aliased tables");
is_deeply($details->{aliased_columns}, {c1 => 'f.col1', c2 => 'b.col2'},
"aliased columns");
# aliasing, do not conflict with "||" operator
($sql, @bind) = $sqla->select(
-columns => [qw/A||B C||D|cd (E||F||G)|efg true|false|bool/],
-from => 'Foo',
);
is_same_sql_bind(
$sql, \@bind,
"SELECT A||B, C||D AS cd, (E||F||G) AS efg, true|false AS bool FROM Foo", [],
"aliased cols with '|'"
);
($sql, @bind) = $sqla->select(
-columns => [qw/NULL|a1 2|a2 x|a3/],
-from => 'Foo',
);
is_same_sql_bind(
$sql, \@bind,
"SELECT NULL AS a1, 2 AS a2, x AS a3 FROM Foo", [],
"aliased cols with '|', single char on left-hand side"
);
# bind_params with SQL types
($sql, @bind) = $sqla->select(
-from => 'Foo',
-where => {foo => [{dbd_attrs => {ora_type => 'TEST'}}, 123]},
);
is_same_sql_bind(
$sql, \@bind,
"SELECT * FROM Foo WHERE foo = ?",
[ [{dbd_attrs => {ora_type => 'TEST'}}, 123] ],
"SQL type with implicit = operator",
);
($sql, @bind) = $sqla->select(
-from => 'Foo',
-where => {bar => {"<" => [{dbd_attrs => {pg_type => 999}}, 456]}},
);
is_same_sql_bind(
$sql, \@bind,
"SELECT * FROM Foo WHERE bar < ?",
[ [{dbd_attrs => {pg_type => 999}}, 456] ],
"SQL type with explicit operator",
);
($sql, @bind) = $sqla->insert(
-into => 'Foo',
-values => {x => [{dbd_attrs => {pg_type => 999}}, 456]},
);
is_same_sql_bind(
$sql, \@bind,
"INSERT INTO Foo(x) VALUES(?)",
[ [{dbd_attrs => {pg_type => 999}}, 456] ],
"INSERT with SQL type",
);
($sql, @bind) = $sqla->update(
-table => 'Foo',
-set => {x => [{dbd_attrs => {pg_type => 999}}, 456]},
-where => {bar => 'buz'},
);
is_same_sql_bind(
$sql, \@bind,
"UPDATE Foo SET x = ? WHERE bar = ?",
[ [{dbd_attrs => {pg_type => 999}}, 456], 'buz' ],
"UPDATE with SQL type",
);
# should not be interpreted as bind_params with SQL types
($sql, @bind) = $sqla->select(
-from => 'Foo',
-where => {bar => [{"=" => undef}, {"<" => 'foo'}]}
);
is_same_sql_bind(
$sql, \@bind,
"SELECT * FROM Foo WHERE bar IS NULL OR bar < ?",
[ 'foo' ],
"OR arrayref pair which is not a value/type pair",
);
#----------------------------------------------------------------------
# auxiliary methods : test an instance with standard parameters
#----------------------------------------------------------------------
($sql, @bind) = $sqla->column_alias(qw/Foo f/);
is_same_sql_bind(
$$sql, \@bind,
"Foo AS f", [],
"column alias",
);
($sql, @bind) = $sqla->column_alias(qw/Foo/);
is_same_sql_bind(
$sql, \@bind,
"Foo", [],
"column alias without alias",
);
($sql, @bind) = $sqla->table_alias(qw/Foo f/);
is_same_sql_bind(
$sql, \@bind,
"Foo AS f", [],
"table alias",
);
($sql, @bind) = $sqla->limit_offset(123, 456);
is_same_sql_bind(
$sql, \@bind,
"LIMIT ? OFFSET ?", [123, 456],
"limit offset",
);
$join = $sqla->join(qw[Foo|f =>{fk_A=pk_A,fk_B=pk_B} Bar]);
is_same_sql_bind(
$join->{sql}, $join->{bind},
"Foo AS f LEFT OUTER JOIN Bar ON f.fk_A = Bar.pk_A AND f.fk_B = Bar.pk_B", [],
"join syntax",
);
$join = $sqla->join(qw[Foo <=>[A<B,C<D] Bar]);
is_same_sql_bind(
$join->{sql}, $join->{bind},
"Foo INNER JOIN Bar ON Foo.A < Bar.B OR Foo.C < Bar.D", [],
"join syntax with OR",
);
$join = $sqla->join(qw[Foo == Bar]);
is_same_sql_bind(
$join->{sql}, $join->{bind},
"Foo NATURAL JOIN Bar", [],
"natural join",
);
# try most syntactic constructs
$join = $sqla->join(qw[Table1|t1 ab=cd Table2|t2
<=>{ef>gh,ij<kl} Table3
=>{t1.mn=op} Table4]);
is_same_sql_bind(
$join->{sql}, $join->{bind},
"Table1 AS t1 INNER JOIN Table2 AS t2 ON t1.ab=t2.cd
INNER JOIN Table3 ON t2.ef>Table3.gh
AND t2.ij<Table3.kl
LEFT OUTER JOIN Table4 ON t1.mn=Table4.op",
[],
);
# full outer join
$join = $sqla->join(qw[Foo >=<{a=b} Bar]);
is_same_sql_bind(
$join->{sql}, $join->{bind},
"Foo FULL OUTER JOIN Bar ON Foo.a=Bar.b", [],
"full outer join",
);
# explicit tables in join condition
$join = $sqla->join(qw[Table1|t1 t1.ab=t2.cd Table2|t2]);
is_same_sql_bind(
$join->{sql}, $join->{bind},
"Table1 AS t1 INNER JOIN Table2 AS t2 ON t1.ab=t2.cd",
[],
"explicit tables in join condition"
);
my $merged = $sqla->merge_conditions(
{a => 12, b => {">" => 34}},
{b => {"<" => 56}, c => 78},
);
is_deeply($merged,
{a => 12, b => [-and => {">" => 34}, {"<" => 56}], c => 78});
#----------------------------------------------------------------------
# test a customized instance
#----------------------------------------------------------------------
$sqla = SQL::Abstract::More->new(table_alias => '%1$s %2$s',
limit_offset => "LimitXY",
sql_dialect => "MsAccess");
$join = $sqla->join(qw[Foo|f =>{fk_A=pk_A,fk_B=pk_B} Bar]);
is_same_sql_bind(
$join->{sql}, $join->{bind},
"Foo f LEFT OUTER JOIN (Bar) ON f.fk_A = Bar.pk_A AND f.fk_B = Bar.pk_B", [],
);
($sql, @bind) = $sqla->limit_offset(123, 456);
is_same_sql_bind(
$sql, \@bind,
"LIMIT ?, ?", [456, 123]
);
ok($sqla->join_assoc_right,
"join_assoc_right is true");
$sqla = SQL::Abstract::More->new(sql_dialect => 'Oracle');
($sql, @bind) = $sqla->select(
-columns => [qw/col1|c1 col2|c2/],
-from => [-join => qw/Foo|f fk=pk Bar|b/],
);
is_same_sql_bind(
$sql, \@bind,
"SELECT col1 c1, col2 c2 FROM Foo f INNER JOIN Bar b ON f.fk=b.pk",
[]
);
($sql, @bind) = $sqla->select(
-from => 'Foo',
-limit => 10,
-offset => 5,
);
is_same_sql_bind(
$sql, \@bind,
"SELECT * FROM (SELECT subq_A.*, ROWNUM rownum__index FROM (SELECT * FROM Foo) subq_A WHERE ROWNUM <= ?) subq_B WHERE rownum__index >= ?",
[15, 6],
);
# Oracle12c version of limit/offset
$sqla = SQL::Abstract::More->new(sql_dialect => 'Oracle12c');
($sql, @bind) = $sqla->limit_offset(123, 456);
is_same_sql_bind(
$sql, \@bind,
"OFFSET ? ROWS FETCH NEXT ? ROWS ONLY",
[456, 123],
"limit/offset for Oracle12c",
);
#----------------------------------------------------------------------
# method redefinition
#----------------------------------------------------------------------
$sqla = SQL::Abstract::More->new(
limit_offset => sub {
my ($self, $limit, $offset) = @_;
defined $limit or die "NO LIMIT!";
$offset ||= 0;
my $last = $offset + $limit;
return ("ROWS ? TO ?", $offset, $last); # ($sql, @bind)
});
($sql, @bind) = $sqla->limit_offset(123, 456);
is_same_sql_bind(
$sql, \@bind,
"ROWS ? TO ?", [456, 579]
);
#----------------------------------------------------------------------
# max_members_IN
#----------------------------------------------------------------------
$sqla = SQL::Abstract::More->new(
max_members_IN => 10
);
my @vals = (1 .. 35);
($sql, @bind) = $sqla->where({foo => {-in => \@vals}});
is_same_sql_bind(
$sql, \@bind,
' WHERE ( ( foo IN ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) '
. ' OR foo IN ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) '
. ' OR foo IN ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) '
. ' OR foo IN ( ?, ?, ?, ?, ?) ) )',
[1 .. 35]
);
($sql, @bind) = $sqla->where({foo => {-not_in => \@vals}});
is_same_sql_bind(
$sql, \@bind,
' WHERE ( ( foo NOT IN ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) '
. ' AND foo NOT IN ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) '
. ' AND foo NOT IN ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) '
. ' AND foo NOT IN ( ?, ?, ?, ?, ?) ) )',
[1 .. 35]
);
$sqla = SQL::Abstract::More->new(
max_members_IN => 3
);
($sql, @bind) = $sqla->where({foo => {-in => [1 .. 5]},
bar => {-not_in => [6 .. 10]}});
is_same_sql_bind(
$sql, \@bind,
' WHERE ( ( bar NOT IN ( ?, ?, ? ) AND bar NOT IN ( ?, ? ) )'
. ' AND ( foo IN ( ?, ?, ? ) OR foo IN ( ?, ? ) ) )',
[6 .. 10, 1 .. 5]
);
# test old API : passing a plain scalar value to -in
($sql, @bind) = $sqla->where({foo => {-in => 123}});
is_same_sql_bind(
$sql, \@bind,
' WHERE ( foo IN (?) )',
[123],
);
#----------------------------------------------------------------------
# -in with objects
#----------------------------------------------------------------------
my $vals = bless [1, 2], 'Array::PseudoScalar'; # doesn't matter if not loaded
($sql, @bind) = $sqla->where({foo => {-in => $vals},
bar => {-not_in => $vals}});
is_same_sql_bind(
$sql, \@bind,
' WHERE ( bar NOT IN ( ?, ? ) AND foo IN ( ?, ? ) )',
[1, 2, 1, 2],
);
#----------------------------------------------------------------------
# select_implicitly_for
#----------------------------------------------------------------------
my $sqla_RO = SQL::Abstract::More->new(
select_implicitly_for => 'READ ONLY',
);
($sql, @bind) = $sqla_RO->select(-from => 'Foo');
is_same_sql_bind(
$sql, \@bind,
'SELECT * FROM FOO FOR READ ONLY', [],
"select_implicitly_for - basic",
);
($sql, @bind) = $sqla_RO->select(-from => 'Foo', -for => 'UPDATE');
is_same_sql_bind(
$sql, \@bind,
'SELECT * FROM FOO FOR UPDATE', [],
"select_implicitly_for - override",
);
($sql, @bind) = $sqla_RO->select(-from => 'Foo', -for => undef);
is_same_sql_bind(
$sql, \@bind,
'SELECT * FROM FOO', [],
"select_implicitly_for - disable",
);
#----------------------------------------------------------------------
# insert
#----------------------------------------------------------------------
# usual, hashref syntax
($sql, @bind) = $sqla->insert(
-into => 'Foo',
-values => {foo => 1, bar => 2},
);
is_same_sql_bind(
$sql, \@bind,
'INSERT INTO Foo(bar, foo) VALUES (?, ?)',
[2, 1],
"insert - hashref",
);
# arrayref syntax
($sql, @bind) = $sqla->insert(
-into => 'Foo',
-values => [1, 2],
);
is_same_sql_bind(
$sql, \@bind,
'INSERT INTO Foo VALUES (?, ?)',
[1, 2],
"insert - arrayref",
);
# insert .. select
($sql, @bind) = $sqla->insert(
-into => 'Foo',
-columns => [qw/a b/],
-select => {-from => 'Bar', -columns => [qw/x y/]},
);
is_same_sql_bind(
$sql, \@bind,
'INSERT INTO Foo(a, b) SELECT x, y FROM Bar',
[],
"insert .. select",
);
# old API
($sql, @bind) = $sqla->insert('Foo', {foo => 1, bar => 2});
is_same_sql_bind(
$sql, \@bind,
'INSERT INTO Foo(bar, foo) VALUES (?, ?)',
[2, 1],
);
($sql, @bind) = eval {$sqla->insert(-foo => 3); };
ok($@, 'unknown arg to insert()');
# add_sql
($sql, @bind) = $sqla->insert(
-into => 'Foo',
-add_sql => 'IGNORE', # MySQL syntax
-values => {foo => 1, bar => 2},
);
is_same_sql_bind(
$sql, \@bind,
'INSERT IGNORE INTO Foo(bar, foo) VALUES (?, ?)',
[2, 1],
);
($sql, @bind) = $sqla->insert(
-into => 'Foo',
-add_sql => 'OR IGNORE', # SQLite syntax
-values => {foo => 1, bar => 2},
);
is_same_sql_bind(
$sql, \@bind,
'INSERT OR IGNORE INTO Foo(bar, foo) VALUES (?, ?)',
[2, 1],
);
# returning
($sql, @bind) = $sqla->insert(
-into => 'Foo',
-values => {foo => 1, bar => 2},
-returning => 'key',
);
is_same_sql_bind(
$sql, \@bind,
'INSERT INTO Foo(bar, foo) VALUES (?, ?) RETURNING key',
[2, 1],
);
($sql, @bind) = $sqla->insert(
-into => 'Foo',
-values => {foo => 1, bar => 2},
-returning => [qw/k1 k2/],
);
is_same_sql_bind(
$sql, \@bind,
'INSERT INTO Foo(bar, foo) VALUES (?, ?) RETURNING k1, k2',
[2, 1],
);
($sql, @bind) = $sqla->insert(
-into => 'Foo',
-values => {foo => 1, bar => 2},
-returning => {k1 => \my $k1, k2 => \my $k2},
);
is_same_sql_bind(
$sql, \@bind,
'INSERT INTO Foo(bar, foo) VALUES (?, ?) RETURNING k1, k2 INTO ?, ?',
[2, 1, \$k2, \$k1],
);
# bind_params
SKIP: {
eval "use DBD::Mock 1.48; 1"
or skip "DBD::Mock 1.48 does not seem to be installed", N_DBI_MOCK_TESTS;
my $dbh = DBI->connect('DBI:Mock:', '', '', {RaiseError => 1});
my $sth = $dbh->prepare($sql);
$sqla->bind_params($sth, @bind);
my $mock_params = $sth->{mock_params};
is_deeply($sth->{mock_params}, [2, 1, \$k2, \$k1], "bind_param_inout");
# test 3-args form of bind_param
$sth = $dbh->prepare('INSERT INTO Foo(bar, foo) VALUES (?, ?)');
@bind= ([{dbd_attrs => {pg_type => 99}}, 123],
[{dbd_attrs => {ora_type => 88}}, 456]);
$sqla->bind_params($sth, @bind);
is_deeply($sth->{mock_params},
[map {$_->[1]} @bind],
'bind_param($val, \%type) - values');
is_deeply($sth->{mock_param_attrs},
[map {$_->[0]{dbd_attrs}} @bind],
'bind_param($val, \%type) - attrs');
}
#----------------------------------------------------------------------
# update
#----------------------------------------------------------------------
# complete syntax
($sql, @bind) = $sqla->update(
-table => 'Foo',
-set => {foo => 1, bar => 2},
-where => {buz => 3},
);
is_same_sql_bind(
$sql, \@bind,
'UPDATE Foo SET bar = ?, foo = ? WHERE buz = ?',
[2, 1, 3],
);
# without where
($sql, @bind) = $sqla->update(
-table => 'Foo',
-set => {foo => 1, bar => 2},
);
is_same_sql_bind(
$sql, \@bind,
'UPDATE Foo SET bar = ?, foo = ?',
[2, 1],
);
# old API
($sql, @bind) = $sqla->update('Foo', {foo => 1, bar => 2}, {buz => 3});
is_same_sql_bind(
$sql, \@bind,
'UPDATE Foo SET bar = ?, foo = ? WHERE buz = ?',
[2, 1, 3],
);
# MySQL supports -limit and -order_by in updates !
# see http://dev.mysql.com/doc/refman/5.6/en/update.html
($sql, @bind) = $sqla->update(
-table => 'Foo',
-set => {foo => 1, bar => 2},
-where => {buz => 3},
-order_by => 'baz',
-limit => 10,
);
is_same_sql_bind(
$sql, \@bind,
'UPDATE Foo SET bar = ?, foo = ? WHERE buz = ? ORDER BY baz LIMIT ?',
[2, 1, 3, 10],
"update with -order_by/-limit",
);
($sql, @bind) = $sqla->update(
-table => [-join => qw/Foo fk=pk Bar/],
-set => {foo => 1, bar => 2},
);
is_same_sql_bind(
$sql, \@bind,
'UPDATE Foo INNER JOIN Bar ON Foo.fk=Bar.pk SET bar = ?, foo = ?',
[2, 1],
);
# returning
($sql, @bind) = $sqla->update(
-table => 'Foo',
-set => {foo => 1},
-returning => 'key',
);
is_same_sql_bind(
$sql, \@bind,
'UPDATE Foo SET foo = ? RETURNING key',
[1],
'update returning (scalar)',
);
($sql, @bind) = $sqla->update(
-table => 'Foo',
-set => {foo => 1},
-returning => [qw/k1 k2/],
);
is_same_sql_bind(
$sql, \@bind,
'UPDATE Foo SET foo = ? RETURNING k1, k2',
[1],
'update returning (arrayref)',
);
($sql, @bind) = $sqla->update(
-table => 'Foo',
-set => {foo => 1},
-returning => {k1 => \my $kupd1, k2 => \my $kupd2},
);
is_same_sql_bind(
$sql, \@bind,
'UPDATE Foo SET foo = ? RETURNING k1, k2 INTO ?, ?',
[1, \$kupd1, \$kupd2],
'update returning (hashref)',
);
# additional keywords
($sql, @bind) = $sqla->update(
-add_sql => 'IGNORE', # MySQL syntax
-table => 'Foo',
-set => {foo => 1},
);
is_same_sql_bind(
$sql, \@bind,
'UPDATE IGNORE Foo SET foo = ?',
[1],
'update IGNORE',
);
#----------------------------------------------------------------------
# delete
#----------------------------------------------------------------------
# complete syntax
($sql, @bind) = $sqla->delete(
-from => 'Foo',
-where => {buz => 3},
);
is_same_sql_bind(
$sql, \@bind,
'DELETE FROM Foo WHERE buz = ?',
[3],
);
# old API
($sql, @bind) = $sqla->delete('Foo', {buz => 3});
is_same_sql_bind(
$sql, \@bind,
'DELETE FROM Foo WHERE buz = ?',
[3],
);
# MySQL supports -limit and -order_by in deletes !
# see http://dev.mysql.com/doc/refman/5.6/en/delete.html
($sql, @bind) = $sqla->delete(
-from => 'Foo',
-where => {buz => 3},
-order_by => 'baz',
-limit => 10,
);
is_same_sql_bind(
$sql, \@bind,
'DELETE FROM Foo WHERE buz = ? ORDER BY baz LIMIT ?',
[3, 10],
"delete with -order_by/-limit",
);
# additional keywords
($sql, @bind) = $sqla->delete(
-from => 'Foo',
-where => {buz => 3},
-add_sql => 'IGNORE', # MySQL syntax
);
is_same_sql_bind(
$sql, \@bind,
'DELETE IGNORE FROM Foo WHERE buz = ?',
[3],
'delete IGNORE',
);
#----------------------------------------------------------------------
# quote
#----------------------------------------------------------------------
$sqla = SQL::Abstract::More->new({ quote_char => q{"}, name_sep => q{.} });
($sql, @bind) = $sqla->select(
-from => [
-join => qw(
t1|left
id=t1_id
t2|link
=>{t3_id=id}
t3|right
)
],
-columns => [ qw(
left.id|left_id
max("right"."id")|max_right_id
) ]
);
is_same_sql_bind(
$sql, \@bind,
'SELECT "left"."id" AS "left_id", max("right"."id") AS "max_right_id" '
. 'FROM "t1" AS "left" '
. 'INNER JOIN "t2" AS "link" ON ("left"."id" = "link"."t1_id")'
. 'LEFT OUTER JOIN "t3" AS "right" ON ("link"."t3_id" = "right"."id")',
[],
);
#----------------------------------------------------------------------
# THE END
#----------------------------------------------------------------------
done_testing();
|