DBIx-Custom / t / sqlite.t /
Newer Older
3464 lines | 104.672kb
cleanup test
Yuki Kimoto authored on 2011-08-06
1
use Test::More;
2
use strict;
3
use warnings;
4
use utf8;
5
use Encode qw/encode_utf8 decode_utf8/;
test cleanup
Yuki Kimoto authored on 2011-08-06
6
use FindBin;
7
use lib "$FindBin::Bin/basic";
cleanup test
Yuki Kimoto authored on 2011-08-06
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
Yuki Kimoto authored on 2011-08-06
19
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/};
cleanup test
Yuki Kimoto authored on 2011-08-06
20
sub test { print "# $_[0]\n" }
21

            
test cleanup
Yuki Kimoto authored on 2011-08-10
22
{
23
    package DBIx::Custom;
24
    has dsn => sub { 'dbi:SQLite:dbname=:memory:' }
25
}
26

            
cleanup test
Yuki Kimoto authored on 2011-08-06
27
# Constant
cleanup test
Yuki Kimoto authored on 2011-08-10
28
my $create_table1 = 'create table table1 (key1 char(255), key2 char(255));';
29
my $create_table1_2 = 'create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));';
test cleanup
Yuki Kimoto authored on 2011-08-10
30
my $create_table2 = 'create table table2 (key1 char(255), key3 char(255));';
31
my $create_table_reserved = 'create table "table" ("select", "update")';
32

            
test cleanup
Yuki Kimoto authored on 2011-08-10
33
my $q = '"';
34
my $p = '"';
cleanup test
Yuki Kimoto authored on 2011-08-06
35

            
cleanup test
Yuki Kimoto authored on 2011-08-06
36
# Variables
cleanup test
Yuki Kimoto authored on 2011-08-06
37
my $builder;
38
my $datas;
cleanup test
Yuki Kimoto authored on 2011-08-06
39
my $dbi;
40
my $sth;
41
my $source;
42
my @sources;
cleanup test
Yuki Kimoto authored on 2011-08-06
43
my $select_source;
44
my $insert_source;
45
my $update_source;
cleanup test
Yuki Kimoto authored on 2011-08-06
46
my $param;
47
my $params;
48
my $sql;
49
my $result;
50
my $row;
51
my @rows;
52
my $rows;
53
my $query;
54
my @queries;
55
my $select_query;
56
my $insert_query;
57
my $update_query;
58
my $ret_val;
59
my $infos;
60
my $model;
61
my $model2;
62
my $where;
63
my $update_param;
64
my $insert_param;
cleanup test
Yuki Kimoto authored on 2011-08-06
65
my $join;
cleanup test
Yuki Kimoto authored on 2011-08-10
66
my $binary;
cleanup test
Yuki Kimoto authored on 2011-08-06
67

            
68
# Prepare table
test cleanup
Yuki Kimoto authored on 2011-08-10
69
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
70

            
71
test 'cache';
test cleanup
Yuki Kimoto authored on 2011-08-10
72
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
73
$dbi->cache(1);
cleanup test
Yuki Kimoto authored on 2011-08-10
74
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
75
$source = 'select * from table1 where key1 = :key1 and key2 = :key2;';
76
$dbi->execute($source, {}, query => 1);
77
is_deeply($dbi->{_cached}->{$source}, 
78
          {sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2'], tables => []}, "cache");
79

            
test cleanup
Yuki Kimoto authored on 2011-08-10
80
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
81
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
82
$dbi->{_cached} = {};
83
$dbi->cache(0);
84
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
85
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
86

            
87
test 'execute';
test cleanup
Yuki Kimoto authored on 2011-08-10
88
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
89
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
90
{
91
    local $Carp::Verbose = 0;
92
    eval{$dbi->execute('select * frm table1')};
93
    like($@, qr/\Qselect * frm table1;/, "fail prepare");
94
    like($@, qr/\.t /, "fail : not verbose");
95
}
96
{
97
    local $Carp::Verbose = 1;
98
    eval{$dbi->execute('select * frm table1')};
99
    like($@, qr/Custom.*\.t /s, "fail : verbose");
100
}
101

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

            
105
$query = $dbi->execute('select * from table1 where key1 = :key1', {}, query => 1);
106
$dbi->dbh->disconnect;
107
eval{$dbi->execute($query, param => {key1 => {a => 1}})};
108
ok($@, "execute fail");
109

            
110
{
111
    local $Carp::Verbose = 0;
112
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
113
    like($@, qr/\Q.t /, "caller spec : not vebose");
114
}
115
{
116
    local $Carp::Verbose = 1;
117
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
118
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
119
}
120

            
121

            
122
test 'transaction';
test cleanup
Yuki Kimoto authored on 2011-08-10
123
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
124
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
125
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
126

            
127
$dbi->begin_work;
128

            
129
eval {
130
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
131
    die "Error";
132
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
133
};
134

            
135
$dbi->rollback if $@;
136

            
137
$result = $dbi->select(table => 'table1');
138
$rows = $result->all;
139
is_deeply($rows, [], "rollback");
140

            
141
$dbi->begin_work;
142

            
143
eval {
144
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
145
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
146
};
147

            
148
$dbi->commit unless $@;
149

            
150
$result = $dbi->select(table => 'table1');
151
$rows = $result->all;
152
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "commit");
153

            
154
$dbi->dbh->{AutoCommit} = 0;
155
eval{ $dbi->begin_work };
156
ok($@, "exception");
157
$dbi->dbh->{AutoCommit} = 1;
158

            
159

            
160
test 'method';
161
$dbi->method(
162
    one => sub { 1 }
163
);
164
$dbi->method(
165
    two => sub { 2 }
166
);
167
$dbi->method({
168
    twice => sub {
169
        my $self = shift;
170
        return $_[0] * 2;
171
    }
172
});
173

            
174
is($dbi->one, 1, "first");
175
is($dbi->two, 2, "second");
176
is($dbi->twice(5), 10 , "second");
177

            
178
eval {$dbi->XXXXXX};
179
ok($@, "not exists");
180

            
181
test 'out filter';
test cleanup
Yuki Kimoto authored on 2011-08-10
182
$dbi = DBIx::Custom->connect;
183
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
184
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
185
$dbi->register_filter(twice => sub { $_[0] * 2 });
186
$dbi->register_filter(three_times => sub { $_[0] * 3});
187
$dbi->apply_filter(
188
    'table1', 'key1' => {out => 'twice', in => 'three_times'}, 
189
              'key2' => {out => 'three_times', in => 'twice'});
190
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-06
191
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
192
$row   = $result->fetch_hash_first;
193
is_deeply($row, {key1 => 2, key2 => 6}, "insert");
194
$result = $dbi->select(table => 'table1');
195
$row   = $result->one;
196
is_deeply($row, {key1 => 6, key2 => 12}, "insert");
197

            
test cleanup
Yuki Kimoto authored on 2011-08-10
198
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
199
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
200
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
201
$dbi->register_filter(twice => sub { $_[0] * 2 });
202
$dbi->register_filter(three_times => sub { $_[0] * 3});
203
$dbi->apply_filter(
204
    'table1', 'key1' => {out => 'twice', in => 'three_times'}, 
205
              'key2' => {out => 'three_times', in => 'twice'});
206
$dbi->apply_filter(
207
    'table1', 'key1' => {out => undef}
208
); 
209
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-06
210
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
211
$row   = $result->one;
212
is_deeply($row, {key1 => 1, key2 => 6}, "insert");
213

            
test cleanup
Yuki Kimoto authored on 2011-08-10
214
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
215
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
216
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
217
$dbi->register_filter(twice => sub { $_[0] * 2 });
218
$dbi->apply_filter(
219
    'table1', 'key1' => {out => 'twice', in => 'twice'}
220
);
221
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => undef});
222
$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-06
223
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
224
$row   = $result->one;
225
is_deeply($row, {key1 => 4, key2 => 2}, "update");
226

            
test cleanup
Yuki Kimoto authored on 2011-08-10
227
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
228
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
229
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
230
$dbi->register_filter(twice => sub { $_[0] * 2 });
231
$dbi->apply_filter(
232
    'table1', 'key1' => {out => 'twice', in => 'twice'}
233
);
234
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1=> undef});
235
$dbi->delete(table => 'table1', where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
236
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
237
$rows   = $result->all;
238
is_deeply($rows, [], "delete");
239

            
test cleanup
Yuki Kimoto authored on 2011-08-10
240
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
241
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
242
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
243
$dbi->register_filter(twice => sub { $_[0] * 2 });
244
$dbi->apply_filter(
245
    'table1', 'key1' => {out => 'twice', in => 'twice'}
246
);
247
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
248
$result = $dbi->select(table => 'table1', where => {key1 => 1});
249
$result->filter({'key2' => 'twice'});
250
$rows   = $result->all;
251
is_deeply($rows, [{key1 => 4, key2 => 4}], "select");
252

            
test cleanup
Yuki Kimoto authored on 2011-08-10
253
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
254
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
255
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
256
$dbi->register_filter(twice => sub { $_[0] * 2 });
257
$dbi->apply_filter(
258
    'table1', 'key1' => {out => 'twice', in => 'twice'}
259
);
260
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
261
$result = $dbi->execute("select * from table1 where key1 = :key1 and key2 = :key2;",
262
                        param => {key1 => 1, key2 => 2},
263
                        table => ['table1']);
264
$rows   = $result->all;
265
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute");
266

            
test cleanup
Yuki Kimoto authored on 2011-08-10
267
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
268
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
269
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
270
$dbi->register_filter(twice => sub { $_[0] * 2 });
271
$dbi->apply_filter(
272
    'table1', 'key1' => {out => 'twice', in => 'twice'}
273
);
274
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
275
$result = $dbi->execute("select * from {table table1} where key1 = :key1 and key2 = :key2;",
276
                        param => {key1 => 1, key2 => 2});
277
$rows   = $result->all;
278
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute table tag");
279

            
test cleanup
Yuki Kimoto authored on 2011-08-10
280
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
281
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
282
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
283
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
284
$dbi->register_filter(twice => sub { $_[0] * 2 });
285
$dbi->register_filter(three_times => sub { $_[0] * 3 });
286
$dbi->apply_filter(
287
    'table1', 'key2' => {out => 'twice', in => 'twice'}
288
);
289
$dbi->apply_filter(
290
    'table2', 'key3' => {out => 'three_times', in => 'three_times'}
291
);
292
$dbi->insert(table => 'table1', param => {key1 => 5, key2 => 2}, filter => {key2 => undef});
293
$dbi->insert(table => 'table2', param => {key1 => 5, key3 => 6}, filter => {key3 => undef});
294
$result = $dbi->select(
295
     table => ['table1', 'table2'],
296
     column => ['key2', 'key3'],
297
     where => {'table1.key2' => 1, 'table2.key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
298

            
299
$result->filter({'key2' => 'twice'});
300
$rows   = $result->all;
301
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join");
302

            
303
$result = $dbi->select(
304
     table => ['table1', 'table2'],
305
     column => ['key2', 'key3'],
306
     where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
307

            
308
$result->filter({'key2' => 'twice'});
309
$rows   = $result->all;
310
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join : omit");
311

            
312
test 'each_column';
test cleanup
Yuki Kimoto authored on 2011-08-10
313
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
314
eval { $dbi->execute('drop table table1') };
315
eval { $dbi->execute('drop table table2') };
test cleanup
Yuki Kimoto authored on 2011-08-10
316
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
317
$dbi->execute('create table table1 (key1 Date, key2 datetime);');
cleanup test
Yuki Kimoto authored on 2011-08-06
318

            
319
$infos = [];
320
$dbi->each_column(sub {
321
    my ($self, $table, $column, $cinfo) = @_;
322
    
323
    if ($table =~ /^table/) {
324
         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
325
         push @$infos, $info;
326
    }
327
});
328
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
329
is_deeply($infos, 
330
    [
331
        ['table1', 'key1', 'key1'],
332
        ['table1', 'key2', 'key2'],
333
        ['table2', 'key1', 'key1'],
334
        ['table2', 'key3', 'key3']
335
    ]
336
    
337
);
338
test 'each_table';
test cleanup
Yuki Kimoto authored on 2011-08-10
339
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
340
eval { $dbi->execute('drop table table1') };
341
eval { $dbi->execute('drop table table2') };
test cleanup
Yuki Kimoto authored on 2011-08-10
342
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
343
$dbi->execute('create table table1 (key1 Date, key2 datetime);');
cleanup test
Yuki Kimoto authored on 2011-08-06
344

            
345
$infos = [];
346
$dbi->each_table(sub {
347
    my ($self, $table, $table_info) = @_;
348
    
349
    if ($table =~ /^table/) {
350
         my $info = [$table, $table_info->{TABLE_NAME}];
351
         push @$infos, $info;
352
    }
353
});
354
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
355
is_deeply($infos, 
356
    [
357
        ['table1', 'table1'],
358
        ['table2', 'table2'],
359
    ]
360
);
361

            
362
test 'limit';
test cleanup
Yuki Kimoto authored on 2011-08-10
363
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
364
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
365
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
366
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
367
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
368
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
369
$dbi->register_tag(
370
    limit => sub {
371
        my ($count, $offset) = @_;
372
        
373
        my $s = '';
374
        $s .= "limit $count";
375
        $s .= " offset $offset" if defined $offset;
376
        
377
        return [$s, []];
378
    }
379
);
380
$rows = $dbi->select(
381
  table => 'table1',
382
  where => {key1 => 1},
383
  append => "order by key2 {limit 1 0}"
384
)->all;
385
is_deeply($rows, [{key1 => 1, key2 => 2}]);
386
$rows = $dbi->select(
387
  table => 'table1',
388
  where => {key1 => 1},
389
  append => "order by key2 {limit 2 1}"
390
)->all;
391
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
392
$rows = $dbi->select(
393
  table => 'table1',
394
  where => {key1 => 1},
395
  append => "order by key2 {limit 1}"
396
)->all;
397
is_deeply($rows, [{key1 => 1, key2 => 2}]);
398

            
399
test 'connect super';
400
{
401
    package MyDBI;
402
    
403
    use base 'DBIx::Custom';
404
    sub connect {
405
        my $self = shift->SUPER::connect(@_);
406
        
407
        return $self;
408
    }
409
    
410
    sub new {
411
        my $self = shift->SUPER::new(@_);
412
        
413
        return $self;
414
    }
415
}
416

            
test cleanup
Yuki Kimoto authored on 2011-08-10
417
$dbi = MyDBI->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
418
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
419
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
420
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
421
is($dbi->select(table => 'table1')->one->{key1}, 1);
422

            
test cleanup
Yuki Kimoto authored on 2011-08-10
423
$dbi = MyDBI->new;
cleanup test
Yuki Kimoto authored on 2011-08-06
424
$dbi->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
425
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
426
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
427
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
428
is($dbi->select(table => 'table1')->one->{key1}, 1);
429

            
430
{
431
    package MyDBI2;
432
    
433
    use base 'DBIx::Custom';
434
    sub connect {
435
        my $self = shift->SUPER::new(@_);
436
        $self->connect;
437
        
438
        return $self;
439
    }
440
}
441

            
test cleanup
Yuki Kimoto authored on 2011-08-10
442
$dbi = MyDBI->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
443
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
444
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
445
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
446
is($dbi->select(table => 'table1')->one->{key1}, 1);
447

            
448
test 'end_filter';
test cleanup
Yuki Kimoto authored on 2011-08-10
449
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
450
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
451
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
452
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
453
$result = $dbi->select(table => 'table1');
454
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
455
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 });
456
$row = $result->fetch_first;
457
is_deeply($row, [6, 40]);
458

            
test cleanup
Yuki Kimoto authored on 2011-08-10
459
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
460
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
461
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
462
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
463
$result = $dbi->select(table => 'table1');
464
$result->filter([qw/key1 key2/] => sub { $_[0] * 2 });
465
$result->end_filter([[qw/key1 key2/] => sub { $_[0] * 3 }]);
466
$row = $result->fetch_first;
467
is_deeply($row, [6, 12]);
468

            
test cleanup
Yuki Kimoto authored on 2011-08-10
469
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
470
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
471
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
472
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
473
$result = $dbi->select(table => 'table1');
474
$result->filter([[qw/key1 key2/] => sub { $_[0] * 2 }]);
475
$result->end_filter([qw/key1 key2/] => sub { $_[0] * 3 });
476
$row = $result->fetch_first;
477
is_deeply($row, [6, 12]);
478

            
479
$dbi->register_filter(five_times => sub { $_[0] * 5 });
480
$result = $dbi->select(table => 'table1');
481
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
482
$result->end_filter({key1 => sub { $_[0] * 3 }, key2 => 'five_times' });
483
$row = $result->one;
484
is_deeply($row, {key1 => 6, key2 => 40});
485

            
486
$dbi->register_filter(five_times => sub { $_[0] * 5 });
487
$dbi->apply_filter('table1',
488
    key1 => {end => sub { $_[0] * 3 } },
489
    key2 => {end => 'five_times'}
490
);
491
$result = $dbi->select(table => 'table1');
492
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
493
$row = $result->one;
494
is_deeply($row, {key1 => 6, key2 => 40}, 'apply_filter');
495

            
496
$dbi->register_filter(five_times => sub { $_[0] * 5 });
497
$dbi->apply_filter('table1',
498
    key1 => {end => sub { $_[0] * 3 } },
499
    key2 => {end => 'five_times'}
500
);
501
$result = $dbi->select(table => 'table1');
502
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
503
$result->filter(key1 => undef);
504
$result->end_filter(key1 => undef);
505
$row = $result->one;
506
is_deeply($row, {key1 => 1, key2 => 40}, 'apply_filter overwrite');
507

            
508
test 'remove_end_filter and remove_filter';
test cleanup
Yuki Kimoto authored on 2011-08-10
509
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
510
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
511
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
512
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
513
$result = $dbi->select(table => 'table1');
514
$row = $result
515
       ->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 })
