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; |
|
renamed build_query to creat...
|
46 |
my $source; |
47 |
my @sources; |
|
removed register_format()
|
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'; |
|
renamed build_query to creat...
|
72 |
$source = "select key1, key2 from table1"; |
73 |
$query = $dbi->create_query($source); |
|
removed register_format()
|
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}); |
|
renamed update tag to update...
|
100 |
$source = "insert into table1 {insert_param key1 key2}"; |
renamed build_query to creat...
|
101 |
$query = $dbi->create_query($source); |
removed register_format()
|
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}); |
|
renamed update tag to update...
|
109 |
$insert_tmpl = "insert into table1 {insert_param key1 key2}"; |
removed register_format()
|
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 | ||
renamed update tag to update...
|
121 |
$insert_tmpl = "insert into table1 {insert_param key1 key2};"; |
renamed build_query to creat...
|
122 |
$insert_query = $dbi->create_query($insert_tmpl); |
removed register_format()
|
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}); |
|
renamed update tag to update...
|
132 |
$insert_tmpl = "insert into table1 {insert_param key1 key2};"; |
renamed build_query to creat...
|
133 |
$insert_query = $dbi->create_query($insert_tmpl); |
removed register_format()
|
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}"; |
|
renamed build_query to creat...
|
136 |
$select_query = $dbi->create_query($select_tmpl); |
removed register_format()
|
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 | ||
renamed build_query to creat...
|
148 |
$source = "select * from table1 where {= key1} and {<> key2} and {< key3} and {> key4} and {>= key5};"; |
149 |
$query = $dbi->create_query($source); |
|
removed register_format()
|
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 | ||
renamed build_query to creat...
|
154 |
$source = "select * from table1 where {<= key1} and {like key2};"; |
155 |
$query = $dbi->create_query($source); |
|
removed register_format()
|
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 | ||
renamed build_query to creat...
|
166 |
$source = "select * from table1 where {in key1 2};"; |
167 |
$query = $dbi->create_query($source); |
|
removed register_format()
|
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"); |
|
renamed update tag to update...
|
174 |
$insert_tmpl = 'insert into table1 {insert_param key1 key2 key3 key4 key5}'; |
removed register_format()
|
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"); |
|
renamed update tag to update...
|
183 |
$insert_tmpl = "insert into table1 {insert_param key1 key2 key3 key4 key5}"; |
removed register_format()
|
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 | ||
renamed update tag to update...
|
187 |
$update_tmpl = 'update table1 {update_param key1 key2 key3 key4} where {= key5}'; |
removed register_format()
|
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}); |
|
renamed build_query to creat...
|
200 |
eval{$dbi->create_query("{p }")}; |
201 |
ok($@, "$test : create_query invalid SQL template"); |
|
removed register_format()
|
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 |
); |
|
renamed default_query_filter...
|
217 |
$dbi->default_bind_filter('twice'); |
removed register_format()
|
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"); |
|
renamed default_query_filter...
|
222 |
$dbi->default_bind_filter(undef); |
removed register_format()
|
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 | ||
changed argument of tag proc...
|
230 |
eval{$dbi->insert(table => 'table1', noexist => 1)}; |
231 |
like($@, qr/noexist/, "$test: invalid name"); |
|
232 | ||
233 | ||
removed register_format()
|
234 |
test 'update'; |
235 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
236 |
$dbi->execute($CREATE_TABLE->{1}); |
|
237 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
238 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
239 |
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}); |
|
240 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
241 |
$rows = $result->fetch_hash_all; |
|
242 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
243 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
244 |
"$test : basic"); |
|
245 |
|
|
246 |
$dbi->execute("delete from table1"); |
|
247 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
248 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
249 |
$dbi->update(table => 'table1', param => {key2 => 12}, where => {key2 => 2, key3 => 3}); |
|
250 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
251 |
$rows = $result->fetch_hash_all; |
|
252 |
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5}, |
|
253 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
254 |
"$test : update key same as search key"); |
|
255 | ||
256 |
$dbi->execute("delete from table1"); |
|
257 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
258 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
259 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
260 |
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}, |
|
261 |
filter => {key2 => 'twice'}); |
|
262 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
263 |
$rows = $result->fetch_hash_all; |
|
264 |
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5}, |
|
265 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
266 |
"$test : filter"); |
|
267 | ||
268 |
$result = $dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}, append => ' '); |
|
269 | ||
changed argument of tag proc...
|
270 |
eval{$dbi->update(table => 'table1', noexist => 1)}; |
271 |
like($@, qr/noexist/, "$test: invalid name"); |
|
272 | ||
273 |
eval{$dbi->update(table => 'table1')}; |
|
274 |
like($@, qr/where/, "$test: not contain where"); |
|
275 | ||
276 | ||
removed register_format()
|
277 |
test 'update_all'; |
278 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
279 |
$dbi->execute($CREATE_TABLE->{1}); |
|
280 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
281 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
282 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
283 |
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'}); |
|
284 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
285 |
$rows = $result->fetch_hash_all; |
|
286 |
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5}, |
|
287 |
{key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}], |
|
288 |
"$test : filter"); |
|
289 | ||
290 | ||
291 |
test 'delete'; |
|
292 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
293 |
$dbi->execute($CREATE_TABLE->{0}); |
|
294 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
295 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
296 |
$dbi->delete(table => 'table1', where => {key1 => 1}); |
|
297 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
298 |
$rows = $result->fetch_hash_all; |
|
299 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : basic"); |
|
300 | ||
301 |
$dbi->execute("delete from table1;"); |
|
302 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
303 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
304 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
305 |
$dbi->delete(table => 'table1', where => {key2 => 1}, filter => {key2 => 'twice'}); |
|
306 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
307 |
$rows = $result->fetch_hash_all; |
|
308 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : filter"); |
|
309 | ||
310 |
$dbi->delete(table => 'table1', where => {key1 => 1}, append => ' '); |
|
311 | ||
312 |
$dbi->delete_all(table => 'table1'); |
|
313 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
314 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
315 |
$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2}); |
|
316 |
$rows = $dbi->select(table => 'table1')->fetch_hash_all; |
|
317 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : delete multi key"); |
|
318 | ||
changed argument of tag proc...
|
319 |
eval{$dbi->delete(table => 'table1', noexist => 1)}; |
320 |
like($@, qr/noexist/, "$test: invalid name"); |
|
321 | ||
322 | ||
removed register_format()
|
323 |
test 'delete error'; |
324 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
325 |
$dbi->execute($CREATE_TABLE->{0}); |
|
326 |
eval{$dbi->delete(table => 'table1')}; |
|
changed argument of tag proc...
|
327 |
like($@, qr/Key-value pairs for where clause must be specified to "delete" second argument/, |
removed register_format()
|
328 |
"$test : where key-value pairs not specified"); |
329 | ||
330 |
test 'delete_all'; |
|
331 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
332 |
$dbi->execute($CREATE_TABLE->{0}); |
|
333 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
334 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
335 |
$dbi->delete_all(table => 'table1'); |
|
336 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
337 |
$rows = $result->fetch_hash_all; |
|
338 |
is_deeply($rows, [], "$test : basic"); |
|
339 | ||
340 | ||
341 |
test 'select'; |
|
342 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
343 |
$dbi->execute($CREATE_TABLE->{0}); |
|
344 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
345 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
346 |
$rows = $dbi->select(table => 'table1')->fetch_hash_all; |
|
347 |
is_deeply($rows, [{key1 => 1, key2 => 2}, |
|
348 |
{key1 => 3, key2 => 4}], "$test : table"); |
|
349 | ||
update document
|
350 |
$rows = $dbi->select(table => 'table1', column => ['key1'])->fetch_hash_all; |
removed register_format()
|
351 |
is_deeply($rows, [{key1 => 1}, {key1 => 3}], "$test : table and columns and where key"); |
352 | ||
353 |
$rows = $dbi->select(table => 'table1', where => {key1 => 1})->fetch_hash_all; |
|
354 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : table and columns and where key"); |
|
355 | ||
update document
|
356 |
$rows = $dbi->select(table => 'table1', where => ['{= key1} and {= key2}', {key1 => 1, key2 => 2}])->fetch_hash_all; |
357 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : table and columns and where string"); |
|
358 | ||
update document
|
359 |
$rows = $dbi->select(table => 'table1', column => ['key1'], where => {key1 => 3})->fetch_hash_all; |
removed register_format()
|
360 |
is_deeply($rows, [{key1 => 3}], "$test : table and columns and where key"); |
361 | ||
362 |
$rows = $dbi->select(table => 'table1', append => "order by key1 desc limit 1")->fetch_hash_all; |
|
363 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : append statement"); |
|
364 | ||
365 |
$dbi->register_filter(decrement => sub { $_[0] - 1 }); |
|
update document
|
366 |
$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'}) |
removed register_format()
|
367 |
->fetch_hash_all; |
368 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : filter"); |
|
369 | ||
370 |
$dbi->execute($CREATE_TABLE->{2}); |
|
371 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
372 |
$rows = $dbi->select( |
|
373 |
table => [qw/table1 table2/], |
|
update document
|
374 |
column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'], |
removed register_format()
|
375 |
where => {'table1.key2' => 2}, |
added commit method
|
376 |
relation => {'table1.key1' => 'table2.key1'} |
removed register_format()
|
377 |
)->fetch_hash_all; |
added commit method
|
378 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "$test : relation : exists where"); |
379 | ||
380 |
$rows = $dbi->select( |
|
381 |
table => [qw/table1 table2/], |
|
382 |
column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'], |
|
383 |
relation => {'table1.key1' => 'table2.key1'} |
|
384 |
)->fetch_hash_all; |
|
385 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "$test : relation : no exists where"); |
|
removed register_format()
|
386 | |
changed argument of tag proc...
|
387 |
eval{$dbi->select(table => 'table1', noexist => 1)}; |
388 |
like($@, qr/noexist/, "$test: invalid name"); |
|
389 | ||
390 | ||
removed register_format()
|
391 |
test 'fetch filter'; |
392 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
393 |
$dbi->register_filter( |
|
394 |
twice => sub { $_[0] * 2 }, |
|
395 |
three_times => sub { $_[0] * 3 } |
|
396 |
); |
|
397 |
$dbi->default_fetch_filter('twice'); |
|
398 |
$dbi->execute($CREATE_TABLE->{0}); |
|
399 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
400 |
$result = $dbi->select(table => 'table1'); |
|
401 |
$result->filter({key1 => 'three_times'}); |
|
removed reconnect method
|
402 |
$row = $result->fetch_hash_first; |
removed register_format()
|
403 |
is_deeply($row, {key1 => 3, key2 => 4}, "$test: default_fetch_filter and filter"); |
404 | ||
405 |
test 'filters'; |
|
406 |
$dbi = DBIx::Custom->new; |
|
407 | ||
update document
|
408 |
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')), |
409 |
'あ', "$test : decode_utf8"); |
|
removed register_format()
|
410 | |
411 |
is($dbi->filters->{encode_utf8}->('あ'), |
|
412 |
encode_utf8('あ'), "$test : encode_utf8"); |
|
413 | ||
added commit method
|
414 |
test 'transaction'; |
415 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
416 |
$dbi->execute($CREATE_TABLE->{0}); |
|
removed DBIx::Custom commit ...
|
417 |
$dbi->dbh->begin_work; |
added commit method
|
418 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
419 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
removed DBIx::Custom commit ...
|
420 |
$dbi->dbh->commit; |
added commit method
|
421 |
$result = $dbi->select(table => 'table1'); |
422 |
is_deeply(scalar $result->fetch_hash_all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}], |
|
423 |
"$test : commit"); |
|
424 | ||
425 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
426 |
$dbi->execute($CREATE_TABLE->{0}); |
|
removed DBIx::Custom commit ...
|
427 |
$dbi->dbh->begin_work(0); |
added commit method
|
428 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
removed DBIx::Custom commit ...
|
429 |
$dbi->dbh->rollback; |
added commit method
|
430 | |
431 |
$result = $dbi->select(table => 'table1'); |
|
removed reconnect method
|
432 |
ok(! $result->fetch_first, "$test: rollback"); |
add cache attribute
|
433 | |
434 |
test 'cache'; |
|
435 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
436 |
$dbi->execute($CREATE_TABLE->{0}); |
|
renamed build_query to creat...
|
437 |
$source = 'select * from table1 where {= key1} and {= key2};'; |
438 |
$dbi->create_query($source); |
|
439 |
is_deeply($dbi->{_cached}->{$source}, |
|
add cache attribute
|
440 |
{sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2']}, "$test : cache"); |
441 | ||
442 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
443 |
$dbi->execute($CREATE_TABLE->{0}); |
|
444 |
$dbi->{_cached} = {}; |
|
445 |
$dbi->cache(0); |
|
446 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
447 |
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache'); |
|
448 | ||
changed argument of tag proc...
|
449 | |
added check_filter attribute
|
450 |
test 'filter_check in fetching'; |
451 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
452 |
$dbi->execute($CREATE_TABLE->{0}); |
|
453 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
454 |
$dbi->default_fetch_filter('not_exists'); |
|
455 |
$result = $dbi->select(table => 'table1'); |
|
456 |
eval{$result->fetch_first}; |
|
457 |
like($@, qr/\QDefault fetch filter "not_exists" is not registered/, "$test : array :default_fetch_filter"); |
|
458 | ||
459 |
$dbi->default_fetch_filter(undef); |
|
460 |
$result = $dbi->select(table => 'table1'); |
|
461 |
$result->filter({key1 => 'not_exists'}); |
|
462 |
eval{$result->fetch_first}; |
|
463 |
like($@, qr/\QFetch filter "not_exists" is not registered/, "$test : array :fetch_filter"); |
|
464 | ||
465 |
$result = $dbi->select(table => 'table1'); |
|
466 |
$result->filter({not_exists => 'encode_utf8'}); |
|
467 |
eval{$result->fetch_first}; |
|
468 |
like($@, qr/\QColumn name "not_exists" in fetch filter is not found in result columns/, |
|
469 |
"$test : array : fetch_filter"); |
|
470 | ||
471 |
$result = $dbi->select(table => 'table1'); |
|
472 |
$result->filter({Key1 => 'encode_utf8'}); |
|
473 |
eval{$result->fetch_first}; |
|
474 |
like($@, qr/\QColumn name "Key1" in fetch filter must lower case string/, |
|
475 |
"$test : array : contain upper case charactor"); |
|
476 | ||
477 |
$dbi->filter_check(0); |
|
478 |
$result = $dbi->select(table => 'table1'); |
|
479 |
$result->filter({Key1 => 'encode_utf8'}); |
|
480 |
eval{$result->fetch_first}; |
|
481 |
ok(!$@, "$test : array : filter_check off"); |
|
482 | ||
483 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
484 |
$dbi->execute($CREATE_TABLE->{0}); |
|
485 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
486 |
$dbi->default_fetch_filter('not_exists'); |
|
487 |
$result = $dbi->select(table => 'table1'); |
|
488 |
eval{$result->fetch_hash_first}; |
|
489 |
like($@, qr/\QDefault fetch filter "not_exists" is not registered/, "$test : hash :default_fetch_filter"); |
|
490 | ||
491 |
$dbi->default_fetch_filter(undef); |
|
492 |
$result = $dbi->select(table => 'table1'); |
|
493 |
$result->filter({key1 => 'not_exists'}); |
|
494 |
eval{$result->fetch_hash_first}; |
|
495 |
like($@, qr/\QFetch filter "not_exists" is not registered/, "$test : hash :fetch_filter"); |
|
496 | ||
497 |
$result = $dbi->select(table => 'table1'); |
|
498 |
$result->filter({not_exists => 'encode_utf8'}); |
|
499 |
eval{$result->fetch_hash_first}; |
|
500 |
like($@, qr/\QColumn name "not_exists" in fetch filter is not found in result columns/, |
|
501 |
"$test : hash : fetch_filter"); |
|
502 | ||
503 |
$result = $dbi->select(table => 'table1'); |
|
504 |
$result->filter({Key1 => 'encode_utf8'}); |
|
505 |
eval{$result->fetch_hash_first}; |
|
506 |
like($@, qr/\QColumn name "Key1" in fetch filter must lower case string/, |
|
507 |
"$test : hash : contain upper case charactor"); |
|
508 | ||
509 |
$dbi->filter_check(0); |
|
510 |
$result = $dbi->select(table => 'table1'); |
|
511 |
$result->filter({Key1 => 'encode_utf8'}); |
|
512 |
eval{$result->fetch_hash_first}; |
|
513 |
ok(!$@, "$test : hash : filter_check off"); |
|
514 | ||
515 | ||
add tests
|
516 |
test 'filter_check in parameter binding'; |
added check_filter attribute
|
517 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
518 |
$dbi->execute($CREATE_TABLE->{0}); |
|
519 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
520 | ||
add tests
|
521 |
$dbi->default_bind_filter('not_exists'); |
522 |
eval{$dbi->select(table => 'table1')}; |
|
523 |
like($@, qr/\QDefault bind filter "not_exists" is not registered/, "$test : default_bind_filter"); |
|
added check_filter attribute
|
524 | |
add tests
|
525 |
$dbi->default_bind_filter(undef); |
526 |
eval{$dbi->select(table => 'table1', filter => {key1 => 'not_exists'})}; |
|
527 |
like($@, qr/\QBind filter "not_exists" is not registered/, "$test : bind_filter"); |
|
added check_filter attribute
|
528 | |
add tests
|
529 |
eval{$dbi->select(table => 'table1', filter => {not_exists => 'encode_utf8'})}; |
530 |
like($@, qr/\QColumn name "not_exists" in bind filter is not found in paramters/, |
|
531 |
"$test : fetch_filter"); |
|
added check_filter attribute
|
532 |