removed register_format()
|
1 |
use Test::More; |
2 |
use strict; |
|
3 |
use warnings; |
|
4 | ||
5 |
use utf8; |
|
6 |
use Encode qw/encode_utf8 decode_utf8/; |
|
7 | ||
8 |
BEGIN { |
|
9 |
eval { require DBD::SQLite; 1 } |
|
10 |
or plan skip_all => 'DBD::SQLite required'; |
|
11 |
eval { DBD::SQLite->VERSION >= 1.25 } |
|
12 |
or plan skip_all => 'DBD::SQLite >= 1.25 required'; |
|
13 | ||
14 |
plan 'no_plan'; |
|
15 |
use_ok('DBIx::Custom'); |
|
16 |
} |
|
17 | ||
18 |
# Function for test name |
|
19 |
my $test; |
|
20 |
sub test { |
|
21 |
$test = shift; |
|
22 |
} |
|
23 | ||
24 |
# Constant varialbes for test |
|
25 |
my $CREATE_TABLE = { |
|
26 |
0 => 'create table table1 (key1 char(255), key2 char(255));', |
|
27 |
1 => 'create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));', |
|
28 |
2 => 'create table table2 (key1 char(255), key3 char(255));' |
|
29 |
}; |
|
30 | ||
31 |
my $SELECT_TMPLS = { |
|
32 |
0 => 'select * from table1;' |
|
33 |
}; |
|
34 | ||
35 |
my $DROP_TABLE = { |
|
36 |
0 => 'drop table table1' |
|
37 |
}; |
|
38 | ||
39 |
my $NEW_ARGS = { |
|
40 |
0 => {data_source => 'dbi:SQLite:dbname=:memory:'} |
|
41 |
}; |
|
42 | ||
43 |
# Variables |
|
44 |
my $dbi; |
|
45 |
my $sth; |
|
46 |
my $tmpl; |
|
47 |
my @tmpls; |
|
48 |
my $select_tmpl; |
|
49 |
my $insert_tmpl; |
|
50 |
my $update_tmpl; |
|
51 |
my $params; |
|
52 |
my $sql; |
|
53 |
my $result; |
|
54 |
my $row; |
|
55 |
my @rows; |
|
56 |
my $rows; |
|
57 |
my $query; |
|
58 |
my @queries; |
|
59 |
my $select_query; |
|
60 |
my $insert_query; |
|
61 |
my $update_query; |
|
62 |
my $ret_val; |
|
63 | ||
64 | ||
65 |
test 'disconnect'; |
|
66 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
67 |
$dbi->disconnect; |
|
68 |
ok(!$dbi->dbh, $test); |
|
69 | ||
70 | ||
71 |
test 'connected'; |
|
72 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
73 |
ok($dbi->connected, "$test : connected"); |
|
74 | ||
75 |
# Prepare table |
|
76 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
77 |
$dbi->execute($CREATE_TABLE->{0}); |
|
78 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
79 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
80 | ||
81 |
test 'DBIx::Custom::Result test'; |
|
82 |
$tmpl = "select key1, key2 from table1"; |
|
83 |
$query = $dbi->create_query($tmpl); |
|
84 |
$result = $dbi->execute($query); |
|
85 | ||
86 |
@rows = (); |
|
87 |
while (my $row = $result->fetch) { |
|
88 |
push @rows, [@$row]; |
|
89 |
} |
|
90 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch scalar context"); |
|
91 | ||
92 |
$result = $dbi->execute($query); |
|
93 |
@rows = (); |
|
94 |
while (my @row = $result->fetch) { |
|
95 |
push @rows, [@row]; |
|
96 |
} |
|
97 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch list context"); |
|
98 | ||
99 |
$result = $dbi->execute($query); |
|
100 |
@rows = (); |
|
101 |
while (my $row = $result->fetch_hash) { |
|
102 |
push @rows, {%$row}; |
|
103 |
} |
|
104 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch_hash scalar context"); |
|
105 | ||
106 |
$result = $dbi->execute($query); |
|
107 |
@rows = (); |
|
108 |
while (my %row = $result->fetch_hash) { |
|
109 |
push @rows, {%row}; |
|
110 |
} |
|
111 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch hash list context"); |
|
112 | ||
113 |
$result = $dbi->execute($query); |
|
114 |
$rows = $result->fetch_all; |
|
115 |
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all scalar context"); |
|
116 | ||
117 |
$result = $dbi->execute($query); |
|
118 |
@rows = $result->fetch_all; |
|
119 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all list context"); |
|
120 | ||
121 |
$result = $dbi->execute($query); |
|
122 |
@rows = $result->fetch_hash_all; |
|
123 |
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_hash_all scalar context"); |
|
124 | ||
125 |
$result = $dbi->execute($query); |
|
126 |
@rows = $result->fetch_all; |
|
127 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_hash_all list context"); |
|
128 | ||
129 | ||
130 |
test 'Insert query return value'; |
|
131 |
$dbi->execute($DROP_TABLE->{0}); |
|
132 |
$dbi->execute($CREATE_TABLE->{0}); |
|
133 |
$tmpl = "insert into table1 {insert key1 key2}"; |
|
134 |
$query = $dbi->create_query($tmpl); |
|
135 |
$ret_val = $dbi->execute($query, param => {key1 => 1, key2 => 2}); |
|
136 |
ok($ret_val, $test); |
|
137 | ||
138 | ||
139 |
test 'Direct query'; |
|
140 |
$dbi->execute($DROP_TABLE->{0}); |
|
141 |
$dbi->execute($CREATE_TABLE->{0}); |
|
142 |
$insert_tmpl = "insert into table1 {insert key1 key2}"; |
|
143 |
$dbi->execute($insert_tmpl, param => {key1 => 1, key2 => 2}); |
|
144 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
145 |
$rows = $result->fetch_hash_all; |
|
146 |
is_deeply($rows, [{key1 => 1, key2 => 2}], $test); |
|
147 | ||
148 |
test 'Filter basic'; |
|
149 |
$dbi->execute($DROP_TABLE->{0}); |
|
150 |
$dbi->execute($CREATE_TABLE->{0}); |
|
151 |
$dbi->register_filter(twice => sub { $_[0] * 2}, |
|
152 |
three_times => sub { $_[0] * 3}); |
|
153 | ||
154 |
$insert_tmpl = "insert into table1 {insert key1 key2};"; |
|
155 |
$insert_query = $dbi->create_query($insert_tmpl); |
|
156 |
$insert_query->filter({key1 => 'twice'}); |
|
157 |
$dbi->execute($insert_query, param => {key1 => 1, key2 => 2}); |
|
158 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
159 |
$rows = $result->filter({key2 => 'three_times'})->fetch_hash_all; |
|
160 |
is_deeply($rows, [{key1 => 2, key2 => 6}], "$test : filter fetch_filter"); |
|
161 |
$dbi->execute($DROP_TABLE->{0}); |
|
162 | ||
163 |
test 'Filter in'; |
|
164 |
$dbi->execute($CREATE_TABLE->{0}); |
|
165 |
$insert_tmpl = "insert into table1 {insert key1 key2};"; |
|
166 |
$insert_query = $dbi->create_query($insert_tmpl); |
|
167 |
$dbi->execute($insert_query, param => {key1 => 2, key2 => 4}); |
|
168 |
$select_tmpl = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}"; |
|
169 |
$select_query = $dbi->create_query($select_tmpl); |
|
170 |
$select_query->filter({'table1.key1' => 'twice'}); |
|
171 |
$result = $dbi->execute($select_query, param => {'table1.key1' => [1,5], 'table1.key2' => [2,4]}); |
|
172 |
$rows = $result->fetch_hash_all; |
|
173 |
is_deeply($rows, [{key1 => 2, key2 => 4}], "$test : filter"); |
|
174 | ||
175 |
test 'DBIx::Custom::SQLTemplate basic tag'; |
|
176 |
$dbi->execute($DROP_TABLE->{0}); |
|
177 |
$dbi->execute($CREATE_TABLE->{1}); |
|
178 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
179 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
180 | ||
181 |
$tmpl = "select * from table1 where {= key1} and {<> key2} and {< key3} and {> key4} and {>= key5};"; |
|
182 |
$query = $dbi->create_query($tmpl); |
|
183 |
$result = $dbi->execute($query, param => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5}); |
|
184 |
$rows = $result->fetch_hash_all; |
|
185 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1"); |
|
186 | ||
187 |
$tmpl = "select * from table1 where {<= key1} and {like key2};"; |
|
188 |
$query = $dbi->create_query($tmpl); |
|
189 |
$result = $dbi->execute($query, param => {key1 => 1, key2 => '%2%'}); |
|
190 |
$rows = $result->fetch_hash_all; |
|
191 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2"); |
|
192 | ||
193 |
test 'DIB::Custom::SQLTemplate in tag'; |
|
194 |
$dbi->execute($DROP_TABLE->{0}); |
|
195 |
$dbi->execute($CREATE_TABLE->{1}); |
|
196 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
197 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
198 | ||
199 |
$tmpl = "select * from table1 where {in key1 2};"; |
|
200 |
$query = $dbi->create_query($tmpl); |
|
201 |
$result = $dbi->execute($query, param => {key1 => [9, 1]}); |
|
202 |
$rows = $result->fetch_hash_all; |
|
203 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic"); |
|
204 | ||
205 |
test 'DBIx::Custom::SQLTemplate insert tag'; |
|
206 |
$dbi->execute("delete from table1"); |
|
207 |
$insert_tmpl = 'insert into table1 {insert key1 key2 key3 key4 key5}'; |
|
208 |
$dbi->execute($insert_tmpl, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
209 | ||
210 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
211 |
$rows = $result->fetch_hash_all; |
|
212 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic"); |
|
213 | ||
214 |
test 'DBIx::Custom::SQLTemplate update tag'; |
|
215 |
$dbi->execute("delete from table1"); |
|
216 |
$insert_tmpl = "insert into table1 {insert key1 key2 key3 key4 key5}"; |
|
217 |
$dbi->execute($insert_tmpl, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
218 |
$dbi->execute($insert_tmpl, param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
219 | ||
220 |
$update_tmpl = 'update table1 {update key1 key2 key3 key4} where {= key5}'; |
|
221 |
$dbi->execute($update_tmpl, param => {key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}); |
|
222 | ||
223 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
224 |
$rows = $result->fetch_hash_all; |
|
225 |
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}, |
|
226 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : basic"); |
|
227 | ||
228 |
test 'Error case'; |
|
229 |
eval {DBIx::Custom->connect(data_source => 'dbi:SQLit')}; |
|
230 |
ok($@, "$test : connect error"); |
|
231 | ||
232 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
233 |
eval{$dbi->create_query("{p }")}; |
|
234 |
ok($@, "$test : create_query invalid SQL template"); |
|
235 | ||
236 |
test 'insert'; |
|
237 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
238 |
$dbi->execute($CREATE_TABLE->{0}); |
|
239 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
240 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
241 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
242 |
$rows = $result->fetch_hash_all; |
|
243 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : basic"); |
|
244 | ||
245 |
$dbi->execute('delete from table1'); |
|
246 |
$dbi->register_filter( |
|
247 |
twice => sub { $_[0] * 2 }, |
|
248 |
three_times => sub { $_[0] * 3 } |
|
249 |
); |
|
250 |
$dbi->default_query_filter('twice'); |
|
251 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => 'three_times'}); |
|
252 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
253 |
$rows = $result->fetch_hash_all; |
|
254 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : filter"); |
|
255 |
$dbi->default_query_filter(undef); |
|
256 | ||
257 |
$dbi->execute($DROP_TABLE->{0}); |
|
258 |
$dbi->execute($CREATE_TABLE->{0}); |
|
259 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, append => ' '); |
|
260 |
$rows = $dbi->select(table => 'table1')->fetch_hash_all; |
|
261 |
is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append'); |
|
262 | ||
263 |
test 'update'; |
|
264 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
265 |
$dbi->execute($CREATE_TABLE->{1}); |
|
266 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
267 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
268 |
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}); |
|
269 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
270 |
$rows = $result->fetch_hash_all; |
|
271 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
272 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
273 |
"$test : basic"); |
|
274 |
|
|
275 |
$dbi->execute("delete from table1"); |
|
276 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
277 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
278 |
$dbi->update(table => 'table1', param => {key2 => 12}, where => {key2 => 2, key3 => 3}); |
|
279 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
280 |
$rows = $result->fetch_hash_all; |
|
281 |
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5}, |
|
282 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
283 |
"$test : update key same as search key"); |
|
284 | ||
285 |
$dbi->execute("delete from table1"); |
|
286 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
287 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
288 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
289 |
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}, |
|
290 |
filter => {key2 => 'twice'}); |
|
291 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
292 |
$rows = $result->fetch_hash_all; |
|
293 |
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5}, |
|
294 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
295 |
"$test : filter"); |
|
296 | ||
297 | ||
298 |
$result = $dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}, append => ' '); |
|
299 | ||
300 |
test 'update_all'; |
|
301 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
302 |
$dbi->execute($CREATE_TABLE->{1}); |
|
303 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
304 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
305 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
306 |
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'}); |
|
307 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
308 |
$rows = $result->fetch_hash_all; |
|
309 |
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5}, |
|
310 |
{key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}], |
|
311 |
"$test : filter"); |
|
312 | ||
313 | ||
314 |
test 'delete'; |
|
315 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
316 |
$dbi->execute($CREATE_TABLE->{0}); |
|
317 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
318 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
319 |
$dbi->delete(table => 'table1', where => {key1 => 1}); |
|
320 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
321 |
$rows = $result->fetch_hash_all; |
|
322 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : basic"); |
|
323 | ||
324 |
$dbi->execute("delete from table1;"); |
|
325 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
326 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
327 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
328 |
$dbi->delete(table => 'table1', where => {key2 => 1}, filter => {key2 => 'twice'}); |
|
329 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
330 |
$rows = $result->fetch_hash_all; |
|
331 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : filter"); |
|
332 | ||
333 |
$dbi->delete(table => 'table1', where => {key1 => 1}, append => ' '); |
|
334 | ||
335 |
$dbi->delete_all(table => 'table1'); |
|
336 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
337 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
338 |
$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2}); |
|
339 |
$rows = $dbi->select(table => 'table1')->fetch_hash_all; |
|
340 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : delete multi key"); |
|
341 | ||
342 |
test 'delete error'; |
|
343 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
344 |
$dbi->execute($CREATE_TABLE->{0}); |
|
345 |
eval{$dbi->delete(table => 'table1')}; |
|
346 |
like($@, qr/Key-value pairs for where clause must be specified to 'delete' second argument/, |
|
347 |
"$test : where key-value pairs not specified"); |
|
348 | ||
349 |
test 'delete_all'; |
|
350 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
351 |
$dbi->execute($CREATE_TABLE->{0}); |
|
352 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
353 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
354 |
$dbi->delete_all(table => 'table1'); |
|
355 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
356 |
$rows = $result->fetch_hash_all; |
|
357 |
is_deeply($rows, [], "$test : basic"); |
|
358 | ||
359 | ||
360 |
test 'select'; |
|
361 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
362 |
$dbi->execute($CREATE_TABLE->{0}); |
|
363 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
364 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
365 |
$rows = $dbi->select(table => 'table1')->fetch_hash_all; |
|
366 |
is_deeply($rows, [{key1 => 1, key2 => 2}, |
|
367 |
{key1 => 3, key2 => 4}], "$test : table"); |
|
368 | ||
update document
|
369 |
$rows = $dbi->select(table => 'table1', column => ['key1'])->fetch_hash_all; |
removed register_format()
|
370 |
is_deeply($rows, [{key1 => 1}, {key1 => 3}], "$test : table and columns and where key"); |
371 | ||
372 |
$rows = $dbi->select(table => 'table1', where => {key1 => 1})->fetch_hash_all; |
|
373 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : table and columns and where key"); |
|
374 | ||
update document
|
375 |
$rows = $dbi->select(table => 'table1', column => ['key1'], where => {key1 => 3})->fetch_hash_all; |
removed register_format()
|
376 |
is_deeply($rows, [{key1 => 3}], "$test : table and columns and where key"); |
377 | ||
378 |
$rows = $dbi->select(table => 'table1', append => "order by key1 desc limit 1")->fetch_hash_all; |
|
379 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : append statement"); |
|
380 | ||
381 |
$dbi->register_filter(decrement => sub { $_[0] - 1 }); |
|
update document
|
382 |
$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'}) |
removed register_format()
|
383 |
->fetch_hash_all; |
384 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : filter"); |
|
385 | ||
386 |
$dbi->execute($CREATE_TABLE->{2}); |
|
387 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
388 |
$rows = $dbi->select( |
|
389 |
table => [qw/table1 table2/], |
|
update document
|
390 |
column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'], |
removed register_format()
|
391 |
where => {'table1.key2' => 2}, |
added commit method
|
392 |
relation => {'table1.key1' => 'table2.key1'} |
removed register_format()
|
393 |
)->fetch_hash_all; |
added commit method
|
394 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "$test : relation : exists where"); |
395 | ||
396 |
$rows = $dbi->select( |
|
397 |
table => [qw/table1 table2/], |
|
398 |
column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'], |
|
399 |
relation => {'table1.key1' => 'table2.key1'} |
|
400 |
)->fetch_hash_all; |
|
401 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "$test : relation : no exists where"); |
|
removed register_format()
|
402 | |
403 |
test 'fetch filter'; |
|
404 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
405 |
$dbi->register_filter( |
|
406 |
twice => sub { $_[0] * 2 }, |
|
407 |
three_times => sub { $_[0] * 3 } |
|
408 |
); |
|
409 |
$dbi->default_fetch_filter('twice'); |
|
410 |
$dbi->execute($CREATE_TABLE->{0}); |
|
411 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
412 |
$result = $dbi->select(table => 'table1'); |
|
413 |
$result->filter({key1 => 'three_times'}); |
|
414 |
$row = $result->fetch_hash_single; |
|
415 |
is_deeply($row, {key1 => 3, key2 => 4}, "$test: default_fetch_filter and filter"); |
|
416 | ||
417 |
test 'filters'; |
|
418 |
$dbi = DBIx::Custom->new; |
|
419 | ||
update document
|
420 |
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')), |
421 |
'あ', "$test : decode_utf8"); |
|
removed register_format()
|
422 | |
423 |
is($dbi->filters->{encode_utf8}->('あ'), |
|
424 |
encode_utf8('あ'), "$test : encode_utf8"); |
|
425 | ||
added commit method
|
426 |
test 'transaction'; |
427 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
428 |
$dbi->execute($CREATE_TABLE->{0}); |
|
429 |
$dbi->auto_commit(0); |
|
430 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
431 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
432 |
$dbi->commit; |
|
433 |
$result = $dbi->select(table => 'table1'); |
|
434 |
is_deeply(scalar $result->fetch_hash_all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}], |
|
435 |
"$test : commit"); |
|
436 | ||
437 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
438 |
$dbi->execute($CREATE_TABLE->{0}); |
|
439 |
$dbi->auto_commit(0); |
|
440 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
441 |
$dbi->rollback; |
|
442 | ||
443 |
$result = $dbi->select(table => 'table1'); |
|
444 |
ok(! $result->fetch_single, "$test: rollback"); |