516
       ->remove_filter
517
       ->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 })
518
       ->remove_end_filter
519
       ->fetch_first;
520
is_deeply($row, [1, 2]);
521

            
522
test 'empty where select';
test cleanup
Yuki Kimoto authored on 2011-08-10
523
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
524
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
525
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
526
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
527
$result = $dbi->select(table => 'table1', where => {});
528
$row = $result->one;
529
is_deeply($row, {key1 => 1, key2 => 2});
530

            
531
test 'select query option';
test cleanup
Yuki Kimoto authored on 2011-08-10
532
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
533
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
534
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
535
$query = $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, query => 1);
536
is(ref $query, 'DBIx::Custom::Query');
537
$query = $dbi->update(table => 'table1', where => {key1 => 1}, param => {key2 => 2}, query => 1);
538
is(ref $query, 'DBIx::Custom::Query');
539
$query = $dbi->delete(table => 'table1', where => {key1 => 1}, query => 1);
540
is(ref $query, 'DBIx::Custom::Query');
541
$query = $dbi->select(table => 'table1', where => {key1 => 1, key2 => 2}, query => 1);
542
is(ref $query, 'DBIx::Custom::Query');
543

            
map cleanup
Yuki Kimoto authored on 2011-08-09
544
test 'where';
test cleanup
Yuki Kimoto authored on 2011-08-10
545
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
546
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
547
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
548
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
549
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
550
$where = $dbi->where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
551
is("$where", "where ( key1 = :key1 and key2 = :key2 )", 'no param');
552

            
553
$where = $dbi->where
554
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
555
             ->param({key1 => 1});
556

            
557
$result = $dbi->select(
558
    table => 'table1',
559
    where => $where
560
);
561
$row = $result->all;
562
is_deeply($row, [{key1 => 1, key2 => 2}]);
563

            
564
$result = $dbi->select(
565
    table => 'table1',
566
    where => [
567
        ['and', 'key1 = :key1', 'key2 = :key2'],
568
        {key1 => 1}
569
    ]
570
);
571
$row = $result->all;
572
is_deeply($row, [{key1 => 1, key2 => 2}]);
573

            
574
$where = $dbi->where
575
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
576
             ->param({key1 => 1, key2 => 2});
577
$result = $dbi->select(
578
    table => 'table1',
579
    where => $where
580
);
581
$row = $result->all;
582
is_deeply($row, [{key1 => 1, key2 => 2}]);
583

            
584
$where = $dbi->where
585
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
586
             ->param({});
587
$result = $dbi->select(
588
    table => 'table1',
589
    where => $where,
590
);
591
$row = $result->all;
592
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
593

            
594
$where = $dbi->where
595
             ->clause(['and', ['or', 'key1 > :key1', 'key1 < :key1'], 'key2 = :key2'])
596
             ->param({key1 => [0, 3], key2 => 2});
597
$result = $dbi->select(
598
    table => 'table1',
599
    where => $where,
600
); 
601
$row = $result->all;
602
is_deeply($row, [{key1 => 1, key2 => 2}]);
603

            
604
$where = $dbi->where;
605
$result = $dbi->select(
606
    table => 'table1',
607
    where => $where
608
);
609
$row = $result->all;
610
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
611

            
612
eval {
613
$where = $dbi->where
614
             ->clause(['uuu']);
615
$result = $dbi->select(
616
    table => 'table1',
617
    where => $where
618
);
619
};
620
ok($@);
621

            
622
$where = $dbi->where;
623
is("$where", '');
624

            
625
$where = $dbi->where
626
             ->clause(['or', ('key1 = :key1') x 2])
627
             ->param({key1 => [1, 3]});
628
$result = $dbi->select(
629
    table => 'table1',
630
    where => $where,
631
);
632
$row = $result->all;
633
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
634

            
635
$where = $dbi->where
636
             ->clause(['or', ('key1 = :key1') x 2])
637
             ->param({key1 => [1]});
638
$result = $dbi->select(
639
    table => 'table1',
640
    where => $where,
641
);
642
$row = $result->all;
643
is_deeply($row, [{key1 => 1, key2 => 2}]);
644

            
645
$where = $dbi->where
646
             ->clause(['or', ('key1 = :key1') x 2])
647
             ->param({key1 => 1});
648
$result = $dbi->select(
649
    table => 'table1',
650
    where => $where,
651
);
652
$row = $result->all;
653
is_deeply($row, [{key1 => 1, key2 => 2}]);
654

            
655
$where = $dbi->where
656
             ->clause('key1 = :key1')
657
             ->param({key1 => 1});
658
$result = $dbi->select(
659
    table => 'table1',
660
    where => $where,
661
);
662
$row = $result->all;
663
is_deeply($row, [{key1 => 1, key2 => 2}]);
664

            
665
$where = $dbi->where
666
             ->clause('key1 = :key1 key2 = :key2')
667
             ->param({key1 => 1});
668
eval{$where->to_string};
669
like($@, qr/one column/);
670

            
671
$where = $dbi->where
672
             ->clause(['or', ('key1 = :key1') x 3])
673
             ->param({key1 => [$dbi->not_exists, 1, 3]});
674
$result = $dbi->select(
675
    table => 'table1',
676
    where => $where,
677
);
678
$row = $result->all;
679
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
680

            
681
$where = $dbi->where
682
             ->clause(['or', ('key1 = :key1') x 3])
683
             ->param({key1 => [1, $dbi->not_exists, 3]});
684
$result = $dbi->select(
685
    table => 'table1',
686
    where => $where,
687
);
688
$row = $result->all;
689
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
690

            
691
$where = $dbi->where
692
             ->clause(['or', ('key1 = :key1') x 3])
693
             ->param({key1 => [1, 3, $dbi->not_exists]});
694
$result = $dbi->select(
695
    table => 'table1',
696
    where => $where,
697
);
698
$row = $result->all;
699
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
700

            
701
$where = $dbi->where
702
             ->clause(['or', ('key1 = :key1') x 3])
703
             ->param({key1 => [1, $dbi->not_exists, $dbi->not_exists]});
704
$result = $dbi->select(
705
    table => 'table1',
706
    where => $where,
707
);
708
$row = $result->all;
709
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
710

            
711
$where = $dbi->where
712
             ->clause(['or', ('key1 = :key1') x 3])
713
             ->param({key1 => [$dbi->not_exists, 1, $dbi->not_exists]});
714
$result = $dbi->select(
715
    table => 'table1',
716
    where => $where,
717
);
718
$row = $result->all;
719
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
720

            
721
$where = $dbi->where
722
             ->clause(['or', ('key1 = :key1') x 3])
723
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists, 1]});
724
$result = $dbi->select(
725
    table => 'table1',
726
    where => $where,
727
);
728
$row = $result->all;
729
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
730

            
731
$where = $dbi->where
732
             ->clause(['or', ('key1 = :key1') x 3])
733
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists, $dbi->not_exists]});
734
$result = $dbi->select(
735
    table => 'table1',
736
    where => $where,
737
);
738
$row = $result->all;
739
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
740

            
741
$where = $dbi->where
742
             ->clause(['or', ('key1 = :key1') x 3])
743
             ->param({key1 => []});
744
$result = $dbi->select(
745
    table => 'table1',
746
    where => $where,
747
);
748
$row = $result->all;
749
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
750

            
751
$where = $dbi->where
752
             ->clause(['and', '{> key1}', '{< key1}' ])
753
             ->param({key1 => [2, $dbi->not_exists]});
754
$result = $dbi->select(
755
    table => 'table1',
756
    where => $where,
757
);
758
$row = $result->all;
759
is_deeply($row, [{key1 => 3, key2 => 4}], 'not_exists');
760

            
761
$where = $dbi->where
762
             ->clause(['and', '{> key1}', '{< key1}' ])
763
             ->param({key1 => [$dbi->not_exists, 2]});
764
$result = $dbi->select(
765
    table => 'table1',
766
    where => $where,
767
);
768
$row = $result->all;
769
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
770

            
771
$where = $dbi->where
772
             ->clause(['and', '{> key1}', '{< key1}' ])
773
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists]});
774
$result = $dbi->select(
775
    table => 'table1',
776
    where => $where,
777
);
778
$row = $result->all;
779
is_deeply($row, [{key1 => 1, key2 => 2},{key1 => 3, key2 => 4}], 'not_exists');
780

            
781
$where = $dbi->where
782
             ->clause(['and', '{> key1}', '{< key1}' ])
783
             ->param({key1 => [0, 2]});
784
$result = $dbi->select(
785
    table => 'table1',
786
    where => $where,
787
);
788
$row = $result->all;
789
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
790

            
791
$where = $dbi->where
792
             ->clause(['and', 'key1 is not null', 'key2 is not null' ]);
793
$result = $dbi->select(
794
    table => 'table1',
795
    where => $where,
796
);
797
$row = $result->all;
798
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
799

            
800
eval {$dbi->where(ppp => 1) };
801
like($@, qr/invalid/);
802

            
803
$where = $dbi->where(
804
    clause => ['and', ['or'], ['and', 'key1 = :key1', 'key2 = :key2']],
805
    param => {key1 => 1, key2 => 2}
806
);
807
$result = $dbi->select(
808
    table => 'table1',
809
    where => $where,
810
);
811
$row = $result->all;
812
is_deeply($row, [{key1 => 1, key2 => 2}]);
813

            
814

            
815
$where = $dbi->where(
816
    clause => ['and', ['or'], ['or', ':key1', ':key2']],
817
    param => {}
818
);
819
$result = $dbi->select(
820
    table => 'table1',
821
    where => $where,
822
);
823
$row = $result->all;
824
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
825

            
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
826
$where = $dbi->where;
827
$where->clause(['and', ':key1{=}']);
828
$where->param({key1 => undef});
829
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
830
$row = $result->all;
831
is_deeply($row, [{key1 => 1, key2 => 2}]);
832

            
833
$where = $dbi->where;
834
$where->clause(['and', ':key1{=}']);
835
$where->param({key1 => undef});
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
836
$where->if('defined');
map cleanup
Yuki Kimoto authored on 2011-08-09
837
$where->map;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
838
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
839
$row = $result->all;
840
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
841

            
842
$where = $dbi->where;
843
$where->clause(['or', ':key1{=}', ':key1{=}']);
844
$where->param({key1 => [undef, undef]});
845
$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]});
846
$row = $result->all;
847
is_deeply($row, [{key1 => 1, key2 => 2}]);
848
$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]});
849
$row = $result->all;
850
is_deeply($row, [{key1 => 1, key2 => 2}]);
851

            
852
$where = $dbi->where;
853
$where->clause(['and', ':key1{=}']);
854
$where->param({key1 => [undef, undef]});
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
855
$where->if('defined');
map cleanup
Yuki Kimoto authored on 2011-08-09
856
$where->map;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
857
$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]});
858
$row = $result->all;
859
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
860
$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]});
861
$row = $result->all;
862
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
863

            
864
$where = $dbi->where;
865
$where->clause(['and', ':key1{=}']);
866
$where->param({key1 => 0});
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
867
$where->if('length');
map cleanup
Yuki Kimoto authored on 2011-08-09
868
$where->map;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
869
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
870
$row = $result->all;
871
is_deeply($row, [{key1 => 1, key2 => 2}]);
872

            
873
$where = $dbi->where;
874
$where->clause(['and', ':key1{=}']);
875
$where->param({key1 => ''});
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
876
$where->if('length');
map cleanup
Yuki Kimoto authored on 2011-08-09
877
$where->map;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
878
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
879
$row = $result->all;
880
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
881

            
882
$where = $dbi->where;
883
$where->clause(['and', ':key1{=}']);
884
$where->param({key1 => 5});
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
885
$where->if(sub { ($_[0] || '') eq 5 });
map cleanup
Yuki Kimoto authored on 2011-08-09
886
$where->map;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
887
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
888
$row = $result->all;
889
is_deeply($row, [{key1 => 1, key2 => 2}]);
890

            
891
$where = $dbi->where;
892
$where->clause(['and', ':key1{=}']);
893
$where->param({key1 => 7});
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
894
$where->if(sub { ($_[0] || '') eq 5 });
map cleanup
Yuki Kimoto authored on 2011-08-09
895
$where->map;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
896
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
897
$row = $result->all;
898
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
899

            
added tests
Yuki Kimoto authored on 2011-08-09
900
$where = $dbi->where;
901
$where->param({id => 1, author => 'Ken', price => 1900});
902
$where->map(id => 'book.id',
903
    author => ['book.author', sub { '%' . $_[0] . '%' }],
904
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
905
);
906
is_deeply($where->param, {'book.id' => 1, 'book.author' => '%Ken%',
907
  'book.price' => 1900});
