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 | ||
renamed auto_filter to apply...
|
552 |
test 'out filter'; |
added auto_filter method
|
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}); |
|
renamed auto_filter to apply...
|
557 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
558 |
'table1', 'key1' => {out => 'twice', in => 'three_times'}, |
559 |
'key2' => {out => 'three_times', in => 'twice'}); |
|
added auto_filter method
|
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"); |
|
renamed auto_filter to apply...
|
564 |
$result = $dbi->select(table => 'table1'); |
565 |
$row = $result->fetch_hash_first; |
|
566 |
is_deeply($row, {key1 => 6, key2 => 12}, "$test : insert"); |
|
added auto_filter method
|
567 | |
568 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
569 |
$dbi->execute($CREATE_TABLE->{0}); |
|
570 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
renamed auto_filter to apply...
|
571 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
572 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
added auto_filter method
|
573 |
); |
renamed auto_filter to apply...
|
574 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => undef}); |
added auto_filter method
|
575 |
$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2}); |
576 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
577 |
$row = $result->fetch_hash_first; |
|
578 |
is_deeply($row, {key1 => 4, key2 => 2}, "$test : update"); |
|
579 | ||
580 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
581 |
$dbi->execute($CREATE_TABLE->{0}); |
|
582 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
renamed auto_filter to apply...
|
583 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
584 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
added auto_filter method
|
585 |
); |
renamed auto_filter to apply...
|
586 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1=> undef}); |
added auto_filter method
|
587 |
$dbi->delete(table => 'table1', where => {key1 => 1}); |
588 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
589 |
$rows = $result->fetch_hash_all; |
|
590 |
is_deeply($rows, [], "$test : delete"); |
|
591 | ||
592 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
593 |
$dbi->execute($CREATE_TABLE->{0}); |
|
594 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
renamed auto_filter to apply...
|
595 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
596 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
added auto_filter method
|
597 |
); |
renamed auto_filter to apply...
|
598 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef}); |
added auto_filter method
|
599 |
$result = $dbi->select(table => 'table1', where => {key1 => 1}); |
600 |
$result->filter({'key2' => 'twice'}); |
|
601 |
$rows = $result->fetch_hash_all; |
|
602 |
is_deeply($rows, [{key1 => 4, key2 => 4}], "$test : select"); |
|
603 | ||
604 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
605 |
$dbi->execute($CREATE_TABLE->{0}); |
|
606 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
renamed auto_filter to apply...
|
607 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
608 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
added auto_filter method
|
609 |
); |
renamed auto_filter to apply...
|
610 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef}); |
added auto_filter method
|
611 |
$result = $dbi->execute("select * from table1 where {= key1} and {= key2};", |
612 |
param => {key1 => 1, key2 => 2}, |
|
renamed auto_filter to apply...
|
613 |
table => ['table1']); |
added auto_filter method
|
614 |
$rows = $result->fetch_hash_all; |
615 |
is_deeply($rows, [{key1 => 4, key2 => 2}], "$test : execute"); |
|
616 | ||
617 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
618 |
$dbi->execute($CREATE_TABLE->{0}); |
|
619 |
$dbi->execute($CREATE_TABLE->{2}); |
|
620 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
621 |
$dbi->register_filter(three_times => sub { $_[0] * 3 }); |
|
renamed auto_filter to apply...
|
622 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
623 |
'table1', 'key2' => {out => 'twice', in => 'twice'} |
added auto_filter method
|
624 |
); |
renamed auto_filter to apply...
|
625 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
626 |
'table2', 'key3' => {out => 'three_times', in => 'three_times'} |
added auto_filter method
|
627 |
); |
renamed auto_filter to apply...
|
628 |
$dbi->insert(table => 'table1', param => {key1 => 5, key2 => 2}, filter => {key2 => undef}); |
629 |
$dbi->insert(table => 'table2', param => {key1 => 5, key3 => 6}, filter => {key3 => undef}); |
|
added auto_filter method
|
630 |
$result = $dbi->select( |
631 |
table => ['table1', 'table2'], |
|
632 |
column => ['key2', 'key3'], |
|
633 |
where => {'table1.key2' => 1, 'table2.key3' => 2}, relation => {'table1.key1' => 'table2.key1'}); |
|
634 | ||
635 |
$result->filter({'key2' => 'twice'}); |
|
636 |
$rows = $result->fetch_hash_all; |
|
637 |
is_deeply($rows, [{key2 => 4, key3 => 18}], "$test : select : join"); |
|
638 | ||
639 |
$result = $dbi->select( |
|
640 |
table => ['table1', 'table2'], |
|
641 |
column => ['key2', 'key3'], |
|
642 |
where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'}); |
|
643 | ||
644 |
$result->filter({'key2' => 'twice'}); |
|
645 |
$rows = $result->fetch_hash_all; |
|
646 |
is_deeply($rows, [{key2 => 4, key3 => 18}], "$test : select : join : omit"); |
|
647 | ||
renamed auto_filter to apply...
|
648 |
test 'iterate_all_columns'; |
added experimental iterate_a...
|
649 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
650 |
$dbi->execute($CREATE_TABLE->{2}); |
|
651 |
$dbi->execute($CREATE_TABLE->{3}); |
|
652 | ||
653 |
$infos = []; |
|
654 |
$dbi->iterate_all_columns(sub { |
|
655 |
my ($table, $column, $cinfo) = @_; |
|
656 |
|
|
657 |
if ($table =~ /^table/) { |
|
658 |
my $info = [$table, $column, $cinfo->{COLUMN_NAME}]; |
|
659 |
push @$infos, $info; |
|
660 |
} |
|
661 |
}); |
|
662 |
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos]; |
|
663 |
is_deeply($infos, |
|
664 |
[ |
|
665 |
['table1', 'key1', 'key1'], |
|
666 |
['table1', 'key2', 'key2'], |
|
667 |
['table2', 'key1', 'key1'], |
|
668 |
['table2', 'key3', 'key3'] |
|
669 |
] |
|
670 |
, $test |
|
671 |
); |
|
added auto_filter method
|
672 | |
remove DBIx::Custom::Model
|
673 |
test 'table'; |
674 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
675 |
$dbi->execute($CREATE_TABLE->{0}); |
|
676 |
$table = $dbi->table('table1'); |
|
simplified DBIx::Custom::Mod...
|
677 |
$table->insert(param => {key1 => 1, key2 => 2}); |
678 |
$table->insert(param => {key1 => 3, key2 => 4}); |
|
added experimental DBIx::Cus...
|
679 |
$rows = $table->select->fetch_hash_all; |
680 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], |
|
681 |
"$test: select"); |
|
simplified DBIx::Custom::Mod...
|
682 |
$rows = $table->select(where => {key2 => 2}, append => 'order by key1', |
683 |
column => ['key1', 'key2'])->fetch_hash_all; |
|
added experimental DBIx::Cus...
|
684 |
is_deeply($rows, [{key1 => 1, key2 => 2}], |
simplified DBIx::Custom::Mod...
|
685 |
"$test: insert insert select"); |
686 |
$table->update(param => {key1 => 3}, where => {key2 => 2}); |
|
687 |
$table->update(param => {key1 => 5}, where => {key2 => 4}); |
|
688 |
$rows = $table->select(where => {key2 => 2})->fetch_hash_all; |
|
added experimental DBIx::Cus...
|
689 |
is_deeply($rows, [{key1 => 3, key2 => 2}], |
690 |
"$test: update"); |
|
simplified DBIx::Custom::Mod...
|
691 |
$table->delete(where => {key2 => 2}); |
added experimental DBIx::Cus...
|
692 |
$rows = $table->select->fetch_hash_all; |
693 |
is_deeply($rows, [{key1 => 5, key2 => 4}], "$test: delete"); |
|
simplified DBIx::Custom::Mod...
|
694 |
$table->update_all(param => {key1 => 3}); |
695 |
$rows = $table->select->fetch_hash_all; |
|
696 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test: update_all"); |
|
added experimental DBIx::Cus...
|
697 |
$table->delete_all; |
698 |
$rows = $table->select->fetch_hash_all; |
|
699 |
is_deeply($rows, [], "$test: delete_all"); |
|
added insert, update, update...
|
700 | |
remove DBIx::Custom::Model
|
701 |
$dbi->dbh->do($CREATE_TABLE->{2}); |
702 |
$dbi->table('table2', ppp => sub { |
|
703 |
my $self = shift; |
|
704 |
|
|
705 |
return $self->name; |
|
706 |
}); |
|
707 |
is($dbi->table('table2')->ppp, 'table2', "$test : helper"); |
|
708 | ||
add examples
|
709 |
test 'limit'; |
710 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
711 |
$dbi->execute($CREATE_TABLE->{0}); |
|
712 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
713 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}); |
|
714 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6}); |
|
715 |
$dbi->query_builder->register_tag_processor( |
|
716 |
limit => sub { |
|
717 |
my ($count, $offset) = @_; |
|
718 |
|
|
719 |
my $s = ''; |
|
720 |
$s .= "limit $count"; |
|
721 |
$s .= " offset $offset" if defined $offset; |
|
722 |
|
|
723 |
return [$s, []]; |
|
724 |
} |
|
725 |
); |
|
726 |
$rows = $dbi->select( |
|
727 |
table => 'table1', |
|
728 |
where => {key1 => 1}, |
|
729 |
append => "order by key2 {limit 1 0}" |
|
730 |
)->fetch_hash_all; |
|
731 |
is_deeply($rows, [{key1 => 1, key2 => 2}], $test); |
|
732 |
$rows = $dbi->select( |
|
733 |
table => 'table1', |
|
734 |
where => {key1 => 1}, |
|
735 |
append => "order by key2 {limit 2 1}" |
|
736 |
)->fetch_hash_all; |
|
737 |
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}], $test); |
|
738 |
$rows = $dbi->select( |
|
739 |
table => 'table1', |
|
740 |
where => {key1 => 1}, |
|
741 |
append => "order by key2 {limit 1}" |
|
742 |
)->fetch_hash_all; |
|
743 |
is_deeply($rows, [{key1 => 1, key2 => 2}], $test); |
|
added insert, update, update...
|
744 | |
remove DBIx::Custom::Model
|
745 |
test 'connect super'; |
746 |
{ |
|
747 |
package MyDBI; |
|
748 |
|
|
749 |
use base 'DBIx::Custom'; |
|
750 |
sub connect { |
|
751 |
my $self = shift->SUPER::connect(@_); |
|
752 |
|
|
753 |
return $self; |
|
754 |
} |
|
755 |
|
|
756 |
sub new { |
|
757 |
my $self = shift->SUPER::connect(@_); |
|
758 |
|
|
759 |
return $self; |
|
760 |
} |
|
761 |
} |
|
762 | ||
763 |
$dbi = MyDBI->connect($NEW_ARGS->{0}); |
|
764 |
$dbi->execute($CREATE_TABLE->{0}); |
|
765 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
766 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1, $test); |
|
767 | ||
768 |
$dbi = MyDBI->new($NEW_ARGS->{0}); |
|
769 |
$dbi->execute($CREATE_TABLE->{0}); |
|
770 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
771 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1, $test); |
|
added experimental DBIx::Cus...
|
772 | |
773 | ||
774 |
test 'end_filter'; |
|
775 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
776 |
$dbi->execute($CREATE_TABLE->{0}); |
|
777 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
778 |
$result = $dbi->select(table => 'table1'); |
|
779 |
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 }); |
|
780 |
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 }); |
|
781 |
$row = $result->fetch_first; |
|
782 |
is_deeply($row, [6, 40]); |
|
783 | ||
784 |
$result = $dbi->select(table => 'table1'); |
|
785 |
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 }); |
|
786 |
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 }); |
|
787 |
$row = $result->fetch_hash_first; |
|
788 |
is_deeply($row, {key1 => 6, key2 => 40}); |
|
fix select method empty wher...
|
789 | |
790 | ||
791 |
test 'empty where select'; |
|
792 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
793 |
$dbi->execute($CREATE_TABLE->{0}); |
|
794 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
795 |
$result = $dbi->select(table => 'table1', where => {}); |
|
796 |
$row = $result->fetch_hash_first; |
|
797 |
is_deeply($row, {key1 => 1, key2 => 2}); |
|
798 | ||
799 |
$result = $dbi->select(table => 'table1', where => [' ', {}]); |
|
800 |
$row = $result->fetch_hash_first; |
|
801 |
is_deeply($row, {key1 => 1, key2 => 2}); |
|
added experimental sugar met...
|
802 | |
803 | ||
804 |
test 'select query option'; |
|
805 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
806 |
$dbi->execute($CREATE_TABLE->{0}); |
|
807 |
$query = $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, query => 1); |
|
808 |
is(ref $query, 'DBIx::Custom::Query'); |
|
809 |
$query = $dbi->update(table => 'table1', where => {key1 => 1}, param => {key2 => 2}, query => 1); |
|
810 |
is(ref $query, 'DBIx::Custom::Query'); |
|
811 |
$query = $dbi->delete(table => 'table1', where => {key1 => 1}, query => 1); |
|
812 |
is(ref $query, 'DBIx::Custom::Query'); |
|
813 |
$query = $dbi->select(table => 'table1', where => {key1 => 1, key2 => 2}, query => 1); |
|
814 |
is(ref $query, 'DBIx::Custom::Query'); |
|
815 | ||
816 |
1; |