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