908

            
909
$where = $dbi->where;
910
$where->param({id => 0, author => 0, price => 0});
911
$where->map(
912
    id => 'book.id',
913
    author => ['book.author', sub { '%' . $_[0] . '%' }],
914
    price => ['book.price', sub { '%' . $_[0] . '%' },
915
      {if => sub { $_[0] eq 0 }}]
916
);
917
is_deeply($where->param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
918

            
919
$where = $dbi->where;
920
$where->param({id => '', author => '', price => ''});
921
$where->if('length');
922
$where->map(
923
    id => 'book.id',
924
    author => ['book.author', sub { '%' . $_[0] . '%' }],
925
    price => ['book.price', sub { '%' . $_[0] . '%' },
926
      {if => sub { $_[0] eq 1 }}]
927
);
928
is_deeply($where->param, {});
929

            
930
$where = $dbi->where;
931
$where->param({id => undef, author => undef, price => undef});
932
$where->if('length');
933
$where->map(
934
    id => 'book.id',
935
    price => ['book.price', {if => 'exists'}]
936
);
937
is_deeply($where->param, {'book.price' => undef});
938

            
939
$where = $dbi->where;
940
$where->param({price => 'a'});
941
$where->if('length');
942
$where->map(
943
    id => ['book.id', {if => 'exists'}],
944
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
945
);
946
is_deeply($where->param, {'book.price' => '%a'});
947

            
fixed if is not converted to...
Yuki Kimoto authored on 2011-08-09
948
$where = $dbi->where;
949
$where->param({id => [1, 2], author => 'Ken', price => 1900});
950
$where->map(
951
    id => 'book.id',
952
    author => ['book.author', sub { '%' . $_[0] . '%' }],
953
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
954
);
955
is_deeply($where->param, {'book.id' => [1, 2], 'book.author' => '%Ken%',
956
  'book.price' => 1900});
957

            
958
$where = $dbi->where;
959
$where->if('length');
960
$where->param({id => ['', ''], author => 'Ken', price => 1900});
961
$where->map(
962
    id => 'book.id',
963
    author => ['book.author', sub { '%' . $_[0] . '%' }],
964
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
965
);
966
is_deeply($where->param, {'book.id' => [$dbi->not_exists, $dbi->not_exists], 'book.author' => '%Ken%',
967
  'book.price' => 1900});
968

            
969
$where = $dbi->where;
970
$where->param({id => ['', ''], author => 'Ken', price => 1900});
971
$where->map(
972
    id => ['book.id', {if => 'length'}],
973
    author => ['book.author', sub { '%' . $_[0] . '%' }, {if => 'defined'}],
974
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
975
);
976
is_deeply($where->param, {'book.id' => [$dbi->not_exists, $dbi->not_exists], 'book.author' => '%Ken%',
977
  'book.price' => 1900});
cleanup test
Yuki Kimoto authored on 2011-08-06
978

            
979
test 'dbi_option default';
980
$dbi = DBIx::Custom->new;
981
is_deeply($dbi->dbi_option, {});
982

            
983
test 'register_tag_processor';
test cleanup
Yuki Kimoto authored on 2011-08-10
984
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
985
$dbi->register_tag_processor(
986
    a => sub { 1 }
987
);
988
is($dbi->query_builder->tag_processors->{a}->(), 1);
989

            
990
test 'register_tag';
test cleanup
Yuki Kimoto authored on 2011-08-10
991
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
992
$dbi->register_tag(
993
    b => sub { 2 }
994
);
995
is($dbi->query_builder->tags->{b}->(), 2);
996

            
997
test 'table not specify exception';
test cleanup
Yuki Kimoto authored on 2011-08-10
998
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
999
eval {$dbi->insert};
1000
like($@, qr/table/);
1001
eval {$dbi->update};
1002
like($@, qr/table/);
1003
eval {$dbi->delete};
1004
like($@, qr/table/);
1005
eval {$dbi->select};
1006
like($@, qr/table/);
1007

            
1008

            
1009
test 'more tests';
test cleanup
Yuki Kimoto authored on 2011-08-10
1010
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1011
eval{$dbi->apply_filter('table', 'column', [])};
1012
like($@, qr/apply_filter/);
1013

            
1014
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
1015
like($@, qr/apply_filter/);
1016

            
1017
$dbi->apply_filter(
1018

            
1019
);
test cleanup
Yuki Kimoto authored on 2011-08-10
1020
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1021
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1022
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1023
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1024
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1025
$dbi->apply_filter('table1', 'key2', 
1026
                   {in => sub { $_[0] * 3 }, out => sub { $_[0] * 2 }});
1027
$rows = $dbi->select(table => 'table1', where => {key2 => 1})->all;
1028
is_deeply($rows, [{key1 => 1, key2 => 6}]);
1029

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1030
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1031
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1032
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1033
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1034
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1035
$dbi->apply_filter('table1', 'key2', {});
1036
$rows = $dbi->select(table => 'table1', where => {key2 => 2})->all;
1037
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1038

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1039
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1040
eval {$dbi->apply_filter('table1', 'key2', {out => 'no'})};
1041
like($@, qr/not registered/);
1042
eval {$dbi->apply_filter('table1', 'key2', {in => 'no'})};
1043
like($@, qr/not registered/);
1044
$dbi->method({one => sub { 1 }});
1045
is($dbi->one, 1);
1046

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1047
eval{DBIx::Custom->connect(dsn => undef)};
cleanup test
Yuki Kimoto authored on 2011-08-06
1048
like($@, qr/_connect/);
1049

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1050
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1051
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1052
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1053
$dbi->register_filter(twice => sub { $_[0] * 2 });
1054
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
1055
             filter => {key1 => 'twice'});
1056
$row = $dbi->select(table => 'table1')->one;
1057
is_deeply($row, {key1 => 2, key2 => 2});
1058
eval {$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
1059
             filter => {key1 => 'no'}) };
