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));', |
|
added experimental iterate_a...
|
28 |
2 => 'create table table2 (key1 char(255), key3 char(255));', |
29 |
3 => 'create table table1 (key1 Date, key2 datetime);' |
|
removed register_format()
|
30 |
}; |
31 | ||
add tests
|
32 |
my $SELECT_SOURCES = { |
removed register_format()
|
33 |
0 => 'select * from table1;' |
34 |
}; |
|
35 | ||
36 |
my $DROP_TABLE = { |
|
37 |
0 => 'drop table table1' |
|
38 |
}; |
|
39 | ||
40 |
my $NEW_ARGS = { |
|
41 |
0 => {data_source => 'dbi:SQLite:dbname=:memory:'} |
|
42 |
}; |
|
43 | ||
44 |
# Variables |
|
45 |
my $dbi; |
|
46 |
my $sth; |
|
renamed build_query to creat...
|
47 |
my $source; |
48 |
my @sources; |
|
add tests
|
49 |
my $select_SOURCE; |
50 |
my $insert_SOURCE; |
|
51 |
my $update_SOURCE; |
|
removed register_format()
|
52 |
my $params; |
53 |
my $sql; |
|
54 |
my $result; |
|
55 |
my $row; |
|
56 |
my @rows; |
|
57 |
my $rows; |
|
58 |
my $query; |
|
59 |
my @queries; |
|
60 |
my $select_query; |
|
61 |
my $insert_query; |
|
62 |
my $update_query; |
|
63 |
my $ret_val; |
|
added experimental iterate_a...
|
64 |
my $infos; |
added experimental DBIx::Cus...
|
65 |
my $table; |
removed register_format()
|
66 | |
67 |
# Prepare table |
|
68 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
69 |
$dbi->execute($CREATE_TABLE->{0}); |
|
70 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
71 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
72 | ||
73 |
test 'DBIx::Custom::Result test'; |
|
renamed build_query to creat...
|
74 |
$source = "select key1, key2 from table1"; |
75 |
$query = $dbi->create_query($source); |
|
removed register_format()
|
76 |
$result = $dbi->execute($query); |
77 | ||
78 |
@rows = (); |
|
79 |
while (my $row = $result->fetch) { |
|
80 |
push @rows, [@$row]; |
|
81 |
} |
|
removed reconnect method
|
82 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch"); |
removed register_format()
|
83 | |
84 |
$result = $dbi->execute($query); |
|
85 |
@rows = (); |
|
86 |
while (my $row = $result->fetch_hash) { |
|
87 |
push @rows, {%$row}; |
|
88 |
} |
|
removed reconnect method
|
89 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch_hash"); |
removed register_format()
|
90 | |
91 |
$result = $dbi->execute($query); |
|
92 |
$rows = $result->fetch_all; |
|
removed reconnect method
|
93 |
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all"); |
removed register_format()
|
94 | |
95 |
$result = $dbi->execute($query); |
|
removed reconnect method
|
96 |
$rows = $result->fetch_hash_all; |
97 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch_hash_all"); |
|
removed register_format()
|
98 | |
99 |
test 'Insert query return value'; |
|
100 |
$dbi->execute($DROP_TABLE->{0}); |
|
101 |
$dbi->execute($CREATE_TABLE->{0}); |
|
renamed update tag to update...
|
102 |
$source = "insert into table1 {insert_param key1 key2}"; |
renamed build_query to creat...
|
103 |
$query = $dbi->create_query($source); |
removed register_format()
|
104 |
$ret_val = $dbi->execute($query, param => {key1 => 1, key2 => 2}); |
105 |
ok($ret_val, $test); |
|
106 | ||
107 | ||
108 |
test 'Direct query'; |
|
109 |
$dbi->execute($DROP_TABLE->{0}); |
|
110 |
$dbi->execute($CREATE_TABLE->{0}); |
|
add tests
|
111 |
$insert_SOURCE = "insert into table1 {insert_param key1 key2}"; |
112 |
$dbi->execute($insert_SOURCE, param => {key1 => 1, key2 => 2}); |
|
113 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
removed register_format()
|
114 |
$rows = $result->fetch_hash_all; |
115 |
is_deeply($rows, [{key1 => 1, key2 => 2}], $test); |
|
116 | ||
117 |
test 'Filter basic'; |
|
118 |
$dbi->execute($DROP_TABLE->{0}); |
|
119 |
$dbi->execute($CREATE_TABLE->{0}); |
|
120 |
$dbi->register_filter(twice => sub { $_[0] * 2}, |
|
121 |
three_times => sub { $_[0] * 3}); |
|
122 | ||
add tests
|
123 |
$insert_SOURCE = "insert into table1 {insert_param key1 key2};"; |
124 |
$insert_query = $dbi->create_query($insert_SOURCE); |
|
removed register_format()
|
125 |
$insert_query->filter({key1 => 'twice'}); |
126 |
$dbi->execute($insert_query, param => {key1 => 1, key2 => 2}); |
|
add tests
|
127 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
128 |
$rows = $result->filter({key2 => 'three_times'})->fetch_hash_all; |
129 |
is_deeply($rows, [{key1 => 2, key2 => 6}], "$test : filter fetch_filter"); |
|
130 |
$dbi->execute($DROP_TABLE->{0}); |
|
131 | ||
132 |
test 'Filter in'; |
|
133 |
$dbi->execute($CREATE_TABLE->{0}); |
|
add tests
|
134 |
$insert_SOURCE = "insert into table1 {insert_param key1 key2};"; |
135 |
$insert_query = $dbi->create_query($insert_SOURCE); |
|
removed register_format()
|
136 |
$dbi->execute($insert_query, param => {key1 => 2, key2 => 4}); |
add tests
|
137 |
$select_SOURCE = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}"; |
138 |
$select_query = $dbi->create_query($select_SOURCE); |
|
removed register_format()
|
139 |
$select_query->filter({'table1.key1' => 'twice'}); |
140 |
$result = $dbi->execute($select_query, param => {'table1.key1' => [1,5], 'table1.key2' => [2,4]}); |
|
141 |
$rows = $result->fetch_hash_all; |
|
142 |
is_deeply($rows, [{key1 => 2, key2 => 4}], "$test : filter"); |
|
143 | ||
144 |
test 'DBIx::Custom::SQLTemplate basic tag'; |
|
145 |
$dbi->execute($DROP_TABLE->{0}); |
|
146 |
$dbi->execute($CREATE_TABLE->{1}); |
|
147 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
148 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
149 | ||
renamed build_query to creat...
|
150 |
$source = "select * from table1 where {= key1} and {<> key2} and {< key3} and {> key4} and {>= key5};"; |
151 |
$query = $dbi->create_query($source); |
|
removed register_format()
|
152 |
$result = $dbi->execute($query, param => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5}); |
153 |
$rows = $result->fetch_hash_all; |
|
154 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1"); |
|
155 | ||
renamed build_query to creat...
|
156 |
$source = "select * from table1 where {<= key1} and {like key2};"; |
157 |
$query = $dbi->create_query($source); |
|
removed register_format()
|
158 |
$result = $dbi->execute($query, param => {key1 => 1, key2 => '%2%'}); |
159 |
$rows = $result->fetch_hash_all; |
|
160 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2"); |
|
161 | ||
162 |
test 'DIB::Custom::SQLTemplate in tag'; |
|
163 |
$dbi->execute($DROP_TABLE->{0}); |
|
164 |
$dbi->execute($CREATE_TABLE->{1}); |
|
165 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
166 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
167 | ||
renamed build_query to creat...
|
168 |
$source = "select * from table1 where {in key1 2};"; |
169 |
$query = $dbi->create_query($source); |
|
removed register_format()
|
170 |
$result = $dbi->execute($query, param => {key1 => [9, 1]}); |
171 |
$rows = $result->fetch_hash_all; |
|
172 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic"); |
|
173 | ||
174 |
test 'DBIx::Custom::SQLTemplate insert tag'; |
|
175 |
$dbi->execute("delete from table1"); |
|
add tests
|
176 |
$insert_SOURCE = 'insert into table1 {insert_param key1 key2 key3 key4 key5}'; |
177 |
$dbi->execute($insert_SOURCE, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
removed register_format()
|
178 | |
add tests
|
179 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
180 |
$rows = $result->fetch_hash_all; |
181 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic"); |
|
182 | ||
183 |
test 'DBIx::Custom::SQLTemplate update tag'; |
|
184 |
$dbi->execute("delete from table1"); |
|
add tests
|
185 |
$insert_SOURCE = "insert into table1 {insert_param key1 key2 key3 key4 key5}"; |
186 |
$dbi->execute($insert_SOURCE, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
187 |
$dbi->execute($insert_SOURCE, param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
removed register_format()
|
188 | |
add tests
|
189 |
$update_SOURCE = 'update table1 {update_param key1 key2 key3 key4} where {= key5}'; |
190 |
$dbi->execute($update_SOURCE, param => {key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}); |
|
removed register_format()
|
191 | |
add tests
|
192 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
193 |
$rows = $result->fetch_hash_all; |
194 |
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}, |
|
195 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : basic"); |
|
196 | ||
197 |
test 'Error case'; |
|
198 |
eval {DBIx::Custom->connect(data_source => 'dbi:SQLit')}; |
|
199 |
ok($@, "$test : connect error"); |
|
200 | ||
201 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
renamed build_query to creat...
|
202 |
eval{$dbi->create_query("{p }")}; |
203 |
ok($@, "$test : create_query invalid SQL template"); |
|
removed register_format()
|
204 | |
205 |
test 'insert'; |
|
206 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
207 |
$dbi->execute($CREATE_TABLE->{0}); |
|
208 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
209 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
add tests
|
210 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
211 |
$rows = $result->fetch_hash_all; |
212 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : basic"); |
|
213 | ||
214 |
$dbi->execute('delete from table1'); |
|
215 |
$dbi->register_filter( |
|
216 |
twice => sub { $_[0] * 2 }, |
|
217 |
three_times => sub { $_[0] * 3 } |
|
218 |
); |
|
renamed default_query_filter...
|
219 |
$dbi->default_bind_filter('twice'); |
removed register_format()
|
220 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => 'three_times'}); |
add tests
|
221 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
222 |
$rows = $result->fetch_hash_all; |
223 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : filter"); |
|
renamed default_query_filter...
|
224 |
$dbi->default_bind_filter(undef); |
removed register_format()
|
225 | |
226 |
$dbi->execute($DROP_TABLE->{0}); |
|
227 |
$dbi->execute($CREATE_TABLE->{0}); |
|
228 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, append => ' '); |
|
229 |
$rows = $dbi->select(table => 'table1')->fetch_hash_all; |
|
230 |
is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append'); |
|
231 | ||
changed argument of tag proc...
|
232 |
eval{$dbi->insert(table => 'table1', noexist => 1)}; |
add tests
|
233 |
like($@, qr/noexist/, "$test: invalid argument"); |
changed argument of tag proc...
|
234 | |
235 | ||
removed register_format()
|
236 |
test 'update'; |
237 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
238 |
$dbi->execute($CREATE_TABLE->{1}); |
|
239 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
240 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
241 |
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}); |
|
add tests
|
242 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
243 |
$rows = $result->fetch_hash_all; |
244 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
245 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
246 |
"$test : basic"); |
|
247 |
|
|
248 |
$dbi->execute("delete from table1"); |
|
249 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
250 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
251 |
$dbi->update(table => 'table1', param => {key2 => 12}, where => {key2 => 2, key3 => 3}); |
|
add tests
|
252 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
253 |
$rows = $result->fetch_hash_all; |
254 |
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5}, |
|
255 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
256 |
"$test : update key same as search key"); |
|
257 | ||
add tests
|
258 |
$dbi->update(table => 'table1', param => {key2 => [12]}, where => {key2 => 2, key3 => 3}); |
259 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
260 |
$rows = $result->fetch_hash_all; |
|
261 |
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5}, |
|
262 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
263 |
"$test : update key same as search key : param is array ref"); |
|
264 | ||
removed register_format()
|
265 |
$dbi->execute("delete from table1"); |
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->register_filter(twice => sub { $_[0] * 2 }); |
|
269 |
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}, |
|
270 |
filter => {key2 => 'twice'}); |
|
add tests
|
271 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
272 |
$rows = $result->fetch_hash_all; |
273 |
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5}, |
|
274 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
275 |
"$test : filter"); |
|
276 | ||
277 |
$result = $dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}, append => ' '); |
|
278 | ||
changed argument of tag proc...
|
279 |
eval{$dbi->update(table => 'table1', noexist => 1)}; |
add tests
|
280 |
like($@, qr/noexist/, "$test: invalid argument"); |
changed argument of tag proc...
|
281 | |
282 |
eval{$dbi->update(table => 'table1')}; |
|
283 |
like($@, qr/where/, "$test: not contain where"); |
|
284 | ||
285 | ||
removed register_format()
|
286 |
test 'update_all'; |
287 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
288 |
$dbi->execute($CREATE_TABLE->{1}); |
|
289 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
290 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
291 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
292 |
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'}); |
|
add tests
|
293 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
294 |
$rows = $result->fetch_hash_all; |
295 |
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5}, |
|
296 |
{key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}], |
|
297 |
"$test : filter"); |
|
298 | ||
299 | ||
300 |
test 'delete'; |
|
301 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
302 |
$dbi->execute($CREATE_TABLE->{0}); |
|
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}); |
|
add tests
|
306 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
307 |
$rows = $result->fetch_hash_all; |
308 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : basic"); |
|
309 | ||
310 |
$dbi->execute("delete from table1;"); |
|
311 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
312 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
313 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
314 |
$dbi->delete(table => 'table1', where => {key2 => 1}, filter => {key2 => 'twice'}); |
|
add tests
|
315 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
316 |
$rows = $result->fetch_hash_all; |
317 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : filter"); |
|
318 | ||
319 |
$dbi->delete(table => 'table1', where => {key1 => 1}, append => ' '); |
|
320 | ||
321 |
$dbi->delete_all(table => 'table1'); |
|
322 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
323 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
324 |
$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2}); |
|
325 |
$rows = $dbi->select(table => 'table1')->fetch_hash_all; |
|
326 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : delete multi key"); |
|
327 | ||
changed argument of tag proc...
|
328 |
eval{$dbi->delete(table => 'table1', noexist => 1)}; |
add tests
|
329 |
like($@, qr/noexist/, "$test: invalid argument"); |
changed argument of tag proc...
|
330 | |
331 | ||
removed register_format()
|
332 |
test 'delete error'; |
333 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
334 |
$dbi->execute($CREATE_TABLE->{0}); |
|
335 |
eval{$dbi->delete(table => 'table1')}; |
|
add tests
|
336 |
like($@, qr/"where" argument must be specified and contains the pairs of column name and value/, |
removed register_format()
|
337 |
"$test : where key-value pairs not specified"); |
338 | ||
339 |
test 'delete_all'; |
|
340 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
341 |
$dbi->execute($CREATE_TABLE->{0}); |
|
342 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
343 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
344 |
$dbi->delete_all(table => 'table1'); |
|
add tests
|
345 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
346 |
$rows = $result->fetch_hash_all; |
347 |
is_deeply($rows, [], "$test : basic"); |
|
348 | ||
349 | ||
350 |
test 'select'; |
|
351 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
352 |
$dbi->execute($CREATE_TABLE->{0}); |
|
353 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
354 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
355 |
$rows = $dbi->select(table => 'table1')->fetch_hash_all; |
|
356 |
is_deeply($rows, [{key1 => 1, key2 => 2}, |
|
357 |
{key1 => 3, key2 => 4}], "$test : table"); |
|
358 | ||
update document
|
359 |
$rows = $dbi->select(table => 'table1', column => ['key1'])->fetch_hash_all; |
removed register_format()
|
360 |
is_deeply($rows, [{key1 => 1}, {key1 => 3}], "$test : table and columns and where key"); |
361 | ||
362 |
$rows = $dbi->select(table => 'table1', where => {key1 => 1})->fetch_hash_all; |
|
363 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : table and columns and where key"); |
|
364 | ||
update document
|
365 |
$rows = $dbi->select(table => 'table1', where => ['{= key1} and {= key2}', {key1 => 1, key2 => 2}])->fetch_hash_all; |
366 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : table and columns and where string"); |
|
367 | ||
update document
|
368 |
$rows = $dbi->select(table => 'table1', column => ['key1'], where => {key1 => 3})->fetch_hash_all; |
removed register_format()
|
369 |
is_deeply($rows, [{key1 => 3}], "$test : table and columns and where key"); |
370 | ||
371 |
$rows = $dbi->select(table => 'table1', append => "order by key1 desc limit 1")->fetch_hash_all; |
|
372 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : append statement"); |
|
373 | ||
374 |
$dbi->register_filter(decrement => sub { $_[0] - 1 }); |
|
update document
|
375 |
$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'}) |
removed register_format()
|
376 |
->fetch_hash_all; |
377 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : filter"); |
|
378 | ||
379 |
$dbi->execute($CREATE_TABLE->{2}); |
|
380 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
381 |
$rows = $dbi->select( |
|
382 |
table => [qw/table1 table2/], |
|
update document
|
383 |
column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'], |
removed register_format()
|
384 |
where => {'table1.key2' => 2}, |
added commit method
|
385 |
relation => {'table1.key1' => 'table2.key1'} |
removed register_format()
|
386 |
)->fetch_hash_all; |
added commit method
|
387 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "$test : relation : exists where"); |
388 | ||
389 |
$rows = $dbi->select( |
|
390 |
table => [qw/table1 table2/], |
|
391 |
column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'], |
|
392 |
relation => {'table1.key1' => 'table2.key1'} |
|
393 |
)->fetch_hash_all; |
|
394 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "$test : relation : no exists where"); |
|
removed register_format()
|
395 | |
changed argument of tag proc...
|
396 |
eval{$dbi->select(table => 'table1', noexist => 1)}; |
add tests
|
397 |
like($@, qr/noexist/, "$test: invalid argument"); |
changed argument of tag proc...
|
398 | |
399 | ||
removed register_format()
|
400 |
test 'fetch filter'; |
401 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
402 |
$dbi->register_filter( |
|
403 |
twice => sub { $_[0] * 2 }, |
|
404 |
three_times => sub { $_[0] * 3 } |
|
405 |
); |
|
406 |
$dbi->default_fetch_filter('twice'); |
|
407 |
$dbi->execute($CREATE_TABLE->{0}); |
|
408 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
409 |
$result = $dbi->select(table => 'table1'); |
|
410 |
$result->filter({key1 => 'three_times'}); |
|
removed reconnect method
|
411 |
$row = $result->fetch_hash_first; |
removed register_format()
|
412 |
is_deeply($row, {key1 => 3, key2 => 4}, "$test: default_fetch_filter and filter"); |
413 | ||
414 |
test 'filters'; |
|
415 |
$dbi = DBIx::Custom->new; |
|
416 | ||
update document
|
417 |
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')), |
418 |
'あ', "$test : decode_utf8"); |
|
removed register_format()
|
419 | |
420 |
is($dbi->filters->{encode_utf8}->('あ'), |
|
421 |
encode_utf8('あ'), "$test : encode_utf8"); |
|
422 | ||
added commit method
|
423 |
test 'transaction'; |
424 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
425 |
$dbi->execute($CREATE_TABLE->{0}); |
|
removed DBIx::Custom commit ...
|
426 |
$dbi->dbh->begin_work; |
added commit method
|
427 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
428 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
removed DBIx::Custom commit ...
|
429 |
$dbi->dbh->commit; |
added commit method
|
430 |
$result = $dbi->select(table => 'table1'); |
431 |
is_deeply(scalar $result->fetch_hash_all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}], |
|
432 |
"$test : commit"); |
|
433 | ||
434 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
435 |
$dbi->execute($CREATE_TABLE->{0}); |
|
removed DBIx::Custom commit ...
|
436 |
$dbi->dbh->begin_work(0); |
added commit method
|
437 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
removed DBIx::Custom commit ...
|
438 |
$dbi->dbh->rollback; |
added commit method
|
439 | |
440 |
$result = $dbi->select(table => 'table1'); |
|
removed reconnect method
|
441 |
ok(! $result->fetch_first, "$test: rollback"); |
add cache attribute
|
442 | |
443 |
test 'cache'; |
|
444 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
445 |
$dbi->execute($CREATE_TABLE->{0}); |
|
renamed build_query to creat...
|
446 |
$source = 'select * from table1 where {= key1} and {= key2};'; |
447 |
$dbi->create_query($source); |
|
448 |
is_deeply($dbi->{_cached}->{$source}, |
|
add cache attribute
|
449 |
{sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2']}, "$test : cache"); |
450 | ||
451 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
452 |
$dbi->execute($CREATE_TABLE->{0}); |
|
453 |
$dbi->{_cached} = {}; |
|
454 |
$dbi->cache(0); |
|
455 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
456 |
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache'); |
|
457 | ||
add tests
|
458 |
test 'execute'; |
459 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
460 |
$dbi->execute($CREATE_TABLE->{0}); |
|
removed experimental registe...
|
461 |
{ |
462 |
local $Carp::Verbose = 0; |
|
463 |
eval{$dbi->execute('select * frm table1')}; |
|
464 |
like($@, qr/\Qselect * frm table1;/, "$test : fail prepare"); |
|
465 |
like($@, qr/\.t /, "$test: fail : not verbose"); |
|
466 |
} |
|
467 |
{ |
|
468 |
local $Carp::Verbose = 1; |
|
469 |
eval{$dbi->execute('select * frm table1')}; |
|
470 |
like($@, qr/Custom.*\.t /s, "$test : fail : verbose"); |
|
471 |
} |
|
add tests
|
472 | |
473 |
eval{$dbi->execute('select * from table1', no_exists => 1)}; |
|
474 |
like($@, qr/\Q"no_exists" is invalid argument/, "$test : invald SQL"); |
|
475 | ||
476 |
$query = $dbi->create_query('select * from table1 where {= key1}'); |
|
477 |
$dbi->dbh->disconnect; |
|
478 |
eval{$dbi->execute($query, param => {key1 => {a => 1}})}; |
|
479 |
ok($@, "$test: execute fail"); |
|
480 | ||
removed experimental registe...
|
481 |
{ |
482 |
local $Carp::Verbose = 0; |
|
483 |
eval{$dbi->create_query('select * from table1 where {0 key1}')}; |
|
484 |
like($@, qr/\Q.t /, "$test : caller spec : not vebose"); |
|
485 |
} |
|
486 |
{ |
|
487 |
local $Carp::Verbose = 1; |
|
488 |
eval{$dbi->create_query('select * from table1 where {0 key1}')}; |
|
489 |
like($@, qr/QueryBuilder.*\.t /s, "$test : caller spec : not vebose"); |
|
490 |
} |
|
cleanup
|
491 | |
492 | ||
493 |
test 'transaction'; |
|
494 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
495 |
$dbi->execute($CREATE_TABLE->{0}); |
|
496 | ||
497 |
$dbi->begin_work; |
|
498 | ||
499 |
eval { |
|
500 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
501 |
die "Error"; |
|
502 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
503 |
}; |
|
504 | ||
505 |
$dbi->rollback if $@; |
|
506 | ||
507 |
$result = $dbi->select(table => 'table1'); |
|
508 |
$rows = $result->fetch_hash_all; |
|
509 |
is_deeply($rows, [], "$test : rollback"); |
|
510 | ||
511 |
$dbi->begin_work; |
|
512 | ||
513 |
eval { |
|
514 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
515 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
516 |
}; |
|
517 | ||
518 |
$dbi->commit unless $@; |
|
519 | ||
520 |
$result = $dbi->select(table => 'table1'); |
|
521 |
$rows = $result->fetch_hash_all; |
|
522 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : commit"); |
|
523 | ||
524 |
$dbi->dbh->{AutoCommit} = 0; |
|
525 |
eval{ $dbi->begin_work }; |
|
526 |
ok($@, "$test : exception"); |
|
527 |
$dbi->dbh->{AutoCommit} = 1; |
|
528 | ||
added helper method
|
529 | |
530 |
test 'helper'; |
|
531 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
532 |
$dbi->helper( |
|
533 |
one => sub { 1 } |
|
534 |
); |
|
535 |
$dbi->helper( |
|
536 |
two => sub { 2 } |
|
537 |
); |
|
538 |
$dbi->helper({ |
|
539 |
twice => sub { |
|
540 |
my $self = shift; |
|
541 |
return $_[0] * 2; |
|
542 |
} |
|
543 |
}); |
|
544 | ||
545 |
is($dbi->one, 1, "$test : first"); |
|
546 |
is($dbi->two, 2, "$test : second"); |
|
547 |
is($dbi->twice(5), 10 , "$test : second"); |
|
548 | ||
549 |
eval {$dbi->XXXXXX}; |
|
550 |
like($@, qr/\QCan't locate object method "XXXXXX" via "DBIx::Custom"/, "$test : not exists"); |
|
551 | ||
added auto_filter method
|
552 |
test 'auto bind filter'; |
553 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
554 |
$dbi->execute($CREATE_TABLE->{0}); |
|
555 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
556 |
$dbi->register_filter(three_times => sub { $_[0] * 3}); |
|
557 |
$dbi->auto_filter( |
|
558 |
'table1', ['key1', 'twice', 'twice'], ['key2', 'three_times', 'three_times']); |
|
559 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
560 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
561 |
$row = $result->fetch_hash_first; |
|
562 |
is_deeply($row, {key1 => 2, key2 => 6}, "$test : insert"); |
|
563 | ||
564 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
565 |
$dbi->execute($CREATE_TABLE->{0}); |
|
566 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
567 |
$dbi->register_filter(three_times => sub { $_[0] * 3}); |
|
568 |
$dbi->auto_filter( |
|
569 |
'table1', ['key1', 'twice', 'twice'], ['key2', 'three_times', 'three_times']); |
|
570 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, auto_filter_table => undef); |
|
571 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
572 |
$row = $result->fetch_hash_first; |
|
573 |
is_deeply($row, {key1 => 1, key2 => 2}, "$test : insert disabe auto_filter_table 1"); |
|
574 | ||
575 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
576 |
$dbi->execute($CREATE_TABLE->{0}); |
|
577 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
578 |
$dbi->register_filter(three_times => sub { $_[0] * 3}); |
|
579 |
$dbi->auto_filter( |
|
580 |
'table1', ['key1', 'twice', 'twice'], ['key2', 'three_times', 'three_times']); |
|
581 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, auto_filter_table => []); |
|
582 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
583 |
$row = $result->fetch_hash_first; |
|
584 |
is_deeply($row, {key1 => 1, key2 => 2}, "$test : insert disabe auto_filter_table 2"); |
|
585 | ||
586 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
587 |
$dbi->execute($CREATE_TABLE->{0}); |
|
588 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
589 |
$dbi->auto_filter( |
|
590 |
'table1', ['key1', 'twice', 'twice'] |
|
591 |
); |
|
592 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, auto_filter_table => undef); |
|
593 |
$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2}); |
|
594 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
595 |
$row = $result->fetch_hash_first; |
|
596 |
is_deeply($row, {key1 => 4, key2 => 2}, "$test : update"); |
|
597 | ||
598 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
599 |
$dbi->execute($CREATE_TABLE->{0}); |
|
600 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
601 |
$dbi->auto_filter( |
|
602 |
'table1', ['key1', 'twice', 'twice'] |
|
603 |
); |
|
604 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, auto_filter_table => undef); |
|
605 |
$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2}, auto_filter_table => []); |
|
606 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
607 |
$row = $result->fetch_hash_first; |
|
608 |
is_deeply($row, {key1 => 2, key2 => 2}, "$test : update : disable bind filter1"); |
|
609 | ||
610 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
611 |
$dbi->execute($CREATE_TABLE->{0}); |
|
612 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
613 |
$dbi->auto_filter( |
|
614 |
'table1', ['key1', 'twice', 'twice'] |
|
615 |
); |
|
616 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, auto_filter_table => undef); |
|
617 |
$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2}, auto_filter_table => undef); |
|
618 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
619 |
$row = $result->fetch_hash_first; |
|
620 |
is_deeply($row, {key1 => 2, key2 => 2}, "$test : update : disable bind filter2"); |
|
621 | ||
622 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
623 |
$dbi->execute($CREATE_TABLE->{0}); |
|
624 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
625 |
$dbi->auto_filter( |
|
626 |
'table1', ['key1', 'twice', 'twice'] |
|
627 |
); |
|
628 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, auto_filter_table => undef); |
|
629 |
$dbi->delete(table => 'table1', where => {key1 => 1}); |
|
630 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
631 |
$rows = $result->fetch_hash_all; |
|
632 |
is_deeply($rows, [], "$test : delete"); |
|
633 | ||
634 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
635 |
$dbi->execute($CREATE_TABLE->{0}); |
|
636 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
637 |
$dbi->auto_filter( |
|
638 |
'table1', ['key1', 'twice', 'twice'] |
|
639 |
); |
|
640 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, auto_filter_table => undef); |
|
641 |
$dbi->delete(table => 'table1', where => {key1 => 1}, auto_filter_table => undef); |
|
642 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
643 |
$rows = $result->fetch_hash_all; |
|
644 |
is_deeply($rows, [{key1 => 2, key2 => 2}], "$test : delete : disable1"); |
|
645 | ||
646 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
647 |
$dbi->execute($CREATE_TABLE->{0}); |
|
648 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
649 |
$dbi->auto_filter( |
|
650 |
'table1', ['key1', 'twice', 'twice'] |
|
651 |
); |
|
652 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, auto_filter_table => undef); |
|
653 |
$dbi->delete(table => 'table1', where => {key1 => 1}, auto_filter_table => []); |
|
654 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
655 |
$rows = $result->fetch_hash_all; |
|
656 |
is_deeply($rows, [{key1 => 2, key2 => 2}], "$test : delete : disable2"); |
|
657 | ||
658 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
659 |
$dbi->execute($CREATE_TABLE->{0}); |
|
660 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
661 |
$dbi->auto_filter( |
|
662 |
'table1', ['key1', 'twice', 'twice'] |
|
663 |
); |
|
664 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, auto_filter_table => undef); |
|
665 |
$result = $dbi->select(table => 'table1', where => {key1 => 1}); |
|
666 |
$result->filter({'key2' => 'twice'}); |
|
667 |
$rows = $result->fetch_hash_all; |
|
668 |
is_deeply($rows, [{key1 => 4, key2 => 4}], "$test : select"); |
|
669 | ||
670 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
671 |
$dbi->execute($CREATE_TABLE->{0}); |
|
672 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
673 |
$dbi->auto_filter( |
|
674 |
'table1', ['key1', 'twice', 'twice'] |
|
675 |
); |
|
676 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, auto_filter_table => undef); |
|
677 |
$result = $dbi->select(table => 'table1', where => {key1 => 2}, auto_filter_table => []); |
|
678 |
$result->filter({'key2' => 'twice'}); |
|
679 |
$rows = $result->fetch_hash_all; |
|
680 |
is_deeply($rows, [{key1 => 2, key2 => 4}], "$test : select : disable"); |
|
681 | ||
682 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
683 |
$dbi->execute($CREATE_TABLE->{0}); |
|
684 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
685 |
$dbi->auto_filter( |
|
686 |
'table1', ['key1', 'twice', 'twice'] |
|
687 |
); |
|
688 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, auto_filter_table => undef); |
|
689 |
$result = $dbi->execute("select * from table1 where {= key1} and {= key2};", |
|
690 |
param => {key1 => 1, key2 => 2}, |
|
691 |
auto_filter_table => ['table1']); |
|
692 |
$rows = $result->fetch_hash_all; |
|
693 |
is_deeply($rows, [{key1 => 4, key2 => 2}], "$test : execute"); |
|
694 | ||
695 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
696 |
$dbi->execute($CREATE_TABLE->{0}); |
|
697 |
$dbi->execute($CREATE_TABLE->{2}); |
|
698 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
699 |
$dbi->register_filter(three_times => sub { $_[0] * 3 }); |
|
700 |
$dbi->auto_filter( |
|
701 |
'table1', ['key2', 'twice', 'twice'] |
|
702 |
); |
|
703 |
$dbi->auto_filter( |
|
704 |
'table2', ['key3', 'three_times', 'three_times'] |
|
705 |
); |
|
706 |
$dbi->insert(table => 'table1', param => {key1 => 5, key2 => 2}, auto_filter_table => undef); |
|
707 |
$dbi->insert(table => 'table2', param => {key1 => 5, key3 => 6}, auto_filter_table => undef); |
|
708 |
$result = $dbi->select( |
|
709 |
table => ['table1', 'table2'], |
|
710 |
column => ['key2', 'key3'], |
|
711 |
where => {'table1.key2' => 1, 'table2.key3' => 2}, relation => {'table1.key1' => 'table2.key1'}); |
|
712 | ||
713 |
$result->filter({'key2' => 'twice'}); |
|
714 |
$rows = $result->fetch_hash_all; |
|
715 |
is_deeply($rows, [{key2 => 4, key3 => 18}], "$test : select : join"); |
|
716 | ||
717 |
$result = $dbi->select( |
|
718 |
table => ['table1', 'table2'], |
|
719 |
column => ['key2', 'key3'], |
|
720 |
where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'}); |
|
721 | ||
722 |
$result->filter({'key2' => 'twice'}); |
|
723 |
$rows = $result->fetch_hash_all; |
|
724 |
is_deeply($rows, [{key2 => 4, key3 => 18}], "$test : select : join : omit"); |
|
725 | ||
added experimental iterate_a...
|
726 |
test 'auto_filter_easy_build'; |
727 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
728 |
$dbi->execute($CREATE_TABLE->{2}); |
|
729 |
$dbi->execute($CREATE_TABLE->{3}); |
|
730 | ||
731 |
$infos = []; |
|
732 |
$dbi->iterate_all_columns(sub { |
|
733 |
my ($table, $column, $cinfo) = @_; |
|
734 |
|
|
735 |
if ($table =~ /^table/) { |
|
736 |
my $info = [$table, $column, $cinfo->{COLUMN_NAME}]; |
|
737 |
push @$infos, $info; |
|
738 |
} |
|
739 |
}); |
|
740 |
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos]; |
|
741 |
is_deeply($infos, |
|
742 |
[ |
|
743 |
['table1', 'key1', 'key1'], |
|
744 |
['table1', 'key2', 'key2'], |
|
745 |
['table2', 'key1', 'key1'], |
|
746 |
['table2', 'key3', 'key3'] |
|
747 |
] |
|
748 |
, $test |
|
749 |
); |
|
added auto_filter method
|
750 | |
remove DBIx::Custom::Model
|
751 |
test 'table'; |
752 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
753 |
$dbi->execute($CREATE_TABLE->{0}); |
|
754 |
$table = $dbi->table('table1'); |
|
simplified DBIx::Custom::Mod...
|
755 |
$table->insert(param => {key1 => 1, key2 => 2}); |
756 |
$table->insert(param => {key1 => 3, key2 => 4}); |
|
added experimental DBIx::Cus...
|
757 |
$rows = $table->select->fetch_hash_all; |
758 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], |
|
759 |
"$test: select"); |
|
simplified DBIx::Custom::Mod...
|
760 |
$rows = $table->select(where => {key2 => 2}, append => 'order by key1', |
761 |
column => ['key1', 'key2'])->fetch_hash_all; |
|
added experimental DBIx::Cus...
|
762 |
is_deeply($rows, [{key1 => 1, key2 => 2}], |
simplified DBIx::Custom::Mod...
|
763 |
"$test: insert insert select"); |
764 |
$table->update(param => {key1 => 3}, where => {key2 => 2}); |
|
765 |
$table->update(param => {key1 => 5}, where => {key2 => 4}); |
|
766 |
$rows = $table->select(where => {key2 => 2})->fetch_hash_all; |
|
added experimental DBIx::Cus...
|
767 |
is_deeply($rows, [{key1 => 3, key2 => 2}], |
768 |
"$test: update"); |
|
simplified DBIx::Custom::Mod...
|
769 |
$table->delete(where => {key2 => 2}); |
added experimental DBIx::Cus...
|
770 |
$rows = $table->select->fetch_hash_all; |
771 |
is_deeply($rows, [{key1 => 5, key2 => 4}], "$test: delete"); |
|
simplified DBIx::Custom::Mod...
|
772 |
$table->update_all(param => {key1 => 3}); |
773 |
$rows = $table->select->fetch_hash_all; |
|
774 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test: update_all"); |
|
added experimental DBIx::Cus...
|
775 |
$table->delete_all; |
776 |
$rows = $table->select->fetch_hash_all; |
|
777 |
is_deeply($rows, [], "$test: delete_all"); |
|
added insert, update, update...
|
778 | |
remove DBIx::Custom::Model
|
779 |
$dbi->dbh->do($CREATE_TABLE->{2}); |
780 |
$dbi->table('table2', ppp => sub { |
|
781 |
my $self = shift; |
|
782 |
|
|
783 |
return $self->name; |
|
784 |
}); |
|
785 |
is($dbi->table('table2')->ppp, 'table2', "$test : helper"); |
|
786 | ||
add examples
|
787 |
test 'limit'; |
788 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
789 |
$dbi->execute($CREATE_TABLE->{0}); |
|
790 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
791 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}); |
|
792 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6}); |
|
793 |
$dbi->query_builder->register_tag_processor( |
|
794 |
limit => sub { |
|
795 |
my ($count, $offset) = @_; |
|
796 |
|
|
797 |
my $s = ''; |
|
798 |
$s .= "limit $count"; |
|
799 |
$s .= " offset $offset" if defined $offset; |
|
800 |
|
|
801 |
return [$s, []]; |
|
802 |
} |
|
803 |
); |
|
804 |
$rows = $dbi->select( |
|
805 |
table => 'table1', |
|
806 |
where => {key1 => 1}, |
|
807 |
append => "order by key2 {limit 1 0}" |
|
808 |
)->fetch_hash_all; |
|
809 |
is_deeply($rows, [{key1 => 1, key2 => 2}], $test); |
|
810 |
$rows = $dbi->select( |
|
811 |
table => 'table1', |
|
812 |
where => {key1 => 1}, |
|
813 |
append => "order by key2 {limit 2 1}" |
|
814 |
)->fetch_hash_all; |
|
815 |
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}], $test); |
|
816 |
$rows = $dbi->select( |
|
817 |
table => 'table1', |
|
818 |
where => {key1 => 1}, |
|
819 |
append => "order by key2 {limit 1}" |
|
820 |
)->fetch_hash_all; |
|
821 |
is_deeply($rows, [{key1 => 1, key2 => 2}], $test); |
|
added insert, update, update...
|
822 | |
remove DBIx::Custom::Model
|
823 |
test 'connect super'; |
824 |
{ |
|
825 |
package MyDBI; |
|
826 |
|
|
827 |
use base 'DBIx::Custom'; |
|
828 |
sub connect { |
|
829 |
my $self = shift->SUPER::connect(@_); |
|
830 |
|
|
831 |
return $self; |
|
832 |
} |
|
833 |
|
|
834 |
sub new { |
|
835 |
my $self = shift->SUPER::connect(@_); |
|
836 |
|
|
837 |
return $self; |
|
838 |
} |
|
839 |
} |
|
840 | ||
841 |
$dbi = MyDBI->connect($NEW_ARGS->{0}); |
|
842 |
$dbi->execute($CREATE_TABLE->{0}); |
|
843 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
844 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1, $test); |
|
845 | ||
846 |
$dbi = MyDBI->new($NEW_ARGS->{0}); |
|
847 |
$dbi->execute($CREATE_TABLE->{0}); |
|
848 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
849 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1, $test); |