DBIx-Custom / t / common.t /
Newer Older
1684 lines | 57.935kb
added common test executing ...
Yuki Kimoto authored on 2011-08-07
1
use Test::More;
2
use strict;
3
use warnings;
test cleanup
Yuki Kimoto authored on 2011-08-10
4
use DBIx::Custom;
5
use Encode qw/encode_utf8/;
added common test executing ...
Yuki Kimoto authored on 2011-08-07
6

            
7
my $dbi;
8

            
9
plan skip_all => $ENV{DBIX_CUSTOM_SKIP_MESSAGE} || 'common.t is always skipped'
10
  unless $ENV{DBIX_CUSTOM_TEST_RUN}
11
    && eval { $dbi = DBIx::Custom->connect; 1 };
12

            
13
plan 'no_plan';
14

            
cleanup test
Yuki Kimoto authored on 2011-08-08
15
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/};
16
sub test { print "# $_[0]\n" }
17

            
added common test executing ...
Yuki Kimoto authored on 2011-08-07
18
# Constant
19
my $create_table1 = $dbi->create_table1;
cleanup test
Yuki Kimoto authored on 2011-08-08
20
my $create_table1_2 = $dbi->create_table1_2;
test cleanup
Yuki Kimoto authored on 2011-08-10
21
my $create_table1_type = $dbi->create_table1_type;
test cleanup
Yuki Kimoto authored on 2011-08-10
22
my $create_table2 = $dbi->create_table2;
23
my $create_table_reserved = $dbi->create_table_reserved;
cleanup test
Yuki Kimoto authored on 2011-08-10
24
my $q = substr($dbi->quote, 0, 1);
25
my $p = substr($dbi->quote, 1, 1) || $q;
added common test executing ...
Yuki Kimoto authored on 2011-08-07
26

            
27
# Variable
cleanup test
Yuki Kimoto authored on 2011-08-08
28
# Variables
29
my $builder;
30
my $datas;
31
my $sth;
32
my $source;
33
my @sources;
34
my $select_source;
35
my $insert_source;
36
my $update_source;
37
my $param;
38
my $params;
39
my $sql;
40
my $result;
41
my $row;
42
my @rows;
43
my $rows;
44
my $query;
45
my @queries;
46
my $select_query;
47
my $insert_query;
48
my $update_query;
49
my $ret_val;
50
my $infos;
added common test executing ...
Yuki Kimoto authored on 2011-08-07
51
my $model;
cleanup test
Yuki Kimoto authored on 2011-08-08
52
my $model2;
53
my $where;
54
my $update_param;
55
my $insert_param;
56
my $join;
cleanup test
Yuki Kimoto authored on 2011-08-10
57
my $binary;
added common test executing ...
Yuki Kimoto authored on 2011-08-07
58

            
59
# Drop table
60
eval { $dbi->execute('drop table table1') };
61

            
62
# Create table
63
$dbi->execute($create_table1);
64
$model = $dbi->create_model(table => 'table1');
65
$model->insert({key1 => 1, key2 => 2});
66
is_deeply($model->select->all, [{key1 => 1, key2 => 2}]);
67

            
cleanup test
Yuki Kimoto authored on 2011-08-08
68
test 'DBIx::Custom::Result test';
69
$dbi->delete_all(table => 'table1');
70
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
71
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
72
$source = "select key1, key2 from table1";
73
$query = $dbi->create_query($source);
74
$result = $dbi->execute($query);
75

            
76
@rows = ();
77
while (my $row = $result->fetch) {
78
    push @rows, [@$row];
79
}
80
is_deeply(\@rows, [[1, 2], [3, 4]], "fetch");
81

            
82
$result = $dbi->execute($query);
83
@rows = ();
84
while (my $row = $result->fetch_hash) {
85
    push @rows, {%$row};
86
}
87
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "fetch_hash");
88

            
89
$result = $dbi->execute($query);
90
$rows = $result->fetch_all;
91
is_deeply($rows, [[1, 2], [3, 4]], "fetch_all");
92

            
93
$result = $dbi->execute($query);
94
$rows = $result->fetch_hash_all;
95
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "all");
96

            
97
test 'Insert query return value';
98
$source = "insert into table1 {insert_param key1 key2}";
99
$query = $dbi->execute($source, {}, query => 1);
100
$ret_val = $dbi->execute($query, param => {key1 => 1, key2 => 2});
101
ok($ret_val);
102

            
103
test 'Direct query';
104
$dbi->delete_all(table => 'table1');
105
$insert_source = "insert into table1 {insert_param key1 key2}";
106
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2});
107
$result = $dbi->execute('select * from table1;');
108
$rows = $result->all;
109
is_deeply($rows, [{key1 => 1, key2 => 2}]);
110

            
111
test 'Filter basic';
112
$dbi->delete_all(table => 'table1');
113
$dbi->register_filter(twice       => sub { $_[0] * 2}, 
114
                    three_times => sub { $_[0] * 3});
