added common test executing ...
|
1 |
use Test::More; |
2 |
use strict; |
|
3 |
use warnings; |
|
test cleanup
|
4 |
use DBIx::Custom; |
5 |
use Encode qw/encode_utf8/; |
|
cleanup test
|
6 |
use FindBin; |
7 |
use lib "$FindBin::Bin/common"; |
|
8 | ||
added common test executing ...
|
9 | |
10 |
my $dbi; |
|
11 | ||
12 |
plan skip_all => $ENV{DBIX_CUSTOM_SKIP_MESSAGE} || 'common.t is always skipped' |
|
13 |
unless $ENV{DBIX_CUSTOM_TEST_RUN} |
|
14 |
&& eval { $dbi = DBIx::Custom->connect; 1 }; |
|
15 | ||
16 |
plan 'no_plan'; |
|
17 | ||
cleanup test
|
18 |
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/}; |
19 |
sub test { print "# $_[0]\n" } |
|
20 | ||
added common test executing ...
|
21 |
# Constant |
22 |
my $create_table1 = $dbi->create_table1; |
|
cleanup test
|
23 |
my $create_table1_2 = $dbi->create_table1_2; |
test cleanup
|
24 |
my $create_table1_type = $dbi->create_table1_type; |
test cleanup
|
25 |
my $create_table2 = $dbi->create_table2; |
test cleanup
|
26 |
my $create_table2_2 = $dbi->create_table2_2; |
27 |
my $create_table3 = $dbi->create_table3; |
|
test cleanup
|
28 |
my $create_table_reserved = $dbi->create_table_reserved; |
cleanup test
|
29 |
my $q = substr($dbi->quote, 0, 1); |
30 |
my $p = substr($dbi->quote, 1, 1) || $q; |
|
added common test executing ...
|
31 | |
32 |
# Variable |
|
cleanup test
|
33 |
# Variables |
34 |
my $builder; |
|
35 |
my $datas; |
|
36 |
my $sth; |
|
37 |
my $source; |
|
38 |
my @sources; |
|
39 |
my $select_source; |
|
40 |
my $insert_source; |
|
41 |
my $update_source; |
|
42 |
my $param; |
|
43 |
my $params; |
|
44 |
my $sql; |
|
45 |
my $result; |
|
46 |
my $row; |
|
47 |
my @rows; |
|
48 |
my $rows; |
|
49 |
my $query; |
|
50 |
my @queries; |
|
51 |
my $select_query; |
|
52 |
my $insert_query; |
|
53 |
my $update_query; |
|
54 |
my $ret_val; |
|
55 |
my $infos; |
|
added common test executing ...
|
56 |
my $model; |
cleanup test
|
57 |
my $model2; |
58 |
my $where; |
|
59 |
my $update_param; |
|
60 |
my $insert_param; |
|
61 |
my $join; |
|
cleanup test
|
62 |
my $binary; |
added common test executing ...
|
63 | |
64 |
# Drop table |
|
65 |
eval { $dbi->execute('drop table table1') }; |
|
66 | ||
67 |
# Create table |
|
68 |
$dbi->execute($create_table1); |
|
69 |
$model = $dbi->create_model(table => 'table1'); |
|
70 |
$model->insert({key1 => 1, key2 => 2}); |
|
71 |
is_deeply($model->select->all, [{key1 => 1, key2 => 2}]); |
|
72 | ||
cleanup test
|
73 |
test 'DBIx::Custom::Result test'; |
74 |
$dbi->delete_all(table => 'table1'); |
|
75 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
76 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
77 |
$source = "select key1, key2 from table1"; |
|
78 |
$query = $dbi->create_query($source); |
|
79 |
$result = $dbi->execute($query); |
|
80 | ||
81 |
@rows = (); |
|
82 |
while (my $row = $result->fetch) { |
|
83 |
push @rows, [@$row]; |
|
84 |
} |
|
85 |
is_deeply(\@rows, [[1, 2], [3, 4]], "fetch"); |
|
86 | ||
87 |
$result = $dbi->execute($query); |
|
88 |
@rows = (); |
|
89 |
while (my $row = $result->fetch_hash) { |
|
90 |
push @rows, {%$row}; |
|
91 |
} |
|
92 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "fetch_hash"); |
|
93 | ||
94 |
$result = $dbi->execute($query); |
|
95 |
$rows = $result->fetch_all; |
|
96 |
is_deeply($rows, [[1, 2], [3, 4]], "fetch_all"); |
|
97 | ||
98 |
$result = $dbi->execute($query); |
|
99 |
$rows = $result->fetch_hash_all; |
|
100 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "all"); |
|
101 | ||
102 |
test 'Insert query return value'; |
|
103 |
$source = "insert into table1 {insert_param key1 key2}"; |
|
104 |
$query = $dbi->execute($source, {}, query => 1); |
|
105 |
$ret_val = $dbi->execute($query, param => {key1 => 1, key2 => 2}); |
|
106 |
ok($ret_val); |
|
107 | ||
108 |
test 'Direct query'; |
|
109 |
$dbi->delete_all(table => 'table1'); |
|
110 |
$insert_source = "insert into table1 {insert_param key1 key2}"; |
|
111 |
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2}); |
|
112 |
$result = $dbi->execute('select * from table1;'); |
|
113 |
$rows = $result->all; |
|
114 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
115 | ||
116 |
test 'Filter basic'; |
|
117 |
$dbi->delete_all(table => 'table1'); |
|
118 |
$dbi->register_filter(twice => sub { $_[0] * 2}, |
|
119 |
three_times => sub { $_[0] * 3}); |
|
120 | ||
121 |
$insert_source = "insert into table1 {insert_param key1 key2};"; |
|
122 |
$insert_query = $dbi->execute($insert_source, {}, query => 1); |
|
123 |
$insert_query->filter({key1 => 'twice'}); |
|
124 |
$dbi->execute($insert_query, param => {key1 => 1, key2 => 2}); |
|
125 |
$result = $dbi->execute('select * from table1;'); |
|
126 |
$rows = $result->filter({key2 => 'three_times'})->all; |
|
127 |
is_deeply($rows, [{key1 => 2, key2 => 6}], "filter fetch_filter"); |
|
128 | ||
129 |
test 'Filter in'; |
|
130 |
$dbi->delete_all(table => 'table1'); |
|
131 |
$insert_source = "insert into table1 {insert_param key1 key2};"; |
|
132 |
$insert_query = $dbi->execute($insert_source, {}, query => 1); |
|
133 |
$dbi->execute($insert_query, param => {key1 => 2, key2 => 4}); |
|
134 |
$select_source = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}"; |
|
135 |
$select_query = $dbi->execute($select_source,{}, query => 1); |
|
136 |
$select_query->filter({'table1.key1' => 'twice'}); |
|
137 |
$result = $dbi->execute($select_query, param => {'table1.key1' => [1,5], 'table1.key2' => [2,4]}); |
|
138 |
$rows = $result->all; |
|
139 |
is_deeply($rows, [{key1 => 2, key2 => 4}], "filter"); |
|
140 | ||
cleanup test
|
141 |
test 'DBIx::Custom::SQLTemplate basic tag'; |
142 |
$dbi->execute('drop table table1'); |
|
cleanup test
|
143 |
$dbi->execute($create_table1_2); |
cleanup test
|
144 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
145 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
146 | ||
147 |
$source = "select * from table1 where key1 = :key1 and {<> key2} and {< key3} and {> key4} and {>= key5};"; |
|
148 |
$query = $dbi->execute($source, {}, query => 1); |
|
149 |
$result = $dbi->execute($query, param => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5}); |
|
150 |
$rows = $result->all; |
|
151 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1"); |
|
152 | ||
153 |
$source = "select * from table1 where key1 = :key1 and {<> key2} and {< key3} and {> key4} and {>= key5};"; |
|
154 |
$query = $dbi->execute($source, {}, query => 1); |
|
155 |
$result = $dbi->execute($query, {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5}); |
|
156 |
$rows = $result->all; |
|
157 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1"); |
|
158 | ||
159 |
$source = "select * from table1 where {<= key1} and {like key2};"; |
|
160 |
$query = $dbi->execute($source, {}, query => 1); |
|
161 |
$result = $dbi->execute($query, param => {key1 => 1, key2 => '%2%'}); |
|
162 |
$rows = $result->all; |
|
163 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag2"); |
|
164 | ||
165 |
test 'DIB::Custom::SQLTemplate in tag'; |
|
166 |
$dbi->execute('drop table table1'); |
|
cleanup test
|
167 |
$dbi->execute($create_table1_2); |
cleanup test
|
168 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
169 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
170 | ||
171 |
$source = "select * from table1 where {in key1 2};"; |
|
172 |
$query = $dbi->execute($source, {}, query => 1); |
|
173 |
$result = $dbi->execute($query, param => {key1 => [9, 1]}); |
|
174 |
$rows = $result->all; |
|
175 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic"); |
|
176 | ||
177 |
test 'DBIx::Custom::SQLTemplate insert tag'; |
|
178 |
$dbi->delete_all(table => 'table1'); |
|
179 |
$insert_source = 'insert into table1 {insert_param key1 key2 key3 key4 key5}'; |
|
180 |
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
181 | ||
182 |
$result = $dbi->execute('select * from table1;'); |
|
183 |
$rows = $result->all; |
|
184 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic"); |
|
185 | ||
186 |
test 'DBIx::Custom::SQLTemplate update tag'; |
|
187 |
$dbi->delete_all(table => 'table1'); |
|
188 |
$insert_source = "insert into table1 {insert_param key1 key2 key3 key4 key5}"; |
|
189 |
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
190 |
$dbi->execute($insert_source, param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
191 | ||
192 |
$update_source = 'update table1 {update_param key1 key2 key3 key4} where {= key5}'; |
|
193 |
$dbi->execute($update_source, param => {key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}); |
|
194 | ||
195 |
$result = $dbi->execute('select * from table1 order by key1;'); |
|
196 |
$rows = $result->all; |
|
197 |
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}, |
|
198 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "basic"); |
|
199 | ||
cleanup test
|
200 |
test 'Named placeholder'; |
201 |
$dbi->execute('drop table table1'); |
|
202 |
$dbi->execute($create_table1_2); |
|
203 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
204 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
205 | ||
206 |
$source = "select * from table1 where key1 = :key1 and key2 = :key2"; |
|
207 |
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2}); |
|
208 |
$rows = $result->all; |
|
209 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]); |
|
210 | ||
211 |
$source = "select * from table1 where key1 = \n:key1\n and key2 = :key2"; |
|
212 |
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2}); |
|
213 |
$rows = $result->all; |
|
214 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]); |
|
215 | ||
216 |
$source = "select * from table1 where key1 = :key1 or key1 = :key1"; |
|
217 |
$result = $dbi->execute($source, param => {key1 => [1, 2]}); |
|
218 |
$rows = $result->all; |
|
219 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]); |
|
220 | ||
221 |
$source = "select * from table1 where key1 = :table1.key1 and key2 = :table1.key2"; |
|
222 |
$result = $dbi->execute( |
|
223 |
$source, |
|
224 |
param => {'table1.key1' => 1, 'table1.key2' => 1}, |
|
225 |
filter => {'table1.key2' => sub { $_[0] * 2 }} |
|
226 |
); |
|
227 |
$rows = $result->all; |
|
228 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]); |
|
229 | ||
230 |
$dbi->execute('drop table table1'); |
|
231 |
$dbi->execute($create_table1); |
|
232 |
$dbi->insert(table => 'table1', param => {key1 => '2011-10-14 12:19:18', key2 => 2}); |
|
233 |
$source = "select * from table1 where key1 = '2011-10-14 12:19:18' and key2 = :key2"; |
|
234 |
$result = $dbi->execute( |
|
235 |
$source, |
|
236 |
param => {'key2' => 2}, |
|
237 |
); |
|
238 | ||
239 |
$rows = $result->all; |
|
240 |
is_deeply($rows, [{key1 => '2011-10-14 12:19:18', key2 => 2}]); |
|
241 | ||
242 |
$dbi->delete_all(table => 'table1'); |
|
243 |
$dbi->insert(table => 'table1', param => {key1 => 'a:b c:d', key2 => 2}); |
|
244 |
$source = "select * from table1 where key1 = 'a\\:b c\\:d' and key2 = :key2"; |
|
245 |
$result = $dbi->execute( |
|
246 |
$source, |
|
247 |
param => {'key2' => 2}, |
|
248 |
); |
|
249 |
$rows = $result->all; |
|
250 |
is_deeply($rows, [{key1 => 'a:b c:d', key2 => 2}]); |
|
251 | ||
test cleanup
|
252 |
test 'Error case'; |
253 |
eval {DBIx::Custom->connect(dsn => 'dbi:SQLit')}; |
|
254 |
ok($@, "connect error"); |
|
255 | ||
256 |
eval{$dbi->execute("{p }", {}, query => 1)}; |
|
257 |
ok($@, "create_query invalid SQL template"); |
|
added EXPERIMENTAL DBIx::Cus...
|
258 | |
cleanup test
|
259 |
test 'insert'; |
cleanup test
|
260 |
eval { $dbi->execute('drop table table1') }; |
261 |
$dbi->execute($create_table1); |
|
cleanup test
|
262 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
263 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
264 |
$result = $dbi->execute('select * from table1;'); |
|
265 |
$rows = $result->all; |
|
266 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic"); |
|
267 | ||
268 |
$dbi->execute('delete from table1'); |
|
269 |
$dbi->register_filter( |
|
270 |
twice => sub { $_[0] * 2 }, |
|
271 |
three_times => sub { $_[0] * 3 } |
|
272 |
); |
|
273 |
$dbi->default_bind_filter('twice'); |
|
274 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => 'three_times'}); |
|
275 |
$result = $dbi->execute('select * from table1;'); |
|
276 |
$rows = $result->all; |
|
277 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter"); |
|
278 |
$dbi->default_bind_filter(undef); |
|
279 | ||
cleanup test
|
280 |
$dbi->execute('drop table table1'); |
281 |
$dbi->execute($create_table1); |
|
cleanup test
|
282 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, append => ' '); |
283 |
$rows = $dbi->select(table => 'table1')->all; |
|
284 |
is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append'); |
|
285 | ||
286 |
eval{$dbi->insert(table => 'table1', noexist => 1)}; |
|
287 |
like($@, qr/noexist/, "invalid"); |
|
288 | ||
289 |
eval{$dbi->insert(table => 'table', param => {';' => 1})}; |
|
290 |
like($@, qr/safety/); |
|
291 | ||
cleanup test
|
292 |
eval { $dbi->execute("drop table ${q}table$p") }; |
test cleanup
|
293 |
$dbi->execute($create_table_reserved); |
cleanup test
|
294 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
295 |
$dbi->insert(table => 'table', param => {select => 1}); |
|
cleanup test
|
296 |
$result = $dbi->execute("select * from ${q}table$p"); |
cleanup test
|
297 |
$rows = $result->all; |
test cleanup
|
298 |
is_deeply($rows, [{select => 2, update => undef}], "reserved word"); |
cleanup test
|
299 | |
cleanup test
|
300 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
301 |
$dbi->execute($create_table1); |
302 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
|
303 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
304 |
$result = $dbi->execute('select * from table1;'); |
|
305 |
$rows = $result->all; |
|
306 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic"); |
|
307 | ||
cleanup test
|
308 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
309 |
$dbi->execute($create_table1); |
310 |
$dbi->insert(table => 'table1', param => {key1 => \"'1'", key2 => 2}); |
|
311 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
312 |
$result = $dbi->execute('select * from table1;'); |
|
313 |
$rows = $result->all; |
|
314 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic"); |
|
315 | ||
test cleanup
|
316 |
test 'update'; |
317 |
eval { $dbi->execute('drop table table1') }; |
|
318 |
$dbi->execute($create_table1_2); |
|
319 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
320 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
321 |
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}); |
|
test cleanup
|
322 |
$result = $dbi->execute('select * from table1 order by key1;'); |
test cleanup
|
323 |
$rows = $result->all; |
324 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
325 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
326 |
"basic"); |
|
327 |
|
|
328 |
$dbi->execute("delete from table1"); |
|
329 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
330 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
331 |
$dbi->update(table => 'table1', param => {key2 => 12}, where => {key2 => 2, key3 => 3}); |
|
test cleanup
|
332 |
$result = $dbi->execute('select * from table1 order by key1;'); |
test cleanup
|
333 |
$rows = $result->all; |
334 |
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5}, |
|
335 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
336 |
"update key same as search key"); |
|
337 | ||
338 |
$dbi->update(table => 'table1', param => {key2 => [12]}, where => {key2 => 2, key3 => 3}); |
|
test cleanup
|
339 |
$result = $dbi->execute('select * from table1 order by key1;'); |
test cleanup
|
340 |
$rows = $result->all; |
341 |
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5}, |
|
342 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
343 |
"update key same as search key : param is array ref"); |
|
344 | ||
345 |
$dbi->execute("delete from table1"); |
|
346 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
347 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
348 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
349 |
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}, |
|
350 |
filter => {key2 => sub { $_[0] * 2 }}); |
|
test cleanup
|
351 |
$result = $dbi->execute('select * from table1 order by key1;'); |
test cleanup
|
352 |
$rows = $result->all; |
353 |
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5}, |
|
354 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
355 |
"filter"); |
|
356 | ||
357 |
$result = $dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}, append => ' '); |
|
358 | ||
359 |
eval{$dbi->update(table => 'table1', where => {key1 => 1}, noexist => 1)}; |
|
360 |
like($@, qr/noexist/, "invalid"); |
|
361 | ||
362 |
eval{$dbi->update(table => 'table1')}; |
|
363 |
like($@, qr/where/, "not contain where"); |
|
364 | ||
365 |
eval { $dbi->execute('drop table table1') }; |
|
366 |
$dbi->execute($create_table1); |
|
367 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
368 |
$where = $dbi->where; |
|
369 |
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']); |
|
370 |
$where->param({key1 => 1, key2 => 2}); |
|
371 |
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where); |
|
372 |
$result = $dbi->select(table => 'table1'); |
|
373 |
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where'); |
|
374 | ||
375 |
eval { $dbi->execute('drop table table1') }; |
|
376 |
$dbi->execute($create_table1); |
|
377 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
378 |
$dbi->update( |
|
379 |
table => 'table1', |
|
380 |
param => {key1 => 3}, |
|
381 |
where => [ |
|
382 |
['and', 'key1 = :key1', 'key2 = :key2'], |
|
383 |
{key1 => 1, key2 => 2} |
|
384 |
] |
|
385 |
); |
|
386 |
$result = $dbi->select(table => 'table1'); |
|
387 |
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where'); |
|
388 | ||
389 |
eval { $dbi->execute('drop table table1') }; |
|
390 |
$dbi->execute($create_table1); |
|
391 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
392 |
$where = $dbi->where; |
|
393 |
$where->clause(['and', 'key2 = :key2']); |
|
394 |
$where->param({key2 => 2}); |
|
395 |
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where); |
|
396 |
$result = $dbi->select(table => 'table1'); |
|
397 |
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where'); |
|
398 | ||
399 |
eval{$dbi->update(table => 'table1', param => {';' => 1})}; |
|
400 |
like($@, qr/safety/); |
|
401 | ||
402 |
eval{$dbi->update(table => 'table1', param => {'key1' => 1}, where => {';' => 1})}; |
|
403 |
like($@, qr/safety/); |
|
404 | ||
405 |
eval { $dbi->execute('drop table table1') }; |
|
406 |
eval { $dbi->execute("drop table ${q}table$p") }; |
|
test cleanup
|
407 |
$dbi->execute($create_table_reserved); |
test cleanup
|
408 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
409 |
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}}); |
|
410 |
$dbi->insert(table => 'table', param => {select => 1}); |
|
411 |
$dbi->update(table => 'table', where => {select => 1}, param => {update => 2}); |
|
412 |
$result = $dbi->execute("select * from ${q}table$p"); |
|
413 |
$rows = $result->all; |
|
414 |
is_deeply($rows, [{select => 2, update => 6}], "reserved word"); |
|
415 | ||
416 |
eval {$dbi->update_all(table => 'table', param => {';' => 2}) }; |
|
417 |
like($@, qr/safety/); |
|
418 | ||
419 |
eval { $dbi->execute("drop table ${q}table$p") }; |
|
test cleanup
|
420 |
$dbi->execute($create_table_reserved); |
test cleanup
|
421 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
422 |
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}}); |
|
423 |
$dbi->insert(table => 'table', param => {select => 1}); |
|
424 |
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2}); |
|
425 |
$result = $dbi->execute("select * from ${q}table$p"); |
|
426 |
$rows = $result->all; |
|
427 |
is_deeply($rows, [{select => 2, update => 6}], "reserved word"); |
|
428 | ||
429 |
eval { $dbi->execute('drop table table1') }; |
|
430 |
$dbi->execute($create_table1_2); |
|
431 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
432 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
433 |
$dbi->update({key2 => 11}, table => 'table1', where => {key1 => 1}); |
|
test cleanup
|
434 |
$result = $dbi->execute('select * from table1 order by key1;'); |
test cleanup
|
435 |
$rows = $result->all; |
436 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
437 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
438 |
"basic"); |
|
439 | ||
440 |
eval { $dbi->execute('drop table table1') }; |
|
441 |
$dbi->execute($create_table1_2); |
|
442 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
443 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
444 |
$dbi->update(table => 'table1', param => {key2 => \"'11'"}, where => {key1 => 1}); |
|
test cleanup
|
445 |
$result = $dbi->execute('select * from table1 order by key1;'); |
test cleanup
|
446 |
$rows = $result->all; |
447 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
448 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
449 |
"basic"); |
|
450 | ||
451 |
test 'update_all'; |
|
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->register_filter(twice => sub { $_[0] * 2 }); |
|
457 |
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'}); |
|
458 |
$result = $dbi->execute('select * from table1;'); |
|
459 |
$rows = $result->all; |
|
460 |
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5}, |
|
461 |
{key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}], |
|
462 |
"filter"); |
|
463 | ||
464 | ||
465 |
test 'delete'; |
|
466 |
eval { $dbi->execute('drop table table1') }; |
|
467 |
$dbi->execute($create_table1); |
|
468 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
469 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
470 |
$dbi->delete(table => 'table1', where => {key1 => 1}); |
|
471 |
$result = $dbi->execute('select * from table1;'); |
|
472 |
$rows = $result->all; |
|
473 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "basic"); |
|
474 | ||
475 |
$dbi->execute("delete from table1;"); |
|
476 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
477 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
478 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
479 |
$dbi->delete(table => 'table1', where => {key2 => 1}, filter => {key2 => 'twice'}); |
|
480 |
$result = $dbi->execute('select * from table1;'); |
|
481 |
$rows = $result->all; |
|
482 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter"); |
|
483 | ||
484 |
$dbi->delete(table => 'table1', where => {key1 => 1}, append => ' '); |
|
485 | ||
486 |
$dbi->delete_all(table => 'table1'); |
|
487 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
488 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
489 |
$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2}); |
|
490 |
$rows = $dbi->select(table => 'table1')->all; |
|
491 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "delete multi key"); |
|
492 | ||
493 |
eval{$dbi->delete(table => 'table1', where => {key1 => 1}, noexist => 1)}; |
|
494 |
like($@, qr/noexist/, "invalid"); |
|
495 | ||
496 |
eval { $dbi->execute('drop table table1') }; |
|
497 |
$dbi->execute($create_table1); |
|
498 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
499 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
500 |
$where = $dbi->where; |
|
501 |
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']); |
|
502 |
$where->param({ke1 => 1, key2 => 2}); |
|
503 |
$dbi->delete(table => 'table1', where => $where); |
|
504 |
$result = $dbi->select(table => 'table1'); |
|
505 |
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where'); |
|
506 | ||
507 |
eval { $dbi->execute('drop table table1') }; |
|
508 |
$dbi->execute($create_table1); |
|
509 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
510 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
511 |
$dbi->delete( |
|
512 |
table => 'table1', |
|
513 |
where => [ |
|
514 |
['and', 'key1 = :key1', 'key2 = :key2'], |
|
515 |
{ke1 => 1, key2 => 2} |
|
516 |
] |
|
517 |
); |
|
518 |
$result = $dbi->select(table => 'table1'); |
|
519 |
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where'); |
|
520 | ||
521 |
eval { $dbi->execute('drop table table1') }; |
|
test cleanup
|
522 |
$dbi->execute($create_table1); |
test cleanup
|
523 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
524 |
$dbi->delete(table => 'table1', where => {key1 => 1}, prefix => ' '); |
|
525 |
$result = $dbi->execute('select * from table1;'); |
|
526 |
$rows = $result->all; |
|
527 |
is_deeply($rows, [], "basic"); |
|
528 | ||
529 |
test 'delete error'; |
|
530 |
eval { $dbi->execute('drop table table1') }; |
|
531 |
$dbi->execute($create_table1); |
|
532 |
eval{$dbi->delete(table => 'table1')}; |
|
533 |
like($@, qr/"where" must be specified/, |
|
534 |
"where key-value pairs not specified"); |
|
535 | ||
536 |
eval{$dbi->delete(table => 'table1', where => {';' => 1})}; |
|
537 |
like($@, qr/safety/); |
|
538 | ||
539 |
$dbi = DBIx::Custom->connect; |
|
540 |
eval { $dbi->execute("drop table ${q}table$p") }; |
|
test cleanup
|
541 |
$dbi->execute($create_table_reserved); |
test cleanup
|
542 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
543 |
$dbi->insert(table => 'table', param => {select => 1}); |
|
544 |
$dbi->delete(table => 'table', where => {select => 1}); |
|
545 |
$result = $dbi->execute("select * from ${q}table$p"); |
|
546 |
$rows = $result->all; |
|
547 |
is_deeply($rows, [], "reserved word"); |
|
548 | ||
549 |
test 'delete_all'; |
|
550 |
eval { $dbi->execute('drop table table1') }; |
|
551 |
$dbi->execute($create_table1); |
|
552 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
553 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
554 |
$dbi->delete_all(table => 'table1'); |
|
555 |
$result = $dbi->execute('select * from table1;'); |
|
556 |
$rows = $result->all; |
|
557 |
is_deeply($rows, [], "basic"); |
|
558 | ||
559 | ||
560 |
test 'select'; |
|
561 |
eval { $dbi->execute('drop table table1') }; |
|
562 |
$dbi->execute($create_table1); |
|
563 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
564 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
565 |
$rows = $dbi->select(table => 'table1')->all; |
|
566 |
is_deeply($rows, [{key1 => 1, key2 => 2}, |
|
567 |
{key1 => 3, key2 => 4}], "table"); |
|
568 | ||
569 |
$rows = $dbi->select(table => 'table1', column => ['key1'])->all; |
|
570 |
is_deeply($rows, [{key1 => 1}, {key1 => 3}], "table and columns and where key"); |
|
571 | ||
572 |
$rows = $dbi->select(table => 'table1', where => {key1 => 1})->all; |
|
573 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "table and columns and where key"); |
|
574 | ||
575 |
$rows = $dbi->select(table => 'table1', column => ['key1'], where => {key1 => 3})->all; |
|
576 |
is_deeply($rows, [{key1 => 3}], "table and columns and where key"); |
|
577 | ||
578 |
$rows = $dbi->select(table => 'table1', append => "order by key1 desc limit 1")->all; |
|
579 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "append statement"); |
|
580 | ||
581 |
$dbi->register_filter(decrement => sub { $_[0] - 1 }); |
|
582 |
$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'}) |
|
583 |
->all; |
|
584 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "filter"); |
|
585 | ||
test cleanup
|
586 |
eval { $dbi->execute("drop table table2") }; |
587 |
$dbi->execute($create_table2); |
|
test cleanup
|
588 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
589 |
$rows = $dbi->select( |
|
590 |
table => [qw/table1 table2/], |
|
591 |
column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3', |
|
592 |
where => {'table1.key2' => 2}, |
|
593 |
relation => {'table1.key1' => 'table2.key1'} |
|
594 |
)->all; |
|
595 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : exists where"); |
|
596 | ||
597 |
$rows = $dbi->select( |
|
598 |
table => [qw/table1 table2/], |
|
599 |
column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'], |
|
600 |
relation => {'table1.key1' => 'table2.key1'} |
|
601 |
)->all; |
|
602 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : no exists where"); |
|
603 | ||
604 |
eval{$dbi->select(table => 'table1', noexist => 1)}; |
|
605 |
like($@, qr/noexist/, "invalid"); |
|
606 | ||
607 |
$dbi = DBIx::Custom->connect; |
|
test cleanup
|
608 |
eval { $dbi->execute("drop table ${q}table$p") }; |
609 |
$dbi->execute($create_table_reserved); |
|
test cleanup
|
610 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
611 |
$dbi->insert(table => 'table', param => {select => 1, update => 2}); |
|
612 |
$result = $dbi->select(table => 'table', where => {select => 1}); |
|
613 |
$rows = $result->all; |
|
614 |
is_deeply($rows, [{select => 2, update => 2}], "reserved word"); |
|
615 | ||
616 |
test 'fetch filter'; |
|
617 |
eval { $dbi->execute('drop table table1') }; |
|
618 |
$dbi->register_filter( |
|
619 |
twice => sub { $_[0] * 2 }, |
|
620 |
three_times => sub { $_[0] * 3 } |
|
621 |
); |
|
622 |
$dbi->default_fetch_filter('twice'); |
|
623 |
$dbi->execute($create_table1); |
|
624 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
625 |
$result = $dbi->select(table => 'table1'); |
|
626 |
$result->filter({key1 => 'three_times'}); |
|
627 |
$row = $result->one; |
|
628 |
is_deeply($row, {key1 => 3, key2 => 4}, "default_fetch_filter and filter"); |
|
629 | ||
630 |
test 'filters'; |
|
631 |
$dbi = DBIx::Custom->new; |
|
632 | ||
633 |
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')), |
|
634 |
'あ', "decode_utf8"); |
|
635 | ||
636 |
is($dbi->filters->{encode_utf8}->('あ'), |
|
637 |
encode_utf8('あ'), "encode_utf8"); |
|
638 | ||
639 |
test 'transaction'; |
|
640 |
$dbi = DBIx::Custom->connect; |
|
641 |
eval { $dbi->execute('drop table table1') }; |
|
642 |
$dbi->execute($create_table1); |
|
643 |
$dbi->dbh->begin_work; |
|
644 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
645 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
646 |
$dbi->dbh->commit; |
|
647 |
$result = $dbi->select(table => 'table1'); |
|
648 |
is_deeply(scalar $result->all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}], |
|
649 |
"commit"); |
|
650 | ||
651 |
$dbi = DBIx::Custom->connect; |
|
652 |
eval { $dbi->execute('drop table table1') }; |
|
653 |
$dbi->execute($create_table1); |
|
654 |
$dbi->dbh->begin_work(0); |
|
655 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
656 |
$dbi->dbh->rollback; |
|
657 | ||
658 |
$result = $dbi->select(table => 'table1'); |
|
659 |
ok(! $result->fetch_first, "rollback"); |
|
660 | ||
661 |
test 'cache'; |
|
662 |
eval { $dbi->execute('drop table table1') }; |
|
663 |
$dbi->cache(1); |
|
664 |
$dbi->execute($create_table1); |
|
665 |
$source = 'select * from table1 where key1 = :key1 and key2 = :key2;'; |
|
666 |
$dbi->execute($source, {}, query => 1); |
|
667 |
is_deeply($dbi->{_cached}->{$source}, |
|
668 |
{sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2'], tables => []}, "cache"); |
|
669 | ||
670 |
eval { $dbi->execute('drop table table1') }; |
|
671 |
$dbi->execute($create_table1); |
|
672 |
$dbi->{_cached} = {}; |
|
673 |
$dbi->cache(0); |
|
674 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
675 |
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache'); |
|
676 | ||
677 |
test 'execute'; |
|
678 |
eval { $dbi->execute('drop table table1') }; |
|
679 |
$dbi->execute($create_table1); |
|
680 |
{ |
|
681 |
local $Carp::Verbose = 0; |
|
682 |
eval{$dbi->execute('select * frm table1')}; |
|
683 |
like($@, qr/\Qselect * frm table1;/, "fail prepare"); |
|
684 |
like($@, qr/\.t /, "fail : not verbose"); |
|
685 |
} |
|
686 |
{ |
|
687 |
local $Carp::Verbose = 1; |
|
688 |
eval{$dbi->execute('select * frm table1')}; |
|
689 |
like($@, qr/Custom.*\.t /s, "fail : verbose"); |
|
690 |
} |
|
691 | ||
692 |
eval{$dbi->execute('select * from table1', no_exists => 1)}; |
|
693 |
like($@, qr/wrong/, "invald SQL"); |
|
694 | ||
695 |
$query = $dbi->execute('select * from table1 where key1 = :key1', {}, query => 1); |
|
696 |
$dbi->dbh->disconnect; |
|
697 |
eval{$dbi->execute($query, param => {key1 => {a => 1}})}; |
|
698 |
ok($@, "execute fail"); |
|
699 | ||
700 |
{ |
|
701 |
local $Carp::Verbose = 0; |
|
702 |
eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)}; |
|
703 |
like($@, qr/\Q.t /, "caller spec : not vebose"); |
|
704 |
} |
|
705 |
{ |
|
706 |
local $Carp::Verbose = 1; |
|
707 |
eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)}; |
|
708 |
like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose"); |
|
709 |
} |
|
710 | ||
711 | ||
712 |
test 'transaction'; |
|
713 |
$dbi = DBIx::Custom->connect; |
|
714 |
eval { $dbi->execute('drop table table1') }; |
|
715 |
$dbi->execute($create_table1); |
|
716 | ||
717 |
$dbi->begin_work; |
|
718 | ||
719 |
eval { |
|
720 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
721 |
die "Error"; |
|
722 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
723 |
}; |
|
724 | ||
725 |
$dbi->rollback if $@; |
|
726 | ||
727 |
$result = $dbi->select(table => 'table1'); |
|
728 |
$rows = $result->all; |
|
729 |
is_deeply($rows, [], "rollback"); |
|
730 | ||
731 |
$dbi->begin_work; |
|
732 | ||
733 |
eval { |
|
734 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
735 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
736 |
}; |
|
737 | ||
738 |
$dbi->commit unless $@; |
|
739 | ||
740 |
$result = $dbi->select(table => 'table1'); |
|
741 |
$rows = $result->all; |
|
742 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "commit"); |
|
743 | ||
744 |
$dbi->dbh->{AutoCommit} = 0; |
|
745 |
eval{ $dbi->begin_work }; |
|
746 |
ok($@, "exception"); |
|
747 |
$dbi->dbh->{AutoCommit} = 1; |
|
added EXPERIMENTAL DBIx::Cus...
|
748 | |
test cleanup
|
749 |
test 'cache'; |
750 |
eval { $dbi->execute('drop table table1') }; |
|
751 |
$dbi->cache(1); |
|
752 |
$dbi->execute($create_table1); |
|
753 |
$source = 'select * from table1 where key1 = :key1 and key2 = :key2;'; |
|
754 |
$dbi->execute($source, {}, query => 1); |
|
755 |
is_deeply($dbi->{_cached}->{$source}, |
|
756 |
{sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2'], tables => []}, "cache"); |
|
757 | ||
758 |
eval { $dbi->execute('drop table table1') }; |
|
759 |
$dbi->execute($create_table1); |
|
760 |
$dbi->{_cached} = {}; |
|
761 |
$dbi->cache(0); |
|
762 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
763 |
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache'); |
|
764 | ||
765 |
test 'execute'; |
|
766 |
eval { $dbi->execute('drop table table1') }; |
|
767 |
$dbi->execute($create_table1); |
|
768 |
{ |
|
769 |
local $Carp::Verbose = 0; |
|
770 |
eval{$dbi->execute('select * frm table1')}; |
|
771 |
like($@, qr/\Qselect * frm table1;/, "fail prepare"); |
|
772 |
like($@, qr/\.t /, "fail : not verbose"); |
|
773 |
} |
|
774 |
{ |
|
775 |
local $Carp::Verbose = 1; |
|
776 |
eval{$dbi->execute('select * frm table1')}; |
|
777 |
like($@, qr/Custom.*\.t /s, "fail : verbose"); |
|
778 |
} |
|
779 | ||
780 |
eval{$dbi->execute('select * from table1', no_exists => 1)}; |
|
781 |
like($@, qr/wrong/, "invald SQL"); |
|
782 | ||
783 |
$query = $dbi->execute('select * from table1 where key1 = :key1', {}, query => 1); |
|
784 |
$dbi->dbh->disconnect; |
|
785 |
eval{$dbi->execute($query, param => {key1 => {a => 1}})}; |
|
786 |
ok($@, "execute fail"); |
|
787 | ||
788 |
{ |
|
789 |
local $Carp::Verbose = 0; |
|
790 |
eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)}; |
|
791 |
like($@, qr/\Q.t /, "caller spec : not vebose"); |
|
792 |
} |
|
793 |
{ |
|
794 |
local $Carp::Verbose = 1; |
|
795 |
eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)}; |
|
796 |
like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose"); |
|
797 |
} |
|
798 | ||
799 | ||
800 |
test 'transaction'; |
|
801 |
$dbi = DBIx::Custom->connect; |
|
802 |
eval { $dbi->execute('drop table table1') }; |
|
803 |
$dbi->execute($create_table1); |
|
804 | ||
805 |
$dbi->begin_work; |
|
806 | ||
807 |
eval { |
|
808 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
809 |
die "Error"; |
|
810 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
811 |
}; |
|
812 | ||
813 |
$dbi->rollback if $@; |
|
814 | ||
815 |
$result = $dbi->select(table => 'table1'); |
|
816 |
$rows = $result->all; |
|
817 |
is_deeply($rows, [], "rollback"); |
|
818 | ||
819 |
$dbi->begin_work; |
|
820 | ||
821 |
eval { |
|
822 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
823 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
824 |
}; |
|
825 | ||
826 |
$dbi->commit unless $@; |
|
827 | ||
828 |
$result = $dbi->select(table => 'table1'); |
|
829 |
$rows = $result->all; |
|
830 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "commit"); |
|
831 | ||
832 |
$dbi->dbh->{AutoCommit} = 0; |
|
833 |
eval{ $dbi->begin_work }; |
|
834 |
ok($@, "exception"); |
|
835 |
$dbi->dbh->{AutoCommit} = 1; |
|
836 | ||
837 | ||
838 |
test 'method'; |
|
839 |
$dbi->method( |
|
840 |
one => sub { 1 } |
|
841 |
); |
|
842 |
$dbi->method( |
|
843 |
two => sub { 2 } |
|
844 |
); |
|
845 |
$dbi->method({ |
|
846 |
twice => sub { |
|
847 |
my $self = shift; |
|
848 |
return $_[0] * 2; |
|
849 |
} |
|
850 |
}); |
|
851 | ||
852 |
is($dbi->one, 1, "first"); |
|
853 |
is($dbi->two, 2, "second"); |
|
854 |
is($dbi->twice(5), 10 , "second"); |
|
855 | ||
856 |
eval {$dbi->XXXXXX}; |
|
857 |
ok($@, "not exists"); |
|
858 | ||
859 |
test 'out filter'; |
|
860 |
$dbi = DBIx::Custom->connect; |
|
861 |
eval { $dbi->execute('drop table table1') }; |
|
862 |
$dbi->execute($create_table1); |
|
863 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
864 |
$dbi->register_filter(three_times => sub { $_[0] * 3}); |
|
865 |
$dbi->apply_filter( |
|
866 |
'table1', 'key1' => {out => 'twice', in => 'three_times'}, |
|
867 |
'key2' => {out => 'three_times', in => 'twice'}); |
|
868 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
869 |
$result = $dbi->execute('select * from table1;'); |
|
870 |
$row = $result->fetch_hash_first; |
|
871 |
is_deeply($row, {key1 => 2, key2 => 6}, "insert"); |
|
872 |
$result = $dbi->select(table => 'table1'); |
|
873 |
$row = $result->one; |
|
874 |
is_deeply($row, {key1 => 6, key2 => 12}, "insert"); |
|
875 | ||
876 |
$dbi = DBIx::Custom->connect; |
|
877 |
eval { $dbi->execute('drop table table1') }; |
|
878 |
$dbi->execute($create_table1); |
|
879 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
880 |
$dbi->register_filter(three_times => sub { $_[0] * 3}); |
|
881 |
$dbi->apply_filter( |
|
882 |
'table1', 'key1' => {out => 'twice', in => 'three_times'}, |
|
883 |
'key2' => {out => 'three_times', in => 'twice'}); |
|
884 |
$dbi->apply_filter( |
|
885 |
'table1', 'key1' => {out => undef} |
|
886 |
); |
|
887 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
888 |
$result = $dbi->execute('select * from table1;'); |
|
889 |
$row = $result->one; |
|
890 |
is_deeply($row, {key1 => 1, key2 => 6}, "insert"); |
|
891 | ||
892 |
$dbi = DBIx::Custom->connect; |
|
893 |
eval { $dbi->execute('drop table table1') }; |
|
894 |
$dbi->execute($create_table1); |
|
895 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
896 |
$dbi->apply_filter( |
|
897 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
|
898 |
); |
|
899 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => undef}); |
|
900 |
$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2}); |
|
901 |
$result = $dbi->execute('select * from table1;'); |
|
902 |
$row = $result->one; |
|
903 |
is_deeply($row, {key1 => 4, key2 => 2}, "update"); |
|
904 | ||
905 |
$dbi = DBIx::Custom->connect; |
|
906 |
eval { $dbi->execute('drop table table1') }; |
|
907 |
$dbi->execute($create_table1); |
|
908 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
909 |
$dbi->apply_filter( |
|
910 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
|
911 |
); |
|
912 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1=> undef}); |
|
913 |
$dbi->delete(table => 'table1', where => {key1 => 1}); |
|
914 |
$result = $dbi->execute('select * from table1;'); |
|
915 |
$rows = $result->all; |
|
916 |
is_deeply($rows, [], "delete"); |
|
917 | ||
918 |
$dbi = DBIx::Custom->connect; |
|
919 |
eval { $dbi->execute('drop table table1') }; |
|
920 |
$dbi->execute($create_table1); |
|
921 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
922 |
$dbi->apply_filter( |
|
923 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
|
924 |
); |
|
925 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef}); |
|
926 |
$result = $dbi->select(table => 'table1', where => {key1 => 1}); |
|
927 |
$result->filter({'key2' => 'twice'}); |
|
928 |
$rows = $result->all; |
|
929 |
is_deeply($rows, [{key1 => 4, key2 => 4}], "select"); |
|
930 | ||
931 |
$dbi = DBIx::Custom->connect; |
|
932 |
eval { $dbi->execute('drop table table1') }; |
|
933 |
$dbi->execute($create_table1); |
|
934 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
935 |
$dbi->apply_filter( |
|
936 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
|
937 |
); |
|
938 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef}); |
|
939 |
$result = $dbi->execute("select * from table1 where key1 = :key1 and key2 = :key2;", |
|
940 |
param => {key1 => 1, key2 => 2}, |
|
941 |
table => ['table1']); |
|
942 |
$rows = $result->all; |
|
943 |
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute"); |
|
944 | ||
945 |
$dbi = DBIx::Custom->connect; |
|
946 |
eval { $dbi->execute('drop table table1') }; |
|
947 |
$dbi->execute($create_table1); |
|
948 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
949 |
$dbi->apply_filter( |
|
950 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
|
951 |
); |
|
952 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef}); |
|
953 |
$result = $dbi->execute("select * from {table table1} where key1 = :key1 and key2 = :key2;", |
|
954 |
param => {key1 => 1, key2 => 2}); |
|
955 |
$rows = $result->all; |
|
956 |
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute table tag"); |
|
957 | ||
958 |
$dbi = DBIx::Custom->connect; |
|
959 |
eval { $dbi->execute('drop table table1') }; |
|
960 |
eval { $dbi->execute('drop table table2') }; |
|
961 |
$dbi->execute($create_table1); |
|
962 |
$dbi->execute($create_table2); |
|
963 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
964 |
$dbi->register_filter(three_times => sub { $_[0] * 3 }); |
|
965 |
$dbi->apply_filter( |
|
966 |
'table1', 'key2' => {out => 'twice', in => 'twice'} |
|
967 |
); |
|
968 |
$dbi->apply_filter( |
|
969 |
'table2', 'key3' => {out => 'three_times', in => 'three_times'} |
|
970 |
); |
|
971 |
$dbi->insert(table => 'table1', param => {key1 => 5, key2 => 2}, filter => {key2 => undef}); |
|
972 |
$dbi->insert(table => 'table2', param => {key1 => 5, key3 => 6}, filter => {key3 => undef}); |
|
973 |
$result = $dbi->select( |
|
974 |
table => ['table1', 'table2'], |
|
975 |
column => ['key2', 'key3'], |
|
976 |
where => {'table1.key2' => 1, 'table2.key3' => 2}, relation => {'table1.key1' => 'table2.key1'}); |
|
977 | ||
978 |
$result->filter({'key2' => 'twice'}); |
|
979 |
$rows = $result->all; |
|
980 |
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join"); |
|
981 | ||
982 |
$result = $dbi->select( |
|
983 |
table => ['table1', 'table2'], |
|
984 |
column => ['key2', 'key3'], |
|
985 |
where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'}); |
|
986 | ||
987 |
$result->filter({'key2' => 'twice'}); |
|
988 |
$rows = $result->all; |
|
989 |
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join : omit"); |
|
990 | ||
991 |
test 'each_column'; |
|
992 |
$dbi = DBIx::Custom->connect; |
|
993 |
eval { $dbi->execute("drop table ${q}table$p") }; |
|
994 |
eval { $dbi->execute('drop table table1') }; |
|
995 |
eval { $dbi->execute('drop table table2') }; |
|
test cleranup
|
996 |
eval { $dbi->execute('drop table table3') }; |
test cleanup
|
997 |
$dbi->execute($create_table1_type); |
998 |
$dbi->execute($create_table2); |
|
999 | ||
1000 |
$infos = []; |
|
1001 |
$dbi->each_column(sub { |
|
1002 |
my ($self, $table, $column, $cinfo) = @_; |
|
1003 |
|
|
1004 |
if ($table =~ /^table\d/) { |
|
1005 |
my $info = [$table, $column, $cinfo->{COLUMN_NAME}]; |
|
1006 |
push @$infos, $info; |
|
1007 |
} |
|
1008 |
}); |
|
1009 |
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos]; |
|
1010 |
is_deeply($infos, |
|
1011 |
[ |
|
1012 |
['table1', 'key1', 'key1'], |
|
1013 |
['table1', 'key2', 'key2'], |
|
1014 |
['table2', 'key1', 'key1'], |
|
1015 |
['table2', 'key3', 'key3'] |
|
1016 |
] |
|
1017 |
|
|
1018 |
); |
|
1019 |
test 'each_table'; |
|
1020 |
$dbi = DBIx::Custom->connect; |
|
1021 |
eval { $dbi->execute('drop table table1') }; |
|
1022 |
eval { $dbi->execute('drop table table2') }; |
|
1023 |
$dbi->execute($create_table2); |
|
1024 |
$dbi->execute($create_table1_type); |
|
1025 | ||
1026 |
$infos = []; |
|
1027 |
$dbi->each_table(sub { |
|
1028 |
my ($self, $table, $table_info) = @_; |
|
1029 |
|
|
1030 |
if ($table =~ /^table\d/) { |
|
1031 |
my $info = [$table, $table_info->{TABLE_NAME}]; |
|
1032 |
push @$infos, $info; |
|
1033 |
} |
|
1034 |
}); |
|
1035 |
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos]; |
|
1036 |
is_deeply($infos, |
|
1037 |
[ |
|
1038 |
['table1', 'table1'], |
|
1039 |
['table2', 'table2'], |
|
1040 |
] |
|
1041 |
); |
|
1042 | ||
1043 |
test 'limit'; |
|
1044 |
$dbi = DBIx::Custom->connect; |
|
1045 |
eval { $dbi->execute('drop table table1') }; |
|
1046 |
$dbi->execute($create_table1); |
|
1047 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1048 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}); |
|
1049 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6}); |
|
1050 |
$dbi->register_tag( |
|
1051 |
limit => sub { |
|
1052 |
my ($count, $offset) = @_; |
|
1053 |
|
|
1054 |
my $s = ''; |
|
1055 |
$s .= "limit $count"; |
|
1056 |
$s .= " offset $offset" if defined $offset; |
|
1057 |
|
|
1058 |
return [$s, []]; |
|
1059 |
} |
|
1060 |
); |
|
1061 |
$rows = $dbi->select( |
|
1062 |
table => 'table1', |
|
1063 |
where => {key1 => 1}, |
|
1064 |
append => "order by key2 {limit 1 0}" |
|
1065 |
)->all; |
|
1066 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
1067 |
$rows = $dbi->select( |
|
1068 |
table => 'table1', |
|
1069 |
where => {key1 => 1}, |
|
1070 |
append => "order by key2 {limit 2 1}" |
|
1071 |
)->all; |
|
1072 |
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]); |
|
1073 |
$rows = $dbi->select( |
|
1074 |
table => 'table1', |
|
1075 |
where => {key1 => 1}, |
|
1076 |
append => "order by key2 {limit 1}" |
|
1077 |
)->all; |
|
1078 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
1079 | ||
1080 |
test 'connect super'; |
|
1081 |
{ |
|
1082 |
package MyDBI; |
|
1083 |
|
|
1084 |
use base 'DBIx::Custom'; |
|
1085 |
sub connect { |
|
1086 |
my $self = shift->SUPER::connect(@_); |
|
1087 |
|
|
1088 |
return $self; |
|
1089 |
} |
|
1090 |
|
|
1091 |
sub new { |
|
1092 |
my $self = shift->SUPER::new(@_); |
|
1093 |
|
|
1094 |
return $self; |
|
1095 |
} |
|
1096 |
} |
|
1097 | ||
1098 |
$dbi = MyDBI->connect; |
|
1099 |
eval { $dbi->execute('drop table table1') }; |
|
1100 |
$dbi->execute($create_table1); |
|
1101 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1102 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
1103 | ||
1104 |
$dbi = MyDBI->new; |
|
1105 |
$dbi->connect; |
|
1106 |
eval { $dbi->execute('drop table table1') }; |
|
1107 |
$dbi->execute($create_table1); |
|
1108 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1109 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
1110 | ||
1111 |
{ |
|
1112 |
package MyDBI2; |
|
1113 |
|
|
1114 |
use base 'DBIx::Custom'; |
|
1115 |
sub connect { |
|
1116 |
my $self = shift->SUPER::new(@_); |
|
1117 |
$self->connect; |
|
1118 |
|
|
1119 |
return $self; |
|
1120 |
} |
|
1121 |
} |
|
1122 | ||
1123 |
$dbi = MyDBI->connect; |
|
1124 |
eval { $dbi->execute('drop table table1') }; |
|
1125 |
$dbi->execute($create_table1); |
|
1126 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1127 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
1128 | ||
1129 |
test 'end_filter'; |
|
1130 |
$dbi = DBIx::Custom->connect; |
|
1131 |
eval { $dbi->execute('drop table table1') }; |
|
1132 |
$dbi->execute($create_table1); |
|
1133 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1134 |
$result = $dbi->select(table => 'table1'); |
|
1135 |
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 }); |
|
1136 |
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 }); |
|
1137 |
$row = $result->fetch_first; |
|
1138 |
is_deeply($row, [6, 40]); |
|
1139 | ||
1140 |
$dbi = DBIx::Custom->connect; |
|
1141 |
eval { $dbi->execute('drop table table1') }; |
|
1142 |
$dbi->execute($create_table1); |
|
1143 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1144 |
$result = $dbi->select(table => 'table1'); |
|
1145 |
$result->filter([qw/key1 key2/] => sub { $_[0] * 2 }); |
|
1146 |
$result->end_filter([[qw/key1 key2/] => sub { $_[0] * 3 }]); |
|
1147 |
$row = $result->fetch_first; |
|
1148 |
is_deeply($row, [6, 12]); |
|
1149 | ||
1150 |
$dbi = DBIx::Custom->connect; |
|
1151 |
eval { $dbi->execute('drop table table1') }; |
|
1152 |
$dbi->execute($create_table1); |
|
1153 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1154 |
$result = $dbi->select(table => 'table1'); |
|
1155 |
$result->filter([[qw/key1 key2/] => sub { $_[0] * 2 }]); |
|
1156 |
$result->end_filter([qw/key1 key2/] => sub { $_[0] * 3 }); |
|
1157 |
$row = $result->fetch_first; |
|
1158 |
is_deeply($row, [6, 12]); |
|
1159 | ||
1160 |
$dbi->register_filter(five_times => sub { $_[0] * 5 }); |
|
1161 |
$result = $dbi->select(table => 'table1'); |
|
1162 |
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 }); |
|
1163 |
$result->end_filter({key1 => sub { $_[0] * 3 }, key2 => 'five_times' }); |
|
1164 |
$row = $result->one; |
|
1165 |
is_deeply($row, {key1 => 6, key2 => 40}); |
|
1166 | ||
1167 |
$dbi->register_filter(five_times => sub { $_[0] * 5 }); |
|
1168 |
$dbi->apply_filter('table1', |
|
1169 |
key1 => {end => sub { $_[0] * 3 } }, |
|
1170 |
key2 => {end => 'five_times'} |
|
1171 |
); |
|
1172 |
$result = $dbi->select(table => 'table1'); |
|
1173 |
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 }); |
|
1174 |
$row = $result->one; |
|
1175 |
is_deeply($row, {key1 => 6, key2 => 40}, 'apply_filter'); |
|
1176 | ||
1177 |
$dbi->register_filter(five_times => sub { $_[0] * 5 }); |
|
1178 |
$dbi->apply_filter('table1', |
|
1179 |
key1 => {end => sub { $_[0] * 3 } }, |
|
1180 |
key2 => {end => 'five_times'} |
|
1181 |
); |
|
1182 |
$result = $dbi->select(table => 'table1'); |
|
1183 |
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 }); |
|
1184 |
$result->filter(key1 => undef); |
|
1185 |
$result->end_filter(key1 => undef); |
|
1186 |
$row = $result->one; |
|
1187 |
is_deeply($row, {key1 => 1, key2 => 40}, 'apply_filter overwrite'); |
|
1188 | ||
test cleanup
|
1189 |
test 'remove_end_filter and remove_filter'; |
1190 |
$dbi = DBIx::Custom->connect; |
|
1191 |
eval { $dbi->execute('drop table table1') }; |
|
1192 |
$dbi->execute($create_table1); |
|
1193 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1194 |
$result = $dbi->select(table => 'table1'); |
|
1195 |
$row = $result |
|
1196 |
->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 }) |
|
1197 |
->remove_filter |
|
1198 |
->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 }) |
|
1199 |
->remove_end_filter |
|
1200 |
->fetch_first; |
|
1201 |
is_deeply($row, [1, 2]); |
|
1202 | ||
1203 |
test 'empty where select'; |
|
1204 |
$dbi = DBIx::Custom->connect; |
|
1205 |
eval { $dbi->execute('drop table table1') }; |
|
1206 |
$dbi->execute($create_table1); |
|
1207 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1208 |
$result = $dbi->select(table => 'table1', where => {}); |
|
1209 |
$row = $result->one; |
|
1210 |
is_deeply($row, {key1 => 1, key2 => 2}); |
|
1211 | ||
1212 |
test 'select query option'; |
|
1213 |
$dbi = DBIx::Custom->connect; |
|
1214 |
eval { $dbi->execute('drop table table1') }; |
|
1215 |
$dbi->execute($create_table1); |
|
1216 |
$query = $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, query => 1); |
|
1217 |
is(ref $query, 'DBIx::Custom::Query'); |
|
1218 |
$query = $dbi->update(table => 'table1', where => {key1 => 1}, param => {key2 => 2}, query => 1); |
|
1219 |
is(ref $query, 'DBIx::Custom::Query'); |
|
1220 |
$query = $dbi->delete(table => 'table1', where => {key1 => 1}, query => 1); |
|
1221 |
is(ref $query, 'DBIx::Custom::Query'); |
|
1222 |
$query = $dbi->select(table => 'table1', where => {key1 => 1, key2 => 2}, query => 1); |
|
1223 |
is(ref $query, 'DBIx::Custom::Query'); |
|
1224 | ||
1225 |
test 'where'; |
|
1226 |
$dbi = DBIx::Custom->connect; |
|
1227 |
eval { $dbi->execute('drop table table1') }; |
|
1228 |
$dbi->execute($create_table1); |
|
1229 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1230 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
1231 |
$where = $dbi->where->clause(['and', 'key1 = :key1', 'key2 = :key2']); |
|
1232 |
is("$where", "where ( key1 = :key1 and key2 = :key2 )", 'no param'); |
|
1233 | ||
1234 |
$where = $dbi->where |
|
1235 |
->clause(['and', 'key1 = :key1', 'key2 = :key2']) |
|
1236 |
->param({key1 => 1}); |
|
1237 | ||
1238 |
$result = $dbi->select( |
|
1239 |
table => 'table1', |
|
1240 |
where => $where |
|
1241 |
); |
|
1242 |
$row = $result->all; |
|
1243 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
1244 | ||
1245 |
$result = $dbi->select( |
|
1246 |
table => 'table1', |
|
1247 |
where => [ |
|
1248 |
['and', 'key1 = :key1', 'key2 = :key2'], |
|
1249 |
{key1 => 1} |
|
1250 |
] |
|
1251 |
); |
|
1252 |
$row = $result->all; |
|
1253 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
1254 | ||
1255 |
$where = $dbi->where |
|
1256 |
->clause(['and', 'key1 = :key1', 'key2 = :key2']) |
|
1257 |
->param({key1 => 1, key2 => 2}); |
|
1258 |
$result = $dbi->select( |
|
1259 |
table => 'table1', |
|
1260 |
where => $where |
|
1261 |
); |
|
1262 |
$row = $result->all; |
|
1263 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
1264 | ||
1265 |
$where = $dbi->where |
|
1266 |
->clause(['and', 'key1 = :key1', 'key2 = :key2']) |
|
1267 |
->param({}); |
|
1268 |
$result = $dbi->select( |
|
1269 |
table => 'table1', |
|
1270 |
where => $where, |
|
1271 |
); |
|
1272 |
$row = $result->all; |
|
1273 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
1274 | ||
1275 |
$where = $dbi->where |
|
1276 |
->clause(['and', ['or', 'key1 > :key1', 'key1 < :key1'], 'key2 = :key2']) |
|
1277 |
->param({key1 => [0, 3], key2 => 2}); |
|
1278 |
$result = $dbi->select( |
|
1279 |
table => 'table1', |
|
1280 |
where => $where, |
|
1281 |
); |
|
1282 |
$row = $result->all; |
|
1283 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
1284 | ||
1285 |
$where = $dbi->where; |
|
1286 |
$result = $dbi->select( |
|
1287 |
table => 'table1', |
|
1288 |
where => $where |
|
1289 |
); |
|
1290 |
$row = $result->all; |
|
1291 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
1292 | ||
1293 |
eval { |
|
1294 |
$where = $dbi->where |
|
1295 |
->clause(['uuu']); |
|
1296 |
$result = $dbi->select( |
|
1297 |
table => 'table1', |
|
1298 |
where => $where |
|
1299 |
); |
|
1300 |
}; |
|
1301 |
ok($@); |
|
1302 | ||
1303 |
$where = $dbi->where; |
|
1304 |
is("$where", ''); |
|
1305 | ||
1306 |
$where = $dbi->where |
|
1307 |
->clause(['or', ('key1 = :key1') x 2]) |
|
1308 |
->param({key1 => [1, 3]}); |
|
1309 |
$result = $dbi->select( |
|
1310 |
table => 'table1', |
|
1311 |
where => $where, |
|
1312 |
); |
|
1313 |
$row = $result->all; |
|
1314 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
1315 | ||
1316 |
$where = $dbi->where |
|
1317 |
->clause(['or', ('key1 = :key1') x 2]) |
|
1318 |
->param({key1 => [1]}); |
|
1319 |
$result = $dbi->select( |
|
1320 |
table => 'table1', |
|
1321 |
where => $where, |
|
1322 |
); |
|
1323 |
$row = $result->all; |
|
1324 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
1325 | ||
1326 |
$where = $dbi->where |
|
1327 |
->clause(['or', ('key1 = :key1') x 2]) |
|
1328 |
->param({key1 => 1}); |
|
1329 |
$result = $dbi->select( |
|
1330 |
table => 'table1', |
|
1331 |
where => $where, |
|
1332 |
); |
|
1333 |
$row = $result->all; |
|
1334 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
1335 | ||
1336 |
$where = $dbi->where |
|
1337 |
->clause('key1 = :key1') |
|
1338 |
->param({key1 => 1}); |
|
1339 |
$result = $dbi->select( |
|
1340 |
table => 'table1', |
|
1341 |
where => $where, |
|
1342 |
); |
|
1343 |
$row = $result->all; |
|
1344 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
1345 | ||
1346 |
$where = $dbi->where |
|
1347 |
->clause('key1 = :key1 key2 = :key2') |
|
1348 |
->param({key1 => 1}); |
|
1349 |
eval{$where->to_string}; |
|
1350 |
like($@, qr/one column/); |
|
1351 | ||
1352 |
$where = $dbi->where |
|
1353 |
->clause(['or', ('key1 = :key1') x 3]) |
|
1354 |
->param({key1 => [$dbi->not_exists, 1, 3]}); |
|
1355 |
$result = $dbi->select( |
|
1356 |
table => 'table1', |
|
1357 |
where => $where, |
|
1358 |
); |
|
1359 |
$row = $result->all; |
|
1360 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists'); |
|
1361 | ||
1362 |
$where = $dbi->where |
|
1363 |
->clause(['or', ('key1 = :key1') x 3]) |
|
1364 |
->param({key1 => [1, $dbi->not_exists, 3]}); |
|
1365 |
$result = $dbi->select( |
|
1366 |
table => 'table1', |
|
1367 |
where => $where, |
|
1368 |
); |
|
1369 |
$row = $result->all; |
|
1370 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists'); |
|
1371 | ||
1372 |
$where = $dbi->where |
|
1373 |
->clause(['or', ('key1 = :key1') x 3]) |
|
1374 |
->param({key1 => [1, 3, $dbi->not_exists]}); |
|
1375 |
$result = $dbi->select( |
|
1376 |
table => 'table1', |
|
1377 |
where => $where, |
|
1378 |
); |
|
1379 |
$row = $result->all; |
|
1380 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists'); |
|
1381 | ||
1382 |
$where = $dbi->where |
|
1383 |
->clause(['or', ('key1 = :key1') x 3]) |
|
1384 |
->param({key1 => [1, $dbi->not_exists, $dbi->not_exists]}); |
|
1385 |
$result = $dbi->select( |
|
1386 |
table => 'table1', |
|
1387 |
where => $where, |
|
1388 |
); |
|
1389 |
$row = $result->all; |
|
1390 |
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists'); |
|
1391 | ||
1392 |
$where = $dbi->where |
|
1393 |
->clause(['or', ('key1 = :key1') x 3]) |
|
1394 |
->param({key1 => [$dbi->not_exists, 1, $dbi->not_exists]}); |
|
1395 |
$result = $dbi->select( |
|
1396 |
table => 'table1', |
|
1397 |
where => $where, |
|
1398 |
); |
|
1399 |
$row = $result->all; |
|
1400 |
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists'); |
|
1401 | ||
1402 |
$where = $dbi->where |
|
1403 |
->clause(['or', ('key1 = :key1') x 3]) |
|
1404 |
->param({key1 => [$dbi->not_exists, $dbi->not_exists, 1]}); |
|
1405 |
$result = $dbi->select( |
|
1406 |
table => 'table1', |
|
1407 |
where => $where, |
|
1408 |
); |
|
1409 |
$row = $result->all; |
|
1410 |
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists'); |
|
1411 | ||
1412 |
$where = $dbi->where |
|
1413 |
->clause(['or', ('key1 = :key1') x 3]) |
|
1414 |
->param({key1 => [$dbi->not_exists, $dbi->not_exists, $dbi->not_exists]}); |
|
1415 |
$result = $dbi->select( |
|
1416 |
table => 'table1', |
|
1417 |
where => $where, |
|
1418 |
); |
|
1419 |
$row = $result->all; |
|
1420 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists'); |
|
1421 | ||
1422 |
$where = $dbi->where |
|
1423 |
->clause(['or', ('key1 = :key1') x 3]) |
|
1424 |
->param({key1 => []}); |
|
1425 |
$result = $dbi->select( |
|
1426 |
table => 'table1', |
|
1427 |
where => $where, |
|
1428 |
); |
|
1429 |
$row = $result->all; |
|
1430 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists'); |
|
1431 | ||
1432 |
$where = $dbi->where |
|
1433 |
->clause(['and', '{> key1}', '{< key1}' ]) |
|
1434 |
->param({key1 => [2, $dbi->not_exists]}); |
|
1435 |
$result = $dbi->select( |
|
1436 |
table => 'table1', |
|
1437 |
where => $where, |
|
1438 |
); |
|
1439 |
$row = $result->all; |
|
1440 |
is_deeply($row, [{key1 => 3, key2 => 4}], 'not_exists'); |
|
1441 | ||
1442 |
$where = $dbi->where |
|
1443 |
->clause(['and', '{> key1}', '{< key1}' ]) |
|
1444 |
->param({key1 => [$dbi->not_exists, 2]}); |
|
1445 |
$result = $dbi->select( |
|
1446 |
table => 'table1', |
|
1447 |
where => $where, |
|
1448 |
); |
|
1449 |
$row = $result->all; |
|
1450 |
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists'); |
|
1451 | ||
1452 |
$where = $dbi->where |
|
1453 |
->clause(['and', '{> key1}', '{< key1}' ]) |
|
1454 |
->param({key1 => [$dbi->not_exists, $dbi->not_exists]}); |
|
1455 |
$result = $dbi->select( |
|
1456 |
table => 'table1', |
|
1457 |
where => $where, |
|
1458 |
); |
|
1459 |
$row = $result->all; |
|
1460 |
is_deeply($row, [{key1 => 1, key2 => 2},{key1 => 3, key2 => 4}], 'not_exists'); |
|
1461 | ||
1462 |
$where = $dbi->where |
|
1463 |
->clause(['and', '{> key1}', '{< key1}' ]) |
|
1464 |
->param({key1 => [0, 2]}); |
|
1465 |
$result = $dbi->select( |
|
1466 |
table => 'table1', |
|
1467 |
where => $where, |
|
1468 |
); |
|
1469 |
$row = $result->all; |
|
1470 |
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists'); |
|
1471 | ||
1472 |
$where = $dbi->where |
|
1473 |
->clause(['and', 'key1 is not null', 'key2 is not null' ]); |
|
1474 |
$result = $dbi->select( |
|
1475 |
table => 'table1', |
|
1476 |
where => $where, |
|
1477 |
); |
|
1478 |
$row = $result->all; |
|
1479 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists'); |
|
1480 | ||
1481 |
eval {$dbi->where(ppp => 1) }; |
|
1482 |
like($@, qr/invalid/); |
|
1483 | ||
1484 |
$where = $dbi->where( |
|
1485 |
clause => ['and', ['or'], ['and', 'key1 = :key1', 'key2 = :key2']], |
|
1486 |
param => {key1 => 1, key2 => 2} |
|
1487 |
); |
|
1488 |
$result = $dbi->select( |
|
1489 |
table => 'table1', |
|
1490 |
where => $where, |
|
1491 |
); |
|
1492 |
$row = $result->all; |
|
1493 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
1494 | ||
1495 | ||
1496 |
$where = $dbi->where( |
|
1497 |
clause => ['and', ['or'], ['or', ':key1', ':key2']], |
|
1498 |
param => {} |
|
1499 |
); |
|
1500 |
$result = $dbi->select( |
|
1501 |
table => 'table1', |
|
1502 |
where => $where, |
|
1503 |
); |
|
1504 |
$row = $result->all; |
|
1505 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
1506 | ||
1507 |
$where = $dbi->where; |
|
1508 |
$where->clause(['and', ':key1{=}']); |
|
1509 |
$where->param({key1 => undef}); |
|
1510 |
$result = $dbi->execute("select * from table1 $where", {key1 => 1}); |
|
1511 |
$row = $result->all; |
|
1512 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
1513 | ||
1514 |
$where = $dbi->where; |
|
1515 |
$where->clause(['and', ':key1{=}']); |
|
1516 |
$where->param({key1 => undef}); |
|
1517 |
$where->if('defined'); |
|
1518 |
$where->map; |
|
1519 |
$result = $dbi->execute("select * from table1 $where", {key1 => 1}); |
|
1520 |
$row = $result->all; |
|
1521 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
1522 | ||
1523 |
$where = $dbi->where; |
|
1524 |
$where->clause(['or', ':key1{=}', ':key1{=}']); |
|
1525 |
$where->param({key1 => [undef, undef]}); |
|
1526 |
$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]}); |
|
1527 |
$row = $result->all; |
|
1528 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
1529 |
$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]}); |
|
1530 |
$row = $result->all; |
|
1531 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
1532 | ||
1533 |
$where = $dbi->where; |
|
1534 |
$where->clause(['and', ':key1{=}']); |
|
1535 |
$where->param({key1 => [undef, undef]}); |
|
1536 |
$where->if('defined'); |
|
1537 |
$where->map; |
|
1538 |
$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]}); |
|
1539 |
$row = $result->all; |
|
1540 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
1541 |
$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]}); |
|
1542 |
$row = $result->all; |
|
1543 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
1544 | ||
1545 |
$where = $dbi->where; |
|
1546 |
$where->clause(['and', ':key1{=}']); |
|
1547 |
$where->param({key1 => 0}); |
|
1548 |
$where->if('length'); |
|
1549 |
$where->map; |
|
1550 |
$result = $dbi->execute("select * from table1 $where", {key1 => 1}); |
|
1551 |
$row = $result->all; |
|
1552 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
1553 | ||
1554 |
$where = $dbi->where; |
|
1555 |
$where->clause(['and', ':key1{=}']); |
|
1556 |
$where->param({key1 => ''}); |
|
1557 |
$where->if('length'); |
|
1558 |
$where->map; |
|
1559 |
$result = $dbi->execute("select * from table1 $where", {key1 => 1}); |
|
1560 |
$row = $result->all; |
|
1561 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
1562 | ||
1563 |
$where = $dbi->where; |
|
1564 |
$where->clause(['and', ':key1{=}']); |
|
1565 |
$where->param({key1 => 5}); |
|
1566 |
$where->if(sub { ($_[0] || '') eq 5 }); |
|
1567 |
$where->map; |
|
1568 |
$result = $dbi->execute("select * from table1 $where", {key1 => 1}); |
|
1569 |
$row = $result->all; |
|
1570 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
1571 | ||
1572 |
$where = $dbi->where; |
|
1573 |
$where->clause(['and', ':key1{=}']); |
|
1574 |
$where->param({key1 => 7}); |
|
1575 |
$where->if(sub { ($_[0] || '') eq 5 }); |
|
1576 |
$where->map; |
|
1577 |
$result = $dbi->execute("select * from table1 $where", {key1 => 1}); |
|
1578 |
$row = $result->all; |
|
1579 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
1580 | ||
1581 |
$where = $dbi->where; |
|
1582 |
$where->param({id => 1, author => 'Ken', price => 1900}); |
|
1583 |
$where->map(id => 'book.id', |
|
1584 |
author => ['book.author', sub { '%' . $_[0] . '%' }], |
|
1585 |
price => ['book.price', {if => sub { $_[0] eq 1900 }}] |
|
1586 |
); |
|
1587 |
is_deeply($where->param, {'book.id' => 1, 'book.author' => '%Ken%', |
|
1588 |
'book.price' => 1900}); |
|
1589 | ||
1590 |
$where = $dbi->where; |
|
1591 |
$where->param({id => 0, author => 0, price => 0}); |
|
1592 |
$where->map( |
|
1593 |
id => 'book.id', |
|
1594 |
author => ['book.author', sub { '%' . $_[0] . '%' }], |
|
1595 |
price => ['book.price', sub { '%' . $_[0] . '%' }, |
|
1596 |
{if => sub { $_[0] eq 0 }}] |
|
1597 |
); |
|
1598 |
is_deeply($where->param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'}); |
|
1599 | ||
1600 |
$where = $dbi->where; |
|
1601 |
$where->param({id => '', author => '', price => ''}); |
|
1602 |
$where->if('length'); |
|
1603 |
$where->map( |
|
1604 |
id => 'book.id', |
|
1605 |
author => ['book.author', sub { '%' . $_[0] . '%' }], |
|
1606 |
price => ['book.price', sub { '%' . $_[0] . '%' }, |
|
1607 |
{if => sub { $_[0] eq 1 }}] |
|
1608 |
); |
|
1609 |
is_deeply($where->param, {}); |
|
1610 | ||
1611 |
$where = $dbi->where; |
|
1612 |
$where->param({id => undef, author => undef, price => undef}); |
|
1613 |
$where->if('length'); |
|
1614 |
$where->map( |
|
1615 |
id => 'book.id', |
|
1616 |
price => ['book.price', {if => 'exists'}] |
|
1617 |
); |
|
1618 |
is_deeply($where->param, {'book.price' => undef}); |
|
1619 | ||
1620 |
$where = $dbi->where; |
|
1621 |
$where->param({price => 'a'}); |
|
1622 |
$where->if('length'); |
|
1623 |
$where->map( |
|
1624 |
id => ['book.id', {if => 'exists'}], |
|
1625 |
price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}] |
|
1626 |
); |
|
1627 |
is_deeply($where->param, {'book.price' => '%a'}); |
|
1628 | ||
1629 |
$where = $dbi->where; |
|
1630 |
$where->param({id => [1, 2], author => 'Ken', price => 1900}); |
|
1631 |
$where->map( |
|
1632 |
id => 'book.id', |
|
1633 |
author => ['book.author', sub { '%' . $_[0] . '%' }], |
|
1634 |
price => ['book.price', {if => sub { $_[0] eq 1900 }}] |
|
1635 |
); |
|
1636 |
is_deeply($where->param, {'book.id' => [1, 2], 'book.author' => '%Ken%', |
|
1637 |
'book.price' => 1900}); |
|
1638 | ||
1639 |
$where = $dbi->where; |
|
1640 |
$where->if('length'); |
|
1641 |
$where->param({id => ['', ''], author => 'Ken', price => 1900}); |
|
1642 |
$where->map( |
|
1643 |
id => 'book.id', |
|
1644 |
author => ['book.author', sub { '%' . $_[0] . '%' }], |
|
1645 |
price => ['book.price', {if => sub { $_[0] eq 1900 }}] |
|
1646 |
); |
|
1647 |
is_deeply($where->param, {'book.id' => [$dbi->not_exists, $dbi->not_exists], 'book.author' => '%Ken%', |
|
1648 |
'book.price' => 1900}); |
|
1649 | ||
1650 |
$where = $dbi->where; |
|
1651 |
$where->param({id => ['', ''], author => 'Ken', price => 1900}); |
|
1652 |
$where->map( |
|
1653 |
id => ['book.id', {if => 'length'}], |
|
1654 |
author => ['book.author', sub { '%' . $_[0] . '%' }, {if => 'defined'}], |
|
1655 |
price => ['book.price', {if => sub { $_[0] eq 1900 }}] |
|
1656 |
); |
|
1657 |
is_deeply($where->param, {'book.id' => [$dbi->not_exists, $dbi->not_exists], 'book.author' => '%Ken%', |
|
1658 |
'book.price' => 1900}); |
|
1659 | ||
1660 |
test 'dbi_option default'; |
|
1661 |
$dbi = DBIx::Custom->new; |
|
1662 |
is_deeply($dbi->dbi_option, {}); |
|
1663 | ||
1664 |
test 'register_tag_processor'; |
|
1665 |
$dbi = DBIx::Custom->connect; |
|
1666 |
$dbi->register_tag_processor( |
|
1667 |
a => sub { 1 } |
|
1668 |
); |
|
1669 |
is($dbi->query_builder->tag_processors->{a}->(), 1); |
|
1670 | ||
1671 |
test 'register_tag'; |
|
1672 |
$dbi = DBIx::Custom->connect; |
|
1673 |
$dbi->register_tag( |
|
1674 |
b => sub { 2 } |
|
1675 |
); |
|
1676 |
is($dbi->query_builder->tags->{b}->(), 2); |
|
1677 | ||
1678 |
test 'table not specify exception'; |
|
1679 |
$dbi = DBIx::Custom->connect; |
|
1680 |
eval {$dbi->insert}; |
|
1681 |
like($@, qr/table/); |
|
1682 |
eval {$dbi->update}; |
|
1683 |
like($@, qr/table/); |
|
1684 |
eval {$dbi->delete}; |
|
1685 |
like($@, qr/table/); |
|
1686 |
eval {$dbi->select}; |
|
1687 |
like($@, qr/table/); |
|
test cleanup
|
1688 | |
test cleanup
|
1689 |
test 'more tests'; |
1690 |
$dbi = DBIx::Custom->connect; |
|
1691 |
eval{$dbi->apply_filter('table', 'column', [])}; |
|
1692 |
like($@, qr/apply_filter/); |
|
1693 | ||
1694 |
eval{$dbi->apply_filter('table', 'column', {outer => 2})}; |
|
1695 |
like($@, qr/apply_filter/); |
|
1696 | ||
1697 |
$dbi->apply_filter( |
|
1698 | ||
1699 |
); |
|
1700 |
$dbi = DBIx::Custom->connect; |
|
1701 |
eval { $dbi->execute('drop table table1') }; |
|
1702 |
$dbi->execute($create_table1); |
|
1703 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1704 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
1705 |
$dbi->apply_filter('table1', 'key2', |
|
1706 |
{in => sub { $_[0] * 3 }, out => sub { $_[0] * 2 }}); |
|
1707 |
$rows = $dbi->select(table => 'table1', where => {key2 => 1})->all; |
|
1708 |
is_deeply($rows, [{key1 => 1, key2 => 6}]); |
|
1709 | ||
1710 |
$dbi = DBIx::Custom->connect; |
|
1711 |
eval { $dbi->execute('drop table table1') }; |
|
1712 |
$dbi->execute($create_table1); |
|
1713 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1714 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
1715 |
$dbi->apply_filter('table1', 'key2', {}); |
|
1716 |
$rows = $dbi->select(table => 'table1', where => {key2 => 2})->all; |
|
1717 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
1718 | ||
1719 |
$dbi = DBIx::Custom->connect; |
|
1720 |
eval {$dbi->apply_filter('table1', 'key2', {out => 'no'})}; |
|
1721 |
like($@, qr/not registered/); |
|
1722 |
eval {$dbi->apply_filter('table1', 'key2', {in => 'no'})}; |
|
1723 |
like($@, qr/not registered/); |
|
1724 |
$dbi->method({one => sub { 1 }}); |
|
1725 |
is($dbi->one, 1); |
|
1726 | ||
1727 |
eval{DBIx::Custom->connect(dsn => undef)}; |
|
1728 |
like($@, qr/_connect/); |
|
1729 | ||
1730 |
$dbi = DBIx::Custom->connect; |
|
1731 |
eval { $dbi->execute('drop table table1') }; |
|
1732 |
$dbi->execute($create_table1); |
|
1733 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
1734 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, |
|
1735 |
filter => {key1 => 'twice'}); |
|
1736 |
$row = $dbi->select(table => 'table1')->one; |
|
1737 |
is_deeply($row, {key1 => 2, key2 => 2}); |
|
1738 |
eval {$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, |
|
1739 |
filter => {key1 => 'no'}) }; |
|
1740 |
like($@, qr//); |
|
1741 | ||
1742 |
$dbi->register_filter(one => sub { }); |
|
1743 |
$dbi->default_fetch_filter('one'); |
|
1744 |
ok($dbi->default_fetch_filter); |
|
1745 |
$dbi->default_bind_filter('one'); |
|
1746 |
ok($dbi->default_bind_filter); |
|
1747 |
eval{$dbi->default_fetch_filter('no')}; |
|
1748 |
like($@, qr/not registered/); |
|
1749 |
eval{$dbi->default_bind_filter('no')}; |
|
1750 |
like($@, qr/not registered/); |
|
1751 |
$dbi->default_bind_filter(undef); |
|
1752 |
ok(!defined $dbi->default_bind_filter); |
|
1753 |
$dbi->default_fetch_filter(undef); |
|
1754 |
ok(!defined $dbi->default_fetch_filter); |
|
1755 |
eval {$dbi->execute('select * from table1 {} {= author') }; |
|
1756 |
like($@, qr/Tag not finished/); |
|
1757 | ||
1758 |
$dbi = DBIx::Custom->connect; |
|
1759 |
eval { $dbi->execute('drop table table1') }; |
|
1760 |
$dbi->execute($create_table1); |
|
1761 |
$dbi->register_filter(one => sub { 1 }); |
|
1762 |
$result = $dbi->select(table => 'table1'); |
|
1763 |
eval {$result->filter(key1 => 'no')}; |
|
1764 |
like($@, qr/not registered/); |
|
1765 |
eval {$result->end_filter(key1 => 'no')}; |
|
1766 |
like($@, qr/not registered/); |
|
1767 |
$result->default_filter(undef); |
|
1768 |
ok(!defined $result->default_filter); |
|
1769 |
$result->default_filter('one'); |
|
1770 |
is($result->default_filter->(), 1); |
|
1771 | ||
1772 |
test 'dbi_option'; |
|
1773 |
$dbi = DBIx::Custom->connect(dbi_option => {PrintError => 1}); |
|
1774 |
ok($dbi->dbh->{PrintError}); |
|
1775 |
$dbi = DBIx::Custom->connect(dbi_options => {PrintError => 1}); |
|
1776 |
ok($dbi->dbh->{PrintError}); |
|
1777 | ||
1778 |
test 'DBIx::Custom::Result stash()'; |
|
1779 |
$result = DBIx::Custom::Result->new; |
|
1780 |
is_deeply($result->stash, {}, 'default'); |
|
1781 |
$result->stash->{foo} = 1; |
|
1782 |
is($result->stash->{foo}, 1, 'get and set'); |
|
test cleanup
|
1783 | |
1784 | ||
1785 | ||
1786 | ||
1787 | ||
1788 | ||
1789 | ||
1790 | ||
1791 | ||
1792 | ||
1793 | ||
1794 | ||
1795 | ||
1796 | ||
1797 | ||
1798 | ||
1799 | ||
1800 | ||
1801 | ||
1802 | ||
1803 | ||
1804 | ||
1805 | ||
1806 | ||
1807 | ||
1808 | ||
1809 | ||
1810 | ||
1811 | ||
1812 | ||
1813 | ||
1814 | ||
1815 | ||
1816 | ||
cleanup test
|
1817 |
1; |