1060
like($@, qr//);
1061

            
1062
$dbi->register_filter(one => sub { });
1063
$dbi->default_fetch_filter('one');
1064
ok($dbi->default_fetch_filter);
1065
$dbi->default_bind_filter('one');
1066
ok($dbi->default_bind_filter);
1067
eval{$dbi->default_fetch_filter('no')};
1068
like($@, qr/not registered/);
1069
eval{$dbi->default_bind_filter('no')};
1070
like($@, qr/not registered/);
1071
$dbi->default_bind_filter(undef);
1072
ok(!defined $dbi->default_bind_filter);
1073
$dbi->default_fetch_filter(undef);
1074
ok(!defined $dbi->default_fetch_filter);
1075
eval {$dbi->execute('select * from table1 {} {= author') };
1076
like($@, qr/Tag not finished/);
1077

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1078
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1079
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1080
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1081
$dbi->register_filter(one => sub { 1 });
1082
$result = $dbi->select(table => 'table1');
1083
eval {$result->filter(key1 => 'no')};
1084
like($@, qr/not registered/);
1085
eval {$result->end_filter(key1 => 'no')};
1086
like($@, qr/not registered/);
1087
$result->default_filter(undef);
1088
ok(!defined $result->default_filter);
1089
$result->default_filter('one');
1090
is($result->default_filter->(), 1);
1091

            
1092
test 'dbi_option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1093
$dbi = DBIx::Custom->connect(dbi_option => {PrintError => 1});
cleanup test
Yuki Kimoto authored on 2011-08-06
1094
ok($dbi->dbh->{PrintError});
test cleanup
Yuki Kimoto authored on 2011-08-10
1095
$dbi = DBIx::Custom->connect(dbi_options => {PrintError => 1});
cleanup test
Yuki Kimoto authored on 2011-08-06
1096
ok($dbi->dbh->{PrintError});
1097

            
1098
test 'DBIx::Custom::Result stash()';
1099
$result = DBIx::Custom::Result->new;
1100
is_deeply($result->stash, {}, 'default');
1101
$result->stash->{foo} = 1;
1102
is($result->stash->{foo}, 1, 'get and set');
1103

            
1104
test 'filter __ expression';
test cleanup
Yuki Kimoto authored on 2011-08-10
1105
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1106
eval { $dbi->execute('drop table company') };
1107
eval { $dbi->execute('drop table location') };
cleanup test
Yuki Kimoto authored on 2011-08-06
1108
$dbi->execute('create table company (id, name, location_id)');
1109
$dbi->execute('create table location (id, name)');
1110
$dbi->apply_filter('location',
1111
  name => {in => sub { uc $_[0] } }
1112
);
1113

            
1114
$dbi->insert(table => 'company', param => {id => 1, name => 'a', location_id => 2});
1115
$dbi->insert(table => 'location', param => {id => 2, name => 'b'});
1116

            
1117
$result = $dbi->select(
1118
    table => ['company', 'location'], relation => {'company.location_id' => 'location.id'},
1119
    column => ['location.name as location__name']
1120
);
1121
is($result->fetch_first->[0], 'B');
1122

            
1123
$result = $dbi->select(
1124
    table => 'company', relation => {'company.location_id' => 'location.id'},
1125
    column => ['location.name as location__name']
1126
);
1127
is($result->fetch_first->[0], 'B');
1128

            
1129
$result = $dbi->select(
1130
    table => 'company', relation => {'company.location_id' => 'location.id'},
1131
    column => ['location.name as "location.name"']
1132
);
1133
is($result->fetch_first->[0], 'B');
1134

            
1135
test 'Model class';
1136
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
1137
$dbi = MyDBI1->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1138
eval { $dbi->execute('drop table book') };
cleanup test
Yuki Kimoto authored on 2011-08-06
1139
$dbi->execute("create table book (title, author)");
1140
$model = $dbi->model('book');
1141
$model->insert({title => 'a', author => 'b'});
1142
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
1143
$dbi->execute("create table company (name)");
1144
$model = $dbi->model('company');
1145
$model->insert({name => 'a'});
1146
is_deeply($model->list->all, [{name => 'a'}], 'basic');
1147
is($dbi->models->{'book'}, $dbi->model('book'));
1148
is($dbi->models->{'company'}, $dbi->model('company'));
1149

            
1150
{
1151
    package MyDBI4;
1152

            
1153
    use strict;
1154
    use warnings;
1155

            
1156
    use base 'DBIx::Custom';
1157

            
1158
    sub connect {
1159
        my $self = shift->SUPER::connect(@_);
1160
        
1161
        $self->include_model(
1162
            MyModel2 => [
1163
                'book',
1164
                {class => 'Company', name => 'company'}
1165
            ]
1166
        );
1167
    }
1168

            
1169
    package MyModel2::Base1;
1170

            
1171
    use strict;
1172
    use warnings;
1173

            
1174
    use base 'DBIx::Custom::Model';
1175

            
1176
    package MyModel2::book;
1177

            
1178
    use strict;
1179
    use warnings;
1180

            
1181
    use base 'MyModel2::Base1';
1182

            
1183
    sub insert {
1184
        my ($self, $param) = @_;
1185
        
1186
        return $self->SUPER::insert(param => $param);
1187
    }
1188

            
1189
    sub list { shift->select; }
1190

            
1191
    package MyModel2::Company;
1192

            
1193
    use strict;
1194
    use warnings;
1195

            
1196
    use base 'MyModel2::Base1';
1197

            
1198
    sub insert {
1199
        my ($self, $param) = @_;
1200
        
1201
        return $self->SUPER::insert(param => $param);
1202
    }
1203

            
1204
    sub list { shift->select; }
1205
}
test cleanup
Yuki Kimoto authored on 2011-08-10
1206
$dbi = MyDBI4->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1207
eval { $dbi->execute('drop table book') };
cleanup test
Yuki Kimoto authored on 2011-08-06
1208
$dbi->execute("create table book (title, author)");
1209
$model = $dbi->model('book');
1210
$model->insert({title => 'a', author => 'b'});
1211
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
1212
$dbi->execute("create table company (name)");
1213
$model = $dbi->model('company');
1214
$model->insert({name => 'a'});
1215
is_deeply($model->list->all, [{name => 'a'}], 'basic');
1216

            
1217
{
1218
     package MyDBI5;
1219

            
1220
    use strict;
1221
    use warnings;
1222

            
1223
    use base 'DBIx::Custom';
1224

            
1225
    sub connect {
1226
        my $self = shift->SUPER::connect(@_);
1227
        
1228
        $self->include_model('MyModel4');
1229
    }
1230
}
test cleanup
Yuki Kimoto authored on 2011-08-10
1231
$dbi = MyDBI5->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1232
eval { $dbi->execute('drop table company') };
1233
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
1234
$dbi->execute("create table company (name)");
1235
$dbi->execute("create table table1 (key1)");
1236
$model = $dbi->model('company');
1237
$model->insert({name => 'a'});
1238
is_deeply($model->list->all, [{name => 'a'}], 'include all model');
1239
$dbi->insert(table => 'table1', param => {key1 => 1});
1240
$model = $dbi->model('book');
1241
is_deeply($model->list->all, [{key1 => 1}], 'include all model');
1242

            
1243
test 'primary_key';
1244
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
1245
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1246
$model = $dbi->model('book');
1247
$model->primary_key(['id', 'number']);
1248
is_deeply($model->primary_key, ['id', 'number']);
1249

            
1250
test 'columns';
1251
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
1252
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1253
$model = $dbi->model('book');
1254
$model->columns(['id', 'number']);
1255
is_deeply($model->columns, ['id', 'number']);
1256

            
1257
test 'setup_model';
1258
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
1259
$dbi = MyDBI1->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1260
eval { $dbi->execute('drop table book') };
1261
eval { $dbi->execute('drop table company') };
1262
eval { $dbi->execute('drop table test') };
1263

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1264
$dbi->execute('create table book (id)');
1265
$dbi->execute('create table company (id, name);');
1266
$dbi->execute('create table test (id, name, primary key (id, name));');
1267
$dbi->setup_model;
1268
is_deeply($dbi->model('book')->columns, ['id']);
1269
is_deeply($dbi->model('company')->columns, ['id', 'name']);
1270

            
1271
test 'delete_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
1272
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1273
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1274
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1275
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1276
$dbi->delete_at(
1277
    table => 'table1',
1278
    primary_key => ['key1', 'key2'],
1279
    where => [1, 2],
1280
);
1281
is_deeply($dbi->select(table => 'table1')->all, []);
1282

            
1283
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1284
$dbi->delete_at(
1285
    table => 'table1',
1286
    primary_key => 'key1',
1287
    where => 1,
1288
);
1289
is_deeply($dbi->select(table => 'table1')->all, []);
1290

            
1291
test 'insert_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
1292
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1293
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1294
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1295
$dbi->insert_at(
1296
    primary_key => ['key1', 'key2'], 
1297
    table => 'table1',
1298
    where => [1, 2],
1299
    param => {key3 => 3}
1300
);
1301
is($dbi->select(table => 'table1')->one->{key1}, 1);
1302
is($dbi->select(table => 'table1')->one->{key2}, 2);
1303
is($dbi->select(table => 'table1')->one->{key3}, 3);
1304

            
1305
$dbi->delete_all(table => 'table1');
1306
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1307
$dbi->insert_at(
1308
    primary_key => 'key1', 
1309
    table => 'table1',
1310
    where => 1,
1311
    param => {key2 => 2, key3 => 3}
1312
);
1313

            
1314
is($dbi->select(table => 'table1')->one->{key1}, 1);
1315
is($dbi->select(table => 'table1')->one->{key2}, 2);
1316
is($dbi->select(table => 'table1')->one->{key3}, 3);
1317

            
1318
eval {
1319
    $dbi->insert_at(
1320
        table => 'table1',
1321
        primary_key => ['key1', 'key2'],
1322
        where => {},
1323
        param => {key1 => 1, key2 => 2, key3 => 3},
1324
    );
1325
};
1326
like($@, qr/must be/);
1327

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1328
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1329
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1330
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1331
$dbi->insert_at(
1332
    {key3 => 3},
1333
    primary_key => ['key1', 'key2'], 
1334
    table => 'table1',
1335
    where => [1, 2],
1336
);
1337
is($dbi->select(table => 'table1')->one->{key1}, 1);
1338
is($dbi->select(table => 'table1')->one->{key2}, 2);
1339
is($dbi->select(table => 'table1')->one->{key3}, 3);
1340

            
1341
test 'update_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
1342
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1343
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1344
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1345
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1346
$dbi->update_at(
1347
    table => 'table1',
1348
    primary_key => ['key1', 'key2'],
1349
    where => [1, 2],
1350
    param => {key3 => 4}
1351
);
1352
is($dbi->select(table => 'table1')->one->{key1}, 1);
1353
is($dbi->select(table => 'table1')->one->{key2}, 2);
1354
is($dbi->select(table => 'table1')->one->{key3}, 4);
1355

            
1356
$dbi->delete_all(table => 'table1');
1357
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1358
$dbi->update_at(
1359
    table => 'table1',
1360
    primary_key => 'key1',
1361
    where => 1,
1362
    param => {key3 => 4}
1363
);
1364
is($dbi->select(table => 'table1')->one->{key1}, 1);
1365
is($dbi->select(table => 'table1')->one->{key2}, 2);
1366
is($dbi->select(table => 'table1')->one->{key3}, 4);
1367

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1368
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1369
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1370
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1371
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1372
$dbi->update_at(
1373
    {key3 => 4},
1374
    table => 'table1',
1375
    primary_key => ['key1', 'key2'],
1376
    where => [1, 2]
1377
);
1378
is($dbi->select(table => 'table1')->one->{key1}, 1);
1379
is($dbi->select(table => 'table1')->one->{key2}, 2);
1380
is($dbi->select(table => 'table1')->one->{key3}, 4);
1381

            
1382
test 'select_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
1383
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1384
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1385
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1386
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1387
$result = $dbi->select_at(
1388
    table => 'table1',
1389
    primary_key => ['key1', 'key2'],
1390
    where => [1, 2]
1391
);
1392
$row = $result->one;
1393
is($row->{key1}, 1);
1394
is($row->{key2}, 2);
1395
is($row->{key3}, 3);
1396

            
1397
$dbi->delete_all(table => 'table1');
1398
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1399
$result = $dbi->select_at(
1400
    table => 'table1',
1401
    primary_key => 'key1',
1402
    where => 1,
1403
);
1404
$row = $result->one;
1405
is($row->{key1}, 1);
1406
is($row->{key2}, 2);
1407
is($row->{key3}, 3);
1408

            
1409
$dbi->delete_all(table => 'table1');
1410
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1411
$result = $dbi->select_at(
1412
    table => 'table1',
1413
    primary_key => ['key1', 'key2'],
1414
    where => [1, 2]
1415
);
1416
$row = $result->one;
1417
is($row->{key1}, 1);
1418
is($row->{key2}, 2);
1419
is($row->{key3}, 3);
1420

            
1421
eval {
1422
    $result = $dbi->select_at(
1423
        table => 'table1',
1424
        primary_key => ['key1', 'key2'],
1425
        where => {},
1426
    );
1427
};
1428
like($@, qr/must be/);
1429

            
1430
eval {
1431
    $result = $dbi->select_at(
1432
        table => 'table1',
1433
        primary_key => ['key1', 'key2'],
1434
        where => [1],
1435
    );
1436
};
1437
like($@, qr/same/);
1438

            
1439
eval {
1440
    $result = $dbi->update_at(
1441
        table => 'table1',
1442
        primary_key => ['key1', 'key2'],
1443
        where => {},
1444
        param => {key1 => 1, key2 => 2},
1445
    );
1446
};
1447
like($@, qr/must be/);
1448

            
1449
eval {
1450
    $result = $dbi->delete_at(
1451
        table => 'table1',
1452
        primary_key => ['key1', 'key2'],
1453
        where => {},
1454
    );
1455
};
1456
like($@, qr/must be/);
1457

            
1458
test 'columns';
1459
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
1460
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1461
$model = $dbi->model('book');
1462

            
1463

            
1464
test 'model delete_at';
1465
{
1466
    package MyDBI6;
1467
    
1468
    use base 'DBIx::Custom';
1469
    
1470
    sub connect {
1471
        my $self = shift->SUPER::connect(@_);
1472
        
1473
        $self->include_model('MyModel5');
1474
        
1475
        return $self;
1476
    }
1477
}
test cleanup
Yuki Kimoto authored on 2011-08-10
1478
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1479
eval { $dbi->execute('drop table table1') };
1480
eval { $dbi->execute('drop table table2') };
1481
eval { $dbi->execute('drop table table3') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1482
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1483
$dbi->execute("create table table2 (key1, key2, key3)");
1484
$dbi->execute("create table table3 (key1, key2, key3)");
1485
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1486
$dbi->model('table1')->delete_at(where => [1, 2]);
1487
is_deeply($dbi->select(table => 'table1')->all, []);
1488
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
1489
$dbi->model('table1_1')->delete_at(where => [1, 2]);
1490
is_deeply($dbi->select(table => 'table1')->all, []);
1491
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
1492
$dbi->model('table1_3')->delete_at(where => [1, 2]);
1493
is_deeply($dbi->select(table => 'table1')->all, []);
1494

            
1495
test 'model insert_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
1496
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1497
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1498
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1499
$dbi->model('table1')->insert_at(
1500
    where => [1, 2],
1501
    param => {key3 => 3}
1502
);
1503
$result = $dbi->model('table1')->select;
1504
$row = $result->one;
1505
is($row->{key1}, 1);
1506
is($row->{key2}, 2);
1507
is($row->{key3}, 3);
1508

            
1509
test 'model update_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
1510
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1511
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1512
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1513
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1514
$dbi->model('table1')->update_at(
1515
    where => [1, 2],
1516
    param => {key3 => 4}
1517
);
1518
$result = $dbi->model('table1')->select;
1519
$row = $result->one;
1520
is($row->{key1}, 1);
1521
is($row->{key2}, 2);
1522
is($row->{key3}, 4);
1523

            
1524
test 'model select_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
1525
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1526
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1527
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1528
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1529
$result = $dbi->model('table1')->select_at(where => [1, 2]);
1530
$row = $result->one;
1531
is($row->{key1}, 1);
1532
is($row->{key2}, 2);
1533
is($row->{key3}, 3);
1534

            
1535

            
1536
test 'mycolumn and column';
1537
{
1538
    package MyDBI7;
1539
    
1540
    use base 'DBIx::Custom';
1541
    
1542
    sub connect {
1543
        my $self = shift->SUPER::connect(@_);
1544
        
1545
        $self->include_model('MyModel6');
1546
        
1547
        
1548
        return $self;
1549
    }
1550
}
test cleanup
Yuki Kimoto authored on 2011-08-10
1551
$dbi = MyDBI7->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1552
eval { $dbi->execute('drop table table1') };
1553
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1554
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1555
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1556
$dbi->separator('__');
1557
$dbi->setup_model;
1558
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1559
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1560
$model = $dbi->model('table1');
1561
$result = $model->select(
1562
    column => [$model->mycolumn, $model->column('table2')],
1563
    where => {'table1.key1' => 1}
1564
);
1565
is_deeply($result->one,
1566
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
1567

            
1568
test 'update_param';
test cleanup
Yuki Kimoto authored on 2011-08-10
1569
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1570
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1571
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1572
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1573
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1574

            
1575
$param = {key2 => 11};
1576
$update_param = $dbi->update_param($param);
1577
$sql = <<"EOS";
1578
update table1 $update_param
1579
where key1 = 1
1580
EOS
1581
$dbi->execute($sql, param => $param);
test cleanup
Yuki Kimoto authored on 2011-08-06
1582
$result = $dbi->execute('select * from table1;', table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
1583
$rows   = $result->all;
1584
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
1585
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1586
                  "basic");
1587

            
1588

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1589
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1590
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1591
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1592
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1593
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1594

            
1595
$param = {key2 => 11, key3 => 33};
1596
$update_param = $dbi->update_param($param);
1597
$sql = <<"EOS";
1598
update table1 $update_param
1599
where key1 = 1
1600
EOS
1601
$dbi->execute($sql, param => $param);
test cleanup
Yuki Kimoto authored on 2011-08-06
1602
$result = $dbi->execute('select * from table1;', table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
1603
$rows   = $result->all;
1604
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
1605
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1606
                  "basic");
1607

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1608
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1609
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1610
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1611
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1612
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1613

            
1614
$param = {key2 => 11, key3 => 33};
1615
$update_param = $dbi->update_param($param, {no_set => 1});
1616
$sql = <<"EOS";
1617
update table1 set $update_param
1618
where key1 = 1
1619
EOS
1620
$dbi->execute($sql, param => $param);
test cleanup
Yuki Kimoto authored on 2011-08-06
1621
$result = $dbi->execute('select * from table1;', table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
1622
$rows   = $result->all;
1623
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
1624
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1625
                  "update param no_set");
1626

            
1627
            
1628
eval { $dbi->update_param({";" => 1}) };
1629
like($@, qr/not safety/);
1630

            
1631

            
1632
test 'update_param';
test cleanup
Yuki Kimoto authored on 2011-08-10
1633
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1634
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1635
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1636
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1637
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1638

            
1639
$param = {key2 => 11};
1640
$update_param = $dbi->assign_param($param);
1641
$sql = <<"EOS";
1642
update table1 set $update_param
1643
where key1 = 1
1644
EOS
1645
$dbi->execute($sql, param => $param, table => 'table1');
test cleanup
Yuki Kimoto authored on 2011-08-06
1646
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
1647
$rows   = $result->all;
1648
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
1649
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1650
                  "basic");
1651

            
1652

            
1653
test 'insert_param';
test cleanup
Yuki Kimoto authored on 2011-08-10
1654
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1655
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1656
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1657
$param = {key1 => 1, key2 => 2};
1658
$insert_param = $dbi->insert_param($param);
1659
$sql = <<"EOS";
1660
insert into table1 $insert_param
1661
EOS
1662
$dbi->execute($sql, param => $param, table => 'table1');
1663
is($dbi->select(table => 'table1')->one->{key1}, 1);
1664
is($dbi->select(table => 'table1')->one->{key2}, 2);
1665

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1666
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1667
$dbi->quote('"');
cleanup test
Yuki Kimoto authored on 2011-08-10
1668
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1669
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1670
$param = {key1 => 1, key2 => 2};
1671
$insert_param = $dbi->insert_param($param);
1672
$sql = <<"EOS";
1673
insert into table1 $insert_param
1674
EOS
1675
$dbi->execute($sql, param => $param, table => 'table1');
1676
is($dbi->select(table => 'table1')->one->{key1}, 1);
1677
is($dbi->select(table => 'table1')->one->{key2}, 2);
1678

            
1679
eval { $dbi->insert_param({";" => 1}) };
1680
like($@, qr/not safety/);
1681

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1682

            
1683
test 'join';
test cleanup
Yuki Kimoto authored on 2011-08-10
1684
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1685
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1686
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1687
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1688
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-10
1689
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1690
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
cleanup test
Yuki Kimoto authored on 2011-08-06
1691
$dbi->execute('create table table3 (key3 int, key4 int);');
cleanup test
Yuki Kimoto authored on 2011-08-06
1692
$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4});
1693
$rows = $dbi->select(
1694
    table => 'table1',
1695
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
1696
    where   => {'table1.key2' => 2},
1697
    join  => ['left outer join table2 on table1.key1 = table2.key1']
1698
)->all;
1699
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]);
1700

            
1701
$rows = $dbi->select(
1702
    table => 'table1',
1703
    where   => {'key1' => 1},
1704
    join  => ['left outer join table2 on table1.key1 = table2.key1']
1705
)->all;
1706
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1707

            
1708
eval {
1709
    $rows = $dbi->select(
1710
        table => 'table1',
1711
        column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
1712
        where   => {'table1.key2' => 2},
1713
        join  => {'table1.key1' => 'table2.key1'}
1714
    );
1715
};
1716
like ($@, qr/array/);
1717

            
1718
$rows = $dbi->select(
1719
    table => 'table1',
1720
    where   => {'key1' => 1},
1721
    join  => ['left outer join table2 on table1.key1 = table2.key1',
1722
              'left outer join table3 on table2.key3 = table3.key3']
1723
)->all;
1724
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1725

            
1726
$rows = $dbi->select(
1727
    column => 'table3.key4 as table3__key4',
1728
    table => 'table1',
1729
    where   => {'table1.key1' => 1},
1730
    join  => ['left outer join table2 on table1.key1 = table2.key1',
1731
              'left outer join table3 on table2.key3 = table3.key3']
1732
)->all;
1733
is_deeply($rows, [{table3__key4 => 4}]);
1734

            
1735
$rows = $dbi->select(
1736
    column => 'table1.key1 as table1__key1',
1737
    table => 'table1',
1738
    where   => {'table3.key4' => 4},
1739
    join  => ['left outer join table2 on table1.key1 = table2.key1',
1740
              'left outer join table3 on table2.key3 = table3.key3']
1741
)->all;
1742
is_deeply($rows, [{table1__key1 => 1}]);
1743

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1744
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1745
$dbi->quote('"');
cleanup test
Yuki Kimoto authored on 2011-08-10
1746
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1747
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1748
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
1749
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1750
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
1751
$rows = $dbi->select(
1752
    table => 'table1',
1753
    column => '"table1"."key1" as "table1_key1", "table2"."key1" as "table2_key1", "key2", "key3"',
1754
    where   => {'table1.key2' => 2},
1755
    join  => ['left outer join "table2" on "table1"."key1" = "table2"."key1"'],
1756
)->all;
1757
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}],
1758
          'quote');