115

            
116
$insert_source  = "insert into table1 {insert_param key1 key2};";
117
$insert_query = $dbi->execute($insert_source, {}, query => 1);
118
$insert_query->filter({key1 => 'twice'});
119
$dbi->execute($insert_query, param => {key1 => 1, key2 => 2});
120
$result = $dbi->execute('select * from table1;');
121
$rows = $result->filter({key2 => 'three_times'})->all;
122
is_deeply($rows, [{key1 => 2, key2 => 6}], "filter fetch_filter");
123

            
124
test 'Filter in';
125
$dbi->delete_all(table => 'table1');
126
$insert_source  = "insert into table1 {insert_param key1 key2};";
127
$insert_query = $dbi->execute($insert_source, {}, query => 1);
128
$dbi->execute($insert_query, param => {key1 => 2, key2 => 4});
129
$select_source = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}";
130
$select_query = $dbi->execute($select_source,{}, query => 1);
131
$select_query->filter({'table1.key1' => 'twice'});
132
$result = $dbi->execute($select_query, param => {'table1.key1' => [1,5], 'table1.key2' => [2,4]});
133
$rows = $result->all;
134
is_deeply($rows, [{key1 => 2, key2 => 4}], "filter");
135

            
cleanup test
Yuki Kimoto authored on 2011-08-08
136
test 'DBIx::Custom::SQLTemplate basic tag';
137
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-08
138
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-08
139
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
140
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
141

            
142
$source = "select * from table1 where key1 = :key1 and {<> key2} and {< key3} and {> key4} and {>= key5};";
143
$query = $dbi->execute($source, {}, query => 1);
144
$result = $dbi->execute($query, param => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
145
$rows = $result->all;
146
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1");
147

            
148
$source = "select * from table1 where key1 = :key1 and {<> key2} and {< key3} and {> key4} and {>= key5};";
149
$query = $dbi->execute($source, {}, query => 1);
150
$result = $dbi->execute($query, {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
151
$rows = $result->all;
152
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1");
153

            
154
$source = "select * from table1 where {<= key1} and {like key2};";
155
$query = $dbi->execute($source, {}, query => 1);
156
$result = $dbi->execute($query, param => {key1 => 1, key2 => '%2%'});
157
$rows = $result->all;
158
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag2");
159

            
160
test 'DIB::Custom::SQLTemplate in tag';
161
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-08
162
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-08
163
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
164
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
165

            
166
$source = "select * from table1 where {in key1 2};";
167
$query = $dbi->execute($source, {}, query => 1);
168
$result = $dbi->execute($query, param => {key1 => [9, 1]});
169
$rows = $result->all;
170
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic");
171

            
172
test 'DBIx::Custom::SQLTemplate insert tag';
173
$dbi->delete_all(table => 'table1');
174
$insert_source = 'insert into table1 {insert_param key1 key2 key3 key4 key5}';
175
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
176

            
177
$result = $dbi->execute('select * from table1;');
178
$rows = $result->all;
179
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic");
180

            
181
test 'DBIx::Custom::SQLTemplate update tag';
182
$dbi->delete_all(table => 'table1');
183
$insert_source = "insert into table1 {insert_param key1 key2 key3 key4 key5}";
184
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
185
$dbi->execute($insert_source, param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
186

            
187
$update_source = 'update table1 {update_param key1 key2 key3 key4} where {= key5}';
188
$dbi->execute($update_source, param => {key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5});
189

            
190
$result = $dbi->execute('select * from table1 order by key1;');
191
$rows = $result->all;
192
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5},
193
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "basic");
194

            
cleanup test
Yuki Kimoto authored on 2011-08-08
195
test 'Named placeholder';
196
$dbi->execute('drop table table1');
197
$dbi->execute($create_table1_2);
198
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
199
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
200

            
201
$source = "select * from table1 where key1 = :key1 and key2 = :key2";
202
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
203
$rows = $result->all;
204
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
205

            
206
$source = "select * from table1 where key1 = \n:key1\n and key2 = :key2";
207
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
208
$rows = $result->all;
209
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
210

            
211
$source = "select * from table1 where key1 = :key1 or key1 = :key1";
212
$result = $dbi->execute($source, param => {key1 => [1, 2]});
213
$rows = $result->all;
214
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
215

            
216
$source = "select * from table1 where key1 = :table1.key1 and key2 = :table1.key2";
217
$result = $dbi->execute(
218
    $source,
219
    param => {'table1.key1' => 1, 'table1.key2' => 1},
220
    filter => {'table1.key2' => sub { $_[0] * 2 }}
221
);
222
$rows = $result->all;
223
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
224

            
225
$dbi->execute('drop table table1');
226
$dbi->execute($create_table1);
227
$dbi->insert(table => 'table1', param => {key1 => '2011-10-14 12:19:18', key2 => 2});
228
$source = "select * from table1 where key1 = '2011-10-14 12:19:18' and key2 = :key2";
229
$result = $dbi->execute(
230
    $source,
231
    param => {'key2' => 2},
232
);
233

            
234
$rows = $result->all;
235
is_deeply($rows, [{key1 => '2011-10-14 12:19:18', key2 => 2}]);
236

            
237
$dbi->delete_all(table => 'table1');
238
$dbi->insert(table => 'table1', param => {key1 => 'a:b c:d', key2 => 2});
239
$source = "select * from table1 where key1 = 'a\\:b c\\:d' and key2 = :key2";
240
$result = $dbi->execute(
241
    $source,
242
    param => {'key2' => 2},
243
);
244
$rows = $result->all;
245
is_deeply($rows, [{key1 => 'a:b c:d', key2 => 2}]);
246

            
test cleanup
Yuki Kimoto authored on 2011-08-10
247
test 'Error case';
248
eval {DBIx::Custom->connect(dsn => 'dbi:SQLit')};
249
ok($@, "connect error");
250

            
251
eval{$dbi->execute("{p }", {}, query => 1)};
252
ok($@, "create_query invalid SQL template");
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
253

            
cleanup test
Yuki Kimoto authored on 2011-08-10
254
test 'insert';
cleanup test
Yuki Kimoto authored on 2011-08-10
255
eval { $dbi->execute('drop table table1') };
256
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
257
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
258
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
259
$result = $dbi->execute('select * from table1;');
260
$rows   = $result->all;
261
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
262

            
263
$dbi->execute('delete from table1');
264
$dbi->register_filter(
265
    twice       => sub { $_[0] * 2 },
266
    three_times => sub { $_[0] * 3 }
267
);
268
$dbi->default_bind_filter('twice');
269
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => 'three_times'});
270
$result = $dbi->execute('select * from table1;');
271
$rows   = $result->all;
272
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter");
273
$dbi->default_bind_filter(undef);
274

            
cleanup test
Yuki Kimoto authored on 2011-08-10
275
$dbi->execute('drop table table1');
276
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
277
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, append => '   ');
278
$rows = $dbi->select(table => 'table1')->all;
279
is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append');
280

            
281
eval{$dbi->insert(table => 'table1', noexist => 1)};
282
like($@, qr/noexist/, "invalid");
283

            
284
eval{$dbi->insert(table => 'table', param => {';' => 1})};
285
like($@, qr/safety/);
286

            
cleanup test
Yuki Kimoto authored on 2011-08-10
287
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
288
$dbi->execute($create_table_reserved);
cleanup test
Yuki Kimoto authored on 2011-08-10
289
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
290
$dbi->insert(table => 'table', param => {select => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
291
$result = $dbi->execute("select * from ${q}table$p");
cleanup test
Yuki Kimoto authored on 2011-08-10
292
$rows   = $result->all;
test cleanup
Yuki Kimoto authored on 2011-08-10
293
is_deeply($rows, [{select => 2, update => undef}], "reserved word");
cleanup test
Yuki Kimoto authored on 2011-08-10
294

            
cleanup test
Yuki Kimoto authored on 2011-08-10
295
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
296
$dbi->execute($create_table1);
297
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
298
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
299
$result = $dbi->execute('select * from table1;');
300
$rows   = $result->all;
301
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
302

            
cleanup test
Yuki Kimoto authored on 2011-08-10
303
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
304
$dbi->execute($create_table1);
305
$dbi->insert(table => 'table1', param => {key1 => \"'1'", key2 => 2});
306
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
307
$result = $dbi->execute('select * from table1;');
308
$rows   = $result->all;
309
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
310

            
test cleanup
Yuki Kimoto authored on 2011-08-10
311
test 'update';
312
eval { $dbi->execute('drop table table1') };
313
$dbi->execute($create_table1_2);
314
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
315
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
316
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
317
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
318
$rows   = $result->all;
319
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
320
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
321
                  "basic");
322
                  
323
$dbi->execute("delete from table1");
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 => 12}, where => {key2 => 2, key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
327
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
328
$rows   = $result->all;
329
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
330
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
331
                  "update key same as search key");
332

            
333
$dbi->update(table => 'table1', param => {key2 => [12]}, where => {key2 => 2, key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
334
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
335
$rows   = $result->all;
336
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
337
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
338
                  "update key same as search key : param is array ref");
339

            
340
$dbi->execute("delete from table1");
341
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
342
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
343
$dbi->register_filter(twice => sub { $_[0] * 2 });
344
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1},
345
              filter => {key2 => sub { $_[0] * 2 }});
test cleanup
Yuki Kimoto authored on 2011-08-10
346
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
347
$rows   = $result->all;
348
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5},
349
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
350
                  "filter");
