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/; |
|
add experimental DBIx::Custo...
|
7 |
use Data::Dumper; |
removed register_format()
|
8 | |
9 |
BEGIN { |
|
10 |
eval { require DBD::SQLite; 1 } |
|
11 |
or plan skip_all => 'DBD::SQLite required'; |
|
12 |
eval { DBD::SQLite->VERSION >= 1.25 } |
|
13 |
or plan skip_all => 'DBD::SQLite >= 1.25 required'; |
|
14 | ||
15 |
plan 'no_plan'; |
|
16 |
use_ok('DBIx::Custom'); |
|
17 |
} |
|
18 | ||
removed experimental base_ta...
|
19 |
use FindBin; |
20 |
use lib "$FindBin::Bin/dbix-custom-core-sqlite"; |
|
21 | ||
removed register_format()
|
22 |
# Function for test name |
table object call dbi object...
|
23 |
sub test { print "# $_[0]\n" } |
removed register_format()
|
24 | |
25 |
# Constant varialbes for test |
|
26 |
my $CREATE_TABLE = { |
|
27 |
0 => 'create table table1 (key1 char(255), key2 char(255));', |
|
28 |
1 => 'create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));', |
|
added experimental iterate_a...
|
29 |
2 => 'create table table2 (key1 char(255), key3 char(255));', |
- added experimental DBIx::C...
|
30 |
3 => 'create table table1 (key1 Date, key2 datetime);', |
31 |
4 => 'create table table3 (key3 int, key4 int);' |
|
removed register_format()
|
32 |
}; |
33 | ||
add tests
|
34 |
my $SELECT_SOURCES = { |
removed register_format()
|
35 |
0 => 'select * from table1;' |
36 |
}; |
|
37 | ||
38 |
my $DROP_TABLE = { |
|
39 |
0 => 'drop table table1' |
|
40 |
}; |
|
41 | ||
42 |
my $NEW_ARGS = { |
|
43 |
0 => {data_source => 'dbi:SQLite:dbname=:memory:'} |
|
44 |
}; |
|
45 | ||
46 |
# Variables |
|
47 |
my $dbi; |
|
48 |
my $sth; |
|
renamed build_query to creat...
|
49 |
my $source; |
50 |
my @sources; |
|
add tests
|
51 |
my $select_SOURCE; |
52 |
my $insert_SOURCE; |
|
53 |
my $update_SOURCE; |
|
added experimental update_pa...
|
54 |
my $param; |
removed register_format()
|
55 |
my $params; |
56 |
my $sql; |
|
57 |
my $result; |
|
58 |
my $row; |
|
59 |
my @rows; |
|
60 |
my $rows; |
|
61 |
my $query; |
|
62 |
my @queries; |
|
63 |
my $select_query; |
|
64 |
my $insert_query; |
|
65 |
my $update_query; |
|
66 |
my $ret_val; |
|
added experimental iterate_a...
|
67 |
my $infos; |
add feture. all model class ...
|
68 |
my $model; |
added experimental DBIx::Cus...
|
69 |
my $where; |
added experimental update_pa...
|
70 |
my $update_param; |
71 |
my $insert_param; |
|
removed register_format()
|
72 | |
73 |
# Prepare table |
|
74 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
75 |
$dbi->execute($CREATE_TABLE->{0}); |
|
76 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
77 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
78 | ||
79 |
test 'DBIx::Custom::Result test'; |
|
renamed build_query to creat...
|
80 |
$source = "select key1, key2 from table1"; |
81 |
$query = $dbi->create_query($source); |
|
removed register_format()
|
82 |
$result = $dbi->execute($query); |
83 | ||
84 |
@rows = (); |
|
85 |
while (my $row = $result->fetch) { |
|
86 |
push @rows, [@$row]; |
|
87 |
} |
|
cleanup
|
88 |
is_deeply(\@rows, [[1, 2], [3, 4]], "fetch"); |
removed register_format()
|
89 | |
90 |
$result = $dbi->execute($query); |
|
91 |
@rows = (); |
|
92 |
while (my $row = $result->fetch_hash) { |
|
93 |
push @rows, {%$row}; |
|
94 |
} |
|
cleanup
|
95 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "fetch_hash"); |
removed register_format()
|
96 | |
97 |
$result = $dbi->execute($query); |
|
98 |
$rows = $result->fetch_all; |
|
cleanup
|
99 |
is_deeply($rows, [[1, 2], [3, 4]], "fetch_all"); |
removed register_format()
|
100 | |
101 |
$result = $dbi->execute($query); |
|
removed reconnect method
|
102 |
$rows = $result->fetch_hash_all; |
cleanup
|
103 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "fetch_hash_all"); |
removed register_format()
|
104 | |
105 |
test 'Insert query return value'; |
|
106 |
$dbi->execute($DROP_TABLE->{0}); |
|
107 |
$dbi->execute($CREATE_TABLE->{0}); |
|
renamed update tag to update...
|
108 |
$source = "insert into table1 {insert_param key1 key2}"; |
renamed build_query to creat...
|
109 |
$query = $dbi->create_query($source); |
removed register_format()
|
110 |
$ret_val = $dbi->execute($query, param => {key1 => 1, key2 => 2}); |
cleanup
|
111 |
ok($ret_val); |
removed register_format()
|
112 | |
113 | ||
114 |
test 'Direct query'; |
|
115 |
$dbi->execute($DROP_TABLE->{0}); |
|
116 |
$dbi->execute($CREATE_TABLE->{0}); |
|
add tests
|
117 |
$insert_SOURCE = "insert into table1 {insert_param key1 key2}"; |
118 |
$dbi->execute($insert_SOURCE, param => {key1 => 1, key2 => 2}); |
|
119 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
removed register_format()
|
120 |
$rows = $result->fetch_hash_all; |
cleanup
|
121 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
removed register_format()
|
122 | |
123 |
test 'Filter basic'; |
|
124 |
$dbi->execute($DROP_TABLE->{0}); |
|
125 |
$dbi->execute($CREATE_TABLE->{0}); |
|
126 |
$dbi->register_filter(twice => sub { $_[0] * 2}, |
|
127 |
three_times => sub { $_[0] * 3}); |
|
128 | ||
add tests
|
129 |
$insert_SOURCE = "insert into table1 {insert_param key1 key2};"; |
130 |
$insert_query = $dbi->create_query($insert_SOURCE); |
|
removed register_format()
|
131 |
$insert_query->filter({key1 => 'twice'}); |
132 |
$dbi->execute($insert_query, param => {key1 => 1, key2 => 2}); |
|
add tests
|
133 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
134 |
$rows = $result->filter({key2 => 'three_times'})->fetch_hash_all; |
cleanup
|
135 |
is_deeply($rows, [{key1 => 2, key2 => 6}], "filter fetch_filter"); |
removed register_format()
|
136 |
$dbi->execute($DROP_TABLE->{0}); |
137 | ||
138 |
test 'Filter in'; |
|
139 |
$dbi->execute($CREATE_TABLE->{0}); |
|
add tests
|
140 |
$insert_SOURCE = "insert into table1 {insert_param key1 key2};"; |
141 |
$insert_query = $dbi->create_query($insert_SOURCE); |
|
removed register_format()
|
142 |
$dbi->execute($insert_query, param => {key1 => 2, key2 => 4}); |
add tests
|
143 |
$select_SOURCE = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}"; |
144 |
$select_query = $dbi->create_query($select_SOURCE); |
|
removed register_format()
|
145 |
$select_query->filter({'table1.key1' => 'twice'}); |
146 |
$result = $dbi->execute($select_query, param => {'table1.key1' => [1,5], 'table1.key2' => [2,4]}); |
|
147 |
$rows = $result->fetch_hash_all; |
|
cleanup
|
148 |
is_deeply($rows, [{key1 => 2, key2 => 4}], "filter"); |
removed register_format()
|
149 | |
150 |
test 'DBIx::Custom::SQLTemplate basic tag'; |
|
151 |
$dbi->execute($DROP_TABLE->{0}); |
|
152 |
$dbi->execute($CREATE_TABLE->{1}); |
|
153 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
154 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
155 | ||
renamed build_query to creat...
|
156 |
$source = "select * from table1 where {= key1} and {<> key2} and {< key3} and {> key4} and {>= key5};"; |
157 |
$query = $dbi->create_query($source); |
|
removed register_format()
|
158 |
$result = $dbi->execute($query, param => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5}); |
159 |
$rows = $result->fetch_hash_all; |
|
cleanup
|
160 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1"); |
removed register_format()
|
161 | |
renamed build_query to creat...
|
162 |
$source = "select * from table1 where {<= key1} and {like key2};"; |
163 |
$query = $dbi->create_query($source); |
|
removed register_format()
|
164 |
$result = $dbi->execute($query, param => {key1 => 1, key2 => '%2%'}); |
165 |
$rows = $result->fetch_hash_all; |
|
cleanup
|
166 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag2"); |
removed register_format()
|
167 | |
168 |
test 'DIB::Custom::SQLTemplate in tag'; |
|
169 |
$dbi->execute($DROP_TABLE->{0}); |
|
170 |
$dbi->execute($CREATE_TABLE->{1}); |
|
171 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
172 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
173 | ||
renamed build_query to creat...
|
174 |
$source = "select * from table1 where {in key1 2};"; |
175 |
$query = $dbi->create_query($source); |
|
removed register_format()
|
176 |
$result = $dbi->execute($query, param => {key1 => [9, 1]}); |
177 |
$rows = $result->fetch_hash_all; |
|
cleanup
|
178 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic"); |
removed register_format()
|
179 | |
180 |
test 'DBIx::Custom::SQLTemplate insert tag'; |
|
181 |
$dbi->execute("delete from table1"); |
|
add tests
|
182 |
$insert_SOURCE = 'insert into table1 {insert_param key1 key2 key3 key4 key5}'; |
183 |
$dbi->execute($insert_SOURCE, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
removed register_format()
|
184 | |
add tests
|
185 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
186 |
$rows = $result->fetch_hash_all; |
cleanup
|
187 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic"); |
removed register_format()
|
188 | |
189 |
test 'DBIx::Custom::SQLTemplate update tag'; |
|
190 |
$dbi->execute("delete from table1"); |
|
add tests
|
191 |
$insert_SOURCE = "insert into table1 {insert_param key1 key2 key3 key4 key5}"; |
192 |
$dbi->execute($insert_SOURCE, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
193 |
$dbi->execute($insert_SOURCE, param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
removed register_format()
|
194 | |
add tests
|
195 |
$update_SOURCE = 'update table1 {update_param key1 key2 key3 key4} where {= key5}'; |
196 |
$dbi->execute($update_SOURCE, param => {key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}); |
|
removed register_format()
|
197 | |
add tests
|
198 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
199 |
$rows = $result->fetch_hash_all; |
200 |
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}, |
|
cleanup
|
201 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "basic"); |
removed register_format()
|
202 | |
203 |
test 'Error case'; |
|
204 |
eval {DBIx::Custom->connect(data_source => 'dbi:SQLit')}; |
|
cleanup
|
205 |
ok($@, "connect error"); |
removed register_format()
|
206 | |
207 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
renamed build_query to creat...
|
208 |
eval{$dbi->create_query("{p }")}; |
cleanup
|
209 |
ok($@, "create_query invalid SQL template"); |
removed register_format()
|
210 | |
211 |
test 'insert'; |
|
212 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
213 |
$dbi->execute($CREATE_TABLE->{0}); |
|
214 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
215 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
add tests
|
216 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
217 |
$rows = $result->fetch_hash_all; |
cleanup
|
218 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic"); |
removed register_format()
|
219 | |
220 |
$dbi->execute('delete from table1'); |
|
221 |
$dbi->register_filter( |
|
222 |
twice => sub { $_[0] * 2 }, |
|
223 |
three_times => sub { $_[0] * 3 } |
|
224 |
); |
|
renamed default_query_filter...
|
225 |
$dbi->default_bind_filter('twice'); |
removed register_format()
|
226 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => 'three_times'}); |
add tests
|
227 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
228 |
$rows = $result->fetch_hash_all; |
cleanup
|
229 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter"); |
renamed default_query_filter...
|
230 |
$dbi->default_bind_filter(undef); |
removed register_format()
|
231 | |
232 |
$dbi->execute($DROP_TABLE->{0}); |
|
233 |
$dbi->execute($CREATE_TABLE->{0}); |
|
234 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, append => ' '); |
|
235 |
$rows = $dbi->select(table => 'table1')->fetch_hash_all; |
|
236 |
is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append'); |
|
237 | ||
changed argument of tag proc...
|
238 |
eval{$dbi->insert(table => 'table1', noexist => 1)}; |
cleanup
|
239 |
like($@, qr/noexist/, "invalid"); |
changed argument of tag proc...
|
240 | |
select() where can't receive...
|
241 |
eval{$dbi->insert(table => 'table', param => {';' => 1})}; |
242 |
like($@, qr/safety/); |
|
changed argument of tag proc...
|
243 | |
removed register_format()
|
244 |
test 'update'; |
245 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
246 |
$dbi->execute($CREATE_TABLE->{1}); |
|
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 => 11}, where => {key1 => 1}); |
|
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 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
253 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
cleanup
|
254 |
"basic"); |
removed register_format()
|
255 |
|
256 |
$dbi->execute("delete from table1"); |
|
257 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
258 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
259 |
$dbi->update(table => 'table1', param => {key2 => 12}, where => {key2 => 2, key3 => 3}); |
|
add tests
|
260 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
261 |
$rows = $result->fetch_hash_all; |
262 |
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5}, |
|
263 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
cleanup
|
264 |
"update key same as search key"); |
removed register_format()
|
265 | |
add tests
|
266 |
$dbi->update(table => 'table1', param => {key2 => [12]}, where => {key2 => 2, key3 => 3}); |
267 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
268 |
$rows = $result->fetch_hash_all; |
|
269 |
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5}, |
|
270 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
cleanup
|
271 |
"update key same as search key : param is array ref"); |
add tests
|
272 | |
removed register_format()
|
273 |
$dbi->execute("delete from table1"); |
274 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
275 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
276 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
277 |
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}, |
|
many changed
|
278 |
filter => {key2 => sub { $_[0] * 2 }}); |
add tests
|
279 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
280 |
$rows = $result->fetch_hash_all; |
281 |
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5}, |
|
282 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
cleanup
|
283 |
"filter"); |
removed register_format()
|
284 | |
285 |
$result = $dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}, append => ' '); |
|
286 | ||
changed argument of tag proc...
|
287 |
eval{$dbi->update(table => 'table1', noexist => 1)}; |
cleanup
|
288 |
like($@, qr/noexist/, "invalid"); |
changed argument of tag proc...
|
289 | |
290 |
eval{$dbi->update(table => 'table1')}; |
|
cleanup
|
291 |
like($@, qr/where/, "not contain where"); |
changed argument of tag proc...
|
292 | |
improved delete() and update...
|
293 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
294 |
$dbi->execute($CREATE_TABLE->{0}); |
|
295 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
296 |
$where = $dbi->where; |
|
297 |
$where->clause(['and', '{= key1}', '{= key2}']); |
|
298 |
$where->param({key1 => 1, key2 => 2}); |
|
299 |
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where); |
|
300 |
$result = $dbi->select(table => 'table1'); |
|
301 |
is_deeply($result->fetch_hash_all, [{key1 => 3, key2 => 2}], 'delete() where'); |
|
302 | ||
303 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
304 |
$dbi->execute($CREATE_TABLE->{0}); |
|
305 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
306 |
$where = $dbi->where; |
|
307 |
$where->clause(['and', '{= key2}']); |
|
308 |
$where->param({key2 => 2}); |
|
309 |
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where); |
|
310 |
$result = $dbi->select(table => 'table1'); |
|
311 |
is_deeply($result->fetch_hash_all, [{key1 => 3, key2 => 2}], 'delete() where'); |
|
changed argument of tag proc...
|
312 | |
select() where can't receive...
|
313 |
eval{$dbi->update(table => 'table1', param => {';' => 1})}; |
314 |
like($@, qr/safety/); |
|
315 | ||
316 |
eval{$dbi->update(table => 'table1', param => {'key1' => 1}, where => {';' => 1})}; |
|
317 |
like($@, qr/safety/); |
|
318 | ||
removed register_format()
|
319 |
test 'update_all'; |
320 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
321 |
$dbi->execute($CREATE_TABLE->{1}); |
|
322 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
323 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
324 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
325 |
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'}); |
|
add tests
|
326 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
327 |
$rows = $result->fetch_hash_all; |
328 |
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5}, |
|
329 |
{key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}], |
|
cleanup
|
330 |
"filter"); |
removed register_format()
|
331 | |
332 | ||
333 |
test 'delete'; |
|
334 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
335 |
$dbi->execute($CREATE_TABLE->{0}); |
|
336 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
337 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
338 |
$dbi->delete(table => 'table1', where => {key1 => 1}); |
|
add tests
|
339 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
340 |
$rows = $result->fetch_hash_all; |
cleanup
|
341 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "basic"); |
removed register_format()
|
342 | |
343 |
$dbi->execute("delete from table1;"); |
|
344 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
345 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
346 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
347 |
$dbi->delete(table => 'table1', where => {key2 => 1}, filter => {key2 => 'twice'}); |
|
add tests
|
348 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
349 |
$rows = $result->fetch_hash_all; |
cleanup
|
350 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter"); |
removed register_format()
|
351 | |
352 |
$dbi->delete(table => 'table1', where => {key1 => 1}, append => ' '); |
|
353 | ||
354 |
$dbi->delete_all(table => 'table1'); |
|
355 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
356 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
357 |
$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2}); |
|
358 |
$rows = $dbi->select(table => 'table1')->fetch_hash_all; |
|
cleanup
|
359 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "delete multi key"); |
removed register_format()
|
360 | |
changed argument of tag proc...
|
361 |
eval{$dbi->delete(table => 'table1', noexist => 1)}; |
cleanup
|
362 |
like($@, qr/noexist/, "invalid"); |
changed argument of tag proc...
|
363 | |
improved delete() and update...
|
364 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
365 |
$dbi->execute($CREATE_TABLE->{0}); |
|
366 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
367 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
368 |
$where = $dbi->where; |
|
369 |
$where->clause(['and', '{= key1}', '{= key2}']); |
|
370 |
$where->param({ke1 => 1, key2 => 2}); |
|
371 |
$dbi->delete(table => 'table1', where => $where); |
|
372 |
$result = $dbi->select(table => 'table1'); |
|
373 |
is_deeply($result->fetch_hash_all, [{key1 => 3, key2 => 4}], 'delete() where'); |
|
changed argument of tag proc...
|
374 | |
removed register_format()
|
375 |
test 'delete error'; |
376 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
377 |
$dbi->execute($CREATE_TABLE->{0}); |
|
378 |
eval{$dbi->delete(table => 'table1')}; |
|
make delete() using where ob...
|
379 |
like($@, qr/"where" must be specified/, |
cleanup
|
380 |
"where key-value pairs not specified"); |
removed register_format()
|
381 | |
select() where can't receive...
|
382 |
eval{$dbi->delete(table => 'table1', where => {';' => 1})}; |
383 |
like($@, qr/safety/); |
|
384 | ||
removed register_format()
|
385 |
test 'delete_all'; |
386 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
387 |
$dbi->execute($CREATE_TABLE->{0}); |
|
388 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
389 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
390 |
$dbi->delete_all(table => 'table1'); |
|
add tests
|
391 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
392 |
$rows = $result->fetch_hash_all; |
cleanup
|
393 |
is_deeply($rows, [], "basic"); |
removed register_format()
|
394 | |
395 | ||
396 |
test 'select'; |
|
397 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
398 |
$dbi->execute($CREATE_TABLE->{0}); |
|
399 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
400 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
401 |
$rows = $dbi->select(table => 'table1')->fetch_hash_all; |
|
402 |
is_deeply($rows, [{key1 => 1, key2 => 2}, |
|
cleanup
|
403 |
{key1 => 3, key2 => 4}], "table"); |
removed register_format()
|
404 | |
update document
|
405 |
$rows = $dbi->select(table => 'table1', column => ['key1'])->fetch_hash_all; |
cleanup
|
406 |
is_deeply($rows, [{key1 => 1}, {key1 => 3}], "table and columns and where key"); |
removed register_format()
|
407 | |
408 |
$rows = $dbi->select(table => 'table1', where => {key1 => 1})->fetch_hash_all; |
|
cleanup
|
409 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "table and columns and where key"); |
removed register_format()
|
410 | |
update document
|
411 |
$rows = $dbi->select(table => 'table1', column => ['key1'], where => {key1 => 3})->fetch_hash_all; |
cleanup
|
412 |
is_deeply($rows, [{key1 => 3}], "table and columns and where key"); |
removed register_format()
|
413 | |
414 |
$rows = $dbi->select(table => 'table1', append => "order by key1 desc limit 1")->fetch_hash_all; |
|
cleanup
|
415 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "append statement"); |
removed register_format()
|
416 | |
417 |
$dbi->register_filter(decrement => sub { $_[0] - 1 }); |
|
update document
|
418 |
$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'}) |
removed register_format()
|
419 |
->fetch_hash_all; |
cleanup
|
420 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "filter"); |
removed register_format()
|
421 | |
422 |
$dbi->execute($CREATE_TABLE->{2}); |
|
423 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
424 |
$rows = $dbi->select( |
|
425 |
table => [qw/table1 table2/], |
|
select method column option ...
|
426 |
column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3', |
removed register_format()
|
427 |
where => {'table1.key2' => 2}, |
added commit method
|
428 |
relation => {'table1.key1' => 'table2.key1'} |
removed register_format()
|
429 |
)->fetch_hash_all; |
cleanup
|
430 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : exists where"); |
added commit method
|
431 | |
432 |
$rows = $dbi->select( |
|
433 |
table => [qw/table1 table2/], |
|
434 |
column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'], |
|
435 |
relation => {'table1.key1' => 'table2.key1'} |
|
436 |
)->fetch_hash_all; |
|
cleanup
|
437 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : no exists where"); |
removed register_format()
|
438 | |
changed argument of tag proc...
|
439 |
eval{$dbi->select(table => 'table1', noexist => 1)}; |
cleanup
|
440 |
like($@, qr/noexist/, "invalid"); |
changed argument of tag proc...
|
441 | |
442 | ||
removed register_format()
|
443 |
test 'fetch filter'; |
444 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
445 |
$dbi->register_filter( |
|
446 |
twice => sub { $_[0] * 2 }, |
|
447 |
three_times => sub { $_[0] * 3 } |
|
448 |
); |
|
449 |
$dbi->default_fetch_filter('twice'); |
|
450 |
$dbi->execute($CREATE_TABLE->{0}); |
|
451 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
452 |
$result = $dbi->select(table => 'table1'); |
|
453 |
$result->filter({key1 => 'three_times'}); |
|
removed reconnect method
|
454 |
$row = $result->fetch_hash_first; |
cleanup
|
455 |
is_deeply($row, {key1 => 3, key2 => 4}, "default_fetch_filter and filter"); |
removed register_format()
|
456 | |
457 |
test 'filters'; |
|
458 |
$dbi = DBIx::Custom->new; |
|
459 | ||
update document
|
460 |
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')), |
cleanup
|
461 |
'あ', "decode_utf8"); |
removed register_format()
|
462 | |
463 |
is($dbi->filters->{encode_utf8}->('あ'), |
|
cleanup
|
464 |
encode_utf8('あ'), "encode_utf8"); |
removed register_format()
|
465 | |
added commit method
|
466 |
test 'transaction'; |
467 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
468 |
$dbi->execute($CREATE_TABLE->{0}); |
|
removed DBIx::Custom commit ...
|
469 |
$dbi->dbh->begin_work; |
added commit method
|
470 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
471 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
removed DBIx::Custom commit ...
|
472 |
$dbi->dbh->commit; |
added commit method
|
473 |
$result = $dbi->select(table => 'table1'); |
474 |
is_deeply(scalar $result->fetch_hash_all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}], |
|
cleanup
|
475 |
"commit"); |
added commit method
|
476 | |
477 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
478 |
$dbi->execute($CREATE_TABLE->{0}); |
|
removed DBIx::Custom commit ...
|
479 |
$dbi->dbh->begin_work(0); |
added commit method
|
480 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
removed DBIx::Custom commit ...
|
481 |
$dbi->dbh->rollback; |
added commit method
|
482 | |
483 |
$result = $dbi->select(table => 'table1'); |
|
cleanup
|
484 |
ok(! $result->fetch_first, "rollback"); |
add cache attribute
|
485 | |
486 |
test 'cache'; |
|
487 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
488 |
$dbi->execute($CREATE_TABLE->{0}); |
|
renamed build_query to creat...
|
489 |
$source = 'select * from table1 where {= key1} and {= key2};'; |
490 |
$dbi->create_query($source); |
|
491 |
is_deeply($dbi->{_cached}->{$source}, |
|
add table tag
|
492 |
{sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2'], tables => []}, "cache"); |
add cache attribute
|
493 | |
494 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
495 |
$dbi->execute($CREATE_TABLE->{0}); |
|
496 |
$dbi->{_cached} = {}; |
|
497 |
$dbi->cache(0); |
|
498 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
499 |
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache'); |
|
500 | ||
add tests
|
501 |
test 'execute'; |
502 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
503 |
$dbi->execute($CREATE_TABLE->{0}); |
|
removed experimental registe...
|
504 |
{ |
505 |
local $Carp::Verbose = 0; |
|
506 |
eval{$dbi->execute('select * frm table1')}; |
|
cleanup
|
507 |
like($@, qr/\Qselect * frm table1;/, "fail prepare"); |
508 |
like($@, qr/\.t /, "fail : not verbose"); |
|
removed experimental registe...
|
509 |
} |
510 |
{ |
|
511 |
local $Carp::Verbose = 1; |
|
512 |
eval{$dbi->execute('select * frm table1')}; |
|
cleanup
|
513 |
like($@, qr/Custom.*\.t /s, "fail : verbose"); |
removed experimental registe...
|
514 |
} |
add tests
|
515 | |
516 |
eval{$dbi->execute('select * from table1', no_exists => 1)}; |
|
cleanup
|
517 |
like($@, qr/invalid/, "invald SQL"); |
add tests
|
518 | |
519 |
$query = $dbi->create_query('select * from table1 where {= key1}'); |
|
520 |
$dbi->dbh->disconnect; |
|
521 |
eval{$dbi->execute($query, param => {key1 => {a => 1}})}; |
|
cleanup
|
522 |
ok($@, "execute fail"); |
add tests
|
523 | |
removed experimental registe...
|
524 |
{ |
525 |
local $Carp::Verbose = 0; |
|
526 |
eval{$dbi->create_query('select * from table1 where {0 key1}')}; |
|
cleanup
|
527 |
like($@, qr/\Q.t /, "caller spec : not vebose"); |
removed experimental registe...
|
528 |
} |
529 |
{ |
|
530 |
local $Carp::Verbose = 1; |
|
531 |
eval{$dbi->create_query('select * from table1 where {0 key1}')}; |
|
cleanup
|
532 |
like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose"); |
removed experimental registe...
|
533 |
} |
cleanup
|
534 | |
535 | ||
536 |
test 'transaction'; |
|
537 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
538 |
$dbi->execute($CREATE_TABLE->{0}); |
|
539 | ||
540 |
$dbi->begin_work; |
|
541 | ||
542 |
eval { |
|
543 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
544 |
die "Error"; |
|
545 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
546 |
}; |
|
547 | ||
548 |
$dbi->rollback if $@; |
|
549 | ||
550 |
$result = $dbi->select(table => 'table1'); |
|
551 |
$rows = $result->fetch_hash_all; |
|
cleanup
|
552 |
is_deeply($rows, [], "rollback"); |
cleanup
|
553 | |
554 |
$dbi->begin_work; |
|
555 | ||
556 |
eval { |
|
557 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
558 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
559 |
}; |
|
560 | ||
561 |
$dbi->commit unless $@; |
|
562 | ||
563 |
$result = $dbi->select(table => 'table1'); |
|
564 |
$rows = $result->fetch_hash_all; |
|
cleanup
|
565 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "commit"); |
cleanup
|
566 | |
567 |
$dbi->dbh->{AutoCommit} = 0; |
|
568 |
eval{ $dbi->begin_work }; |
|
cleanup
|
569 |
ok($@, "exception"); |
cleanup
|
570 |
$dbi->dbh->{AutoCommit} = 1; |
571 | ||
added helper method
|
572 | |
renamed helper to method.
|
573 |
test 'method'; |
added helper method
|
574 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
renamed helper to method.
|
575 |
$dbi->method( |
added helper method
|
576 |
one => sub { 1 } |
577 |
); |
|
renamed helper to method.
|
578 |
$dbi->method( |
added helper method
|
579 |
two => sub { 2 } |
580 |
); |
|
renamed helper to method.
|
581 |
$dbi->method({ |
added helper method
|
582 |
twice => sub { |
583 |
my $self = shift; |
|
584 |
return $_[0] * 2; |
|
585 |
} |
|
586 |
}); |
|
587 | ||
cleanup
|
588 |
is($dbi->one, 1, "first"); |
589 |
is($dbi->two, 2, "second"); |
|
590 |
is($dbi->twice(5), 10 , "second"); |
|
added helper method
|
591 | |
592 |
eval {$dbi->XXXXXX}; |
|
autoload DBI method
|
593 |
ok($@, "not exists"); |
added helper method
|
594 | |
renamed auto_filter to apply...
|
595 |
test 'out filter'; |
added auto_filter method
|
596 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
597 |
$dbi->execute($CREATE_TABLE->{0}); |
|
598 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
599 |
$dbi->register_filter(three_times => sub { $_[0] * 3}); |
|
renamed auto_filter to apply...
|
600 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
601 |
'table1', 'key1' => {out => 'twice', in => 'three_times'}, |
602 |
'key2' => {out => 'three_times', in => 'twice'}); |
|
added auto_filter method
|
603 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
604 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
605 |
$row = $result->fetch_hash_first; |
|
cleanup
|
606 |
is_deeply($row, {key1 => 2, key2 => 6}, "insert"); |
renamed auto_filter to apply...
|
607 |
$result = $dbi->select(table => 'table1'); |
608 |
$row = $result->fetch_hash_first; |
|
cleanup
|
609 |
is_deeply($row, {key1 => 6, key2 => 12}, "insert"); |
added auto_filter method
|
610 | |
fix bug : filter can't over...
|
611 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
612 |
$dbi->execute($CREATE_TABLE->{0}); |
|
613 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
614 |
$dbi->register_filter(three_times => sub { $_[0] * 3}); |
|
615 |
$dbi->apply_filter( |
|
616 |
'table1', 'key1' => {out => 'twice', in => 'three_times'}, |
|
617 |
'key2' => {out => 'three_times', in => 'twice'}); |
|
618 |
$dbi->apply_filter( |
|
619 |
'table1', 'key1' => {out => undef} |
|
620 |
); |
|
621 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
622 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
623 |
$row = $result->fetch_hash_first; |
|
624 |
is_deeply($row, {key1 => 1, key2 => 6}, "insert"); |
|
625 | ||
added auto_filter method
|
626 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
627 |
$dbi->execute($CREATE_TABLE->{0}); |
|
628 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
renamed auto_filter to apply...
|
629 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
630 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
added auto_filter method
|
631 |
); |
renamed auto_filter to apply...
|
632 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => undef}); |
added auto_filter method
|
633 |
$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2}); |
634 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
635 |
$row = $result->fetch_hash_first; |
|
cleanup
|
636 |
is_deeply($row, {key1 => 4, key2 => 2}, "update"); |
added auto_filter method
|
637 | |
638 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
639 |
$dbi->execute($CREATE_TABLE->{0}); |
|
640 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
renamed auto_filter to apply...
|
641 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
642 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
added auto_filter method
|
643 |
); |
renamed auto_filter to apply...
|
644 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1=> undef}); |
added auto_filter method
|
645 |
$dbi->delete(table => 'table1', where => {key1 => 1}); |
646 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
647 |
$rows = $result->fetch_hash_all; |
|
cleanup
|
648 |
is_deeply($rows, [], "delete"); |
added auto_filter method
|
649 | |
650 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
651 |
$dbi->execute($CREATE_TABLE->{0}); |
|
652 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
renamed auto_filter to apply...
|
653 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
654 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
added auto_filter method
|
655 |
); |
renamed auto_filter to apply...
|
656 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef}); |
added auto_filter method
|
657 |
$result = $dbi->select(table => 'table1', where => {key1 => 1}); |
658 |
$result->filter({'key2' => 'twice'}); |
|
659 |
$rows = $result->fetch_hash_all; |
|
cleanup
|
660 |
is_deeply($rows, [{key1 => 4, key2 => 4}], "select"); |
added auto_filter method
|
661 | |
662 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
663 |
$dbi->execute($CREATE_TABLE->{0}); |
|
664 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
renamed auto_filter to apply...
|
665 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
666 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
added auto_filter method
|
667 |
); |
renamed auto_filter to apply...
|
668 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef}); |
added auto_filter method
|
669 |
$result = $dbi->execute("select * from table1 where {= key1} and {= key2};", |
670 |
param => {key1 => 1, key2 => 2}, |
|
renamed auto_filter to apply...
|
671 |
table => ['table1']); |
added auto_filter method
|
672 |
$rows = $result->fetch_hash_all; |
cleanup
|
673 |
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute"); |
added auto_filter method
|
674 | |
add table tag
|
675 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
676 |
$dbi->execute($CREATE_TABLE->{0}); |
|
677 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
678 |
$dbi->apply_filter( |
|
679 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
|
680 |
); |
|
681 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef}); |
|
682 |
$result = $dbi->execute("select * from {table table1} where {= key1} and {= key2};", |
|
683 |
param => {key1 => 1, key2 => 2}); |
|
684 |
$rows = $result->fetch_hash_all; |
|
685 |
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute table tag"); |
|
686 | ||
added auto_filter method
|
687 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
688 |
$dbi->execute($CREATE_TABLE->{0}); |
|
689 |
$dbi->execute($CREATE_TABLE->{2}); |
|
690 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
691 |
$dbi->register_filter(three_times => sub { $_[0] * 3 }); |
|
renamed auto_filter to apply...
|
692 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
693 |
'table1', 'key2' => {out => 'twice', in => 'twice'} |
added auto_filter method
|
694 |
); |
renamed auto_filter to apply...
|
695 |
$dbi->apply_filter( |
renamed auto_filter to apply...
|
696 |
'table2', 'key3' => {out => 'three_times', in => 'three_times'} |
added auto_filter method
|
697 |
); |
renamed auto_filter to apply...
|
698 |
$dbi->insert(table => 'table1', param => {key1 => 5, key2 => 2}, filter => {key2 => undef}); |
699 |
$dbi->insert(table => 'table2', param => {key1 => 5, key3 => 6}, filter => {key3 => undef}); |
|
added auto_filter method
|
700 |
$result = $dbi->select( |
701 |
table => ['table1', 'table2'], |
|
702 |
column => ['key2', 'key3'], |
|
703 |
where => {'table1.key2' => 1, 'table2.key3' => 2}, relation => {'table1.key1' => 'table2.key1'}); |
|
704 | ||
705 |
$result->filter({'key2' => 'twice'}); |
|
706 |
$rows = $result->fetch_hash_all; |
|
cleanup
|
707 |
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join"); |
added auto_filter method
|
708 | |
709 |
$result = $dbi->select( |
|
710 |
table => ['table1', 'table2'], |
|
711 |
column => ['key2', 'key3'], |
|
712 |
where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'}); |
|
713 | ||
714 |
$result->filter({'key2' => 'twice'}); |
|
715 |
$rows = $result->fetch_hash_all; |
|
cleanup
|
716 |
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join : omit"); |
added auto_filter method
|
717 | |
pod fix
|
718 |
test 'each_column'; |
added experimental iterate_a...
|
719 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
720 |
$dbi->execute($CREATE_TABLE->{2}); |
|
721 |
$dbi->execute($CREATE_TABLE->{3}); |
|
722 | ||
723 |
$infos = []; |
|
pod fix
|
724 |
$dbi->each_column(sub { |
removed experimental txn_sco...
|
725 |
my ($self, $table, $column, $cinfo) = @_; |
added experimental iterate_a...
|
726 |
|
727 |
if ($table =~ /^table/) { |
|
728 |
my $info = [$table, $column, $cinfo->{COLUMN_NAME}]; |
|
729 |
push @$infos, $info; |
|
730 |
} |
|
731 |
}); |
|
732 |
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos]; |
|
733 |
is_deeply($infos, |
|
734 |
[ |
|
735 |
['table1', 'key1', 'key1'], |
|
736 |
['table1', 'key2', 'key2'], |
|
737 |
['table2', 'key1', 'key1'], |
|
738 |
['table2', 'key3', 'key3'] |
|
739 |
] |
|
cleanup
|
740 |
|
added experimental iterate_a...
|
741 |
); |
added auto_filter method
|
742 | |
add examples
|
743 |
test 'limit'; |
744 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
745 |
$dbi->execute($CREATE_TABLE->{0}); |
|
746 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
747 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}); |
|
748 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6}); |
|
renamed DBIx::Custom::TagPro...
|
749 |
$dbi->register_tag( |
add examples
|
750 |
limit => sub { |
751 |
my ($count, $offset) = @_; |
|
752 |
|
|
753 |
my $s = ''; |
|
754 |
$s .= "limit $count"; |
|
755 |
$s .= " offset $offset" if defined $offset; |
|
756 |
|
|
757 |
return [$s, []]; |
|
758 |
} |
|
759 |
); |
|
760 |
$rows = $dbi->select( |
|
761 |
table => 'table1', |
|
762 |
where => {key1 => 1}, |
|
763 |
append => "order by key2 {limit 1 0}" |
|
764 |
)->fetch_hash_all; |
|
cleanup
|
765 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
add examples
|
766 |
$rows = $dbi->select( |
767 |
table => 'table1', |
|
768 |
where => {key1 => 1}, |
|
769 |
append => "order by key2 {limit 2 1}" |
|
770 |
)->fetch_hash_all; |
|
cleanup
|
771 |
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]); |
add examples
|
772 |
$rows = $dbi->select( |
773 |
table => 'table1', |
|
774 |
where => {key1 => 1}, |
|
775 |
append => "order by key2 {limit 1}" |
|
776 |
)->fetch_hash_all; |
|
cleanup
|
777 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
added insert, update, update...
|
778 | |
remove DBIx::Custom::Model
|
779 |
test 'connect super'; |
780 |
{ |
|
781 |
package MyDBI; |
|
782 |
|
|
783 |
use base 'DBIx::Custom'; |
|
784 |
sub connect { |
|
785 |
my $self = shift->SUPER::connect(@_); |
|
786 |
|
|
787 |
return $self; |
|
788 |
} |
|
789 |
|
|
790 |
sub new { |
|
cleanup
|
791 |
my $self = shift->SUPER::new(@_); |
remove DBIx::Custom::Model
|
792 |
|
793 |
return $self; |
|
794 |
} |
|
795 |
} |
|
796 | ||
797 |
$dbi = MyDBI->connect($NEW_ARGS->{0}); |
|
798 |
$dbi->execute($CREATE_TABLE->{0}); |
|
799 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
cleanup
|
800 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1); |
remove DBIx::Custom::Model
|
801 | |
802 |
$dbi = MyDBI->new($NEW_ARGS->{0}); |
|
cleanup
|
803 |
$dbi->connect; |
remove DBIx::Custom::Model
|
804 |
$dbi->execute($CREATE_TABLE->{0}); |
805 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
cleanup
|
806 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1); |
added experimental DBIx::Cus...
|
807 | |
cleanup
|
808 |
{ |
809 |
package MyDBI2; |
|
810 |
|
|
811 |
use base 'DBIx::Custom'; |
|
812 |
sub connect { |
|
813 |
my $self = shift->SUPER::new(@_); |
|
814 |
$self->connect; |
|
815 |
|
|
816 |
return $self; |
|
817 |
} |
|
818 |
} |
|
819 | ||
820 |
$dbi = MyDBI->connect($NEW_ARGS->{0}); |
|
821 |
$dbi->execute($CREATE_TABLE->{0}); |
|
822 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
823 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1); |
|
added experimental DBIx::Cus...
|
824 | |
825 |
test 'end_filter'; |
|
826 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
827 |
$dbi->execute($CREATE_TABLE->{0}); |
|
828 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
829 |
$result = $dbi->select(table => 'table1'); |
|
830 |
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 }); |
|
831 |
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 }); |
|
832 |
$row = $result->fetch_first; |
|
833 |
is_deeply($row, [6, 40]); |
|
834 | ||
all filter can receive array...
|
835 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
836 |
$dbi->execute($CREATE_TABLE->{0}); |
|
837 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
838 |
$result = $dbi->select(table => 'table1'); |
|
839 |
$result->filter([qw/key1 key2/] => sub { $_[0] * 2 }); |
|
840 |
$result->end_filter([[qw/key1 key2/] => sub { $_[0] * 3 }]); |
|
841 |
$row = $result->fetch_first; |
|
842 |
is_deeply($row, [6, 12]); |
|
843 | ||
844 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
845 |
$dbi->execute($CREATE_TABLE->{0}); |
|
846 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
847 |
$result = $dbi->select(table => 'table1'); |
|
848 |
$result->filter([[qw/key1 key2/] => sub { $_[0] * 2 }]); |
|
849 |
$result->end_filter([qw/key1 key2/] => sub { $_[0] * 3 }); |
|
850 |
$row = $result->fetch_first; |
|
851 |
is_deeply($row, [6, 12]); |
|
852 | ||
many changed
|
853 |
$dbi->register_filter(five_times => sub { $_[0] * 5 }); |
added experimental DBIx::Cus...
|
854 |
$result = $dbi->select(table => 'table1'); |
855 |
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 }); |
|
many changed
|
856 |
$result->end_filter({key1 => sub { $_[0] * 3 }, key2 => 'five_times' }); |
added experimental DBIx::Cus...
|
857 |
$row = $result->fetch_hash_first; |
858 |
is_deeply($row, {key1 => 6, key2 => 40}); |
|
fix select method empty wher...
|
859 | |
fix bug : filter can't over...
|
860 |
$dbi->register_filter(five_times => sub { $_[0] * 5 }); |
861 |
$dbi->apply_filter('table1', |
|
862 |
key1 => {end => sub { $_[0] * 3 } }, |
|
863 |
key2 => {end => 'five_times'} |
|
864 |
); |
|
865 |
$result = $dbi->select(table => 'table1'); |
|
866 |
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 }); |
|
867 |
$row = $result->fetch_hash_first; |
|
868 |
is_deeply($row, {key1 => 6, key2 => 40}, 'apply_filter'); |
|
869 | ||
870 |
$dbi->register_filter(five_times => sub { $_[0] * 5 }); |
|
871 |
$dbi->apply_filter('table1', |
|
872 |
key1 => {end => sub { $_[0] * 3 } }, |
|
873 |
key2 => {end => 'five_times'} |
|
874 |
); |
|
875 |
$result = $dbi->select(table => 'table1'); |
|
876 |
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 }); |
|
877 |
$result->filter(key1 => undef); |
|
878 |
$result->end_filter(key1 => undef); |
|
879 |
$row = $result->fetch_hash_first; |
|
880 |
is_deeply($row, {key1 => 1, key2 => 40}, 'apply_filter overwrite'); |
|
fix select method empty wher...
|
881 | |
added experimental DBIx::Cus...
|
882 |
test 'remove_end_filter and remove_filter'; |
added experimental DBIx::Cus...
|
883 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
884 |
$dbi->execute($CREATE_TABLE->{0}); |
|
885 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
886 |
$result = $dbi->select(table => 'table1'); |
|
added experimental DBIx::Cus...
|
887 |
$row = $result |
888 |
->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 }) |
|
889 |
->remove_filter |
|
890 |
->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 }) |
|
added experimental DBIx::Cus...
|
891 |
->remove_end_filter |
892 |
->fetch_first; |
|
added experimental DBIx::Cus...
|
893 |
is_deeply($row, [1, 2]); |
added experimental DBIx::Cus...
|
894 | |
fix select method empty wher...
|
895 |
test 'empty where select'; |
896 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
897 |
$dbi->execute($CREATE_TABLE->{0}); |
|
898 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
899 |
$result = $dbi->select(table => 'table1', where => {}); |
|
900 |
$row = $result->fetch_hash_first; |
|
901 |
is_deeply($row, {key1 => 1, key2 => 2}); |
|
902 | ||
added experimental sugar met...
|
903 |
test 'select query option'; |
904 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
905 |
$dbi->execute($CREATE_TABLE->{0}); |
|
906 |
$query = $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, query => 1); |
|
907 |
is(ref $query, 'DBIx::Custom::Query'); |
|
908 |
$query = $dbi->update(table => 'table1', where => {key1 => 1}, param => {key2 => 2}, query => 1); |
|
909 |
is(ref $query, 'DBIx::Custom::Query'); |
|
910 |
$query = $dbi->delete(table => 'table1', where => {key1 => 1}, query => 1); |
|
911 |
is(ref $query, 'DBIx::Custom::Query'); |
|
912 |
$query = $dbi->select(table => 'table1', where => {key1 => 1, key2 => 2}, query => 1); |
|
913 |
is(ref $query, 'DBIx::Custom::Query'); |
|
914 | ||
added experimental DBIx::Cus...
|
915 |
test 'DBIx::Custom::Where'; |
experimental extended select...
|
916 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
917 |
$dbi->execute($CREATE_TABLE->{0}); |
|
918 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
919 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
renamed DBIx::Custom::TagPro...
|
920 |
$where = $dbi->where->clause(['and', '{= key1}', '{= key2}']); |
921 |
is("$where", "where ( {= key1} and {= key2} )", 'no param'); |
|
922 | ||
added experimental DBIx::Cus...
|
923 |
$where = $dbi->where |
added test
|
924 |
->clause(['and', '{= key1}', '{= key2}']) |
added experimental DBIx::Cus...
|
925 |
->param({key1 => 1}); |
added test
|
926 | |
experimental extended select...
|
927 |
$result = $dbi->select( |
928 |
table => 'table1', |
|
added experimental DBIx::Cus...
|
929 |
where => $where |
experimental extended select...
|
930 |
); |
931 |
$row = $result->fetch_hash_all; |
|
932 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
933 | ||
added experimental DBIx::Cus...
|
934 |
$where = $dbi->where |
added test
|
935 |
->clause(['and', '{= key1}', '{= key2}']) |
added experimental DBIx::Cus...
|
936 |
->param({key1 => 1, key2 => 2}); |
experimental extended select...
|
937 |
$result = $dbi->select( |
938 |
table => 'table1', |
|
added experimental DBIx::Cus...
|
939 |
where => $where |
experimental extended select...
|
940 |
); |
941 |
$row = $result->fetch_hash_all; |
|
942 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
943 | ||
added experimental DBIx::Cus...
|
944 |
$where = $dbi->where |
added test
|
945 |
->clause(['and', '{= key1}', '{= key2}']) |
added experimental DBIx::Cus...
|
946 |
->param({}); |
experimental extended select...
|
947 |
$result = $dbi->select( |
948 |
table => 'table1', |
|
added experimental DBIx::Cus...
|
949 |
where => $where, |
experimental extended select...
|
950 |
); |
951 |
$row = $result->fetch_hash_all; |
|
952 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
953 | ||
added experimental DBIx::Cus...
|
954 |
$where = $dbi->where |
added test
|
955 |
->clause(['and', ['or', '{> key1}', '{< key1}'], '{= key2}']) |
added experimental DBIx::Cus...
|
956 |
->param({key1 => [0, 3], key2 => 2}); |
experimental extended select...
|
957 |
$result = $dbi->select( |
958 |
table => 'table1', |
|
added experimental DBIx::Cus...
|
959 |
where => $where, |
added experimental not_exist...
|
960 |
); |
experimental extended select...
|
961 |
$row = $result->fetch_hash_all; |
962 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
963 | ||
added experimental DBIx::Cus...
|
964 |
$where = $dbi->where; |
965 |
$result = $dbi->select( |
|
966 |
table => 'table1', |
|
967 |
where => $where |
|
968 |
); |
|
969 |
$row = $result->fetch_hash_all; |
|
970 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
971 | ||
experimental extended select...
|
972 |
eval { |
added experimental DBIx::Cus...
|
973 |
$where = $dbi->where |
added test
|
974 |
->clause(['uuu']); |
experimental extended select...
|
975 |
$result = $dbi->select( |
976 |
table => 'table1', |
|
added experimental DBIx::Cus...
|
977 |
where => $where |
experimental extended select...
|
978 |
); |
979 |
}; |
|
980 |
ok($@); |
|
981 | ||
added experimental DBIx::Cus...
|
982 |
$where = $dbi->where; |
983 |
is("$where", ''); |
|
984 | ||
added test
|
985 |
$where = $dbi->where |
986 |
->clause(['or', ('{= key1}') x 2]) |
|
987 |
->param({key1 => [1, 3]}); |
|
988 |
$result = $dbi->select( |
|
989 |
table => 'table1', |
|
990 |
where => $where, |
|
991 |
); |
|
992 |
$row = $result->fetch_hash_all; |
|
993 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
994 | ||
995 |
$where = $dbi->where |
|
996 |
->clause(['or', ('{= key1}') x 2]) |
|
997 |
->param({key1 => [1]}); |
|
998 |
$result = $dbi->select( |
|
999 |
table => 'table1', |
|
1000 |
where => $where, |
|
1001 |
); |
|
1002 |
$row = $result->fetch_hash_all; |
|
1003 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
1004 | ||
1005 |
$where = $dbi->where |
|
1006 |
->clause(['or', ('{= key1}') x 2]) |
|
1007 |
->param({key1 => 1}); |
|
1008 |
$result = $dbi->select( |
|
1009 |
table => 'table1', |
|
1010 |
where => $where, |
|
1011 |
); |
|
1012 |
$row = $result->fetch_hash_all; |
|
1013 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
added experimental DBIx::Cus...
|
1014 | |
many changed
|
1015 |
$where = $dbi->where |
1016 |
->clause('{= key1}') |
|
1017 |
->param({key1 => 1}); |
|
1018 |
$result = $dbi->select( |
|
1019 |
table => 'table1', |
|
1020 |
where => $where, |
|
1021 |
); |
|
1022 |
$row = $result->fetch_hash_all; |
|
1023 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
1024 | ||
1025 |
$where = $dbi->where |
|
1026 |
->clause('{= key1} {= key2}') |
|
1027 |
->param({key1 => 1}); |
|
1028 |
eval{$where->to_string}; |
|
1029 |
like($@, qr/one column/); |
|
1030 | ||
added experimental not_exist...
|
1031 |
$where = $dbi->where |
1032 |
->clause('{= key1}') |
|
1033 |
->param([]); |
|
1034 |
eval{$where->to_string}; |
|
1035 |
like($@, qr/Parameter/); |
|
1036 | ||
1037 |
$where = $dbi->where |
|
1038 |
->clause(['or', ('{= key1}') x 3]) |
|
1039 |
->param({key1 => [$dbi->not_exists, 1, 3]}); |
|
1040 |
$result = $dbi->select( |
|
1041 |
table => 'table1', |
|
1042 |
where => $where, |
|
1043 |
); |
|
1044 |
$row = $result->fetch_hash_all; |
|
1045 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists'); |
|
1046 | ||
1047 |
$where = $dbi->where |
|
1048 |
->clause(['or', ('{= key1}') x 3]) |
|
1049 |
->param({key1 => [1, $dbi->not_exists, 3]}); |
|
1050 |
$result = $dbi->select( |
|
1051 |
table => 'table1', |
|
1052 |
where => $where, |
|
1053 |
); |
|
1054 |
$row = $result->fetch_hash_all; |
|
1055 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists'); |
|
1056 | ||
1057 |
$where = $dbi->where |
|
1058 |
->clause(['or', ('{= key1}') x 3]) |
|
1059 |
->param({key1 => [1, 3, $dbi->not_exists]}); |
|
1060 |
$result = $dbi->select( |
|
1061 |
table => 'table1', |
|
1062 |
where => $where, |
|
1063 |
); |
|
1064 |
$row = $result->fetch_hash_all; |
|
1065 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists'); |
|
1066 | ||
1067 |
$where = $dbi->where |
|
1068 |
->clause(['or', ('{= key1}') x 3]) |
|
1069 |
->param({key1 => [1, $dbi->not_exists, $dbi->not_exists]}); |
|
1070 |
$result = $dbi->select( |
|
1071 |
table => 'table1', |
|
1072 |
where => $where, |
|
1073 |
); |
|
1074 |
$row = $result->fetch_hash_all; |
|
1075 |
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists'); |
|
1076 | ||
1077 |
$where = $dbi->where |
|
1078 |
->clause(['or', ('{= key1}') x 3]) |
|
1079 |
->param({key1 => [$dbi->not_exists, 1, $dbi->not_exists]}); |
|
1080 |
$result = $dbi->select( |
|
1081 |
table => 'table1', |
|
1082 |
where => $where, |
|
1083 |
); |
|
1084 |
$row = $result->fetch_hash_all; |
|
1085 |
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists'); |
|
1086 | ||
1087 |
$where = $dbi->where |
|
1088 |
->clause(['or', ('{= key1}') x 3]) |
|
1089 |
->param({key1 => [$dbi->not_exists, $dbi->not_exists, 1]}); |
|
1090 |
$result = $dbi->select( |
|
1091 |
table => 'table1', |
|
1092 |
where => $where, |
|
1093 |
); |
|
1094 |
$row = $result->fetch_hash_all; |
|
1095 |
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists'); |
|
1096 | ||
1097 |
$where = $dbi->where |
|
1098 |
->clause(['or', ('{= key1}') x 3]) |
|
1099 |
->param({key1 => [$dbi->not_exists, $dbi->not_exists, $dbi->not_exists]}); |
|
1100 |
$result = $dbi->select( |
|
1101 |
table => 'table1', |
|
1102 |
where => $where, |
|
1103 |
); |
|
1104 |
$row = $result->fetch_hash_all; |
|
1105 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists'); |
|
1106 | ||
make delete() using where ob...
|
1107 |
$where = $dbi->where |
1108 |
->clause(['or', ('{= key1}') x 3]) |
|
1109 |
->param({key1 => []}); |
|
1110 |
$result = $dbi->select( |
|
1111 |
table => 'table1', |
|
1112 |
where => $where, |
|
1113 |
); |
|
1114 |
$row = $result->fetch_hash_all; |
|
1115 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists'); |
|
1116 | ||
1117 |
$where = $dbi->where |
|
1118 |
->clause(['and', '{> key1}', '{< key1}' ]) |
|
1119 |
->param({key1 => [2, $dbi->not_exists]}); |
|
1120 |
$result = $dbi->select( |
|
1121 |
table => 'table1', |
|
1122 |
where => $where, |
|
1123 |
); |
|
1124 |
$row = $result->fetch_hash_all; |
|
1125 |
is_deeply($row, [{key1 => 3, key2 => 4}], 'not_exists'); |
|
1126 | ||
1127 |
$where = $dbi->where |
|
1128 |
->clause(['and', '{> key1}', '{< key1}' ]) |
|
1129 |
->param({key1 => [$dbi->not_exists, 2]}); |
|
1130 |
$result = $dbi->select( |
|
1131 |
table => 'table1', |
|
1132 |
where => $where, |
|
1133 |
); |
|
1134 |
$row = $result->fetch_hash_all; |
|
1135 |
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists'); |
|
1136 | ||
1137 |
$where = $dbi->where |
|
1138 |
->clause(['and', '{> key1}', '{< key1}' ]) |
|
1139 |
->param({key1 => [$dbi->not_exists, $dbi->not_exists]}); |
|
1140 |
$result = $dbi->select( |
|
1141 |
table => 'table1', |
|
1142 |
where => $where, |
|
1143 |
); |
|
1144 |
$row = $result->fetch_hash_all; |
|
1145 |
is_deeply($row, [{key1 => 1, key2 => 2},{key1 => 3, key2 => 4}], 'not_exists'); |
|
1146 | ||
1147 |
$where = $dbi->where |
|
1148 |
->clause(['and', '{> key1}', '{< key1}' ]) |
|
1149 |
->param({key1 => [0, 2]}); |
|
1150 |
$result = $dbi->select( |
|
1151 |
table => 'table1', |
|
1152 |
where => $where, |
|
1153 |
); |
|
1154 |
$row = $result->fetch_hash_all; |
|
1155 |
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists'); |
|
1156 | ||
renamed dbi_options to dbi_o...
|
1157 |
test 'dbi_option default'; |
added experimental DBIx::Cus...
|
1158 |
$dbi = DBIx::Custom->new; |
renamed dbi_options to dbi_o...
|
1159 |
is_deeply($dbi->dbi_option, {}); |
added experimental DBIx::Cus...
|
1160 | |
added register_tag_processor
|
1161 |
test 'register_tag_processor'; |
1162 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1163 |
$dbi->register_tag_processor( |
|
1164 |
a => sub { 1 } |
|
1165 |
); |
|
1166 |
is($dbi->query_builder->tag_processors->{a}->(), 1); |
|
added experimental DBIx::Cus...
|
1167 | |
renamed DBIx::Custom::TagPro...
|
1168 |
test 'register_tag'; |
1169 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1170 |
$dbi->register_tag( |
|
1171 |
b => sub { 2 } |
|
1172 |
); |
|
1173 |
is($dbi->query_builder->tags->{b}->(), 2); |
|
1174 | ||
added table not specified ex...
|
1175 |
test 'table not specify exception'; |
1176 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1177 |
eval {$dbi->insert}; |
|
1178 |
like($@, qr/table/); |
|
1179 |
eval {$dbi->update}; |
|
1180 |
like($@, qr/table/); |
|
1181 |
eval {$dbi->delete}; |
|
1182 |
like($@, qr/table/); |
|
1183 |
eval {$dbi->select}; |
|
1184 |
like($@, qr/table/); |
|
many changed
|
1185 | |
1186 | ||
1187 |
test 'more tests'; |
|
1188 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1189 |
eval{$dbi->apply_filter('table', 'column', [])}; |
|
1190 |
like($@, qr/apply_filter/); |
|
1191 | ||
1192 |
eval{$dbi->apply_filter('table', 'column', {outer => 2})}; |
|
1193 |
like($@, qr/apply_filter/); |
|
1194 | ||
1195 |
$dbi->apply_filter( |
|
1196 | ||
1197 |
); |
|
1198 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1199 |
$dbi->execute($CREATE_TABLE->{0}); |
|
1200 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1201 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
1202 |
$dbi->apply_filter('table1', 'key2', |
|
1203 |
{in => sub { $_[0] * 3 }, out => sub { $_[0] * 2 }}); |
|
1204 |
$rows = $dbi->select(table => 'table1', where => {key2 => 1})->fetch_hash_all; |
|
1205 |
is_deeply($rows, [{key1 => 1, key2 => 6}]); |
|
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 |
$rows = $dbi->select(table => 'table1', where => {key2 => 2})->fetch_hash_all; |
|
1213 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
1214 | ||
1215 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1216 |
eval {$dbi->apply_filter('table1', 'key2', {out => 'no'})}; |
|
1217 |
like($@, qr/not registered/); |
|
1218 |
eval {$dbi->apply_filter('table1', 'key2', {in => 'no'})}; |
|
1219 |
like($@, qr/not registered/); |
|
renamed helper to method.
|
1220 |
$dbi->method({one => sub { 1 }}); |
many changed
|
1221 |
is($dbi->one, 1); |
1222 | ||
1223 |
eval{DBIx::Custom->connect()}; |
|
1224 |
like($@, qr/connect/); |
|
1225 | ||
1226 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1227 |
$dbi->execute($CREATE_TABLE->{0}); |
|
1228 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
1229 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, |
|
1230 |
filter => {key1 => 'twice'}); |
|
1231 |
$row = $dbi->select(table => 'table1')->fetch_hash_first; |
|
1232 |
is_deeply($row, {key1 => 2, key2 => 2}); |
|
1233 |
eval {$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, |
|
1234 |
filter => {key1 => 'no'}) }; |
|
1235 |
like($@, qr//); |
|
1236 | ||
1237 |
$dbi->register_filter(one => sub { }); |
|
1238 |
$dbi->default_fetch_filter('one'); |
|
1239 |
ok($dbi->default_fetch_filter); |
|
1240 |
$dbi->default_bind_filter('one'); |
|
1241 |
ok($dbi->default_bind_filter); |
|
1242 |
eval{$dbi->default_fetch_filter('no')}; |
|
1243 |
like($@, qr/not registered/); |
|
1244 |
eval{$dbi->default_bind_filter('no')}; |
|
1245 |
like($@, qr/not registered/); |
|
1246 |
$dbi->default_bind_filter(undef); |
|
1247 |
ok(!defined $dbi->default_bind_filter); |
|
1248 |
$dbi->default_fetch_filter(undef); |
|
1249 |
ok(!defined $dbi->default_fetch_filter); |
|
1250 |
eval {$dbi->execute('select * from table1 {= author') }; |
|
1251 |
like($@, qr/Tag not finished/); |
|
1252 | ||
1253 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1254 |
$dbi->execute($CREATE_TABLE->{0}); |
|
1255 |
$dbi->register_filter(one => sub { 1 }); |
|
1256 |
$result = $dbi->select(table => 'table1'); |
|
1257 |
eval {$result->filter(key1 => 'no')}; |
|
1258 |
like($@, qr/not registered/); |
|
1259 |
eval {$result->end_filter(key1 => 'no')}; |
|
1260 |
like($@, qr/not registered/); |
|
1261 |
$result->default_filter(undef); |
|
1262 |
ok(!defined $result->default_filter); |
|
1263 |
$result->default_filter('one'); |
|
1264 |
is($result->default_filter->(), 1); |
|
1265 | ||
renamed dbi_options to dbi_o...
|
1266 |
test 'dbi_option'; |
1267 |
$dbi = DBIx::Custom->connect(data_source => 'dbi:SQLite:dbname=:memory:', |
|
1268 |
dbi_option => {PrintError => 1}); |
|
1269 |
ok($dbi->dbh->{PrintError}); |
|
1270 |
$dbi = DBIx::Custom->connect(data_source => 'dbi:SQLite:dbname=:memory:', |
|
1271 |
dbi_options => {PrintError => 1}); |
|
1272 |
ok($dbi->dbh->{PrintError}); |
|
1273 | ||
added experimental DBIx::Cus...
|
1274 |
test 'DBIx::Custom::Result stash()'; |
1275 |
$result = DBIx::Custom::Result->new; |
|
1276 |
is_deeply($result->stash, {}, 'default'); |
|
1277 |
$result->stash->{foo} = 1; |
|
1278 |
is($result->stash->{foo}, 1, 'get and set'); |
|
table object call dbi object...
|
1279 | |
fix bug : filter can't over...
|
1280 |
test 'filter __ expression'; |
1281 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1282 |
$dbi->execute('create table company (id, name, location_id)'); |
|
1283 |
$dbi->execute('create table location (id, name)'); |
|
1284 |
$dbi->apply_filter('location', |
|
1285 |
name => {in => sub { uc $_[0] } } |
|
1286 |
); |
|
1287 | ||
1288 |
$dbi->insert(table => 'company', param => {id => 1, name => 'a', location_id => 2}); |
|
1289 |
$dbi->insert(table => 'location', param => {id => 2, name => 'b'}); |
|
1290 | ||
1291 |
$result = $dbi->select( |
|
1292 |
table => ['company', 'location'], relation => {'company.location_id' => 'location.id'}, |
|
1293 |
column => ['location.name as location__name'] |
|
1294 |
); |
|
1295 |
is($result->fetch_first->[0], 'B'); |
|
1296 | ||
add experimental DBIx::Custo...
|
1297 |
$result = $dbi->select( |
1298 |
table => 'company', relation => {'company.location_id' => 'location.id'}, |
|
1299 |
column => ['location.name as location__name'] |
|
1300 |
); |
|
1301 |
is($result->fetch_first->[0], 'B'); |
|
1302 | ||
add experimental selection o...
|
1303 |
test 'selection'; |
1304 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1305 |
$dbi->execute($CREATE_TABLE->{0}); |
|
1306 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
remove experimental DBIx::Cu...
|
1307 |
$result = $dbi->select(selection => '* from {table table1}', where => {key1 => 1}); |
add experimental selection o...
|
1308 |
is_deeply($result->fetch_hash_all, [{key1 => 1, key2 => 2}]); |
removed experimental base_ta...
|
1309 | |
add feture. all model class ...
|
1310 |
test 'Model class'; |
removed experimental base_ta...
|
1311 |
use MyDBI1; |
1312 |
$dbi = MyDBI1->connect($NEW_ARGS->{0}); |
|
1313 |
$dbi->execute("create table book (title, author)"); |
|
add feture. all model class ...
|
1314 |
$model = $dbi->model('book'); |
1315 |
$model->insert({title => 'a', author => 'b'}); |
|
1316 |
is_deeply($model->list->fetch_hash_all, [{title => 'a', author => 'b'}], 'basic'); |
|
removed experimental base_ta...
|
1317 |
$dbi->execute("create table company (name)"); |
add feture. all model class ...
|
1318 |
$model = $dbi->model('company'); |
1319 |
$model->insert({name => 'a'}); |
|
1320 |
is_deeply($model->list->fetch_hash_all, [{name => 'a'}], 'basic'); |
|
add models() attribute
|
1321 |
is($dbi->models->{'book'}, $dbi->model('book')); |
1322 |
is($dbi->models->{'company'}, $dbi->model('company')); |
|
removed experimental base_ta...
|
1323 | |
1324 |
{ |
|
1325 |
package MyDBI4; |
|
1326 | ||
1327 |
use strict; |
|
1328 |
use warnings; |
|
1329 | ||
1330 |
use base 'DBIx::Custom'; |
|
1331 | ||
1332 |
sub connect { |
|
1333 |
my $self = shift->SUPER::connect(@_); |
|
1334 |
|
|
add feture. all model class ...
|
1335 |
$self->include_model( |
1336 |
MyModel2 => [ |
|
removed experimental base_ta...
|
1337 |
'book', |
add experimental DBIx::Custo...
|
1338 |
{class => 'Company', name => 'company'} |
removed experimental base_ta...
|
1339 |
] |
1340 |
); |
|
1341 |
} |
|
1342 | ||
add feture. all model class ...
|
1343 |
package MyModel2::Base1; |
removed experimental base_ta...
|
1344 | |
1345 |
use strict; |
|
1346 |
use warnings; |
|
1347 | ||
add feture. all model class ...
|
1348 |
use base 'DBIx::Custom::Model'; |
removed experimental base_ta...
|
1349 | |
add feture. all model class ...
|
1350 |
package MyModel2::book; |
removed experimental base_ta...
|
1351 | |
1352 |
use strict; |
|
1353 |
use warnings; |
|
1354 | ||
add feture. all model class ...
|
1355 |
use base 'MyModel2::Base1'; |
removed experimental base_ta...
|
1356 | |
1357 |
sub insert { |
|
1358 |
my ($self, $param) = @_; |
|
1359 |
|
|
1360 |
return $self->SUPER::insert(param => $param); |
|
1361 |
} |
|
1362 | ||
1363 |
sub list { shift->select; } |
|
1364 | ||
add feture. all model class ...
|
1365 |
package MyModel2::Company; |
removed experimental base_ta...
|
1366 | |
1367 |
use strict; |
|
1368 |
use warnings; |
|
1369 | ||
add feture. all model class ...
|
1370 |
use base 'MyModel2::Base1'; |
removed experimental base_ta...
|
1371 | |
1372 |
sub insert { |
|
1373 |
my ($self, $param) = @_; |
|
1374 |
|
|
1375 |
return $self->SUPER::insert(param => $param); |
|
1376 |
} |
|
1377 | ||
1378 |
sub list { shift->select; } |
|
1379 |
} |
|
1380 |
$dbi = MyDBI4->connect($NEW_ARGS->{0}); |
|
1381 |
$dbi->execute("create table book (title, author)"); |
|
add feture. all model class ...
|
1382 |
$model = $dbi->model('book'); |
1383 |
$model->insert({title => 'a', author => 'b'}); |
|
1384 |
is_deeply($model->list->fetch_hash_all, [{title => 'a', author => 'b'}], 'basic'); |
|
removed experimental base_ta...
|
1385 |
$dbi->execute("create table company (name)"); |
add feture. all model class ...
|
1386 |
$model = $dbi->model('company'); |
1387 |
$model->insert({name => 'a'}); |
|
1388 |
is_deeply($model->list->fetch_hash_all, [{name => 'a'}], 'basic'); |
|
1389 | ||
1390 |
{ |
|
1391 |
package MyDBI5; |
|
1392 | ||
1393 |
use strict; |
|
1394 |
use warnings; |
|
1395 | ||
1396 |
use base 'DBIx::Custom'; |
|
1397 | ||
1398 |
sub connect { |
|
1399 |
my $self = shift->SUPER::connect(@_); |
|
1400 |
|
|
1401 |
$self->include_model('MyModel4'); |
|
1402 |
} |
|
1403 |
} |
|
1404 |
$dbi = MyDBI5->connect($NEW_ARGS->{0}); |
|
1405 |
$dbi->execute("create table company (name)"); |
|
1406 |
$model = $dbi->model('company'); |
|
1407 |
$model->insert({name => 'a'}); |
|
1408 |
is_deeply($model->list->fetch_hash_all, [{name => 'a'}], 'include all model'); |
|
1409 |
$model = $dbi->model('book'); |
|
1410 |
is_deeply($model->list->fetch_hash_all, [{name => 'a'}], 'include all model'); |
|
1411 | ||
add DBIx::Custom::Model fore...
|
1412 |
test 'primary_key'; |
1413 |
use MyDBI1; |
|
1414 |
$dbi = MyDBI1->connect($NEW_ARGS->{0}); |
|
1415 |
$model = $dbi->model('book'); |
|
1416 |
$model->primary_key(['id', 'number']); |
|
1417 |
is_deeply($model->primary_key, ['id', 'number']); |
|
1418 | ||
add DBIx::Custom::Model colu...
|
1419 |
test 'columns'; |
1420 |
use MyDBI1; |
|
1421 |
$dbi = MyDBI1->connect($NEW_ARGS->{0}); |
|
1422 |
$model = $dbi->model('book'); |
|
1423 |
$model->columns(['id', 'number']); |
|
1424 |
is_deeply($model->columns, ['id', 'number']); |
|
add feture. all model class ...
|
1425 | |
add experimental setup_model...
|
1426 |
test 'setup_model'; |
1427 |
use MyDBI1; |
|
1428 |
$dbi = MyDBI1->connect($NEW_ARGS->{0}); |
|
1429 |
$dbi->execute('create table book (id)'); |
|
1430 |
$dbi->execute('create table company (id, name);'); |
|
1431 |
$dbi->execute('create table test (id, name, primary key (id, name));'); |
|
1432 |
$dbi->setup_model; |
|
1433 |
is_deeply($dbi->model('book')->columns, ['id']); |
|
1434 |
is_deeply($dbi->model('company')->columns, ['id', 'name']); |
|
add experimental update_at()...
|
1435 | |
1436 |
test 'delete_at'; |
|
1437 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1438 |
$dbi->execute($CREATE_TABLE->{1}); |
|
1439 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
1440 |
$dbi->delete_at( |
|
1441 |
table => 'table1', |
|
1442 |
primary_key => ['key1', 'key2'], |
|
1443 |
where => [1, 2], |
|
1444 |
); |
|
1445 |
is_deeply($dbi->select(table => 'table1')->fetch_hash_all, []); |
|
1446 | ||
1447 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
1448 |
$dbi->delete_at( |
|
1449 |
table => 'table1', |
|
1450 |
primary_key => 'key1', |
|
1451 |
where => 1, |
|
1452 |
); |
|
1453 |
is_deeply($dbi->select(table => 'table1')->fetch_hash_all, []); |
|
1454 | ||
- added experimental DBIx::C...
|
1455 |
test 'insert_at'; |
1456 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1457 |
$dbi->execute($CREATE_TABLE->{1}); |
|
1458 |
$dbi->insert_at( |
|
1459 |
primary_key => ['key1', 'key2'], |
|
1460 |
table => 'table1', |
|
1461 |
where => [1, 2], |
|
1462 |
param => {key3 => 3} |
|
1463 |
); |
|
1464 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1); |
|
1465 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key2}, 2); |
|
1466 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key3}, 3); |
|
1467 | ||
1468 |
$dbi->delete_all(table => 'table1'); |
|
add experimental update_at()...
|
1469 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
- added experimental DBIx::C...
|
1470 |
$dbi->insert_at( |
1471 |
primary_key => 'key1', |
|
add experimental update_at()...
|
1472 |
table => 'table1', |
- added experimental DBIx::C...
|
1473 |
where => 1, |
1474 |
param => {key2 => 2, key3 => 3} |
|
add experimental update_at()...
|
1475 |
); |
1476 | ||
- added experimental DBIx::C...
|
1477 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1); |
1478 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key2}, 2); |
|
1479 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key3}, 3); |
|
1480 | ||
1481 |
eval { |
|
1482 |
$dbi->insert_at( |
|
1483 |
table => 'table1', |
|
1484 |
primary_key => ['key1', 'key2'], |
|
1485 |
where => {}, |
|
1486 |
param => {key1 => 1, key2 => 2, key3 => 3}, |
|
1487 |
); |
|
1488 |
}; |
|
1489 |
like($@, qr/must be/); |
|
add experimental update_at()...
|
1490 | |
1491 |
test 'update_at'; |
|
1492 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1493 |
$dbi->execute($CREATE_TABLE->{1}); |
|
1494 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
1495 |
$dbi->update_at( |
|
1496 |
table => 'table1', |
|
1497 |
primary_key => ['key1', 'key2'], |
|
1498 |
where => [1, 2], |
|
1499 |
param => {key3 => 4} |
|
1500 |
); |
|
1501 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1); |
|
1502 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key2}, 2); |
|
1503 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key3}, 4); |
|
1504 | ||
1505 |
$dbi->delete_all(table => 'table1'); |
|
1506 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
1507 |
$dbi->update_at( |
|
1508 |
table => 'table1', |
|
1509 |
primary_key => 'key1', |
|
1510 |
where => 1, |
|
1511 |
param => {key3 => 4} |
|
1512 |
); |
|
1513 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1); |
|
1514 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key2}, 2); |
|
1515 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key3}, 4); |
|
1516 | ||
1517 |
test 'select_at'; |
|
1518 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1519 |
$dbi->execute($CREATE_TABLE->{1}); |
|
1520 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
1521 |
$result = $dbi->select_at( |
|
1522 |
table => 'table1', |
|
1523 |
primary_key => ['key1', 'key2'], |
|
1524 |
where => [1, 2] |
|
1525 |
); |
|
1526 |
$row = $result->fetch_hash_first; |
|
1527 |
is($row->{key1}, 1); |
|
1528 |
is($row->{key2}, 2); |
|
1529 |
is($row->{key3}, 3); |
|
1530 | ||
1531 |
$dbi->delete_all(table => 'table1'); |
|
1532 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
1533 |
$result = $dbi->select_at( |
|
1534 |
table => 'table1', |
|
1535 |
primary_key => 'key1', |
|
1536 |
where => 1, |
|
1537 |
); |
|
1538 |
$row = $result->fetch_hash_first; |
|
1539 |
is($row->{key1}, 1); |
|
1540 |
is($row->{key2}, 2); |
|
1541 |
is($row->{key3}, 3); |
|
1542 | ||
1543 |
$dbi->delete_all(table => 'table1'); |
|
1544 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
1545 |
$result = $dbi->select_at( |
|
1546 |
table => 'table1', |
|
1547 |
primary_key => ['key1', 'key2'], |
|
1548 |
param => {key1 => 1, key2 => 2}, |
|
1549 |
); |
|
1550 |
$row = $result->fetch_hash_first; |
|
1551 |
is($row->{key1}, 1); |
|
1552 |
is($row->{key2}, 2); |
|
1553 |
is($row->{key3}, 3); |
|
1554 | ||
- added experimental DBIx::C...
|
1555 |
eval { |
1556 |
$result = $dbi->select_at( |
|
1557 |
table => 'table1', |
|
1558 |
primary_key => ['key1', 'key2'], |
|
1559 |
where => {}, |
|
1560 |
param => {key1 => 1, key2 => 2}, |
|
1561 |
); |
|
1562 |
}; |
|
1563 |
like($@, qr/must be/); |
|
1564 | ||
1565 |
eval { |
|
1566 |
$result = $dbi->update_at( |
|
1567 |
table => 'table1', |
|
1568 |
primary_key => ['key1', 'key2'], |
|
1569 |
where => {}, |
|
1570 |
param => {key1 => 1, key2 => 2}, |
|
1571 |
); |
|
1572 |
}; |
|
1573 |
like($@, qr/must be/); |
|
1574 | ||
1575 |
eval { |
|
1576 |
$result = $dbi->delete_at( |
|
1577 |
table => 'table1', |
|
1578 |
primary_key => ['key1', 'key2'], |
|
1579 |
where => {}, |
|
1580 |
param => {key1 => 1, key2 => 2}, |
|
1581 |
); |
|
1582 |
}; |
|
1583 |
like($@, qr/must be/); |
|
add experimental update_at()...
|
1584 | |
add experimental DBIx::Custo...
|
1585 |
test 'columns'; |
1586 |
use MyDBI1; |
|
1587 |
$dbi = MyDBI1->connect($NEW_ARGS->{0}); |
|
1588 |
$model = $dbi->model('book'); |
|
add experimental DBIx::Custo...
|
1589 | |
1590 | ||
1591 |
test 'model delete_at'; |
|
1592 |
{ |
|
1593 |
package MyDBI6; |
|
1594 |
|
|
1595 |
use base 'DBIx::Custom'; |
|
1596 |
|
|
1597 |
sub connect { |
|
1598 |
my $self = shift->SUPER::connect(@_); |
|
1599 |
|
|
1600 |
$self->include_model('MyModel5'); |
|
1601 |
|
|
1602 |
return $self; |
|
1603 |
} |
|
1604 |
} |
|
1605 |
$dbi = MyDBI6->connect($NEW_ARGS->{0}); |
|
1606 |
$dbi->execute($CREATE_TABLE->{1}); |
|
1607 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
1608 |
$dbi->model('table1')->delete_at(where => [1, 2]); |
|
1609 |
is_deeply($dbi->select(table => 'table1')->fetch_hash_all, []); |
|
add experimental DBIx::Custo...
|
1610 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
1611 |
$dbi->model('table1_1')->delete_at(where => [1, 2]); |
|
1612 |
is_deeply($dbi->select(table => 'table1')->fetch_hash_all, []); |
|
1613 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
1614 |
$dbi->model('table1_3')->delete_at(where => [1, 2]); |
|
1615 |
is_deeply($dbi->select(table => 'table1')->fetch_hash_all, []); |
|
add experimental DBIx::Custo...
|
1616 | |
- added experimental DBIx::C...
|
1617 |
test 'model insert_at'; |
1618 |
$dbi = MyDBI6->connect($NEW_ARGS->{0}); |
|
1619 |
$dbi->execute($CREATE_TABLE->{1}); |
|
1620 |
$dbi->model('table1')->insert_at( |
|
1621 |
where => [1, 2], |
|
1622 |
param => {key3 => 3} |
|
1623 |
); |
|
1624 |
$result = $dbi->model('table1')->select; |
|
1625 |
$row = $result->fetch_hash_first; |
|
1626 |
is($row->{key1}, 1); |
|
1627 |
is($row->{key2}, 2); |
|
1628 |
is($row->{key3}, 3); |
|
add experimental DBIx::Custo...
|
1629 | |
- added experimental DBIx::C...
|
1630 |
test 'model update_at'; |
add experimental DBIx::Custo...
|
1631 |
$dbi = MyDBI6->connect($NEW_ARGS->{0}); |
1632 |
$dbi->execute($CREATE_TABLE->{1}); |
|
1633 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
1634 |
$dbi->model('table1')->update_at( |
|
1635 |
where => [1, 2], |
|
1636 |
param => {key3 => 4} |
|
1637 |
); |
|
1638 |
$result = $dbi->model('table1')->select; |
|
1639 |
$row = $result->fetch_hash_first; |
|
1640 |
is($row->{key1}, 1); |
|
1641 |
is($row->{key2}, 2); |
|
1642 |
is($row->{key3}, 4); |
|
1643 | ||
- added experimental DBIx::C...
|
1644 |
test 'model select_at'; |
add experimental DBIx::Custo...
|
1645 |
$dbi = MyDBI6->connect($NEW_ARGS->{0}); |
1646 |
$dbi->execute($CREATE_TABLE->{1}); |
|
1647 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
1648 |
$result = $dbi->model('table1')->select_at(where => [1, 2]); |
|
1649 |
$row = $result->fetch_hash_first; |
|
1650 |
is($row->{key1}, 1); |
|
1651 |
is($row->{key2}, 2); |
|
1652 |
is($row->{key3}, 3); |
|
DBIx::Custom::Model select()...
|
1653 | |
1654 | ||
remove experimental DBIx::Cu...
|
1655 |
test 'column_clause'; |
DBIx::Custom::Model select()...
|
1656 |
{ |
1657 |
package MyDBI7; |
|
1658 |
|
|
1659 |
use base 'DBIx::Custom'; |
|
1660 |
|
|
1661 |
sub connect { |
|
1662 |
my $self = shift->SUPER::connect(@_); |
|
1663 |
|
|
1664 |
$self->include_model('MyModel6'); |
|
1665 |
|
|
add experimental DBIx::Custo...
|
1666 |
|
DBIx::Custom::Model select()...
|
1667 |
return $self; |
1668 |
} |
|
1669 |
} |
|
1670 |
$dbi = MyDBI7->connect($NEW_ARGS->{0}); |
|
1671 |
$dbi->execute($CREATE_TABLE->{0}); |
|
1672 |
$dbi->execute($CREATE_TABLE->{2}); |
|
add experimental DBIx::Custo...
|
1673 |
$dbi->setup_model; |
1674 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1675 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3}); |
|
1676 |
$model = $dbi->model('table1'); |
|
1677 |
$result = $model->select(column => $model->column_clause, where => {'table1.key1' => 1}); |
|
1678 |
is_deeply($result->fetch_hash_first, {key1 => 1, key2 => 2}); |
|
1679 |
$result = $model->select(column => $model->column_clause(remove => ['key1']), where => {'table1.key1' => 1}); |
|
1680 |
is_deeply($result->fetch_hash_first, {key2 => 2}); |
|
- added experimental DBIx::C...
|
1681 |
$result = $model->select(column => $model->column_clause(add => ['table2.key3']), where => {'table1.key1' => 1}); |
add experimental DBIx::Custo...
|
1682 |
is_deeply($result->fetch_hash_first, {key1 => 1, key2 => 2, key3 => 3}); |
1683 | ||
added experimental update_pa...
|
1684 |
test 'update_param'; |
1685 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1686 |
$dbi->execute($CREATE_TABLE->{1}); |
|
1687 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
1688 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
1689 | ||
1690 |
$param = {key2 => 11}; |
|
1691 |
$update_param = $dbi->update_param($param); |
|
1692 |
$sql = <<"EOS"; |
|
1693 |
update {table table1} $update_param |
|
1694 |
where key1 = 1 |
|
1695 |
EOS |
|
1696 |
$dbi->execute($sql, param => $param); |
|
1697 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
1698 |
$rows = $result->fetch_hash_all; |
|
1699 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
1700 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
1701 |
"basic"); |
|
1702 | ||
1703 | ||
1704 |
eval { $dbi->update_param({";" => 1}) }; |
|
1705 |
like($@, qr/not safety/); |
|
1706 | ||
1707 | ||
1708 |
test 'insert_param'; |
|
1709 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1710 |
$dbi->execute($CREATE_TABLE->{1}); |
|
1711 |
$param = {key1 => 1}; |
|
1712 |
$insert_param = $dbi->insert_param($param); |
|
1713 |
$sql = <<"EOS"; |
|
1714 |
insert into {table table1} $insert_param |
|
1715 |
EOS |
|
1716 | ||
1717 |
$dbi->execute($sql, param => $param); |
|
1718 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1); |
|
add experimental DBIx::Custo...
|
1719 | |
added experimental update_pa...
|
1720 |
eval { $dbi->insert_param({";" => 1}) }; |
1721 |
like($@, qr/not safety/); |
|
cleanup
|
1722 | |
1723 | ||
- added experimental DBIx::C...
|
1724 |
test 'join'; |
cleanup
|
1725 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
1726 |
$dbi->execute($CREATE_TABLE->{0}); |
|
1727 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1728 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
1729 |
$dbi->execute($CREATE_TABLE->{2}); |
|
1730 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
- added experimental DBIx::C...
|
1731 |
$dbi->execute($CREATE_TABLE->{4}); |
1732 |
$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4}); |
|
cleanup
|
1733 |
$rows = $dbi->select( |
1734 |
table => 'table1', |
|
1735 |
column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3', |
|
1736 |
where => {'table1.key2' => 2}, |
|
- added experimental DBIx::C...
|
1737 |
join => ['left outer join table2 on table1.key1 = table2.key1'] |
cleanup
|
1738 |
)->fetch_hash_all; |
1739 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]); |
|
1740 | ||
- added experimental DBIx::C...
|
1741 |
$rows = $dbi->select( |
1742 |
table => 'table1', |
|
1743 |
where => {'key1' => 1}, |
|
1744 |
join => ['left outer join table2 on table1.key1 = table2.key1'] |
|
1745 |
)->fetch_hash_all; |
|
1746 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
1747 | ||
cleanup
|
1748 |
eval { |
1749 |
$rows = $dbi->select( |
|
1750 |
table => 'table1', |
|
1751 |
column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3', |
|
1752 |
where => {'table1.key2' => 2}, |
|
- added experimental DBIx::C...
|
1753 |
join => {'table1.key1' => 'table2.key1'} |
cleanup
|
1754 |
); |
1755 |
}; |
|
1756 |
like ($@, qr/array/); |
|
- added experimental DBIx::C...
|
1757 | |
1758 |
$rows = $dbi->select( |
|
1759 |
table => 'table1', |
|
1760 |
where => {'key1' => 1}, |
|
1761 |
join => ['left outer join table2 on table1.key1 = table2.key1', |
|
1762 |
'left outer join table3 on table2.key3 = table3.key3'] |
|
1763 |
)->fetch_hash_all; |
|
1764 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
1765 | ||
1766 |
$rows = $dbi->select( |
|
1767 |
column => 'table3.key4 as table3__key4', |
|
1768 |
table => 'table1', |
|
1769 |
where => {'table1.key1' => 1}, |
|
1770 |
join => ['left outer join table2 on table1.key1 = table2.key1', |
|
1771 |
'left outer join table3 on table2.key3 = table3.key3'] |
|
1772 |
)->fetch_hash_all; |
|
1773 |
is_deeply($rows, [{table3__key4 => 4}]); |
|
1774 | ||
1775 |
$rows = $dbi->select( |
|
1776 |
column => 'table1.key1 as table1__key1', |
|
1777 |
table => 'table1', |
|
1778 |
where => {'table3.key4' => 4}, |
|
1779 |
join => ['left outer join table2 on table1.key1 = table2.key1', |
|
1780 |
'left outer join table3 on table2.key3 = table3.key3'] |
|
1781 |
)->fetch_hash_all; |
|
1782 |
is_deeply($rows, [{table1__key1 => 1}]); |
|
- added experimental DBIx::C...
|
1783 | |
1784 | ||
added select() all_column op...
|
1785 |
test 'model join and column attribute and all_column option'; |
- added experimental DBIx::C...
|
1786 |
{ |
1787 |
package MyDBI8; |
|
1788 |
|
|
1789 |
use base 'DBIx::Custom'; |
|
1790 |
|
|
1791 |
sub connect { |
|
1792 |
my $self = shift->SUPER::connect(@_); |
|
1793 |
|
|
1794 |
$self->include_model('MyModel7'); |
|
1795 |
|
|
1796 |
return $self; |
|
1797 |
} |
|
1798 |
} |
|
1799 |
$dbi = MyDBI8->connect($NEW_ARGS->{0}); |
|
1800 |
$dbi->execute($CREATE_TABLE->{0}); |
|
1801 |
$dbi->execute($CREATE_TABLE->{2}); |
|
1802 |
$dbi->setup_model; |
|
1803 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1804 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3}); |
|
1805 |
$model = $dbi->model('table1'); |
|
- added EXPERIMENTAL DBIx::C...
|
1806 |
$result = $model->select_at( |
1807 |
all_column => ['table1', 'table2'], |
|
1808 |
where => 1 |
|
1809 |
); |
|
- added experimental DBIx::C...
|
1810 |
is_deeply($result->fetch_hash_first, |
1811 |
{key1 => 1, key2 => 2, table2__key1 => 1, table2__key3 => 3}); |
|
added select() all_column op...
|
1812 |
$result = $model->select(all_column => 1); |
1813 |
is_deeply($result->fetch_hash_first, |
|
1814 |
{key1 => 1, key2 => 2, table2__key1 => 1, table2__key3 => 3}); |
|
1815 | ||
- added EXPERIMENTAL DBIx::C...
|
1816 |
test 'view'; |
1817 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1818 |
$dbi->execute($CREATE_TABLE->{0}); |
|
1819 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => ' a '}); |
|
1820 |
$dbi->view('table1_trim' => 'select key1, trim(key2) as key2 from table1'); |
|
1821 |
$result = $dbi->select( |
|
1822 |
table => 'table1', |
|
1823 |
column => ['table1_trim.key2 as table1_trim__key2'], |
|
1824 |
join => ["left outer join " . $dbi->view('table1_trim') . " on table1.key1 = table1_trim.key1"] |
|
1825 |
); |
|
1826 |
is($result->fetch_hash_first->{'table1_trim__key2'}, 'a'); |
|
1827 | ||
1828 |
$result = $dbi->select(table => 'table1_trim'); |
|
1829 |
is_deeply($result->fetch_hash_first, {key1 => 1, key2 => 'a'}); |
|
1830 | ||
1831 |
{ |
|
1832 |
package MyDBI9; |
|
1833 |
|
|
1834 |
use base 'DBIx::Custom'; |
|
1835 |
|
|
1836 |
sub connect { |
|
1837 |
my $self = shift->SUPER::connect(@_); |
|
1838 |
|
|
1839 |
$self->include_model('MyModel8')->setup_model; |
|
1840 |
|
|
1841 |
return $self; |
|
1842 |
} |
|
1843 |
} |
|
1844 |
$dbi = MyDBI9->connect($NEW_ARGS->{0}); |
|
1845 |
$dbi->execute($CREATE_TABLE->{0}); |
|
1846 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => ' a '}); |
|
1847 |
$result = $dbi->model('table1_trim')->select; |
|
1848 |
is_deeply($result->fetch_hash_first, {key1 => 1, key2 => 'a'}); |