cleanup test
|
1 |
use Test::More; |
2 |
use strict; |
|
3 |
use warnings; |
|
4 |
use utf8; |
|
5 |
use Encode qw/encode_utf8 decode_utf8/; |
|
test cleanup
|
6 |
use FindBin; |
7 |
use lib "$FindBin::Bin/basic"; |
|
cleanup test
|
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 | ||
test cleanup
|
19 |
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/}; |
cleanup test
|
20 |
sub test { print "# $_[0]\n" } |
21 | ||
cleanup test
|
22 |
# Constant |
23 |
my %memory = (dsn => 'dbi:SQLite:dbname=:memory:'); |
|
cleanup test
|
24 |
my $create_table1 = 'create table table1 (key1 char(255), key2 char(255));'; |
25 |
my $create_table1_2 = 'create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));'; |
|
cleanup test
|
26 | |
cleanup test
|
27 |
# Variables |
cleanup test
|
28 |
my $builder; |
29 |
my $datas; |
|
cleanup test
|
30 |
my $dbi; |
31 |
my $sth; |
|
32 |
my $source; |
|
33 |
my @sources; |
|
cleanup test
|
34 |
my $select_source; |
35 |
my $insert_source; |
|
36 |
my $update_source; |
|
cleanup test
|
37 |
my $param; |
38 |
my $params; |
|
39 |
my $sql; |
|
40 |
my $result; |
|
41 |
my $row; |
|
42 |
my @rows; |
|
43 |
my $rows; |
|
44 |
my $query; |
|
45 |
my @queries; |
|
46 |
my $select_query; |
|
47 |
my $insert_query; |
|
48 |
my $update_query; |
|
49 |
my $ret_val; |
|
50 |
my $infos; |
|
51 |
my $model; |
|
52 |
my $model2; |
|
53 |
my $where; |
|
54 |
my $update_param; |
|
55 |
my $insert_param; |
|
cleanup test
|
56 |
my $join; |
cleanup test
|
57 | |
58 |
# Prepare table |
|
cleanup test
|
59 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
60 | |
61 | ||
62 |
test 'insert'; |
|
cleanup test
|
63 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
64 |
$dbi->execute($create_table1); |
cleanup test
|
65 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
66 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
test cleanup
|
67 |
$result = $dbi->execute('select * from table1;'); |
cleanup test
|
68 |
$rows = $result->all; |
69 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic"); |
|
70 | ||
71 |
$dbi->execute('delete from table1'); |
|
72 |
$dbi->register_filter( |
|
73 |
twice => sub { $_[0] * 2 }, |
|
74 |
three_times => sub { $_[0] * 3 } |
|
75 |
); |
|
76 |
$dbi->default_bind_filter('twice'); |
|
77 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => 'three_times'}); |
|
test cleanup
|
78 |
$result = $dbi->execute('select * from table1;'); |
cleanup test
|
79 |
$rows = $result->all; |
80 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter"); |
|
81 |
$dbi->default_bind_filter(undef); |
|
82 | ||
cleanup test
|
83 |
$dbi->execute('drop table table1'); |
cleanup test
|
84 |
$dbi->execute($create_table1); |
cleanup test
|
85 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, append => ' '); |
86 |
$rows = $dbi->select(table => 'table1')->all; |
|
87 |
is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append'); |
|
88 | ||
89 |
eval{$dbi->insert(table => 'table1', noexist => 1)}; |
|
90 |
like($@, qr/noexist/, "invalid"); |
|
91 | ||
92 |
eval{$dbi->insert(table => 'table', param => {';' => 1})}; |
|
93 |
like($@, qr/safety/); |
|
94 | ||
cleanup test
|
95 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
96 |
$dbi->quote('"'); |
97 |
$dbi->execute('create table "table" ("select")'); |
|
98 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
|
99 |
$dbi->insert(table => 'table', param => {select => 1}); |
|
100 |
$result = $dbi->execute('select * from "table"'); |
|
101 |
$rows = $result->all; |
|
102 |
is_deeply($rows, [{select => 2}], "reserved word"); |
|
103 | ||
cleanup test
|
104 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
105 |
$dbi->execute($create_table1); |
cleanup test
|
106 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
107 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
test cleanup
|
108 |
$result = $dbi->execute('select * from table1;'); |
cleanup test
|
109 |
$rows = $result->all; |
110 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic"); |
|
111 | ||
cleanup test
|
112 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
113 |
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))"); |
114 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
115 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace'); |
|
test cleanup
|
116 |
$result = $dbi->execute('select * from table1;'); |
cleanup test
|
117 |
$rows = $result->all; |
118 |
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic"); |
|
119 | ||
cleanup test
|
120 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
121 |
$dbi->execute($create_table1); |
cleanup test
|
122 |
$dbi->insert(table => 'table1', param => {key1 => \"'1'", key2 => 2}); |
123 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
test cleanup
|
124 |
$result = $dbi->execute('select * from table1;'); |
cleanup test
|
125 |
$rows = $result->all; |
126 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic"); |
|
127 | ||
128 |
test 'update'; |
|
cleanup test
|
129 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
130 |
$dbi->execute($create_table1_2); |
cleanup test
|
131 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
132 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
133 |
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}); |
|
test cleanup
|
134 |
$result = $dbi->execute('select * from table1;'); |
cleanup test
|
135 |
$rows = $result->all; |
136 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
137 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
138 |
"basic"); |
|
139 |
|
|
140 |
$dbi->execute("delete from table1"); |
|
141 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
142 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
143 |
$dbi->update(table => 'table1', param => {key2 => 12}, where => {key2 => 2, key3 => 3}); |
|
test cleanup
|
144 |
$result = $dbi->execute('select * from table1;'); |
cleanup test
|
145 |
$rows = $result->all; |
146 |
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5}, |
|
147 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
148 |
"update key same as search key"); |
|
149 | ||
150 |
$dbi->update(table => 'table1', param => {key2 => [12]}, where => {key2 => 2, key3 => 3}); |
|
test cleanup
|
151 |
$result = $dbi->execute('select * from table1;'); |
cleanup test
|
152 |
$rows = $result->all; |
153 |
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5}, |
|
154 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
155 |
"update key same as search key : param is array ref"); |
|
156 | ||
157 |
$dbi->execute("delete from table1"); |
|
158 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
159 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
160 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
161 |
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}, |
|
162 |
filter => {key2 => sub { $_[0] * 2 }}); |
|
test cleanup
|
163 |
$result = $dbi->execute('select * from table1;'); |
cleanup test
|
164 |
$rows = $result->all; |
165 |
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5}, |
|
166 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
167 |
"filter"); |
|
168 | ||
169 |
$result = $dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}, append => ' '); |
|
170 | ||
171 |
eval{$dbi->update(table => 'table1', where => {key1 => 1}, noexist => 1)}; |
|
172 |
like($@, qr/noexist/, "invalid"); |
|
173 | ||
174 |
eval{$dbi->update(table => 'table1')}; |
|
175 |
like($@, qr/where/, "not contain where"); |
|
176 | ||
cleanup test
|
177 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
178 |
$dbi->execute($create_table1); |
cleanup test
|
179 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
180 |
$where = $dbi->where; |
|
181 |
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']); |
|
182 |
$where->param({key1 => 1, key2 => 2}); |
|
183 |
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where); |
|
184 |
$result = $dbi->select(table => 'table1'); |
|
185 |
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where'); |
|
186 | ||
cleanup test
|
187 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
188 |
$dbi->execute($create_table1); |
cleanup test
|
189 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
190 |
$dbi->update( |
|
191 |
table => 'table1', |
|
192 |
param => {key1 => 3}, |
|
193 |
where => [ |
|
194 |
['and', 'key1 = :key1', 'key2 = :key2'], |
|
195 |
{key1 => 1, key2 => 2} |
|
196 |
] |
|
197 |
); |
|
198 |
$result = $dbi->select(table => 'table1'); |
|
199 |
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where'); |
|
200 | ||
cleanup test
|
201 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
202 |
$dbi->execute($create_table1); |
cleanup test
|
203 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
204 |
$where = $dbi->where; |
|
205 |
$where->clause(['and', 'key2 = :key2']); |
|
206 |
$where->param({key2 => 2}); |
|
207 |
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where); |
|
208 |
$result = $dbi->select(table => 'table1'); |
|
209 |
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where'); |
|
210 | ||
211 |
eval{$dbi->update(table => 'table1', param => {';' => 1})}; |
|
212 |
like($@, qr/safety/); |
|
213 | ||
214 |
eval{$dbi->update(table => 'table1', param => {'key1' => 1}, where => {';' => 1})}; |
|
215 |
like($@, qr/safety/); |
|
216 | ||
cleanup test
|
217 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
218 |
$dbi->quote('"'); |
219 |
$dbi->execute('create table "table" ("select", "update")'); |
|
220 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
|
221 |
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}}); |
|
222 |
$dbi->insert(table => 'table', param => {select => 1}); |
|
223 |
$dbi->update(table => 'table', where => {select => 1}, param => {update => 2}); |
|
224 |
$result = $dbi->execute('select * from "table"'); |
|
225 |
$rows = $result->all; |
|
226 |
is_deeply($rows, [{select => 2, update => 6}], "reserved word"); |
|
227 | ||
228 |
eval {$dbi->update_all(table => 'table', param => {';' => 2}) }; |
|
229 |
like($@, qr/safety/); |
|
230 | ||
cleanup test
|
231 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
232 |
$dbi->reserved_word_quote('"'); |
233 |
$dbi->execute('create table "table" ("select", "update")'); |
|
234 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
|
235 |
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}}); |
|
236 |
$dbi->insert(table => 'table', param => {select => 1}); |
|
237 |
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2}); |
|
238 |
$result = $dbi->execute('select * from "table"'); |
|
239 |
$rows = $result->all; |
|
240 |
is_deeply($rows, [{select => 2, update => 6}], "reserved word"); |
|
241 | ||
cleanup test
|
242 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
243 |
$dbi->execute($create_table1_2); |
cleanup test
|
244 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
245 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
246 |
$dbi->update({key2 => 11}, table => 'table1', where => {key1 => 1}); |
|
test cleanup
|
247 |
$result = $dbi->execute('select * from table1;'); |
cleanup test
|
248 |
$rows = $result->all; |
249 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
250 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
251 |
"basic"); |
|
252 | ||
cleanup test
|
253 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
254 |
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))"); |
255 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
256 |
$dbi->update(table => 'table1', param => {key2 => 4}, |
|
257 |
where => {key1 => 1}, prefix => 'or replace'); |
|
test cleanup
|
258 |
$result = $dbi->execute('select * from table1;'); |
cleanup test
|
259 |
$rows = $result->all; |
260 |
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic"); |
|
261 | ||
cleanup test
|
262 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
263 |
$dbi->execute($create_table1_2); |
cleanup test
|
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->update(table => 'table1', param => {key2 => \"'11'"}, where => {key1 => 1}); |
|
test cleanup
|
267 |
$result = $dbi->execute('select * from table1;'); |
cleanup test
|
268 |
$rows = $result->all; |
269 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
270 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
271 |
"basic"); |
|
272 | ||
273 |
test 'update_all'; |
|
cleanup test
|
274 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
275 |
$dbi->execute($create_table1_2); |
cleanup test
|
276 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
277 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
278 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
279 |
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'}); |
|
test cleanup
|
280 |
$result = $dbi->execute('select * from table1;'); |
cleanup test
|
281 |
$rows = $result->all; |
282 |
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5}, |
|
283 |
{key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}], |
|
284 |
"filter"); |
|
285 | ||
286 | ||
287 |
test 'delete'; |
|
cleanup test
|
288 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
289 |
$dbi->execute($create_table1); |
cleanup test
|
290 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
291 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
292 |
$dbi->delete(table => 'table1', where => {key1 => 1}); |
|
test cleanup
|
293 |
$result = $dbi->execute('select * from table1;'); |
cleanup test
|
294 |
$rows = $result->all; |
295 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "basic"); |
|
296 | ||
297 |
$dbi->execute("delete from table1;"); |
|
298 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
299 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
300 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
301 |
$dbi->delete(table => 'table1', where => {key2 => 1}, filter => {key2 => 'twice'}); |
|
test cleanup
|
302 |
$result = $dbi->execute('select * from table1;'); |
cleanup test
|
303 |
$rows = $result->all; |
304 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter"); |
|
305 | ||
306 |
$dbi->delete(table => 'table1', where => {key1 => 1}, append => ' '); |
|
307 | ||
308 |
$dbi->delete_all(table => 'table1'); |
|
309 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
310 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
311 |
$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2}); |
|
312 |
$rows = $dbi->select(table => 'table1')->all; |
|
313 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "delete multi key"); |
|
314 | ||
315 |
eval{$dbi->delete(table => 'table1', where => {key1 => 1}, noexist => 1)}; |
|
316 |
like($@, qr/noexist/, "invalid"); |
|
317 | ||
cleanup test
|
318 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
319 |
$dbi->execute($create_table1); |
cleanup test
|
320 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
321 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
322 |
$where = $dbi->where; |
|
323 |
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']); |
|
324 |
$where->param({ke1 => 1, key2 => 2}); |
|
325 |
$dbi->delete(table => 'table1', where => $where); |
|
326 |
$result = $dbi->select(table => 'table1'); |
|
327 |
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where'); |
|
328 | ||
cleanup test
|
329 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
330 |
$dbi->execute($create_table1); |
cleanup test
|
331 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
332 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
333 |
$dbi->delete( |
|
334 |
table => 'table1', |
|
335 |
where => [ |
|
336 |
['and', 'key1 = :key1', 'key2 = :key2'], |
|
337 |
{ke1 => 1, key2 => 2} |
|
338 |
] |
|
339 |
); |
|
340 |
$result = $dbi->select(table => 'table1'); |
|
341 |
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where'); |
|
342 | ||
cleanup test
|
343 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
344 |
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))"); |
345 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
346 |
$dbi->delete(table => 'table1', where => {key1 => 1}, prefix => ' '); |
|
test cleanup
|
347 |
$result = $dbi->execute('select * from table1;'); |
cleanup test
|
348 |
$rows = $result->all; |
349 |
is_deeply($rows, [], "basic"); |
|
350 | ||
351 |
test 'delete error'; |
|
cleanup test
|
352 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
353 |
$dbi->execute($create_table1); |
cleanup test
|
354 |
eval{$dbi->delete(table => 'table1')}; |
355 |
like($@, qr/"where" must be specified/, |
|
356 |
"where key-value pairs not specified"); |
|
357 | ||
358 |
eval{$dbi->delete(table => 'table1', where => {';' => 1})}; |
|
359 |
like($@, qr/safety/); |
|
360 | ||
cleanup test
|
361 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
362 |
$dbi->quote('"'); |
363 |
$dbi->execute('create table "table" ("select", "update")'); |
|
364 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
|
365 |
$dbi->insert(table => 'table', param => {select => 1}); |
|
366 |
$dbi->delete(table => 'table', where => {select => 1}); |
|
367 |
$result = $dbi->execute('select * from "table"'); |
|
368 |
$rows = $result->all; |
|
369 |
is_deeply($rows, [], "reserved word"); |
|
370 | ||
371 |
test 'delete_all'; |
|
cleanup test
|
372 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
373 |
$dbi->execute($create_table1); |
cleanup test
|
374 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
375 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
376 |
$dbi->delete_all(table => 'table1'); |
|
test cleanup
|
377 |
$result = $dbi->execute('select * from table1;'); |
cleanup test
|
378 |
$rows = $result->all; |
379 |
is_deeply($rows, [], "basic"); |
|
380 | ||
381 | ||
382 |
test 'select'; |
|
cleanup test
|
383 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
384 |
$dbi->execute($create_table1); |
cleanup test
|
385 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
386 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
387 |
$rows = $dbi->select(table => 'table1')->all; |
|
388 |
is_deeply($rows, [{key1 => 1, key2 => 2}, |
|
389 |
{key1 => 3, key2 => 4}], "table"); |
|
390 | ||
391 |
$rows = $dbi->select(table => 'table1', column => ['key1'])->all; |
|
392 |
is_deeply($rows, [{key1 => 1}, {key1 => 3}], "table and columns and where key"); |
|
393 | ||
394 |
$rows = $dbi->select(table => 'table1', where => {key1 => 1})->all; |
|
395 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "table and columns and where key"); |
|
396 | ||
397 |
$rows = $dbi->select(table => 'table1', column => ['key1'], where => {key1 => 3})->all; |
|
398 |
is_deeply($rows, [{key1 => 3}], "table and columns and where key"); |
|
399 | ||
400 |
$rows = $dbi->select(table => 'table1', append => "order by key1 desc limit 1")->all; |
|
401 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "append statement"); |
|
402 | ||
403 |
$dbi->register_filter(decrement => sub { $_[0] - 1 }); |
|
404 |
$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'}) |
|
405 |
->all; |
|
406 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "filter"); |
|
407 | ||
cleanup test
|
408 |
$dbi->execute('create table table2 (key1 char(255), key3 char(255));'); |
cleanup test
|
409 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
410 |
$rows = $dbi->select( |
|
411 |
table => [qw/table1 table2/], |
|
412 |
column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3', |
|
413 |
where => {'table1.key2' => 2}, |
|
414 |
relation => {'table1.key1' => 'table2.key1'} |
|
415 |
)->all; |
|
416 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : exists where"); |
|
417 | ||
418 |
$rows = $dbi->select( |
|
419 |
table => [qw/table1 table2/], |
|
420 |
column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'], |
|
421 |
relation => {'table1.key1' => 'table2.key1'} |
|
422 |
)->all; |
|
423 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : no exists where"); |
|
424 | ||
425 |
eval{$dbi->select(table => 'table1', noexist => 1)}; |
|
426 |
like($@, qr/noexist/, "invalid"); |
|
427 | ||
cleanup test
|
428 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
429 |
$dbi->quote('"'); |
430 |
$dbi->execute('create table "table" ("select", "update")'); |
|
431 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
|
432 |
$dbi->insert(table => 'table', param => {select => 1, update => 2}); |
|
433 |
$result = $dbi->select(table => 'table', where => {select => 1}); |
|
434 |
$rows = $result->all; |
|
435 |
is_deeply($rows, [{select => 2, update => 2}], "reserved word"); |
|
436 | ||
437 |
test 'fetch filter'; |
|
cleanup test
|
438 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
439 |
$dbi->register_filter( |
440 |
twice => sub { $_[0] * 2 }, |
|
441 |
three_times => sub { $_[0] * 3 } |
|
442 |
); |
|
443 |
$dbi->default_fetch_filter('twice'); |
|
cleanup test
|
444 |
$dbi->execute($create_table1); |
cleanup test
|
445 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
446 |
$result = $dbi->select(table => 'table1'); |
|
447 |
$result->filter({key1 => 'three_times'}); |
|
448 |
$row = $result->one; |
|
449 |
is_deeply($row, {key1 => 3, key2 => 4}, "default_fetch_filter and filter"); |
|
450 | ||
451 |
test 'filters'; |
|
452 |
$dbi = DBIx::Custom->new; |
|
453 | ||
454 |
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')), |
|
455 |
'あ', "decode_utf8"); |
|
456 | ||
457 |
is($dbi->filters->{encode_utf8}->('あ'), |
|
458 |
encode_utf8('あ'), "encode_utf8"); |
|
459 | ||
460 |
test 'transaction'; |
|
cleanup test
|
461 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
462 |
$dbi->execute($create_table1); |
cleanup test
|
463 |
$dbi->dbh->begin_work; |
464 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
465 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
466 |
$dbi->dbh->commit; |
|
467 |
$result = $dbi->select(table => 'table1'); |
|
468 |
is_deeply(scalar $result->all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}], |
|
469 |
"commit"); |
|
470 | ||
cleanup test
|
471 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
472 |
$dbi->execute($create_table1); |
cleanup test
|
473 |
$dbi->dbh->begin_work(0); |
474 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
475 |
$dbi->dbh->rollback; |
|
476 | ||
477 |
$result = $dbi->select(table => 'table1'); |
|
478 |
ok(! $result->fetch_first, "rollback"); |
|
479 | ||
480 |
test 'cache'; |
|
cleanup test
|
481 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
482 |
$dbi->cache(1); |
cleanup test
|
483 |
$dbi->execute($create_table1); |
cleanup test
|
484 |
$source = 'select * from table1 where key1 = :key1 and key2 = :key2;'; |
485 |
$dbi->execute($source, {}, query => 1); |
|
486 |
is_deeply($dbi->{_cached}->{$source}, |
|
487 |
{sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2'], tables => []}, "cache"); |
|
488 | ||
cleanup test
|
489 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
490 |
$dbi->execute($create_table1); |
cleanup test
|
491 |
$dbi->{_cached} = {}; |
492 |
$dbi->cache(0); |
|
493 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
494 |
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache'); |
|
495 | ||
496 |
test 'execute'; |
|
cleanup test
|
497 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
498 |
$dbi->execute($create_table1); |
cleanup test
|
499 |
{ |
500 |
local $Carp::Verbose = 0; |
|
501 |
eval{$dbi->execute('select * frm table1')}; |
|
502 |
like($@, qr/\Qselect * frm table1;/, "fail prepare"); |
|
503 |
like($@, qr/\.t /, "fail : not verbose"); |
|
504 |
} |
|
505 |
{ |
|
506 |
local $Carp::Verbose = 1; |
|
507 |
eval{$dbi->execute('select * frm table1')}; |
|
508 |
like($@, qr/Custom.*\.t /s, "fail : verbose"); |
|
509 |
} |
|
510 | ||
511 |
eval{$dbi->execute('select * from table1', no_exists => 1)}; |
|
512 |
like($@, qr/wrong/, "invald SQL"); |
|
513 | ||
514 |
$query = $dbi->execute('select * from table1 where key1 = :key1', {}, query => 1); |
|
515 |
$dbi->dbh->disconnect; |
|
516 |
eval{$dbi->execute($query, param => {key1 => {a => 1}})}; |
|
517 |
ok($@, "execute fail"); |
|
518 | ||
519 |
{ |
|
520 |
local $Carp::Verbose = 0; |
|
521 |
eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)}; |
|
522 |
like($@, qr/\Q.t /, "caller spec : not vebose"); |
|
523 |
} |
|
524 |
{ |
|
525 |
local $Carp::Verbose = 1; |
|
526 |
eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)}; |
|
527 |
like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose"); |
|
528 |
} |
|
529 | ||
530 | ||
531 |
test 'transaction'; |
|
cleanup test
|
532 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
533 |
$dbi->execute($create_table1); |
cleanup test
|
534 | |
535 |
$dbi->begin_work; |
|
536 | ||
537 |
eval { |
|
538 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
539 |
die "Error"; |
|
540 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
541 |
}; |
|
542 | ||
543 |
$dbi->rollback if $@; |
|
544 | ||
545 |
$result = $dbi->select(table => 'table1'); |
|
546 |
$rows = $result->all; |
|
547 |
is_deeply($rows, [], "rollback"); |
|
548 | ||
549 |
$dbi->begin_work; |
|
550 | ||
551 |
eval { |
|
552 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
553 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
554 |
}; |
|
555 | ||
556 |
$dbi->commit unless $@; |
|
557 | ||
558 |
$result = $dbi->select(table => 'table1'); |
|
559 |
$rows = $result->all; |
|
560 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "commit"); |
|
561 | ||
562 |
$dbi->dbh->{AutoCommit} = 0; |
|
563 |
eval{ $dbi->begin_work }; |
|
564 |
ok($@, "exception"); |
|
565 |
$dbi->dbh->{AutoCommit} = 1; |
|
566 | ||
567 | ||
568 |
test 'method'; |
|
cleanup test
|
569 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
570 |
$dbi->method( |
571 |
one => sub { 1 } |
|
572 |
); |
|
573 |
$dbi->method( |
|
574 |
two => sub { 2 } |
|
575 |
); |
|
576 |
$dbi->method({ |
|
577 |
twice => sub { |
|
578 |
my $self = shift; |
|
579 |
return $_[0] * 2; |
|
580 |
} |
|
581 |
}); |
|
582 | ||
583 |
is($dbi->one, 1, "first"); |
|
584 |
is($dbi->two, 2, "second"); |
|
585 |
is($dbi->twice(5), 10 , "second"); |
|
586 | ||
587 |
eval {$dbi->XXXXXX}; |
|
588 |
ok($@, "not exists"); |
|
589 | ||
590 |
test 'out filter'; |
|
cleanup test
|
591 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
592 |
$dbi->execute($create_table1); |
cleanup test
|
593 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
594 |
$dbi->register_filter(three_times => sub { $_[0] * 3}); |
|
595 |
$dbi->apply_filter( |
|
596 |
'table1', 'key1' => {out => 'twice', in => 'three_times'}, |
|
597 |
'key2' => {out => 'three_times', in => 'twice'}); |
|
598 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
test cleanup
|
599 |
$result = $dbi->execute('select * from table1;'); |
cleanup test
|
600 |
$row = $result->fetch_hash_first; |
601 |
is_deeply($row, {key1 => 2, key2 => 6}, "insert"); |
|
602 |
$result = $dbi->select(table => 'table1'); |
|
603 |
$row = $result->one; |
|
604 |
is_deeply($row, {key1 => 6, key2 => 12}, "insert"); |
|
605 | ||
cleanup test
|
606 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
607 |
$dbi->execute($create_table1); |
cleanup test
|
608 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
609 |
$dbi->register_filter(three_times => sub { $_[0] * 3}); |
|
610 |
$dbi->apply_filter( |
|
611 |
'table1', 'key1' => {out => 'twice', in => 'three_times'}, |
|
612 |
'key2' => {out => 'three_times', in => 'twice'}); |
|
613 |
$dbi->apply_filter( |
|
614 |
'table1', 'key1' => {out => undef} |
|
615 |
); |
|
616 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
test cleanup
|
617 |
$result = $dbi->execute('select * from table1;'); |
cleanup test
|
618 |
$row = $result->one; |
619 |
is_deeply($row, {key1 => 1, key2 => 6}, "insert"); |
|
620 | ||
cleanup test
|
621 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
622 |
$dbi->execute($create_table1); |
cleanup test
|
623 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
624 |
$dbi->apply_filter( |
|
625 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
|
626 |
); |
|
627 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => undef}); |
|
628 |
$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2}); |
|
test cleanup
|
629 |
$result = $dbi->execute('select * from table1;'); |
cleanup test
|
630 |
$row = $result->one; |
631 |
is_deeply($row, {key1 => 4, key2 => 2}, "update"); |
|
632 | ||
cleanup test
|
633 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
634 |
$dbi->execute($create_table1); |
cleanup test
|
635 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
636 |
$dbi->apply_filter( |
|
637 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
|
638 |
); |
|
639 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1=> undef}); |
|
640 |
$dbi->delete(table => 'table1', where => {key1 => 1}); |
|
test cleanup
|
641 |
$result = $dbi->execute('select * from table1;'); |
cleanup test
|
642 |
$rows = $result->all; |
643 |
is_deeply($rows, [], "delete"); |
|
644 | ||
cleanup test
|
645 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
646 |
$dbi->execute($create_table1); |
cleanup test
|
647 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
648 |
$dbi->apply_filter( |
|
649 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
|
650 |
); |
|
651 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef}); |
|
652 |
$result = $dbi->select(table => 'table1', where => {key1 => 1}); |
|
653 |
$result->filter({'key2' => 'twice'}); |
|
654 |
$rows = $result->all; |
|
655 |
is_deeply($rows, [{key1 => 4, key2 => 4}], "select"); |
|
656 | ||
cleanup test
|
657 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
658 |
$dbi->execute($create_table1); |
cleanup test
|
659 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
660 |
$dbi->apply_filter( |
|
661 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
|
662 |
); |
|
663 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef}); |
|
664 |
$result = $dbi->execute("select * from table1 where key1 = :key1 and key2 = :key2;", |
|
665 |
param => {key1 => 1, key2 => 2}, |
|
666 |
table => ['table1']); |
|
667 |
$rows = $result->all; |
|
668 |
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute"); |
|
669 | ||
cleanup test
|
670 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
671 |
$dbi->execute($create_table1); |
cleanup test
|
672 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
673 |
$dbi->apply_filter( |
|
674 |
'table1', 'key1' => {out => 'twice', in => 'twice'} |
|
675 |
); |
|
676 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef}); |
|
677 |
$result = $dbi->execute("select * from {table table1} where key1 = :key1 and key2 = :key2;", |
|
678 |
param => {key1 => 1, key2 => 2}); |
|
679 |
$rows = $result->all; |
|
680 |
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute table tag"); |
|
681 | ||
cleanup test
|
682 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
683 |
$dbi->execute($create_table1); |
cleanup test
|
684 |
$dbi->execute('create table table2 (key1 char(255), key3 char(255));'); |
cleanup test
|
685 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
686 |
$dbi->register_filter(three_times => sub { $_[0] * 3 }); |
|
687 |
$dbi->apply_filter( |
|
688 |
'table1', 'key2' => {out => 'twice', in => 'twice'} |
|
689 |
); |
|
690 |
$dbi->apply_filter( |
|
691 |
'table2', 'key3' => {out => 'three_times', in => 'three_times'} |
|
692 |
); |
|
693 |
$dbi->insert(table => 'table1', param => {key1 => 5, key2 => 2}, filter => {key2 => undef}); |
|
694 |
$dbi->insert(table => 'table2', param => {key1 => 5, key3 => 6}, filter => {key3 => undef}); |
|
695 |
$result = $dbi->select( |
|
696 |
table => ['table1', 'table2'], |
|
697 |
column => ['key2', 'key3'], |
|
698 |
where => {'table1.key2' => 1, 'table2.key3' => 2}, relation => {'table1.key1' => 'table2.key1'}); |
|
699 | ||
700 |
$result->filter({'key2' => 'twice'}); |
|
701 |
$rows = $result->all; |
|
702 |
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join"); |
|
703 | ||
704 |
$result = $dbi->select( |
|
705 |
table => ['table1', 'table2'], |
|
706 |
column => ['key2', 'key3'], |
|
707 |
where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'}); |
|
708 | ||
709 |
$result->filter({'key2' => 'twice'}); |
|
710 |
$rows = $result->all; |
|
711 |
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join : omit"); |
|
712 | ||
713 |
test 'each_column'; |
|
cleanup test
|
714 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
715 |
$dbi->execute('create table table2 (key1 char(255), key3 char(255));'); |
cleanup test
|
716 |
$dbi->execute('create table table1 (key1 Date, key2 datetime);'); |
cleanup test
|
717 | |
718 |
$infos = []; |
|
719 |
$dbi->each_column(sub { |
|
720 |
my ($self, $table, $column, $cinfo) = @_; |
|
721 |
|
|
722 |
if ($table =~ /^table/) { |
|
723 |
my $info = [$table, $column, $cinfo->{COLUMN_NAME}]; |
|
724 |
push @$infos, $info; |
|
725 |
} |
|
726 |
}); |
|
727 |
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos]; |
|
728 |
is_deeply($infos, |
|
729 |
[ |
|
730 |
['table1', 'key1', 'key1'], |
|
731 |
['table1', 'key2', 'key2'], |
|
732 |
['table2', 'key1', 'key1'], |
|
733 |
['table2', 'key3', 'key3'] |
|
734 |
] |
|
735 |
|
|
736 |
); |
|
737 |
test 'each_table'; |
|
cleanup test
|
738 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
739 |
$dbi->execute('create table table2 (key1 char(255), key3 char(255));'); |
cleanup test
|
740 |
$dbi->execute('create table table1 (key1 Date, key2 datetime);'); |
cleanup test
|
741 | |
742 |
$infos = []; |
|
743 |
$dbi->each_table(sub { |
|
744 |
my ($self, $table, $table_info) = @_; |
|
745 |
|
|
746 |
if ($table =~ /^table/) { |
|
747 |
my $info = [$table, $table_info->{TABLE_NAME}]; |
|
748 |
push @$infos, $info; |
|
749 |
} |
|
750 |
}); |
|
751 |
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos]; |
|
752 |
is_deeply($infos, |
|
753 |
[ |
|
754 |
['table1', 'table1'], |
|
755 |
['table2', 'table2'], |
|
756 |
] |
|
757 |
); |
|
758 | ||
759 |
test 'limit'; |
|
cleanup test
|
760 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
761 |
$dbi->execute($create_table1); |
cleanup test
|
762 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
763 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}); |
|
764 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6}); |
|
765 |
$dbi->register_tag( |
|
766 |
limit => sub { |
|
767 |
my ($count, $offset) = @_; |
|
768 |
|
|
769 |
my $s = ''; |
|
770 |
$s .= "limit $count"; |
|
771 |
$s .= " offset $offset" if defined $offset; |
|
772 |
|
|
773 |
return [$s, []]; |
|
774 |
} |
|
775 |
); |
|
776 |
$rows = $dbi->select( |
|
777 |
table => 'table1', |
|
778 |
where => {key1 => 1}, |
|
779 |
append => "order by key2 {limit 1 0}" |
|
780 |
)->all; |
|
781 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
782 |
$rows = $dbi->select( |
|
783 |
table => 'table1', |
|
784 |
where => {key1 => 1}, |
|
785 |
append => "order by key2 {limit 2 1}" |
|
786 |
)->all; |
|
787 |
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]); |
|
788 |
$rows = $dbi->select( |
|
789 |
table => 'table1', |
|
790 |
where => {key1 => 1}, |
|
791 |
append => "order by key2 {limit 1}" |
|
792 |
)->all; |
|
793 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
794 | ||
795 |
test 'connect super'; |
|
796 |
{ |
|
797 |
package MyDBI; |
|
798 |
|
|
799 |
use base 'DBIx::Custom'; |
|
800 |
sub connect { |
|
801 |
my $self = shift->SUPER::connect(@_); |
|
802 |
|
|
803 |
return $self; |
|
804 |
} |
|
805 |
|
|
806 |
sub new { |
|
807 |
my $self = shift->SUPER::new(@_); |
|
808 |
|
|
809 |
return $self; |
|
810 |
} |
|
811 |
} |
|
812 | ||
cleanup test
|
813 |
$dbi = MyDBI->connect(%memory); |
cleanup test
|
814 |
$dbi->execute($create_table1); |
cleanup test
|
815 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
816 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
817 | ||
cleanup test
|
818 |
$dbi = MyDBI->new(%memory); |
cleanup test
|
819 |
$dbi->connect; |
cleanup test
|
820 |
$dbi->execute($create_table1); |
cleanup test
|
821 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
822 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
823 | ||
824 |
{ |
|
825 |
package MyDBI2; |
|
826 |
|
|
827 |
use base 'DBIx::Custom'; |
|
828 |
sub connect { |
|
829 |
my $self = shift->SUPER::new(@_); |
|
830 |
$self->connect; |
|
831 |
|
|
832 |
return $self; |
|
833 |
} |
|
834 |
} |
|
835 | ||
cleanup test
|
836 |
$dbi = MyDBI->connect(%memory); |
cleanup test
|
837 |
$dbi->execute($create_table1); |
cleanup test
|
838 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
839 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
840 | ||
841 |
test 'end_filter'; |
|
cleanup test
|
842 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
843 |
$dbi->execute($create_table1); |
cleanup test
|
844 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
845 |
$result = $dbi->select(table => 'table1'); |
|
846 |
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 }); |
|
847 |
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 }); |
|
848 |
$row = $result->fetch_first; |
|
849 |
is_deeply($row, [6, 40]); |
|
850 | ||
cleanup test
|
851 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
852 |
$dbi->execute($create_table1); |
cleanup test
|
853 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
854 |
$result = $dbi->select(table => 'table1'); |
|
855 |
$result->filter([qw/key1 key2/] => sub { $_[0] * 2 }); |
|
856 |
$result->end_filter([[qw/key1 key2/] => sub { $_[0] * 3 }]); |
|
857 |
$row = $result->fetch_first; |
|
858 |
is_deeply($row, [6, 12]); |
|
859 | ||
cleanup test
|
860 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
861 |
$dbi->execute($create_table1); |
cleanup test
|
862 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
863 |
$result = $dbi->select(table => 'table1'); |
|
864 |
$result->filter([[qw/key1 key2/] => sub { $_[0] * 2 }]); |
|
865 |
$result->end_filter([qw/key1 key2/] => sub { $_[0] * 3 }); |
|
866 |
$row = $result->fetch_first; |
|
867 |
is_deeply($row, [6, 12]); |
|
868 | ||
869 |
$dbi->register_filter(five_times => sub { $_[0] * 5 }); |
|
870 |
$result = $dbi->select(table => 'table1'); |
|
871 |
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 }); |
|
872 |
$result->end_filter({key1 => sub { $_[0] * 3 }, key2 => 'five_times' }); |
|
873 |
$row = $result->one; |
|
874 |
is_deeply($row, {key1 => 6, key2 => 40}); |
|
875 | ||
876 |
$dbi->register_filter(five_times => sub { $_[0] * 5 }); |
|
877 |
$dbi->apply_filter('table1', |
|
878 |
key1 => {end => sub { $_[0] * 3 } }, |
|
879 |
key2 => {end => 'five_times'} |
|
880 |
); |
|
881 |
$result = $dbi->select(table => 'table1'); |
|
882 |
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 }); |
|
883 |
$row = $result->one; |
|
884 |
is_deeply($row, {key1 => 6, key2 => 40}, 'apply_filter'); |
|
885 | ||
886 |
$dbi->register_filter(five_times => sub { $_[0] * 5 }); |
|
887 |
$dbi->apply_filter('table1', |
|
888 |
key1 => {end => sub { $_[0] * 3 } }, |
|
889 |
key2 => {end => 'five_times'} |
|
890 |
); |
|
891 |
$result = $dbi->select(table => 'table1'); |
|
892 |
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 }); |
|
893 |
$result->filter(key1 => undef); |
|
894 |
$result->end_filter(key1 => undef); |
|
895 |
$row = $result->one; |
|
896 |
is_deeply($row, {key1 => 1, key2 => 40}, 'apply_filter overwrite'); |
|
897 | ||
898 |
test 'remove_end_filter and remove_filter'; |
|
cleanup test
|
899 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
900 |
$dbi->execute($create_table1); |
cleanup test
|
901 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
902 |
$result = $dbi->select(table => 'table1'); |
|
903 |
$row = $result |
|
904 |
->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 }) |
|
905 |
->remove_filter |
|
906 |
->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 }) |
|
907 |
->remove_end_filter |
|
908 |
->fetch_first; |
|
909 |
is_deeply($row, [1, 2]); |
|
910 | ||
911 |
test 'empty where select'; |
|
cleanup test
|
912 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
913 |
$dbi->execute($create_table1); |
cleanup test
|
914 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
915 |
$result = $dbi->select(table => 'table1', where => {}); |
|
916 |
$row = $result->one; |
|
917 |
is_deeply($row, {key1 => 1, key2 => 2}); |
|
918 | ||
919 |
test 'select query option'; |
|
cleanup test
|
920 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
921 |
$dbi->execute($create_table1); |
cleanup test
|
922 |
$query = $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, query => 1); |
923 |
is(ref $query, 'DBIx::Custom::Query'); |
|
924 |
$query = $dbi->update(table => 'table1', where => {key1 => 1}, param => {key2 => 2}, query => 1); |
|
925 |
is(ref $query, 'DBIx::Custom::Query'); |
|
926 |
$query = $dbi->delete(table => 'table1', where => {key1 => 1}, query => 1); |
|
927 |
is(ref $query, 'DBIx::Custom::Query'); |
|
928 |
$query = $dbi->select(table => 'table1', where => {key1 => 1, key2 => 2}, query => 1); |
|
929 |
is(ref $query, 'DBIx::Custom::Query'); |
|
930 | ||
map cleanup
|
931 |
test 'where'; |
cleanup test
|
932 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
933 |
$dbi->execute($create_table1); |
cleanup test
|
934 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
935 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
936 |
$where = $dbi->where->clause(['and', 'key1 = :key1', 'key2 = :key2']); |
|
937 |
is("$where", "where ( key1 = :key1 and key2 = :key2 )", 'no param'); |
|
938 | ||
939 |
$where = $dbi->where |
|
940 |
->clause(['and', 'key1 = :key1', 'key2 = :key2']) |
|
941 |
->param({key1 => 1}); |
|
942 | ||
943 |
$result = $dbi->select( |
|
944 |
table => 'table1', |
|
945 |
where => $where |
|
946 |
); |
|
947 |
$row = $result->all; |
|
948 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
949 | ||
950 |
$result = $dbi->select( |
|
951 |
table => 'table1', |
|
952 |
where => [ |
|
953 |
['and', 'key1 = :key1', 'key2 = :key2'], |
|
954 |
{key1 => 1} |
|
955 |
] |
|
956 |
); |
|
957 |
$row = $result->all; |
|
958 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
959 | ||
960 |
$where = $dbi->where |
|
961 |
->clause(['and', 'key1 = :key1', 'key2 = :key2']) |
|
962 |
->param({key1 => 1, key2 => 2}); |
|
963 |
$result = $dbi->select( |
|
964 |
table => 'table1', |
|
965 |
where => $where |
|
966 |
); |
|
967 |
$row = $result->all; |
|
968 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
969 | ||
970 |
$where = $dbi->where |
|
971 |
->clause(['and', 'key1 = :key1', 'key2 = :key2']) |
|
972 |
->param({}); |
|
973 |
$result = $dbi->select( |
|
974 |
table => 'table1', |
|
975 |
where => $where, |
|
976 |
); |
|
977 |
$row = $result->all; |
|
978 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
979 | ||
980 |
$where = $dbi->where |
|
981 |
->clause(['and', ['or', 'key1 > :key1', 'key1 < :key1'], 'key2 = :key2']) |
|
982 |
->param({key1 => [0, 3], key2 => 2}); |
|
983 |
$result = $dbi->select( |
|
984 |
table => 'table1', |
|
985 |
where => $where, |
|
986 |
); |
|
987 |
$row = $result->all; |
|
988 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
989 | ||
990 |
$where = $dbi->where; |
|
991 |
$result = $dbi->select( |
|
992 |
table => 'table1', |
|
993 |
where => $where |
|
994 |
); |
|
995 |
$row = $result->all; |
|
996 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
997 | ||
998 |
eval { |
|
999 |
$where = $dbi->where |
|
1000 |
->clause(['uuu']); |
|
1001 |
$result = $dbi->select( |
|
1002 |
table => 'table1', |
|
1003 |
where => $where |
|
1004 |
); |
|
1005 |
}; |
|
1006 |
ok($@); |
|
1007 | ||
1008 |
$where = $dbi->where; |
|
1009 |
is("$where", ''); |
|
1010 | ||
1011 |
$where = $dbi->where |
|
1012 |
->clause(['or', ('key1 = :key1') x 2]) |
|
1013 |
->param({key1 => [1, 3]}); |
|
1014 |
$result = $dbi->select( |
|
1015 |
table => 'table1', |
|
1016 |
where => $where, |
|
1017 |
); |
|
1018 |
$row = $result->all; |
|
1019 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
1020 | ||
1021 |
$where = $dbi->where |
|
1022 |
->clause(['or', ('key1 = :key1') x 2]) |
|
1023 |
->param({key1 => [1]}); |
|
1024 |
$result = $dbi->select( |
|
1025 |
table => 'table1', |
|
1026 |
where => $where, |
|
1027 |
); |
|
1028 |
$row = $result->all; |
|
1029 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
1030 | ||
1031 |
$where = $dbi->where |
|
1032 |
->clause(['or', ('key1 = :key1') x 2]) |
|
1033 |
->param({key1 => 1}); |
|
1034 |
$result = $dbi->select( |
|
1035 |
table => 'table1', |
|
1036 |
where => $where, |
|
1037 |
); |
|
1038 |
$row = $result->all; |
|
1039 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
1040 | ||
1041 |
$where = $dbi->where |
|
1042 |
->clause('key1 = :key1') |
|
1043 |
->param({key1 => 1}); |
|
1044 |
$result = $dbi->select( |
|
1045 |
table => 'table1', |
|
1046 |
where => $where, |
|
1047 |
); |
|
1048 |
$row = $result->all; |
|
1049 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
1050 | ||
1051 |
$where = $dbi->where |
|
1052 |
->clause('key1 = :key1 key2 = :key2') |
|
1053 |
->param({key1 => 1}); |
|
1054 |
eval{$where->to_string}; |
|
1055 |
like($@, qr/one column/); |
|
1056 | ||
1057 |
$where = $dbi->where |
|
1058 |
->clause(['or', ('key1 = :key1') x 3]) |
|
1059 |
->param({key1 => [$dbi->not_exists, 1, 3]}); |
|
1060 |
$result = $dbi->select( |
|
1061 |
table => 'table1', |
|
1062 |
where => $where, |
|
1063 |
); |
|
1064 |
$row = $result->all; |
|
1065 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists'); |
|
1066 | ||
1067 |
$where = $dbi->where |
|
1068 |
->clause(['or', ('key1 = :key1') x 3]) |
|
1069 |
->param({key1 => [1, $dbi->not_exists, 3]}); |
|
1070 |
$result = $dbi->select( |
|
1071 |
table => 'table1', |
|
1072 |
where => $where, |
|
1073 |
); |
|
1074 |
$row = $result->all; |
|
1075 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists'); |
|
1076 | ||
1077 |
$where = $dbi->where |
|
1078 |
->clause(['or', ('key1 = :key1') x 3]) |
|
1079 |
->param({key1 => [1, 3, $dbi->not_exists]}); |
|
1080 |
$result = $dbi->select( |
|
1081 |
table => 'table1', |
|
1082 |
where => $where, |
|
1083 |
); |
|
1084 |
$row = $result->all; |
|
1085 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists'); |
|
1086 | ||
1087 |
$where = $dbi->where |
|
1088 |
->clause(['or', ('key1 = :key1') x 3]) |
|
1089 |
->param({key1 => [1, $dbi->not_exists, $dbi->not_exists]}); |
|
1090 |
$result = $dbi->select( |
|
1091 |
table => 'table1', |
|
1092 |
where => $where, |
|
1093 |
); |
|
1094 |
$row = $result->all; |
|
1095 |
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists'); |
|
1096 | ||
1097 |
$where = $dbi->where |
|
1098 |
->clause(['or', ('key1 = :key1') x 3]) |
|
1099 |
->param({key1 => [$dbi->not_exists, 1, $dbi->not_exists]}); |
|
1100 |
$result = $dbi->select( |
|
1101 |
table => 'table1', |
|
1102 |
where => $where, |
|
1103 |
); |
|
1104 |
$row = $result->all; |
|
1105 |
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists'); |
|
1106 | ||
1107 |
$where = $dbi->where |
|
1108 |
->clause(['or', ('key1 = :key1') x 3]) |
|
1109 |
->param({key1 => [$dbi->not_exists, $dbi->not_exists, 1]}); |
|
1110 |
$result = $dbi->select( |
|
1111 |
table => 'table1', |
|
1112 |
where => $where, |
|
1113 |
); |
|
1114 |
$row = $result->all; |
|
1115 |
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists'); |
|
1116 | ||
1117 |
$where = $dbi->where |
|
1118 |
->clause(['or', ('key1 = :key1') x 3]) |
|
1119 |
->param({key1 => [$dbi->not_exists, $dbi->not_exists, $dbi->not_exists]}); |
|
1120 |
$result = $dbi->select( |
|
1121 |
table => 'table1', |
|
1122 |
where => $where, |
|
1123 |
); |
|
1124 |
$row = $result->all; |
|
1125 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists'); |
|
1126 | ||
1127 |
$where = $dbi->where |
|
1128 |
->clause(['or', ('key1 = :key1') x 3]) |
|
1129 |
->param({key1 => []}); |
|
1130 |
$result = $dbi->select( |
|
1131 |
table => 'table1', |
|
1132 |
where => $where, |
|
1133 |
); |
|
1134 |
$row = $result->all; |
|
1135 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists'); |
|
1136 | ||
1137 |
$where = $dbi->where |
|
1138 |
->clause(['and', '{> key1}', '{< key1}' ]) |
|
1139 |
->param({key1 => [2, $dbi->not_exists]}); |
|
1140 |
$result = $dbi->select( |
|
1141 |
table => 'table1', |
|
1142 |
where => $where, |
|
1143 |
); |
|
1144 |
$row = $result->all; |
|
1145 |
is_deeply($row, [{key1 => 3, key2 => 4}], 'not_exists'); |
|
1146 | ||
1147 |
$where = $dbi->where |
|
1148 |
->clause(['and', '{> key1}', '{< key1}' ]) |
|
1149 |
->param({key1 => [$dbi->not_exists, 2]}); |
|
1150 |
$result = $dbi->select( |
|
1151 |
table => 'table1', |
|
1152 |
where => $where, |
|
1153 |
); |
|
1154 |
$row = $result->all; |
|
1155 |
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists'); |
|
1156 | ||
1157 |
$where = $dbi->where |
|
1158 |
->clause(['and', '{> key1}', '{< key1}' ]) |
|
1159 |
->param({key1 => [$dbi->not_exists, $dbi->not_exists]}); |
|
1160 |
$result = $dbi->select( |
|
1161 |
table => 'table1', |
|
1162 |
where => $where, |
|
1163 |
); |
|
1164 |
$row = $result->all; |
|
1165 |
is_deeply($row, [{key1 => 1, key2 => 2},{key1 => 3, key2 => 4}], 'not_exists'); |
|
1166 | ||
1167 |
$where = $dbi->where |
|
1168 |
->clause(['and', '{> key1}', '{< key1}' ]) |
|
1169 |
->param({key1 => [0, 2]}); |
|
1170 |
$result = $dbi->select( |
|
1171 |
table => 'table1', |
|
1172 |
where => $where, |
|
1173 |
); |
|
1174 |
$row = $result->all; |
|
1175 |
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists'); |
|
1176 | ||
1177 |
$where = $dbi->where |
|
1178 |
->clause(['and', 'key1 is not null', 'key2 is not null' ]); |
|
1179 |
$result = $dbi->select( |
|
1180 |
table => 'table1', |
|
1181 |
where => $where, |
|
1182 |
); |
|
1183 |
$row = $result->all; |
|
1184 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists'); |
|
1185 | ||
1186 |
eval {$dbi->where(ppp => 1) }; |
|
1187 |
like($@, qr/invalid/); |
|
1188 | ||
1189 |
$where = $dbi->where( |
|
1190 |
clause => ['and', ['or'], ['and', 'key1 = :key1', 'key2 = :key2']], |
|
1191 |
param => {key1 => 1, key2 => 2} |
|
1192 |
); |
|
1193 |
$result = $dbi->select( |
|
1194 |
table => 'table1', |
|
1195 |
where => $where, |
|
1196 |
); |
|
1197 |
$row = $result->all; |
|
1198 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
1199 | ||
1200 | ||
1201 |
$where = $dbi->where( |
|
1202 |
clause => ['and', ['or'], ['or', ':key1', ':key2']], |
|
1203 |
param => {} |
|
1204 |
); |
|
1205 |
$result = $dbi->select( |
|
1206 |
table => 'table1', |
|
1207 |
where => $where, |
|
1208 |
); |
|
1209 |
$row = $result->all; |
|
1210 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
1211 | ||
added EXPERIMENTAL DBIx::Cus...
|
1212 |
$where = $dbi->where; |
1213 |
$where->clause(['and', ':key1{=}']); |
|
1214 |
$where->param({key1 => undef}); |
|
1215 |
$result = $dbi->execute("select * from table1 $where", {key1 => 1}); |
|
1216 |
$row = $result->all; |
|
1217 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
1218 | ||
1219 |
$where = $dbi->where; |
|
1220 |
$where->clause(['and', ':key1{=}']); |
|
1221 |
$where->param({key1 => undef}); |
|
added map method(not complet...
|
1222 |
$where->if('defined'); |
map cleanup
|
1223 |
$where->map; |
added EXPERIMENTAL DBIx::Cus...
|
1224 |
$result = $dbi->execute("select * from table1 $where", {key1 => 1}); |
1225 |
$row = $result->all; |
|
1226 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
1227 | ||
1228 |
$where = $dbi->where; |
|
1229 |
$where->clause(['or', ':key1{=}', ':key1{=}']); |
|
1230 |
$where->param({key1 => [undef, undef]}); |
|
1231 |
$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]}); |
|
1232 |
$row = $result->all; |
|
1233 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
1234 |
$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]}); |
|
1235 |
$row = $result->all; |
|
1236 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
1237 | ||
1238 |
$where = $dbi->where; |
|
1239 |
$where->clause(['and', ':key1{=}']); |
|
1240 |
$where->param({key1 => [undef, undef]}); |
|
added map method(not complet...
|
1241 |
$where->if('defined'); |
map cleanup
|
1242 |
$where->map; |
added EXPERIMENTAL DBIx::Cus...
|
1243 |
$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]}); |
1244 |
$row = $result->all; |
|
1245 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
1246 |
$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]}); |
|
1247 |
$row = $result->all; |
|
1248 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
1249 | ||
1250 |
$where = $dbi->where; |
|
1251 |
$where->clause(['and', ':key1{=}']); |
|
1252 |
$where->param({key1 => 0}); |
|
added map method(not complet...
|
1253 |
$where->if('length'); |
map cleanup
|
1254 |
$where->map; |
added EXPERIMENTAL DBIx::Cus...
|
1255 |
$result = $dbi->execute("select * from table1 $where", {key1 => 1}); |
1256 |
$row = $result->all; |
|
1257 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
1258 | ||
1259 |
$where = $dbi->where; |
|
1260 |
$where->clause(['and', ':key1{=}']); |
|
1261 |
$where->param({key1 => ''}); |
|
added map method(not complet...
|
1262 |
$where->if('length'); |
map cleanup
|
1263 |
$where->map; |
added EXPERIMENTAL DBIx::Cus...
|
1264 |
$result = $dbi->execute("select * from table1 $where", {key1 => 1}); |
1265 |
$row = $result->all; |
|
1266 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
1267 | ||
1268 |
$where = $dbi->where; |
|
1269 |
$where->clause(['and', ':key1{=}']); |
|
1270 |
$where->param({key1 => 5}); |
|
added map method(not complet...
|
1271 |
$where->if(sub { ($_[0] || '') eq 5 }); |
map cleanup
|
1272 |
$where->map; |
added EXPERIMENTAL DBIx::Cus...
|
1273 |
$result = $dbi->execute("select * from table1 $where", {key1 => 1}); |
1274 |
$row = $result->all; |
|
1275 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
1276 | ||
1277 |
$where = $dbi->where; |
|
1278 |
$where->clause(['and', ':key1{=}']); |
|
1279 |
$where->param({key1 => 7}); |
|
added map method(not complet...
|
1280 |
$where->if(sub { ($_[0] || '') eq 5 }); |
map cleanup
|
1281 |
$where->map; |
added EXPERIMENTAL DBIx::Cus...
|
1282 |
$result = $dbi->execute("select * from table1 $where", {key1 => 1}); |
1283 |
$row = $result->all; |
|
1284 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
1285 | ||
added tests
|
1286 |
$where = $dbi->where; |
1287 |
$where->param({id => 1, author => 'Ken', price => 1900}); |
|
1288 |
$where->map(id => 'book.id', |
|
1289 |
author => ['book.author', sub { '%' . $_[0] . '%' }], |
|
1290 |
price => ['book.price', {if => sub { $_[0] eq 1900 }}] |
|
1291 |
); |
|
1292 |
is_deeply($where->param, {'book.id' => 1, 'book.author' => '%Ken%', |
|
1293 |
'book.price' => 1900}); |
|
1294 | ||
1295 |
$where = $dbi->where; |
|
1296 |
$where->param({id => 0, author => 0, price => 0}); |
|
1297 |
$where->map( |
|
1298 |
id => 'book.id', |
|
1299 |
author => ['book.author', sub { '%' . $_[0] . '%' }], |
|
1300 |
price => ['book.price', sub { '%' . $_[0] . '%' }, |
|
1301 |
{if => sub { $_[0] eq 0 }}] |
|
1302 |
); |
|
1303 |
is_deeply($where->param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'}); |
|
1304 | ||
1305 |
$where = $dbi->where; |
|
1306 |
$where->param({id => '', author => '', price => ''}); |
|
1307 |
$where->if('length'); |
|
1308 |
$where->map( |
|
1309 |
id => 'book.id', |
|
1310 |
author => ['book.author', sub { '%' . $_[0] . '%' }], |
|
1311 |
price => ['book.price', sub { '%' . $_[0] . '%' }, |
|
1312 |
{if => sub { $_[0] eq 1 }}] |
|
1313 |
); |
|
1314 |
is_deeply($where->param, {}); |
|
1315 | ||
1316 |
$where = $dbi->where; |
|
1317 |
$where->param({id => undef, author => undef, price => undef}); |
|
1318 |
$where->if('length'); |
|
1319 |
$where->map( |
|
1320 |
id => 'book.id', |
|
1321 |
price => ['book.price', {if => 'exists'}] |
|
1322 |
); |
|
1323 |
is_deeply($where->param, {'book.price' => undef}); |
|
1324 | ||
1325 |
$where = $dbi->where; |
|
1326 |
$where->param({price => 'a'}); |
|
1327 |
$where->if('length'); |
|
1328 |
$where->map( |
|
1329 |
id => ['book.id', {if => 'exists'}], |
|
1330 |
price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}] |
|
1331 |
); |
|
1332 |
is_deeply($where->param, {'book.price' => '%a'}); |
|
1333 | ||
fixed if is not converted to...
|
1334 |
$where = $dbi->where; |
1335 |
$where->param({id => [1, 2], author => 'Ken', price => 1900}); |
|
1336 |
$where->map( |
|
1337 |
id => 'book.id', |
|
1338 |
author => ['book.author', sub { '%' . $_[0] . '%' }], |
|
1339 |
price => ['book.price', {if => sub { $_[0] eq 1900 }}] |
|
1340 |
); |
|
1341 |
is_deeply($where->param, {'book.id' => [1, 2], 'book.author' => '%Ken%', |
|
1342 |
'book.price' => 1900}); |
|
1343 | ||
1344 |
$where = $dbi->where; |
|
1345 |
$where->if('length'); |
|
1346 |
$where->param({id => ['', ''], author => 'Ken', price => 1900}); |
|
1347 |
$where->map( |
|
1348 |
id => 'book.id', |
|
1349 |
author => ['book.author', sub { '%' . $_[0] . '%' }], |
|
1350 |
price => ['book.price', {if => sub { $_[0] eq 1900 }}] |
|
1351 |
); |
|
1352 |
is_deeply($where->param, {'book.id' => [$dbi->not_exists, $dbi->not_exists], 'book.author' => '%Ken%', |
|
1353 |
'book.price' => 1900}); |
|
1354 | ||
1355 |
$where = $dbi->where; |
|
1356 |
$where->param({id => ['', ''], author => 'Ken', price => 1900}); |
|
1357 |
$where->map( |
|
1358 |
id => ['book.id', {if => 'length'}], |
|
1359 |
author => ['book.author', sub { '%' . $_[0] . '%' }, {if => 'defined'}], |
|
1360 |
price => ['book.price', {if => sub { $_[0] eq 1900 }}] |
|
1361 |
); |
|
1362 |
is_deeply($where->param, {'book.id' => [$dbi->not_exists, $dbi->not_exists], 'book.author' => '%Ken%', |
|
1363 |
'book.price' => 1900}); |
|
cleanup test
|
1364 | |
1365 |
test 'dbi_option default'; |
|
1366 |
$dbi = DBIx::Custom->new; |
|
1367 |
is_deeply($dbi->dbi_option, {}); |
|
1368 | ||
1369 |
test 'register_tag_processor'; |
|
cleanup test
|
1370 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
1371 |
$dbi->register_tag_processor( |
1372 |
a => sub { 1 } |
|
1373 |
); |
|
1374 |
is($dbi->query_builder->tag_processors->{a}->(), 1); |
|
1375 | ||
1376 |
test 'register_tag'; |
|
cleanup test
|
1377 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
1378 |
$dbi->register_tag( |
1379 |
b => sub { 2 } |
|
1380 |
); |
|
1381 |
is($dbi->query_builder->tags->{b}->(), 2); |
|
1382 | ||
1383 |
test 'table not specify exception'; |
|
cleanup test
|
1384 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
1385 |
eval {$dbi->insert}; |
1386 |
like($@, qr/table/); |
|
1387 |
eval {$dbi->update}; |
|
1388 |
like($@, qr/table/); |
|
1389 |
eval {$dbi->delete}; |
|
1390 |
like($@, qr/table/); |
|
1391 |
eval {$dbi->select}; |
|
1392 |
like($@, qr/table/); |
|
1393 | ||
1394 | ||
1395 |
test 'more tests'; |
|
cleanup test
|
1396 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
1397 |
eval{$dbi->apply_filter('table', 'column', [])}; |
1398 |
like($@, qr/apply_filter/); |
|
1399 | ||
1400 |
eval{$dbi->apply_filter('table', 'column', {outer => 2})}; |
|
1401 |
like($@, qr/apply_filter/); |
|
1402 | ||
1403 |
$dbi->apply_filter( |
|
1404 | ||
1405 |
); |
|
cleanup test
|
1406 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
1407 |
$dbi->execute($create_table1); |
cleanup test
|
1408 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
1409 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
1410 |
$dbi->apply_filter('table1', 'key2', |
|
1411 |
{in => sub { $_[0] * 3 }, out => sub { $_[0] * 2 }}); |
|
1412 |
$rows = $dbi->select(table => 'table1', where => {key2 => 1})->all; |
|
1413 |
is_deeply($rows, [{key1 => 1, key2 => 6}]); |
|
1414 | ||
cleanup test
|
1415 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
1416 |
$dbi->execute($create_table1); |
cleanup test
|
1417 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
1418 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
1419 |
$dbi->apply_filter('table1', 'key2', {}); |
|
1420 |
$rows = $dbi->select(table => 'table1', where => {key2 => 2})->all; |
|
1421 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
1422 | ||
cleanup test
|
1423 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
1424 |
eval {$dbi->apply_filter('table1', 'key2', {out => 'no'})}; |
1425 |
like($@, qr/not registered/); |
|
1426 |
eval {$dbi->apply_filter('table1', 'key2', {in => 'no'})}; |
|
1427 |
like($@, qr/not registered/); |
|
1428 |
$dbi->method({one => sub { 1 }}); |
|
1429 |
is($dbi->one, 1); |
|
1430 | ||
1431 |
eval{DBIx::Custom->connect()}; |
|
1432 |
like($@, qr/_connect/); |
|
1433 | ||
cleanup test
|
1434 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
1435 |
$dbi->execute($create_table1); |
cleanup test
|
1436 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
1437 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, |
|
1438 |
filter => {key1 => 'twice'}); |
|
1439 |
$row = $dbi->select(table => 'table1')->one; |
|
1440 |
is_deeply($row, {key1 => 2, key2 => 2}); |
|
1441 |
eval {$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, |
|
1442 |
filter => {key1 => 'no'}) }; |
|
1443 |
like($@, qr//); |
|
1444 | ||
1445 |
$dbi->register_filter(one => sub { }); |
|
1446 |
$dbi->default_fetch_filter('one'); |
|
1447 |
ok($dbi->default_fetch_filter); |
|
1448 |
$dbi->default_bind_filter('one'); |
|
1449 |
ok($dbi->default_bind_filter); |
|
1450 |
eval{$dbi->default_fetch_filter('no')}; |
|
1451 |
like($@, qr/not registered/); |
|
1452 |
eval{$dbi->default_bind_filter('no')}; |
|
1453 |
like($@, qr/not registered/); |
|
1454 |
$dbi->default_bind_filter(undef); |
|
1455 |
ok(!defined $dbi->default_bind_filter); |
|
1456 |
$dbi->default_fetch_filter(undef); |
|
1457 |
ok(!defined $dbi->default_fetch_filter); |
|
1458 |
eval {$dbi->execute('select * from table1 {} {= author') }; |
|
1459 |
like($@, qr/Tag not finished/); |
|
1460 | ||
cleanup test
|
1461 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
1462 |
$dbi->execute($create_table1); |
cleanup test
|
1463 |
$dbi->register_filter(one => sub { 1 }); |
1464 |
$result = $dbi->select(table => 'table1'); |
|
1465 |
eval {$result->filter(key1 => 'no')}; |
|
1466 |
like($@, qr/not registered/); |
|
1467 |
eval {$result->end_filter(key1 => 'no')}; |
|
1468 |
like($@, qr/not registered/); |
|
1469 |
$result->default_filter(undef); |
|
1470 |
ok(!defined $result->default_filter); |
|
1471 |
$result->default_filter('one'); |
|
1472 |
is($result->default_filter->(), 1); |
|
1473 | ||
1474 |
test 'dbi_option'; |
|
cleanup test
|
1475 |
$dbi = DBIx::Custom->connect(%memory, |
cleanup test
|
1476 |
dbi_option => {PrintError => 1}); |
1477 |
ok($dbi->dbh->{PrintError}); |
|
cleanup test
|
1478 |
$dbi = DBIx::Custom->connect(%memory, |
cleanup test
|
1479 |
dbi_options => {PrintError => 1}); |
1480 |
ok($dbi->dbh->{PrintError}); |
|
1481 | ||
1482 |
test 'DBIx::Custom::Result stash()'; |
|
1483 |
$result = DBIx::Custom::Result->new; |
|
1484 |
is_deeply($result->stash, {}, 'default'); |
|
1485 |
$result->stash->{foo} = 1; |
|
1486 |
is($result->stash->{foo}, 1, 'get and set'); |
|
1487 | ||
1488 |
test 'filter __ expression'; |
|
cleanup test
|
1489 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
1490 |
$dbi->execute('create table company (id, name, location_id)'); |
1491 |
$dbi->execute('create table location (id, name)'); |
|
1492 |
$dbi->apply_filter('location', |
|
1493 |
name => {in => sub { uc $_[0] } } |
|
1494 |
); |
|
1495 | ||
1496 |
$dbi->insert(table => 'company', param => {id => 1, name => 'a', location_id => 2}); |
|
1497 |
$dbi->insert(table => 'location', param => {id => 2, name => 'b'}); |
|
1498 | ||
1499 |
$result = $dbi->select( |
|
1500 |
table => ['company', 'location'], relation => {'company.location_id' => 'location.id'}, |
|
1501 |
column => ['location.name as location__name'] |
|
1502 |
); |
|
1503 |
is($result->fetch_first->[0], 'B'); |
|
1504 | ||
1505 |
$result = $dbi->select( |
|
1506 |
table => 'company', relation => {'company.location_id' => 'location.id'}, |
|
1507 |
column => ['location.name as location__name'] |
|
1508 |
); |
|
1509 |
is($result->fetch_first->[0], 'B'); |
|
1510 | ||
1511 |
$result = $dbi->select( |
|
1512 |
table => 'company', relation => {'company.location_id' => 'location.id'}, |
|
1513 |
column => ['location.name as "location.name"'] |
|
1514 |
); |
|
1515 |
is($result->fetch_first->[0], 'B'); |
|
1516 | ||
1517 |
test 'Model class'; |
|
1518 |
use MyDBI1; |
|
cleanup test
|
1519 |
$dbi = MyDBI1->connect(%memory); |
cleanup test
|
1520 |
$dbi->execute("create table book (title, author)"); |
1521 |
$model = $dbi->model('book'); |
|
1522 |
$model->insert({title => 'a', author => 'b'}); |
|
1523 |
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic'); |
|
1524 |
$dbi->execute("create table company (name)"); |
|
1525 |
$model = $dbi->model('company'); |
|
1526 |
$model->insert({name => 'a'}); |
|
1527 |
is_deeply($model->list->all, [{name => 'a'}], 'basic'); |
|
1528 |
is($dbi->models->{'book'}, $dbi->model('book')); |
|
1529 |
is($dbi->models->{'company'}, $dbi->model('company')); |
|
1530 | ||
1531 |
{ |
|
1532 |
package MyDBI4; |
|
1533 | ||
1534 |
use strict; |
|
1535 |
use warnings; |
|
1536 | ||
1537 |
use base 'DBIx::Custom'; |
|
1538 | ||
1539 |
sub connect { |
|
1540 |
my $self = shift->SUPER::connect(@_); |
|
1541 |
|
|
1542 |
$self->include_model( |
|
1543 |
MyModel2 => [ |
|
1544 |
'book', |
|
1545 |
{class => 'Company', name => 'company'} |
|
1546 |
] |
|
1547 |
); |
|
1548 |
} |
|
1549 | ||
1550 |
package MyModel2::Base1; |
|
1551 | ||
1552 |
use strict; |
|
1553 |
use warnings; |
|
1554 | ||
1555 |
use base 'DBIx::Custom::Model'; |
|
1556 | ||
1557 |
package MyModel2::book; |
|
1558 | ||
1559 |
use strict; |
|
1560 |
use warnings; |
|
1561 | ||
1562 |
use base 'MyModel2::Base1'; |
|
1563 | ||
1564 |
sub insert { |
|
1565 |
my ($self, $param) = @_; |
|
1566 |
|
|
1567 |
return $self->SUPER::insert(param => $param); |
|
1568 |
} |
|
1569 | ||
1570 |
sub list { shift->select; } |
|
1571 | ||
1572 |
package MyModel2::Company; |
|
1573 | ||
1574 |
use strict; |
|
1575 |
use warnings; |
|
1576 | ||
1577 |
use base 'MyModel2::Base1'; |
|
1578 | ||
1579 |
sub insert { |
|
1580 |
my ($self, $param) = @_; |
|
1581 |
|
|
1582 |
return $self->SUPER::insert(param => $param); |
|
1583 |
} |
|
1584 | ||
1585 |
sub list { shift->select; } |
|
1586 |
} |
|
cleanup test
|
1587 |
$dbi = MyDBI4->connect(%memory); |
cleanup test
|
1588 |
$dbi->execute("create table book (title, author)"); |
1589 |
$model = $dbi->model('book'); |
|
1590 |
$model->insert({title => 'a', author => 'b'}); |
|
1591 |
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic'); |
|
1592 |
$dbi->execute("create table company (name)"); |
|
1593 |
$model = $dbi->model('company'); |
|
1594 |
$model->insert({name => 'a'}); |
|
1595 |
is_deeply($model->list->all, [{name => 'a'}], 'basic'); |
|
1596 | ||
1597 |
{ |
|
1598 |
package MyDBI5; |
|
1599 | ||
1600 |
use strict; |
|
1601 |
use warnings; |
|
1602 | ||
1603 |
use base 'DBIx::Custom'; |
|
1604 | ||
1605 |
sub connect { |
|
1606 |
my $self = shift->SUPER::connect(@_); |
|
1607 |
|
|
1608 |
$self->include_model('MyModel4'); |
|
1609 |
} |
|
1610 |
} |
|
cleanup test
|
1611 |
$dbi = MyDBI5->connect(%memory); |
cleanup test
|
1612 |
$dbi->execute("create table company (name)"); |
1613 |
$dbi->execute("create table table1 (key1)"); |
|
1614 |
$model = $dbi->model('company'); |
|
1615 |
$model->insert({name => 'a'}); |
|
1616 |
is_deeply($model->list->all, [{name => 'a'}], 'include all model'); |
|
1617 |
$dbi->insert(table => 'table1', param => {key1 => 1}); |
|
1618 |
$model = $dbi->model('book'); |
|
1619 |
is_deeply($model->list->all, [{key1 => 1}], 'include all model'); |
|
1620 | ||
1621 |
test 'primary_key'; |
|
1622 |
use MyDBI1; |
|
cleanup test
|
1623 |
$dbi = MyDBI1->connect(%memory); |
cleanup test
|
1624 |
$model = $dbi->model('book'); |
1625 |
$model->primary_key(['id', 'number']); |
|
1626 |
is_deeply($model->primary_key, ['id', 'number']); |
|
1627 | ||
1628 |
test 'columns'; |
|
1629 |
use MyDBI1; |
|
cleanup test
|
1630 |
$dbi = MyDBI1->connect(%memory); |
cleanup test
|
1631 |
$model = $dbi->model('book'); |
1632 |
$model->columns(['id', 'number']); |
|
1633 |
is_deeply($model->columns, ['id', 'number']); |
|
1634 | ||
1635 |
test 'setup_model'; |
|
1636 |
use MyDBI1; |
|
cleanup test
|
1637 |
$dbi = MyDBI1->connect(%memory); |
cleanup test
|
1638 |
$dbi->execute('create table book (id)'); |
1639 |
$dbi->execute('create table company (id, name);'); |
|
1640 |
$dbi->execute('create table test (id, name, primary key (id, name));'); |
|
1641 |
$dbi->setup_model; |
|
1642 |
is_deeply($dbi->model('book')->columns, ['id']); |
|
1643 |
is_deeply($dbi->model('company')->columns, ['id', 'name']); |
|
1644 | ||
1645 |
test 'delete_at'; |
|
cleanup test
|
1646 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
1647 |
$dbi->execute($create_table1_2); |
cleanup test
|
1648 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
1649 |
$dbi->delete_at( |
|
1650 |
table => 'table1', |
|
1651 |
primary_key => ['key1', 'key2'], |
|
1652 |
where => [1, 2], |
|
1653 |
); |
|
1654 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
1655 | ||
1656 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
1657 |
$dbi->delete_at( |
|
1658 |
table => 'table1', |
|
1659 |
primary_key => 'key1', |
|
1660 |
where => 1, |
|
1661 |
); |
|
1662 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
1663 | ||
1664 |
test 'insert_at'; |
|
cleanup test
|
1665 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
1666 |
$dbi->execute($create_table1_2); |
cleanup test
|
1667 |
$dbi->insert_at( |
1668 |
primary_key => ['key1', 'key2'], |
|
1669 |
table => 'table1', |
|
1670 |
where => [1, 2], |
|
1671 |
param => {key3 => 3} |
|
1672 |
); |
|
1673 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
1674 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
1675 |
is($dbi->select(table => 'table1')->one->{key3}, 3); |
|
1676 | ||
1677 |
$dbi->delete_all(table => 'table1'); |
|
1678 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
1679 |
$dbi->insert_at( |
|
1680 |
primary_key => 'key1', |
|
1681 |
table => 'table1', |
|
1682 |
where => 1, |
|
1683 |
param => {key2 => 2, key3 => 3} |
|
1684 |
); |
|
1685 | ||
1686 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
1687 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
1688 |
is($dbi->select(table => 'table1')->one->{key3}, 3); |
|
1689 | ||
1690 |
eval { |
|
1691 |
$dbi->insert_at( |
|
1692 |
table => 'table1', |
|
1693 |
primary_key => ['key1', 'key2'], |
|
1694 |
where => {}, |
|
1695 |
param => {key1 => 1, key2 => 2, key3 => 3}, |
|
1696 |
); |
|
1697 |
}; |
|
1698 |
like($@, qr/must be/); |
|
1699 | ||
cleanup test
|
1700 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
1701 |
$dbi->execute($create_table1_2); |
cleanup test
|
1702 |
$dbi->insert_at( |
1703 |
{key3 => 3}, |
|
1704 |
primary_key => ['key1', 'key2'], |
|
1705 |
table => 'table1', |
|
1706 |
where => [1, 2], |
|
1707 |
); |
|
1708 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
1709 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
1710 |
is($dbi->select(table => 'table1')->one->{key3}, 3); |
|
1711 | ||
1712 |
test 'update_at'; |
|
cleanup test
|
1713 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
1714 |
$dbi->execute($create_table1_2); |
cleanup test
|
1715 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
1716 |
$dbi->update_at( |
|
1717 |
table => 'table1', |
|
1718 |
primary_key => ['key1', 'key2'], |
|
1719 |
where => [1, 2], |
|
1720 |
param => {key3 => 4} |
|
1721 |
); |
|
1722 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
1723 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
1724 |
is($dbi->select(table => 'table1')->one->{key3}, 4); |
|
1725 | ||
1726 |
$dbi->delete_all(table => 'table1'); |
|
1727 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
1728 |
$dbi->update_at( |
|
1729 |
table => 'table1', |
|
1730 |
primary_key => 'key1', |
|
1731 |
where => 1, |
|
1732 |
param => {key3 => 4} |
|
1733 |
); |
|
1734 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
1735 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
1736 |
is($dbi->select(table => 'table1')->one->{key3}, 4); |
|
1737 | ||
cleanup test
|
1738 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
1739 |
$dbi->execute($create_table1_2); |
cleanup test
|
1740 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
1741 |
$dbi->update_at( |
|
1742 |
{key3 => 4}, |
|
1743 |
table => 'table1', |
|
1744 |
primary_key => ['key1', 'key2'], |
|
1745 |
where => [1, 2] |
|
1746 |
); |
|
1747 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
1748 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
1749 |
is($dbi->select(table => 'table1')->one->{key3}, 4); |
|
1750 | ||
1751 |
test 'select_at'; |
|
cleanup test
|
1752 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
1753 |
$dbi->execute($create_table1_2); |
cleanup test
|
1754 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
1755 |
$result = $dbi->select_at( |
|
1756 |
table => 'table1', |
|
1757 |
primary_key => ['key1', 'key2'], |
|
1758 |
where => [1, 2] |
|
1759 |
); |
|
1760 |
$row = $result->one; |
|
1761 |
is($row->{key1}, 1); |
|
1762 |
is($row->{key2}, 2); |
|
1763 |
is($row->{key3}, 3); |
|
1764 | ||
1765 |
$dbi->delete_all(table => 'table1'); |
|
1766 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
1767 |
$result = $dbi->select_at( |
|
1768 |
table => 'table1', |
|
1769 |
primary_key => 'key1', |
|
1770 |
where => 1, |
|
1771 |
); |
|
1772 |
$row = $result->one; |
|
1773 |
is($row->{key1}, 1); |
|
1774 |
is($row->{key2}, 2); |
|
1775 |
is($row->{key3}, 3); |
|
1776 | ||
1777 |
$dbi->delete_all(table => 'table1'); |
|
1778 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
1779 |
$result = $dbi->select_at( |
|
1780 |
table => 'table1', |
|
1781 |
primary_key => ['key1', 'key2'], |
|
1782 |
where => [1, 2] |
|
1783 |
); |
|
1784 |
$row = $result->one; |
|
1785 |
is($row->{key1}, 1); |
|
1786 |
is($row->{key2}, 2); |
|
1787 |
is($row->{key3}, 3); |
|
1788 | ||
1789 |
eval { |
|
1790 |
$result = $dbi->select_at( |
|
1791 |
table => 'table1', |
|
1792 |
primary_key => ['key1', 'key2'], |
|
1793 |
where => {}, |
|
1794 |
); |
|
1795 |
}; |
|
1796 |
like($@, qr/must be/); |
|
1797 | ||
1798 |
eval { |
|
1799 |
$result = $dbi->select_at( |
|
1800 |
table => 'table1', |
|
1801 |
primary_key => ['key1', 'key2'], |
|
1802 |
where => [1], |
|
1803 |
); |
|
1804 |
}; |
|
1805 |
like($@, qr/same/); |
|
1806 | ||
1807 |
eval { |
|
1808 |
$result = $dbi->update_at( |
|
1809 |
table => 'table1', |
|
1810 |
primary_key => ['key1', 'key2'], |
|
1811 |
where => {}, |
|
1812 |
param => {key1 => 1, key2 => 2}, |
|
1813 |
); |
|
1814 |
}; |
|
1815 |
like($@, qr/must be/); |
|
1816 | ||
1817 |
eval { |
|
1818 |
$result = $dbi->delete_at( |
|
1819 |
table => 'table1', |
|
1820 |
primary_key => ['key1', 'key2'], |
|
1821 |
where => {}, |
|
1822 |
); |
|
1823 |
}; |
|
1824 |
like($@, qr/must be/); |
|
1825 | ||
1826 |
test 'columns'; |
|
1827 |
use MyDBI1; |
|
cleanup test
|
1828 |
$dbi = MyDBI1->connect(%memory); |
cleanup test
|
1829 |
$model = $dbi->model('book'); |
1830 | ||
1831 | ||
1832 |
test 'model delete_at'; |
|
1833 |
{ |
|
1834 |
package MyDBI6; |
|
1835 |
|
|
1836 |
use base 'DBIx::Custom'; |
|
1837 |
|
|
1838 |
sub connect { |
|
1839 |
my $self = shift->SUPER::connect(@_); |
|
1840 |
|
|
1841 |
$self->include_model('MyModel5'); |
|
1842 |
|
|
1843 |
return $self; |
|
1844 |
} |
|
1845 |
} |
|
cleanup test
|
1846 |
$dbi = MyDBI6->connect(%memory); |
cleanup test
|
1847 |
$dbi->execute($create_table1_2); |
cleanup test
|
1848 |
$dbi->execute("create table table2 (key1, key2, key3)"); |
1849 |
$dbi->execute("create table table3 (key1, key2, key3)"); |
|
1850 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
1851 |
$dbi->model('table1')->delete_at(where => [1, 2]); |
|
1852 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
1853 |
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
1854 |
$dbi->model('table1_1')->delete_at(where => [1, 2]); |
|
1855 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
1856 |
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
1857 |
$dbi->model('table1_3')->delete_at(where => [1, 2]); |
|
1858 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
1859 | ||
1860 |
test 'model insert_at'; |
|
cleanup test
|
1861 |
$dbi = MyDBI6->connect(%memory); |
cleanup test
|
1862 |
$dbi->execute($create_table1_2); |
cleanup test
|
1863 |
$dbi->model('table1')->insert_at( |
1864 |
where => [1, 2], |
|
1865 |
param => {key3 => 3} |
|
1866 |
); |
|
1867 |
$result = $dbi->model('table1')->select; |
|
1868 |
$row = $result->one; |
|
1869 |
is($row->{key1}, 1); |
|
1870 |
is($row->{key2}, 2); |
|
1871 |
is($row->{key3}, 3); |
|
1872 | ||
1873 |
test 'model update_at'; |
|
cleanup test
|
1874 |
$dbi = MyDBI6->connect(%memory); |
cleanup test
|
1875 |
$dbi->execute($create_table1_2); |
cleanup test
|
1876 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
1877 |
$dbi->model('table1')->update_at( |
|
1878 |
where => [1, 2], |
|
1879 |
param => {key3 => 4} |
|
1880 |
); |
|
1881 |
$result = $dbi->model('table1')->select; |
|
1882 |
$row = $result->one; |
|
1883 |
is($row->{key1}, 1); |
|
1884 |
is($row->{key2}, 2); |
|
1885 |
is($row->{key3}, 4); |
|
1886 | ||
1887 |
test 'model select_at'; |
|
cleanup test
|
1888 |
$dbi = MyDBI6->connect(%memory); |
cleanup test
|
1889 |
$dbi->execute($create_table1_2); |
cleanup test
|
1890 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
1891 |
$result = $dbi->model('table1')->select_at(where => [1, 2]); |
|
1892 |
$row = $result->one; |
|
1893 |
is($row->{key1}, 1); |
|
1894 |
is($row->{key2}, 2); |
|
1895 |
is($row->{key3}, 3); |
|
1896 | ||
1897 | ||
1898 |
test 'mycolumn and column'; |
|
1899 |
{ |
|
1900 |
package MyDBI7; |
|
1901 |
|
|
1902 |
use base 'DBIx::Custom'; |
|
1903 |
|
|
1904 |
sub connect { |
|
1905 |
my $self = shift->SUPER::connect(@_); |
|
1906 |
|
|
1907 |
$self->include_model('MyModel6'); |
|
1908 |
|
|
1909 |
|
|
1910 |
return $self; |
|
1911 |
} |
|
1912 |
} |
|
cleanup test
|
1913 |
$dbi = MyDBI7->connect(%memory); |
cleanup test
|
1914 |
$dbi->execute($create_table1); |
cleanup test
|
1915 |
$dbi->execute('create table table2 (key1 char(255), key3 char(255));'); |
cleanup test
|
1916 |
$dbi->separator('__'); |
1917 |
$dbi->setup_model; |
|
1918 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1919 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3}); |
|
1920 |
$model = $dbi->model('table1'); |
|
1921 |
$result = $model->select( |
|
1922 |
column => [$model->mycolumn, $model->column('table2')], |
|
1923 |
where => {'table1.key1' => 1} |
|
1924 |
); |
|
1925 |
is_deeply($result->one, |
|
1926 |
{key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3}); |
|
1927 | ||
1928 |
test 'update_param'; |
|
cleanup test
|
1929 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
1930 |
$dbi->execute($create_table1_2); |
cleanup test
|
1931 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
1932 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
1933 | ||
1934 |
$param = {key2 => 11}; |
|
1935 |
$update_param = $dbi->update_param($param); |
|
1936 |
$sql = <<"EOS"; |
|
1937 |
update table1 $update_param |
|
1938 |
where key1 = 1 |
|
1939 |
EOS |
|
1940 |
$dbi->execute($sql, param => $param); |
|
test cleanup
|
1941 |
$result = $dbi->execute('select * from table1;', table => 'table1'); |
cleanup test
|
1942 |
$rows = $result->all; |
1943 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
1944 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
1945 |
"basic"); |
|
1946 | ||
1947 | ||
cleanup test
|
1948 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
1949 |
$dbi->execute($create_table1_2); |
cleanup test
|
1950 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
1951 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
1952 | ||
1953 |
$param = {key2 => 11, key3 => 33}; |
|
1954 |
$update_param = $dbi->update_param($param); |
|
1955 |
$sql = <<"EOS"; |
|
1956 |
update table1 $update_param |
|
1957 |
where key1 = 1 |
|
1958 |
EOS |
|
1959 |
$dbi->execute($sql, param => $param); |
|
test cleanup
|
1960 |
$result = $dbi->execute('select * from table1;', table => 'table1'); |
cleanup test
|
1961 |
$rows = $result->all; |
1962 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5}, |
|
1963 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
1964 |
"basic"); |
|
1965 | ||
cleanup test
|
1966 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
1967 |
$dbi->execute($create_table1_2); |
cleanup test
|
1968 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
1969 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
1970 | ||
1971 |
$param = {key2 => 11, key3 => 33}; |
|
1972 |
$update_param = $dbi->update_param($param, {no_set => 1}); |
|
1973 |
$sql = <<"EOS"; |
|
1974 |
update table1 set $update_param |
|
1975 |
where key1 = 1 |
|
1976 |
EOS |
|
1977 |
$dbi->execute($sql, param => $param); |
|
test cleanup
|
1978 |
$result = $dbi->execute('select * from table1;', table => 'table1'); |
cleanup test
|
1979 |
$rows = $result->all; |
1980 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5}, |
|
1981 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
1982 |
"update param no_set"); |
|
1983 | ||
1984 |
|
|
1985 |
eval { $dbi->update_param({";" => 1}) }; |
|
1986 |
like($@, qr/not safety/); |
|
1987 | ||
1988 | ||
1989 |
test 'update_param'; |
|
cleanup test
|
1990 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
1991 |
$dbi->execute($create_table1_2); |
cleanup test
|
1992 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
1993 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
1994 | ||
1995 |
$param = {key2 => 11}; |
|
1996 |
$update_param = $dbi->assign_param($param); |
|
1997 |
$sql = <<"EOS"; |
|
1998 |
update table1 set $update_param |
|
1999 |
where key1 = 1 |
|
2000 |
EOS |
|
2001 |
$dbi->execute($sql, param => $param, table => 'table1'); |
|
test cleanup
|
2002 |
$result = $dbi->execute('select * from table1;'); |
cleanup test
|
2003 |
$rows = $result->all; |
2004 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
2005 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
2006 |
"basic"); |
|
2007 | ||
2008 | ||
2009 |
test 'insert_param'; |
|
cleanup test
|
2010 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2011 |
$dbi->execute($create_table1_2); |
cleanup test
|
2012 |
$param = {key1 => 1, key2 => 2}; |
2013 |
$insert_param = $dbi->insert_param($param); |
|
2014 |
$sql = <<"EOS"; |
|
2015 |
insert into table1 $insert_param |
|
2016 |
EOS |
|
2017 |
$dbi->execute($sql, param => $param, table => 'table1'); |
|
2018 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
2019 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
2020 | ||
cleanup test
|
2021 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2022 |
$dbi->quote('"'); |
cleanup test
|
2023 |
$dbi->execute($create_table1_2); |
cleanup test
|
2024 |
$param = {key1 => 1, key2 => 2}; |
2025 |
$insert_param = $dbi->insert_param($param); |
|
2026 |
$sql = <<"EOS"; |
|
2027 |
insert into table1 $insert_param |
|
2028 |
EOS |
|
2029 |
$dbi->execute($sql, param => $param, table => 'table1'); |
|
2030 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
2031 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
2032 | ||
2033 |
eval { $dbi->insert_param({";" => 1}) }; |
|
2034 |
like($@, qr/not safety/); |
|
2035 | ||
cleanup test
|
2036 | |
2037 |
test 'join'; |
|
cleanup test
|
2038 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2039 |
$dbi->execute($create_table1); |
cleanup test
|
2040 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
2041 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
cleanup test
|
2042 |
$dbi->execute('create table table2 (key1 char(255), key3 char(255));'); |
cleanup test
|
2043 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
cleanup test
|
2044 |
$dbi->execute('create table table3 (key3 int, key4 int);'); |
cleanup test
|
2045 |
$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4}); |
2046 |
$rows = $dbi->select( |
|
2047 |
table => 'table1', |
|
2048 |
column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3', |
|
2049 |
where => {'table1.key2' => 2}, |
|
2050 |
join => ['left outer join table2 on table1.key1 = table2.key1'] |
|
2051 |
)->all; |
|
2052 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]); |
|
2053 | ||
2054 |
$rows = $dbi->select( |
|
2055 |
table => 'table1', |
|
2056 |
where => {'key1' => 1}, |
|
2057 |
join => ['left outer join table2 on table1.key1 = table2.key1'] |
|
2058 |
)->all; |
|
2059 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
2060 | ||
2061 |
eval { |
|
2062 |
$rows = $dbi->select( |
|
2063 |
table => 'table1', |
|
2064 |
column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3', |
|
2065 |
where => {'table1.key2' => 2}, |
|
2066 |
join => {'table1.key1' => 'table2.key1'} |
|
2067 |
); |
|
2068 |
}; |
|
2069 |
like ($@, qr/array/); |
|
2070 | ||
2071 |
$rows = $dbi->select( |
|
2072 |
table => 'table1', |
|
2073 |
where => {'key1' => 1}, |
|
2074 |
join => ['left outer join table2 on table1.key1 = table2.key1', |
|
2075 |
'left outer join table3 on table2.key3 = table3.key3'] |
|
2076 |
)->all; |
|
2077 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
2078 | ||
2079 |
$rows = $dbi->select( |
|
2080 |
column => 'table3.key4 as table3__key4', |
|
2081 |
table => 'table1', |
|
2082 |
where => {'table1.key1' => 1}, |
|
2083 |
join => ['left outer join table2 on table1.key1 = table2.key1', |
|
2084 |
'left outer join table3 on table2.key3 = table3.key3'] |
|
2085 |
)->all; |
|
2086 |
is_deeply($rows, [{table3__key4 => 4}]); |
|
2087 | ||
2088 |
$rows = $dbi->select( |
|
2089 |
column => 'table1.key1 as table1__key1', |
|
2090 |
table => 'table1', |
|
2091 |
where => {'table3.key4' => 4}, |
|
2092 |
join => ['left outer join table2 on table1.key1 = table2.key1', |
|
2093 |
'left outer join table3 on table2.key3 = table3.key3'] |
|
2094 |
)->all; |
|
2095 |
is_deeply($rows, [{table1__key1 => 1}]); |
|
2096 | ||
cleanup test
|
2097 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2098 |
$dbi->quote('"'); |
cleanup test
|
2099 |
$dbi->execute($create_table1); |
cleanup test
|
2100 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
cleanup test
|
2101 |
$dbi->execute('create table table2 (key1 char(255), key3 char(255));'); |
cleanup test
|
2102 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
2103 |
$rows = $dbi->select( |
|
2104 |
table => 'table1', |
|
2105 |
column => '"table1"."key1" as "table1_key1", "table2"."key1" as "table2_key1", "key2", "key3"', |
|
2106 |
where => {'table1.key2' => 2}, |
|
2107 |
join => ['left outer join "table2" on "table1"."key1" = "table2"."key1"'], |
|
2108 |
)->all; |
|
2109 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], |
|
2110 |
'quote'); |
|
2111 | ||
2112 |
{ |
|
2113 |
package MyDBI8; |
|
2114 |
|
|
2115 |
use base 'DBIx::Custom'; |
|
2116 |
|
|
2117 |
sub connect { |
|
2118 |
my $self = shift->SUPER::connect(@_); |
|
2119 |
|
|
2120 |
$self->include_model('MyModel7'); |
|
2121 |
|
|
2122 |
return $self; |
|
2123 |
} |
|
2124 |
} |
|
cleanup test
|
2125 | |
cleanup test
|
2126 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2127 |
$dbi->execute($create_table1); |
cleanup test
|
2128 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
2129 |
$sql = <<"EOS"; |
|
cleanup test
|
2130 |
left outer join ( |
2131 |
select * from table1 as t1 |
|
2132 |
where t1.key2 = ( |
|
2133 |
select max(t2.key2) from table1 as t2 |
|
2134 |
where t1.key1 = t2.key1 |
|
2135 |
) |
|
2136 |
) as latest_table1 on table1.key1 = latest_table1.key1 |
|
2137 |
EOS |
|
cleanup test
|
2138 |
$join = [$sql]; |
2139 |
$rows = $dbi->select( |
|
2140 |
table => 'table1', |
|
2141 |
column => 'latest_table1.key1 as latest_table1__key1', |
|
2142 |
join => $join |
|
2143 |
)->all; |
|
2144 |
is_deeply($rows, [{latest_table1__key1 => 1}]); |
|
2145 | ||
cleanup test
|
2146 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2147 |
$dbi->execute($create_table1); |
cleanup test
|
2148 |
$dbi->execute('create table table2 (key1 char(255), key3 char(255));'); |
cleanup test
|
2149 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
2150 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4}); |
|
2151 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
2152 |
$result = $dbi->select( |
|
2153 |
table => 'table1', |
|
2154 |
join => [ |
|
2155 |
"left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1" |
|
2156 |
] |
|
2157 |
); |
|
2158 |
is_deeply($result->all, [{key1 => 1, key2 => 2}]); |
|
2159 |
$result = $dbi->select( |
|
2160 |
table => 'table1', |
|
2161 |
column => [{table2 => ['key3']}], |
|
2162 |
join => [ |
|
2163 |
"left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1" |
|
2164 |
] |
|
2165 |
); |
|
2166 |
is_deeply($result->all, [{'table2.key3' => 4}]); |
|
2167 |
$result = $dbi->select( |
|
2168 |
table => 'table1', |
|
2169 |
column => [{table2 => ['key3']}], |
|
2170 |
join => [ |
|
2171 |
"left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'" |
|
2172 |
] |
|
2173 |
); |
|
2174 |
is_deeply($result->all, [{'table2.key3' => 4}]); |
|
2175 | ||
cleanup test
|
2176 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2177 |
$dbi->execute($create_table1); |
cleanup test
|
2178 |
$dbi->execute('create table table2 (key1 char(255), key3 char(255));'); |
cleanup test
|
2179 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
2180 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4}); |
|
2181 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
2182 |
$result = $dbi->select( |
|
2183 |
table => 'table1', |
|
2184 |
column => [{table2 => ['key3']}], |
|
2185 |
join => [ |
|
2186 |
{ |
|
2187 |
clause => "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1", |
|
2188 |
table => ['table1', 'table2'] |
|
2189 |
} |
|
2190 |
] |
|
2191 |
); |
|
2192 |
is_deeply($result->all, [{'table2.key3' => 4}]); |
|
2193 | ||
2194 |
test 'mycolumn'; |
|
cleanup test
|
2195 |
$dbi = MyDBI8->connect(%memory); |
cleanup test
|
2196 |
$dbi->execute($create_table1); |
cleanup test
|
2197 |
$dbi->execute('create table table2 (key1 char(255), key3 char(255));'); |
cleanup test
|
2198 |
$dbi->setup_model; |
2199 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
2200 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3}); |
|
2201 |
$model = $dbi->model('table1'); |
|
2202 |
$result = $model->select_at( |
|
2203 |
column => [ |
|
2204 |
$model->mycolumn, |
|
2205 |
$model->column('table2') |
|
2206 |
] |
|
2207 |
); |
|
2208 |
is_deeply($result->one, |
|
2209 |
{key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3}); |
|
2210 | ||
2211 |
$result = $model->select_at( |
|
2212 |
column => [ |
|
2213 |
$model->mycolumn(['key1']), |
|
2214 |
$model->column(table2 => ['key1']) |
|
2215 |
] |
|
2216 |
); |
|
2217 |
is_deeply($result->one, |
|
2218 |
{key1 => 1, 'table2.key1' => 1}); |
|
2219 |
$result = $model->select_at( |
|
2220 |
column => [ |
|
2221 |
$model->mycolumn(['key1']), |
|
2222 |
{table2 => ['key1']} |
|
2223 |
] |
|
2224 |
); |
|
2225 |
is_deeply($result->one, |
|
2226 |
{key1 => 1, 'table2.key1' => 1}); |
|
2227 | ||
2228 |
$result = $model->select_at( |
|
2229 |
column => [ |
|
2230 |
$model->mycolumn(['key1']), |
|
2231 |
['table2.key1', as => 'table2.key1'] |
|
2232 |
] |
|
2233 |
); |
|
2234 |
is_deeply($result->one, |
|
2235 |
{key1 => 1, 'table2.key1' => 1}); |
|
2236 | ||
2237 |
$result = $model->select_at( |
|
2238 |
column => [ |
|
2239 |
$model->mycolumn(['key1']), |
|
2240 |
['table2.key1' => 'table2.key1'] |
|
2241 |
] |
|
2242 |
); |
|
2243 |
is_deeply($result->one, |
|
2244 |
{key1 => 1, 'table2.key1' => 1}); |
|
2245 | ||
2246 |
test 'dbi method from model'; |
|
2247 |
{ |
|
2248 |
package MyDBI9; |
|
2249 |
|
|
2250 |
use base 'DBIx::Custom'; |
|
2251 |
|
|
2252 |
sub connect { |
|
2253 |
my $self = shift->SUPER::connect(@_); |
|
2254 |
|
|
2255 |
$self->include_model('MyModel8')->setup_model; |
|
2256 |
|
|
2257 |
return $self; |
|
2258 |
} |
|
2259 |
} |
|
cleanup test
|
2260 |
$dbi = MyDBI9->connect(%memory); |
cleanup test
|
2261 |
$dbi->execute($create_table1); |
cleanup test
|
2262 |
$model = $dbi->model('table1'); |
2263 |
eval{$model->execute('select * from table1')}; |
|
2264 |
ok(!$@); |
|
2265 | ||
2266 |
test 'column table option'; |
|
cleanup test
|
2267 |
$dbi = MyDBI9->connect(%memory); |
cleanup test
|
2268 |
$dbi->execute($create_table1); |
cleanup test
|
2269 |
$dbi->execute('create table table2 (key1 char(255), key3 char(255));'); |
cleanup test
|
2270 |
$dbi->setup_model; |
2271 |
$dbi->execute('insert into table1 (key1, key2) values (1, 2);'); |
|
2272 |
$dbi->execute('insert into table2 (key1, key3) values (1, 4);'); |
|
2273 |
$model = $dbi->model('table1'); |
|
2274 |
$result = $model->select( |
|
2275 |
column => [ |
|
2276 |
$model->column('table2', {alias => 'table2_alias'}) |
|
2277 |
], |
|
2278 |
where => {'table2_alias.key3' => 4} |
|
2279 |
); |
|
2280 |
is_deeply($result->one, |
|
2281 |
{'table2_alias.key1' => 1, 'table2_alias.key3' => 4}); |
|
2282 | ||
2283 |
$dbi->separator('__'); |
|
2284 |
$result = $model->select( |
|
2285 |
column => [ |
|
2286 |
$model->column('table2', {alias => 'table2_alias'}) |
|
2287 |
], |
|
2288 |
where => {'table2_alias.key3' => 4} |
|
2289 |
); |
|
2290 |
is_deeply($result->one, |
|
2291 |
{'table2_alias__key1' => 1, 'table2_alias__key3' => 4}); |
|
2292 | ||
2293 |
$dbi->separator('-'); |
|
2294 |
$result = $model->select( |
|
2295 |
column => [ |
|
2296 |
$model->column('table2', {alias => 'table2_alias'}) |
|
2297 |
], |
|
2298 |
where => {'table2_alias.key3' => 4} |
|
2299 |
); |
|
2300 |
is_deeply($result->one, |
|
2301 |
{'table2_alias-key1' => 1, 'table2_alias-key3' => 4}); |
|
2302 | ||
2303 |
test 'type option'; # DEPRECATED! |
|
2304 |
$dbi = DBIx::Custom->connect( |
|
2305 |
data_source => 'dbi:SQLite:dbname=:memory:', |
|
2306 |
dbi_option => { |
|
2307 |
$DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1) |
|
2308 |
} |
|
2309 |
); |
|
2310 |
my $binary = pack("I3", 1, 2, 3); |
|
2311 |
$dbi->execute('create table table1(key1, key2)'); |
|
2312 |
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]); |
|
2313 |
$result = $dbi->select(table => 'table1'); |
|
2314 |
$row = $result->one; |
|
2315 |
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic"); |
|
2316 |
$result = $dbi->execute('select length(key1) as key1_length from table1'); |
|
2317 |
$row = $result->one; |
|
2318 |
is($row->{key1_length}, length $binary); |
|
2319 | ||
2320 |
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [['key1'] => DBI::SQL_BLOB]); |
|
2321 |
$result = $dbi->select(table => 'table1'); |
|
2322 |
$row = $result->one; |
|
2323 |
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic"); |
|
2324 |
$result = $dbi->execute('select length(key1) as key1_length from table1'); |
|
2325 |
$row = $result->one; |
|
2326 |
is($row->{key1_length}, length $binary); |
|
2327 | ||
2328 | ||
2329 |
test 'bind_type option'; |
|
2330 |
$dbi = DBIx::Custom->connect( |
|
2331 |
data_source => 'dbi:SQLite:dbname=:memory:', |
|
2332 |
dbi_option => { |
|
2333 |
$DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1) |
|
2334 |
} |
|
2335 |
); |
|
2336 |
$binary = pack("I3", 1, 2, 3); |
|
2337 |
$dbi->execute('create table table1(key1, key2)'); |
|
2338 |
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, bind_type => [key1 => DBI::SQL_BLOB]); |
|
2339 |
$result = $dbi->select(table => 'table1'); |
|
2340 |
$row = $result->one; |
|
2341 |
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic"); |
|
2342 |
$result = $dbi->execute('select length(key1) as key1_length from table1'); |
|
2343 |
$row = $result->one; |
|
2344 |
is($row->{key1_length}, length $binary); |
|
2345 | ||
2346 |
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, bind_type => [['key1'] => DBI::SQL_BLOB]); |
|
2347 |
$result = $dbi->select(table => 'table1'); |
|
2348 |
$row = $result->one; |
|
2349 |
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic"); |
|
2350 |
$result = $dbi->execute('select length(key1) as key1_length from table1'); |
|
2351 |
$row = $result->one; |
|
2352 |
is($row->{key1_length}, length $binary); |
|
2353 | ||
2354 |
test 'model type attribute'; |
|
2355 |
$dbi = DBIx::Custom->connect( |
|
2356 |
data_source => 'dbi:SQLite:dbname=:memory:', |
|
2357 |
dbi_option => { |
|
2358 |
$DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1) |
|
2359 |
} |
|
2360 |
); |
|
2361 |
$binary = pack("I3", 1, 2, 3); |
|
2362 |
$dbi->execute('create table table1(key1, key2)'); |
|
2363 |
$model = $dbi->create_model(table => 'table1', bind_type => [key1 => DBI::SQL_BLOB]); |
|
2364 |
$model->insert(param => {key1 => $binary, key2 => 'あ'}); |
|
2365 |
$result = $dbi->select(table => 'table1'); |
|
2366 |
$row = $result->one; |
|
2367 |
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic"); |
|
2368 |
$result = $dbi->execute('select length(key1) as key1_length from table1'); |
|
2369 |
$row = $result->one; |
|
2370 |
is($row->{key1_length}, length $binary); |
|
2371 | ||
2372 |
test 'create_model'; |
|
cleanup test
|
2373 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2374 |
$dbi->execute($create_table1); |
cleanup test
|
2375 |
$dbi->execute('create table table2 (key1 char(255), key3 char(255));'); |
cleanup test
|
2376 | |
2377 |
$dbi->create_model( |
|
2378 |
table => 'table1', |
|
2379 |
join => [ |
|
2380 |
'left outer join table2 on table1.key1 = table2.key1' |
|
2381 |
], |
|
2382 |
primary_key => ['key1'] |
|
2383 |
); |
|
2384 |
$model2 = $dbi->create_model( |
|
2385 |
table => 'table2' |
|
2386 |
); |
|
2387 |
$dbi->create_model( |
|
2388 |
table => 'table3', |
|
2389 |
filter => [ |
|
2390 |
key1 => {in => sub { uc $_[0] }} |
|
2391 |
] |
|
2392 |
); |
|
2393 |
$dbi->setup_model; |
|
2394 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
2395 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3}); |
|
2396 |
$model = $dbi->model('table1'); |
|
2397 |
$result = $model->select( |
|
2398 |
column => [$model->mycolumn, $model->column('table2')], |
|
2399 |
where => {'table1.key1' => 1} |
|
2400 |
); |
|
2401 |
is_deeply($result->one, |
|
2402 |
{key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3}); |
|
2403 |
is_deeply($model2->select->one, {key1 => 1, key3 => 3}); |
|
2404 | ||
2405 |
test 'model method'; |
|
2406 |
test 'create_model'; |
|
cleanup test
|
2407 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2408 |
$dbi->execute('create table table2 (key1 char(255), key3 char(255));'); |
cleanup test
|
2409 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3}); |
2410 |
$model = $dbi->create_model( |
|
2411 |
table => 'table2' |
|
2412 |
); |
|
2413 |
$model->method(foo => sub { shift->select(@_) }); |
|
2414 |
is_deeply($model->foo->one, {key1 => 1, key3 => 3}); |
|
2415 | ||
2416 |
test 'merge_param'; |
|
cleanup test
|
2417 |
$dbi = DBIx::Custom->new; |
2418 |
$params = [ |
|
2419 |
{key1 => 1, key2 => 2, key3 => 3}, |
|
2420 |
{key1 => 1, key2 => 2}, |
|
2421 |
{key1 => 1} |
|
2422 |
]; |
|
2423 |
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]); |
|
2424 |
is_deeply($param, {key1 => [1, 1, 1], key2 => [2, 2], key3 => 3}); |
|
2425 | ||
2426 |
$params = [ |
|
2427 |
{key1 => [1, 2], key2 => 1, key3 => [1, 2]}, |
|
2428 |
{key1 => [3, 4], key2 => [2, 3], key3 => 3} |
|
2429 |
]; |
|
2430 |
$param = $dbi->merge_param($params->[0], $params->[1]); |
|
2431 |
is_deeply($param, {key1 => [1, 2, 3, 4], key2 => [1, 2, 3], key3 => [1, 2, 3]}); |
|
cleanup test
|
2432 | |
2433 |
test 'select() param option'; |
|
cleanup test
|
2434 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2435 |
$dbi->execute($create_table1); |
cleanup test
|
2436 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
2437 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
cleanup test
|
2438 |
$dbi->execute('create table table2 (key1 char(255), key3 char(255));'); |
cleanup test
|
2439 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4}); |
2440 |
$dbi->insert(table => 'table2', param => {key1 => 2, key3 => 5}); |
|
2441 |
$rows = $dbi->select( |
|
2442 |
table => 'table1', |
|
2443 |
column => 'table1.key1 as table1_key1, key2, key3', |
|
2444 |
where => {'table1.key2' => 3}, |
|
2445 |
join => ['inner join (select * from table2 where {= table2.key3})' . |
|
2446 |
' as table2 on table1.key1 = table2.key1'], |
|
2447 |
param => {'table2.key3' => 5} |
|
2448 |
)->all; |
|
2449 |
is_deeply($rows, [{table1_key1 => 2, key2 => 3, key3 => 5}]); |
|
2450 | ||
2451 | ||
2452 |
test 'select() wrap option'; |
|
cleanup test
|
2453 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2454 |
$dbi->execute($create_table1); |
cleanup test
|
2455 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
2456 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
2457 |
$rows = $dbi->select( |
|
2458 |
table => 'table1', |
|
2459 |
column => 'key1', |
|
2460 |
wrap => ['select * from (', ') as t where key1 = 1'] |
|
2461 |
)->all; |
|
2462 |
is_deeply($rows, [{key1 => 1}]); |
|
2463 | ||
2464 |
eval { |
|
2465 |
$dbi->select( |
|
2466 |
table => 'table1', |
|
2467 |
column => 'key1', |
|
2468 |
wrap => 'select * from (' |
|
2469 |
) |
|
2470 |
}; |
|
2471 |
like($@, qr/array/); |
|
2472 | ||
2473 |
test 'select() string where'; |
|
cleanup test
|
2474 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2475 |
$dbi->execute($create_table1); |
cleanup test
|
2476 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
2477 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
2478 |
$rows = $dbi->select( |
|
2479 |
table => 'table1', |
|
2480 |
where => 'key1 = :key1 and key2 = :key2', |
|
2481 |
where_param => {key1 => 1, key2 => 2} |
|
2482 |
)->all; |
|
2483 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
2484 | ||
cleanup test
|
2485 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2486 |
$dbi->execute($create_table1); |
cleanup test
|
2487 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
2488 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
2489 |
$rows = $dbi->select( |
|
2490 |
table => 'table1', |
|
2491 |
where => [ |
|
2492 |
'key1 = :key1 and key2 = :key2', |
|
2493 |
{key1 => 1, key2 => 2} |
|
2494 |
] |
|
2495 |
)->all; |
|
2496 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
2497 | ||
2498 |
test 'delete() string where'; |
|
cleanup test
|
2499 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2500 |
$dbi->execute($create_table1); |
cleanup test
|
2501 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
2502 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
2503 |
$dbi->delete( |
|
2504 |
table => 'table1', |
|
2505 |
where => 'key1 = :key1 and key2 = :key2', |
|
2506 |
where_param => {key1 => 1, key2 => 2} |
|
2507 |
); |
|
2508 |
$rows = $dbi->select(table => 'table1')->all; |
|
2509 |
is_deeply($rows, [{key1 => 2, key2 => 3}]); |
|
2510 | ||
cleanup test
|
2511 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2512 |
$dbi->execute($create_table1); |
cleanup test
|
2513 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
2514 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
2515 |
$dbi->delete( |
|
2516 |
table => 'table1', |
|
2517 |
where => [ |
|
2518 |
'key1 = :key1 and key2 = :key2', |
|
2519 |
{key1 => 1, key2 => 2} |
|
2520 |
] |
|
2521 |
); |
|
2522 |
$rows = $dbi->select(table => 'table1')->all; |
|
2523 |
is_deeply($rows, [{key1 => 2, key2 => 3}]); |
|
2524 | ||
2525 | ||
2526 |
test 'update() string where'; |
|
cleanup test
|
2527 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2528 |
$dbi->execute($create_table1); |
cleanup test
|
2529 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
2530 |
$dbi->update( |
|
2531 |
table => 'table1', |
|
2532 |
param => {key1 => 5}, |
|
2533 |
where => 'key1 = :key1 and key2 = :key2', |
|
2534 |
where_param => {key1 => 1, key2 => 2} |
|
2535 |
); |
|
2536 |
$rows = $dbi->select(table => 'table1')->all; |
|
2537 |
is_deeply($rows, [{key1 => 5, key2 => 2}]); |
|
2538 | ||
cleanup test
|
2539 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2540 |
$dbi->execute($create_table1); |
cleanup test
|
2541 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
2542 |
$dbi->update( |
|
2543 |
table => 'table1', |
|
2544 |
param => {key1 => 5}, |
|
2545 |
where => [ |
|
2546 |
'key1 = :key1 and key2 = :key2', |
|
2547 |
{key1 => 1, key2 => 2} |
|
2548 |
] |
|
2549 |
); |
|
2550 |
$rows = $dbi->select(table => 'table1')->all; |
|
2551 |
is_deeply($rows, [{key1 => 5, key2 => 2}]); |
|
2552 | ||
2553 |
test 'insert id and primary_key option'; |
|
cleanup test
|
2554 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2555 |
$dbi->execute($create_table1_2); |
cleanup test
|
2556 |
$dbi->insert( |
2557 |
primary_key => ['key1', 'key2'], |
|
2558 |
table => 'table1', |
|
2559 |
id => [1, 2], |
|
2560 |
param => {key3 => 3} |
|
2561 |
); |
|
2562 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
2563 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
2564 |
is($dbi->select(table => 'table1')->one->{key3}, 3); |
|
2565 | ||
2566 |
$dbi->delete_all(table => 'table1'); |
|
2567 |
$dbi->insert( |
|
2568 |
primary_key => 'key1', |
|
2569 |
table => 'table1', |
|
2570 |
id => 0, |
|
2571 |
param => {key2 => 2, key3 => 3} |
|
2572 |
); |
|
2573 | ||
2574 |
is($dbi->select(table => 'table1')->one->{key1}, 0); |
|
2575 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
2576 |
is($dbi->select(table => 'table1')->one->{key3}, 3); |
|
2577 | ||
cleanup test
|
2578 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2579 |
$dbi->execute($create_table1_2); |
cleanup test
|
2580 |
$dbi->insert( |
2581 |
{key3 => 3}, |
|
2582 |
primary_key => ['key1', 'key2'], |
|
2583 |
table => 'table1', |
|
2584 |
id => [1, 2], |
|
2585 |
); |
|
2586 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
2587 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
2588 |
is($dbi->select(table => 'table1')->one->{key3}, 3); |
|
2589 | ||
2590 | ||
2591 |
test 'model insert id and primary_key option'; |
|
cleanup test
|
2592 |
$dbi = MyDBI6->connect(%memory); |
cleanup test
|
2593 |
$dbi->execute($create_table1_2); |
cleanup test
|
2594 |
$dbi->model('table1')->insert( |
2595 |
id => [1, 2], |
|
2596 |
param => {key3 => 3} |
|
2597 |
); |
|
2598 |
$result = $dbi->model('table1')->select; |
|
2599 |
$row = $result->one; |
|
2600 |
is($row->{key1}, 1); |
|
2601 |
is($row->{key2}, 2); |
|
2602 |
is($row->{key3}, 3); |
|
2603 | ||
cleanup test
|
2604 |
$dbi = MyDBI6->connect(%memory); |
cleanup test
|
2605 |
$dbi->execute($create_table1_2); |
cleanup test
|
2606 |
$dbi->model('table1')->insert( |
2607 |
{key3 => 3}, |
|
2608 |
id => [1, 2] |
|
2609 |
); |
|
2610 |
$result = $dbi->model('table1')->select; |
|
2611 |
$row = $result->one; |
|
2612 |
is($row->{key1}, 1); |
|
2613 |
is($row->{key2}, 2); |
|
2614 |
is($row->{key3}, 3); |
|
2615 | ||
2616 |
test 'update and id option'; |
|
cleanup test
|
2617 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2618 |
$dbi->execute($create_table1_2); |
cleanup test
|
2619 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
2620 |
$dbi->update( |
|
2621 |
table => 'table1', |
|
2622 |
primary_key => ['key1', 'key2'], |
|
2623 |
id => [1, 2], |
|
2624 |
param => {key3 => 4} |
|
2625 |
); |
|
2626 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
2627 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
2628 |
is($dbi->select(table => 'table1')->one->{key3}, 4); |
|
2629 | ||
2630 |
$dbi->delete_all(table => 'table1'); |
|
2631 |
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3}); |
|
2632 |
$dbi->update( |
|
2633 |
table => 'table1', |
|
2634 |
primary_key => 'key1', |
|
2635 |
id => 0, |
|
2636 |
param => {key3 => 4} |
|
2637 |
); |
|
2638 |
is($dbi->select(table => 'table1')->one->{key1}, 0); |
|
2639 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
2640 |
is($dbi->select(table => 'table1')->one->{key3}, 4); |
|
2641 | ||
cleanup test
|
2642 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2643 |
$dbi->execute($create_table1_2); |
cleanup test
|
2644 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
2645 |
$dbi->update( |
|
2646 |
{key3 => 4}, |
|
2647 |
table => 'table1', |
|
2648 |
primary_key => ['key1', 'key2'], |
|
2649 |
id => [1, 2] |
|
2650 |
); |
|
2651 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
2652 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
2653 |
is($dbi->select(table => 'table1')->one->{key3}, 4); |
|
2654 | ||
2655 | ||
2656 |
test 'model update and id option'; |
|
cleanup test
|
2657 |
$dbi = MyDBI6->connect(%memory); |
cleanup test
|
2658 |
$dbi->execute($create_table1_2); |
cleanup test
|
2659 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
2660 |
$dbi->model('table1')->update( |
|
2661 |
id => [1, 2], |
|
2662 |
param => {key3 => 4} |
|
2663 |
); |
|
2664 |
$result = $dbi->model('table1')->select; |
|
2665 |
$row = $result->one; |
|
2666 |
is($row->{key1}, 1); |
|
2667 |
is($row->{key2}, 2); |
|
2668 |
is($row->{key3}, 4); |
|
2669 | ||
2670 | ||
2671 |
test 'delete and id option'; |
|
cleanup test
|
2672 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2673 |
$dbi->execute($create_table1_2); |
cleanup test
|
2674 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
2675 |
$dbi->delete( |
|
2676 |
table => 'table1', |
|
2677 |
primary_key => ['key1', 'key2'], |
|
2678 |
id => [1, 2], |
|
2679 |
); |
|
2680 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
2681 | ||
2682 |
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3}); |
|
2683 |
$dbi->delete( |
|
2684 |
table => 'table1', |
|
2685 |
primary_key => 'key1', |
|
2686 |
id => 0, |
|
2687 |
); |
|
2688 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
2689 | ||
2690 | ||
2691 |
test 'model delete and id option'; |
|
cleanup test
|
2692 |
$dbi = MyDBI6->connect(%memory); |
cleanup test
|
2693 |
$dbi->execute($create_table1_2); |
cleanup test
|
2694 |
$dbi->execute("create table table2 (key1, key2, key3)"); |
2695 |
$dbi->execute("create table table3 (key1, key2, key3)"); |
|
2696 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
2697 |
$dbi->model('table1')->delete(id => [1, 2]); |
|
2698 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
2699 |
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
2700 |
$dbi->model('table1_1')->delete(id => [1, 2]); |
|
2701 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
2702 |
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
2703 |
$dbi->model('table1_3')->delete(id => [1, 2]); |
|
2704 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
2705 | ||
2706 | ||
2707 |
test 'select and id option'; |
|
cleanup test
|
2708 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2709 |
$dbi->execute($create_table1_2); |
cleanup test
|
2710 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
2711 |
$result = $dbi->select( |
|
2712 |
table => 'table1', |
|
2713 |
primary_key => ['key1', 'key2'], |
|
2714 |
id => [1, 2] |
|
2715 |
); |
|
2716 |
$row = $result->one; |
|
2717 |
is($row->{key1}, 1); |
|
2718 |
is($row->{key2}, 2); |
|
2719 |
is($row->{key3}, 3); |
|
2720 | ||
2721 |
$dbi->delete_all(table => 'table1'); |
|
2722 |
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3}); |
|
2723 |
$result = $dbi->select( |
|
2724 |
table => 'table1', |
|
2725 |
primary_key => 'key1', |
|
2726 |
id => 0, |
|
2727 |
); |
|
2728 |
$row = $result->one; |
|
2729 |
is($row->{key1}, 0); |
|
2730 |
is($row->{key2}, 2); |
|
2731 |
is($row->{key3}, 3); |
|
2732 | ||
2733 |
$dbi->delete_all(table => 'table1'); |
|
2734 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
2735 |
$result = $dbi->select( |
|
2736 |
table => 'table1', |
|
2737 |
primary_key => ['key1', 'key2'], |
|
2738 |
id => [1, 2] |
|
2739 |
); |
|
2740 |
$row = $result->one; |
|
2741 |
is($row->{key1}, 1); |
|
2742 |
is($row->{key2}, 2); |
|
2743 |
is($row->{key3}, 3); |
|
2744 | ||
2745 | ||
2746 |
test 'model select_at'; |
|
cleanup test
|
2747 |
$dbi = MyDBI6->connect(%memory); |
cleanup test
|
2748 |
$dbi->execute($create_table1_2); |
cleanup test
|
2749 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
2750 |
$result = $dbi->model('table1')->select(id => [1, 2]); |
|
2751 |
$row = $result->one; |
|
2752 |
is($row->{key1}, 1); |
|
2753 |
is($row->{key2}, 2); |
|
2754 |
is($row->{key3}, 3); |
|
2755 | ||
2756 |
test 'column separator is default .'; |
|
cleanup test
|
2757 |
$dbi = MyDBI7->connect(%memory); |
cleanup test
|
2758 |
$dbi->execute($create_table1); |
cleanup test
|
2759 |
$dbi->execute('create table table2 (key1 char(255), key3 char(255));'); |
cleanup test
|
2760 |
$dbi->setup_model; |
2761 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
2762 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3}); |
|
2763 |
$model = $dbi->model('table1'); |
|
2764 |
$result = $model->select( |
|
2765 |
column => [$model->column('table2')], |
|
2766 |
where => {'table1.key1' => 1} |
|
2767 |
); |
|
2768 |
is_deeply($result->one, |
|
2769 |
{'table2.key1' => 1, 'table2.key3' => 3}); |
|
2770 | ||
2771 |
$result = $model->select( |
|
2772 |
column => [$model->column('table2' => [qw/key1 key3/])], |
|
2773 |
where => {'table1.key1' => 1} |
|
2774 |
); |
|
2775 |
is_deeply($result->one, |
|
2776 |
{'table2.key1' => 1, 'table2.key3' => 3}); |
|
2777 | ||
2778 | ||
2779 |
test 'type_rule from'; |
|
cleanup test
|
2780 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2781 |
$dbi->type_rule( |
2782 |
from1 => { |
|
2783 |
date => sub { uc $_[0] } |
|
2784 |
} |
|
2785 |
); |
|
2786 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
2787 |
$dbi->insert({key1 => 'a'}, table => 'table1'); |
|
2788 |
$result = $dbi->select(table => 'table1'); |
|
2789 |
is($result->fetch_first->[0], 'A'); |
|
2790 | ||
2791 |
$result = $dbi->select(table => 'table1'); |
|
2792 |
is($result->one->{key1}, 'A'); |
|
2793 | ||
2794 | ||
2795 |
test 'type_rule into'; |
|
cleanup test
|
2796 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2797 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
2798 |
$dbi->type_rule( |
|
2799 |
into1 => { |
|
2800 |
date => sub { uc $_[0] } |
|
2801 |
} |
|
2802 |
); |
|
2803 |
$dbi->insert({key1 => 'a'}, table => 'table1'); |
|
2804 |
$result = $dbi->select(table => 'table1'); |
|
2805 |
is($result->one->{key1}, 'A'); |
|
2806 | ||
cleanup test
|
2807 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2808 |
$dbi->execute("create table table1 (key1 date, key2 datetime)"); |
2809 |
$dbi->type_rule( |
|
2810 |
into1 => [ |
|
2811 |
[qw/date datetime/] => sub { uc $_[0] } |
|
2812 |
] |
|
2813 |
); |
|
2814 |
$dbi->insert({key1 => 'a', key2 => 'b'}, table => 'table1'); |
|
2815 |
$result = $dbi->select(table => 'table1'); |
|
2816 |
$row = $result->one; |
|
2817 |
is($row->{key1}, 'A'); |
|
2818 |
is($row->{key2}, 'B'); |
|
2819 | ||
cleanup test
|
2820 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2821 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
2822 |
$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1'); |
|
2823 |
$dbi->type_rule( |
|
2824 |
into1 => [ |
|
2825 |
[qw/date datetime/] => sub { uc $_[0] } |
|
2826 |
] |
|
2827 |
); |
|
2828 |
$result = $dbi->execute( |
|
2829 |
"select * from table1 where key1 = :key1 and key2 = :table1.key2;", |
|
2830 |
param => {key1 => 'a', 'table1.key2' => 'b'} |
|
2831 |
); |
|
2832 |
$row = $result->one; |
|
2833 |
is($row->{key1}, 'a'); |
|
2834 |
is($row->{key2}, 'B'); |
|
2835 | ||
cleanup test
|
2836 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2837 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
2838 |
$dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1'); |
|
2839 |
$dbi->type_rule( |
|
2840 |
into1 => [ |
|
2841 |
[qw/date datetime/] => sub { uc $_[0] } |
|
2842 |
] |
|
2843 |
); |
|
2844 |
$result = $dbi->execute( |
|
2845 |
"select * from table1 where key1 = :key1 and key2 = :table1.key2;", |
|
2846 |
param => {key1 => 'a', 'table1.key2' => 'b'}, |
|
2847 |
table => 'table1' |
|
2848 |
); |
|
2849 |
$row = $result->one; |
|
2850 |
is($row->{key1}, 'A'); |
|
2851 |
is($row->{key2}, 'B'); |
|
2852 | ||
cleanup test
|
2853 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2854 |
$dbi->execute("create table table1 (key1 date, key2 datetime)"); |
2855 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
2856 |
$dbi->type_rule( |
|
2857 |
from1 => { |
|
2858 |
date => 'twice', |
|
2859 |
}, |
|
2860 |
into1 => { |
|
2861 |
date => 'twice', |
|
2862 |
} |
|
2863 |
); |
|
2864 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
2865 |
$result = $dbi->select(table => 'table1'); |
|
2866 |
is($result->fetch->[0], 8); |
|
2867 | ||
2868 |
test 'type_rule and filter order'; |
|
cleanup test
|
2869 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2870 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
2871 |
$dbi->type_rule( |
|
2872 |
into1 => { |
|
2873 |
date => sub { $_[0] . 'b' } |
|
2874 |
}, |
|
2875 |
into2 => { |
|
2876 |
date => sub { $_[0] . 'c' } |
|
2877 |
}, |
|
2878 |
from1 => { |
|
2879 |
date => sub { $_[0] . 'd' } |
|
2880 |
}, |
|
2881 |
from2 => { |
|
2882 |
date => sub { $_[0] . 'e' } |
|
2883 |
} |
|
2884 |
); |
|
2885 |
$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }}); |
|
2886 |
$result = $dbi->select(table => 'table1'); |
|
2887 |
$result->filter(key1 => sub { $_[0] . 'f' }); |
|
2888 |
is($result->fetch_first->[0], '1abcdef'); |
|
2889 | ||
cleanup test
|
2890 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2891 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
2892 |
$dbi->type_rule( |
|
2893 |
from1 => { |
|
2894 |
date => sub { $_[0] . 'p' } |
|
2895 |
}, |
|
2896 |
from2 => { |
|
2897 |
date => sub { $_[0] . 'q' } |
|
2898 |
}, |
|
2899 |
); |
|
2900 |
$dbi->insert({key1 => '1'}, table => 'table1'); |
|
2901 |
$result = $dbi->select(table => 'table1'); |
|
2902 |
$result->type_rule( |
|
2903 |
from1 => { |
|
2904 |
date => sub { $_[0] . 'd' } |
|
2905 |
}, |
|
2906 |
from2 => { |
|
2907 |
date => sub { $_[0] . 'e' } |
|
2908 |
} |
|
2909 |
); |
|
2910 |
$result->filter(key1 => sub { $_[0] . 'f' }); |
|
2911 |
is($result->fetch_first->[0], '1def'); |
|
2912 | ||
2913 |
test 'type_rule_off'; |
|
cleanup test
|
2914 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2915 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
2916 |
$dbi->type_rule( |
|
2917 |
from1 => { |
|
2918 |
date => sub { $_[0] * 2 }, |
|
2919 |
}, |
|
2920 |
into1 => { |
|
2921 |
date => sub { $_[0] * 2 }, |
|
2922 |
} |
|
2923 |
); |
|
2924 |
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1); |
|
2925 |
$result = $dbi->select(table => 'table1', type_rule_off => 1); |
|
2926 |
is($result->type_rule_off->fetch->[0], 2); |
|
2927 | ||
cleanup test
|
2928 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2929 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
2930 |
$dbi->type_rule( |
|
2931 |
from1 => { |
|
2932 |
date => sub { $_[0] * 2 }, |
|
2933 |
}, |
|
2934 |
into1 => { |
|
2935 |
date => sub { $_[0] * 3 }, |
|
2936 |
} |
|
2937 |
); |
|
2938 |
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1); |
|
2939 |
$result = $dbi->select(table => 'table1', type_rule_off => 1); |
|
2940 |
is($result->one->{key1}, 4); |
|
2941 | ||
cleanup test
|
2942 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2943 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
2944 |
$dbi->type_rule( |
|
2945 |
from1 => { |
|
2946 |
date => sub { $_[0] * 2 }, |
|
2947 |
}, |
|
2948 |
into1 => { |
|
2949 |
date => sub { $_[0] * 3 }, |
|
2950 |
} |
|
2951 |
); |
|
2952 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
2953 |
$result = $dbi->select(table => 'table1'); |
|
2954 |
is($result->one->{key1}, 12); |
|
2955 | ||
cleanup test
|
2956 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2957 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
2958 |
$dbi->type_rule( |
|
2959 |
from1 => { |
|
2960 |
date => sub { $_[0] * 2 }, |
|
2961 |
}, |
|
2962 |
into1 => { |
|
2963 |
date => sub { $_[0] * 3 }, |
|
2964 |
} |
|
2965 |
); |
|
2966 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
2967 |
$result = $dbi->select(table => 'table1'); |
|
2968 |
is($result->fetch->[0], 12); |
|
2969 | ||
cleanup test
|
2970 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2971 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
2972 |
$dbi->register_filter(ppp => sub { uc $_[0] }); |
|
2973 |
$dbi->type_rule( |
|
2974 |
into1 => { |
|
2975 |
date => 'ppp' |
|
2976 |
} |
|
2977 |
); |
|
2978 |
$dbi->insert({key1 => 'a'}, table => 'table1'); |
|
2979 |
$result = $dbi->select(table => 'table1'); |
|
2980 |
is($result->one->{key1}, 'A'); |
|
2981 | ||
2982 |
eval{$dbi->type_rule( |
|
2983 |
into1 => { |
|
2984 |
date => 'pp' |
|
2985 |
} |
|
2986 |
)}; |
|
2987 |
like($@, qr/not registered/); |
|
2988 | ||
cleanup test
|
2989 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
2990 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
2991 |
eval { |
|
2992 |
$dbi->type_rule( |
|
2993 |
from1 => { |
|
2994 |
Date => sub { $_[0] * 2 }, |
|
2995 |
} |
|
2996 |
); |
|
2997 |
}; |
|
2998 |
like($@, qr/lower/); |
|
2999 | ||
3000 |
eval { |
|
3001 |
$dbi->type_rule( |
|
3002 |
into1 => { |
|
3003 |
Date => sub { $_[0] * 2 }, |
|
3004 |
} |
|
3005 |
); |
|
3006 |
}; |
|
3007 |
like($@, qr/lower/); |
|
3008 | ||
cleanup test
|
3009 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
3010 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
3011 |
$dbi->type_rule( |
|
3012 |
from1 => { |
|
3013 |
date => sub { $_[0] * 2 }, |
|
3014 |
}, |
|
3015 |
into1 => { |
|
3016 |
date => sub { $_[0] * 3 }, |
|
3017 |
} |
|
3018 |
); |
|
3019 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
3020 |
$result = $dbi->select(table => 'table1'); |
|
3021 |
$result->type_rule_off; |
|
3022 |
is($result->one->{key1}, 6); |
|
3023 | ||
cleanup test
|
3024 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
3025 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
3026 |
$dbi->type_rule( |
|
3027 |
from1 => { |
|
3028 |
date => sub { $_[0] * 2 }, |
|
3029 |
datetime => sub { $_[0] * 4 }, |
|
3030 |
}, |
|
3031 |
); |
|
3032 |
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1'); |
|
3033 |
$result = $dbi->select(table => 'table1'); |
|
3034 |
$result->type_rule( |
|
3035 |
from1 => { |
|
3036 |
date => sub { $_[0] * 3 } |
|
3037 |
} |
|
3038 |
); |
|
3039 |
$row = $result->one; |
|
3040 |
is($row->{key1}, 6); |
|
3041 |
is($row->{key2}, 2); |
|
3042 | ||
3043 |
$result = $dbi->select(table => 'table1'); |
|
3044 |
$result->type_rule( |
|
3045 |
from1 => { |
|
3046 |
date => sub { $_[0] * 3 } |
|
3047 |
} |
|
3048 |
); |
|
3049 |
$row = $result->one; |
|
3050 |
is($row->{key1}, 6); |
|
3051 |
is($row->{key2}, 2); |
|
3052 | ||
3053 |
$result = $dbi->select(table => 'table1'); |
|
3054 |
$result->type_rule( |
|
3055 |
from1 => { |
|
3056 |
date => sub { $_[0] * 3 } |
|
3057 |
} |
|
3058 |
); |
|
3059 |
$row = $result->one; |
|
3060 |
is($row->{key1}, 6); |
|
3061 |
is($row->{key2}, 2); |
|
3062 |
$result = $dbi->select(table => 'table1'); |
|
3063 |
$result->type_rule( |
|
3064 |
from1 => [date => sub { $_[0] * 3 }] |
|
3065 |
); |
|
3066 |
$row = $result->one; |
|
3067 |
is($row->{key1}, 6); |
|
3068 |
is($row->{key2}, 2); |
|
3069 |
$dbi->register_filter(fivetimes => sub { $_[0] * 5}); |
|
3070 |
$result = $dbi->select(table => 'table1'); |
|
3071 |
$result->type_rule( |
|
3072 |
from1 => [date => 'fivetimes'] |
|
3073 |
); |
|
3074 |
$row = $result->one; |
|
3075 |
is($row->{key1}, 10); |
|
3076 |
is($row->{key2}, 2); |
|
3077 |
$result = $dbi->select(table => 'table1'); |
|
3078 |
$result->type_rule( |
|
3079 |
from1 => [date => undef] |
|
3080 |
); |
|
3081 |
$row = $result->one; |
|
3082 |
is($row->{key1}, 2); |
|
3083 |
is($row->{key2}, 2); |
|
3084 | ||
cleanup test
|
3085 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
3086 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
3087 |
$dbi->type_rule( |
|
3088 |
from1 => { |
|
3089 |
date => sub { $_[0] * 2 }, |
|
3090 |
}, |
|
3091 |
); |
|
3092 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
3093 |
$result = $dbi->select(table => 'table1'); |
|
3094 |
$result->filter(key1 => sub { $_[0] * 3 }); |
|
3095 |
is($result->one->{key1}, 12); |
|
3096 | ||
cleanup test
|
3097 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
3098 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
3099 |
$dbi->type_rule( |
|
3100 |
from1 => { |
|
3101 |
date => sub { $_[0] * 2 }, |
|
3102 |
}, |
|
3103 |
); |
|
3104 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
3105 |
$result = $dbi->select(table => 'table1'); |
|
3106 |
$result->filter(key1 => sub { $_[0] * 3 }); |
|
3107 |
is($result->fetch->[0], 12); |
|
3108 | ||
cleanup test
|
3109 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
3110 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
3111 |
$dbi->type_rule( |
|
3112 |
into1 => { |
|
3113 |
date => sub { $_[0] . 'b' } |
|
3114 |
}, |
|
3115 |
into2 => { |
|
3116 |
date => sub { $_[0] . 'c' } |
|
3117 |
}, |
|
3118 |
from1 => { |
|
3119 |
date => sub { $_[0] . 'd' } |
|
3120 |
}, |
|
3121 |
from2 => { |
|
3122 |
date => sub { $_[0] . 'e' } |
|
3123 |
} |
|
3124 |
); |
|
3125 |
$dbi->insert({key1 => '1'}, table => 'table1', type_rule_off => 1); |
|
3126 |
$result = $dbi->select(table => 'table1'); |
|
3127 |
is($result->type_rule_off->fetch_first->[0], '1'); |
|
3128 |
$result = $dbi->select(table => 'table1'); |
|
3129 |
is($result->type_rule_on->fetch_first->[0], '1de'); |
|
3130 | ||
cleanup test
|
3131 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
3132 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
3133 |
$dbi->type_rule( |
|
3134 |
into1 => { |
|
3135 |
date => sub { $_[0] . 'b' } |
|
3136 |
}, |
|
3137 |
into2 => { |
|
3138 |
date => sub { $_[0] . 'c' } |
|
3139 |
}, |
|
3140 |
from1 => { |
|
3141 |
date => sub { $_[0] . 'd' } |
|
3142 |
}, |
|
3143 |
from2 => { |
|
3144 |
date => sub { $_[0] . 'e' } |
|
3145 |
} |
|
3146 |
); |
|
3147 |
$dbi->insert({key1 => '1'}, table => 'table1', type_rule1_off => 1); |
|
3148 |
$result = $dbi->select(table => 'table1'); |
|
3149 |
is($result->type_rule1_off->fetch_first->[0], '1ce'); |
|
3150 |
$result = $dbi->select(table => 'table1'); |
|
3151 |
is($result->type_rule1_on->fetch_first->[0], '1cde'); |
|
3152 | ||
cleanup test
|
3153 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
3154 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
3155 |
$dbi->type_rule( |
|
3156 |
into1 => { |
|
3157 |
date => sub { $_[0] . 'b' } |
|
3158 |
}, |
|
3159 |
into2 => { |
|
3160 |
date => sub { $_[0] . 'c' } |
|
3161 |
}, |
|
3162 |
from1 => { |
|
3163 |
date => sub { $_[0] . 'd' } |
|
3164 |
}, |
|
3165 |
from2 => { |
|
3166 |
date => sub { $_[0] . 'e' } |
|
3167 |
} |
|
3168 |
); |
|
3169 |
$dbi->insert({key1 => '1'}, table => 'table1', type_rule2_off => 1); |
|
3170 |
$result = $dbi->select(table => 'table1'); |
|
3171 |
is($result->type_rule2_off->fetch_first->[0], '1bd'); |
|
3172 |
$result = $dbi->select(table => 'table1'); |
|
3173 |
is($result->type_rule2_on->fetch_first->[0], '1bde'); |
|
3174 | ||
3175 |
test 'separator'; |
|
cleanup test
|
3176 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
3177 |
$dbi->execute($create_table1); |
cleanup test
|
3178 |
$dbi->execute('create table table2 (key1 char(255), key3 char(255));'); |
cleanup test
|
3179 | |
3180 |
$dbi->create_model( |
|
3181 |
table => 'table1', |
|
3182 |
join => [ |
|
3183 |
'left outer join table2 on table1.key1 = table2.key1' |
|
3184 |
], |
|
3185 |
primary_key => ['key1'], |
|
3186 |
); |
|
3187 |
$model2 = $dbi->create_model( |
|
3188 |
table => 'table2', |
|
3189 |
); |
|
3190 |
$dbi->setup_model; |
|
3191 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
3192 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3}); |
|
3193 |
$model = $dbi->model('table1'); |
|
3194 |
$result = $model->select( |
|
3195 |
column => [ |
|
3196 |
$model->mycolumn, |
|
3197 |
{table2 => [qw/key1 key3/]} |
|
3198 |
], |
|
3199 |
where => {'table1.key1' => 1} |
|
3200 |
); |
|
3201 |
is_deeply($result->one, |
|
3202 |
{key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3}); |
|
3203 |
is_deeply($model2->select->one, {key1 => 1, key3 => 3}); |
|
3204 | ||
3205 |
$dbi->separator('__'); |
|
3206 |
$model = $dbi->model('table1'); |
|
3207 |
$result = $model->select( |
|
3208 |
column => [ |
|
3209 |
$model->mycolumn, |
|
3210 |
{table2 => [qw/key1 key3/]} |
|
3211 |
], |
|
3212 |
where => {'table1.key1' => 1} |
|
3213 |
); |
|
3214 |
is_deeply($result->one, |
|
3215 |
{key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3}); |
|
3216 |
is_deeply($model2->select->one, {key1 => 1, key3 => 3}); |
|
3217 | ||
3218 |
$dbi->separator('-'); |
|
3219 |
$model = $dbi->model('table1'); |
|
3220 |
$result = $model->select( |
|
3221 |
column => [ |
|
3222 |
$model->mycolumn, |
|
3223 |
{table2 => [qw/key1 key3/]} |
|
3224 |
], |
|
3225 |
where => {'table1.key1' => 1} |
|
3226 |
); |
|
3227 |
is_deeply($result->one, |
|
3228 |
{key1 => 1, key2 => 2, 'table2-key1' => 1, 'table2-key3' => 3}); |
|
3229 |
is_deeply($model2->select->one, {key1 => 1, key3 => 3}); |
|
3230 | ||
3231 | ||
3232 |
test 'filter_off'; |
|
cleanup test
|
3233 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
3234 |
$dbi->execute($create_table1); |
cleanup test
|
3235 |
$dbi->execute('create table table2 (key1 char(255), key3 char(255));'); |
cleanup test
|
3236 | |
3237 |
$dbi->create_model( |
|
3238 |
table => 'table1', |
|
3239 |
join => [ |
|
3240 |
'left outer join table2 on table1.key1 = table2.key1' |
|
3241 |
], |
|
3242 |
primary_key => ['key1'], |
|
3243 |
); |
|
3244 |
$dbi->setup_model; |
|
3245 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
3246 |
$model = $dbi->model('table1'); |
|
3247 |
$result = $model->select(column => 'key1'); |
|
3248 |
$result->filter(key1 => sub { $_[0] * 2 }); |
|
3249 |
is_deeply($result->one, {key1 => 2}); |
|
3250 | ||
added EXPERIMENTAL DBIx::Cus...
|
3251 |
test 'available_datetype'; |
cleanup test
|
3252 |
$dbi = DBIx::Custom->connect(%memory); |
added EXPERIMENTAL DBIx::Cus...
|
3253 |
ok($dbi->can('available_datatype')); |
cleanup test
|
3254 | |
3255 | ||
3256 |
test 'select prefix option'; |
|
cleanup test
|
3257 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
3258 |
$dbi->execute($create_table1); |
cleanup test
|
3259 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
3260 |
$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all; |
|
3261 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "table"); |
|
3262 | ||
3263 | ||
3264 |
test 'separator'; |
|
cleanup test
|
3265 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
3266 |
is($dbi->separator, '.'); |
3267 |
$dbi->separator('-'); |
|
3268 |
is($dbi->separator, '-'); |
|
3269 |
$dbi->separator('__'); |
|
3270 |
is($dbi->separator, '__'); |
|
3271 |
eval { $dbi->separator('?') }; |
|
3272 |
like($@, qr/Separator/); |
|
3273 | ||
3274 | ||
3275 |
test 'map_param'; |
|
cleanup test
|
3276 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
3277 |
$param = $dbi->map_param( |
3278 |
{id => 1, author => 'Ken', price => 1900}, |
|
3279 |
id => 'book.id', |
|
3280 |
author => ['book.author', sub { '%' . $_[0] . '%' }], |
|
3281 |
price => ['book.price', {if => sub { $_[0] eq 1900 }}] |
|
3282 |
); |
|
3283 |
is_deeply($param, {'book.id' => 1, 'book.author' => '%Ken%', |
|
3284 |
'book.price' => 1900}); |
|
3285 | ||
3286 |
$param = $dbi->map_param( |
|
3287 |
{id => 0, author => 0, price => 0}, |
|
3288 |
id => 'book.id', |
|
3289 |
author => ['book.author', sub { '%' . $_[0] . '%' }], |
|
3290 |
price => ['book.price', sub { '%' . $_[0] . '%' }, |
|
3291 |
{if => sub { $_[0] eq 0 }}] |
|
3292 |
); |
|
3293 |
is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'}); |
|
3294 | ||
3295 |
$param = $dbi->map_param( |
|
3296 |
{id => '', author => '', price => ''}, |
|
3297 |
id => 'book.id', |
|
3298 |
author => ['book.author', sub { '%' . $_[0] . '%' }], |
|
3299 |
price => ['book.price', sub { '%' . $_[0] . '%' }, |
|
3300 |
{if => sub { $_[0] eq 1 }}] |
|
3301 |
); |
|
3302 |
is_deeply($param, {}); |
|
3303 | ||
3304 |
$param = $dbi->map_param( |
|
3305 |
{id => undef, author => undef, price => undef}, |
|
3306 |
id => 'book.id', |
|
3307 |
price => ['book.price', {if => 'exists'}] |
|
3308 |
); |
|
3309 |
is_deeply($param, {'book.price' => undef}); |
|
3310 | ||
3311 |
$param = $dbi->map_param( |
|
3312 |
{price => 'a'}, |
|
3313 |
id => ['book.id', {if => 'exists'}], |
|
3314 |
price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}] |
|
3315 |
); |
|
3316 |
is_deeply($param, {'book.price' => '%a'}); |
|
3317 | ||
3318 | ||
3319 |
test 'table_alias'; |
|
cleanup test
|
3320 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
3321 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
3322 |
$dbi->type_rule( |
|
3323 |
into1 => { |
|
3324 |
date => sub { uc $_[0] } |
|
3325 |
} |
|
3326 |
); |
|
3327 |
$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'}, |
|
3328 |
table_alias => {table2 => 'table1'}); |
|
3329 |
$result = $dbi->select(table => 'table1'); |
|
3330 |
is($result->one->{key1}, 'A'); |
|
3331 | ||
3332 | ||
3333 |
test 'order'; |
|
cleanup test
|
3334 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
3335 |
$dbi->execute("create table table1 (key1, key2)"); |
3336 |
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1'); |
|
3337 |
$dbi->insert({key1 => 1, key2 => 3}, table => 'table1'); |
|
3338 |
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1'); |
|
3339 |
$dbi->insert({key1 => 2, key2 => 4}, table => 'table1'); |
|
3340 |
my $order = $dbi->order; |
|
3341 |
$order->prepend('key1', 'key2 desc'); |
|
3342 |
$result = $dbi->select(table => 'table1', append => "$order"); |
|
3343 |
is_deeply($result->all, [{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}, |
|
3344 |
{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}]); |
|
3345 |
$order->prepend('key1 desc'); |
|
3346 |
$result = $dbi->select(table => 'table1', append => "$order"); |
|
3347 |
is_deeply($result->all, [{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}, |
|
3348 |
{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}]); |
|
3349 | ||
3350 |
$order = $dbi->order; |
|
3351 |
$order->prepend(['table1-key1'], [qw/table1-key2 desc/]); |
|
3352 |
$result = $dbi->select(table => 'table1', |
|
3353 |
column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']], |
|
3354 |
append => "$order"); |
|
3355 |
is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3}, |
|
3356 |
{'table1-key1' => 1, 'table1-key2' => 1}, |
|
3357 |
{'table1-key1' => 2, 'table1-key2' => 4}, |
|
3358 |
{'table1-key1' => 2, 'table1-key2' => 2}]); |
|
cleanup test
|
3359 | |
3360 |
test 'tag_parse'; |
|
cleanup test
|
3361 |
$dbi = DBIx::Custom->connect(%memory); |
cleanup test
|
3362 |
$dbi->tag_parse(0); |
cleanup test
|
3363 |
$dbi->execute("create table table1 (key1, key2)"); |
3364 |
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1'); |
|
3365 |
eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})}; |
|
3366 |
ok($@); |
|
cleanup test
|
3367 | |
3368 |
test 'last_sql'; |
|
cleanup test
|
3369 |
$dbi = DBIx::Custom->connect(%memory); |
3370 |
$dbi->execute("create table table1 (key1, key2)"); |
|
3371 |
$dbi->execute('select * from table1'); |
|
3372 |
is($dbi->last_sql, 'select * from table1;'); |
|
3373 | ||
3374 |
eval{$dbi->execute("aaa")}; |
|
3375 |
is($dbi->last_sql, 'aaa;'); |
|
cleanup test
|
3376 | |
3377 |
test 'DBIx::Custom header'; |
|
cleanup test
|
3378 |
$dbi = DBIx::Custom->connect(%memory); |
3379 |
$dbi->execute("create table table1 (key1, key2)"); |
|
3380 |
$result = $dbi->execute('select key1 as h1, key2 as h2 from table1'); |
|
3381 |
is_deeply($result->header, [qw/h1 h2/]); |
|
cleanup test
|
3382 | |
3383 |
test 'Named placeholder :name(operater) syntax'; |
|
cleanup test
|
3384 |
$dbi->execute('drop table table1'); |
cleanup test
|
3385 |
$dbi->execute($create_table1_2); |
cleanup test
|
3386 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
3387 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
3388 | ||
3389 |
$source = "select * from table1 where :key1{=} and :key2{=}"; |
|
3390 |
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2}); |
|
3391 |
$rows = $result->all; |
|
3392 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]); |
|
3393 | ||
3394 |
$source = "select * from table1 where :key1{ = } and :key2{=}"; |
|
3395 |
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2}); |
|
3396 |
$rows = $result->all; |
|
3397 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]); |
|
3398 | ||
3399 |
$source = "select * from table1 where :key1{<} and :key2{=}"; |
|
3400 |
$result = $dbi->execute($source, param => {key1 => 5, key2 => 2}); |
|
3401 |
$rows = $result->all; |
|
3402 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]); |
|
3403 | ||
3404 |
$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}"; |
|
3405 |
$result = $dbi->execute( |
|
3406 |
$source, |
|
3407 |
param => {'table1.key1' => 1, 'table1.key2' => 1}, |
|
3408 |
filter => {'table1.key2' => sub { $_[0] * 2 }} |
|
3409 |
); |
|
3410 |
$rows = $result->all; |
|
3411 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]); |
|
3412 | ||
3413 |
test 'high perfomance way'; |
|
cleanup test
|
3414 |
$dbi->execute('drop table table1'); |
cleanup test
|
3415 |
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);"); |
3416 |
$rows = [ |
|
3417 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7}, |
|
3418 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8}, |
|
3419 |
]; |
|
3420 |
{ |
|
3421 |
my $query; |
|
3422 |
foreach my $row (@$rows) { |
|
3423 |
$query ||= $dbi->insert($row, table => 'table1', query => 1); |
|
3424 |
$dbi->execute($query, $row, filter => {ab => sub { $_[0] * 2 }}); |
|
3425 |
} |
|
3426 |
is_deeply($dbi->select(table => 'table1')->all, |
|
3427 |
[ |
|
3428 |
{ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7}, |
|
3429 |
{ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8}, |
|
3430 |
] |
|
3431 |
); |
|
3432 |
} |
|
3433 | ||
cleanup test
|
3434 |
$dbi->execute('drop table table1'); |
cleanup test
|
3435 |
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);"); |
3436 |
$rows = [ |
|
3437 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7}, |
|
3438 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8}, |
|
3439 |
]; |
|
3440 |
{ |
|
3441 |
my $query; |
|
3442 |
my $sth; |
|
3443 |
foreach my $row (@$rows) { |
|
3444 |
$query ||= $dbi->insert($row, table => 'table1', query => 1); |
|
3445 |
$sth ||= $query->sth; |
|
3446 |
$sth->execute(map { $row->{$_} } sort keys %$row); |
|
3447 |
} |
|
3448 |
is_deeply($dbi->select(table => 'table1')->all, |
|
3449 |
[ |
|
3450 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7}, |
|
3451 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8}, |
|
3452 |
] |
|
3453 |
); |
|
3454 |
} |
|
cleanup test
|
3455 | |
clenup test
|
3456 |
test 'result'; |
3457 |
$dbi = DBIx::Custom->connect(%memory); |
|
cleanup test
|
3458 |
$dbi->execute($create_table1); |
clenup test
|
3459 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
3460 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
3461 | ||
3462 |
$result = $dbi->select(table => 'table1'); |
|
3463 |
@rows = (); |
|
3464 |
while (my $row = $result->fetch) { |
|
3465 |
push @rows, [@$row]; |
|
3466 |
} |
|
3467 |
is_deeply(\@rows, [[1, 2], [3, 4]]); |
|
3468 | ||
3469 |
$result = $dbi->select(table => 'table1'); |
|
3470 |
@rows = (); |
|
3471 |
while (my $row = $result->fetch_hash) { |
|
3472 |
push @rows, {%$row}; |
|
3473 |
} |
|
3474 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
3475 | ||
3476 |
$result = $dbi->select(table => 'table1'); |
|
3477 |
$row = $result->fetch_first; |
|
3478 |
is_deeply($row, [1, 2], "row"); |
|
3479 |
$row = $result->fetch; |
|
3480 |
ok(!$row, "finished"); |
|
3481 | ||
3482 |
$result = $dbi->select(table => 'table1'); |
|
3483 |
$row = $result->fetch_hash_first; |
|
3484 |
is_deeply($row, {key1 => 1, key2 => 2}, "row"); |
|
3485 |
$row = $result->fetch_hash; |
|
3486 |
ok(!$row, "finished"); |
|
3487 | ||
3488 |
$dbi->execute('create table table2 (key1, key2);'); |
|
3489 |
$result = $dbi->select(table => 'table2'); |
|
3490 |
$row = $result->fetch_hash_first; |
|
3491 |
ok(!$row, "no row fetch"); |
|
3492 | ||
3493 |
$dbi = DBIx::Custom->connect(%memory); |
|
cleanup test
|
3494 |
$dbi->execute($create_table1); |
clenup test
|
3495 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
3496 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
3497 |
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1'); |
|
3498 |
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1'); |
|
3499 |
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1'); |
|
3500 |
$result = $dbi->select(table => 'table1'); |
|
3501 |
$rows = $result->fetch_multi(2); |
|
3502 |
is_deeply($rows, [[1, 2], |
|
3503 |
[3, 4]], "fetch_multi first"); |
|
3504 |
$rows = $result->fetch_multi(2); |
|
3505 |
is_deeply($rows, [[5, 6], |
|
3506 |
[7, 8]], "fetch_multi secound"); |
|
3507 |
$rows = $result->fetch_multi(2); |
|
3508 |
is_deeply($rows, [[9, 10]], "fetch_multi third"); |
|
3509 |
$rows = $result->fetch_multi(2); |
|
3510 |
ok(!$rows); |
|
3511 | ||
3512 |
$result = $dbi->select(table => 'table1'); |
|
3513 |
eval {$result->fetch_multi}; |
|
3514 |
like($@, qr/Row count must be specified/, "Not specified row count"); |
|
3515 | ||
3516 |
$result = $dbi->select(table => 'table1'); |
|
3517 |
$rows = $result->fetch_hash_multi(2); |
|
3518 |
is_deeply($rows, [{key1 => 1, key2 => 2}, |
|
3519 |
{key1 => 3, key2 => 4}], "fetch_multi first"); |
|
3520 |
$rows = $result->fetch_hash_multi(2); |
|
3521 |
is_deeply($rows, [{key1 => 5, key2 => 6}, |
|
3522 |
{key1 => 7, key2 => 8}], "fetch_multi secound"); |
|
3523 |
$rows = $result->fetch_hash_multi(2); |
|
3524 |
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third"); |
|
3525 |
$rows = $result->fetch_hash_multi(2); |
|
3526 |
ok(!$rows); |
|
3527 | ||
3528 |
$result = $dbi->select(table => 'table1'); |
|
3529 |
eval {$result->fetch_hash_multi}; |
|
3530 |
like($@, qr/Row count must be specified/, "Not specified row count"); |
|
3531 | ||
3532 |
$dbi = DBIx::Custom->connect(%memory); |
|
cleanup test
|
3533 |
$dbi->execute($create_table1); |
clenup test
|
3534 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
3535 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
3536 | ||
3537 |
test 'fetch_all'; |
|
3538 |
$result = $dbi->select(table => 'table1'); |
|
3539 |
$rows = $result->fetch_all; |
|
3540 |
is_deeply($rows, [[1, 2], [3, 4]]); |
|
3541 | ||
3542 |
$result = $dbi->select(table => 'table1'); |
|
3543 |
$rows = $result->fetch_hash_all; |
|
3544 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
3545 | ||
3546 |
$result = $dbi->select(table => 'table1'); |
|
3547 |
$result->dbi->filters({three_times => sub { $_[0] * 3}}); |
|
3548 |
$result->filter({key1 => 'three_times'}); |
|
3549 | ||
3550 |
$rows = $result->fetch_all; |
|
3551 |
is_deeply($rows, [[3, 2], [9, 4]], "array"); |
|
3552 | ||
3553 |
$result = $dbi->select(table => 'table1'); |
|
3554 |
$result->dbi->filters({three_times => sub { $_[0] * 3}}); |
|
3555 |
$result->filter({key1 => 'three_times'}); |
|
3556 |
$rows = $result->fetch_hash_all; |
|
3557 |
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash"); |
|
3558 | ||
cleanup test
|
3559 |
test "query_builder"; |
3560 |
$datas = [ |
|
3561 |
# Basic tests |
|
3562 |
{ name => 'placeholder basic', |
|
3563 |
source => "a {? k1} b {= k2} {<> k3} {> k4} {< k5} {>= k6} {<= k7} {like k8}", , |
|
3564 |
sql_expected => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;", |
|
3565 |
columns_expected => [qw/k1 k2 k3 k4 k5 k6 k7 k8/] |
|
3566 |
}, |
|
3567 |
{ |
|
3568 |
name => 'placeholder in', |
|
3569 |
source => "{in k1 3};", |
|
3570 |
sql_expected => "k1 in (?, ?, ?);", |
|
3571 |
columns_expected => [qw/k1 k1 k1/] |
|
3572 |
}, |
|
3573 |
|
|
3574 |
# Table name |
|
3575 |
{ |
|
3576 |
name => 'placeholder with table name', |
|
3577 |
source => "{= a.k1} {= a.k2}", |
|
3578 |
sql_expected => "a.k1 = ? a.k2 = ?;", |
|
3579 |
columns_expected => [qw/a.k1 a.k2/] |
|
3580 |
}, |
|
3581 |
{ |
|
3582 |
name => 'placeholder in with table name', |
|
3583 |
source => "{in a.k1 2} {in b.k2 2}", |
|
3584 |
sql_expected => "a.k1 in (?, ?) b.k2 in (?, ?);", |
|
3585 |
columns_expected => [qw/a.k1 a.k1 b.k2 b.k2/] |
|
3586 |
}, |
|
3587 |
{ |
|
3588 |
name => 'not contain tag', |
|
3589 |
source => "aaa", |
|
3590 |
sql_expected => "aaa;", |
|
3591 |
columns_expected => [], |
|
3592 |
} |
|
3593 |
]; |
|
3594 | ||
3595 |
for (my $i = 0; $i < @$datas; $i++) { |
|
3596 |
my $data = $datas->[$i]; |
|
3597 |
my $builder = DBIx::Custom->new->query_builder; |
|
3598 |
my $query = $builder->build_query($data->{source}); |
|
3599 |
is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql"); |
|
3600 |
is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns"); |
|
3601 |
} |
|
3602 | ||
3603 |
$builder = DBIx::Custom->new->query_builder; |
|
3604 |
$ret_val = $builder->register_tag( |
|
3605 |
p => sub { |
|
3606 |
my @args = @_; |
|
3607 |
|
|
3608 |
my $expand = "? $args[0] $args[1]"; |
|
3609 |
my $columns = [2]; |
|
3610 |
return [$expand, $columns]; |
|
3611 |
} |
|
3612 |
); |
|
3613 | ||
3614 |
$query = $builder->build_query("{p a b}"); |
|
3615 |
is($query->{sql}, "? a b;", "register_tag sql"); |
|
3616 |
is_deeply($query->{columns}, [2], "register_tag columns"); |
|
3617 |
isa_ok($ret_val, 'DBIx::Custom::QueryBuilder'); |
|
3618 | ||
3619 |
$builder = DBIx::Custom->new->query_builder; |
|
3620 | ||
3621 |
eval{$builder->build_query('{? }')}; |
|
3622 |
like($@, qr/\QColumn name must be specified in tag "{? }"/, "? not arguments"); |
|
3623 | ||
3624 |
eval{$builder->build_query("{a }")}; |
|
3625 |
like($@, qr/\QTag "a" is not registered/, "tag not exist"); |
|
3626 | ||
3627 |
$builder->register_tag({ |
|
3628 |
q => 'string' |
|
3629 |
}); |
|
3630 | ||
3631 |
eval{$builder->build_query("{q}", {})}; |
|
3632 |
like($@, qr/Tag "q" must be sub reference/, "tag not code ref"); |
|
3633 | ||
3634 |
$builder->register_tag({ |
|
3635 |
r => sub {} |
|
3636 |
}); |
|
3637 | ||
3638 |
eval{$builder->build_query("{r}")}; |
|
3639 |
like($@, qr/\QTag "r" must return [STRING, ARRAY_REFERENCE]/, "tag return noting"); |
|
3640 | ||
3641 |
$builder->register_tag({ |
|
3642 |
s => sub { return ["a", ""]} |
|
3643 |
}); |
|
3644 | ||
3645 |
eval{$builder->build_query("{s}")}; |
|
3646 |
like($@, qr/\QTag "s" must return [STRING, ARRAY_REFERENCE]/, "tag return not array columns"); |
|
3647 | ||
3648 |
$builder->register_tag( |
|
3649 |
t => sub {return ["a", []]} |
|
3650 |
); |
|
3651 | ||
3652 | ||
3653 |
test 'General error case'; |
|
3654 |
$builder = DBIx::Custom->new->query_builder; |
|
3655 |
$builder->register_tag( |
|
3656 |
a => sub { |
|
3657 |
return ["? ? ?", ['']]; |
|
3658 |
} |
|
3659 |
); |
|
3660 |
eval{$builder->build_query("{a}")}; |
|
3661 |
like($@, qr/\QPlaceholder count/, "placeholder count is invalid"); |
|
3662 | ||
3663 | ||
3664 |
test 'Default tag Error case'; |
|
3665 |
eval{$builder->build_query("{= }")}; |
|
3666 |
like($@, qr/Column name must be specified in tag "{= }"/, "basic '=' : key not exist"); |
|
3667 | ||
3668 |
eval{$builder->build_query("{in }")}; |
|
3669 |
like($@, qr/Column name and count of values must be specified in tag "{in }"/, "in : key not exist"); |
|
3670 | ||
3671 |
eval{$builder->build_query("{in a}")}; |
|
3672 |
like($@, qr/\QColumn name and count of values must be specified in tag "{in }"/, |
|
3673 |
"in : key not exist"); |
|
3674 | ||
3675 |
eval{$builder->build_query("{in a r}")}; |
|
3676 |
like($@, qr/\QColumn name and count of values must be specified in tag "{in }"/, |
|
3677 |
"in : key not exist"); |
|
3678 | ||
3679 |
test 'variouse source'; |
|
3680 |
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;"; |
|
3681 |
$query = $builder->build_query($source); |
|
3682 |
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1"); |
|
3683 | ||
3684 |
$source = "abc;"; |
|
3685 |
$query = $builder->build_query($source); |
|
3686 |
is($query->sql, 'abc;', "basic : 2"); |
|
3687 | ||
3688 |
$source = "{= a}"; |
|
3689 |
$query = $builder->build_query($source); |
|
3690 |
is($query->sql, 'a = ?;', "only tag"); |
|
3691 | ||
3692 |
$source = "000;"; |
|
3693 |
$query = $builder->build_query($source); |
|
3694 |
is($query->sql, '000;', "contain 0 value"); |
|
3695 | ||
3696 |
$source = "a {= b} }"; |
|
3697 |
eval{$builder->build_query($source)}; |
|
3698 |
like($@, qr/unexpected "}"/, "error : 1"); |
|
3699 | ||
3700 |
$source = "a {= {}"; |
|
3701 |
eval{$builder->build_query($source)}; |
|
3702 |
like($@, qr/unexpected "{"/, "error : 2"); |
|
added EXPERIMENTAL DBIx::Cus...
|
3703 |