351

            
352
$result = $dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}, append => '   ');
353

            
354
eval{$dbi->update(table => 'table1', where => {key1 => 1}, noexist => 1)};
355
like($@, qr/noexist/, "invalid");
356

            
357
eval{$dbi->update(table => 'table1')};
358
like($@, qr/where/, "not contain where");
359

            
360
eval { $dbi->execute('drop table table1') };
361
$dbi->execute($create_table1);
362
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
363
$where = $dbi->where;
364
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
365
$where->param({key1 => 1, key2 => 2});
366
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
367
$result = $dbi->select(table => 'table1');
368
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
369

            
370
eval { $dbi->execute('drop table table1') };
371
$dbi->execute($create_table1);
372
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
373
$dbi->update(
374
    table => 'table1',
375
    param => {key1 => 3},
376
    where => [
377
        ['and', 'key1 = :key1', 'key2 = :key2'],
378
        {key1 => 1, key2 => 2}
379
    ]
380
);
381
$result = $dbi->select(table => 'table1');
382
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
383

            
384
eval { $dbi->execute('drop table table1') };
385
$dbi->execute($create_table1);
386
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
387
$where = $dbi->where;
388
$where->clause(['and', 'key2 = :key2']);
389
$where->param({key2 => 2});
390
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
391
$result = $dbi->select(table => 'table1');
392
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
393

            
394
eval{$dbi->update(table => 'table1', param => {';' => 1})};
395
like($@, qr/safety/);
396

            
397
eval{$dbi->update(table => 'table1', param => {'key1' => 1}, where => {';' => 1})};
398
like($@, qr/safety/);
399

            
400
eval { $dbi->execute('drop table table1') };
401
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
402
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
403
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
404
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
405
$dbi->insert(table => 'table', param => {select => 1});
406
$dbi->update(table => 'table', where => {select => 1}, param => {update => 2});
407
$result = $dbi->execute("select * from ${q}table$p");
408
$rows   = $result->all;
409
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
410

            
411
eval {$dbi->update_all(table => 'table', param => {';' => 2}) };
412
like($@, qr/safety/);
413

            
414
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
415
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
416
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
417
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
418
$dbi->insert(table => 'table', param => {select => 1});
419
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
420
$result = $dbi->execute("select * from ${q}table$p");
421
$rows   = $result->all;
422
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
423

            
424
eval { $dbi->execute('drop table table1') };
425
$dbi->execute($create_table1_2);
426
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
427
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
428
$dbi->update({key2 => 11}, table => 'table1', where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
429
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
430
$rows   = $result->all;
431
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
432
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
433
                  "basic");
434

            
435
eval { $dbi->execute('drop table table1') };
436
$dbi->execute($create_table1_2);
437
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
438
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
439
$dbi->update(table => 'table1', param => {key2 => \"'11'"}, where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
440
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
441
$rows   = $result->all;
442
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
443
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
444
                  "basic");
445

            
446
test 'update_all';
447
eval { $dbi->execute('drop table table1') };
448
$dbi->execute($create_table1_2);
449
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
450
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
451
$dbi->register_filter(twice => sub { $_[0] * 2 });
452
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'});
453
$result = $dbi->execute('select * from table1;');
454
$rows   = $result->all;
455
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
456
                  {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
457
                  "filter");
458

            
459

            
460
test 'delete';
461
eval { $dbi->execute('drop table table1') };
462
$dbi->execute($create_table1);
463
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
464
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
465
$dbi->delete(table => 'table1', where => {key1 => 1});
466
$result = $dbi->execute('select * from table1;');
467
$rows   = $result->all;
468
is_deeply($rows, [{key1 => 3, key2 => 4}], "basic");
469

            
470
$dbi->execute("delete from table1;");
471
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
472
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
473
$dbi->register_filter(twice => sub { $_[0] * 2 });
474
$dbi->delete(table => 'table1', where => {key2 => 1}, filter => {key2 => 'twice'});
475
$result = $dbi->execute('select * from table1;');
476
$rows   = $result->all;
477
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter");
478

            
479
$dbi->delete(table => 'table1', where => {key1 => 1}, append => '   ');
480

            
481
$dbi->delete_all(table => 'table1');
482
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
483
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
484
$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2});
485
$rows = $dbi->select(table => 'table1')->all;
486
is_deeply($rows, [{key1 => 3, key2 => 4}], "delete multi key");
487

            
488
eval{$dbi->delete(table => 'table1', where => {key1 => 1}, noexist => 1)};
489
like($@, qr/noexist/, "invalid");
490

            
491
eval { $dbi->execute('drop table table1') };
492
$dbi->execute($create_table1);
493
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
494
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
495
$where = $dbi->where;
496
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
497
$where->param({ke1 => 1, key2 => 2});
498
$dbi->delete(table => 'table1', where => $where);
499
$result = $dbi->select(table => 'table1');
500
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
501

            
502
eval { $dbi->execute('drop table table1') };
503
$dbi->execute($create_table1);
504
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
505
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
506
$dbi->delete(
507
    table => 'table1',
508
    where => [
509
        ['and', 'key1 = :key1', 'key2 = :key2'],
510
        {ke1 => 1, key2 => 2}
511
    ]
512
);
513
$result = $dbi->select(table => 'table1');
514
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
515

            
516
eval { $dbi->execute('drop table table1') };
test cleanup
Yuki Kimoto authored on 2011-08-10
517
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
518
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
519
$dbi->delete(table => 'table1', where => {key1 => 1}, prefix => '    ');
520
$result = $dbi->execute('select * from table1;');
521
$rows   = $result->all;
522
is_deeply($rows, [], "basic");
523

            
524
test 'delete error';
525
eval { $dbi->execute('drop table table1') };
526
$dbi->execute($create_table1);
527
eval{$dbi->delete(table => 'table1')};
528
like($@, qr/"where" must be specified/,
529
         "where key-value pairs not specified");
530

            
531
eval{$dbi->delete(table => 'table1', where => {';' => 1})};
532
like($@, qr/safety/);
533

            
534
$dbi = DBIx::Custom->connect;
535
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
536
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
537
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
538
$dbi->insert(table => 'table', param => {select => 1});
539
$dbi->delete(table => 'table', where => {select => 1});
540
$result = $dbi->execute("select * from ${q}table$p");
541
$rows   = $result->all;
542
is_deeply($rows, [], "reserved word");
543

            
544
test 'delete_all';
545
eval { $dbi->execute('drop table table1') };
546
$dbi->execute($create_table1);
547
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
548
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
549
$dbi->delete_all(table => 'table1');
550
$result = $dbi->execute('select * from table1;');
551
$rows   = $result->all;
552
is_deeply($rows, [], "basic");
553

            
554

            
555
test 'select';
556
eval { $dbi->execute('drop table table1') };
557
$dbi->execute($create_table1);
558
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
559
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
560
$rows = $dbi->select(table => 'table1')->all;
561
is_deeply($rows, [{key1 => 1, key2 => 2},
562
                  {key1 => 3, key2 => 4}], "table");
563

            
564
$rows = $dbi->select(table => 'table1', column => ['key1'])->all;
565
is_deeply($rows, [{key1 => 1}, {key1 => 3}], "table and columns and where key");
566

            
567
$rows = $dbi->select(table => 'table1', where => {key1 => 1})->all;
568
is_deeply($rows, [{key1 => 1, key2 => 2}], "table and columns and where key");
569

            
570
$rows = $dbi->select(table => 'table1', column => ['key1'], where => {key1 => 3})->all;
571
is_deeply($rows, [{key1 => 3}], "table and columns and where key");
572

            
573
$rows = $dbi->select(table => 'table1', append => "order by key1 desc limit 1")->all;
574
is_deeply($rows, [{key1 => 3, key2 => 4}], "append statement");
575

            
576
$dbi->register_filter(decrement => sub { $_[0] - 1 });
577
$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'})
578
            ->all;
