cleanup test
|
1 |
use Test::More; |
2 |
use strict; |
|
3 |
use warnings; |
|
4 |
use utf8; |
|
5 |
use Encode qw/encode_utf8 decode_utf8/; |
|
test cleanup
|
6 |
use FindBin; |
cleanup test
|
7 |
use lib "$FindBin::Bin/common"; |
cleanup test
|
8 | |
9 |
BEGIN { |
|
10 |
eval { require DBD::SQLite; 1 } |
|
11 |
or plan skip_all => 'DBD::SQLite required'; |
|
12 |
eval { DBD::SQLite->VERSION >= 1.25 } |
|
13 |
or plan skip_all => 'DBD::SQLite >= 1.25 required'; |
|
14 | ||
15 |
plan 'no_plan'; |
|
16 |
use_ok('DBIx::Custom'); |
|
17 |
} |
|
18 | ||
test cleanup
|
19 |
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/}; |
cleanup test
|
20 |
sub test { print "# $_[0]\n" } |
21 | ||
test cleanup
|
22 |
use DBIx::Custom; |
test cleanup
|
23 |
{ |
24 |
package DBIx::Custom; |
|
25 |
has dsn => sub { 'dbi:SQLite:dbname=:memory:' } |
|
26 |
} |
|
test cleanup
|
27 | |
cleanup test
|
28 |
# Constant |
cleanup test
|
29 |
my $create_table1 = 'create table table1 (key1 varchar, key2 varchar);'; |
30 |
my $create_table_reserved = 'create table "table" ("select" varchar, "update" varchar)'; |
|
test cleanup
|
31 |
my $q = '"'; |
32 |
my $p = '"'; |
|
cleanup test
|
33 | |
cleanup test
|
34 |
# Variables |
35 |
my $dbi; |
|
36 |
my $result; |
|
37 |
my $row; |
|
38 |
my $rows; |
|
cleanup test
|
39 |
my $binary; |
cleanup test
|
40 | |
41 |
# Prepare table |
|
test cleanup
|
42 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
43 | |
test cleanup
|
44 |
### SQLite only test |
cleanup test
|
45 |
test 'dbi_option default'; |
46 |
$dbi = DBIx::Custom->new; |
|
47 |
is_deeply($dbi->dbi_option, {}); |
|
48 | ||
49 | ||
test cleanup
|
50 |
test 'prefix'; |
51 |
$dbi = DBIx::Custom->connect; |
|
52 |
eval { $dbi->execute('drop table table1') }; |
|
53 |
$dbi->execute('create table table1 (key1 varchar, key2 varchar, primary key(key1));'); |
|
cleanup
|
54 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
55 |
$dbi->insert({key1 => 1, key2 => 4}, table => 'table1', prefix => 'or replace'); |
|
test cleanup
|
56 |
$result = $dbi->execute('select * from table1;'); |
57 |
$rows = $result->all; |
|
58 |
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic"); |
|
test cleanup
|
59 | |
test cleanup
|
60 |
$dbi = DBIx::Custom->connect; |
61 |
eval { $dbi->execute('drop table table1') }; |
|
62 |
$dbi->execute('create table table1 (key1 varchar, key2 varchar, primary key(key1));'); |
|
cleanup
|
63 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
64 |
$dbi->update({key2 => 4}, table => 'table1', |
|
test cleanup
|
65 |
where => {key1 => 1}, prefix => 'or replace'); |
66 |
$result = $dbi->execute('select * from table1;'); |
|
67 |
$rows = $result->all; |
|
68 |
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic"); |
|
test cleanup
|
69 | |
70 | ||
test cleanup
|
71 |
test 'quote'; |
72 |
$dbi = DBIx::Custom->connect; |
|
73 |
$dbi->quote('"'); |
|
74 |
eval { $dbi->execute("drop table ${q}table$p") }; |
|
75 |
$dbi->execute($create_table_reserved); |
|
76 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
|
cleanup
|
77 |
$dbi->insert({select => 1}, table => 'table'); |
test cleanup
|
78 |
$dbi->delete(table => 'table', where => {select => 1}); |
79 |
$result = $dbi->execute("select * from ${q}table$p"); |
|
80 |
$rows = $result->all; |
|
81 |
is_deeply($rows, [], "reserved word"); |
|
82 | ||
test cleanup
|
83 |
test 'finish statement handle'; |
84 |
$dbi = DBIx::Custom->connect; |
|
85 |
$dbi->execute($create_table1); |
|
86 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
|
87 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
88 | ||
89 |
$result = $dbi->select(table => 'table1'); |
|
90 |
$row = $result->fetch_first; |
|
91 |
is_deeply($row, [1, 2], "row"); |
|
92 |
$row = $result->fetch; |
|
93 |
ok(!$row, "finished"); |
|
94 | ||
95 |
$result = $dbi->select(table => 'table1'); |
|
96 |
$row = $result->fetch_hash_first; |
|
97 |
is_deeply($row, {key1 => 1, key2 => 2}, "row"); |
|
98 |
$row = $result->fetch_hash; |
|
99 |
ok(!$row, "finished"); |
|
100 | ||
101 |
$dbi->execute('create table table2 (key1, key2);'); |
|
102 |
$result = $dbi->select(table => 'table2'); |
|
103 |
$row = $result->fetch_hash_first; |
|
104 |
ok(!$row, "no row fetch"); |
|
105 | ||
106 |
$dbi = DBIx::Custom->connect; |
|
107 |
eval { $dbi->execute('drop table table1') }; |
|
108 |
$dbi->execute($create_table1); |
|
109 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
|
110 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
111 |
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1'); |
|
112 |
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1'); |
|
113 |
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1'); |
|
114 |
$result = $dbi->select(table => 'table1'); |
|
115 |
$rows = $result->fetch_multi(2); |
|
116 |
is_deeply($rows, [[1, 2], |
|
117 |
[3, 4]], "fetch_multi first"); |
|
118 |
$rows = $result->fetch_multi(2); |
|
119 |
is_deeply($rows, [[5, 6], |
|
120 |
[7, 8]], "fetch_multi secound"); |
|
121 |
$rows = $result->fetch_multi(2); |
|
122 |
is_deeply($rows, [[9, 10]], "fetch_multi third"); |
|
123 |
$rows = $result->fetch_multi(2); |
|
124 |
ok(!$rows); |
|
125 | ||
126 |
$result = $dbi->select(table => 'table1'); |
|
127 |
eval {$result->fetch_multi}; |
|
128 |
like($@, qr/Row count must be specified/, "Not specified row count"); |
|
129 | ||
130 |
$result = $dbi->select(table => 'table1'); |
|
131 |
$rows = $result->fetch_hash_multi(2); |
|
132 |
is_deeply($rows, [{key1 => 1, key2 => 2}, |
|
133 |
{key1 => 3, key2 => 4}], "fetch_multi first"); |
|
134 |
$rows = $result->fetch_hash_multi(2); |
|
135 |
is_deeply($rows, [{key1 => 5, key2 => 6}, |
|
136 |
{key1 => 7, key2 => 8}], "fetch_multi secound"); |
|
137 |
$rows = $result->fetch_hash_multi(2); |
|
138 |
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third"); |
|
139 |
$rows = $result->fetch_hash_multi(2); |
|
140 |
ok(!$rows); |
|
141 | ||
142 |
$result = $dbi->select(table => 'table1'); |
|
143 |
eval {$result->fetch_hash_multi}; |
|
144 |
like($@, qr/Row count must be specified/, "Not specified row count"); |
|
145 | ||
test cleanup
|
146 | |
cleanup test
|
147 |
test 'type option'; # DEPRECATED! |
148 |
$dbi = DBIx::Custom->connect( |
|
149 |
data_source => 'dbi:SQLite:dbname=:memory:', |
|
150 |
dbi_option => { |
|
151 |
$DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1) |
|
152 |
} |
|
153 |
); |
|
154 |
$binary = pack("I3", 1, 2, 3); |
|
155 |
eval { $dbi->execute('drop table table1') }; |
|
156 |
$dbi->execute('create table table1(key1, key2)'); |
|
cleanup
|
157 |
$dbi->insert({key1 => $binary, key2 => 'あ'}, table => 'table1', type => [key1 => DBI::SQL_BLOB]); |
cleanup test
|
158 |
$result = $dbi->select(table => 'table1'); |
159 |
$row = $result->one; |
|
160 |
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic"); |
|
161 |
$result = $dbi->execute('select length(key1) as key1_length from table1'); |
|
162 |
$row = $result->one; |
|
163 |
is($row->{key1_length}, length $binary); |
|
164 | ||
micro optimization
|
165 |
test 'bind_type option'; # DEPRECATED! |
166 |
$binary = pack("I3", 1, 2, 3); |
|
167 |
eval { $dbi->execute('drop table table1') }; |
|
168 |
$dbi->execute('create table table1(key1, key2)'); |
|
169 |
$dbi->insert({key1 => $binary, key2 => 'あ'}, table => 'table1', bind_type => [key1 => DBI::SQL_BLOB]); |
|
170 |
$result = $dbi->select(table => 'table1'); |
|
171 |
$row = $result->one; |
|
172 |
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic"); |
|
173 |
$result = $dbi->execute('select length(key1) as key1_length from table1'); |
|
174 |
$row = $result->one; |
|
175 |
is($row->{key1_length}, length $binary); |
|
176 | ||
cleanup test
|
177 |
test 'type_rule from'; |
178 |
$dbi = DBIx::Custom->connect; |
|
179 |
$dbi->type_rule( |
|
180 |
from1 => { |
|
181 |
date => sub { uc $_[0] } |
|
182 |
} |
|
183 |
); |
|
184 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
185 |
$dbi->insert({key1 => 'a'}, table => 'table1'); |
|
186 |
$result = $dbi->select(table => 'table1'); |
|
187 |
is($result->fetch_first->[0], 'A'); |
|
188 | ||
189 |
$result = $dbi->select(table => 'table1'); |
|
190 |
is($result->one->{key1}, 'A'); |
|
191 | ||
added SQL Server test
|
192 |
test 'select limit'; |
193 |
eval { $dbi->execute('drop table table1') }; |
|
194 |
$dbi->execute($create_table1); |
|
cleanup
|
195 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
196 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
added SQL Server test
|
197 |
$rows = $dbi->select(table => 'table1', append => "order by key1 desc limit 1")->all; |
198 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "append statement"); |
|
199 | ||
200 | ||
201 | ||
test cleanup
|
202 |
# DEPRECATED! test |
203 |
test 'filter __ expression'; |
|
204 |
$dbi = DBIx::Custom->connect; |
|
cleanup test
|
205 |
eval { $dbi->execute('drop table table2') }; |
206 |
eval { $dbi->execute('drop table table3') }; |
|
207 |
$dbi->execute('create table table2 (id, name, table3_id)'); |
|
208 |
$dbi->execute('create table table3 (id, name)'); |
|
209 |
$dbi->apply_filter('table3', |
|
test cleanup
|
210 |
name => {in => sub { uc $_[0] } } |
211 |
); |
|
212 | ||
cleanup
|
213 |
$dbi->insert({id => 1, name => 'a', table3_id => 2}, table => 'table2'); |
214 |
$dbi->insert({id => 2, name => 'b'}, table => 'table3'); |
|
test cleanup
|
215 | |
216 |
$result = $dbi->select( |
|
cleanup test
|
217 |
table => ['table2', 'table3'], relation => {'table2.table3_id' => 'table3.id'}, |
218 |
column => ['table3.name as table3__name'] |
|
test cleanup
|
219 |
); |
220 |
is($result->fetch_first->[0], 'B'); |
|
221 | ||
222 |
$result = $dbi->select( |
|
cleanup test
|
223 |
table => 'table2', relation => {'table2.table3_id' => 'table3.id'}, |
224 |
column => ['table3.name as table3__name'] |
|
test cleanup
|
225 |
); |
226 |
is($result->fetch_first->[0], 'B'); |
|
227 | ||
228 |
$result = $dbi->select( |
|
cleanup test
|
229 |
table => 'table2', relation => {'table2.table3_id' => 'table3.id'}, |
230 |
column => ['table3.name as "table3.name"'] |
|
test cleanup
|
231 |
); |
232 |
is($result->fetch_first->[0], 'B'); |
|
test cleanup
|
233 | |
234 |
test 'reserved_word_quote'; |
|
235 |
$dbi = DBIx::Custom->connect; |
|
236 |
eval { $dbi->execute("drop table ${q}table$p") }; |
|
237 |
$dbi->reserved_word_quote('"'); |
|
238 |
$dbi->execute($create_table_reserved); |
|
239 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
|
240 |
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}}); |
|
cleanup
|
241 |
$dbi->insert({select => 1}, table => 'table'); |
242 |
$dbi->update({update => 2}, table => 'table', where => {'table.select' => 1}); |
|
test cleanup
|
243 |
$result = $dbi->execute("select * from ${q}table$p"); |
244 |
$rows = $result->all; |
|
245 |
is_deeply($rows, [{select => 2, update => 6}], "reserved word"); |
|
added SQL Server test
|
246 | |
247 |
test 'limit tag'; |
|
248 |
$dbi = DBIx::Custom->connect; |
|
249 |
eval { $dbi->execute('drop table table1') }; |
|
250 |
$dbi->execute($create_table1); |
|
cleanup
|
251 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
252 |
$dbi->insert({key1 => 1, key2 => 4}, table => 'table1'); |
|
253 |
$dbi->insert({key1 => 1, key2 => 6}, table => 'table1'); |
|
added SQL Server test
|
254 |
$dbi->register_tag( |
255 |
limit => sub { |
|
256 |
my ($count, $offset) = @_; |
|
257 |
|
|
258 |
my $s = ''; |
|
259 |
$s .= "limit $count"; |
|
260 |
$s .= " offset $offset" if defined $offset; |
|
261 |
|
|
262 |
return [$s, []]; |
|
263 |
} |
|
264 |
); |
|
265 |
$rows = $dbi->select( |
|
266 |
table => 'table1', |
|
267 |
where => {key1 => 1}, |
|
268 |
append => "order by key2 {limit 1 0}" |
|
269 |
)->all; |
|
270 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
271 |
$rows = $dbi->select( |
|
272 |
table => 'table1', |
|
273 |
where => {key1 => 1}, |
|
274 |
append => "order by key2 {limit 2 1}" |
|
275 |
)->all; |
|
276 |
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]); |
|
277 |
$rows = $dbi->select( |
|
278 |
table => 'table1', |
|
279 |
where => {key1 => 1}, |
|
280 |
append => "order by key2 {limit 1}" |
|
281 |
)->all; |
|
282 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
improved join clause parsing
|
283 | |
284 |
test 'join function'; |
|
285 |
$dbi = DBIx::Custom->connect; |
|
286 |
eval { $dbi->execute("drop table table1") }; |
|
287 |
eval { $dbi->execute("drop table table2") }; |
|
288 |
$dbi->execute($create_table1); |
|
289 |
$dbi->execute("create table table2 (key1, key3)"); |
|
cleanup
|
290 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
291 |
$dbi->insert({key1 => 1, key3 => 4}, table => 'table2'); |
|
292 |
$dbi->insert({key1 => 1, key3 => 1}, table => 'table2'); |
|
improved join clause parsing
|
293 |
$result = $dbi->select( |
294 |
table => 'table1', |
|
295 |
column => [{table2 => ['key3']}], |
|
296 |
join => [ |
|
- update_param is DEPRECATED...
|
297 |
"left outer join table2 on coalesce(table1.key1, 0) = coalesce(table2.key1, 0) and table2.key3 > '3'" |
improved join clause parsing
|
298 |
] |
299 |
); |
|
- update_param is DEPRECATED...
|
300 |
is_deeply($result->all, [{"table2.key3" => 4}]); |
301 | ||
302 |
$dbi = DBIx::Custom->connect; |
|
303 |
eval { $dbi->execute("drop table table1") }; |
|
304 |
eval { $dbi->execute("drop table table2") }; |
|
305 |
$dbi->execute($create_table1); |
|
306 |
$dbi->execute("create table table2 (key1, key3)"); |
|
cleanup
|
307 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
308 |
$dbi->insert({key1 => 1, key3 => 4}, table => 'table2'); |
|
309 |
$dbi->insert({key1 => 1, key3 => 1}, table => 'table2'); |
|
- update_param is DEPRECATED...
|
310 |
$result = $dbi->select( |
311 |
table => 'table1', |
|
312 |
column => [{table2 => ['key3']}], |
|
313 |
join => [ |
|
314 |
"left outer join table2 on table2.key3 > '3' and coalesce(table1.key1, 0) = coalesce(table2.key1, 0)" |
|
315 |
] |
|
316 |
); |
|
317 |
is_deeply($result->all, [{"table2.key3" => 4}]); |