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