579
is_deeply($rows, [{key1 => 1, key2 => 2}], "filter");
580

            
test cleanup
Yuki Kimoto authored on 2011-08-10
581
eval { $dbi->execute("drop table table2") };
582
$dbi->execute($create_table2);
test cleanup
Yuki Kimoto authored on 2011-08-10
583
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
584
$rows = $dbi->select(
585
    table => [qw/table1 table2/],
586
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
587
    where   => {'table1.key2' => 2},
588
    relation  => {'table1.key1' => 'table2.key1'}
589
)->all;
590
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : exists where");
591

            
592
$rows = $dbi->select(
593
    table => [qw/table1 table2/],
594
    column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
595
    relation  => {'table1.key1' => 'table2.key1'}
596
)->all;
597
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : no exists where");
598

            
599
eval{$dbi->select(table => 'table1', noexist => 1)};
600
like($@, qr/noexist/, "invalid");
601

            
602
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
603
eval { $dbi->execute("drop table ${q}table$p") };
604
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
605
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
606
$dbi->insert(table => 'table', param => {select => 1, update => 2});
607
$result = $dbi->select(table => 'table', where => {select => 1});
608
$rows   = $result->all;
609
is_deeply($rows, [{select => 2, update => 2}], "reserved word");
610

            
611
test 'fetch filter';
612
eval { $dbi->execute('drop table table1') };
613
$dbi->register_filter(
614
    twice       => sub { $_[0] * 2 },
615
    three_times => sub { $_[0] * 3 }
616
);
617
$dbi->default_fetch_filter('twice');
618
$dbi->execute($create_table1);
619
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
620
$result = $dbi->select(table => 'table1');
621
$result->filter({key1 => 'three_times'});
622
$row = $result->one;
623
is_deeply($row, {key1 => 3, key2 => 4}, "default_fetch_filter and filter");
624

            
625
test 'filters';
626
$dbi = DBIx::Custom->new;
627

            
628
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')),
629
   'あ', "decode_utf8");
630

            
631
is($dbi->filters->{encode_utf8}->('あ'),
632
   encode_utf8('あ'), "encode_utf8");
633

            
634
test 'transaction';
635
$dbi = DBIx::Custom->connect;
636
eval { $dbi->execute('drop table table1') };
637
$dbi->execute($create_table1);
638
$dbi->dbh->begin_work;
639
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
640
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
641
$dbi->dbh->commit;
642
$result = $dbi->select(table => 'table1');
643
is_deeply(scalar $result->all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}],
644
          "commit");
645

            
646
$dbi = DBIx::Custom->connect;
647
eval { $dbi->execute('drop table table1') };
648
$dbi->execute($create_table1);
649
$dbi->dbh->begin_work(0);
650
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
651
$dbi->dbh->rollback;
652

            
653
$result = $dbi->select(table => 'table1');
654
ok(! $result->fetch_first, "rollback");
655

            
656
test 'cache';
657
eval { $dbi->execute('drop table table1') };
658
$dbi->cache(1);
659
$dbi->execute($create_table1);
660
$source = 'select * from table1 where key1 = :key1 and key2 = :key2;';
661
$dbi->execute($source, {}, query => 1);
662
is_deeply($dbi->{_cached}->{$source}, 
663
          {sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2'], tables => []}, "cache");
664

            
665
eval { $dbi->execute('drop table table1') };
666
$dbi->execute($create_table1);
667
$dbi->{_cached} = {};
668
$dbi->cache(0);
669
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
670
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
671

            
672
test 'execute';
673
eval { $dbi->execute('drop table table1') };
674
$dbi->execute($create_table1);
675
{
676
    local $Carp::Verbose = 0;
677
    eval{$dbi->execute('select * frm table1')};
678
    like($@, qr/\Qselect * frm table1;/, "fail prepare");
679
    like($@, qr/\.t /, "fail : not verbose");
680
}
681
{
682
    local $Carp::Verbose = 1;
683
    eval{$dbi->execute('select * frm table1')};
684
    like($@, qr/Custom.*\.t /s, "fail : verbose");
685
}
686

            
687
eval{$dbi->execute('select * from table1', no_exists => 1)};
688
like($@, qr/wrong/, "invald SQL");
689

            
690
$query = $dbi->execute('select * from table1 where key1 = :key1', {}, query => 1);
691
$dbi->dbh->disconnect;
692
eval{$dbi->execute($query, param => {key1 => {a => 1}})};
693
ok($@, "execute fail");
694

            
695
{
696
    local $Carp::Verbose = 0;
697
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
698
    like($@, qr/\Q.t /, "caller spec : not vebose");
699
}
700
{
701
    local $Carp::Verbose = 1;
702
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
703
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
704
}
705

            
706

            
707
test 'transaction';
708
$dbi = DBIx::Custom->connect;
709
eval { $dbi->execute('drop table table1') };
710
$dbi->execute($create_table1);
711

            
712
$dbi->begin_work;
713

            
714
eval {
715
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
716
    die "Error";
717
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
718
};
719

            
720
$dbi->rollback if $@;
721

            
722
$result = $dbi->select(table => 'table1');
723
$rows = $result->all;
724
is_deeply($rows, [], "rollback");
725

            
726
$dbi->begin_work;
727

            
728
eval {
729
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
730
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
731
};
732

            
733
$dbi->commit unless $@;
734

            
735
$result = $dbi->select(table => 'table1');
736
$rows = $result->all;
737
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "commit");
738

            
739
$dbi->dbh->{AutoCommit} = 0;
740
eval{ $dbi->begin_work };
741
ok($@, "exception");
742
$dbi->dbh->{AutoCommit} = 1;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
743

            
test cleanup
Yuki Kimoto authored on 2011-08-10
744
test 'cache';
745
eval { $dbi->execute('drop table table1') };
746
$dbi->cache(1);
747
$dbi->execute($create_table1);
748
$source = 'select * from table1 where key1 = :key1 and key2 = :key2;';
749
$dbi->execute($source, {}, query => 1);
750
is_deeply($dbi->{_cached}->{$source}, 
751
          {sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2'], tables => []}, "cache");
752

            
753
eval { $dbi->execute('drop table table1') };
754
$dbi->execute($create_table1);
755
$dbi->{_cached} = {};
756
$dbi->cache(0);
757
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
758
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
759

            
760
test 'execute';
761
eval { $dbi->execute('drop table table1') };
762
$dbi->execute($create_table1);
763
{
764
    local $Carp::Verbose = 0;
765
    eval{$dbi->execute('select * frm table1')};
766
    like($@, qr/\Qselect * frm table1;/, "fail prepare");
767
    like($@, qr/\.t /, "fail : not verbose");
768
}
769
{
770
    local $Carp::Verbose = 1;
771
    eval{$dbi->execute('select * frm table1')};
772
    like($@, qr/Custom.*\.t /s, "fail : verbose");
773
}
774

            
775
eval{$dbi->execute('select * from table1', no_exists => 1)};
776
like($@, qr/wrong/, "invald SQL");
777

            
778
$query = $dbi->execute('select * from table1 where key1 = :key1', {}, query => 1);
779
$dbi->dbh->disconnect;
780
eval{$dbi->execute($query, param => {key1 => {a => 1}})};
781
ok($@, "execute fail");
782

            
783
{
784
    local $Carp::Verbose = 0;
785
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
786
    like($@, qr/\Q.t /, "caller spec : not vebose");
787
}
788
{
789
    local $Carp::Verbose = 1;
790
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
791
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
792
}
793

            
794

            
795
test 'transaction';
796
$dbi = DBIx::Custom->connect;
797
eval { $dbi->execute('drop table table1') };
798
$dbi->execute($create_table1);
799

            
800
$dbi->begin_work;
801

            
802
eval {
803
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
804
    die "Error";
805
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
806
};
807

            
808
$dbi->rollback if $@;
809

            
810
$result = $dbi->select(table => 'table1');
811
$rows = $result->all;
812
is_deeply($rows, [], "rollback");
813

            
814
$dbi->begin_work;
815

            
816
eval {
817
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
818
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
819
};
820

            
821
$dbi->commit unless $@;
822

            
823
$result = $dbi->select(table => 'table1');
824
$rows = $result->all;
825
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "commit");
826

            
827
$dbi->dbh->{AutoCommit} = 0;
828
eval{ $dbi->begin_work };
829
ok($@, "exception");
830
$dbi->dbh->{AutoCommit} = 1;
831

            
832

            
833
test 'method';
834
$dbi->method(
835
    one => sub { 1 }
836
);
837
$dbi->method(
838
    two => sub { 2 }
839
);
840
$dbi->method({
841
    twice => sub {
842
        my $self = shift;
843
        return $_[0] * 2;
844
    }
845
});
846

            
847
is($dbi->one, 1, "first");
848
is($dbi->two, 2, "second");
849
is($dbi->twice(5), 10 , "second");
850

            
851
eval {$dbi->XXXXXX};
852
ok($@, "not exists");
853

            
854
test 'out filter';
855
$dbi = DBIx::Custom->connect;
856
eval { $dbi->execute('drop table table1') };
857
$dbi->execute($create_table1);
858
$dbi->register_filter(twice => sub { $_[0] * 2 });
859
$dbi->register_filter(three_times => sub { $_[0] * 3});
860
$dbi->apply_filter(
861
    'table1', 'key1' => {out => 'twice', in => 'three_times'}, 
862
              'key2' => {out => 'three_times', in => 'twice'});
863
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
864
$result = $dbi->execute('select * from table1;');
865
$row   = $result->fetch_hash_first;
866
is_deeply($row, {key1 => 2, key2 => 6}, "insert");
867
$result = $dbi->select(table => 'table1');
868
$row   = $result->one;
869
is_deeply($row, {key1 => 6, key2 => 12}, "insert");
870

            
871
$dbi = DBIx::Custom->connect;
872
eval { $dbi->execute('drop table table1') };
873
$dbi->execute($create_table1);
874
$dbi->register_filter(twice => sub { $_[0] * 2 });
875
$dbi->register_filter(three_times => sub { $_[0] * 3});
876
$dbi->apply_filter(
877
    'table1', 'key1' => {out => 'twice', in => 'three_times'}, 
878
              'key2' => {out => 'three_times', in => 'twice'});
879
$dbi->apply_filter(
880
    'table1', 'key1' => {out => undef}
881
); 
882
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
883
$result = $dbi->execute('select * from table1;');
884
$row   = $result->one;
885
is_deeply($row, {key1 => 1, key2 => 6}, "insert");
886

            
887
$dbi = DBIx::Custom->connect;
888
eval { $dbi->execute('drop table table1') };
889
$dbi->execute($create_table1);
890
$dbi->register_filter(twice => sub { $_[0] * 2 });
891
$dbi->apply_filter(
892
    'table1', 'key1' => {out => 'twice', in => 'twice'}
893
);
894
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => undef});
895
$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2});
896
$result = $dbi->execute('select * from table1;');
897
$row   = $result->one;
898
is_deeply($row, {key1 => 4, key2 => 2}, "update");
899

            
900
$dbi = DBIx::Custom->connect;
901
eval { $dbi->execute('drop table table1') };
902
$dbi->execute($create_table1);
903
$dbi->register_filter(twice => sub { $_[0] * 2 });
904
$dbi->apply_filter(
905
    'table1', 'key1' => {out => 'twice', in => 'twice'}
906
);
907
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1=> undef});
908
$dbi->delete(table => 'table1', where => {key1 => 1});
909
$result = $dbi->execute('select * from table1;');
910
$rows   = $result->all;
911
is_deeply($rows, [], "delete");
912

            
913
$dbi = DBIx::Custom->connect;
914
eval { $dbi->execute('drop table table1') };
915
$dbi->execute($create_table1);
916
$dbi->register_filter(twice => sub { $_[0] * 2 });
917
$dbi->apply_filter(
918
    'table1', 'key1' => {out => 'twice', in => 'twice'}
919
);
920
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
921
$result = $dbi->select(table => 'table1', where => {key1 => 1});
922
$result->filter({'key2' => 'twice'});
923
$rows   = $result->all;
924
is_deeply($rows, [{key1 => 4, key2 => 4}], "select");
925

            
926
$dbi = DBIx::Custom->connect;
927
eval { $dbi->execute('drop table table1') };
928
$dbi->execute($create_table1);
929
$dbi->register_filter(twice => sub { $_[0] * 2 });
930
$dbi->apply_filter(
931
    'table1', 'key1' => {out => 'twice', in => 'twice'}
932
);
933
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
934
$result = $dbi->execute("select * from table1 where key1 = :key1 and key2 = :key2;",
935
                        param => {key1 => 1, key2 => 2},
936
                        table => ['table1']);
