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 |
|
cleanup
|
19 |
sub test { "# $_[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 | |
233 | ||
removed register_format()
|
234 |
test 'update'; |
235 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
236 |
$dbi->execute($CREATE_TABLE->{1}); |
|
237 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
238 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
239 |
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}); |
|
add tests
|
240 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
241 |
$rows = $result->fetch_hash_all; |
242 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
243 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
cleanup
|
244 |
"basic"); |
removed register_format()
|
245 |
|
246 |
$dbi->execute("delete from table1"); |
|
247 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
248 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
249 |
$dbi->update(table => 'table1', param => {key2 => 12}, where => {key2 => 2, key3 => 3}); |
|
add tests
|
250 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
251 |
$rows = $result->fetch_hash_all; |
252 |
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5}, |
|
253 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
cleanup
|
254 |
"update key same as search key"); |
removed register_format()
|
255 | |
add tests
|
256 |
$dbi->update(table => 'table1', param => {key2 => [12]}, where => {key2 => 2, key3 => 3}); |
257 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
258 |
$rows = $result->fetch_hash_all; |
|
259 |
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5}, |
|
260 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
cleanup
|
261 |
"update key same as search key : param is array ref"); |
add tests
|
262 | |
removed register_format()
|
263 |
$dbi->execute("delete from table1"); |
264 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
265 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
266 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
267 |
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}, |
|
many changed
|
268 |
filter => {key2 => sub { $_[0] * 2 }}); |
add tests
|
269 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
270 |
$rows = $result->fetch_hash_all; |
271 |
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5}, |
|
272 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
cleanup
|
273 |
"filter"); |
removed register_format()
|
274 | |
275 |
$result = $dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}, append => ' '); |
|
276 | ||
changed argument of tag proc...
|
277 |
eval{$dbi->update(table => 'table1', noexist => 1)}; |
cleanup
|
278 |
like($@, qr/noexist/, "invalid argument"); |
changed argument of tag proc...
|
279 | |
280 |
eval{$dbi->update(table => 'table1')}; |
|
cleanup
|
281 |
like($@, qr/where/, "not contain where"); |
changed argument of tag proc...
|
282 | |
283 | ||
removed register_format()
|
284 |
test 'update_all'; |
285 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
286 |
$dbi->execute($CREATE_TABLE->{1}); |
|
287 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
288 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
289 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
290 |
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'}); |
|
add tests
|
291 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
292 |
$rows = $result->fetch_hash_all; |
293 |
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5}, |
|
294 |
{key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}], |
|
cleanup
|
295 |
"filter"); |
removed register_format()
|
296 | |
297 | ||
298 |
test 'delete'; |
|
299 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
300 |
$dbi->execute($CREATE_TABLE->{0}); |
|
301 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
302 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
303 |
$dbi->delete(table => 'table1', where => {key1 => 1}); |
|
add tests
|
304 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
305 |
$rows = $result->fetch_hash_all; |
cleanup
|
306 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "basic"); |
removed register_format()
|
307 | |
308 |
$dbi->execute("delete from table1;"); |
|
309 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
310 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
311 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
312 |
$dbi->delete(table => 'table1', where => {key2 => 1}, filter => {key2 => 'twice'}); |
|
add tests
|
313 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
314 |
$rows = $result->fetch_hash_all; |
cleanup
|
315 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter"); |
removed register_format()
|
316 | |
317 |
$dbi->delete(table => 'table1', where => {key1 => 1}, append => ' '); |
|
318 | ||
319 |
$dbi->delete_all(table => 'table1'); |
|
320 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
321 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
322 |
$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2}); |
|
323 |
$rows = $dbi->select(table => 'table1')->fetch_hash_all; |
|
cleanup
|
324 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "delete multi key"); |
removed register_format()
|
325 | |
changed argument of tag proc...
|
326 |
eval{$dbi->delete(table => 'table1', noexist => 1)}; |
cleanup
|
327 |
like($@, qr/noexist/, "invalid argument"); |
changed argument of tag proc...
|
328 | |
329 | ||
removed register_format()
|
330 |
test 'delete error'; |
331 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
332 |
$dbi->execute($CREATE_TABLE->{0}); |
|
333 |
eval{$dbi->delete(table => 'table1')}; |
|
add tests
|
334 |
like($@, qr/"where" argument must be specified and contains the pairs of column name and value/, |
cleanup
|
335 |
"where key-value pairs not specified"); |
removed register_format()
|
336 | |
337 |
test 'delete_all'; |
|
338 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
339 |
$dbi->execute($CREATE_TABLE->{0}); |
|
340 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
341 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
342 |
$dbi->delete_all(table => 'table1'); |
|
add tests
|
343 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
344 |
$rows = $result->fetch_hash_all; |
cleanup
|
345 |
is_deeply($rows, [], "basic"); |
removed register_format()
|
346 | |
347 | ||
348 |
test 'select'; |
|
349 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
350 |
$dbi->execute($CREATE_TABLE->{0}); |
|
351 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
352 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
353 |
$rows = $dbi->select(table => 'table1')->fetch_hash_all; |
|
354 |
is_deeply($rows, [{key1 => 1, key2 => 2}, |
|
cleanup
|
355 |
{key1 => 3, key2 => 4}], "table"); |
removed register_format()
|
356 | |
update document
|
357 |
$rows = $dbi->select(table => 'table1', column => ['key1'])->fetch_hash_all; |
cleanup
|
358 |
is_deeply($rows, [{key1 => 1}, {key1 => 3}], "table and columns and where key"); |
removed register_format()
|
359 | |
360 |
$rows = $dbi->select(table => 'table1', where => {key1 => 1})->fetch_hash_all; |
|
cleanup
|
361 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "table and columns and where key"); |
removed register_format()
|
362 | |
update document
|
363 |
$rows = $dbi->select(table => 'table1', where => ['{= key1} and {= key2}', {key1 => 1, key2 => 2}])->fetch_hash_all; |
cleanup
|
364 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "table and columns and where string"); |
update document
|
365 | |
update document
|
366 |
$rows = $dbi->select(table => 'table1', column => ['key1'], where => {key1 => 3})->fetch_hash_all; |
cleanup
|
367 |
is_deeply($rows, [{key1 => 3}], "table and columns and where key"); |
removed register_format()
|
368 | |
369 |
$rows = $dbi->select(table => 'table1', append => "order by key1 desc limit 1")->fetch_hash_all; |
|
cleanup
|
370 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "append statement"); |
removed register_format()
|
371 | |
372 |
$dbi->register_filter(decrement => sub { $_[0] - 1 }); |
|
update document
|
373 |
$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'}) |
removed register_format()
|
374 |
->fetch_hash_all; |
cleanup
|
375 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "filter"); |
removed register_format()
|
376 | |
377 |
$dbi->execute($CREATE_TABLE->{2}); |
|
378 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
379 |
$rows = $dbi->select( |
|
380 |
table => [qw/table1 table2/], |
|
update document
|
381 |
column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'], |
removed register_format()
|
382 |
where => {'table1.key2' => 2}, |
added commit method
|
383 |
relation => {'table1.key1' => 'table2.key1'} |
removed register_format()
|
384 |
)->fetch_hash_all; |
cleanup
|
385 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : exists where"); |
added commit method
|
386 | |
387 |
$rows = $dbi->select( |
|
388 |
table => [qw/table1 table2/], |
|
389 |
column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'], |
|
390 |
relation => {'table1.key1' => 'table2.key1'} |
|
391 |
)->fetch_hash_all; |
|
cleanup
|
392 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : no exists where"); |
removed register_format()
|
393 | |
changed argument of tag proc...
|
394 |
eval{$dbi->select(table => 'table1', noexist => 1)}; |
cleanup
|
395 |
like($@, qr/noexist/, "invalid argument"); |
changed argument of tag proc...
|
396 | |
397 | ||
removed register_format()
|
398 |
test 'fetch filter'; |
399 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
400 |
$dbi->register_filter( |
|
401 |
twice => sub { $_[0] * 2 }, |
|
402 |
three_times => sub { $_[0] * 3 } |
|
403 |
); |
|
404 |
$dbi->default_fetch_filter('twice'); |
|
405 |
$dbi->execute($CREATE_TABLE->{0}); |
|
406 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
407 |
$result = $dbi->select(table => 'table1'); |
|
408 |
$result->filter({key1 => 'three_times'}); |
|
removed reconnect method
|
409 |
$row = $result->fetch_hash_first; |
cleanup
|
410 |
is_deeply($row, {key1 => 3, key2 => 4}, "default_fetch_filter and filter"); |
removed register_format()
|
411 | |
412 |
test 'filters'; |
|
413 |
$dbi = DBIx::Custom->new; |
|
414 | ||
update document
|
415 |
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')), |
cleanup
|
416 |
'あ', "decode_utf8"); |
removed register_format()
|
417 | |
418 |
is($dbi->filters->{encode_utf8}->('あ'), |
|
cleanup
|
419 |
encode_utf8('あ'), "encode_utf8"); |
removed register_format()
|
420 | |
added commit method
|
421 |
test 'transaction'; |
422 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
423 |
$dbi->execute($CREATE_TABLE->{0}); |
|
removed DBIx::Custom commit ...
|
424 |
$dbi->dbh->begin_work; |
added commit method
|
425 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
426 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
removed DBIx::Custom commit ...
|
427 |
$dbi->dbh->commit; |
added commit method
|
428 |
$result = $dbi->select(table => 'table1'); |
429 |
is_deeply(scalar $result->fetch_hash_all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}], |
|
cleanup
|
430 |
"commit"); |
added commit method
|
431 | |
432 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
433 |
$dbi->execute($CREATE_TABLE->{0}); |
|
removed DBIx::Custom commit ...
|
434 |
$dbi->dbh->begin_work(0); |
added commit method
|
435 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
removed DBIx::Custom commit ...
|
436 |
$dbi->dbh->rollback; |
added commit method
|
437 | |
438 |
$result = $dbi->select(table => 'table1'); |
|
cleanup
|
439 |
ok(! $result->fetch_first, "rollback"); |
add cache attribute
|
440 | |
441 |
test 'cache'; |
|
442 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
443 |
$dbi->execute($CREATE_TABLE->{0}); |
|
renamed build_query to creat...
|
444 |
$source = 'select * from table1 where {= key1} and {= key2};'; |
445 |
$dbi->create_query($source); |
|
446 |
is_deeply($dbi->{_cached}->{$source}, |
|
cleanup
|
447 |
{sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2']}, "cache"); |
add cache attribute
|
448 | |
449 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
450 |
$dbi->execute($CREATE_TABLE->{0}); |
|
451 |
$dbi->{_cached} = {}; |
|
452 |
$dbi->cache(0); |
|
453 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
454 |
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache'); |
|
455 | ||
add tests
|
456 |
test 'execute'; |
457 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
458 |
$dbi->execute($CREATE_TABLE->{0}); |
|
removed experimental registe...
|
459 |
{ |
460 |
local $Carp::Verbose = 0; |
|
461 |
eval{$dbi->execute('select * frm table1')}; |
|
cleanup
|
462 |
like($@, qr/\Qselect * frm table1;/, "fail prepare"); |
463 |
like($@, qr/\.t /, "fail : not verbose"); |
|
removed experimental registe...
|
464 |
} |
465 |
{ |
|
466 |
local $Carp::Verbose = 1; |
|
467 |
eval{$dbi->execute('select * frm table1')}; |
|
cleanup
|
468 |
like($@, qr/Custom.*\.t /s, "fail : verbose"); |
removed experimental registe...
|
469 |
} |
add tests
|
470 | |
471 |
eval{$dbi->execute('select * from table1', no_exists => 1)}; |
|
cleanup
|
472 |
like($@, qr/\Q"no_exists" is invalid argument/, "invald SQL"); |
add tests
|
473 | |
474 |
$query = $dbi->create_query('select * from table1 where {= key1}'); |
|
475 |
$dbi->dbh->disconnect; |
|
476 |
eval{$dbi->execute($query, param => {key1 => {a => 1}})}; |
|
cleanup
|
477 |
ok($@, "execute fail"); |
add tests
|
478 | |
removed experimental registe...
|
479 |
{ |
480 |
local $Carp::Verbose = 0; |
|
481 |
eval{$dbi->create_query('select * from table1 where {0 key1}')}; |
|
cleanup
|
482 |
like($@, qr/\Q.t /, "caller spec : not vebose"); |
removed experimental registe...
|
483 |
} |
484 |
{ |
|
485 |
local $Carp::Verbose = 1; |
|
486 |
eval{$dbi->create_query('select * from table1 where {0 key1}')}; |
|
cleanup
|
487 |
like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose"); |
removed experimental registe...
|
488 |
} |
cleanup
|
489 | |
490 | ||
491 |
test 'transaction'; |
|
492 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
493 |
$dbi->execute($CREATE_TABLE->{0}); |
|
494 | ||
495 |
$dbi->begin_work; |
|
496 | ||
497 |
eval { |
|
498 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
499 |
die "Error"; |
|
500 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
501 |
}; |
|
502 | ||
503 |
$dbi->rollback if $@; |
|
504 | ||
505 |
$result = $dbi->select(table => 'table1'); |
|
506 |
$rows = $result->fetch_hash_all; |
|
cleanup
|
507 |
is_deeply($rows, [], "rollback"); |
cleanup
|
508 | |
509 |
$dbi->begin_work; |
|
510 | ||
511 |
eval { |
|
512 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
513 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
514 |
}; |
|
515 | ||
516 |
$dbi->commit unless $@; |
|
517 | ||
518 |
$result = $dbi->select(table => 'table1'); |
|
519 |
$rows = $result->fetch_hash_all; |
|
cleanup
|
520 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "commit"); |
cleanup
|
521 | |
522 |
$dbi->dbh->{AutoCommit} = 0; |
|
523 |
eval{ $dbi->begin_work }; |
|
cleanup
|
524 |
ok($@, "exception"); |
cleanup
|
525 |
$dbi->dbh->{AutoCommit} = 1; |
526 | ||
added helper method
|
527 | |
528 |
test 'helper'; |
|
529 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
530 |
$dbi->helper( |
|
531 |
one => sub { 1 } |
|
532 |
); |
|
533 |
$dbi->helper( |
|
534 |
two => sub { 2 } |
|
535 |
); |
|
536 |
$dbi->helper({ |
|
537 |
twice => sub { |
|
538 |
my $self = shift; |
|
539 |
return $_[0] * 2; |
|
540 |
} |
|
541 |
}); |
|
542 | ||
cleanup
|
543 |
is($dbi->one, 1, "first"); |
544 |
is($dbi->two, 2, "second"); |
|
545 |
is($dbi->twice(5), 10 , "second"); |
|
added helper method
|
546 | |
547 |
eval {$dbi->XXXXXX}; |
|
cleanup
|
548 |
like($@, qr/\QCan't locate object method "XXXXXX" via "DBIx::Custom"/, "not exists"); |
added helper method
|
549 | |
renamed auto_filter to apply...
|
550 |
test 'out filter'; |
added auto_filter method
|
551 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
552 |
$dbi->execute($CREATE_TABLE->{0}); |
|
553 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
554 |
$dbi->register_filter(three_times => sub { $_[0] * 3}); |
|
renamed auto_filter to apply...
|
555 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
556 |
'table1', 'key1' => {out => 'twice', in => 'three_times'}, |
557 |
'key2' => {out => 'three_times', in => 'twice'}); |
|
added auto_filter method
|
558 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
559 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
560 |
$row = $result->fetch_hash_first; |
|
cleanup
|
561 |
is_deeply($row, {key1 => 2, key2 => 6}, "insert"); |
renamed auto_filter to apply...
|
562 |
$result = $dbi->select(table => 'table1'); |
563 |
$row = $result->fetch_hash_first; |
|
cleanup
|
564 |
is_deeply($row, {key1 => 6, key2 => 12}, "insert"); |
added auto_filter method
|
565 | |
566 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
567 |
$dbi->execute($CREATE_TABLE->{0}); |
|
568 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
renamed auto_filter to apply...
|
569 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
570 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
added auto_filter method
|
571 |
); |
renamed auto_filter to apply...
|
572 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => undef}); |
added auto_filter method
|
573 |
$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2}); |
574 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
575 |
$row = $result->fetch_hash_first; |
|
cleanup
|
576 |
is_deeply($row, {key1 => 4, key2 => 2}, "update"); |
added auto_filter method
|
577 | |
578 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
579 |
$dbi->execute($CREATE_TABLE->{0}); |
|
580 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
renamed auto_filter to apply...
|
581 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
582 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
added auto_filter method
|
583 |
); |
renamed auto_filter to apply...
|
584 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1=> undef}); |
added auto_filter method
|
585 |
$dbi->delete(table => 'table1', where => {key1 => 1}); |
586 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
587 |
$rows = $result->fetch_hash_all; |
|
cleanup
|
588 |
is_deeply($rows, [], "delete"); |
added auto_filter method
|
589 | |
590 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
591 |
$dbi->execute($CREATE_TABLE->{0}); |
|
592 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
renamed auto_filter to apply...
|
593 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
594 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
added auto_filter method
|
595 |
); |
renamed auto_filter to apply...
|
596 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef}); |
added auto_filter method
|
597 |
$result = $dbi->select(table => 'table1', where => {key1 => 1}); |
598 |
$result->filter({'key2' => 'twice'}); |
|
599 |
$rows = $result->fetch_hash_all; |
|
cleanup
|
600 |
is_deeply($rows, [{key1 => 4, key2 => 4}], "select"); |
added auto_filter method
|
601 | |
602 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
603 |
$dbi->execute($CREATE_TABLE->{0}); |
|
604 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
renamed auto_filter to apply...
|
605 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
606 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
added auto_filter method
|
607 |
); |
renamed auto_filter to apply...
|
608 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef}); |
added auto_filter method
|
609 |
$result = $dbi->execute("select * from table1 where {= key1} and {= key2};", |
610 |
param => {key1 => 1, key2 => 2}, |
|
renamed auto_filter to apply...
|
611 |
table => ['table1']); |
added auto_filter method
|
612 |
$rows = $result->fetch_hash_all; |
cleanup
|
613 |
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute"); |
added auto_filter method
|
614 | |
615 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
616 |
$dbi->execute($CREATE_TABLE->{0}); |
|
617 |
$dbi->execute($CREATE_TABLE->{2}); |
|
618 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
619 |
$dbi->register_filter(three_times => sub { $_[0] * 3 }); |
|
renamed auto_filter to apply...
|
620 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
621 |
'table1', 'key2' => {out => 'twice', in => 'twice'} |
added auto_filter method
|
622 |
); |
renamed auto_filter to apply...
|
623 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
624 |
'table2', 'key3' => {out => 'three_times', in => 'three_times'} |
added auto_filter method
|
625 |
); |
renamed auto_filter to apply...
|
626 |
$dbi->insert(table => 'table1', param => {key1 => 5, key2 => 2}, filter => {key2 => undef}); |
627 |
$dbi->insert(table => 'table2', param => {key1 => 5, key3 => 6}, filter => {key3 => undef}); |
|
added auto_filter method
|
628 |
$result = $dbi->select( |
629 |
table => ['table1', 'table2'], |
|
630 |
column => ['key2', 'key3'], |
|
631 |
where => {'table1.key2' => 1, 'table2.key3' => 2}, relation => {'table1.key1' => 'table2.key1'}); |
|
632 | ||
633 |
$result->filter({'key2' => 'twice'}); |
|
634 |
$rows = $result->fetch_hash_all; |
|
cleanup
|
635 |
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join"); |
added auto_filter method
|
636 | |
637 |
$result = $dbi->select( |
|
638 |
table => ['table1', 'table2'], |
|
639 |
column => ['key2', 'key3'], |
|
640 |
where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'}); |
|
641 | ||
642 |
$result->filter({'key2' => 'twice'}); |
|
643 |
$rows = $result->fetch_hash_all; |
|
cleanup
|
644 |
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join : omit"); |
added auto_filter method
|
645 | |
pod fix
|
646 |
test 'each_column'; |
added experimental iterate_a...
|
647 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
648 |
$dbi->execute($CREATE_TABLE->{2}); |
|
649 |
$dbi->execute($CREATE_TABLE->{3}); |
|
650 | ||
651 |
$infos = []; |
|
pod fix
|
652 |
$dbi->each_column(sub { |
added experimental iterate_a...
|
653 |
my ($table, $column, $cinfo) = @_; |
654 |
|
|
655 |
if ($table =~ /^table/) { |
|
656 |
my $info = [$table, $column, $cinfo->{COLUMN_NAME}]; |
|
657 |
push @$infos, $info; |
|
658 |
} |
|
659 |
}); |
|
660 |
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos]; |
|
661 |
is_deeply($infos, |
|
662 |
[ |
|
663 |
['table1', 'key1', 'key1'], |
|
664 |
['table1', 'key2', 'key2'], |
|
665 |
['table2', 'key1', 'key1'], |
|
666 |
['table2', 'key3', 'key3'] |
|
667 |
] |
|
cleanup
|
668 |
|
added experimental iterate_a...
|
669 |
); |
added auto_filter method
|
670 | |
remove DBIx::Custom::Model
|
671 |
test 'table'; |
672 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
673 |
$dbi->execute($CREATE_TABLE->{0}); |
|
674 |
$table = $dbi->table('table1'); |
|
simplified DBIx::Custom::Mod...
|
675 |
$table->insert(param => {key1 => 1, key2 => 2}); |
676 |
$table->insert(param => {key1 => 3, key2 => 4}); |
|
added experimental DBIx::Cus...
|
677 |
$rows = $table->select->fetch_hash_all; |
678 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], |
|
cleanup
|
679 |
"select"); |
simplified DBIx::Custom::Mod...
|
680 |
$rows = $table->select(where => {key2 => 2}, append => 'order by key1', |
681 |
column => ['key1', 'key2'])->fetch_hash_all; |
|
added experimental DBIx::Cus...
|
682 |
is_deeply($rows, [{key1 => 1, key2 => 2}], |
cleanup
|
683 |
"insert insert select"); |
simplified DBIx::Custom::Mod...
|
684 |
$table->update(param => {key1 => 3}, where => {key2 => 2}); |
685 |
$table->update(param => {key1 => 5}, where => {key2 => 4}); |
|
686 |
$rows = $table->select(where => {key2 => 2})->fetch_hash_all; |
|
added experimental DBIx::Cus...
|
687 |
is_deeply($rows, [{key1 => 3, key2 => 2}], |
cleanup
|
688 |
"update"); |
simplified DBIx::Custom::Mod...
|
689 |
$table->delete(where => {key2 => 2}); |
added experimental DBIx::Cus...
|
690 |
$rows = $table->select->fetch_hash_all; |
cleanup
|
691 |
is_deeply($rows, [{key1 => 5, key2 => 4}], "delete"); |
simplified DBIx::Custom::Mod...
|
692 |
$table->update_all(param => {key1 => 3}); |
693 |
$rows = $table->select->fetch_hash_all; |
|
cleanup
|
694 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "update_all"); |
added experimental DBIx::Cus...
|
695 |
$table->delete_all; |
696 |
$rows = $table->select->fetch_hash_all; |
|
cleanup
|
697 |
is_deeply($rows, [], "delete_all"); |
added insert, update, update...
|
698 | |
remove DBIx::Custom::Model
|
699 |
$dbi->dbh->do($CREATE_TABLE->{2}); |
700 |
$dbi->table('table2', ppp => sub { |
|
701 |
my $self = shift; |
|
702 |
|
|
703 |
return $self->name; |
|
704 |
}); |
|
cleanup
|
705 |
is($dbi->table('table2')->ppp, 'table2', "helper"); |
remove DBIx::Custom::Model
|
706 | |
many changed
|
707 |
$dbi->table('table2', {qqq => sub { |
708 |
my $self = shift; |
|
709 |
|
|
710 |
return $self->name; |
|
711 |
}}); |
|
cleanup
|
712 |
is($dbi->table('table2')->qqq, 'table2', "helper"); |
many changed
|
713 | |
714 | ||
add examples
|
715 |
test 'limit'; |
716 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
717 |
$dbi->execute($CREATE_TABLE->{0}); |
|
718 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
719 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}); |
|
720 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6}); |
|
renamed DBIx::Custom::TagPro...
|
721 |
$dbi->register_tag( |
add examples
|
722 |
limit => sub { |
723 |
my ($count, $offset) = @_; |
|
724 |
|
|
725 |
my $s = ''; |
|
726 |
$s .= "limit $count"; |
|
727 |
$s .= " offset $offset" if defined $offset; |
|
728 |
|
|
729 |
return [$s, []]; |
|
730 |
} |
|
731 |
); |
|
732 |
$rows = $dbi->select( |
|
733 |
table => 'table1', |
|
734 |
where => {key1 => 1}, |
|
735 |
append => "order by key2 {limit 1 0}" |
|
736 |
)->fetch_hash_all; |
|
cleanup
|
737 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
add examples
|
738 |
$rows = $dbi->select( |
739 |
table => 'table1', |
|
740 |
where => {key1 => 1}, |
|
741 |
append => "order by key2 {limit 2 1}" |
|
742 |
)->fetch_hash_all; |
|
cleanup
|
743 |
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]); |
add examples
|
744 |
$rows = $dbi->select( |
745 |
table => 'table1', |
|
746 |
where => {key1 => 1}, |
|
747 |
append => "order by key2 {limit 1}" |
|
748 |
)->fetch_hash_all; |
|
cleanup
|
749 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
added insert, update, update...
|
750 | |
remove DBIx::Custom::Model
|
751 |
test 'connect super'; |
752 |
{ |
|
753 |
package MyDBI; |
|
754 |
|
|
755 |
use base 'DBIx::Custom'; |
|
756 |
sub connect { |
|
757 |
my $self = shift->SUPER::connect(@_); |
|
758 |
|
|
759 |
return $self; |
|
760 |
} |
|
761 |
|
|
762 |
sub new { |
|
763 |
my $self = shift->SUPER::connect(@_); |
|
764 |
|
|
765 |
return $self; |
|
766 |
} |
|
767 |
} |
|
768 | ||
769 |
$dbi = MyDBI->connect($NEW_ARGS->{0}); |
|
770 |
$dbi->execute($CREATE_TABLE->{0}); |
|
771 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
cleanup
|
772 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1); |
remove DBIx::Custom::Model
|
773 | |
774 |
$dbi = MyDBI->new($NEW_ARGS->{0}); |
|
775 |
$dbi->execute($CREATE_TABLE->{0}); |
|
776 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
cleanup
|
777 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1); |
added experimental DBIx::Cus...
|
778 | |
779 | ||
780 |
test 'end_filter'; |
|
781 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
782 |
$dbi->execute($CREATE_TABLE->{0}); |
|
783 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
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_first; |
|
788 |
is_deeply($row, [6, 40]); |
|
789 | ||
many changed
|
790 |
$dbi->register_filter(five_times => sub { $_[0] * 5 }); |
added experimental DBIx::Cus...
|
791 |
$result = $dbi->select(table => 'table1'); |
792 |
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 }); |
|
many changed
|
793 |
$result->end_filter({key1 => sub { $_[0] * 3 }, key2 => 'five_times' }); |
added experimental DBIx::Cus...
|
794 |
$row = $result->fetch_hash_first; |
795 |
is_deeply($row, {key1 => 6, key2 => 40}); |
|
fix select method empty wher...
|
796 | |
797 | ||
798 |
test 'empty where select'; |
|
799 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
800 |
$dbi->execute($CREATE_TABLE->{0}); |
|
801 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
802 |
$result = $dbi->select(table => 'table1', where => {}); |
|
803 |
$row = $result->fetch_hash_first; |
|
804 |
is_deeply($row, {key1 => 1, key2 => 2}); |
|
805 | ||
806 |
$result = $dbi->select(table => 'table1', where => [' ', {}]); |
|
807 |
$row = $result->fetch_hash_first; |
|
808 |
is_deeply($row, {key1 => 1, key2 => 2}); |
|
added experimental sugar met...
|
809 | |
810 | ||
811 |
test 'select query option'; |
|
812 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
813 |
$dbi->execute($CREATE_TABLE->{0}); |
|
814 |
$query = $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, query => 1); |
|
815 |
is(ref $query, 'DBIx::Custom::Query'); |
|
816 |
$query = $dbi->update(table => 'table1', where => {key1 => 1}, param => {key2 => 2}, query => 1); |
|
817 |
is(ref $query, 'DBIx::Custom::Query'); |
|
818 |
$query = $dbi->delete(table => 'table1', where => {key1 => 1}, query => 1); |
|
819 |
is(ref $query, 'DBIx::Custom::Query'); |
|
820 |
$query = $dbi->select(table => 'table1', where => {key1 => 1, key2 => 2}, query => 1); |
|
821 |
is(ref $query, 'DBIx::Custom::Query'); |
|
822 | ||
added experimental DBIx::Cus...
|
823 |
test 'DBIx::Custom::Where'; |
experimental extended select...
|
824 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
825 |
$dbi->execute($CREATE_TABLE->{0}); |
|
826 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
827 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
renamed DBIx::Custom::TagPro...
|
828 |
$where = $dbi->where->clause(['and', '{= key1}', '{= key2}']); |
829 |
is("$where", "where ( {= key1} and {= key2} )", 'no param'); |
|
830 | ||
added experimental DBIx::Cus...
|
831 |
$where = $dbi->where |
added test
|
832 |
->clause(['and', '{= key1}', '{= key2}']) |
added experimental DBIx::Cus...
|
833 |
->param({key1 => 1}); |
added test
|
834 | |
experimental extended select...
|
835 |
$result = $dbi->select( |
836 |
table => 'table1', |
|
added experimental DBIx::Cus...
|
837 |
where => $where |
experimental extended select...
|
838 |
); |
839 |
$row = $result->fetch_hash_all; |
|
840 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
841 | ||
added experimental DBIx::Cus...
|
842 |
$where = $dbi->where |
added test
|
843 |
->clause(['and', '{= key1}', '{= key2}']) |
added experimental DBIx::Cus...
|
844 |
->param({key1 => 1, key2 => 2}); |
experimental extended select...
|
845 |
$result = $dbi->select( |
846 |
table => 'table1', |
|
added experimental DBIx::Cus...
|
847 |
where => $where |
experimental extended select...
|
848 |
); |
849 |
$row = $result->fetch_hash_all; |
|
850 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
851 | ||
added experimental DBIx::Cus...
|
852 |
$where = $dbi->where |
added test
|
853 |
->clause(['and', '{= key1}', '{= key2}']) |
added experimental DBIx::Cus...
|
854 |
->param({}); |
experimental extended select...
|
855 |
$result = $dbi->select( |
856 |
table => 'table1', |
|
added experimental DBIx::Cus...
|
857 |
where => $where, |
experimental extended select...
|
858 |
); |
859 |
$row = $result->fetch_hash_all; |
|
860 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
861 | ||
added experimental DBIx::Cus...
|
862 |
$where = $dbi->where |
added test
|
863 |
->clause(['and', ['or', '{> key1}', '{< key1}'], '{= key2}']) |
added experimental DBIx::Cus...
|
864 |
->param({key1 => [0, 3], key2 => 2}); |
experimental extended select...
|
865 |
$result = $dbi->select( |
866 |
table => 'table1', |
|
added experimental DBIx::Cus...
|
867 |
where => $where, |
experimental extended select...
|
868 |
); |
869 |
$row = $result->fetch_hash_all; |
|
870 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
871 | ||
added experimental DBIx::Cus...
|
872 |
$where = $dbi->where; |
873 |
$result = $dbi->select( |
|
874 |
table => 'table1', |
|
875 |
where => $where |
|
876 |
); |
|
877 |
$row = $result->fetch_hash_all; |
|
878 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
879 | ||
experimental extended select...
|
880 |
eval { |
added experimental DBIx::Cus...
|
881 |
$where = $dbi->where |
added test
|
882 |
->clause(['uuu']); |
experimental extended select...
|
883 |
$result = $dbi->select( |
884 |
table => 'table1', |
|
added experimental DBIx::Cus...
|
885 |
where => $where |
experimental extended select...
|
886 |
); |
887 |
}; |
|
888 |
ok($@); |
|
889 | ||
added experimental DBIx::Cus...
|
890 |
$where = $dbi->where; |
891 |
is("$where", ''); |
|
892 | ||
added test
|
893 |
$where = $dbi->where |
894 |
->clause(['or', ('{= key1}') x 2]) |
|
895 |
->param({key1 => [1, 3]}); |
|
896 |
$result = $dbi->select( |
|
897 |
table => 'table1', |
|
898 |
where => $where, |
|
899 |
); |
|
900 |
$row = $result->fetch_hash_all; |
|
901 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
902 | ||
903 |
$where = $dbi->where |
|
904 |
->clause(['or', ('{= key1}') x 2]) |
|
905 |
->param({key1 => [1]}); |
|
906 |
$result = $dbi->select( |
|
907 |
table => 'table1', |
|
908 |
where => $where, |
|
909 |
); |
|
910 |
$row = $result->fetch_hash_all; |
|
911 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
912 | ||
913 |
$where = $dbi->where |
|
914 |
->clause(['or', ('{= key1}') x 2]) |
|
915 |
->param({key1 => 1}); |
|
916 |
$result = $dbi->select( |
|
917 |
table => 'table1', |
|
918 |
where => $where, |
|
919 |
); |
|
920 |
$row = $result->fetch_hash_all; |
|
921 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
added experimental DBIx::Cus...
|
922 | |
many changed
|
923 |
$where = $dbi->where |
924 |
->clause('{= key1}') |
|
925 |
->param({key1 => 1}); |
|
926 |
$result = $dbi->select( |
|
927 |
table => 'table1', |
|
928 |
where => $where, |
|
929 |
); |
|
930 |
$row = $result->fetch_hash_all; |
|
931 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
932 | ||
933 |
$where = $dbi->where |
|
934 |
->clause('{= key1} {= key2}') |
|
935 |
->param({key1 => 1}); |
|
936 |
eval{$where->to_string}; |
|
937 |
like($@, qr/one column/); |
|
938 | ||
renamed dbi_options to dbi_o...
|
939 |
test 'dbi_option default'; |
added experimental DBIx::Cus...
|
940 |
$dbi = DBIx::Custom->new; |
renamed dbi_options to dbi_o...
|
941 |
is_deeply($dbi->dbi_option, {}); |
added experimental DBIx::Cus...
|
942 | |
added register_tag_processor
|
943 |
test 'register_tag_processor'; |
944 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
945 |
$dbi->register_tag_processor( |
|
946 |
a => sub { 1 } |
|
947 |
); |
|
948 |
is($dbi->query_builder->tag_processors->{a}->(), 1); |
|
added experimental DBIx::Cus...
|
949 | |
renamed DBIx::Custom::TagPro...
|
950 |
test 'register_tag'; |
951 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
952 |
$dbi->register_tag( |
|
953 |
b => sub { 2 } |
|
954 |
); |
|
955 |
is($dbi->query_builder->tags->{b}->(), 2); |
|
956 | ||
added table not specified ex...
|
957 |
test 'table not specify exception'; |
958 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
959 |
eval {$dbi->insert}; |
|
960 |
like($@, qr/table/); |
|
961 |
eval {$dbi->update}; |
|
962 |
like($@, qr/table/); |
|
963 |
eval {$dbi->delete}; |
|
964 |
like($@, qr/table/); |
|
965 |
eval {$dbi->select}; |
|
966 |
like($@, qr/table/); |
|
many changed
|
967 | |
968 | ||
969 |
test 'more tests'; |
|
970 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
971 |
eval{$dbi->apply_filter('table', 'column', [])}; |
|
972 |
like($@, qr/apply_filter/); |
|
973 | ||
974 |
eval{$dbi->apply_filter('table', 'column', {outer => 2})}; |
|
975 |
like($@, qr/apply_filter/); |
|
976 | ||
977 |
$dbi->apply_filter( |
|
978 | ||
979 |
); |
|
980 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
981 |
$dbi->execute($CREATE_TABLE->{0}); |
|
982 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
983 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
984 |
$dbi->apply_filter('table1', 'key2', |
|
985 |
{in => sub { $_[0] * 3 }, out => sub { $_[0] * 2 }}); |
|
986 |
$rows = $dbi->select(table => 'table1', where => {key2 => 1})->fetch_hash_all; |
|
987 |
is_deeply($rows, [{key1 => 1, key2 => 6}]); |
|
988 | ||
989 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
990 |
$dbi->execute($CREATE_TABLE->{0}); |
|
991 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
992 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
993 |
$dbi->apply_filter('table1', 'key2', {}); |
|
994 |
$rows = $dbi->select(table => 'table1', where => {key2 => 2})->fetch_hash_all; |
|
995 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
996 | ||
997 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
998 |
eval {$dbi->apply_filter('table1', 'key2', {out => 'no'})}; |
|
999 |
like($@, qr/not registered/); |
|
1000 |
eval {$dbi->apply_filter('table1', 'key2', {in => 'no'})}; |
|
1001 |
like($@, qr/not registered/); |
|
1002 |
$dbi->helper({one => sub { 1 }}); |
|
1003 |
is($dbi->one, 1); |
|
1004 | ||
1005 |
eval{DBIx::Custom->connect()}; |
|
1006 |
like($@, qr/connect/); |
|
1007 | ||
1008 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1009 |
$dbi->execute($CREATE_TABLE->{0}); |
|
1010 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
1011 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, |
|
1012 |
filter => {key1 => 'twice'}); |
|
1013 |
$row = $dbi->select(table => 'table1')->fetch_hash_first; |
|
1014 |
is_deeply($row, {key1 => 2, key2 => 2}); |
|
1015 |
eval {$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, |
|
1016 |
filter => {key1 => 'no'}) }; |
|
1017 |
like($@, qr//); |
|
1018 |
$dbi->table_class('!!!!'); |
|
1019 |
eval {$dbi->table}; |
|
1020 |
like($@, qr/Invalid table/); |
|
1021 | ||
1022 |
$dbi->table_class('NOTEXIST'); |
|
1023 |
eval {$dbi->table}; |
|
1024 |
ok($@); |
|
1025 |
$dbi->register_filter(one => sub { }); |
|
1026 |
$dbi->default_fetch_filter('one'); |
|
1027 |
ok($dbi->default_fetch_filter); |
|
1028 |
$dbi->default_bind_filter('one'); |
|
1029 |
ok($dbi->default_bind_filter); |
|
1030 |
eval{$dbi->default_fetch_filter('no')}; |
|
1031 |
like($@, qr/not registered/); |
|
1032 |
eval{$dbi->default_bind_filter('no')}; |
|
1033 |
like($@, qr/not registered/); |
|
1034 |
$dbi->default_bind_filter(undef); |
|
1035 |
ok(!defined $dbi->default_bind_filter); |
|
1036 |
$dbi->default_fetch_filter(undef); |
|
1037 |
ok(!defined $dbi->default_fetch_filter); |
|
1038 |
eval {$dbi->execute('select * from table1 {= author') }; |
|
1039 |
like($@, qr/Tag not finished/); |
|
1040 | ||
1041 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1042 |
$dbi->execute($CREATE_TABLE->{0}); |
|
1043 |
$dbi->register_filter(one => sub { 1 }); |
|
1044 |
$result = $dbi->select(table => 'table1'); |
|
1045 |
eval {$result->filter(key1 => 'no')}; |
|
1046 |
like($@, qr/not registered/); |
|
1047 |
eval {$result->end_filter(key1 => 'no')}; |
|
1048 |
like($@, qr/not registered/); |
|
1049 |
$result->default_filter(undef); |
|
1050 |
ok(!defined $result->default_filter); |
|
1051 |
$result->default_filter('one'); |
|
1052 |
is($result->default_filter->(), 1); |
|
1053 | ||
1054 |
$dbi->table('book'); |
|
1055 |
eval{$dbi->table('book')->no_exists}; |
|
renamed dbi_options to dbi_o...
|
1056 |
like($@, qr/locate/); |
1057 | ||
1058 |
test 'dbi_option'; |
|
1059 |
$dbi = DBIx::Custom->connect(data_source => 'dbi:SQLite:dbname=:memory:', |
|
1060 |
dbi_option => {PrintError => 1}); |
|
1061 |
ok($dbi->dbh->{PrintError}); |
|
1062 |
$dbi = DBIx::Custom->connect(data_source => 'dbi:SQLite:dbname=:memory:', |
|
1063 |
dbi_options => {PrintError => 1}); |
|
1064 |
ok($dbi->dbh->{PrintError}); |
|
1065 |