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; |
added experimental DBIx::Cus...
|
66 |
my $where; |
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 | ||
renamed auto_filter to apply...
|
553 |
test 'out filter'; |
added auto_filter method
|
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}); |
|
renamed auto_filter to apply...
|
558 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
559 |
'table1', 'key1' => {out => 'twice', in => 'three_times'}, |
560 |
'key2' => {out => 'three_times', in => 'twice'}); |
|
added auto_filter method
|
561 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
562 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
563 |
$row = $result->fetch_hash_first; |
|
564 |
is_deeply($row, {key1 => 2, key2 => 6}, "$test : insert"); |
|
renamed auto_filter to apply...
|
565 |
$result = $dbi->select(table => 'table1'); |
566 |
$row = $result->fetch_hash_first; |
|
567 |
is_deeply($row, {key1 => 6, key2 => 12}, "$test : insert"); |
|
added auto_filter method
|
568 | |
569 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
570 |
$dbi->execute($CREATE_TABLE->{0}); |
|
571 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
renamed auto_filter to apply...
|
572 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
573 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
added auto_filter method
|
574 |
); |
renamed auto_filter to apply...
|
575 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => undef}); |
added auto_filter method
|
576 |
$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2}); |
577 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
578 |
$row = $result->fetch_hash_first; |
|
579 |
is_deeply($row, {key1 => 4, key2 => 2}, "$test : update"); |
|
580 | ||
581 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
582 |
$dbi->execute($CREATE_TABLE->{0}); |
|
583 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
renamed auto_filter to apply...
|
584 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
585 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
added auto_filter method
|
586 |
); |
renamed auto_filter to apply...
|
587 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1=> undef}); |
added auto_filter method
|
588 |
$dbi->delete(table => 'table1', where => {key1 => 1}); |
589 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
590 |
$rows = $result->fetch_hash_all; |
|
591 |
is_deeply($rows, [], "$test : delete"); |
|
592 | ||
593 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
594 |
$dbi->execute($CREATE_TABLE->{0}); |
|
595 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
renamed auto_filter to apply...
|
596 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
597 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
added auto_filter method
|
598 |
); |
renamed auto_filter to apply...
|
599 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef}); |
added auto_filter method
|
600 |
$result = $dbi->select(table => 'table1', where => {key1 => 1}); |
601 |
$result->filter({'key2' => 'twice'}); |
|
602 |
$rows = $result->fetch_hash_all; |
|
603 |
is_deeply($rows, [{key1 => 4, key2 => 4}], "$test : select"); |
|
604 | ||
605 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
606 |
$dbi->execute($CREATE_TABLE->{0}); |
|
607 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
renamed auto_filter to apply...
|
608 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
609 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
added auto_filter method
|
610 |
); |
renamed auto_filter to apply...
|
611 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef}); |
added auto_filter method
|
612 |
$result = $dbi->execute("select * from table1 where {= key1} and {= key2};", |
613 |
param => {key1 => 1, key2 => 2}, |
|
renamed auto_filter to apply...
|
614 |
table => ['table1']); |
added auto_filter method
|
615 |
$rows = $result->fetch_hash_all; |
616 |
is_deeply($rows, [{key1 => 4, key2 => 2}], "$test : execute"); |
|
617 | ||
618 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
619 |
$dbi->execute($CREATE_TABLE->{0}); |
|
620 |
$dbi->execute($CREATE_TABLE->{2}); |
|
621 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
622 |
$dbi->register_filter(three_times => sub { $_[0] * 3 }); |
|
renamed auto_filter to apply...
|
623 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
624 |
'table1', 'key2' => {out => 'twice', in => 'twice'} |
added auto_filter method
|
625 |
); |
renamed auto_filter to apply...
|
626 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
627 |
'table2', 'key3' => {out => 'three_times', in => 'three_times'} |
added auto_filter method
|
628 |
); |
renamed auto_filter to apply...
|
629 |
$dbi->insert(table => 'table1', param => {key1 => 5, key2 => 2}, filter => {key2 => undef}); |
630 |
$dbi->insert(table => 'table2', param => {key1 => 5, key3 => 6}, filter => {key3 => undef}); |
|
added auto_filter method
|
631 |
$result = $dbi->select( |
632 |
table => ['table1', 'table2'], |
|
633 |
column => ['key2', 'key3'], |
|
634 |
where => {'table1.key2' => 1, 'table2.key3' => 2}, relation => {'table1.key1' => 'table2.key1'}); |
|
635 | ||
636 |
$result->filter({'key2' => 'twice'}); |
|
637 |
$rows = $result->fetch_hash_all; |
|
638 |
is_deeply($rows, [{key2 => 4, key3 => 18}], "$test : select : join"); |
|
639 | ||
640 |
$result = $dbi->select( |
|
641 |
table => ['table1', 'table2'], |
|
642 |
column => ['key2', 'key3'], |
|
643 |
where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'}); |
|
644 | ||
645 |
$result->filter({'key2' => 'twice'}); |
|
646 |
$rows = $result->fetch_hash_all; |
|
647 |
is_deeply($rows, [{key2 => 4, key3 => 18}], "$test : select : join : omit"); |
|
648 | ||
renamed auto_filter to apply...
|
649 |
test 'iterate_all_columns'; |
added experimental iterate_a...
|
650 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
651 |
$dbi->execute($CREATE_TABLE->{2}); |
|
652 |
$dbi->execute($CREATE_TABLE->{3}); |
|
653 | ||
654 |
$infos = []; |
|
655 |
$dbi->iterate_all_columns(sub { |
|
656 |
my ($table, $column, $cinfo) = @_; |
|
657 |
|
|
658 |
if ($table =~ /^table/) { |
|
659 |
my $info = [$table, $column, $cinfo->{COLUMN_NAME}]; |
|
660 |
push @$infos, $info; |
|
661 |
} |
|
662 |
}); |
|
663 |
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos]; |
|
664 |
is_deeply($infos, |
|
665 |
[ |
|
666 |
['table1', 'key1', 'key1'], |
|
667 |
['table1', 'key2', 'key2'], |
|
668 |
['table2', 'key1', 'key1'], |
|
669 |
['table2', 'key3', 'key3'] |
|
670 |
] |
|
671 |
, $test |
|
672 |
); |
|
added auto_filter method
|
673 | |
remove DBIx::Custom::Model
|
674 |
test 'table'; |
675 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
676 |
$dbi->execute($CREATE_TABLE->{0}); |
|
677 |
$table = $dbi->table('table1'); |
|
simplified DBIx::Custom::Mod...
|
678 |
$table->insert(param => {key1 => 1, key2 => 2}); |
679 |
$table->insert(param => {key1 => 3, key2 => 4}); |
|
added experimental DBIx::Cus...
|
680 |
$rows = $table->select->fetch_hash_all; |
681 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], |
|
682 |
"$test: select"); |
|
simplified DBIx::Custom::Mod...
|
683 |
$rows = $table->select(where => {key2 => 2}, append => 'order by key1', |
684 |
column => ['key1', 'key2'])->fetch_hash_all; |
|
added experimental DBIx::Cus...
|
685 |
is_deeply($rows, [{key1 => 1, key2 => 2}], |
simplified DBIx::Custom::Mod...
|
686 |
"$test: insert insert select"); |
687 |
$table->update(param => {key1 => 3}, where => {key2 => 2}); |
|
688 |
$table->update(param => {key1 => 5}, where => {key2 => 4}); |
|
689 |
$rows = $table->select(where => {key2 => 2})->fetch_hash_all; |
|
added experimental DBIx::Cus...
|
690 |
is_deeply($rows, [{key1 => 3, key2 => 2}], |
691 |
"$test: update"); |
|
simplified DBIx::Custom::Mod...
|
692 |
$table->delete(where => {key2 => 2}); |
added experimental DBIx::Cus...
|
693 |
$rows = $table->select->fetch_hash_all; |
694 |
is_deeply($rows, [{key1 => 5, key2 => 4}], "$test: delete"); |
|
simplified DBIx::Custom::Mod...
|
695 |
$table->update_all(param => {key1 => 3}); |
696 |
$rows = $table->select->fetch_hash_all; |
|
697 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test: update_all"); |
|
added experimental DBIx::Cus...
|
698 |
$table->delete_all; |
699 |
$rows = $table->select->fetch_hash_all; |
|
700 |
is_deeply($rows, [], "$test: delete_all"); |
|
added insert, update, update...
|
701 | |
remove DBIx::Custom::Model
|
702 |
$dbi->dbh->do($CREATE_TABLE->{2}); |
703 |
$dbi->table('table2', ppp => sub { |
|
704 |
my $self = shift; |
|
705 |
|
|
706 |
return $self->name; |
|
707 |
}); |
|
708 |
is($dbi->table('table2')->ppp, 'table2', "$test : helper"); |
|
709 | ||
add examples
|
710 |
test 'limit'; |
711 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
712 |
$dbi->execute($CREATE_TABLE->{0}); |
|
713 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
714 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}); |
|
715 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6}); |
|
716 |
$dbi->query_builder->register_tag_processor( |
|
717 |
limit => sub { |
|
718 |
my ($count, $offset) = @_; |
|
719 |
|
|
720 |
my $s = ''; |
|
721 |
$s .= "limit $count"; |
|
722 |
$s .= " offset $offset" if defined $offset; |
|
723 |
|
|
724 |
return [$s, []]; |
|
725 |
} |
|
726 |
); |
|
727 |
$rows = $dbi->select( |
|
728 |
table => 'table1', |
|
729 |
where => {key1 => 1}, |
|
730 |
append => "order by key2 {limit 1 0}" |
|
731 |
)->fetch_hash_all; |
|
732 |
is_deeply($rows, [{key1 => 1, key2 => 2}], $test); |
|
733 |
$rows = $dbi->select( |
|
734 |
table => 'table1', |
|
735 |
where => {key1 => 1}, |
|
736 |
append => "order by key2 {limit 2 1}" |
|
737 |
)->fetch_hash_all; |
|
738 |
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}], $test); |
|
739 |
$rows = $dbi->select( |
|
740 |
table => 'table1', |
|
741 |
where => {key1 => 1}, |
|
742 |
append => "order by key2 {limit 1}" |
|
743 |
)->fetch_hash_all; |
|
744 |
is_deeply($rows, [{key1 => 1, key2 => 2}], $test); |
|
added insert, update, update...
|
745 | |
remove DBIx::Custom::Model
|
746 |
test 'connect super'; |
747 |
{ |
|
748 |
package MyDBI; |
|
749 |
|
|
750 |
use base 'DBIx::Custom'; |
|
751 |
sub connect { |
|
752 |
my $self = shift->SUPER::connect(@_); |
|
753 |
|
|
754 |
return $self; |
|
755 |
} |
|
756 |
|
|
757 |
sub new { |
|
758 |
my $self = shift->SUPER::connect(@_); |
|
759 |
|
|
760 |
return $self; |
|
761 |
} |
|
762 |
} |
|
763 | ||
764 |
$dbi = MyDBI->connect($NEW_ARGS->{0}); |
|
765 |
$dbi->execute($CREATE_TABLE->{0}); |
|
766 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
767 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1, $test); |
|
768 | ||
769 |
$dbi = MyDBI->new($NEW_ARGS->{0}); |
|
770 |
$dbi->execute($CREATE_TABLE->{0}); |
|
771 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
772 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1, $test); |
|
added experimental DBIx::Cus...
|
773 | |
774 | ||
775 |
test 'end_filter'; |
|
776 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
777 |
$dbi->execute($CREATE_TABLE->{0}); |
|
778 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
779 |
$result = $dbi->select(table => 'table1'); |
|
780 |
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 }); |
|
781 |
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 }); |
|
782 |
$row = $result->fetch_first; |
|
783 |
is_deeply($row, [6, 40]); |
|
784 | ||
785 |
$result = $dbi->select(table => 'table1'); |
|
786 |
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 }); |
|
787 |
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 }); |
|
788 |
$row = $result->fetch_hash_first; |
|
789 |
is_deeply($row, {key1 => 6, key2 => 40}); |
|
fix select method empty wher...
|
790 | |
791 | ||
792 |
test 'empty where select'; |
|
793 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
794 |
$dbi->execute($CREATE_TABLE->{0}); |
|
795 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
796 |
$result = $dbi->select(table => 'table1', where => {}); |
|
797 |
$row = $result->fetch_hash_first; |
|
798 |
is_deeply($row, {key1 => 1, key2 => 2}); |
|
799 | ||
800 |
$result = $dbi->select(table => 'table1', where => [' ', {}]); |
|
801 |
$row = $result->fetch_hash_first; |
|
802 |
is_deeply($row, {key1 => 1, key2 => 2}); |
|
added experimental sugar met...
|
803 | |
804 | ||
805 |
test 'select query option'; |
|
806 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
807 |
$dbi->execute($CREATE_TABLE->{0}); |
|
808 |
$query = $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, query => 1); |
|
809 |
is(ref $query, 'DBIx::Custom::Query'); |
|
810 |
$query = $dbi->update(table => 'table1', where => {key1 => 1}, param => {key2 => 2}, query => 1); |
|
811 |
is(ref $query, 'DBIx::Custom::Query'); |
|
812 |
$query = $dbi->delete(table => 'table1', where => {key1 => 1}, query => 1); |
|
813 |
is(ref $query, 'DBIx::Custom::Query'); |
|
814 |
$query = $dbi->select(table => 'table1', where => {key1 => 1, key2 => 2}, query => 1); |
|
815 |
is(ref $query, 'DBIx::Custom::Query'); |
|
816 | ||
added experimental DBIx::Cus...
|
817 |
test 'DBIx::Custom::Where'; |
experimental extended select...
|
818 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
819 |
$dbi->execute($CREATE_TABLE->{0}); |
|
820 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
821 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
added experimental DBIx::Cus...
|
822 |
$where = $dbi->where |
added test
|
823 |
->clause(['and', '{= key1}', '{= key2}']) |
added experimental DBIx::Cus...
|
824 |
->param({key1 => 1}); |
added test
|
825 | |
experimental extended select...
|
826 |
$result = $dbi->select( |
827 |
table => 'table1', |
|
added experimental DBIx::Cus...
|
828 |
where => $where |
experimental extended select...
|
829 |
); |
830 |
$row = $result->fetch_hash_all; |
|
831 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
832 | ||
added experimental DBIx::Cus...
|
833 |
$where = $dbi->where |
added test
|
834 |
->clause(['and', '{= key1}', '{= key2}']) |
added experimental DBIx::Cus...
|
835 |
->param({key1 => 1, key2 => 2}); |
experimental extended select...
|
836 |
$result = $dbi->select( |
837 |
table => 'table1', |
|
added experimental DBIx::Cus...
|
838 |
where => $where |
experimental extended select...
|
839 |
); |
840 |
$row = $result->fetch_hash_all; |
|
841 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
842 | ||
added experimental DBIx::Cus...
|
843 |
$where = $dbi->where |
added test
|
844 |
->clause(['and', '{= key1}', '{= key2}']) |
added experimental DBIx::Cus...
|
845 |
->param({}); |
experimental extended select...
|
846 |
$result = $dbi->select( |
847 |
table => 'table1', |
|
added experimental DBIx::Cus...
|
848 |
where => $where, |
experimental extended select...
|
849 |
); |
850 |
$row = $result->fetch_hash_all; |
|
851 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
852 | ||
added experimental DBIx::Cus...
|
853 |
$where = $dbi->where |
added test
|
854 |
->clause(['and', ['or', '{> key1}', '{< key1}'], '{= key2}']) |
added experimental DBIx::Cus...
|
855 |
->param({key1 => [0, 3], key2 => 2}); |
experimental extended select...
|
856 |
$result = $dbi->select( |
857 |
table => 'table1', |
|
added experimental DBIx::Cus...
|
858 |
where => $where, |
experimental extended select...
|
859 |
); |
860 |
$row = $result->fetch_hash_all; |
|
861 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
862 | ||
added experimental DBIx::Cus...
|
863 |
$where = $dbi->where; |
864 |
$result = $dbi->select( |
|
865 |
table => 'table1', |
|
866 |
where => $where |
|
867 |
); |
|
868 |
$row = $result->fetch_hash_all; |
|
869 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
870 | ||
added test
|
871 |
__END__ |
872 | ||
experimental extended select...
|
873 |
eval { |
added experimental DBIx::Cus...
|
874 |
$where = $dbi->where |
875 |
->clause(key1 => "{= key1}" ) |
|
876 |
->or_clause(key2 => "{= key2}" ) |
|
877 |
->param({key3 => 5}); |
|
experimental extended select...
|
878 |
$result = $dbi->select( |
879 |
table => 'table1', |
|
added experimental DBIx::Cus...
|
880 |
where => $where |
experimental extended select...
|
881 |
); |
882 |
}; |
|
883 |
ok($@); |
|
884 | ||
added experimental DBIx::Cus...
|
885 |
$where = $dbi->where; |
886 |
is("$where", ''); |
|
887 | ||
888 |
$where = $dbi->where->clause(key1 => 'pppp')->param({key1 => 1}); |
|
889 |
like("$where", qr/pppp/); |
|
890 | ||
891 |
test 'dbi_options default'; |
|
892 |
$dbi = DBIx::Custom->new; |
|
893 |
is_deeply($dbi->dbi_options, {}); |
|
894 | ||
895 | ||
experimental extended select...
|
896 |