1759

            
1760
{
1761
    package MyDBI8;
1762
    
1763
    use base 'DBIx::Custom';
1764
    
1765
    sub connect {
1766
        my $self = shift->SUPER::connect(@_);
1767
        
1768
        $self->include_model('MyModel7');
1769
        
1770
        return $self;
1771
    }
1772
}
cleanup test
Yuki Kimoto authored on 2011-08-06
1773

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1774
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1775
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1776
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1777
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1778
$sql = <<"EOS";
cleanup test
Yuki Kimoto authored on 2011-08-06
1779
left outer join (
1780
  select * from table1 as t1
1781
  where t1.key2 = (
1782
    select max(t2.key2) from table1 as t2
1783
    where t1.key1 = t2.key1
1784
  )
1785
) as latest_table1 on table1.key1 = latest_table1.key1
1786
EOS
cleanup test
Yuki Kimoto authored on 2011-08-06
1787
$join = [$sql];
1788
$rows = $dbi->select(
1789
    table => 'table1',
1790
    column => 'latest_table1.key1 as latest_table1__key1',
1791
    join  => $join
1792
)->all;
1793
is_deeply($rows, [{latest_table1__key1 => 1}]);
1794

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1795
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1796
eval { $dbi->execute('drop table table1') };
1797
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1798
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1799
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1800
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1801
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
1802
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
1803
$result = $dbi->select(
1804
    table => 'table1',
1805
    join => [
1806
        "left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1"
1807
    ]
1808
);
1809
is_deeply($result->all, [{key1 => 1, key2 => 2}]);
1810
$result = $dbi->select(
1811
    table => 'table1',
1812
    column => [{table2 => ['key3']}],
1813
    join => [
1814
        "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1"
1815
    ]
1816
);
1817
is_deeply($result->all, [{'table2.key3' => 4}]);
1818
$result = $dbi->select(
1819
    table => 'table1',
1820
    column => [{table2 => ['key3']}],
1821
    join => [
1822
        "left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'"
1823
    ]
1824
);
1825
is_deeply($result->all, [{'table2.key3' => 4}]);
1826

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1827
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1828
eval { $dbi->execute('drop table table1') };
1829
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1830
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1831
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1832
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1833
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
1834
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
1835
$result = $dbi->select(
1836
    table => 'table1',
1837
    column => [{table2 => ['key3']}],
1838
    join => [
1839
        {
1840
            clause => "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1",
1841
            table => ['table1', 'table2']
1842
        }
1843
    ]
1844
);
1845
is_deeply($result->all, [{'table2.key3' => 4}]);
1846

            
1847
test 'mycolumn';
test cleanup
Yuki Kimoto authored on 2011-08-10
1848
$dbi = MyDBI8->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1849
eval { $dbi->execute('drop table table1') };
1850
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1851
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1852
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1853
$dbi->setup_model;
1854
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1855
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1856
$model = $dbi->model('table1');
1857
$result = $model->select_at(
1858
    column => [
1859
        $model->mycolumn,
1860
        $model->column('table2')
1861
    ]
1862
);
1863
is_deeply($result->one,
1864
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
1865

            
1866
$result = $model->select_at(
1867
    column => [
1868
        $model->mycolumn(['key1']),
1869
        $model->column(table2 => ['key1'])
1870
    ]
1871
);
1872
is_deeply($result->one,
1873
          {key1 => 1, 'table2.key1' => 1});
1874
$result = $model->select_at(
1875
    column => [
1876
        $model->mycolumn(['key1']),
1877
        {table2 => ['key1']}
1878
    ]
1879
);
1880
is_deeply($result->one,
1881
          {key1 => 1, 'table2.key1' => 1});
1882

            
1883
$result = $model->select_at(
1884
    column => [
1885
        $model->mycolumn(['key1']),
1886
        ['table2.key1', as => 'table2.key1']
1887
    ]
1888
);
1889
is_deeply($result->one,
1890
          {key1 => 1, 'table2.key1' => 1});
1891

            
1892
$result = $model->select_at(
1893
    column => [
1894
        $model->mycolumn(['key1']),
1895
        ['table2.key1' => 'table2.key1']
1896
    ]
1897
);
1898
is_deeply($result->one,
1899
          {key1 => 1, 'table2.key1' => 1});
1900

            
1901
test 'dbi method from model';
1902
{
1903
    package MyDBI9;
1904
    
1905
    use base 'DBIx::Custom';
1906
    
1907
    sub connect {
1908
        my $self = shift->SUPER::connect(@_);
1909
        
1910
        $self->include_model('MyModel8')->setup_model;
1911
        
1912
        return $self;
1913
    }
1914
}
test cleanup
Yuki Kimoto authored on 2011-08-10
1915
$dbi = MyDBI9->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1916
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1917
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1918
$model = $dbi->model('table1');
1919
eval{$model->execute('select * from table1')};
1920
ok(!$@);
1921

            
1922
test 'column table option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1923
$dbi = MyDBI9->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1924
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1925
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1926
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1927
$dbi->setup_model;
1928
$dbi->execute('insert into table1 (key1, key2) values (1, 2);');
1929
$dbi->execute('insert into table2 (key1, key3) values (1, 4);');
1930
$model = $dbi->model('table1');
1931
$result = $model->select(
1932
    column => [
1933
        $model->column('table2', {alias => 'table2_alias'})
1934
    ],
1935
    where => {'table2_alias.key3' => 4}
1936
);
1937
is_deeply($result->one, 
1938
          {'table2_alias.key1' => 1, 'table2_alias.key3' => 4});
1939

            
1940
$dbi->separator('__');
1941
$result = $model->select(
1942
    column => [
1943
        $model->column('table2', {alias => 'table2_alias'})
1944
    ],
1945
    where => {'table2_alias.key3' => 4}
1946
);
1947
is_deeply($result->one, 
1948
          {'table2_alias__key1' => 1, 'table2_alias__key3' => 4});
1949

            
1950
$dbi->separator('-');
1951
$result = $model->select(
1952
    column => [
1953
        $model->column('table2', {alias => 'table2_alias'})
1954
    ],
1955
    where => {'table2_alias.key3' => 4}
1956
);
1957
is_deeply($result->one, 
1958
          {'table2_alias-key1' => 1, 'table2_alias-key3' => 4});
1959

            
1960
test 'type option'; # DEPRECATED!
1961
$dbi = DBIx::Custom->connect(
1962
    dbi_option => {
1963
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
1964
    }
1965
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1966
$binary = pack("I3", 1, 2, 3);
1967
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
1968
$dbi->execute('create table table1(key1, key2)');
1969
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
1970
$result = $dbi->select(table => 'table1');
1971
$row   = $result->one;
1972
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
1973
$result = $dbi->execute('select length(key1) as key1_length from table1');
1974
$row = $result->one;
1975
is($row->{key1_length}, length $binary);
1976

            
1977
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [['key1'] => DBI::SQL_BLOB]);
1978
$result = $dbi->select(table => 'table1');
1979
$row   = $result->one;
1980
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
1981
$result = $dbi->execute('select length(key1) as key1_length from table1');
1982
$row = $result->one;
1983
is($row->{key1_length}, length $binary);
1984

            
1985

            
1986
test 'bind_type option';
1987
$dbi = DBIx::Custom->connect(
1988
    dbi_option => {
1989
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
1990
    }
1991
);
1992
$binary = pack("I3", 1, 2, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
1993
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
1994
$dbi->execute('create table table1(key1, key2)');
1995
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, bind_type => [key1 => DBI::SQL_BLOB]);
1996
$result = $dbi->select(table => 'table1');
1997
$row   = $result->one;
1998
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
1999
$result = $dbi->execute('select length(key1) as key1_length from table1');
2000
$row = $result->one;
2001
is($row->{key1_length}, length $binary);
2002

            
2003
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, bind_type => [['key1'] => DBI::SQL_BLOB]);
2004
$result = $dbi->select(table => 'table1');
2005
$row   = $result->one;
2006
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2007
$result = $dbi->execute('select length(key1) as key1_length from table1');
2008
$row = $result->one;
2009
is($row->{key1_length}, length $binary);
2010

            
2011
test 'model type attribute';
2012
$dbi = DBIx::Custom->connect(
2013
    dbi_option => {
2014
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
2015
    }
2016
);
2017
$binary = pack("I3", 1, 2, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2018
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
2019
$dbi->execute('create table table1(key1, key2)');
2020
$model = $dbi->create_model(table => 'table1', bind_type => [key1 => DBI::SQL_BLOB]);
2021
$model->insert(param => {key1 => $binary, key2 => 'あ'});
2022
$result = $dbi->select(table => 'table1');
2023
$row   = $result->one;
2024
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2025
$result = $dbi->execute('select length(key1) as key1_length from table1');
2026
$row = $result->one;
2027
is($row->{key1_length}, length $binary);
2028

            
2029
test 'create_model';
test cleanup
Yuki Kimoto authored on 2011-08-10
2030
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2031
eval { $dbi->execute('drop table table1') };
2032
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2033
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2034
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2035

            
2036
$dbi->create_model(
2037
    table => 'table1',
2038
    join => [
2039
       'left outer join table2 on table1.key1 = table2.key1'
2040
    ],
2041
    primary_key => ['key1']
2042
);
2043
$model2 = $dbi->create_model(
2044
    table => 'table2'
2045
);
2046
$dbi->create_model(
2047
    table => 'table3',
2048
    filter => [
2049
        key1 => {in => sub { uc $_[0] }}
2050
    ]
2051
);
2052
$dbi->setup_model;
2053
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2054
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2055
$model = $dbi->model('table1');
2056
$result = $model->select(
2057
    column => [$model->mycolumn, $model->column('table2')],
2058
    where => {'table1.key1' => 1}
2059
);
2060
is_deeply($result->one,
2061
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
2062
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
2063

            
2064
test 'model method';
2065
test 'create_model';
test cleanup
Yuki Kimoto authored on 2011-08-10
2066
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2067
eval { $dbi->execute('drop table table2') };
test cleanup
Yuki Kimoto authored on 2011-08-10
2068
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2069
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2070
$model = $dbi->create_model(
2071
    table => 'table2'
2072
);
2073
$model->method(foo => sub { shift->select(@_) });
2074
is_deeply($model->foo->one, {key1 => 1, key3 => 3});
2075

            
2076
test 'merge_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
2077
$dbi = DBIx::Custom->new;
2078
$params = [
2079
    {key1 => 1, key2 => 2, key3 => 3},
2080
    {key1 => 1, key2 => 2},
2081
    {key1 => 1}
2082
];
2083
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]);
2084
is_deeply($param, {key1 => [1, 1, 1], key2 => [2, 2], key3 => 3});
2085

            
2086
$params = [
2087
    {key1 => [1, 2], key2 => 1, key3 => [1, 2]},
2088
    {key1 => [3, 4], key2 => [2, 3], key3 => 3}
2089
];
2090
$param = $dbi->merge_param($params->[0], $params->[1]);
2091
is_deeply($param, {key1 => [1, 2, 3, 4], key2 => [1, 2, 3], key3 => [1, 2, 3]});
cleanup test
Yuki Kimoto authored on 2011-08-06
2092

            
2093
test 'select() param option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2094
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2095
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2096
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2097
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2098
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2099
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2100
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2101
$dbi->insert(table => 'table2', param => {key1 => 2, key3 => 5});
2102
$rows = $dbi->select(
2103
    table => 'table1',
2104
    column => 'table1.key1 as table1_key1, key2, key3',
2105
    where   => {'table1.key2' => 3},
2106
    join  => ['inner join (select * from table2 where {= table2.key3})' . 
2107
              ' as table2 on table1.key1 = table2.key1'],
2108
    param => {'table2.key3' => 5}
2109
)->all;
2110
is_deeply($rows, [{table1_key1 => 2, key2 => 3, key3 => 5}]);
2111

            
2112

            
2113
test 'select() wrap option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2114
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2115
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2116
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2117
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2118
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2119
$rows = $dbi->select(
2120
    table => 'table1',
2121
    column => 'key1',
2122
    wrap => ['select * from (', ') as t where key1 = 1']
2123
)->all;
2124
is_deeply($rows, [{key1 => 1}]);
2125

            
2126
eval {
2127
$dbi->select(
2128
    table => 'table1',
2129
    column => 'key1',
2130
    wrap => 'select * from ('
2131
)
2132
};
2133
like($@, qr/array/);
2134

            
2135
test 'select() string where';
test cleanup
Yuki Kimoto authored on 2011-08-10
2136
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2137
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2138
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2139
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2140
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2141
$rows = $dbi->select(
2142
    table => 'table1',
2143
    where => 'key1 = :key1 and key2 = :key2',
2144
    where_param => {key1 => 1, key2 => 2}
2145
)->all;
2146
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2147

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2148
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2149
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2150
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2151
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2152
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2153
$rows = $dbi->select(
2154
    table => 'table1',
2155
    where => [
2156
        'key1 = :key1 and key2 = :key2',
2157
        {key1 => 1, key2 => 2}
2158
    ]
2159
)->all;
2160
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2161

            
2162
test 'delete() string where';
test cleanup
Yuki Kimoto authored on 2011-08-10
2163
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2164
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2165
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2166
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2167
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2168
$dbi->delete(
2169
    table => 'table1',
2170
    where => 'key1 = :key1 and key2 = :key2',
2171
    where_param => {key1 => 1, key2 => 2}
2172
);
2173
$rows = $dbi->select(table => 'table1')->all;
2174
is_deeply($rows, [{key1 => 2, key2 => 3}]);
2175

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2176
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2177
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2178
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2179
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2180
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2181
$dbi->delete(
2182
    table => 'table1',
2183
    where => [
2184
        'key1 = :key1 and key2 = :key2',
2185
         {key1 => 1, key2 => 2}
2186
    ]
2187
);
2188
$rows = $dbi->select(table => 'table1')->all;
2189
is_deeply($rows, [{key1 => 2, key2 => 3}]);
2190

            
2191

            
2192
test 'update() string where';
test cleanup
Yuki Kimoto authored on 2011-08-10
2193
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2194
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2195
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2196
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2197
$dbi->update(
2198
    table => 'table1',
2199
    param => {key1 => 5},
2200
    where => 'key1 = :key1 and key2 = :key2',
2201
    where_param => {key1 => 1, key2 => 2}
2202
);
2203
$rows = $dbi->select(table => 'table1')->all;
2204
is_deeply($rows, [{key1 => 5, key2 => 2}]);
2205

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2206
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2207
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2208
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2209
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2210
$dbi->update(
2211
    table => 'table1',
2212
    param => {key1 => 5},
2213
    where => [
2214
        'key1 = :key1 and key2 = :key2',
2215
        {key1 => 1, key2 => 2}
2216
    ]
2217
);
2218
$rows = $dbi->select(table => 'table1')->all;
2219
is_deeply($rows, [{key1 => 5, key2 => 2}]);
2220

            
2221
test 'insert id and primary_key option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2222
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2223
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2224
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2225
$dbi->insert(
2226
    primary_key => ['key1', 'key2'], 
2227
    table => 'table1',
2228
    id => [1, 2],
2229
    param => {key3 => 3}
2230
);
2231
is($dbi->select(table => 'table1')->one->{key1}, 1);
2232
is($dbi->select(table => 'table1')->one->{key2}, 2);
2233
is($dbi->select(table => 'table1')->one->{key3}, 3);
2234

            
2235
$dbi->delete_all(table => 'table1');
2236
$dbi->insert(
2237
    primary_key => 'key1', 
2238
    table => 'table1',
2239
    id => 0,
2240
    param => {key2 => 2, key3 => 3}
2241
);
2242

            
2243
is($dbi->select(table => 'table1')->one->{key1}, 0);
2244
is($dbi->select(table => 'table1')->one->{key2}, 2);
2245
is($dbi->select(table => 'table1')->one->{key3}, 3);
2246

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2247
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2248
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2249
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2250
$dbi->insert(
2251
    {key3 => 3},
2252
    primary_key => ['key1', 'key2'], 
2253
    table => 'table1',
2254
    id => [1, 2],
2255
);
2256
is($dbi->select(table => 'table1')->one->{key1}, 1);
2257
is($dbi->select(table => 'table1')->one->{key2}, 2);
2258
is($dbi->select(table => 'table1')->one->{key3}, 3);
2259

            
2260

            
2261
test 'model insert id and primary_key option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2262
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2263
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2264
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2265
$dbi->model('table1')->insert(
2266
    id => [1, 2],
2267
    param => {key3 => 3}
2268
);
2269
$result = $dbi->model('table1')->select;
2270
$row = $result->one;
2271
is($row->{key1}, 1);
2272
is($row->{key2}, 2);
2273
is($row->{key3}, 3);
2274

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2275
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2276
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2277
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2278
$dbi->model('table1')->insert(
2279
    {key3 => 3},
2280
    id => [1, 2]
2281
);
2282
$result = $dbi->model('table1')->select;
2283
$row = $result->one;
2284
is($row->{key1}, 1);
2285
is($row->{key2}, 2);
2286
is($row->{key3}, 3);
2287

            
2288
test 'update and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2289
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2290
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2291
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2292
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2293
$dbi->update(
2294
    table => 'table1',
2295
    primary_key => ['key1', 'key2'],
2296
    id => [1, 2],
2297
    param => {key3 => 4}
2298
);
2299
is($dbi->select(table => 'table1')->one->{key1}, 1);
2300
is($dbi->select(table => 'table1')->one->{key2}, 2);
2301
is($dbi->select(table => 'table1')->one->{key3}, 4);
2302

            
2303
$dbi->delete_all(table => 'table1');
2304
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2305
$dbi->update(
2306
    table => 'table1',
2307
    primary_key => 'key1',
2308
    id => 0,
2309
    param => {key3 => 4}
2310
);
2311
is($dbi->select(table => 'table1')->one->{key1}, 0);
2312
is($dbi->select(table => 'table1')->one->{key2}, 2);
2313
is($dbi->select(table => 'table1')->one->{key3}, 4);
2314

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2315
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2316
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2317
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2318
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2319
$dbi->update(
2320
    {key3 => 4},
2321
    table => 'table1',
2322
    primary_key => ['key1', 'key2'],
2323
    id => [1, 2]
2324
);
2325
is($dbi->select(table => 'table1')->one->{key1}, 1);
2326
is($dbi->select(table => 'table1')->one->{key2}, 2);
2327
is($dbi->select(table => 'table1')->one->{key3}, 4);
2328

            
2329

            
2330
test 'model update and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2331
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2332
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2333
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2334
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2335
$dbi->model('table1')->update(
2336
    id => [1, 2],
2337
    param => {key3 => 4}
2338
);
2339
$result = $dbi->model('table1')->select;
2340
$row = $result->one;
2341
is($row->{key1}, 1);
2342
is($row->{key2}, 2);
2343
is($row->{key3}, 4);
2344

            
2345

            
2346
test 'delete and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2347
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2348
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2349
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2350
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2351
$dbi->delete(
2352
    table => 'table1',
2353
    primary_key => ['key1', 'key2'],
2354
    id => [1, 2],
2355
);
2356
is_deeply($dbi->select(table => 'table1')->all, []);
2357

            
2358
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2359
$dbi->delete(
2360
    table => 'table1',
2361
    primary_key => 'key1',
2362
    id => 0,
2363
);
2364
is_deeply($dbi->select(table => 'table1')->all, []);
2365

            
2366

            
2367
test 'model delete and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2368
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2369
eval { $dbi->execute('drop table table1') };
2370
eval { $dbi->execute('drop table table2') };
2371
eval { $dbi->execute('drop table table3') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2372
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2373
$dbi->execute("create table table2 (key1, key2, key3)");
2374
$dbi->execute("create table table3 (key1, key2, key3)");
2375
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2376
$dbi->model('table1')->delete(id => [1, 2]);
2377
is_deeply($dbi->select(table => 'table1')->all, []);
2378
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
2379
$dbi->model('table1_1')->delete(id => [1, 2]);
2380
is_deeply($dbi->select(table => 'table1')->all, []);
2381
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
2382
$dbi->model('table1_3')->delete(id => [1, 2]);
2383
is_deeply($dbi->select(table => 'table1')->all, []);
2384

            
2385

            
2386
test 'select and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2387
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2388
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2389
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2390
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2391
$result = $dbi->select(
2392
    table => 'table1',
2393
    primary_key => ['key1', 'key2'],
2394
    id => [1, 2]
2395
);
2396
$row = $result->one;
2397
is($row->{key1}, 1);
2398
is($row->{key2}, 2);
2399
is($row->{key3}, 3);
2400

            
2401
$dbi->delete_all(table => 'table1');
2402
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2403
$result = $dbi->select(
2404
    table => 'table1',
2405
    primary_key => 'key1',
2406
    id => 0,
2407
);
2408
$row = $result->one;
2409
is($row->{key1}, 0);
2410
is($row->{key2}, 2);
2411
is($row->{key3}, 3);
2412

            
2413
$dbi->delete_all(table => 'table1');
2414
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2415
$result = $dbi->select(
2416
    table => 'table1',
2417
    primary_key => ['key1', 'key2'],
2418
    id => [1, 2]
2419
);
2420
$row = $result->one;
2421
is($row->{key1}, 1);
2422
is($row->{key2}, 2);
2423
is($row->{key3}, 3);
2424

            
2425

            
2426
test 'model select_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
2427
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2428
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2429
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2430
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2431
$result = $dbi->model('table1')->select(id => [1, 2]);
2432
$row = $result->one;
2433
is($row->{key1}, 1);
2434
is($row->{key2}, 2);
2435
is($row->{key3}, 3);
2436

            
2437
test 'column separator is default .';
test cleanup
Yuki Kimoto authored on 2011-08-10
2438
$dbi = MyDBI7->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2439
eval { $dbi->execute('drop table table1') };
2440
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2441
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2442
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2443
$dbi->setup_model;
2444
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2445
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2446
$model = $dbi->model('table1');
2447
$result = $model->select(
2448
    column => [$model->column('table2')],
2449
    where => {'table1.key1' => 1}
2450
);
2451
is_deeply($result->one,
2452
          {'table2.key1' => 1, 'table2.key3' => 3});
2453

            
2454
$result = $model->select(
2455
    column => [$model->column('table2' => [qw/key1 key3/])],
2456
    where => {'table1.key1' => 1}
2457
);
2458
is_deeply($result->one,
2459
          {'table2.key1' => 1, 'table2.key3' => 3});
2460

            
2461

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2462

            
2463
test 'separator';
test cleanup
Yuki Kimoto authored on 2011-08-10
2464
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2465
eval { $dbi->execute('drop table table1') };
2466
eval { $dbi->execute('drop table table2') };
2467
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2468
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-10
2469

            
2470
$dbi->create_model(
2471
    table => 'table1',
2472
    join => [
2473
       'left outer join table2 on table1.key1 = table2.key1'
2474
    ],
2475
    primary_key => ['key1'],
cleanup test
Yuki Kimoto authored on 2011-08-06
2476
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2477
$model2 = $dbi->create_model(
2478
    table => 'table2',
2479
);
2480
$dbi->setup_model;
2481
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2482
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2483
$model = $dbi->model('table1');
2484
$result = $model->select(
2485
    column => [
2486
        $model->mycolumn,
2487
        {table2 => [qw/key1 key3/]}
2488
    ],
2489
    where => {'table1.key1' => 1}
2490
);
2491
is_deeply($result->one,
2492
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
2493
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
2494

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2495
$dbi->separator('__');
2496
$model = $dbi->model('table1');
2497
$result = $model->select(
2498
    column => [
2499
        $model->mycolumn,
2500
        {table2 => [qw/key1 key3/]}
2501
    ],
2502
    where => {'table1.key1' => 1}
2503
);
2504
is_deeply($result->one,
2505
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
2506
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
2507

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2508
$dbi->separator('-');
2509
$model = $dbi->model('table1');
2510
$result = $model->select(
2511
    column => [
2512
        $model->mycolumn,
2513
        {table2 => [qw/key1 key3/]}
2514
    ],
2515
    where => {'table1.key1' => 1}
2516
);
2517
is_deeply($result->one,
2518
          {key1 => 1, key2 => 2, 'table2-key1' => 1, 'table2-key3' => 3});
2519
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
2520

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2521

            
2522
test 'filter_off';
test cleanup
Yuki Kimoto authored on 2011-08-10
2523
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2524
eval { $dbi->execute('drop table table1') };
2525
eval { $dbi->execute('drop table table2') };
2526
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2527
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-10
2528

            
2529
$dbi->create_model(
2530
    table => 'table1',
2531
    join => [
2532
       'left outer join table2 on table1.key1 = table2.key1'
2533
    ],
2534
    primary_key => ['key1'],
cleanup test
Yuki Kimoto authored on 2011-08-06
2535
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2536
$dbi->setup_model;
2537
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2538
$model = $dbi->model('table1');
2539
$result = $model->select(column => 'key1');
2540
$result->filter(key1 => sub { $_[0] * 2 });
2541
is_deeply($result->one, {key1 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-06
2542

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2543
test 'available_datetype';
test cleanup
Yuki Kimoto authored on 2011-08-10
2544
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2545
ok($dbi->can('available_datatype'));
2546

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2547

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2548
test 'select prefix option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2549
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2550
eval { $dbi->execute('drop table table1') };
2551
$dbi->execute($create_table1);
2552
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2553
$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all;
2554
is_deeply($rows, [{key1 => 1, key2 => 2}], "table");
2555

            
2556

            
2557
test 'separator';
2558
$dbi = DBIx::Custom->connect;
2559
is($dbi->separator, '.');
2560
$dbi->separator('-');
2561
is($dbi->separator, '-');
2562
$dbi->separator('__');
2563
is($dbi->separator, '__');
2564
eval { $dbi->separator('?') };
2565
like($@, qr/Separator/);
2566

            
2567

            
2568
test 'map_param';
2569
$dbi = DBIx::Custom->connect;
2570
$param = $dbi->map_param(
2571
    {id => 1, author => 'Ken', price => 1900},
2572
    id => 'book.id',
2573
    author => ['book.author', sub { '%' . $_[0] . '%' }],
2574
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
cleanup test
Yuki Kimoto authored on 2011-08-06
2575
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2576
is_deeply($param, {'book.id' => 1, 'book.author' => '%Ken%',
2577
  'book.price' => 1900});
2578

            
2579
$param = $dbi->map_param(
2580
    {id => 0, author => 0, price => 0},
2581
    id => 'book.id',
2582
    author => ['book.author', sub { '%' . $_[0] . '%' }],
2583
    price => ['book.price', sub { '%' . $_[0] . '%' },
2584
      {if => sub { $_[0] eq 0 }}]
cleanup test
Yuki Kimoto authored on 2011-08-06
2585
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2586
is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
cleanup test
Yuki Kimoto authored on 2011-08-06
2587

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2588
$param = $dbi->map_param(
2589
    {id => '', author => '', price => ''},
2590
    id => 'book.id',
2591
    author => ['book.author', sub { '%' . $_[0] . '%' }],
2592
    price => ['book.price', sub { '%' . $_[0] . '%' },
2593
      {if => sub { $_[0] eq 1 }}]
cleanup test
Yuki Kimoto authored on 2011-08-06
2594
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2595
is_deeply($param, {});
2596

            
2597
$param = $dbi->map_param(
2598
    {id => undef, author => undef, price => undef},
2599
    id => 'book.id',
2600
    price => ['book.price', {if => 'exists'}]
cleanup test
Yuki Kimoto authored on 2011-08-06
2601
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2602
is_deeply($param, {'book.price' => undef});
2603

            
2604
$param = $dbi->map_param(
2605
    {price => 'a'},
2606
    id => ['book.id', {if => 'exists'}],
2607
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
2608
);
2609
is_deeply($param, {'book.price' => '%a'});
cleanup test
Yuki Kimoto authored on 2011-08-06
2610

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2611

            
2612
test 'table_alias';
test cleanup
Yuki Kimoto authored on 2011-08-10
2613
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2614
eval { $dbi->execute('drop table table1') };
2615
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
cleanup test
Yuki Kimoto authored on 2011-08-06
2616
$dbi->type_rule(
2617
    into1 => {
cleanup test
Yuki Kimoto authored on 2011-08-10
2618
        date => sub { uc $_[0] }
cleanup test
Yuki Kimoto authored on 2011-08-06
2619
    }
2620
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2621
$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'},
2622
  table_alias => {table2 => 'table1'});
cleanup test
Yuki Kimoto authored on 2011-08-06
2623
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2624
is($result->one->{key1}, 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
2625

            
2626

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2627
test 'order';
test cleanup
Yuki Kimoto authored on 2011-08-10
2628
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2629
eval { $dbi->execute('drop table table1') };
2630
$dbi->execute("create table table1 (key1, key2)");
2631
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
2632
$dbi->insert({key1 => 1, key2 => 3}, table => 'table1');
2633
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
2634
$dbi->insert({key1 => 2, key2 => 4}, table => 'table1');
2635
my $order = $dbi->order;
2636
$order->prepend('key1', 'key2 desc');
2637
$result = $dbi->select(table => 'table1', append => "$order");
2638
is_deeply($result->all, [{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1},
2639
  {key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}]);
2640
$order->prepend('key1 desc');
2641
$result = $dbi->select(table => 'table1', append => "$order");
2642
is_deeply($result->all, [{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2},
2643
  {key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
2644

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2645
$order = $dbi->order;
2646
$order->prepend(['table1-key1'], [qw/table1-key2 desc/]);
2647
$result = $dbi->select(table => 'table1',
2648
  column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']],
2649
  append => "$order");
2650
is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3},
2651
  {'table1-key1' => 1, 'table1-key2' => 1},
2652
  {'table1-key1' => 2, 'table1-key2' => 4},
2653
  {'table1-key1' => 2, 'table1-key2' => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
2654

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2655
test 'tag_parse';
test cleanup
Yuki Kimoto authored on 2011-08-10
2656
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2657
$dbi->tag_parse(0);
2658
eval { $dbi->execute('drop table table1') };
2659
$dbi->execute("create table table1 (key1, key2)");
2660
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
2661
eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})};
2662
ok($@);
cleanup test
Yuki Kimoto authored on 2011-08-06
2663

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2664
test 'last_sql';
test cleanup
Yuki Kimoto authored on 2011-08-10
2665
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2666
eval { $dbi->execute('drop table table1') };
2667
$dbi->execute("create table table1 (key1, key2)");
2668
$dbi->execute('select * from table1');
2669
is($dbi->last_sql, 'select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
2670

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2671
eval{$dbi->execute("aaa")};
2672
is($dbi->last_sql, 'aaa;');
cleanup test
Yuki Kimoto authored on 2011-08-06
2673

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2674
test 'DBIx::Custom header';
test cleanup
Yuki Kimoto authored on 2011-08-10
2675
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2676
eval { $dbi->execute('drop table table1') };
2677
$dbi->execute("create table table1 (key1, key2)");
2678
$result = $dbi->execute('select key1 as h1, key2 as h2 from table1');
2679
is_deeply($result->header, [qw/h1 h2/]);
cleanup test
Yuki Kimoto authored on 2011-08-06
2680

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2681
test 'Named placeholder :name(operater) syntax';
2682
$dbi->execute('drop table table1');
2683
$dbi->execute($create_table1_2);
2684
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
2685
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
cleanup test
Yuki Kimoto authored on 2011-08-06
2686

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2687
$source = "select * from table1 where :key1{=} and :key2{=}";
2688
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
2689
$rows = $result->all;
2690
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
2691

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2692
$source = "select * from table1 where :key1{ = } and :key2{=}";
2693
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
2694
$rows = $result->all;
2695
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
2696

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2697
$source = "select * from table1 where :key1{<} and :key2{=}";
2698
$result = $dbi->execute($source, param => {key1 => 5, key2 => 2});
2699
$rows = $result->all;
2700
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
2701

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2702
$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}";
2703
$result = $dbi->execute(
2704
    $source,
2705
    param => {'table1.key1' => 1, 'table1.key2' => 1},
2706
    filter => {'table1.key2' => sub { $_[0] * 2 }}
cleanup test
Yuki Kimoto authored on 2011-08-06
2707
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2708
$rows = $result->all;
2709
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
2710

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2711
test 'high perfomance way';
2712
$dbi->execute('drop table table1');
2713
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
2714
$rows = [
2715
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
2716
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
2717
];
2718
{
2719
    my $query;
2720
    foreach my $row (@$rows) {
2721
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
2722
      $dbi->execute($query, $row, filter => {ab => sub { $_[0] * 2 }});
cleanup test
Yuki Kimoto authored on 2011-08-06
2723
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
2724
    is_deeply($dbi->select(table => 'table1')->all,
2725
      [
2726
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
2727
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
2728
      ]
2729
    );
2730
}
2731

            
2732
$dbi->execute('drop table table1');
2733
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
2734
$rows = [
2735
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
2736
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
2737
];
2738
{
2739
    my $query;
2740
    my $sth;
2741
    foreach my $row (@$rows) {
2742
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
2743
      $sth ||= $query->sth;
2744
      $sth->execute(map { $row->{$_} } sort keys %$row);
cleanup test
Yuki Kimoto authored on 2011-08-06
2745
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
2746
    is_deeply($dbi->select(table => 'table1')->all,
2747
      [
2748
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
2749
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
2750
      ]
2751
    );
2752
}
cleanup test
Yuki Kimoto authored on 2011-08-06
2753

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2754
test 'result';
test cleanup
Yuki Kimoto authored on 2011-08-10
2755
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2756
eval { $dbi->execute('drop table table1') };
2757
$dbi->execute($create_table1);
2758
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
2759
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
2760

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2761
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2762
@rows = ();
2763
while (my $row = $result->fetch) {
2764
    push @rows, [@$row];
2765
}
2766
is_deeply(\@rows, [[1, 2], [3, 4]]);
cleanup test
Yuki Kimoto authored on 2011-08-06
2767

            
2768
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2769
@rows = ();
2770
while (my $row = $result->fetch_hash) {
2771
    push @rows, {%$row};
2772
}
2773
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
2774

            
2775
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2776
$row = $result->fetch_first;
2777
is_deeply($row, [1, 2], "row");
2778
$row = $result->fetch;
2779
ok(!$row, "finished");
2780

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2781
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2782
$row = $result->fetch_hash_first;
2783
is_deeply($row, {key1 => 1, key2 => 2}, "row");
2784
$row = $result->fetch_hash;
2785
ok(!$row, "finished");
2786

            
2787
$dbi->execute('create table table2 (key1, key2);');
2788
$result = $dbi->select(table => 'table2');
2789
$row = $result->fetch_hash_first;
2790
ok(!$row, "no row fetch");
cleanup test
Yuki Kimoto authored on 2011-08-06
2791

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2792
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2793
eval { $dbi->execute('drop table table1') };
2794
$dbi->execute($create_table1);
2795
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
2796
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
2797
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1');
2798
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1');
2799
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
2800
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2801
$rows = $result->fetch_multi(2);
2802
is_deeply($rows, [[1, 2],
2803
                  [3, 4]], "fetch_multi first");
2804
$rows = $result->fetch_multi(2);
2805
is_deeply($rows, [[5, 6],
2806
                  [7, 8]], "fetch_multi secound");
2807
$rows = $result->fetch_multi(2);
2808
is_deeply($rows, [[9, 10]], "fetch_multi third");
2809
$rows = $result->fetch_multi(2);
2810
ok(!$rows);
2811

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2812
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2813
eval {$result->fetch_multi};
2814
like($@, qr/Row count must be specified/, "Not specified row count");
cleanup test
Yuki Kimoto authored on 2011-08-06
2815

            
2816
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2817
$rows = $result->fetch_hash_multi(2);
2818
is_deeply($rows, [{key1 => 1, key2 => 2},
2819
                  {key1 => 3, key2 => 4}], "fetch_multi first");
2820
$rows = $result->fetch_hash_multi(2);
2821
is_deeply($rows, [{key1 => 5, key2 => 6},
2822
                  {key1 => 7, key2 => 8}], "fetch_multi secound");
2823
$rows = $result->fetch_hash_multi(2);
2824
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third");
2825
$rows = $result->fetch_hash_multi(2);
2826
ok(!$rows);
2827

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2828
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2829
eval {$result->fetch_hash_multi};
2830
like($@, qr/Row count must be specified/, "Not specified row count");
cleanup test
Yuki Kimoto authored on 2011-08-06
2831

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2832
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2833
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2834
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2835
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
2836
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
2837

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2838
test 'fetch_all';
2839
$result = $dbi->select(table => 'table1');
2840
$rows = $result->fetch_all;
2841
is_deeply($rows, [[1, 2], [3, 4]]);
cleanup test
Yuki Kimoto authored on 2011-08-06
2842

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2843
$result = $dbi->select(table => 'table1');
2844
$rows = $result->fetch_hash_all;
2845
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
2846

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2847
$result = $dbi->select(table => 'table1');
2848
$result->dbi->filters({three_times => sub { $_[0] * 3}});
2849
$result->filter({key1 => 'three_times'});
cleanup test
Yuki Kimoto authored on 2011-08-06
2850

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2851
$rows = $result->fetch_all;
2852
is_deeply($rows, [[3, 2], [9, 4]], "array");
cleanup test
Yuki Kimoto authored on 2011-08-06
2853

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2854
$result = $dbi->select(table => 'table1');
2855
$result->dbi->filters({three_times => sub { $_[0] * 3}});
2856
$result->filter({key1 => 'three_times'});
2857
$rows = $result->fetch_hash_all;
2858
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash");
2859

            
2860
test "query_builder";
2861
$datas = [
2862
    # Basic tests
2863
    {   name            => 'placeholder basic',
2864
        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
2865
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;",
2866
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
2867
    },
2868
    {
2869
        name            => 'placeholder in',
2870
        source            => "{in k1 3};",
2871
        sql_expected    => "k1 in (?, ?, ?);",
2872
        columns_expected   => [qw/k1 k1 k1/]
2873
    },
2874
    
2875
    # Table name
2876
    {
2877
        name            => 'placeholder with table name',
2878
        source            => "{= a.k1} {= a.k2}",
2879
        sql_expected    => "a.k1 = ? a.k2 = ?;",
2880
        columns_expected  => [qw/a.k1 a.k2/]
2881
    },
2882
    {   
2883
        name            => 'placeholder in with table name',
2884
        source            => "{in a.k1 2} {in b.k2 2}",
2885
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?);",
2886
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
2887
    },
2888
    {
2889
        name            => 'not contain tag',
2890
        source            => "aaa",
2891
        sql_expected    => "aaa;",
2892
        columns_expected  => [],
2893
    }
2894
];
2895

            
2896
for (my $i = 0; $i < @$datas; $i++) {
2897
    my $data = $datas->[$i];
2898
    my $builder = DBIx::Custom->new->query_builder;
2899
    my $query = $builder->build_query($data->{source});
2900
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
2901
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
2902
}
2903

            
2904
$builder = DBIx::Custom->new->query_builder;
2905
$ret_val = $builder->register_tag(
2906
    p => sub {
2907
        my @args = @_;
2908
        
2909
        my $expand    = "? $args[0] $args[1]";
2910
        my $columns = [2];
2911
        return [$expand, $columns];
2912
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
2913
);
2914

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2915
$query = $builder->build_query("{p a b}");
2916
is($query->{sql}, "? a b;", "register_tag sql");
2917
is_deeply($query->{columns}, [2], "register_tag columns");
2918
isa_ok($ret_val, 'DBIx::Custom::QueryBuilder');
cleanup test
Yuki Kimoto authored on 2011-08-06
2919

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2920
$builder = DBIx::Custom->new->query_builder;
cleanup test
Yuki Kimoto authored on 2011-08-06
2921

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2922
eval{$builder->build_query('{? }')};
2923
like($@, qr/\QColumn name must be specified in tag "{? }"/, "? not arguments");
cleanup test
Yuki Kimoto authored on 2011-08-06
2924

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2925
eval{$builder->build_query("{a }")};
2926
like($@, qr/\QTag "a" is not registered/, "tag not exist");
cleanup test
Yuki Kimoto authored on 2011-08-06
2927

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2928
$builder->register_tag({
2929
    q => 'string'
2930
});
cleanup test
Yuki Kimoto authored on 2011-08-06
2931

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2932
eval{$builder->build_query("{q}", {})};
2933
like($@, qr/Tag "q" must be sub reference/, "tag not code ref");
cleanup test
Yuki Kimoto authored on 2011-08-06
2934

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2935
$builder->register_tag({
2936
   r => sub {} 
2937
});
cleanup test
Yuki Kimoto authored on 2011-08-06
2938

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2939
eval{$builder->build_query("{r}")};
2940
like($@, qr/\QTag "r" must return [STRING, ARRAY_REFERENCE]/, "tag return noting");
2941

            
2942
$builder->register_tag({
2943
   s => sub { return ["a", ""]} 
2944
});
2945

            
2946
eval{$builder->build_query("{s}")};
2947
like($@, qr/\QTag "s" must return [STRING, ARRAY_REFERENCE]/, "tag return not array columns");
2948

            
2949
$builder->register_tag(
2950
    t => sub {return ["a", []]}
cleanup test
Yuki Kimoto authored on 2011-08-06
2951
);
2952

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2953

            
2954
test 'General error case';
2955
$builder = DBIx::Custom->new->query_builder;
2956
$builder->register_tag(
2957
    a => sub {
2958
        return ["? ? ?", ['']];
2959
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
2960
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2961
eval{$builder->build_query("{a}")};
2962
like($@, qr/\QPlaceholder count/, "placeholder count is invalid");
cleanup test
Yuki Kimoto authored on 2011-08-06
2963

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2964

            
2965
test 'Default tag Error case';
2966
eval{$builder->build_query("{= }")};
2967
like($@, qr/Column name must be specified in tag "{= }"/, "basic '=' : key not exist");
2968

            
2969
eval{$builder->build_query("{in }")};
2970
like($@, qr/Column name and count of values must be specified in tag "{in }"/, "in : key not exist");
2971

            
2972
eval{$builder->build_query("{in a}")};
2973
like($@, qr/\QColumn name and count of values must be specified in tag "{in }"/,
2974
     "in : key not exist");
2975

            
2976
eval{$builder->build_query("{in a r}")};
2977
like($@, qr/\QColumn name and count of values must be specified in tag "{in }"/,
2978
     "in : key not exist");
2979

            
2980
test 'variouse source';
2981
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;";
2982
$query = $builder->build_query($source);
2983
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1");
2984

            
2985
$source = "abc;";
2986
$query = $builder->build_query($source);
2987
is($query->sql, 'abc;', "basic : 2");
2988

            
2989
$source = "{= a}";
2990
$query = $builder->build_query($source);
2991
is($query->sql, 'a = ?;', "only tag");
2992

            
2993
$source = "000;";
2994
$query = $builder->build_query($source);
2995
is($query->sql, '000;', "contain 0 value");
2996

            
2997
$source = "a {= b} }";
2998
eval{$builder->build_query($source)};
2999
like($@, qr/unexpected "}"/, "error : 1");
3000

            
3001
$source = "a {= {}";
3002
eval{$builder->build_query($source)};
3003
like($@, qr/unexpected "{"/, "error : 2");
3004

            
3005
### SQLite test
3006
test 'type option'; # DEPRECATED!
3007
$dbi = DBIx::Custom->connect(
3008
    data_source => 'dbi:SQLite:dbname=:memory:',
3009
    dbi_option => {
3010
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
3011
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
3012
);
cleanup test
Yuki Kimoto authored on 2011-08-10
3013
$binary = pack("I3", 1, 2, 3);
3014
eval { $dbi->execute('drop table table1') };
3015
$dbi->execute('create table table1(key1, key2)');
3016
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
3017
$result = $dbi->select(table => 'table1');
3018
$row   = $result->one;
3019
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
3020
$result = $dbi->execute('select length(key1) as key1_length from table1');
3021
$row = $result->one;
3022
is($row->{key1_length}, length $binary);
cleanup test
Yuki Kimoto authored on 2011-08-06
3023

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3024
test 'type_rule from';
3025
$dbi = DBIx::Custom->connect;
3026
$dbi->type_rule(
3027
    from1 => {
3028
        date => sub { uc $_[0] }
3029
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
3030
);
cleanup test
Yuki Kimoto authored on 2011-08-10
3031
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3032
$dbi->insert({key1 => 'a'}, table => 'table1');
3033
$result = $dbi->select(table => 'table1');
3034
is($result->fetch_first->[0], 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
3035

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3036
$result = $dbi->select(table => 'table1');
3037
is($result->one->{key1}, 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
3038

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3039

            
3040
test 'type_rule into';
test cleanup
Yuki Kimoto authored on 2011-08-10
3041
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
3042
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3043
$dbi->type_rule(
3044
    into1 => {
3045
        date => sub { uc $_[0] }
3046
    }
3047
);
cleanup test
Yuki Kimoto authored on 2011-08-10
3048
$dbi->insert({key1 => 'a'}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
3049
$result = $dbi->select(table => 'table1');
3050
is($result->one->{key1}, 'A');
3051

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3052
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3053
$dbi->execute("create table table1 (key1 date, key2 datetime)");
3054
$dbi->type_rule(
3055
    into1 => [
3056
         [qw/date datetime/] => sub { uc $_[0] }
3057
    ]
3058
);
3059
$dbi->insert({key1 => 'a', key2 => 'b'}, table => 'table1');
3060
$result = $dbi->select(table => 'table1');
3061
$row = $result->one;
3062
is($row->{key1}, 'A');
3063
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
3064

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3065
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3066
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3067
$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1');
3068
$dbi->type_rule(
3069
    into1 => [
3070
        [qw/date datetime/] => sub { uc $_[0] }
3071
    ]
3072
);
3073
$result = $dbi->execute(
3074
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
3075
    param => {key1 => 'a', 'table1.key2' => 'b'}
3076
);
3077
$row = $result->one;
3078
is($row->{key1}, 'a');
3079
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
3080

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3081
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3082
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3083
$dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1');
3084
$dbi->type_rule(
3085
    into1 => [
3086
        [qw/date datetime/] => sub { uc $_[0] }
3087
    ]
3088
);
3089
$result = $dbi->execute(
3090
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
3091
    param => {key1 => 'a', 'table1.key2' => 'b'},
3092
    table => 'table1'
3093
);
3094
$row = $result->one;
3095
is($row->{key1}, 'A');
3096
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
3097

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3098
$dbi = DBIx::Custom->connect;
3099
$dbi->execute("create table table1 (key1 date, key2 datetime)");
3100
$dbi->register_filter(twice => sub { $_[0] * 2 });
3101
$dbi->type_rule(
3102
    from1 => {
3103
        date => 'twice',
3104
    },
3105
    into1 => {
3106
        date => 'twice',
3107
    }
3108
);
3109
$dbi->insert({key1 => 2}, table => 'table1');
3110
$result = $dbi->select(table => 'table1');
3111
is($result->fetch->[0], 8);
cleanup test
Yuki Kimoto authored on 2011-08-06
3112

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3113
test 'type_rule and filter order';
test cleanup
Yuki Kimoto authored on 2011-08-10
3114
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3115
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3116
$dbi->type_rule(
3117
    into1 => {
3118
        date => sub { $_[0] . 'b' }
3119
    },
3120
    into2 => {
3121
        date => sub { $_[0] . 'c' }
3122
    },
3123
    from1 => {
3124
        date => sub { $_[0] . 'd' }
3125
    },
3126
    from2 => {
3127
        date => sub { $_[0] . 'e' }
3128
    }
3129
);
3130
$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }});
3131
$result = $dbi->select(table => 'table1');
3132
$result->filter(key1 => sub { $_[0] . 'f' });
3133
is($result->fetch_first->[0], '1abcdef');
cleanup test
Yuki Kimoto authored on 2011-08-06
3134

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3135
$dbi = DBIx::Custom->connect;
3136
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3137
$dbi->type_rule(
3138
    from1 => {
3139
        date => sub { $_[0] . 'p' }
3140
    },
3141
    from2 => {
3142
        date => sub { $_[0] . 'q' }
3143
    },
3144
);
3145
$dbi->insert({key1 => '1'}, table => 'table1');
3146
$result = $dbi->select(table => 'table1');
3147
$result->type_rule(
3148
    from1 => {
3149
        date => sub { $_[0] . 'd' }
3150
    },
3151
    from2 => {
3152
        date => sub { $_[0] . 'e' }
3153
    }
3154
);
3155
$result->filter(key1 => sub { $_[0] . 'f' });
3156
is($result->fetch_first->[0], '1def');
cleanup test
Yuki Kimoto authored on 2011-08-06
3157

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3158
test 'type_rule_off';
3159
$dbi = DBIx::Custom->connect;
3160
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3161
$dbi->type_rule(
3162
    from1 => {
3163
        date => sub { $_[0] * 2 },
3164
    },
3165
    into1 => {
3166
        date => sub { $_[0] * 2 },
3167
    }
3168
);
3169
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
3170
$result = $dbi->select(table => 'table1', type_rule_off => 1);
3171
is($result->type_rule_off->fetch->[0], 2);
cleanup test
Yuki Kimoto authored on 2011-08-06
3172

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3173
$dbi = DBIx::Custom->connect;
3174
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3175
$dbi->type_rule(
3176
    from1 => {
3177
        date => sub { $_[0] * 2 },
3178
    },
3179
    into1 => {
3180
        date => sub { $_[0] * 3 },
3181
    }
3182
);
3183
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
3184
$result = $dbi->select(table => 'table1', type_rule_off => 1);
3185
is($result->one->{key1}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-06
3186

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3187
$dbi = DBIx::Custom->connect;
3188
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3189
$dbi->type_rule(
3190
    from1 => {
3191
        date => sub { $_[0] * 2 },
3192
    },
3193
    into1 => {
3194
        date => sub { $_[0] * 3 },
3195
    }
3196
);
3197
$dbi->insert({key1 => 2}, table => 'table1');
3198
$result = $dbi->select(table => 'table1');
3199
is($result->one->{key1}, 12);
cleanup test
Yuki Kimoto authored on 2011-08-06
3200

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3201
$dbi = DBIx::Custom->connect;
3202
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3203
$dbi->type_rule(
3204
    from1 => {
3205
        date => sub { $_[0] * 2 },
3206
    },
3207
    into1 => {
3208
        date => sub { $_[0] * 3 },
3209
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
3210
);
cleanup test
Yuki Kimoto authored on 2011-08-10
3211
$dbi->insert({key1 => 2}, table => 'table1');
3212
$result = $dbi->select(table => 'table1');
3213
is($result->fetch->[0], 12);
cleanup test
Yuki Kimoto authored on 2011-08-06
3214

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3215
$dbi = DBIx::Custom->connect;
3216
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3217
$dbi->register_filter(ppp => sub { uc $_[0] });
3218
$dbi->type_rule(
3219
    into1 => {
3220
        date => 'ppp'
cleanup test
Yuki Kimoto authored on 2011-08-06
3221
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3222
);
3223
$dbi->insert({key1 => 'a'}, table => 'table1');
3224
$result = $dbi->select(table => 'table1');
3225
is($result->one->{key1}, 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
3226

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3227
eval{$dbi->type_rule(
3228
    into1 => {
3229
        date => 'pp'
cleanup test
Yuki Kimoto authored on 2011-08-06
3230
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3231
)};
3232
like($@, qr/not registered/);
cleanup test
Yuki Kimoto authored on 2011-08-06
3233

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3234
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3235
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3236
eval {
3237
    $dbi->type_rule(
3238
        from1 => {
3239
            Date => sub { $_[0] * 2 },
3240
        }
3241
    );
3242
};
3243
like($@, qr/lower/);
clenup test
Yuki Kimoto authored on 2011-08-06
3244

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3245
eval {
3246
    $dbi->type_rule(
3247
        into1 => {
3248
            Date => sub { $_[0] * 2 },
3249
        }
3250
    );
3251
};
3252
like($@, qr/lower/);
clenup test
Yuki Kimoto authored on 2011-08-06
3253

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3254
$dbi = DBIx::Custom->connect;
3255
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3256
$dbi->type_rule(
3257
    from1 => {
3258
        date => sub { $_[0] * 2 },
3259
    },
3260
    into1 => {
3261
        date => sub { $_[0] * 3 },
3262
    }
3263
);
3264
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
3265
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3266
$result->type_rule_off;
3267
is($result->one->{key1}, 6);
clenup test
Yuki Kimoto authored on 2011-08-06
3268

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3269
$dbi = DBIx::Custom->connect;
3270
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3271
$dbi->type_rule(
3272
    from1 => {
3273
        date => sub { $_[0] * 2 },
3274
        datetime => sub { $_[0] * 4 },
3275
    },
3276
);
3277
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
3278
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3279
$result->type_rule(
3280
    from1 => {
3281
        date => sub { $_[0] * 3 }
3282
    }
3283
);
3284
$row = $result->one;
3285
is($row->{key1}, 6);
3286
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
3287

            
3288
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3289
$result->type_rule(
3290
    from1 => {
3291
        date => sub { $_[0] * 3 }
3292
    }
3293
);
3294
$row = $result->one;
3295
is($row->{key1}, 6);
3296
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
3297

            
3298
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3299
$result->type_rule(
3300
    from1 => {
3301
        date => sub { $_[0] * 3 }
3302
    }
3303
);
3304
$row = $result->one;
3305
is($row->{key1}, 6);
3306
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
3307
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3308
$result->type_rule(
3309
    from1 => [date => sub { $_[0] * 3 }]
3310
);
3311
$row = $result->one;
3312
is($row->{key1}, 6);
3313
is($row->{key2}, 2);
3314
$dbi->register_filter(fivetimes => sub { $_[0] * 5});
clenup test
Yuki Kimoto authored on 2011-08-06
3315
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3316
$result->type_rule(
3317
    from1 => [date => 'fivetimes']
3318
);
3319
$row = $result->one;
3320
is($row->{key1}, 10);
3321
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
3322
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3323
$result->type_rule(
3324
    from1 => [date => undef]
3325
);
3326
$row = $result->one;
3327
is($row->{key1}, 2);
3328
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
3329

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3330
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3331
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3332
$dbi->type_rule(
3333
    from1 => {
3334
        date => sub { $_[0] * 2 },
3335
    },
3336
);
3337
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
3338
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3339
$result->filter(key1 => sub { $_[0] * 3 });
3340
is($result->one->{key1}, 12);
clenup test
Yuki Kimoto authored on 2011-08-06
3341

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3342
$dbi = DBIx::Custom->connect;
3343
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3344
$dbi->type_rule(
3345
    from1 => {
3346
        date => sub { $_[0] * 2 },
3347
    },
3348
);
3349
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
3350
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3351
$result->filter(key1 => sub { $_[0] * 3 });
3352
is($result->fetch->[0], 12);
clenup test
Yuki Kimoto authored on 2011-08-06
3353

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3354
$dbi = DBIx::Custom->connect;
3355
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3356
$dbi->type_rule(
3357
    into1 => {
3358
        date => sub { $_[0] . 'b' }
3359
    },
3360
    into2 => {
3361
        date => sub { $_[0] . 'c' }
3362
    },
3363
    from1 => {
3364
        date => sub { $_[0] . 'd' }
3365
    },
3366
    from2 => {
3367
        date => sub { $_[0] . 'e' }
3368
    }
3369
);
3370
$dbi->insert({key1 => '1'}, table => 'table1', type_rule_off => 1);
clenup test
Yuki Kimoto authored on 2011-08-06
3371
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3372
is($result->type_rule_off->fetch_first->[0], '1');
clenup test
Yuki Kimoto authored on 2011-08-06
3373
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3374
is($result->type_rule_on->fetch_first->[0], '1de');
clenup test
Yuki Kimoto authored on 2011-08-06
3375

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3376
$dbi = DBIx::Custom->connect;
3377
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3378
$dbi->type_rule(
3379
    into1 => {
3380
        date => sub { $_[0] . 'b' }
cleanup test
Yuki Kimoto authored on 2011-08-06
3381
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
3382
    into2 => {
3383
        date => sub { $_[0] . 'c' }
cleanup test
Yuki Kimoto authored on 2011-08-06
3384
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
3385
    from1 => {
3386
        date => sub { $_[0] . 'd' }
cleanup test
Yuki Kimoto authored on 2011-08-06
3387
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
3388
    from2 => {
3389
        date => sub { $_[0] . 'e' }
cleanup test
Yuki Kimoto authored on 2011-08-06
3390
    }
3391
);
cleanup test
Yuki Kimoto authored on 2011-08-10
3392
$dbi->insert({key1 => '1'}, table => 'table1', type_rule1_off => 1);
3393
$result = $dbi->select(table => 'table1');
3394
is($result->type_rule1_off->fetch_first->[0], '1ce');
3395
$result = $dbi->select(table => 'table1');
3396
is($result->type_rule1_on->fetch_first->[0], '1cde');
cleanup test
Yuki Kimoto authored on 2011-08-06
3397

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3398
$dbi = DBIx::Custom->connect;
3399
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3400
$dbi->type_rule(
3401
    into1 => {
3402
        date => sub { $_[0] . 'b' }
3403
    },
3404
    into2 => {
3405
        date => sub { $_[0] . 'c' }
3406
    },
3407
    from1 => {
3408
        date => sub { $_[0] . 'd' }
3409
    },
3410
    from2 => {
3411
        date => sub { $_[0] . 'e' }
cleanup test
Yuki Kimoto authored on 2011-08-06
3412
    }
3413
);
cleanup test
Yuki Kimoto authored on 2011-08-10
3414
$dbi->insert({key1 => '1'}, table => 'table1', type_rule2_off => 1);
3415
$result = $dbi->select(table => 'table1');
3416
is($result->type_rule2_off->fetch_first->[0], '1bd');
3417
$result = $dbi->select(table => 'table1');
3418
is($result->type_rule2_on->fetch_first->[0], '1bde');
test cleanup
Yuki Kimoto authored on 2011-08-10
3419

            
3420
test 'prefix';
3421
$dbi = DBIx::Custom->connect;
3422
eval { $dbi->execute('drop table table1') };
3423
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
3424
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3425
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace');
3426
$result = $dbi->execute('select * from table1;');
3427
$rows   = $result->all;
3428
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
3429

            
3430
$dbi = DBIx::Custom->connect;
3431
eval { $dbi->execute('drop table table1') };
3432
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
3433
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3434
$dbi->update(table => 'table1', param => {key2 => 4},
3435
  where => {key1 => 1}, prefix => 'or replace');
3436
$result = $dbi->execute('select * from table1;');
3437
$rows   = $result->all;
3438
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
3439

            
3440

            
3441
test 'reserved_word_quote';
3442
$dbi = DBIx::Custom->connect;
3443
eval { $dbi->execute("drop table ${q}table$p") };
3444
$dbi->reserved_word_quote('"');
3445
$dbi->execute($create_table_reserved);
3446
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
3447
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
3448
$dbi->insert(table => 'table', param => {select => 1});
3449
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
3450
$result = $dbi->execute("select * from ${q}table$p");
3451
$rows   = $result->all;
3452
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
3453

            
3454
test 'quote';
3455
$dbi = DBIx::Custom->connect;
3456
$dbi->quote('"');
3457
eval { $dbi->execute("drop table ${q}table$p") };
3458
$dbi->execute($create_table_reserved);
3459
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
3460
$dbi->insert(table => 'table', param => {select => 1});
3461
$dbi->delete(table => 'table', where => {select => 1});
3462
$result = $dbi->execute("select * from ${q}table$p");
3463
$rows   = $result->all;
3464
is_deeply($rows, [], "reserved word");