937
$rows   = $result->all;
938
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute");
939

            
940
$dbi = DBIx::Custom->connect;
941
eval { $dbi->execute('drop table table1') };
942
$dbi->execute($create_table1);
943
$dbi->register_filter(twice => sub { $_[0] * 2 });
944
$dbi->apply_filter(
945
    'table1', 'key1' => {out => 'twice', in => 'twice'}
946
);
947
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
948
$result = $dbi->execute("select * from {table table1} where key1 = :key1 and key2 = :key2;",
949
                        param => {key1 => 1, key2 => 2});
950
$rows   = $result->all;
951
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute table tag");
952

            
953
$dbi = DBIx::Custom->connect;
954
eval { $dbi->execute('drop table table1') };
955
eval { $dbi->execute('drop table table2') };
956
$dbi->execute($create_table1);
957
$dbi->execute($create_table2);
958
$dbi->register_filter(twice => sub { $_[0] * 2 });
959
$dbi->register_filter(three_times => sub { $_[0] * 3 });
960
$dbi->apply_filter(
961
    'table1', 'key2' => {out => 'twice', in => 'twice'}
962
);
963
$dbi->apply_filter(
964
    'table2', 'key3' => {out => 'three_times', in => 'three_times'}
965
);
966
$dbi->insert(table => 'table1', param => {key1 => 5, key2 => 2}, filter => {key2 => undef});
967
$dbi->insert(table => 'table2', param => {key1 => 5, key3 => 6}, filter => {key3 => undef});
968
$result = $dbi->select(
969
     table => ['table1', 'table2'],
970
     column => ['key2', 'key3'],
971
     where => {'table1.key2' => 1, 'table2.key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
972

            
973
$result->filter({'key2' => 'twice'});
974
$rows   = $result->all;
975
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join");
976

            
977
$result = $dbi->select(
978
     table => ['table1', 'table2'],
979
     column => ['key2', 'key3'],
980
     where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
981

            
982
$result->filter({'key2' => 'twice'});
983
$rows   = $result->all;
984
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join : omit");
985

            
986
test 'each_column';
987
$dbi = DBIx::Custom->connect;
988
eval { $dbi->execute("drop table ${q}table$p") };
989
eval { $dbi->execute('drop table table1') };
990
eval { $dbi->execute('drop table table2') };
test cleranup
Yuki Kimoto authored on 2011-08-10
991
eval { $dbi->execute('drop table table3') };
test cleanup
Yuki Kimoto authored on 2011-08-10
992
$dbi->execute($create_table1_type);
993
$dbi->execute($create_table2);
994

            
995
$infos = [];
996
$dbi->each_column(sub {
997
    my ($self, $table, $column, $cinfo) = @_;
998
    
999
    if ($table =~ /^table\d/) {
1000
         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
1001
         push @$infos, $info;
1002
    }
1003
});
1004
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
1005
is_deeply($infos, 
1006
    [
1007
        ['table1', 'key1', 'key1'],
1008
        ['table1', 'key2', 'key2'],
1009
        ['table2', 'key1', 'key1'],
1010
        ['table2', 'key3', 'key3']
1011
    ]
1012
    
1013
);
1014
test 'each_table';
1015
$dbi = DBIx::Custom->connect;
1016
eval { $dbi->execute('drop table table1') };
1017
eval { $dbi->execute('drop table table2') };
1018
$dbi->execute($create_table2);
1019
$dbi->execute($create_table1_type);
1020

            
1021
$infos = [];
1022
$dbi->each_table(sub {
1023
    my ($self, $table, $table_info) = @_;
1024
    
1025
    if ($table =~ /^table\d/) {
1026
         my $info = [$table, $table_info->{TABLE_NAME}];
1027
         push @$infos, $info;
1028
    }
1029
});
1030
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
1031
is_deeply($infos, 
1032
    [
1033
        ['table1', 'table1'],
1034
        ['table2', 'table2'],
1035
    ]
1036
);
1037

            
1038
test 'limit';
1039
$dbi = DBIx::Custom->connect;
1040
eval { $dbi->execute('drop table table1') };
1041
$dbi->execute($create_table1);
1042
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1043
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
1044
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
1045
$dbi->register_tag(
1046
    limit => sub {
1047
        my ($count, $offset) = @_;
1048
        
1049
        my $s = '';
1050
        $s .= "limit $count";
1051
        $s .= " offset $offset" if defined $offset;
1052
        
1053
        return [$s, []];
1054
    }
1055
);
1056
$rows = $dbi->select(
1057
  table => 'table1',
1058
  where => {key1 => 1},
1059
  append => "order by key2 {limit 1 0}"
1060
)->all;
1061
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1062
$rows = $dbi->select(
1063
  table => 'table1',
1064
  where => {key1 => 1},
1065
  append => "order by key2 {limit 2 1}"
1066
)->all;
1067
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
1068
$rows = $dbi->select(
1069
  table => 'table1',
1070
  where => {key1 => 1},
1071
  append => "order by key2 {limit 1}"
1072
)->all;
1073
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1074

            
1075
test 'connect super';
1076
{
1077
    package MyDBI;
1078
    
1079
    use base 'DBIx::Custom';
1080
    sub connect {
1081
        my $self = shift->SUPER::connect(@_);
1082
        
1083
        return $self;
1084
    }
1085
    
1086
    sub new {
1087
        my $self = shift->SUPER::new(@_);
1088
        
1089
        return $self;
1090
    }
1091
}
1092

            
1093
$dbi = MyDBI->connect;
1094
eval { $dbi->execute('drop table table1') };
1095
$dbi->execute($create_table1);
1096
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1097
is($dbi->select(table => 'table1')->one->{key1}, 1);
1098

            
1099
$dbi = MyDBI->new;
1100
$dbi->connect;
1101
eval { $dbi->execute('drop table table1') };
1102
$dbi->execute($create_table1);
1103
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1104
is($dbi->select(table => 'table1')->one->{key1}, 1);
1105

            
1106
{
1107
    package MyDBI2;
1108
    
1109
    use base 'DBIx::Custom';
1110
    sub connect {
1111
        my $self = shift->SUPER::new(@_);
1112
        $self->connect;
1113
        
1114
        return $self;
1115
    }
1116
}
1117

            
1118
$dbi = MyDBI->connect;
1119
eval { $dbi->execute('drop table table1') };
1120
$dbi->execute($create_table1);
1121
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1122
is($dbi->select(table => 'table1')->one->{key1}, 1);
1123

            
1124
test 'end_filter';
1125
$dbi = DBIx::Custom->connect;
1126
eval { $dbi->execute('drop table table1') };
1127
$dbi->execute($create_table1);
1128
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1129
$result = $dbi->select(table => 'table1');
1130
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1131
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 });
1132
$row = $result->fetch_first;
1133
is_deeply($row, [6, 40]);
1134

            
1135
$dbi = DBIx::Custom->connect;
1136
eval { $dbi->execute('drop table table1') };
1137
$dbi->execute($create_table1);
1138
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1139
$result = $dbi->select(table => 'table1');
1140
$result->filter([qw/key1 key2/] => sub { $_[0] * 2 });
1141
$result->end_filter([[qw/key1 key2/] => sub { $_[0] * 3 }]);
1142
$row = $result->fetch_first;
1143
is_deeply($row, [6, 12]);
1144

            
1145
$dbi = DBIx::Custom->connect;
1146
eval { $dbi->execute('drop table table1') };
1147
$dbi->execute($create_table1);
1148
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1149
$result = $dbi->select(table => 'table1');
1150
$result->filter([[qw/key1 key2/] => sub { $_[0] * 2 }]);
1151
$result->end_filter([qw/key1 key2/] => sub { $_[0] * 3 });
1152
$row = $result->fetch_first;
1153
is_deeply($row, [6, 12]);
1154

            
1155
$dbi->register_filter(five_times => sub { $_[0] * 5 });
1156
$result = $dbi->select(table => 'table1');
1157
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1158
$result->end_filter({key1 => sub { $_[0] * 3 }, key2 => 'five_times' });
1159
$row = $result->one;
1160
is_deeply($row, {key1 => 6, key2 => 40});
1161

            
1162
$dbi->register_filter(five_times => sub { $_[0] * 5 });
1163
$dbi->apply_filter('table1',
1164
    key1 => {end => sub { $_[0] * 3 } },
1165
    key2 => {end => 'five_times'}
1166
);
1167
$result = $dbi->select(table => 'table1');
1168
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1169
$row = $result->one;
1170
is_deeply($row, {key1 => 6, key2 => 40}, 'apply_filter');
1171

            
1172
$dbi->register_filter(five_times => sub { $_[0] * 5 });
1173
$dbi->apply_filter('table1',
1174
    key1 => {end => sub { $_[0] * 3 } },
1175
    key2 => {end => 'five_times'}
1176
);
1177
$result = $dbi->select(table => 'table1');
1178
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1179
$result->filter(key1 => undef);
1180
$result->end_filter(key1 => undef);
1181
$row = $result->one;
1182
is_deeply($row, {key1 => 1, key2 => 40}, 'apply_filter overwrite');
1183

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1184
test 'remove_end_filter and remove_filter';
1185
$dbi = DBIx::Custom->connect;
1186
eval { $dbi->execute('drop table table1') };
1187
$dbi->execute($create_table1);
1188
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1189
$result = $dbi->select(table => 'table1');
1190
$row = $result
1191
       ->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 })
