Commit | Line | Data |
cb551b07 |
1 | use DBIx::Class::Optional::Dependencies -skip_all_without => 'test_rdbms_db2'; |
2 | |
70350518 |
3 | use strict; |
835cdc8d |
4 | use warnings; |
70350518 |
5 | |
6 | use Test::More; |
88d20956 |
7 | use Test::Exception; |
96eacdb7 |
8 | use Try::Tiny; |
70350518 |
9 | use lib qw(t/lib); |
10 | use DBICTest; |
843f8ecd |
11 | |
12 | my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_DB2_${_}" } qw/DSN USER PASS/}; |
3ff5b740 |
13 | my $schema = DBICTest::Schema->connect($dsn, $user, $pass); |
843f8ecd |
14 | |
af1f4f84 |
15 | my $name_sep = $schema->storage->_dbh_get_info('SQL_QUALIFIER_NAME_SEPARATOR'); |
16 | |
3ff5b740 |
17 | my $dbh = $schema->storage->dbh; |
843f8ecd |
18 | |
96eacdb7 |
19 | # test RNO and name_sep detection |
96eacdb7 |
20 | |
21 | is $schema->storage->sql_maker->name_sep, $name_sep, |
22 | 'name_sep detection'; |
23 | |
24 | my $have_rno = try { |
25 | $dbh->selectrow_array( |
26 | "SELECT row_number() OVER (ORDER BY 1) FROM sysibm${name_sep}sysdummy1" |
27 | ); |
28 | 1; |
29 | }; |
30 | |
31 | is $schema->storage->sql_maker->limit_dialect, |
32 | ($have_rno ? 'RowNumberOver' : 'FetchFirst'), |
33 | 'limit_dialect detection'; |
34 | |
88c00130 |
35 | eval { $dbh->do("DROP TABLE artist") }; |
843f8ecd |
36 | |
4e88dfc4 |
37 | $dbh->do("CREATE TABLE artist (artistid INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), name VARCHAR(255), charfield CHAR(10), rank INTEGER DEFAULT 13);"); |
843f8ecd |
38 | |
25aa8311 |
39 | my $ars = $schema->resultset('Artist'); |
88d20956 |
40 | is ( $ars->count, 0, 'No rows at first' ); |
25aa8311 |
41 | |
835cdc8d |
42 | # test primary key handling |
25aa8311 |
43 | my $new = $ars->create({ name => 'foo' }); |
843f8ecd |
44 | ok($new->artistid, "Auto-PK worked"); |
45 | |
835cdc8d |
46 | # test explicit key spec |
88d20956 |
47 | $new = $ars->create ({ name => 'bar', artistid => 66 }); |
48 | is($new->artistid, 66, 'Explicit PK worked'); |
49 | $new->discard_changes; |
50 | is($new->artistid, 66, 'Explicit PK assigned'); |
51 | |
835cdc8d |
52 | # test populate |
88d20956 |
53 | lives_ok (sub { |
54 | my @pop; |
55 | for (1..2) { |
56 | push @pop, { name => "Artist_$_" }; |
57 | } |
58 | $ars->populate (\@pop); |
59 | }); |
60 | |
835cdc8d |
61 | # test populate with explicit key |
88d20956 |
62 | lives_ok (sub { |
63 | my @pop; |
64 | for (1..2) { |
65 | push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ }; |
66 | } |
67 | $ars->populate (\@pop); |
68 | }); |
835cdc8d |
69 | |
70 | # count what we did so far |
88d20956 |
71 | is ($ars->count, 6, 'Simple count works'); |
72 | |
835cdc8d |
73 | # test LIMIT support |
88d20956 |
74 | my $lim = $ars->search( {}, |
25aa8311 |
75 | { |
76 | rows => 3, |
88d20956 |
77 | offset => 4, |
25aa8311 |
78 | order_by => 'artistid' |
79 | } |
843f8ecd |
80 | ); |
835cdc8d |
81 | is( $lim->count, 2, 'ROWS+OFFSET count ok' ); |
88d20956 |
82 | is( $lim->all, 2, 'Number of ->all objects matches count' ); |
25aa8311 |
83 | |
e5372da4 |
84 | # Limit with select-lock |
4ca1fd6f |
85 | { |
e5372da4 |
86 | local $TODO = "Seems we can't SELECT ... FOR ... on subqueries"; |
87 | lives_ok { |
88 | $schema->txn_do (sub { |
89 | isa_ok ( |
90 | $schema->resultset('Artist')->find({artistid => 1}, {for => 'update', rows => 1}), |
91 | 'DBICTest::Schema::Artist', |
92 | ); |
93 | }); |
94 | } 'Limited FOR UPDATE select works'; |
95 | } |
96 | |
835cdc8d |
97 | # test iterator |
88d20956 |
98 | $lim->reset; |
99 | is( $lim->next->artistid, 101, "iterator->next ok" ); |
100 | is( $lim->next->artistid, 102, "iterator->next ok" ); |
101 | is( $lim->next, undef, "next past end of resultset ok" ); |
25aa8311 |
102 | |
96eacdb7 |
103 | # test FetchFirst limit dialect syntax |
104 | { |
105 | local $schema->storage->sql_maker->{limit_dialect} = 'FetchFirst'; |
106 | |
107 | my $lim = $ars->search({}, { |
108 | rows => 3, |
109 | offset => 2, |
110 | order_by => 'artistid', |
111 | }); |
112 | |
113 | is $lim->count, 3, 'fetch first limit count ok'; |
114 | |
115 | is $lim->all, 3, 'fetch first number of ->all objects matches count'; |
116 | |
117 | is $lim->next->artistid, 3, 'iterator->next ok'; |
118 | is $lim->next->artistid, 66, 'iterator->next ok'; |
119 | is $lim->next->artistid, 101, 'iterator->next ok'; |
120 | is $lim->next, undef, 'iterator->next past end of resultset ok'; |
121 | } |
843f8ecd |
122 | |
123 | my $test_type_info = { |
124 | 'artistid' => { |
125 | 'data_type' => 'INTEGER', |
126 | 'is_nullable' => 0, |
fc22fbac |
127 | 'size' => 10 |
843f8ecd |
128 | }, |
129 | 'name' => { |
130 | 'data_type' => 'VARCHAR', |
131 | 'is_nullable' => 1, |
132 | 'size' => 255 |
133 | }, |
134 | 'charfield' => { |
fc22fbac |
135 | 'data_type' => 'CHAR', |
843f8ecd |
136 | 'is_nullable' => 1, |
835cdc8d |
137 | 'size' => 10 |
843f8ecd |
138 | }, |
70c29d32 |
139 | 'rank' => { |
140 | 'data_type' => 'INTEGER', |
141 | 'is_nullable' => 1, |
835cdc8d |
142 | 'size' => 10 |
70c29d32 |
143 | }, |
843f8ecd |
144 | }; |
145 | |
146 | |
3ff5b740 |
147 | my $type_info = $schema->storage->columns_info_for('artist'); |
843f8ecd |
148 | is_deeply($type_info, $test_type_info, 'columns_info_for - column data types'); |
149 | |
88d20956 |
150 | done_testing; |
151 | |
843f8ecd |
152 | # clean up our mess |
3ff5b740 |
153 | END { |
65d35121 |
154 | my $dbh = eval { $schema->storage->_dbh }; |
155 | $dbh->do("DROP TABLE artist") if $dbh; |
156 | undef $schema; |
3ff5b740 |
157 | } |