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