1192
       ->remove_filter
1193
       ->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 })
1194
       ->remove_end_filter
1195
       ->fetch_first;
1196
is_deeply($row, [1, 2]);
1197

            
1198
test 'empty where select';
1199
$dbi = DBIx::Custom->connect;
1200
eval { $dbi->execute('drop table table1') };
1201
$dbi->execute($create_table1);
1202
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1203
$result = $dbi->select(table => 'table1', where => {});
1204
$row = $result->one;
1205
is_deeply($row, {key1 => 1, key2 => 2});
1206

            
1207
test 'select query option';
1208
$dbi = DBIx::Custom->connect;
1209
eval { $dbi->execute('drop table table1') };
1210
$dbi->execute($create_table1);
1211
$query = $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, query => 1);
1212
is(ref $query, 'DBIx::Custom::Query');
1213
$query = $dbi->update(table => 'table1', where => {key1 => 1}, param => {key2 => 2}, query => 1);
1214
is(ref $query, 'DBIx::Custom::Query');
1215
$query = $dbi->delete(table => 'table1', where => {key1 => 1}, query => 1);
1216
is(ref $query, 'DBIx::Custom::Query');
1217
$query = $dbi->select(table => 'table1', where => {key1 => 1, key2 => 2}, query => 1);
1218
is(ref $query, 'DBIx::Custom::Query');
1219

            
1220
test 'where';
1221
$dbi = DBIx::Custom->connect;
1222
eval { $dbi->execute('drop table table1') };
1223
$dbi->execute($create_table1);
1224
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1225
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1226
$where = $dbi->where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
1227
is("$where", "where ( key1 = :key1 and key2 = :key2 )", 'no param');
1228

            
1229
$where = $dbi->where
1230
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
1231
             ->param({key1 => 1});
