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 |
|
table object call dbi object...
|
19 |
sub test { print "# $_[0]\n" } |
removed register_format()
|
20 | |
21 |
# Constant varialbes for test |
|
22 |
my $CREATE_TABLE = { |
|
23 |
0 => 'create table table1 (key1 char(255), key2 char(255));', |
|
24 |
1 => 'create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));', |
|
added experimental iterate_a...
|
25 |
2 => 'create table table2 (key1 char(255), key3 char(255));', |
26 |
3 => 'create table table1 (key1 Date, key2 datetime);' |
|
removed register_format()
|
27 |
}; |
28 | ||
add tests
|
29 |
my $SELECT_SOURCES = { |
removed register_format()
|
30 |
0 => 'select * from table1;' |
31 |
}; |
|
32 | ||
33 |
my $DROP_TABLE = { |
|
34 |
0 => 'drop table table1' |
|
35 |
}; |
|
36 | ||
37 |
my $NEW_ARGS = { |
|
38 |
0 => {data_source => 'dbi:SQLite:dbname=:memory:'} |
|
39 |
}; |
|
40 | ||
41 |
# Variables |
|
42 |
my $dbi; |
|
43 |
my $sth; |
|
renamed build_query to creat...
|
44 |
my $source; |
45 |
my @sources; |
|
add tests
|
46 |
my $select_SOURCE; |
47 |
my $insert_SOURCE; |
|
48 |
my $update_SOURCE; |
|
removed register_format()
|
49 |
my $params; |
50 |
my $sql; |
|
51 |
my $result; |
|
52 |
my $row; |
|
53 |
my @rows; |
|
54 |
my $rows; |
|
55 |
my $query; |
|
56 |
my @queries; |
|
57 |
my $select_query; |
|
58 |
my $insert_query; |
|
59 |
my $update_query; |
|
60 |
my $ret_val; |
|
added experimental iterate_a...
|
61 |
my $infos; |
added experimental DBIx::Cus...
|
62 |
my $table; |
added experimental DBIx::Cus...
|
63 |
my $where; |
removed register_format()
|
64 | |
65 |
# Prepare table |
|
66 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
67 |
$dbi->execute($CREATE_TABLE->{0}); |
|
68 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
69 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
70 | ||
71 |
test 'DBIx::Custom::Result test'; |
|
renamed build_query to creat...
|
72 |
$source = "select key1, key2 from table1"; |
73 |
$query = $dbi->create_query($source); |
|
removed register_format()
|
74 |
$result = $dbi->execute($query); |
75 | ||
76 |
@rows = (); |
|
77 |
while (my $row = $result->fetch) { |
|
78 |
push @rows, [@$row]; |
|
79 |
} |
|
cleanup
|
80 |
is_deeply(\@rows, [[1, 2], [3, 4]], "fetch"); |
removed register_format()
|
81 | |
82 |
$result = $dbi->execute($query); |
|
83 |
@rows = (); |
|
84 |
while (my $row = $result->fetch_hash) { |
|
85 |
push @rows, {%$row}; |
|
86 |
} |
|
cleanup
|
87 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "fetch_hash"); |
removed register_format()
|
88 | |
89 |
$result = $dbi->execute($query); |
|
90 |
$rows = $result->fetch_all; |
|
cleanup
|
91 |
is_deeply($rows, [[1, 2], [3, 4]], "fetch_all"); |
removed register_format()
|
92 | |
93 |
$result = $dbi->execute($query); |
|
removed reconnect method
|
94 |
$rows = $result->fetch_hash_all; |
cleanup
|
95 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "fetch_hash_all"); |
removed register_format()
|
96 | |
97 |
test 'Insert query return value'; |
|
98 |
$dbi->execute($DROP_TABLE->{0}); |
|
99 |
$dbi->execute($CREATE_TABLE->{0}); |
|
renamed update tag to update...
|
100 |
$source = "insert into table1 {insert_param key1 key2}"; |
renamed build_query to creat...
|
101 |
$query = $dbi->create_query($source); |
removed register_format()
|
102 |
$ret_val = $dbi->execute($query, param => {key1 => 1, key2 => 2}); |
cleanup
|
103 |
ok($ret_val); |
removed register_format()
|
104 | |
105 | ||
106 |
test 'Direct query'; |
|
107 |
$dbi->execute($DROP_TABLE->{0}); |
|
108 |
$dbi->execute($CREATE_TABLE->{0}); |
|
add tests
|
109 |
$insert_SOURCE = "insert into table1 {insert_param key1 key2}"; |
110 |
$dbi->execute($insert_SOURCE, param => {key1 => 1, key2 => 2}); |
|
111 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
removed register_format()
|
112 |
$rows = $result->fetch_hash_all; |
cleanup
|
113 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
removed register_format()
|
114 | |
115 |
test 'Filter basic'; |
|
116 |
$dbi->execute($DROP_TABLE->{0}); |
|
117 |
$dbi->execute($CREATE_TABLE->{0}); |
|
118 |
$dbi->register_filter(twice => sub { $_[0] * 2}, |
|
119 |
three_times => sub { $_[0] * 3}); |
|
120 | ||
add tests
|
121 |
$insert_SOURCE = "insert into table1 {insert_param key1 key2};"; |
122 |
$insert_query = $dbi->create_query($insert_SOURCE); |
|
removed register_format()
|
123 |
$insert_query->filter({key1 => 'twice'}); |
124 |
$dbi->execute($insert_query, param => {key1 => 1, key2 => 2}); |
|
add tests
|
125 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
126 |
$rows = $result->filter({key2 => 'three_times'})->fetch_hash_all; |
cleanup
|
127 |
is_deeply($rows, [{key1 => 2, key2 => 6}], "filter fetch_filter"); |
removed register_format()
|
128 |
$dbi->execute($DROP_TABLE->{0}); |
129 | ||
130 |
test 'Filter in'; |
|
131 |
$dbi->execute($CREATE_TABLE->{0}); |
|
add tests
|
132 |
$insert_SOURCE = "insert into table1 {insert_param key1 key2};"; |
133 |
$insert_query = $dbi->create_query($insert_SOURCE); |
|
removed register_format()
|
134 |
$dbi->execute($insert_query, param => {key1 => 2, key2 => 4}); |
add tests
|
135 |
$select_SOURCE = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}"; |
136 |
$select_query = $dbi->create_query($select_SOURCE); |
|
removed register_format()
|
137 |
$select_query->filter({'table1.key1' => 'twice'}); |
138 |
$result = $dbi->execute($select_query, param => {'table1.key1' => [1,5], 'table1.key2' => [2,4]}); |
|
139 |
$rows = $result->fetch_hash_all; |
|
cleanup
|
140 |
is_deeply($rows, [{key1 => 2, key2 => 4}], "filter"); |
removed register_format()
|
141 | |
142 |
test 'DBIx::Custom::SQLTemplate basic tag'; |
|
143 |
$dbi->execute($DROP_TABLE->{0}); |
|
144 |
$dbi->execute($CREATE_TABLE->{1}); |
|
145 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
146 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
147 | ||
renamed build_query to creat...
|
148 |
$source = "select * from table1 where {= key1} and {<> key2} and {< key3} and {> key4} and {>= key5};"; |
149 |
$query = $dbi->create_query($source); |
|
removed register_format()
|
150 |
$result = $dbi->execute($query, param => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5}); |
151 |
$rows = $result->fetch_hash_all; |
|
cleanup
|
152 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1"); |
removed register_format()
|
153 | |
renamed build_query to creat...
|
154 |
$source = "select * from table1 where {<= key1} and {like key2};"; |
155 |
$query = $dbi->create_query($source); |
|
removed register_format()
|
156 |
$result = $dbi->execute($query, param => {key1 => 1, key2 => '%2%'}); |
157 |
$rows = $result->fetch_hash_all; |
|
cleanup
|
158 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag2"); |
removed register_format()
|
159 | |
160 |
test 'DIB::Custom::SQLTemplate in tag'; |
|
161 |
$dbi->execute($DROP_TABLE->{0}); |
|
162 |
$dbi->execute($CREATE_TABLE->{1}); |
|
163 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
164 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
165 | ||
renamed build_query to creat...
|
166 |
$source = "select * from table1 where {in key1 2};"; |
167 |
$query = $dbi->create_query($source); |
|
removed register_format()
|
168 |
$result = $dbi->execute($query, param => {key1 => [9, 1]}); |
169 |
$rows = $result->fetch_hash_all; |
|
cleanup
|
170 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic"); |
removed register_format()
|
171 | |
172 |
test 'DBIx::Custom::SQLTemplate insert tag'; |
|
173 |
$dbi->execute("delete from table1"); |
|
add tests
|
174 |
$insert_SOURCE = 'insert into table1 {insert_param key1 key2 key3 key4 key5}'; |
175 |
$dbi->execute($insert_SOURCE, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
removed register_format()
|
176 | |
add tests
|
177 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
178 |
$rows = $result->fetch_hash_all; |
cleanup
|
179 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic"); |
removed register_format()
|
180 | |
181 |
test 'DBIx::Custom::SQLTemplate update tag'; |
|
182 |
$dbi->execute("delete from table1"); |
|
add tests
|
183 |
$insert_SOURCE = "insert into table1 {insert_param key1 key2 key3 key4 key5}"; |
184 |
$dbi->execute($insert_SOURCE, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
185 |
$dbi->execute($insert_SOURCE, param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
removed register_format()
|
186 | |
add tests
|
187 |
$update_SOURCE = 'update table1 {update_param key1 key2 key3 key4} where {= key5}'; |
188 |
$dbi->execute($update_SOURCE, param => {key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}); |
|
removed register_format()
|
189 | |
add tests
|
190 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
191 |
$rows = $result->fetch_hash_all; |
192 |
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}, |
|
cleanup
|
193 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "basic"); |
removed register_format()
|
194 | |
195 |
test 'Error case'; |
|
196 |
eval {DBIx::Custom->connect(data_source => 'dbi:SQLit')}; |
|
cleanup
|
197 |
ok($@, "connect error"); |
removed register_format()
|
198 | |
199 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
renamed build_query to creat...
|
200 |
eval{$dbi->create_query("{p }")}; |
cleanup
|
201 |
ok($@, "create_query invalid SQL template"); |
removed register_format()
|
202 | |
203 |
test 'insert'; |
|
204 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
205 |
$dbi->execute($CREATE_TABLE->{0}); |
|
206 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
207 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
add tests
|
208 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
209 |
$rows = $result->fetch_hash_all; |
cleanup
|
210 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic"); |
removed register_format()
|
211 | |
212 |
$dbi->execute('delete from table1'); |
|
213 |
$dbi->register_filter( |
|
214 |
twice => sub { $_[0] * 2 }, |
|
215 |
three_times => sub { $_[0] * 3 } |
|
216 |
); |
|
renamed default_query_filter...
|
217 |
$dbi->default_bind_filter('twice'); |
removed register_format()
|
218 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => 'three_times'}); |
add tests
|
219 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
220 |
$rows = $result->fetch_hash_all; |
cleanup
|
221 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter"); |
renamed default_query_filter...
|
222 |
$dbi->default_bind_filter(undef); |
removed register_format()
|
223 | |
224 |
$dbi->execute($DROP_TABLE->{0}); |
|
225 |
$dbi->execute($CREATE_TABLE->{0}); |
|
226 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, append => ' '); |
|
227 |
$rows = $dbi->select(table => 'table1')->fetch_hash_all; |
|
228 |
is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append'); |
|
229 | ||
changed argument of tag proc...
|
230 |
eval{$dbi->insert(table => 'table1', noexist => 1)}; |
cleanup
|
231 |
like($@, qr/noexist/, "invalid argument"); |
changed argument of tag proc...
|
232 | |
select() where can't receive...
|
233 |
eval{$dbi->insert(table => 'table', param => {';' => 1})}; |
234 |
like($@, qr/safety/); |
|
changed argument of tag proc...
|
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}], |
|
cleanup
|
246 |
"basic"); |
removed register_format()
|
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}], |
|
cleanup
|
256 |
"update key same as search key"); |
removed register_format()
|
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}], |
|
cleanup
|
263 |
"update key same as search key : param is array ref"); |
add tests
|
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}, |
|
many changed
|
270 |
filter => {key2 => sub { $_[0] * 2 }}); |
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}], |
|
cleanup
|
275 |
"filter"); |
removed register_format()
|
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)}; |
cleanup
|
280 |
like($@, qr/noexist/, "invalid argument"); |
changed argument of tag proc...
|
281 | |
282 |
eval{$dbi->update(table => 'table1')}; |
|
cleanup
|
283 |
like($@, qr/where/, "not contain where"); |
changed argument of tag proc...
|
284 | |
improved delete() and update...
|
285 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
286 |
$dbi->execute($CREATE_TABLE->{0}); |
|
287 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
288 |
$where = $dbi->where; |
|
289 |
$where->clause(['and', '{= key1}', '{= key2}']); |
|
290 |
$where->param({key1 => 1, key2 => 2}); |
|
291 |
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where); |
|
292 |
$result = $dbi->select(table => 'table1'); |
|
293 |
is_deeply($result->fetch_hash_all, [{key1 => 3, key2 => 2}], 'delete() where'); |
|
294 | ||
295 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
296 |
$dbi->execute($CREATE_TABLE->{0}); |
|
297 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
298 |
$where = $dbi->where; |
|
299 |
$where->clause(['and', '{= key2}']); |
|
300 |
$where->param({key2 => 2}); |
|
301 |
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where); |
|
302 |
$result = $dbi->select(table => 'table1'); |
|
303 |
is_deeply($result->fetch_hash_all, [{key1 => 3, key2 => 2}], 'delete() where'); |
|
changed argument of tag proc...
|
304 | |
select() where can't receive...
|
305 |
eval{$dbi->update(table => 'table1', param => {';' => 1})}; |
306 |
like($@, qr/safety/); |
|
307 | ||
308 |
eval{$dbi->update(table => 'table1', param => {'key1' => 1}, where => {';' => 1})}; |
|
309 |
like($@, qr/safety/); |
|
310 | ||
removed register_format()
|
311 |
test 'update_all'; |
312 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
313 |
$dbi->execute($CREATE_TABLE->{1}); |
|
314 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
315 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
316 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
317 |
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'}); |
|
add tests
|
318 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
319 |
$rows = $result->fetch_hash_all; |
320 |
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5}, |
|
321 |
{key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}], |
|
cleanup
|
322 |
"filter"); |
removed register_format()
|
323 | |
324 | ||
325 |
test 'delete'; |
|
326 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
327 |
$dbi->execute($CREATE_TABLE->{0}); |
|
328 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
329 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
330 |
$dbi->delete(table => 'table1', where => {key1 => 1}); |
|
add tests
|
331 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
332 |
$rows = $result->fetch_hash_all; |
cleanup
|
333 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "basic"); |
removed register_format()
|
334 | |
335 |
$dbi->execute("delete from table1;"); |
|
336 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
337 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
338 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
339 |
$dbi->delete(table => 'table1', where => {key2 => 1}, filter => {key2 => 'twice'}); |
|
add tests
|
340 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
341 |
$rows = $result->fetch_hash_all; |
cleanup
|
342 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter"); |
removed register_format()
|
343 | |
344 |
$dbi->delete(table => 'table1', where => {key1 => 1}, append => ' '); |
|
345 | ||
346 |
$dbi->delete_all(table => 'table1'); |
|
347 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
348 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
349 |
$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2}); |
|
350 |
$rows = $dbi->select(table => 'table1')->fetch_hash_all; |
|
cleanup
|
351 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "delete multi key"); |
removed register_format()
|
352 | |
changed argument of tag proc...
|
353 |
eval{$dbi->delete(table => 'table1', noexist => 1)}; |
cleanup
|
354 |
like($@, qr/noexist/, "invalid argument"); |
changed argument of tag proc...
|
355 | |
improved delete() and update...
|
356 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
357 |
$dbi->execute($CREATE_TABLE->{0}); |
|
358 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
359 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
360 |
$where = $dbi->where; |
|
361 |
$where->clause(['and', '{= key1}', '{= key2}']); |
|
362 |
$where->param({ke1 => 1, key2 => 2}); |
|
363 |
$dbi->delete(table => 'table1', where => $where); |
|
364 |
$result = $dbi->select(table => 'table1'); |
|
365 |
is_deeply($result->fetch_hash_all, [{key1 => 3, key2 => 4}], 'delete() where'); |
|
changed argument of tag proc...
|
366 | |
removed register_format()
|
367 |
test 'delete error'; |
368 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
369 |
$dbi->execute($CREATE_TABLE->{0}); |
|
370 |
eval{$dbi->delete(table => 'table1')}; |
|
make delete() using where ob...
|
371 |
like($@, qr/"where" must be specified/, |
cleanup
|
372 |
"where key-value pairs not specified"); |
removed register_format()
|
373 | |
select() where can't receive...
|
374 |
eval{$dbi->delete(table => 'table1', where => {';' => 1})}; |
375 |
like($@, qr/safety/); |
|
376 | ||
removed register_format()
|
377 |
test 'delete_all'; |
378 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
379 |
$dbi->execute($CREATE_TABLE->{0}); |
|
380 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
381 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
382 |
$dbi->delete_all(table => 'table1'); |
|
add tests
|
383 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
384 |
$rows = $result->fetch_hash_all; |
cleanup
|
385 |
is_deeply($rows, [], "basic"); |
removed register_format()
|
386 | |
387 | ||
388 |
test 'select'; |
|
389 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
390 |
$dbi->execute($CREATE_TABLE->{0}); |
|
391 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
392 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
393 |
$rows = $dbi->select(table => 'table1')->fetch_hash_all; |
|
394 |
is_deeply($rows, [{key1 => 1, key2 => 2}, |
|
cleanup
|
395 |
{key1 => 3, key2 => 4}], "table"); |
removed register_format()
|
396 | |
update document
|
397 |
$rows = $dbi->select(table => 'table1', column => ['key1'])->fetch_hash_all; |
cleanup
|
398 |
is_deeply($rows, [{key1 => 1}, {key1 => 3}], "table and columns and where key"); |
removed register_format()
|
399 | |
400 |
$rows = $dbi->select(table => 'table1', where => {key1 => 1})->fetch_hash_all; |
|
cleanup
|
401 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "table and columns and where key"); |
removed register_format()
|
402 | |
update document
|
403 |
$rows = $dbi->select(table => 'table1', column => ['key1'], where => {key1 => 3})->fetch_hash_all; |
cleanup
|
404 |
is_deeply($rows, [{key1 => 3}], "table and columns and where key"); |
removed register_format()
|
405 | |
406 |
$rows = $dbi->select(table => 'table1', append => "order by key1 desc limit 1")->fetch_hash_all; |
|
cleanup
|
407 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "append statement"); |
removed register_format()
|
408 | |
409 |
$dbi->register_filter(decrement => sub { $_[0] - 1 }); |
|
update document
|
410 |
$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'}) |
removed register_format()
|
411 |
->fetch_hash_all; |
cleanup
|
412 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "filter"); |
removed register_format()
|
413 | |
414 |
$dbi->execute($CREATE_TABLE->{2}); |
|
415 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
416 |
$rows = $dbi->select( |
|
417 |
table => [qw/table1 table2/], |
|
update document
|
418 |
column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'], |
removed register_format()
|
419 |
where => {'table1.key2' => 2}, |
added commit method
|
420 |
relation => {'table1.key1' => 'table2.key1'} |
removed register_format()
|
421 |
)->fetch_hash_all; |
cleanup
|
422 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : exists where"); |
added commit method
|
423 | |
424 |
$rows = $dbi->select( |
|
425 |
table => [qw/table1 table2/], |
|
426 |
column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'], |
|
427 |
relation => {'table1.key1' => 'table2.key1'} |
|
428 |
)->fetch_hash_all; |
|
cleanup
|
429 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : no exists where"); |
removed register_format()
|
430 | |
changed argument of tag proc...
|
431 |
eval{$dbi->select(table => 'table1', noexist => 1)}; |
cleanup
|
432 |
like($@, qr/noexist/, "invalid argument"); |
changed argument of tag proc...
|
433 | |
434 | ||
removed register_format()
|
435 |
test 'fetch filter'; |
436 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
437 |
$dbi->register_filter( |
|
438 |
twice => sub { $_[0] * 2 }, |
|
439 |
three_times => sub { $_[0] * 3 } |
|
440 |
); |
|
441 |
$dbi->default_fetch_filter('twice'); |
|
442 |
$dbi->execute($CREATE_TABLE->{0}); |
|
443 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
444 |
$result = $dbi->select(table => 'table1'); |
|
445 |
$result->filter({key1 => 'three_times'}); |
|
removed reconnect method
|
446 |
$row = $result->fetch_hash_first; |
cleanup
|
447 |
is_deeply($row, {key1 => 3, key2 => 4}, "default_fetch_filter and filter"); |
removed register_format()
|
448 | |
449 |
test 'filters'; |
|
450 |
$dbi = DBIx::Custom->new; |
|
451 | ||
update document
|
452 |
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')), |
cleanup
|
453 |
'あ', "decode_utf8"); |
removed register_format()
|
454 | |
455 |
is($dbi->filters->{encode_utf8}->('あ'), |
|
cleanup
|
456 |
encode_utf8('あ'), "encode_utf8"); |
removed register_format()
|
457 | |
added commit method
|
458 |
test 'transaction'; |
459 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
460 |
$dbi->execute($CREATE_TABLE->{0}); |
|
removed DBIx::Custom commit ...
|
461 |
$dbi->dbh->begin_work; |
added commit method
|
462 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
463 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
removed DBIx::Custom commit ...
|
464 |
$dbi->dbh->commit; |
added commit method
|
465 |
$result = $dbi->select(table => 'table1'); |
466 |
is_deeply(scalar $result->fetch_hash_all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}], |
|
cleanup
|
467 |
"commit"); |
added commit method
|
468 | |
469 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
470 |
$dbi->execute($CREATE_TABLE->{0}); |
|
removed DBIx::Custom commit ...
|
471 |
$dbi->dbh->begin_work(0); |
added commit method
|
472 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
removed DBIx::Custom commit ...
|
473 |
$dbi->dbh->rollback; |
added commit method
|
474 | |
475 |
$result = $dbi->select(table => 'table1'); |
|
cleanup
|
476 |
ok(! $result->fetch_first, "rollback"); |
add cache attribute
|
477 | |
478 |
test 'cache'; |
|
479 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
480 |
$dbi->execute($CREATE_TABLE->{0}); |
|
renamed build_query to creat...
|
481 |
$source = 'select * from table1 where {= key1} and {= key2};'; |
482 |
$dbi->create_query($source); |
|
483 |
is_deeply($dbi->{_cached}->{$source}, |
|
add table tag
|
484 |
{sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2'], tables => []}, "cache"); |
add cache attribute
|
485 | |
486 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
487 |
$dbi->execute($CREATE_TABLE->{0}); |
|
488 |
$dbi->{_cached} = {}; |
|
489 |
$dbi->cache(0); |
|
490 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
491 |
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache'); |
|
492 | ||
add tests
|
493 |
test 'execute'; |
494 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
495 |
$dbi->execute($CREATE_TABLE->{0}); |
|
removed experimental registe...
|
496 |
{ |
497 |
local $Carp::Verbose = 0; |
|
498 |
eval{$dbi->execute('select * frm table1')}; |
|
cleanup
|
499 |
like($@, qr/\Qselect * frm table1;/, "fail prepare"); |
500 |
like($@, qr/\.t /, "fail : not verbose"); |
|
removed experimental registe...
|
501 |
} |
502 |
{ |
|
503 |
local $Carp::Verbose = 1; |
|
504 |
eval{$dbi->execute('select * frm table1')}; |
|
cleanup
|
505 |
like($@, qr/Custom.*\.t /s, "fail : verbose"); |
removed experimental registe...
|
506 |
} |
add tests
|
507 | |
508 |
eval{$dbi->execute('select * from table1', no_exists => 1)}; |
|
cleanup
|
509 |
like($@, qr/\Q"no_exists" is invalid argument/, "invald SQL"); |
add tests
|
510 | |
511 |
$query = $dbi->create_query('select * from table1 where {= key1}'); |
|
512 |
$dbi->dbh->disconnect; |
|
513 |
eval{$dbi->execute($query, param => {key1 => {a => 1}})}; |
|
cleanup
|
514 |
ok($@, "execute fail"); |
add tests
|
515 | |
removed experimental registe...
|
516 |
{ |
517 |
local $Carp::Verbose = 0; |
|
518 |
eval{$dbi->create_query('select * from table1 where {0 key1}')}; |
|
cleanup
|
519 |
like($@, qr/\Q.t /, "caller spec : not vebose"); |
removed experimental registe...
|
520 |
} |
521 |
{ |
|
522 |
local $Carp::Verbose = 1; |
|
523 |
eval{$dbi->create_query('select * from table1 where {0 key1}')}; |
|
cleanup
|
524 |
like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose"); |
removed experimental registe...
|
525 |
} |
cleanup
|
526 | |
527 | ||
528 |
test 'transaction'; |
|
529 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
530 |
$dbi->execute($CREATE_TABLE->{0}); |
|
531 | ||
532 |
$dbi->begin_work; |
|
533 | ||
534 |
eval { |
|
535 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
536 |
die "Error"; |
|
537 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
538 |
}; |
|
539 | ||
540 |
$dbi->rollback if $@; |
|
541 | ||
542 |
$result = $dbi->select(table => 'table1'); |
|
543 |
$rows = $result->fetch_hash_all; |
|
cleanup
|
544 |
is_deeply($rows, [], "rollback"); |
cleanup
|
545 | |
546 |
$dbi->begin_work; |
|
547 | ||
548 |
eval { |
|
549 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
550 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
551 |
}; |
|
552 | ||
553 |
$dbi->commit unless $@; |
|
554 | ||
555 |
$result = $dbi->select(table => 'table1'); |
|
556 |
$rows = $result->fetch_hash_all; |
|
cleanup
|
557 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "commit"); |
cleanup
|
558 | |
559 |
$dbi->dbh->{AutoCommit} = 0; |
|
560 |
eval{ $dbi->begin_work }; |
|
cleanup
|
561 |
ok($@, "exception"); |
cleanup
|
562 |
$dbi->dbh->{AutoCommit} = 1; |
563 | ||
added helper method
|
564 | |
renamed helper to method.
|
565 |
test 'method'; |
added helper method
|
566 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
renamed helper to method.
|
567 |
$dbi->method( |
added helper method
|
568 |
one => sub { 1 } |
569 |
); |
|
renamed helper to method.
|
570 |
$dbi->method( |
added helper method
|
571 |
two => sub { 2 } |
572 |
); |
|
renamed helper to method.
|
573 |
$dbi->method({ |
added helper method
|
574 |
twice => sub { |
575 |
my $self = shift; |
|
576 |
return $_[0] * 2; |
|
577 |
} |
|
578 |
}); |
|
579 | ||
cleanup
|
580 |
is($dbi->one, 1, "first"); |
581 |
is($dbi->two, 2, "second"); |
|
582 |
is($dbi->twice(5), 10 , "second"); |
|
added helper method
|
583 | |
584 |
eval {$dbi->XXXXXX}; |
|
autoload DBI method
|
585 |
ok($@, "not exists"); |
added helper method
|
586 | |
renamed auto_filter to apply...
|
587 |
test 'out filter'; |
added auto_filter method
|
588 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
589 |
$dbi->execute($CREATE_TABLE->{0}); |
|
590 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
591 |
$dbi->register_filter(three_times => sub { $_[0] * 3}); |
|
renamed auto_filter to apply...
|
592 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
593 |
'table1', 'key1' => {out => 'twice', in => 'three_times'}, |
594 |
'key2' => {out => 'three_times', in => 'twice'}); |
|
added auto_filter method
|
595 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
596 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
597 |
$row = $result->fetch_hash_first; |
|
cleanup
|
598 |
is_deeply($row, {key1 => 2, key2 => 6}, "insert"); |
renamed auto_filter to apply...
|
599 |
$result = $dbi->select(table => 'table1'); |
600 |
$row = $result->fetch_hash_first; |
|
cleanup
|
601 |
is_deeply($row, {key1 => 6, key2 => 12}, "insert"); |
added auto_filter method
|
602 | |
fix bug : filter can't over...
|
603 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
604 |
$dbi->execute($CREATE_TABLE->{0}); |
|
605 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
606 |
$dbi->register_filter(three_times => sub { $_[0] * 3}); |
|
607 |
$dbi->apply_filter( |
|
608 |
'table1', 'key1' => {out => 'twice', in => 'three_times'}, |
|
609 |
'key2' => {out => 'three_times', in => 'twice'}); |
|
610 |
$dbi->apply_filter( |
|
611 |
'table1', 'key1' => {out => undef} |
|
612 |
); |
|
613 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
614 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
615 |
$row = $result->fetch_hash_first; |
|
616 |
is_deeply($row, {key1 => 1, key2 => 6}, "insert"); |
|
617 | ||
added auto_filter method
|
618 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
619 |
$dbi->execute($CREATE_TABLE->{0}); |
|
620 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
renamed auto_filter to apply...
|
621 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
622 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
added auto_filter method
|
623 |
); |
renamed auto_filter to apply...
|
624 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => undef}); |
added auto_filter method
|
625 |
$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2}); |
626 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
627 |
$row = $result->fetch_hash_first; |
|
cleanup
|
628 |
is_deeply($row, {key1 => 4, key2 => 2}, "update"); |
added auto_filter method
|
629 | |
630 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
631 |
$dbi->execute($CREATE_TABLE->{0}); |
|
632 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
renamed auto_filter to apply...
|
633 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
634 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
added auto_filter method
|
635 |
); |
renamed auto_filter to apply...
|
636 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1=> undef}); |
added auto_filter method
|
637 |
$dbi->delete(table => 'table1', where => {key1 => 1}); |
638 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
639 |
$rows = $result->fetch_hash_all; |
|
cleanup
|
640 |
is_deeply($rows, [], "delete"); |
added auto_filter method
|
641 | |
642 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
643 |
$dbi->execute($CREATE_TABLE->{0}); |
|
644 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
renamed auto_filter to apply...
|
645 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
646 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
added auto_filter method
|
647 |
); |
renamed auto_filter to apply...
|
648 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef}); |
added auto_filter method
|
649 |
$result = $dbi->select(table => 'table1', where => {key1 => 1}); |
650 |
$result->filter({'key2' => 'twice'}); |
|
651 |
$rows = $result->fetch_hash_all; |
|
cleanup
|
652 |
is_deeply($rows, [{key1 => 4, key2 => 4}], "select"); |
added auto_filter method
|
653 | |
654 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
655 |
$dbi->execute($CREATE_TABLE->{0}); |
|
656 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
renamed auto_filter to apply...
|
657 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
658 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
added auto_filter method
|
659 |
); |
renamed auto_filter to apply...
|
660 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef}); |
added auto_filter method
|
661 |
$result = $dbi->execute("select * from table1 where {= key1} and {= key2};", |
662 |
param => {key1 => 1, key2 => 2}, |
|
renamed auto_filter to apply...
|
663 |
table => ['table1']); |
added auto_filter method
|
664 |
$rows = $result->fetch_hash_all; |
cleanup
|
665 |
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute"); |
added auto_filter method
|
666 | |
add table tag
|
667 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
668 |
$dbi->execute($CREATE_TABLE->{0}); |
|
669 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
670 |
$dbi->apply_filter( |
|
671 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
|
672 |
); |
|
673 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef}); |
|
674 |
$result = $dbi->execute("select * from {table table1} where {= key1} and {= key2};", |
|
675 |
param => {key1 => 1, key2 => 2}); |
|
676 |
$rows = $result->fetch_hash_all; |
|
677 |
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute table tag"); |
|
678 | ||
added auto_filter method
|
679 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
680 |
$dbi->execute($CREATE_TABLE->{0}); |
|
681 |
$dbi->execute($CREATE_TABLE->{2}); |
|
682 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
683 |
$dbi->register_filter(three_times => sub { $_[0] * 3 }); |
|
renamed auto_filter to apply...
|
684 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
685 |
'table1', 'key2' => {out => 'twice', in => 'twice'} |
added auto_filter method
|
686 |
); |
renamed auto_filter to apply...
|
687 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
688 |
'table2', 'key3' => {out => 'three_times', in => 'three_times'} |
added auto_filter method
|
689 |
); |
renamed auto_filter to apply...
|
690 |
$dbi->insert(table => 'table1', param => {key1 => 5, key2 => 2}, filter => {key2 => undef}); |
691 |
$dbi->insert(table => 'table2', param => {key1 => 5, key3 => 6}, filter => {key3 => undef}); |
|
added auto_filter method
|
692 |
$result = $dbi->select( |
693 |
table => ['table1', 'table2'], |
|
694 |
column => ['key2', 'key3'], |
|
695 |
where => {'table1.key2' => 1, 'table2.key3' => 2}, relation => {'table1.key1' => 'table2.key1'}); |
|
696 | ||
697 |
$result->filter({'key2' => 'twice'}); |
|
698 |
$rows = $result->fetch_hash_all; |
|
cleanup
|
699 |
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join"); |
added auto_filter method
|
700 | |
701 |
$result = $dbi->select( |
|
702 |
table => ['table1', 'table2'], |
|
703 |
column => ['key2', 'key3'], |
|
704 |
where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'}); |
|
705 | ||
706 |
$result->filter({'key2' => 'twice'}); |
|
707 |
$rows = $result->fetch_hash_all; |
|
cleanup
|
708 |
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join : omit"); |
added auto_filter method
|
709 | |
pod fix
|
710 |
test 'each_column'; |
added experimental iterate_a...
|
711 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
712 |
$dbi->execute($CREATE_TABLE->{2}); |
|
713 |
$dbi->execute($CREATE_TABLE->{3}); |
|
714 | ||
715 |
$infos = []; |
|
pod fix
|
716 |
$dbi->each_column(sub { |
removed experimental txn_sco...
|
717 |
my ($self, $table, $column, $cinfo) = @_; |
added experimental iterate_a...
|
718 |
|
719 |
if ($table =~ /^table/) { |
|
720 |
my $info = [$table, $column, $cinfo->{COLUMN_NAME}]; |
|
721 |
push @$infos, $info; |
|
722 |
} |
|
723 |
}); |
|
724 |
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos]; |
|
725 |
is_deeply($infos, |
|
726 |
[ |
|
727 |
['table1', 'key1', 'key1'], |
|
728 |
['table1', 'key2', 'key2'], |
|
729 |
['table2', 'key1', 'key1'], |
|
730 |
['table2', 'key3', 'key3'] |
|
731 |
] |
|
cleanup
|
732 |
|
added experimental iterate_a...
|
733 |
); |
added auto_filter method
|
734 | |
remove DBIx::Custom::Model
|
735 |
test 'table'; |
736 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
737 |
$dbi->execute($CREATE_TABLE->{0}); |
|
738 |
$table = $dbi->table('table1'); |
|
simplified DBIx::Custom::Mod...
|
739 |
$table->insert(param => {key1 => 1, key2 => 2}); |
740 |
$table->insert(param => {key1 => 3, key2 => 4}); |
|
added experimental DBIx::Cus...
|
741 |
$rows = $table->select->fetch_hash_all; |
742 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], |
|
cleanup
|
743 |
"select"); |
simplified DBIx::Custom::Mod...
|
744 |
$rows = $table->select(where => {key2 => 2}, append => 'order by key1', |
745 |
column => ['key1', 'key2'])->fetch_hash_all; |
|
added experimental DBIx::Cus...
|
746 |
is_deeply($rows, [{key1 => 1, key2 => 2}], |
cleanup
|
747 |
"insert insert select"); |
simplified DBIx::Custom::Mod...
|
748 |
$table->update(param => {key1 => 3}, where => {key2 => 2}); |
749 |
$table->update(param => {key1 => 5}, where => {key2 => 4}); |
|
750 |
$rows = $table->select(where => {key2 => 2})->fetch_hash_all; |
|
added experimental DBIx::Cus...
|
751 |
is_deeply($rows, [{key1 => 3, key2 => 2}], |
cleanup
|
752 |
"update"); |
simplified DBIx::Custom::Mod...
|
753 |
$table->delete(where => {key2 => 2}); |
added experimental DBIx::Cus...
|
754 |
$rows = $table->select->fetch_hash_all; |
cleanup
|
755 |
is_deeply($rows, [{key1 => 5, key2 => 4}], "delete"); |
simplified DBIx::Custom::Mod...
|
756 |
$table->update_all(param => {key1 => 3}); |
757 |
$rows = $table->select->fetch_hash_all; |
|
cleanup
|
758 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "update_all"); |
added experimental DBIx::Cus...
|
759 |
$table->delete_all; |
760 |
$rows = $table->select->fetch_hash_all; |
|
cleanup
|
761 |
is_deeply($rows, [], "delete_all"); |
added insert, update, update...
|
762 | |
remove DBIx::Custom::Model
|
763 |
$dbi->dbh->do($CREATE_TABLE->{2}); |
renamed experimental DBIx::C...
|
764 |
$dbi->table('table2')->method( |
765 |
ppp => sub { |
|
766 |
my $self = shift; |
|
remove DBIx::Custom::Model
|
767 |
|
renamed experimental DBIx::C...
|
768 |
return $self->name; |
769 |
} |
|
770 |
); |
|
771 |
is($dbi->table('table2')->ppp, 'table2', "method"); |
|
remove DBIx::Custom::Model
|
772 | |
renamed experimental DBIx::C...
|
773 |
$dbi->table('table2')->method({ |
774 |
qqq => sub { |
|
775 |
my $self = shift; |
|
many changed
|
776 |
|
renamed experimental DBIx::C...
|
777 |
return $self->name; |
778 |
} |
|
779 |
}); |
|
780 |
is($dbi->table('table2')->qqq, 'table2', "method"); |
|
many changed
|
781 | |
782 | ||
add examples
|
783 |
test 'limit'; |
784 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
785 |
$dbi->execute($CREATE_TABLE->{0}); |
|
786 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
787 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}); |
|
788 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6}); |
|
renamed DBIx::Custom::TagPro...
|
789 |
$dbi->register_tag( |
add examples
|
790 |
limit => sub { |
791 |
my ($count, $offset) = @_; |
|
792 |
|
|
793 |
my $s = ''; |
|
794 |
$s .= "limit $count"; |
|
795 |
$s .= " offset $offset" if defined $offset; |
|
796 |
|
|
797 |
return [$s, []]; |
|
798 |
} |
|
799 |
); |
|
800 |
$rows = $dbi->select( |
|
801 |
table => 'table1', |
|
802 |
where => {key1 => 1}, |
|
803 |
append => "order by key2 {limit 1 0}" |
|
804 |
)->fetch_hash_all; |
|
cleanup
|
805 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
add examples
|
806 |
$rows = $dbi->select( |
807 |
table => 'table1', |
|
808 |
where => {key1 => 1}, |
|
809 |
append => "order by key2 {limit 2 1}" |
|
810 |
)->fetch_hash_all; |
|
cleanup
|
811 |
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]); |
add examples
|
812 |
$rows = $dbi->select( |
813 |
table => 'table1', |
|
814 |
where => {key1 => 1}, |
|
815 |
append => "order by key2 {limit 1}" |
|
816 |
)->fetch_hash_all; |
|
cleanup
|
817 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
added insert, update, update...
|
818 | |
remove DBIx::Custom::Model
|
819 |
test 'connect super'; |
820 |
{ |
|
821 |
package MyDBI; |
|
822 |
|
|
823 |
use base 'DBIx::Custom'; |
|
824 |
sub connect { |
|
825 |
my $self = shift->SUPER::connect(@_); |
|
826 |
|
|
827 |
return $self; |
|
828 |
} |
|
829 |
|
|
830 |
sub new { |
|
cleanup
|
831 |
my $self = shift->SUPER::new(@_); |
remove DBIx::Custom::Model
|
832 |
|
833 |
return $self; |
|
834 |
} |
|
835 |
} |
|
836 | ||
837 |
$dbi = MyDBI->connect($NEW_ARGS->{0}); |
|
838 |
$dbi->execute($CREATE_TABLE->{0}); |
|
839 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
cleanup
|
840 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1); |
remove DBIx::Custom::Model
|
841 | |
842 |
$dbi = MyDBI->new($NEW_ARGS->{0}); |
|
cleanup
|
843 |
$dbi->connect; |
remove DBIx::Custom::Model
|
844 |
$dbi->execute($CREATE_TABLE->{0}); |
845 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
cleanup
|
846 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1); |
added experimental DBIx::Cus...
|
847 | |
cleanup
|
848 |
{ |
849 |
package MyDBI2; |
|
850 |
|
|
851 |
use base 'DBIx::Custom'; |
|
852 |
sub connect { |
|
853 |
my $self = shift->SUPER::new(@_); |
|
854 |
$self->connect; |
|
855 |
|
|
856 |
return $self; |
|
857 |
} |
|
858 |
} |
|
859 | ||
860 |
$dbi = MyDBI->connect($NEW_ARGS->{0}); |
|
861 |
$dbi->execute($CREATE_TABLE->{0}); |
|
862 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
863 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1); |
|
added experimental DBIx::Cus...
|
864 | |
865 |
test 'end_filter'; |
|
866 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
867 |
$dbi->execute($CREATE_TABLE->{0}); |
|
868 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
869 |
$result = $dbi->select(table => 'table1'); |
|
870 |
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 }); |
|
871 |
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 }); |
|
872 |
$row = $result->fetch_first; |
|
873 |
is_deeply($row, [6, 40]); |
|
874 | ||
many changed
|
875 |
$dbi->register_filter(five_times => sub { $_[0] * 5 }); |
added experimental DBIx::Cus...
|
876 |
$result = $dbi->select(table => 'table1'); |
877 |
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 }); |
|
many changed
|
878 |
$result->end_filter({key1 => sub { $_[0] * 3 }, key2 => 'five_times' }); |
added experimental DBIx::Cus...
|
879 |
$row = $result->fetch_hash_first; |
880 |
is_deeply($row, {key1 => 6, key2 => 40}); |
|
fix select method empty wher...
|
881 | |
fix bug : filter can't over...
|
882 |
$dbi->register_filter(five_times => sub { $_[0] * 5 }); |
883 |
$dbi->apply_filter('table1', |
|
884 |
key1 => {end => sub { $_[0] * 3 } }, |
|
885 |
key2 => {end => 'five_times'} |
|
886 |
); |
|
887 |
$result = $dbi->select(table => 'table1'); |
|
888 |
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 }); |
|
889 |
$row = $result->fetch_hash_first; |
|
890 |
is_deeply($row, {key1 => 6, key2 => 40}, 'apply_filter'); |
|
891 | ||
892 |
$dbi->register_filter(five_times => sub { $_[0] * 5 }); |
|
893 |
$dbi->apply_filter('table1', |
|
894 |
key1 => {end => sub { $_[0] * 3 } }, |
|
895 |
key2 => {end => 'five_times'} |
|
896 |
); |
|
897 |
$result = $dbi->select(table => 'table1'); |
|
898 |
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 }); |
|
899 |
$result->filter(key1 => undef); |
|
900 |
$result->end_filter(key1 => undef); |
|
901 |
$row = $result->fetch_hash_first; |
|
902 |
is_deeply($row, {key1 => 1, key2 => 40}, 'apply_filter overwrite'); |
|
fix select method empty wher...
|
903 | |
904 |
test 'empty where select'; |
|
905 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
906 |
$dbi->execute($CREATE_TABLE->{0}); |
|
907 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
908 |
$result = $dbi->select(table => 'table1', where => {}); |
|
909 |
$row = $result->fetch_hash_first; |
|
910 |
is_deeply($row, {key1 => 1, key2 => 2}); |
|
911 | ||
added experimental sugar met...
|
912 |
test 'select query option'; |
913 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
914 |
$dbi->execute($CREATE_TABLE->{0}); |
|
915 |
$query = $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, query => 1); |
|
916 |
is(ref $query, 'DBIx::Custom::Query'); |
|
917 |
$query = $dbi->update(table => 'table1', where => {key1 => 1}, param => {key2 => 2}, query => 1); |
|
918 |
is(ref $query, 'DBIx::Custom::Query'); |
|
919 |
$query = $dbi->delete(table => 'table1', where => {key1 => 1}, query => 1); |
|
920 |
is(ref $query, 'DBIx::Custom::Query'); |
|
921 |
$query = $dbi->select(table => 'table1', where => {key1 => 1, key2 => 2}, query => 1); |
|
922 |
is(ref $query, 'DBIx::Custom::Query'); |
|
923 | ||
added experimental DBIx::Cus...
|
924 |
test 'DBIx::Custom::Where'; |
experimental extended select...
|
925 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
926 |
$dbi->execute($CREATE_TABLE->{0}); |
|
927 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
928 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
renamed DBIx::Custom::TagPro...
|
929 |
$where = $dbi->where->clause(['and', '{= key1}', '{= key2}']); |
930 |
is("$where", "where ( {= key1} and {= key2} )", 'no param'); |
|
931 | ||
added experimental DBIx::Cus...
|
932 |
$where = $dbi->where |
added test
|
933 |
->clause(['and', '{= key1}', '{= key2}']) |
added experimental DBIx::Cus...
|
934 |
->param({key1 => 1}); |
added test
|
935 | |
experimental extended select...
|
936 |
$result = $dbi->select( |
937 |
table => 'table1', |
|
added experimental DBIx::Cus...
|
938 |
where => $where |
experimental extended select...
|
939 |
); |
940 |
$row = $result->fetch_hash_all; |
|
941 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
942 | ||
added experimental DBIx::Cus...
|
943 |
$where = $dbi->where |
added test
|
944 |
->clause(['and', '{= key1}', '{= key2}']) |
added experimental DBIx::Cus...
|
945 |
->param({key1 => 1, key2 => 2}); |
experimental extended select...
|
946 |
$result = $dbi->select( |
947 |
table => 'table1', |
|
added experimental DBIx::Cus...
|
948 |
where => $where |
experimental extended select...
|
949 |
); |
950 |
$row = $result->fetch_hash_all; |
|
951 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
952 | ||
added experimental DBIx::Cus...
|
953 |
$where = $dbi->where |
added test
|
954 |
->clause(['and', '{= key1}', '{= key2}']) |
added experimental DBIx::Cus...
|
955 |
->param({}); |
experimental extended select...
|
956 |
$result = $dbi->select( |
957 |
table => 'table1', |
|
added experimental DBIx::Cus...
|
958 |
where => $where, |
experimental extended select...
|
959 |
); |
960 |
$row = $result->fetch_hash_all; |
|
961 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
962 | ||
added experimental DBIx::Cus...
|
963 |
$where = $dbi->where |
added test
|
964 |
->clause(['and', ['or', '{> key1}', '{< key1}'], '{= key2}']) |
added experimental DBIx::Cus...
|
965 |
->param({key1 => [0, 3], key2 => 2}); |
experimental extended select...
|
966 |
$result = $dbi->select( |
967 |
table => 'table1', |
|
added experimental DBIx::Cus...
|
968 |
where => $where, |
added experimental not_exist...
|
969 |
); |
experimental extended select...
|
970 |
$row = $result->fetch_hash_all; |
971 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
972 | ||
added experimental DBIx::Cus...
|
973 |
$where = $dbi->where; |
974 |
$result = $dbi->select( |
|
975 |
table => 'table1', |
|
976 |
where => $where |
|
977 |
); |
|
978 |
$row = $result->fetch_hash_all; |
|
979 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
980 | ||
experimental extended select...
|
981 |
eval { |
added experimental DBIx::Cus...
|
982 |
$where = $dbi->where |
added test
|
983 |
->clause(['uuu']); |
experimental extended select...
|
984 |
$result = $dbi->select( |
985 |
table => 'table1', |
|
added experimental DBIx::Cus...
|
986 |
where => $where |
experimental extended select...
|
987 |
); |
988 |
}; |
|
989 |
ok($@); |
|
990 | ||
added experimental DBIx::Cus...
|
991 |
$where = $dbi->where; |
992 |
is("$where", ''); |
|
993 | ||
added test
|
994 |
$where = $dbi->where |
995 |
->clause(['or', ('{= key1}') x 2]) |
|
996 |
->param({key1 => [1, 3]}); |
|
997 |
$result = $dbi->select( |
|
998 |
table => 'table1', |
|
999 |
where => $where, |
|
1000 |
); |
|
1001 |
$row = $result->fetch_hash_all; |
|
1002 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
1003 | ||
1004 |
$where = $dbi->where |
|
1005 |
->clause(['or', ('{= key1}') x 2]) |
|
1006 |
->param({key1 => [1]}); |
|
1007 |
$result = $dbi->select( |
|
1008 |
table => 'table1', |
|
1009 |
where => $where, |
|
1010 |
); |
|
1011 |
$row = $result->fetch_hash_all; |
|
1012 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
1013 | ||
1014 |
$where = $dbi->where |
|
1015 |
->clause(['or', ('{= key1}') x 2]) |
|
1016 |
->param({key1 => 1}); |
|
1017 |
$result = $dbi->select( |
|
1018 |
table => 'table1', |
|
1019 |
where => $where, |
|
1020 |
); |
|
1021 |
$row = $result->fetch_hash_all; |
|
1022 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
added experimental DBIx::Cus...
|
1023 | |
many changed
|
1024 |
$where = $dbi->where |
1025 |
->clause('{= key1}') |
|
1026 |
->param({key1 => 1}); |
|
1027 |
$result = $dbi->select( |
|
1028 |
table => 'table1', |
|
1029 |
where => $where, |
|
1030 |
); |
|
1031 |
$row = $result->fetch_hash_all; |
|
1032 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
1033 | ||
1034 |
$where = $dbi->where |
|
1035 |
->clause('{= key1} {= key2}') |
|
1036 |
->param({key1 => 1}); |
|
1037 |
eval{$where->to_string}; |
|
1038 |
like($@, qr/one column/); |
|
1039 | ||
added experimental not_exist...
|
1040 |
$where = $dbi->where |
1041 |
->clause('{= key1}') |
|
1042 |
->param([]); |
|
1043 |
eval{$where->to_string}; |
|
1044 |
like($@, qr/Parameter/); |
|
1045 | ||
1046 |
$where = $dbi->where |
|
1047 |
->clause(['or', ('{= key1}') x 3]) |
|
1048 |
->param({key1 => [$dbi->not_exists, 1, 3]}); |
|
1049 |
$result = $dbi->select( |
|
1050 |
table => 'table1', |
|
1051 |
where => $where, |
|
1052 |
); |
|
1053 |
$row = $result->fetch_hash_all; |
|
1054 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists'); |
|
1055 | ||
1056 |
$where = $dbi->where |
|
1057 |
->clause(['or', ('{= key1}') x 3]) |
|
1058 |
->param({key1 => [1, $dbi->not_exists, 3]}); |
|
1059 |
$result = $dbi->select( |
|
1060 |
table => 'table1', |
|
1061 |
where => $where, |
|
1062 |
); |
|
1063 |
$row = $result->fetch_hash_all; |
|
1064 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists'); |
|
1065 | ||
1066 |
$where = $dbi->where |
|
1067 |
->clause(['or', ('{= key1}') x 3]) |
|
1068 |
->param({key1 => [1, 3, $dbi->not_exists]}); |
|
1069 |
$result = $dbi->select( |
|
1070 |
table => 'table1', |
|
1071 |
where => $where, |
|
1072 |
); |
|
1073 |
$row = $result->fetch_hash_all; |
|
1074 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists'); |
|
1075 | ||
1076 |
$where = $dbi->where |
|
1077 |
->clause(['or', ('{= key1}') x 3]) |
|
1078 |
->param({key1 => [1, $dbi->not_exists, $dbi->not_exists]}); |
|
1079 |
$result = $dbi->select( |
|
1080 |
table => 'table1', |
|
1081 |
where => $where, |
|
1082 |
); |
|
1083 |
$row = $result->fetch_hash_all; |
|
1084 |
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists'); |
|
1085 | ||
1086 |
$where = $dbi->where |
|
1087 |
->clause(['or', ('{= key1}') x 3]) |
|
1088 |
->param({key1 => [$dbi->not_exists, 1, $dbi->not_exists]}); |
|
1089 |
$result = $dbi->select( |
|
1090 |
table => 'table1', |
|
1091 |
where => $where, |
|
1092 |
); |
|
1093 |
$row = $result->fetch_hash_all; |
|
1094 |
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists'); |
|
1095 | ||
1096 |
$where = $dbi->where |
|
1097 |
->clause(['or', ('{= key1}') x 3]) |
|
1098 |
->param({key1 => [$dbi->not_exists, $dbi->not_exists, 1]}); |
|
1099 |
$result = $dbi->select( |
|
1100 |
table => 'table1', |
|
1101 |
where => $where, |
|
1102 |
); |
|
1103 |
$row = $result->fetch_hash_all; |
|
1104 |
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists'); |
|
1105 | ||
1106 |
$where = $dbi->where |
|
1107 |
->clause(['or', ('{= key1}') x 3]) |
|
1108 |
->param({key1 => [$dbi->not_exists, $dbi->not_exists, $dbi->not_exists]}); |
|
1109 |
$result = $dbi->select( |
|
1110 |
table => 'table1', |
|
1111 |
where => $where, |
|
1112 |
); |
|
1113 |
$row = $result->fetch_hash_all; |
|
1114 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists'); |
|
1115 | ||
make delete() using where ob...
|
1116 |
$where = $dbi->where |
1117 |
->clause(['or', ('{= key1}') x 3]) |
|
1118 |
->param({key1 => []}); |
|
1119 |
$result = $dbi->select( |
|
1120 |
table => 'table1', |
|
1121 |
where => $where, |
|
1122 |
); |
|
1123 |
$row = $result->fetch_hash_all; |
|
1124 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists'); |
|
1125 | ||
1126 |
$where = $dbi->where |
|
1127 |
->clause(['and', '{> key1}', '{< key1}' ]) |
|
1128 |
->param({key1 => [2, $dbi->not_exists]}); |
|
1129 |
$result = $dbi->select( |
|
1130 |
table => 'table1', |
|
1131 |
where => $where, |
|
1132 |
); |
|
1133 |
$row = $result->fetch_hash_all; |
|
1134 |
is_deeply($row, [{key1 => 3, key2 => 4}], 'not_exists'); |
|
1135 | ||
1136 |
$where = $dbi->where |
|
1137 |
->clause(['and', '{> key1}', '{< key1}' ]) |
|
1138 |
->param({key1 => [$dbi->not_exists, 2]}); |
|
1139 |
$result = $dbi->select( |
|
1140 |
table => 'table1', |
|
1141 |
where => $where, |
|
1142 |
); |
|
1143 |
$row = $result->fetch_hash_all; |
|
1144 |
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists'); |
|
1145 | ||
1146 |
$where = $dbi->where |
|
1147 |
->clause(['and', '{> key1}', '{< key1}' ]) |
|
1148 |
->param({key1 => [$dbi->not_exists, $dbi->not_exists]}); |
|
1149 |
$result = $dbi->select( |
|
1150 |
table => 'table1', |
|
1151 |
where => $where, |
|
1152 |
); |
|
1153 |
$row = $result->fetch_hash_all; |
|
1154 |
is_deeply($row, [{key1 => 1, key2 => 2},{key1 => 3, key2 => 4}], 'not_exists'); |
|
1155 | ||
1156 |
$where = $dbi->where |
|
1157 |
->clause(['and', '{> key1}', '{< key1}' ]) |
|
1158 |
->param({key1 => [0, 2]}); |
|
1159 |
$result = $dbi->select( |
|
1160 |
table => 'table1', |
|
1161 |
where => $where, |
|
1162 |
); |
|
1163 |
$row = $result->fetch_hash_all; |
|
1164 |
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists'); |
|
1165 | ||
renamed dbi_options to dbi_o...
|
1166 |
test 'dbi_option default'; |
added experimental DBIx::Cus...
|
1167 |
$dbi = DBIx::Custom->new; |
renamed dbi_options to dbi_o...
|
1168 |
is_deeply($dbi->dbi_option, {}); |
added experimental DBIx::Cus...
|
1169 | |
added register_tag_processor
|
1170 |
test 'register_tag_processor'; |
1171 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1172 |
$dbi->register_tag_processor( |
|
1173 |
a => sub { 1 } |
|
1174 |
); |
|
1175 |
is($dbi->query_builder->tag_processors->{a}->(), 1); |
|
added experimental DBIx::Cus...
|
1176 | |
renamed DBIx::Custom::TagPro...
|
1177 |
test 'register_tag'; |
1178 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1179 |
$dbi->register_tag( |
|
1180 |
b => sub { 2 } |
|
1181 |
); |
|
1182 |
is($dbi->query_builder->tags->{b}->(), 2); |
|
1183 | ||
added table not specified ex...
|
1184 |
test 'table not specify exception'; |
1185 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1186 |
eval {$dbi->insert}; |
|
1187 |
like($@, qr/table/); |
|
1188 |
eval {$dbi->update}; |
|
1189 |
like($@, qr/table/); |
|
1190 |
eval {$dbi->delete}; |
|
1191 |
like($@, qr/table/); |
|
1192 |
eval {$dbi->select}; |
|
1193 |
like($@, qr/table/); |
|
many changed
|
1194 | |
1195 | ||
1196 |
test 'more tests'; |
|
1197 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1198 |
eval{$dbi->apply_filter('table', 'column', [])}; |
|
1199 |
like($@, qr/apply_filter/); |
|
1200 | ||
1201 |
eval{$dbi->apply_filter('table', 'column', {outer => 2})}; |
|
1202 |
like($@, qr/apply_filter/); |
|
1203 | ||
1204 |
$dbi->apply_filter( |
|
1205 | ||
1206 |
); |
|
1207 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1208 |
$dbi->execute($CREATE_TABLE->{0}); |
|
1209 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1210 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
1211 |
$dbi->apply_filter('table1', 'key2', |
|
1212 |
{in => sub { $_[0] * 3 }, out => sub { $_[0] * 2 }}); |
|
1213 |
$rows = $dbi->select(table => 'table1', where => {key2 => 1})->fetch_hash_all; |
|
1214 |
is_deeply($rows, [{key1 => 1, key2 => 6}]); |
|
1215 | ||
1216 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1217 |
$dbi->execute($CREATE_TABLE->{0}); |
|
1218 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1219 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
1220 |
$dbi->apply_filter('table1', 'key2', {}); |
|
1221 |
$rows = $dbi->select(table => 'table1', where => {key2 => 2})->fetch_hash_all; |
|
1222 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
1223 | ||
1224 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1225 |
eval {$dbi->apply_filter('table1', 'key2', {out => 'no'})}; |
|
1226 |
like($@, qr/not registered/); |
|
1227 |
eval {$dbi->apply_filter('table1', 'key2', {in => 'no'})}; |
|
1228 |
like($@, qr/not registered/); |
|
renamed helper to method.
|
1229 |
$dbi->method({one => sub { 1 }}); |
many changed
|
1230 |
is($dbi->one, 1); |
1231 | ||
1232 |
eval{DBIx::Custom->connect()}; |
|
1233 |
like($@, qr/connect/); |
|
1234 | ||
1235 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1236 |
$dbi->execute($CREATE_TABLE->{0}); |
|
1237 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
1238 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, |
|
1239 |
filter => {key1 => 'twice'}); |
|
1240 |
$row = $dbi->select(table => 'table1')->fetch_hash_first; |
|
1241 |
is_deeply($row, {key1 => 2, key2 => 2}); |
|
1242 |
eval {$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, |
|
1243 |
filter => {key1 => 'no'}) }; |
|
1244 |
like($@, qr//); |
|
1245 | ||
1246 |
$dbi->register_filter(one => sub { }); |
|
1247 |
$dbi->default_fetch_filter('one'); |
|
1248 |
ok($dbi->default_fetch_filter); |
|
1249 |
$dbi->default_bind_filter('one'); |
|
1250 |
ok($dbi->default_bind_filter); |
|
1251 |
eval{$dbi->default_fetch_filter('no')}; |
|
1252 |
like($@, qr/not registered/); |
|
1253 |
eval{$dbi->default_bind_filter('no')}; |
|
1254 |
like($@, qr/not registered/); |
|
1255 |
$dbi->default_bind_filter(undef); |
|
1256 |
ok(!defined $dbi->default_bind_filter); |
|
1257 |
$dbi->default_fetch_filter(undef); |
|
1258 |
ok(!defined $dbi->default_fetch_filter); |
|
1259 |
eval {$dbi->execute('select * from table1 {= author') }; |
|
1260 |
like($@, qr/Tag not finished/); |
|
1261 | ||
1262 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1263 |
$dbi->execute($CREATE_TABLE->{0}); |
|
1264 |
$dbi->register_filter(one => sub { 1 }); |
|
1265 |
$result = $dbi->select(table => 'table1'); |
|
1266 |
eval {$result->filter(key1 => 'no')}; |
|
1267 |
like($@, qr/not registered/); |
|
1268 |
eval {$result->end_filter(key1 => 'no')}; |
|
1269 |
like($@, qr/not registered/); |
|
1270 |
$result->default_filter(undef); |
|
1271 |
ok(!defined $result->default_filter); |
|
1272 |
$result->default_filter('one'); |
|
1273 |
is($result->default_filter->(), 1); |
|
1274 | ||
1275 |
$dbi->table('book'); |
|
1276 |
eval{$dbi->table('book')->no_exists}; |
|
renamed dbi_options to dbi_o...
|
1277 |
like($@, qr/locate/); |
1278 | ||
1279 |
test 'dbi_option'; |
|
1280 |
$dbi = DBIx::Custom->connect(data_source => 'dbi:SQLite:dbname=:memory:', |
|
1281 |
dbi_option => {PrintError => 1}); |
|
1282 |
ok($dbi->dbh->{PrintError}); |
|
1283 |
$dbi = DBIx::Custom->connect(data_source => 'dbi:SQLite:dbname=:memory:', |
|
1284 |
dbi_options => {PrintError => 1}); |
|
1285 |
ok($dbi->dbh->{PrintError}); |
|
1286 | ||
added experimental DBIx::Cus...
|
1287 |
test 'DBIx::Custom::Result stash()'; |
1288 |
$result = DBIx::Custom::Result->new; |
|
1289 |
is_deeply($result->stash, {}, 'default'); |
|
1290 |
$result->stash->{foo} = 1; |
|
1291 |
is($result->stash->{foo}, 1, 'get and set'); |
|
table object call dbi object...
|
1292 | |
1293 |
test 'base_table'; |
|
1294 |
$dbi = DBIx::Custom->new; |
|
1295 |
$dbi->base_table->method( |
|
removed experimental DBIx::C...
|
1296 |
twice => sub { |
1297 |
my $self = shift; |
|
1298 |
|
|
1299 |
return $_[0] * 2; |
|
1300 |
} |
|
table object call dbi object...
|
1301 |
); |
1302 |
$table = $dbi->table('book'); |
|
1303 |
$table->method( |
|
removed experimental DBIx::C...
|
1304 |
three_times => sub { |
1305 |
my $self = shift; |
|
1306 |
return $_[0] * 3; |
|
1307 |
} |
|
table object call dbi object...
|
1308 |
); |
removed experimental DBIx::C...
|
1309 |
is($table->base_twice(1), 2, 'method'); |
1310 |
is($table->twice(1), 2, 'inherit method'); |
|
1311 |
is($table->three_times(1), 3, 'child table method'); |
|
1312 |
eval {$dbi->base_two}; |
|
table object call dbi object...
|
1313 |
ok($@); |
1314 | ||
fix bug : filter can't over...
|
1315 |
test 'filter __ expression'; |
1316 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1317 |
$dbi->execute('create table company (id, name, location_id)'); |
|
1318 |
$dbi->execute('create table location (id, name)'); |
|
1319 |
$dbi->apply_filter('location', |
|
1320 |
name => {in => sub { uc $_[0] } } |
|
1321 |
); |
|
1322 | ||
1323 |
$dbi->insert(table => 'company', param => {id => 1, name => 'a', location_id => 2}); |
|
1324 |
$dbi->insert(table => 'location', param => {id => 2, name => 'b'}); |
|
1325 | ||
1326 |
$result = $dbi->select( |
|
1327 |
table => ['company', 'location'], relation => {'company.location_id' => 'location.id'}, |
|
1328 |
column => ['location.name as location__name'] |
|
1329 |
); |
|
1330 |
is($result->fetch_first->[0], 'B'); |
|
1331 | ||
add experimental selection o...
|
1332 |
test 'selection'; |
1333 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1334 |
$dbi->execute($CREATE_TABLE->{0}); |
|
1335 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1336 |
$result = $dbi->select(selection => '* from table1', where => {key1 => 1}); |
|
1337 |
is_deeply($result->fetch_hash_all, [{key1 => 1, key2 => 2}]); |