1232

            
1233
$result = $dbi->select(
1234
    table => 'table1',
1235
    where => $where
1236
);
1237
$row = $result->all;
1238
is_deeply($row, [{key1 => 1, key2 => 2}]);
1239

            
1240
$result = $dbi->select(
1241
    table => 'table1',
1242
    where => [
1243
        ['and', 'key1 = :key1', 'key2 = :key2'],
1244
        {key1 => 1}
1245
    ]
1246
);
1247
$row = $result->all;
1248
is_deeply($row, [{key1 => 1, key2 => 2}]);
1249

            
1250
$where = $dbi->where
1251
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
1252
             ->param({key1 => 1, key2 => 2});
1253
$result = $dbi->select(
1254
    table => 'table1',
1255
    where => $where
1256
);
1257
$row = $result->all;
1258
is_deeply($row, [{key1 => 1, key2 => 2}]);
1259

            
1260
$where = $dbi->where
1261
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
1262
             ->param({});
1263
$result = $dbi->select(
1264
    table => 'table1',
1265
    where => $where,
1266
);
1267
$row = $result->all;
1268
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1269

            
1270
$where = $dbi->where
1271
             ->clause(['and', ['or', 'key1 > :key1', 'key1 < :key1'], 'key2 = :key2'])
1272
             ->param({key1 => [0, 3], key2 => 2});
1273
$result = $dbi->select(
1274
    table => 'table1',
1275
    where => $where,
1276
); 
1277
$row = $result->all;
1278
is_deeply($row, [{key1 => 1, key2 => 2}]);
1279

            
1280
$where = $dbi->where;
1281
$result = $dbi->select(
1282
    table => 'table1',
1283
    where => $where
1284
);
1285
$row = $result->all;
1286
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1287

            
1288
eval {
1289
$where = $dbi->where
1290
             ->clause(['uuu']);
1291
$result = $dbi->select(
1292
    table => 'table1',
1293
    where => $where
1294
);
1295
};
1296
ok($@);
1297

            
1298
$where = $dbi->where;
1299
is("$where", '');
1300

            
1301
$where = $dbi->where
1302
             ->clause(['or', ('key1 = :key1') x 2])
1303
             ->param({key1 => [1, 3]});
1304
$result = $dbi->select(
1305
    table => 'table1',
1306
    where => $where,
1307
);
1308
$row = $result->all;
1309
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1310

            
1311
$where = $dbi->where
1312
             ->clause(['or', ('key1 = :key1') x 2])
1313
             ->param({key1 => [1]});
1314
$result = $dbi->select(
1315
    table => 'table1',
1316
    where => $where,
1317
);
1318
$row = $result->all;
1319
is_deeply($row, [{key1 => 1, key2 => 2}]);
1320

            
1321
$where = $dbi->where
1322
             ->clause(['or', ('key1 = :key1') x 2])
1323
             ->param({key1 => 1});
1324
$result = $dbi->select(
1325
    table => 'table1',
1326
    where => $where,
1327
);
1328
$row = $result->all;
1329
is_deeply($row, [{key1 => 1, key2 => 2}]);
1330

            
1331
$where = $dbi->where
1332
             ->clause('key1 = :key1')
1333
             ->param({key1 => 1});
1334
$result = $dbi->select(
1335
    table => 'table1',
1336
    where => $where,
1337
);
1338
$row = $result->all;
1339
is_deeply($row, [{key1 => 1, key2 => 2}]);
1340

            
1341
$where = $dbi->where
1342
             ->clause('key1 = :key1 key2 = :key2')
1343
             ->param({key1 => 1});
1344
eval{$where->to_string};
1345
like($@, qr/one column/);
1346

            
1347
$where = $dbi->where
1348
             ->clause(['or', ('key1 = :key1') x 3])
1349
             ->param({key1 => [$dbi->not_exists, 1, 3]});
1350
$result = $dbi->select(
1351
    table => 'table1',
1352
    where => $where,
1353
);
1354
$row = $result->all;
1355
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1356

            
1357
$where = $dbi->where
1358
             ->clause(['or', ('key1 = :key1') x 3])
1359
             ->param({key1 => [1, $dbi->not_exists, 3]});
1360
$result = $dbi->select(
1361
    table => 'table1',
1362
    where => $where,
1363
);
1364
$row = $result->all;
1365
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1366

            
1367
$where = $dbi->where
1368
             ->clause(['or', ('key1 = :key1') x 3])
1369
             ->param({key1 => [1, 3, $dbi->not_exists]});
1370
$result = $dbi->select(
1371
    table => 'table1',
1372
    where => $where,
1373
);
1374
$row = $result->all;
1375
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1376

            
1377
$where = $dbi->where
1378
             ->clause(['or', ('key1 = :key1') x 3])
1379
             ->param({key1 => [1, $dbi->not_exists, $dbi->not_exists]});
1380
$result = $dbi->select(
1381
    table => 'table1',
1382
    where => $where,
1383
);
1384
$row = $result->all;
1385
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1386

            
1387
$where = $dbi->where
1388
             ->clause(['or', ('key1 = :key1') x 3])
1389
             ->param({key1 => [$dbi->not_exists, 1, $dbi->not_exists]});
1390
$result = $dbi->select(
1391
    table => 'table1',
1392
    where => $where,
1393
);
1394
$row = $result->all;
1395
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1396

            
1397
$where = $dbi->where
1398
             ->clause(['or', ('key1 = :key1') x 3])
1399
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists, 1]});
1400
$result = $dbi->select(
1401
    table => 'table1',
1402
    where => $where,
1403
);
1404
$row = $result->all;
1405
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1406

            
1407
$where = $dbi->where
1408
             ->clause(['or', ('key1 = :key1') x 3])
1409
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists, $dbi->not_exists]});
1410
$result = $dbi->select(
1411
    table => 'table1',
1412
    where => $where,
1413
);
1414
$row = $result->all;
1415
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1416

            
1417
$where = $dbi->where
1418
             ->clause(['or', ('key1 = :key1') x 3])
1419
             ->param({key1 => []});
1420
$result = $dbi->select(
1421
    table => 'table1',
1422
    where => $where,
1423
);
1424
$row = $result->all;
1425
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1426

            
1427
$where = $dbi->where
1428
             ->clause(['and', '{> key1}', '{< key1}' ])
1429
             ->param({key1 => [2, $dbi->not_exists]});
1430
$result = $dbi->select(
1431
    table => 'table1',
1432
    where => $where,
1433
);
1434
$row = $result->all;
1435
is_deeply($row, [{key1 => 3, key2 => 4}], 'not_exists');
1436

            
1437
$where = $dbi->where
1438
             ->clause(['and', '{> key1}', '{< key1}' ])
1439
             ->param({key1 => [$dbi->not_exists, 2]});
1440
$result = $dbi->select(
1441
    table => 'table1',
1442
    where => $where,
1443
);
1444
$row = $result->all;
1445
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1446

            
1447
$where = $dbi->where
1448
             ->clause(['and', '{> key1}', '{< key1}' ])
1449
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists]});
1450
$result = $dbi->select(
1451
    table => 'table1',
1452
    where => $where,
1453
);
1454
$row = $result->all;
1455
is_deeply($row, [{key1 => 1, key2 => 2},{key1 => 3, key2 => 4}], 'not_exists');
1456

            
1457
$where = $dbi->where
1458
             ->clause(['and', '{> key1}', '{< key1}' ])
1459
             ->param({key1 => [0, 2]});
1460
$result = $dbi->select(
1461
    table => 'table1',
1462
    where => $where,
1463
);
1464
$row = $result->all;
1465
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1466

            
1467
$where = $dbi->where
1468
             ->clause(['and', 'key1 is not null', 'key2 is not null' ]);
1469
$result = $dbi->select(
1470
    table => 'table1',
1471
    where => $where,
1472
);
1473
$row = $result->all;
1474
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1475

            
1476
eval {$dbi->where(ppp => 1) };
1477
like($@, qr/invalid/);
1478

            
1479
$where = $dbi->where(
1480
    clause => ['and', ['or'], ['and', 'key1 = :key1', 'key2 = :key2']],
1481
    param => {key1 => 1, key2 => 2}
1482
);
1483
$result = $dbi->select(
1484
    table => 'table1',
1485
    where => $where,
1486
);
1487
$row = $result->all;
1488
is_deeply($row, [{key1 => 1, key2 => 2}]);
1489

            
1490

            
1491
$where = $dbi->where(
1492
    clause => ['and', ['or'], ['or', ':key1', ':key2']],
1493
    param => {}
1494
);
1495
$result = $dbi->select(
1496
    table => 'table1',
1497
    where => $where,
1498
);
1499
$row = $result->all;
1500
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1501

            
1502
$where = $dbi->where;
1503
$where->clause(['and', ':key1{=}']);
1504
$where->param({key1 => undef});
1505
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1506
$row = $result->all;
1507
is_deeply($row, [{key1 => 1, key2 => 2}]);
1508

            
1509
$where = $dbi->where;
1510
$where->clause(['and', ':key1{=}']);
1511
$where->param({key1 => undef});
1512
$where->if('defined');
1513
$where->map;
1514
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1515
$row = $result->all;
1516
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1517

            
1518
$where = $dbi->where;
1519
$where->clause(['or', ':key1{=}', ':key1{=}']);
1520
$where->param({key1 => [undef, undef]});
1521
$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]});
1522
$row = $result->all;
1523
is_deeply($row, [{key1 => 1, key2 => 2}]);
1524
$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]});
1525
$row = $result->all;
1526
is_deeply($row, [{key1 => 1, key2 => 2}]);
1527

            
1528
$where = $dbi->where;
1529
$where->clause(['and', ':key1{=}']);
1530
$where->param({key1 => [undef, undef]});
1531
$where->if('defined');
1532
$where->map;
1533
$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]});
1534
$row = $result->all;
1535
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1536
$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]});
1537
$row = $result->all;
1538
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1539

            
1540
$where = $dbi->where;
1541
$where->clause(['and', ':key1{=}']);
1542
$where->param({key1 => 0});
1543
$where->if('length');
1544
$where->map;
1545
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1546
$row = $result->all;
1547
is_deeply($row, [{key1 => 1, key2 => 2}]);
1548

            
1549
$where = $dbi->where;
1550
$where->clause(['and', ':key1{=}']);
1551
$where->param({key1 => ''});
1552
$where->if('length');
1553
$where->map;
1554
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1555
$row = $result->all;
1556
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1557

            
1558
$where = $dbi->where;
1559
$where->clause(['and', ':key1{=}']);
1560
$where->param({key1 => 5});
1561
$where->if(sub { ($_[0] || '') eq 5 });
1562
$where->map;
1563
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1564
$row = $result->all;
1565
is_deeply($row, [{key1 => 1, key2 => 2}]);
1566

            
1567
$where = $dbi->where;
1568
$where->clause(['and', ':key1{=}']);
1569
$where->param({key1 => 7});
1570
$where->if(sub { ($_[0] || '') eq 5 });
1571
$where->map;
1572
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1573
$row = $result->all;
1574
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1575

            
1576
$where = $dbi->where;
1577
$where->param({id => 1, author => 'Ken', price => 1900});
1578
$where->map(id => 'book.id',
1579
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1580
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1581
);
1582
is_deeply($where->param, {'book.id' => 1, 'book.author' => '%Ken%',
1583
  'book.price' => 1900});
1584

            
1585
$where = $dbi->where;
1586
$where->param({id => 0, author => 0, price => 0});
1587
$where->map(
1588
    id => 'book.id',
1589
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1590
    price => ['book.price', sub { '%' . $_[0] . '%' },
1591
      {if => sub { $_[0] eq 0 }}]
1592
);
1593
is_deeply($where->param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
1594

            
1595
$where = $dbi->where;
1596
$where->param({id => '', author => '', price => ''});
1597
$where->if('length');
1598
$where->map(
1599
    id => 'book.id',
1600
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1601
    price => ['book.price', sub { '%' . $_[0] . '%' },
1602
      {if => sub { $_[0] eq 1 }}]
1603
);
1604
is_deeply($where->param, {});
1605

            
1606
$where = $dbi->where;
1607
$where->param({id => undef, author => undef, price => undef});
1608
$where->if('length');
1609
$where->map(
1610
    id => 'book.id',
1611
    price => ['book.price', {if => 'exists'}]
1612
);
1613
is_deeply($where->param, {'book.price' => undef});
1614

            
1615
$where = $dbi->where;
1616
$where->param({price => 'a'});
1617
$where->if('length');
1618
$where->map(
1619
    id => ['book.id', {if => 'exists'}],
1620
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
1621
);
1622
is_deeply($where->param, {'book.price' => '%a'});
1623

            
1624
$where = $dbi->where;
1625
$where->param({id => [1, 2], author => 'Ken', price => 1900});
1626
$where->map(
1627
    id => 'book.id',
1628
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1629
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1630
);
1631
is_deeply($where->param, {'book.id' => [1, 2], 'book.author' => '%Ken%',
1632
  'book.price' => 1900});
1633

            
1634
$where = $dbi->where;
1635
$where->if('length');
1636
$where->param({id => ['', ''], author => 'Ken', price => 1900});
1637
$where->map(
1638
    id => 'book.id',
1639
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1640
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1641
);
1642
is_deeply($where->param, {'book.id' => [$dbi->not_exists, $dbi->not_exists], 'book.author' => '%Ken%',
1643
  'book.price' => 1900});
1644

            
1645
$where = $dbi->where;
1646
$where->param({id => ['', ''], author => 'Ken', price => 1900});
1647
$where->map(
1648
    id => ['book.id', {if => 'length'}],
1649
    author => ['book.author', sub { '%' . $_[0] . '%' }, {if => 'defined'}],
1650
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1651
);
1652
is_deeply($where->param, {'book.id' => [$dbi->not_exists, $dbi->not_exists], 'book.author' => '%Ken%',
1653
  'book.price' => 1900});
1654

            
1655
test 'dbi_option default';
1656
$dbi = DBIx::Custom->new;
1657
is_deeply($dbi->dbi_option, {});
1658

            
1659
test 'register_tag_processor';
1660
$dbi = DBIx::Custom->connect;
1661
$dbi->register_tag_processor(
1662
    a => sub { 1 }
1663
);
1664
is($dbi->query_builder->tag_processors->{a}->(), 1);
1665

            
1666
test 'register_tag';
1667
$dbi = DBIx::Custom->connect;
1668
$dbi->register_tag(
1669
    b => sub { 2 }
1670
);
1671
is($dbi->query_builder->tags->{b}->(), 2);
1672

            
1673
test 'table not specify exception';
1674
$dbi = DBIx::Custom->connect;
1675
eval {$dbi->insert};
1676
like($@, qr/table/);
1677
eval {$dbi->update};
1678
like($@, qr/table/);
1679
eval {$dbi->delete};
1680
like($@, qr/table/);
1681
eval {$dbi->select};
1682
like($@, qr/table/);
test cleanup
Yuki Kimoto authored on 2011-08-10
1683

            
cleanup test
Yuki Kimoto authored on 2011-08-08
1684
1;