DBIx-Custom / t / sqlite.t /
Newer Older
2061 lines | 59.92kb
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;
cleanup test
Yuki Kimoto authored on 2011-08-10
7
use lib "$FindBin::Bin/common";
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
use DBIx::Custom;
test cleanup
Yuki Kimoto authored on 2011-08-10
23
{
24
    package DBIx::Custom;
25
    has dsn => sub { 'dbi:SQLite:dbname=:memory:' }
26
}
test cleanup
Yuki Kimoto authored on 2011-08-10
27
use MyDBI1;
28
{
29
    package MyDBI4;
30

            
31
    use strict;
32
    use warnings;
33

            
34
    use base 'DBIx::Custom';
35

            
36
    sub connect {
37
        my $self = shift->SUPER::connect(@_);
38
        
39
        $self->include_model(
40
            MyModel2 => [
41
                'book',
42
                {class => 'Company', name => 'company'}
43
            ]
44
        );
45
    }
46

            
47
    package MyModel2::Base1;
48

            
49
    use strict;
50
    use warnings;
51

            
52
    use base 'DBIx::Custom::Model';
53

            
54
    package MyModel2::book;
55

            
56
    use strict;
57
    use warnings;
58

            
59
    use base 'MyModel2::Base1';
60

            
61
    sub insert {
62
        my ($self, $param) = @_;
63
        
64
        return $self->SUPER::insert(param => $param);
65
    }
66

            
67
    sub list { shift->select; }
68

            
69
    package MyModel2::Company;
70

            
71
    use strict;
72
    use warnings;
73

            
74
    use base 'MyModel2::Base1';
75

            
76
    sub insert {
77
        my ($self, $param) = @_;
78
        
79
        return $self->SUPER::insert(param => $param);
80
    }
81

            
82
    sub list { shift->select; }
83
}
84
{
85
     package MyDBI5;
86

            
87
    use strict;
88
    use warnings;
89

            
90
    use base 'DBIx::Custom';
91

            
92
    sub connect {
93
        my $self = shift->SUPER::connect(@_);
94
        
95
        $self->include_model('MyModel4');
96
    }
97
}
98
{
99
    package MyDBI6;
100
    
101
    use base 'DBIx::Custom';
102
    
103
    sub connect {
104
        my $self = shift->SUPER::connect(@_);
105
        
106
        $self->include_model('MyModel5');
107
        
108
        return $self;
109
    }
110
}
111
{
112
    package MyDBI7;
113
    
114
    use base 'DBIx::Custom';
115
    
116
    sub connect {
117
        my $self = shift->SUPER::connect(@_);
118
        
119
        $self->include_model('MyModel6');
120
        
121
        
122
        return $self;
123
    }
124
}
125
{
126
    package MyDBI8;
127
    
128
    use base 'DBIx::Custom';
129
    
130
    sub connect {
131
        my $self = shift->SUPER::connect(@_);
132
        
133
        $self->include_model('MyModel7');
134
        
135
        return $self;
136
    }
137
}
138

            
139
{
140
    package MyDBI9;
141
    
142
    use base 'DBIx::Custom';
143
    
144
    sub connect {
145
        my $self = shift->SUPER::connect(@_);
146
        
147
        $self->include_model('MyModel8')->setup_model;
148
        
149
        return $self;
150
    }
151
}
test cleanup
Yuki Kimoto authored on 2011-08-10
152

            
cleanup test
Yuki Kimoto authored on 2011-08-06
153
# Constant
cleanup test
Yuki Kimoto authored on 2011-08-10
154
my $create_table1 = 'create table table1 (key1 char(255), key2 char(255));';
155
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
156
my $create_table2 = 'create table table2 (key1 char(255), key3 char(255));';
cleanup test
Yuki Kimoto authored on 2011-08-10
157
my $create_table2_2 = "create table table2 (key1, key2, key3)";
158
my $create_table3 = "create table table3 (key1, key2, key3)";
test cleanup
Yuki Kimoto authored on 2011-08-10
159
my $create_table_reserved = 'create table "table" ("select", "update")';
160

            
test cleanup
Yuki Kimoto authored on 2011-08-10
161
my $q = '"';
162
my $p = '"';
cleanup test
Yuki Kimoto authored on 2011-08-06
163

            
cleanup test
Yuki Kimoto authored on 2011-08-06
164
# Variables
cleanup test
Yuki Kimoto authored on 2011-08-06
165
my $builder;
166
my $datas;
cleanup test
Yuki Kimoto authored on 2011-08-06
167
my $dbi;
168
my $sth;
169
my $source;
170
my @sources;
cleanup test
Yuki Kimoto authored on 2011-08-06
171
my $select_source;
172
my $insert_source;
173
my $update_source;
cleanup test
Yuki Kimoto authored on 2011-08-06
174
my $param;
175
my $params;
176
my $sql;
177
my $result;
178
my $row;
179
my @rows;
180
my $rows;
181
my $query;
182
my @queries;
183
my $select_query;
184
my $insert_query;
185
my $update_query;
186
my $ret_val;
187
my $infos;
188
my $model;
189
my $model2;
190
my $where;
191
my $update_param;
192
my $insert_param;
cleanup test
Yuki Kimoto authored on 2011-08-06
193
my $join;
cleanup test
Yuki Kimoto authored on 2011-08-10
194
my $binary;
cleanup test
Yuki Kimoto authored on 2011-08-06
195

            
196
# Prepare table
test cleanup
Yuki Kimoto authored on 2011-08-10
197
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
198

            
199
test 'dbi method from model';
test cleanup
Yuki Kimoto authored on 2011-08-10
200
$dbi = MyDBI9->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
201
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
202
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
203
$dbi->setup_model;
cleanup test
Yuki Kimoto authored on 2011-08-06
204
$model = $dbi->model('table1');
205
eval{$model->execute('select * from table1')};
206
ok(!$@);
207

            
208
test 'column table option';
test cleanup
Yuki Kimoto authored on 2011-08-10
209
$dbi = MyDBI9->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
210
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
211
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
212
eval { $dbi->execute('drop table table2') };
test cleanup
Yuki Kimoto authored on 2011-08-10
213
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
214
$dbi->setup_model;
215
$dbi->execute('insert into table1 (key1, key2) values (1, 2);');
216
$dbi->execute('insert into table2 (key1, key3) values (1, 4);');
217
$model = $dbi->model('table1');
218
$result = $model->select(
219
    column => [
220
        $model->column('table2', {alias => 'table2_alias'})
221
    ],
222
    where => {'table2_alias.key3' => 4}
223
);
224
is_deeply($result->one, 
225
          {'table2_alias.key1' => 1, 'table2_alias.key3' => 4});
226

            
227
$dbi->separator('__');
228
$result = $model->select(
229
    column => [
230
        $model->column('table2', {alias => 'table2_alias'})
231
    ],
232
    where => {'table2_alias.key3' => 4}
233
);
234
is_deeply($result->one, 
235
          {'table2_alias__key1' => 1, 'table2_alias__key3' => 4});
236

            
237
$dbi->separator('-');
238
$result = $model->select(
239
    column => [
240
        $model->column('table2', {alias => 'table2_alias'})
241
    ],
242
    where => {'table2_alias.key3' => 4}
243
);
244
is_deeply($result->one, 
245
          {'table2_alias-key1' => 1, 'table2_alias-key3' => 4});
246

            
247
test 'create_model';
test cleanup
Yuki Kimoto authored on 2011-08-10
248
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
249
eval { $dbi->execute('drop table table1') };
250
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
251
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
252
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
253

            
254
$dbi->create_model(
255
    table => 'table1',
256
    join => [
257
       'left outer join table2 on table1.key1 = table2.key1'
258
    ],
259
    primary_key => ['key1']
260
);
261
$model2 = $dbi->create_model(
262
    table => 'table2'
263
);
264
$dbi->create_model(
265
    table => 'table3',
266
    filter => [
267
        key1 => {in => sub { uc $_[0] }}
268
    ]
269
);
270
$dbi->setup_model;
271
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
272
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
273
$model = $dbi->model('table1');
274
$result = $model->select(
275
    column => [$model->mycolumn, $model->column('table2')],
276
    where => {'table1.key1' => 1}
277
);
278
is_deeply($result->one,
279
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
280
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
281

            
282
test 'model method';
283
test 'create_model';
test cleanup
Yuki Kimoto authored on 2011-08-10
284
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
285
eval { $dbi->execute('drop table table2') };
test cleanup
Yuki Kimoto authored on 2011-08-10
286
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
287
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
288
$model = $dbi->create_model(
289
    table => 'table2'
290
);
291
$model->method(foo => sub { shift->select(@_) });
292
is_deeply($model->foo->one, {key1 => 1, key3 => 3});
293

            
294
test 'merge_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
295
$dbi = DBIx::Custom->new;
296
$params = [
297
    {key1 => 1, key2 => 2, key3 => 3},
298
    {key1 => 1, key2 => 2},
299
    {key1 => 1}
300
];
301
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]);
302
is_deeply($param, {key1 => [1, 1, 1], key2 => [2, 2], key3 => 3});
303

            
304
$params = [
305
    {key1 => [1, 2], key2 => 1, key3 => [1, 2]},
306
    {key1 => [3, 4], key2 => [2, 3], key3 => 3}
307
];
308
$param = $dbi->merge_param($params->[0], $params->[1]);
309
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
310

            
311
test 'select() param option';
test cleanup
Yuki Kimoto authored on 2011-08-10
312
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
313
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
314
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
315
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
316
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
317
eval { $dbi->execute('drop table table2') };
test cleanup
Yuki Kimoto authored on 2011-08-10
318
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
319
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
320
$dbi->insert(table => 'table2', param => {key1 => 2, key3 => 5});
321
$rows = $dbi->select(
322
    table => 'table1',
323
    column => 'table1.key1 as table1_key1, key2, key3',
324
    where   => {'table1.key2' => 3},
325
    join  => ['inner join (select * from table2 where {= table2.key3})' . 
326
              ' as table2 on table1.key1 = table2.key1'],
327
    param => {'table2.key3' => 5}
328
)->all;
329
is_deeply($rows, [{table1_key1 => 2, key2 => 3, key3 => 5}]);
330

            
331

            
332
test 'select() wrap option';
test cleanup
Yuki Kimoto authored on 2011-08-10
333
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
334
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
335
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
336
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
337
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
338
$rows = $dbi->select(
339
    table => 'table1',
340
    column => 'key1',
341
    wrap => ['select * from (', ') as t where key1 = 1']
342
)->all;
343
is_deeply($rows, [{key1 => 1}]);
344

            
345
eval {
346
$dbi->select(
347
    table => 'table1',
348
    column => 'key1',
349
    wrap => 'select * from ('
350
)
351
};
352
like($@, qr/array/);
353

            
354
test 'select() string where';
test cleanup
Yuki Kimoto authored on 2011-08-10
355
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
356
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
357
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
358
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
359
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
360
$rows = $dbi->select(
361
    table => 'table1',
362
    where => 'key1 = :key1 and key2 = :key2',
363
    where_param => {key1 => 1, key2 => 2}
364
)->all;
365
is_deeply($rows, [{key1 => 1, key2 => 2}]);
366

            
test cleanup
Yuki Kimoto authored on 2011-08-10
367
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
368
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
369
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
370
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
371
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
372
$rows = $dbi->select(
373
    table => 'table1',
374
    where => [
375
        'key1 = :key1 and key2 = :key2',
376
        {key1 => 1, key2 => 2}
377
    ]
378
)->all;
379
is_deeply($rows, [{key1 => 1, key2 => 2}]);
380

            
381
test 'delete() string where';
test cleanup
Yuki Kimoto authored on 2011-08-10
382
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
383
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
384
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
385
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
386
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
387
$dbi->delete(
388
    table => 'table1',
389
    where => 'key1 = :key1 and key2 = :key2',
390
    where_param => {key1 => 1, key2 => 2}
391
);
392
$rows = $dbi->select(table => 'table1')->all;
393
is_deeply($rows, [{key1 => 2, key2 => 3}]);
394

            
test cleanup
Yuki Kimoto authored on 2011-08-10
395
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
396
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
397
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
398
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
399
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
400
$dbi->delete(
401
    table => 'table1',
402
    where => [
403
        'key1 = :key1 and key2 = :key2',
404
         {key1 => 1, key2 => 2}
405
    ]
406
);
407
$rows = $dbi->select(table => 'table1')->all;
408
is_deeply($rows, [{key1 => 2, key2 => 3}]);
409

            
410

            
411
test 'update() string where';
test cleanup
Yuki Kimoto authored on 2011-08-10
412
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
413
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
414
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
415
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
416
$dbi->update(
417
    table => 'table1',
418
    param => {key1 => 5},
419
    where => 'key1 = :key1 and key2 = :key2',
420
    where_param => {key1 => 1, key2 => 2}
421
);
422
$rows = $dbi->select(table => 'table1')->all;
423
is_deeply($rows, [{key1 => 5, key2 => 2}]);
424

            
test cleanup
Yuki Kimoto authored on 2011-08-10
425
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
426
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
427
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
428
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
429
$dbi->update(
430
    table => 'table1',
431
    param => {key1 => 5},
432
    where => [
433
        'key1 = :key1 and key2 = :key2',
434
        {key1 => 1, key2 => 2}
435
    ]
436
);
437
$rows = $dbi->select(table => 'table1')->all;
438
is_deeply($rows, [{key1 => 5, key2 => 2}]);
439

            
440
test 'insert id and primary_key option';
test cleanup
Yuki Kimoto authored on 2011-08-10
441
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
442
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
443
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
444
$dbi->insert(
445
    primary_key => ['key1', 'key2'], 
446
    table => 'table1',
447
    id => [1, 2],
448
    param => {key3 => 3}
449
);
450
is($dbi->select(table => 'table1')->one->{key1}, 1);
451
is($dbi->select(table => 'table1')->one->{key2}, 2);
452
is($dbi->select(table => 'table1')->one->{key3}, 3);
453

            
454
$dbi->delete_all(table => 'table1');
455
$dbi->insert(
456
    primary_key => 'key1', 
457
    table => 'table1',
458
    id => 0,
459
    param => {key2 => 2, key3 => 3}
460
);
461

            
462
is($dbi->select(table => 'table1')->one->{key1}, 0);
463
is($dbi->select(table => 'table1')->one->{key2}, 2);
464
is($dbi->select(table => 'table1')->one->{key3}, 3);
465

            
test cleanup
Yuki Kimoto authored on 2011-08-10
466
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
467
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
468
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
469
$dbi->insert(
470
    {key3 => 3},
471
    primary_key => ['key1', 'key2'], 
472
    table => 'table1',
473
    id => [1, 2],
474
);
475
is($dbi->select(table => 'table1')->one->{key1}, 1);
476
is($dbi->select(table => 'table1')->one->{key2}, 2);
477
is($dbi->select(table => 'table1')->one->{key3}, 3);
478

            
479

            
480
test 'model insert id and primary_key option';
test cleanup
Yuki Kimoto authored on 2011-08-10
481
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
482
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
483
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
484
$dbi->model('table1')->insert(
485
    id => [1, 2],
486
    param => {key3 => 3}
487
);
488
$result = $dbi->model('table1')->select;
489
$row = $result->one;
490
is($row->{key1}, 1);
491
is($row->{key2}, 2);
492
is($row->{key3}, 3);
493

            
test cleanup
Yuki Kimoto authored on 2011-08-10
494
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
495
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
496
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
497
$dbi->model('table1')->insert(
498
    {key3 => 3},
499
    id => [1, 2]
500
);
501
$result = $dbi->model('table1')->select;
502
$row = $result->one;
503
is($row->{key1}, 1);
504
is($row->{key2}, 2);
505
is($row->{key3}, 3);
506

            
507
test 'update and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
508
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
509
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
510
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
511
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
512
$dbi->update(
513
    table => 'table1',
514
    primary_key => ['key1', 'key2'],
515
    id => [1, 2],
516
    param => {key3 => 4}
517
);
518
is($dbi->select(table => 'table1')->one->{key1}, 1);
519
is($dbi->select(table => 'table1')->one->{key2}, 2);
520
is($dbi->select(table => 'table1')->one->{key3}, 4);
521

            
522
$dbi->delete_all(table => 'table1');
523
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
524
$dbi->update(
525
    table => 'table1',
526
    primary_key => 'key1',
527
    id => 0,
528
    param => {key3 => 4}
529
);
530
is($dbi->select(table => 'table1')->one->{key1}, 0);
531
is($dbi->select(table => 'table1')->one->{key2}, 2);
532
is($dbi->select(table => 'table1')->one->{key3}, 4);
533

            
test cleanup
Yuki Kimoto authored on 2011-08-10
534
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
535
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
536
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
537
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
538
$dbi->update(
539
    {key3 => 4},
540
    table => 'table1',
541
    primary_key => ['key1', 'key2'],
542
    id => [1, 2]
543
);
544
is($dbi->select(table => 'table1')->one->{key1}, 1);
545
is($dbi->select(table => 'table1')->one->{key2}, 2);
546
is($dbi->select(table => 'table1')->one->{key3}, 4);
547

            
548

            
549
test 'model update and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
550
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
551
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
552
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
553
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
554
$dbi->model('table1')->update(
555
    id => [1, 2],
556
    param => {key3 => 4}
557
);
558
$result = $dbi->model('table1')->select;
559
$row = $result->one;
560
is($row->{key1}, 1);
561
is($row->{key2}, 2);
562
is($row->{key3}, 4);
563

            
564

            
565
test 'delete and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
566
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
567
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
568
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
569
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
570
$dbi->delete(
571
    table => 'table1',
572
    primary_key => ['key1', 'key2'],
573
    id => [1, 2],
574
);
575
is_deeply($dbi->select(table => 'table1')->all, []);
576

            
577
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
578
$dbi->delete(
579
    table => 'table1',
580
    primary_key => 'key1',
581
    id => 0,
582
);
583
is_deeply($dbi->select(table => 'table1')->all, []);
584

            
585

            
586
test 'model delete and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
587
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
588
eval { $dbi->execute('drop table table1') };
589
eval { $dbi->execute('drop table table2') };
590
eval { $dbi->execute('drop table table3') };
cleanup test
Yuki Kimoto authored on 2011-08-10
591
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-10
592
$dbi->execute($create_table2_2);
593
$dbi->execute($create_table3);
cleanup test
Yuki Kimoto authored on 2011-08-06
594
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
595
$dbi->model('table1')->delete(id => [1, 2]);
596
is_deeply($dbi->select(table => 'table1')->all, []);
597
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
598
$dbi->model('table1_1')->delete(id => [1, 2]);
599
is_deeply($dbi->select(table => 'table1')->all, []);
600
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
601
$dbi->model('table1_3')->delete(id => [1, 2]);
602
is_deeply($dbi->select(table => 'table1')->all, []);
603

            
604

            
605
test 'select and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
606
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
607
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
608
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
609
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
610
$result = $dbi->select(
611
    table => 'table1',
612
    primary_key => ['key1', 'key2'],
613
    id => [1, 2]
614
);
615
$row = $result->one;
616
is($row->{key1}, 1);
617
is($row->{key2}, 2);
618
is($row->{key3}, 3);
619

            
620
$dbi->delete_all(table => 'table1');
621
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
622
$result = $dbi->select(
623
    table => 'table1',
624
    primary_key => 'key1',
625
    id => 0,
626
);
627
$row = $result->one;
628
is($row->{key1}, 0);
629
is($row->{key2}, 2);
630
is($row->{key3}, 3);
631

            
632
$dbi->delete_all(table => 'table1');
633
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
634
$result = $dbi->select(
635
    table => 'table1',
636
    primary_key => ['key1', 'key2'],
637
    id => [1, 2]
638
);
639
$row = $result->one;
640
is($row->{key1}, 1);
641
is($row->{key2}, 2);
642
is($row->{key3}, 3);
643

            
644

            
645
test 'model select_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
646
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
647
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
648
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
649
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
650
$result = $dbi->model('table1')->select(id => [1, 2]);
651
$row = $result->one;
652
is($row->{key1}, 1);
653
is($row->{key2}, 2);
654
is($row->{key3}, 3);
655

            
656
test 'column separator is default .';
test cleanup
Yuki Kimoto authored on 2011-08-10
657
$dbi = MyDBI7->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
658
eval { $dbi->execute('drop table table1') };
659
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
660
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
661
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
662
$dbi->setup_model;
663
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
664
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
665
$model = $dbi->model('table1');
666
$result = $model->select(
667
    column => [$model->column('table2')],
668
    where => {'table1.key1' => 1}
669
);
670
is_deeply($result->one,
671
          {'table2.key1' => 1, 'table2.key3' => 3});
672

            
673
$result = $model->select(
674
    column => [$model->column('table2' => [qw/key1 key3/])],
675
    where => {'table1.key1' => 1}
676
);
677
is_deeply($result->one,
678
          {'table2.key1' => 1, 'table2.key3' => 3});
679

            
680

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

            
682
test 'separator';
test cleanup
Yuki Kimoto authored on 2011-08-10
683
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
684
eval { $dbi->execute('drop table table1') };
685
eval { $dbi->execute('drop table table2') };
686
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
687
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-10
688

            
689
$dbi->create_model(
690
    table => 'table1',
691
    join => [
692
       'left outer join table2 on table1.key1 = table2.key1'
693
    ],
694
    primary_key => ['key1'],
cleanup test
Yuki Kimoto authored on 2011-08-06
695
);
cleanup test
Yuki Kimoto authored on 2011-08-10
696
$model2 = $dbi->create_model(
697
    table => 'table2',
698
);
699
$dbi->setup_model;
700
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
701
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
702
$model = $dbi->model('table1');
703
$result = $model->select(
704
    column => [
705
        $model->mycolumn,
706
        {table2 => [qw/key1 key3/]}
707
    ],
708
    where => {'table1.key1' => 1}
709
);
710
is_deeply($result->one,
711
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
712
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
713

            
cleanup test
Yuki Kimoto authored on 2011-08-10
714
$dbi->separator('__');
715
$model = $dbi->model('table1');
716
$result = $model->select(
717
    column => [
718
        $model->mycolumn,
719
        {table2 => [qw/key1 key3/]}
720
    ],
721
    where => {'table1.key1' => 1}
722
);
723
is_deeply($result->one,
724
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
725
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
726

            
cleanup test
Yuki Kimoto authored on 2011-08-10
727
$dbi->separator('-');
728
$model = $dbi->model('table1');
729
$result = $model->select(
730
    column => [
731
        $model->mycolumn,
732
        {table2 => [qw/key1 key3/]}
733
    ],
734
    where => {'table1.key1' => 1}
735
);
736
is_deeply($result->one,
737
          {key1 => 1, key2 => 2, 'table2-key1' => 1, 'table2-key3' => 3});
738
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
739

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

            
741
test 'filter_off';
test cleanup
Yuki Kimoto authored on 2011-08-10
742
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
743
eval { $dbi->execute('drop table table1') };
744
eval { $dbi->execute('drop table table2') };
745
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
746
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-10
747

            
748
$dbi->create_model(
749
    table => 'table1',
750
    join => [
751
       'left outer join table2 on table1.key1 = table2.key1'
752
    ],
753
    primary_key => ['key1'],
cleanup test
Yuki Kimoto authored on 2011-08-06
754
);
cleanup test
Yuki Kimoto authored on 2011-08-10
755
$dbi->setup_model;
756
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
757
$model = $dbi->model('table1');
758
$result = $model->select(column => 'key1');
759
$result->filter(key1 => sub { $_[0] * 2 });
760
is_deeply($result->one, {key1 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-06
761

            
cleanup test
Yuki Kimoto authored on 2011-08-10
762
test 'available_datetype';
test cleanup
Yuki Kimoto authored on 2011-08-10
763
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
764
ok($dbi->can('available_datatype'));
765

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
767
test 'select prefix option';
test cleanup
Yuki Kimoto authored on 2011-08-10
768
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
769
eval { $dbi->execute('drop table table1') };
770
$dbi->execute($create_table1);
771
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
772
$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all;
773
is_deeply($rows, [{key1 => 1, key2 => 2}], "table");
774

            
775
test 'map_param';
776
$dbi = DBIx::Custom->connect;
777
$param = $dbi->map_param(
778
    {id => 1, author => 'Ken', price => 1900},
779
    id => 'book.id',
780
    author => ['book.author', sub { '%' . $_[0] . '%' }],
781
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
cleanup test
Yuki Kimoto authored on 2011-08-06
782
);
cleanup test
Yuki Kimoto authored on 2011-08-10
783
is_deeply($param, {'book.id' => 1, 'book.author' => '%Ken%',
784
  'book.price' => 1900});
785

            
786
$param = $dbi->map_param(
787
    {id => 0, author => 0, price => 0},
788
    id => 'book.id',
789
    author => ['book.author', sub { '%' . $_[0] . '%' }],
790
    price => ['book.price', sub { '%' . $_[0] . '%' },
791
      {if => sub { $_[0] eq 0 }}]
cleanup test
Yuki Kimoto authored on 2011-08-06
792
);
cleanup test
Yuki Kimoto authored on 2011-08-10
793
is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
cleanup test
Yuki Kimoto authored on 2011-08-06
794

            
cleanup test
Yuki Kimoto authored on 2011-08-10
795
$param = $dbi->map_param(
796
    {id => '', author => '', price => ''},
797
    id => 'book.id',
798
    author => ['book.author', sub { '%' . $_[0] . '%' }],
799
    price => ['book.price', sub { '%' . $_[0] . '%' },
800
      {if => sub { $_[0] eq 1 }}]
cleanup test
Yuki Kimoto authored on 2011-08-06
801
);
cleanup test
Yuki Kimoto authored on 2011-08-10
802
is_deeply($param, {});
803

            
804
$param = $dbi->map_param(
805
    {id => undef, author => undef, price => undef},
806
    id => 'book.id',
807
    price => ['book.price', {if => 'exists'}]
cleanup test
Yuki Kimoto authored on 2011-08-06
808
);
cleanup test
Yuki Kimoto authored on 2011-08-10
809
is_deeply($param, {'book.price' => undef});
810

            
811
$param = $dbi->map_param(
812
    {price => 'a'},
813
    id => ['book.id', {if => 'exists'}],
814
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
815
);
816
is_deeply($param, {'book.price' => '%a'});
cleanup test
Yuki Kimoto authored on 2011-08-06
817

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

            
819
test 'table_alias';
test cleanup
Yuki Kimoto authored on 2011-08-10
820
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
821
eval { $dbi->execute('drop table table1') };
822
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
cleanup test
Yuki Kimoto authored on 2011-08-06
823
$dbi->type_rule(
824
    into1 => {
cleanup test
Yuki Kimoto authored on 2011-08-10
825
        date => sub { uc $_[0] }
cleanup test
Yuki Kimoto authored on 2011-08-06
826
    }
827
);
cleanup test
Yuki Kimoto authored on 2011-08-10
828
$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'},
829
  table_alias => {table2 => 'table1'});
cleanup test
Yuki Kimoto authored on 2011-08-06
830
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
831
is($result->one->{key1}, 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
832

            
833

            
cleanup test
Yuki Kimoto authored on 2011-08-10
834
test 'order';
test cleanup
Yuki Kimoto authored on 2011-08-10
835
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
836
eval { $dbi->execute('drop table table1') };
837
$dbi->execute("create table table1 (key1, key2)");
838
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
839
$dbi->insert({key1 => 1, key2 => 3}, table => 'table1');
840
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
841
$dbi->insert({key1 => 2, key2 => 4}, table => 'table1');
842
my $order = $dbi->order;
843
$order->prepend('key1', 'key2 desc');
844
$result = $dbi->select(table => 'table1', append => "$order");
845
is_deeply($result->all, [{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1},
846
  {key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}]);
847
$order->prepend('key1 desc');
848
$result = $dbi->select(table => 'table1', append => "$order");
849
is_deeply($result->all, [{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2},
850
  {key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
851

            
cleanup test
Yuki Kimoto authored on 2011-08-10
852
$order = $dbi->order;
853
$order->prepend(['table1-key1'], [qw/table1-key2 desc/]);
854
$result = $dbi->select(table => 'table1',
855
  column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']],
856
  append => "$order");
857
is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3},
858
  {'table1-key1' => 1, 'table1-key2' => 1},
859
  {'table1-key1' => 2, 'table1-key2' => 4},
860
  {'table1-key1' => 2, 'table1-key2' => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
861

            
cleanup test
Yuki Kimoto authored on 2011-08-10
862
test 'tag_parse';
test cleanup
Yuki Kimoto authored on 2011-08-10
863
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
864
$dbi->tag_parse(0);
865
eval { $dbi->execute('drop table table1') };
866
$dbi->execute("create table table1 (key1, key2)");
867
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
868
eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})};
869
ok($@);
cleanup test
Yuki Kimoto authored on 2011-08-06
870

            
cleanup test
Yuki Kimoto authored on 2011-08-10
871
test 'last_sql';
test cleanup
Yuki Kimoto authored on 2011-08-10
872
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
873
eval { $dbi->execute('drop table table1') };
874
$dbi->execute("create table table1 (key1, key2)");
875
$dbi->execute('select * from table1');
876
is($dbi->last_sql, 'select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
877

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
881
test 'DBIx::Custom header';
test cleanup
Yuki Kimoto authored on 2011-08-10
882
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
883
eval { $dbi->execute('drop table table1') };
884
$dbi->execute("create table table1 (key1, key2)");
885
$result = $dbi->execute('select key1 as h1, key2 as h2 from table1');
886
is_deeply($result->header, [qw/h1 h2/]);
cleanup test
Yuki Kimoto authored on 2011-08-06
887

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

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
909
$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}";
910
$result = $dbi->execute(
911
    $source,
912
    param => {'table1.key1' => 1, 'table1.key2' => 1},
913
    filter => {'table1.key2' => sub { $_[0] * 2 }}
cleanup test
Yuki Kimoto authored on 2011-08-06
914
);
cleanup test
Yuki Kimoto authored on 2011-08-10
915
$rows = $result->all;
916
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
917

            
cleanup test
Yuki Kimoto authored on 2011-08-10
918
test 'high perfomance way';
919
$dbi->execute('drop table table1');
920
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
921
$rows = [
922
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
923
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
924
];
925
{
926
    my $query;
927
    foreach my $row (@$rows) {
928
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
929
      $dbi->execute($query, $row, filter => {ab => sub { $_[0] * 2 }});
cleanup test
Yuki Kimoto authored on 2011-08-06
930
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
931
    is_deeply($dbi->select(table => 'table1')->all,
932
      [
933
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
934
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
935
      ]
936
    );
937
}
938

            
939
$dbi->execute('drop table table1');
940
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
941
$rows = [
942
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
943
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
944
];
945
{
946
    my $query;
947
    my $sth;
948
    foreach my $row (@$rows) {
949
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
950
      $sth ||= $query->sth;
951
      $sth->execute(map { $row->{$_} } sort keys %$row);
cleanup test
Yuki Kimoto authored on 2011-08-06
952
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
953
    is_deeply($dbi->select(table => 'table1')->all,
954
      [
955
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
956
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
957
      ]
958
    );
959
}
cleanup test
Yuki Kimoto authored on 2011-08-06
960

            
cleanup test
Yuki Kimoto authored on 2011-08-10
961
test 'result';
test cleanup
Yuki Kimoto authored on 2011-08-10
962
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
963
eval { $dbi->execute('drop table table1') };
964
$dbi->execute($create_table1);
965
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
966
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
967

            
cleanup test
Yuki Kimoto authored on 2011-08-06
968
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
969
@rows = ();
970
while (my $row = $result->fetch) {
971
    push @rows, [@$row];
972
}
973
is_deeply(\@rows, [[1, 2], [3, 4]]);
cleanup test
Yuki Kimoto authored on 2011-08-06
974

            
975
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
976
@rows = ();
977
while (my $row = $result->fetch_hash) {
978
    push @rows, {%$row};
979
}
980
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
981

            
982
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
983
$row = $result->fetch_first;
984
is_deeply($row, [1, 2], "row");
985
$row = $result->fetch;
986
ok(!$row, "finished");
987

            
cleanup test
Yuki Kimoto authored on 2011-08-06
988
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
989
$row = $result->fetch_hash_first;
990
is_deeply($row, {key1 => 1, key2 => 2}, "row");
991
$row = $result->fetch_hash;
992
ok(!$row, "finished");
993

            
994
$dbi->execute('create table table2 (key1, key2);');
995
$result = $dbi->select(table => 'table2');
996
$row = $result->fetch_hash_first;
997
ok(!$row, "no row fetch");
cleanup test
Yuki Kimoto authored on 2011-08-06
998

            
test cleanup
Yuki Kimoto authored on 2011-08-10
999
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1000
eval { $dbi->execute('drop table table1') };
1001
$dbi->execute($create_table1);
1002
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
1003
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
1004
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1');
1005
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1');
1006
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
1007
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1008
$rows = $result->fetch_multi(2);
1009
is_deeply($rows, [[1, 2],
1010
                  [3, 4]], "fetch_multi first");
1011
$rows = $result->fetch_multi(2);
1012
is_deeply($rows, [[5, 6],
1013
                  [7, 8]], "fetch_multi secound");
1014
$rows = $result->fetch_multi(2);
1015
is_deeply($rows, [[9, 10]], "fetch_multi third");
1016
$rows = $result->fetch_multi(2);
1017
ok(!$rows);
1018

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

            
1023
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1024
$rows = $result->fetch_hash_multi(2);
1025
is_deeply($rows, [{key1 => 1, key2 => 2},
1026
                  {key1 => 3, key2 => 4}], "fetch_multi first");
1027
$rows = $result->fetch_hash_multi(2);
1028
is_deeply($rows, [{key1 => 5, key2 => 6},
1029
                  {key1 => 7, key2 => 8}], "fetch_multi secound");
1030
$rows = $result->fetch_hash_multi(2);
1031
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third");
1032
$rows = $result->fetch_hash_multi(2);
1033
ok(!$rows);
1034

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1039
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1040
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1041
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
1042
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
1043
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
1044

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1045
test 'fetch_all';
1046
$result = $dbi->select(table => 'table1');
1047
$rows = $result->fetch_all;
1048
is_deeply($rows, [[1, 2], [3, 4]]);
cleanup test
Yuki Kimoto authored on 2011-08-06
1049

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1061
$result = $dbi->select(table => 'table1');
1062
$result->dbi->filters({three_times => sub { $_[0] * 3}});
1063
$result->filter({key1 => 'three_times'});
1064
$rows = $result->fetch_hash_all;
1065
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash");
1066

            
1067
test "query_builder";
1068
$datas = [
1069
    # Basic tests
1070
    {   name            => 'placeholder basic',
1071
        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
1072
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;",
1073
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
1074
    },
1075
    {
1076
        name            => 'placeholder in',
1077
        source            => "{in k1 3};",
1078
        sql_expected    => "k1 in (?, ?, ?);",
1079
        columns_expected   => [qw/k1 k1 k1/]
1080
    },
1081
    
1082
    # Table name
1083
    {
1084
        name            => 'placeholder with table name',
1085
        source            => "{= a.k1} {= a.k2}",
1086
        sql_expected    => "a.k1 = ? a.k2 = ?;",
1087
        columns_expected  => [qw/a.k1 a.k2/]
1088
    },
1089
    {   
1090
        name            => 'placeholder in with table name',
1091
        source            => "{in a.k1 2} {in b.k2 2}",
1092
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?);",
1093
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
1094
    },
1095
    {
1096
        name            => 'not contain tag',
1097
        source            => "aaa",
1098
        sql_expected    => "aaa;",
1099
        columns_expected  => [],
1100
    }
1101
];
1102

            
1103
for (my $i = 0; $i < @$datas; $i++) {
1104
    my $data = $datas->[$i];
1105
    my $builder = DBIx::Custom->new->query_builder;
1106
    my $query = $builder->build_query($data->{source});
1107
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
1108
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
1109
}
1110

            
1111
$builder = DBIx::Custom->new->query_builder;
1112
$ret_val = $builder->register_tag(
1113
    p => sub {
1114
        my @args = @_;
1115
        
1116
        my $expand    = "? $args[0] $args[1]";
1117
        my $columns = [2];
1118
        return [$expand, $columns];
1119
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
1120
);
1121

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

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1135
$builder->register_tag({
1136
    q => 'string'
1137
});
cleanup test
Yuki Kimoto authored on 2011-08-06
1138

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1142
$builder->register_tag({
1143
   r => sub {} 
1144
});
cleanup test
Yuki Kimoto authored on 2011-08-06
1145

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

            
1149
$builder->register_tag({
1150
   s => sub { return ["a", ""]} 
1151
});
1152

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

            
1156
$builder->register_tag(
1157
    t => sub {return ["a", []]}
cleanup test
Yuki Kimoto authored on 2011-08-06
1158
);
1159

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

            
1161
test 'General error case';
1162
$builder = DBIx::Custom->new->query_builder;
1163
$builder->register_tag(
1164
    a => sub {
1165
        return ["? ? ?", ['']];
1166
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
1167
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1168
eval{$builder->build_query("{a}")};
1169
like($@, qr/\QPlaceholder count/, "placeholder count is invalid");
cleanup test
Yuki Kimoto authored on 2011-08-06
1170

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

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

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

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

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

            
1187
test 'variouse source';
1188
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;";
1189
$query = $builder->build_query($source);
1190
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1");
1191

            
1192
$source = "abc;";
1193
$query = $builder->build_query($source);
1194
is($query->sql, 'abc;', "basic : 2");
1195

            
1196
$source = "{= a}";
1197
$query = $builder->build_query($source);
1198
is($query->sql, 'a = ?;', "only tag");
1199

            
1200
$source = "000;";
1201
$query = $builder->build_query($source);
1202
is($query->sql, '000;', "contain 0 value");
1203

            
1204
$source = "a {= b} }";
1205
eval{$builder->build_query($source)};
1206
like($@, qr/unexpected "}"/, "error : 1");
1207

            
1208
$source = "a {= {}";
1209
eval{$builder->build_query($source)};
1210
like($@, qr/unexpected "{"/, "error : 2");
1211

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

            
1213

            
1214

            
1215

            
1216

            
1217

            
1218

            
1219

            
1220

            
1221

            
1222

            
1223

            
1224

            
1225

            
1226

            
1227

            
1228

            
1229

            
1230

            
1231

            
1232

            
1233

            
1234

            
1235

            
1236

            
1237

            
1238
### a little complex test
cleanup test
Yuki Kimoto authored on 2011-08-10
1239
test 'join';
1240
$dbi = DBIx::Custom->connect;
1241
eval { $dbi->execute('drop table table1') };
1242
$dbi->execute($create_table1);
1243
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1244
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1245
$dbi->execute($create_table2);
1246
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
1247
$dbi->execute('create table table3 (key3 int, key4 int);');
1248
$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4});
1249
$rows = $dbi->select(
1250
    table => 'table1',
1251
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
1252
    where   => {'table1.key2' => 2},
1253
    join  => ['left outer join table2 on table1.key1 = table2.key1']
1254
)->all;
1255
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]);
1256

            
1257
$rows = $dbi->select(
1258
    table => 'table1',
1259
    where   => {'key1' => 1},
1260
    join  => ['left outer join table2 on table1.key1 = table2.key1']
1261
)->all;
1262
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1263

            
1264
eval {
1265
    $rows = $dbi->select(
1266
        table => 'table1',
1267
        column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
1268
        where   => {'table1.key2' => 2},
1269
        join  => {'table1.key1' => 'table2.key1'}
1270
    );
1271
};
1272
like ($@, qr/array/);
1273

            
1274
$rows = $dbi->select(
1275
    table => 'table1',
1276
    where   => {'key1' => 1},
1277
    join  => ['left outer join table2 on table1.key1 = table2.key1',
1278
              'left outer join table3 on table2.key3 = table3.key3']
1279
)->all;
1280
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1281

            
1282
$rows = $dbi->select(
1283
    column => 'table3.key4 as table3__key4',
1284
    table => 'table1',
1285
    where   => {'table1.key1' => 1},
1286
    join  => ['left outer join table2 on table1.key1 = table2.key1',
1287
              'left outer join table3 on table2.key3 = table3.key3']
1288
)->all;
1289
is_deeply($rows, [{table3__key4 => 4}]);
1290

            
1291
$rows = $dbi->select(
1292
    column => 'table1.key1 as table1__key1',
1293
    table => 'table1',
1294
    where   => {'table3.key4' => 4},
1295
    join  => ['left outer join table2 on table1.key1 = table2.key1',
1296
              'left outer join table3 on table2.key3 = table3.key3']
1297
)->all;
1298
is_deeply($rows, [{table1__key1 => 1}]);
1299

            
1300
$dbi = DBIx::Custom->connect;
1301
$dbi->quote('"');
1302
eval { $dbi->execute('drop table table1') };
1303
$dbi->execute($create_table1);
1304
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1305
$dbi->execute($create_table2);
1306
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
1307
$rows = $dbi->select(
1308
    table => 'table1',
1309
    column => '"table1"."key1" as "table1_key1", "table2"."key1" as "table2_key1", "key2", "key3"',
1310
    where   => {'table1.key2' => 2},
1311
    join  => ['left outer join "table2" on "table1"."key1" = "table2"."key1"'],
1312
)->all;
1313
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}],
1314
          'quote');
1315

            
1316

            
1317
$dbi = DBIx::Custom->connect;
1318
eval { $dbi->execute('drop table table1') };
1319
$dbi->execute($create_table1);
1320
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1321
$sql = <<"EOS";
1322
left outer join (
1323
  select * from table1 as t1
1324
  where t1.key2 = (
1325
    select max(t2.key2) from table1 as t2
1326
    where t1.key1 = t2.key1
1327
  )
1328
) as latest_table1 on table1.key1 = latest_table1.key1
1329
EOS
1330
$join = [$sql];
1331
$rows = $dbi->select(
1332
    table => 'table1',
1333
    column => 'latest_table1.key1 as latest_table1__key1',
1334
    join  => $join
1335
)->all;
1336
is_deeply($rows, [{latest_table1__key1 => 1}]);
1337

            
1338
$dbi = DBIx::Custom->connect;
1339
eval { $dbi->execute('drop table table1') };
1340
eval { $dbi->execute('drop table table2') };
1341
$dbi->execute($create_table1);
1342
$dbi->execute($create_table2);
1343
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1344
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
1345
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
1346
$result = $dbi->select(
1347
    table => 'table1',
1348
    join => [
1349
        "left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1"
1350
    ]
1351
);
1352
is_deeply($result->all, [{key1 => 1, key2 => 2}]);
1353
$result = $dbi->select(
1354
    table => 'table1',
1355
    column => [{table2 => ['key3']}],
1356
    join => [
1357
        "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1"
1358
    ]
1359
);
1360
is_deeply($result->all, [{'table2.key3' => 4}]);
1361
$result = $dbi->select(
1362
    table => 'table1',
1363
    column => [{table2 => ['key3']}],
1364
    join => [
1365
        "left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'"
1366
    ]
1367
);
1368
is_deeply($result->all, [{'table2.key3' => 4}]);
1369

            
1370
$dbi = DBIx::Custom->connect;
1371
eval { $dbi->execute('drop table table1') };
1372
eval { $dbi->execute('drop table table2') };
1373
$dbi->execute($create_table1);
1374
$dbi->execute($create_table2);
1375
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1376
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
1377
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
1378
$result = $dbi->select(
1379
    table => 'table1',
1380
    column => [{table2 => ['key3']}],
1381
    join => [
1382
        {
1383
            clause => "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1",
1384
            table => ['table1', 'table2']
1385
        }
1386
    ]
1387
);
1388
is_deeply($result->all, [{'table2.key3' => 4}]);
1389

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

            
1391
test 'update_param';
1392
$dbi = DBIx::Custom->connect;
1393
eval { $dbi->execute('drop table table1') };
1394
$dbi->execute($create_table1_2);
1395
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1396
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1397

            
1398
$param = {key2 => 11};
1399
$update_param = $dbi->update_param($param);
1400
$sql = <<"EOS";
1401
update table1 $update_param
1402
where key1 = 1
1403
EOS
1404
$dbi->execute($sql, param => $param);
1405
$result = $dbi->execute('select * from table1;', table => 'table1');
1406
$rows   = $result->all;
1407
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
1408
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1409
                  "basic");
1410

            
1411

            
1412
$dbi = DBIx::Custom->connect;
1413
eval { $dbi->execute('drop table table1') };
1414
$dbi->execute($create_table1_2);
1415
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1416
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1417

            
1418
$param = {key2 => 11, key3 => 33};
1419
$update_param = $dbi->update_param($param);
1420
$sql = <<"EOS";
1421
update table1 $update_param
1422
where key1 = 1
1423
EOS
1424
$dbi->execute($sql, param => $param);
1425
$result = $dbi->execute('select * from table1;', table => 'table1');
1426
$rows   = $result->all;
1427
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
1428
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1429
                  "basic");
1430

            
1431
$dbi = DBIx::Custom->connect;
1432
eval { $dbi->execute('drop table table1') };
1433
$dbi->execute($create_table1_2);
1434
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1435
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1436

            
1437
$param = {key2 => 11, key3 => 33};
1438
$update_param = $dbi->update_param($param, {no_set => 1});
1439
$sql = <<"EOS";
1440
update table1 set $update_param
1441
where key1 = 1
1442
EOS
1443
$dbi->execute($sql, param => $param);
1444
$result = $dbi->execute('select * from table1;', table => 'table1');
1445
$rows   = $result->all;
1446
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
1447
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1448
                  "update param no_set");
1449

            
1450
            
1451
eval { $dbi->update_param({";" => 1}) };
1452
like($@, qr/not safety/);
1453

            
1454

            
1455
test 'update_param';
1456
$dbi = DBIx::Custom->connect;
1457
eval { $dbi->execute('drop table table1') };
1458
$dbi->execute($create_table1_2);
1459
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1460
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1461

            
1462
$param = {key2 => 11};
1463
$update_param = $dbi->assign_param($param);
1464
$sql = <<"EOS";
1465
update table1 set $update_param
1466
where key1 = 1
1467
EOS
1468
$dbi->execute($sql, param => $param, table => 'table1');
1469
$result = $dbi->execute('select * from table1;');
1470
$rows   = $result->all;
1471
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
1472
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1473
                  "basic");
1474

            
1475

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1476
test 'type option'; # DEPRECATED!
1477
$dbi = DBIx::Custom->connect(
1478
    data_source => 'dbi:SQLite:dbname=:memory:',
1479
    dbi_option => {
1480
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
1481
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
1482
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1483
$binary = pack("I3", 1, 2, 3);
1484
eval { $dbi->execute('drop table table1') };
1485
$dbi->execute('create table table1(key1, key2)');
1486
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
1487
$result = $dbi->select(table => 'table1');
1488
$row   = $result->one;
1489
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
1490
$result = $dbi->execute('select length(key1) as key1_length from table1');
1491
$row = $result->one;
1492
is($row->{key1_length}, length $binary);
cleanup test
Yuki Kimoto authored on 2011-08-06
1493

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1494
test 'type_rule from';
1495
$dbi = DBIx::Custom->connect;
1496
$dbi->type_rule(
1497
    from1 => {
1498
        date => sub { uc $_[0] }
1499
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
1500
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1501
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1502
$dbi->insert({key1 => 'a'}, table => 'table1');
1503
$result = $dbi->select(table => 'table1');
1504
is($result->fetch_first->[0], 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
1505

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

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

            
1510
test 'type_rule into';
test cleanup
Yuki Kimoto authored on 2011-08-10
1511
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1512
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1513
$dbi->type_rule(
1514
    into1 => {
1515
        date => sub { uc $_[0] }
1516
    }
1517
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1518
$dbi->insert({key1 => 'a'}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
1519
$result = $dbi->select(table => 'table1');
1520
is($result->one->{key1}, 'A');
1521

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1522
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1523
$dbi->execute("create table table1 (key1 date, key2 datetime)");
1524
$dbi->type_rule(
1525
    into1 => [
1526
         [qw/date datetime/] => sub { uc $_[0] }
1527
    ]
1528
);
1529
$dbi->insert({key1 => 'a', key2 => 'b'}, table => 'table1');
1530
$result = $dbi->select(table => 'table1');
1531
$row = $result->one;
1532
is($row->{key1}, 'A');
1533
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
1534

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1535
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1536
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1537
$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1');
1538
$dbi->type_rule(
1539
    into1 => [
1540
        [qw/date datetime/] => sub { uc $_[0] }
1541
    ]
1542
);
1543
$result = $dbi->execute(
1544
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
1545
    param => {key1 => 'a', 'table1.key2' => 'b'}
1546
);
1547
$row = $result->one;
1548
is($row->{key1}, 'a');
1549
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
1550

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1551
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1552
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1553
$dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1');
1554
$dbi->type_rule(
1555
    into1 => [
1556
        [qw/date datetime/] => sub { uc $_[0] }
1557
    ]
1558
);
1559
$result = $dbi->execute(
1560
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
1561
    param => {key1 => 'a', 'table1.key2' => 'b'},
1562
    table => 'table1'
1563
);
1564
$row = $result->one;
1565
is($row->{key1}, 'A');
1566
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
1567

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1568
$dbi = DBIx::Custom->connect;
1569
$dbi->execute("create table table1 (key1 date, key2 datetime)");
1570
$dbi->register_filter(twice => sub { $_[0] * 2 });
1571
$dbi->type_rule(
1572
    from1 => {
1573
        date => 'twice',
1574
    },
1575
    into1 => {
1576
        date => 'twice',
1577
    }
1578
);
1579
$dbi->insert({key1 => 2}, table => 'table1');
1580
$result = $dbi->select(table => 'table1');
1581
is($result->fetch->[0], 8);
cleanup test
Yuki Kimoto authored on 2011-08-06
1582

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1583
test 'type_rule and filter order';
test cleanup
Yuki Kimoto authored on 2011-08-10
1584
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1585
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1586
$dbi->type_rule(
1587
    into1 => {
1588
        date => sub { $_[0] . 'b' }
1589
    },
1590
    into2 => {
1591
        date => sub { $_[0] . 'c' }
1592
    },
1593
    from1 => {
1594
        date => sub { $_[0] . 'd' }
1595
    },
1596
    from2 => {
1597
        date => sub { $_[0] . 'e' }
1598
    }
1599
);
1600
$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }});
1601
$result = $dbi->select(table => 'table1');
1602
$result->filter(key1 => sub { $_[0] . 'f' });
1603
is($result->fetch_first->[0], '1abcdef');
cleanup test
Yuki Kimoto authored on 2011-08-06
1604

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1605
$dbi = DBIx::Custom->connect;
1606
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1607
$dbi->type_rule(
1608
    from1 => {
1609
        date => sub { $_[0] . 'p' }
1610
    },
1611
    from2 => {
1612
        date => sub { $_[0] . 'q' }
1613
    },
1614
);
1615
$dbi->insert({key1 => '1'}, table => 'table1');
1616
$result = $dbi->select(table => 'table1');
1617
$result->type_rule(
1618
    from1 => {
1619
        date => sub { $_[0] . 'd' }
1620
    },
1621
    from2 => {
1622
        date => sub { $_[0] . 'e' }
1623
    }
1624
);
1625
$result->filter(key1 => sub { $_[0] . 'f' });
1626
is($result->fetch_first->[0], '1def');
cleanup test
Yuki Kimoto authored on 2011-08-06
1627

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1628
test 'type_rule_off';
1629
$dbi = DBIx::Custom->connect;
1630
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1631
$dbi->type_rule(
1632
    from1 => {
1633
        date => sub { $_[0] * 2 },
1634
    },
1635
    into1 => {
1636
        date => sub { $_[0] * 2 },
1637
    }
1638
);
1639
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
1640
$result = $dbi->select(table => 'table1', type_rule_off => 1);
1641
is($result->type_rule_off->fetch->[0], 2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1642

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1643
$dbi = DBIx::Custom->connect;
1644
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1645
$dbi->type_rule(
1646
    from1 => {
1647
        date => sub { $_[0] * 2 },
1648
    },
1649
    into1 => {
1650
        date => sub { $_[0] * 3 },
1651
    }
1652
);
1653
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
1654
$result = $dbi->select(table => 'table1', type_rule_off => 1);
1655
is($result->one->{key1}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-06
1656

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1657
$dbi = DBIx::Custom->connect;
1658
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1659
$dbi->type_rule(
1660
    from1 => {
1661
        date => sub { $_[0] * 2 },
1662
    },
1663
    into1 => {
1664
        date => sub { $_[0] * 3 },
1665
    }
1666
);
1667
$dbi->insert({key1 => 2}, table => 'table1');
1668
$result = $dbi->select(table => 'table1');
1669
is($result->one->{key1}, 12);
cleanup test
Yuki Kimoto authored on 2011-08-06
1670

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1671
$dbi = DBIx::Custom->connect;
1672
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1673
$dbi->type_rule(
1674
    from1 => {
1675
        date => sub { $_[0] * 2 },
1676
    },
1677
    into1 => {
1678
        date => sub { $_[0] * 3 },
1679
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
1680
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1681
$dbi->insert({key1 => 2}, table => 'table1');
1682
$result = $dbi->select(table => 'table1');
1683
is($result->fetch->[0], 12);
cleanup test
Yuki Kimoto authored on 2011-08-06
1684

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1685
$dbi = DBIx::Custom->connect;
1686
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1687
$dbi->register_filter(ppp => sub { uc $_[0] });
1688
$dbi->type_rule(
1689
    into1 => {
1690
        date => 'ppp'
cleanup test
Yuki Kimoto authored on 2011-08-06
1691
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
1692
);
1693
$dbi->insert({key1 => 'a'}, table => 'table1');
1694
$result = $dbi->select(table => 'table1');
1695
is($result->one->{key1}, 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
1696

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1697
eval{$dbi->type_rule(
1698
    into1 => {
1699
        date => 'pp'
cleanup test
Yuki Kimoto authored on 2011-08-06
1700
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
1701
)};
1702
like($@, qr/not registered/);
cleanup test
Yuki Kimoto authored on 2011-08-06
1703

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1704
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1705
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1706
eval {
1707
    $dbi->type_rule(
1708
        from1 => {
1709
            Date => sub { $_[0] * 2 },
1710
        }
1711
    );
1712
};
1713
like($@, qr/lower/);
clenup test
Yuki Kimoto authored on 2011-08-06
1714

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1715
eval {
1716
    $dbi->type_rule(
1717
        into1 => {
1718
            Date => sub { $_[0] * 2 },
1719
        }
1720
    );
1721
};
1722
like($@, qr/lower/);
clenup test
Yuki Kimoto authored on 2011-08-06
1723

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1724
$dbi = DBIx::Custom->connect;
1725
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1726
$dbi->type_rule(
1727
    from1 => {
1728
        date => sub { $_[0] * 2 },
1729
    },
1730
    into1 => {
1731
        date => sub { $_[0] * 3 },
1732
    }
1733
);
1734
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
1735
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1736
$result->type_rule_off;
1737
is($result->one->{key1}, 6);
clenup test
Yuki Kimoto authored on 2011-08-06
1738

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1739
$dbi = DBIx::Custom->connect;
1740
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1741
$dbi->type_rule(
1742
    from1 => {
1743
        date => sub { $_[0] * 2 },
1744
        datetime => sub { $_[0] * 4 },
1745
    },
1746
);
1747
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
1748
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1749
$result->type_rule(
1750
    from1 => {
1751
        date => sub { $_[0] * 3 }
1752
    }
1753
);
1754
$row = $result->one;
1755
is($row->{key1}, 6);
1756
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
1757

            
1758
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1759
$result->type_rule(
1760
    from1 => {
1761
        date => sub { $_[0] * 3 }
1762
    }
1763
);
1764
$row = $result->one;
1765
is($row->{key1}, 6);
1766
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
1767

            
1768
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1769
$result->type_rule(
1770
    from1 => {
1771
        date => sub { $_[0] * 3 }
1772
    }
1773
);
1774
$row = $result->one;
1775
is($row->{key1}, 6);
1776
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
1777
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1778
$result->type_rule(
1779
    from1 => [date => sub { $_[0] * 3 }]
1780
);
1781
$row = $result->one;
1782
is($row->{key1}, 6);
1783
is($row->{key2}, 2);
1784
$dbi->register_filter(fivetimes => sub { $_[0] * 5});
clenup test
Yuki Kimoto authored on 2011-08-06
1785
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1786
$result->type_rule(
1787
    from1 => [date => 'fivetimes']
1788
);
1789
$row = $result->one;
1790
is($row->{key1}, 10);
1791
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
1792
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1793
$result->type_rule(
1794
    from1 => [date => undef]
1795
);
1796
$row = $result->one;
1797
is($row->{key1}, 2);
1798
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
1799

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1800
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1801
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1802
$dbi->type_rule(
1803
    from1 => {
1804
        date => sub { $_[0] * 2 },
1805
    },
1806
);
1807
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
1808
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1809
$result->filter(key1 => sub { $_[0] * 3 });
1810
is($result->one->{key1}, 12);
clenup test
Yuki Kimoto authored on 2011-08-06
1811

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1812
$dbi = DBIx::Custom->connect;
1813
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1814
$dbi->type_rule(
1815
    from1 => {
1816
        date => sub { $_[0] * 2 },
1817
    },
1818
);
1819
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
1820
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1821
$result->filter(key1 => sub { $_[0] * 3 });
1822
is($result->fetch->[0], 12);
clenup test
Yuki Kimoto authored on 2011-08-06
1823

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1824
$dbi = DBIx::Custom->connect;
1825
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1826
$dbi->type_rule(
1827
    into1 => {
1828
        date => sub { $_[0] . 'b' }
1829
    },
1830
    into2 => {
1831
        date => sub { $_[0] . 'c' }
1832
    },
1833
    from1 => {
1834
        date => sub { $_[0] . 'd' }
1835
    },
1836
    from2 => {
1837
        date => sub { $_[0] . 'e' }
1838
    }
1839
);
1840
$dbi->insert({key1 => '1'}, table => 'table1', type_rule_off => 1);
clenup test
Yuki Kimoto authored on 2011-08-06
1841
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1842
is($result->type_rule_off->fetch_first->[0], '1');
clenup test
Yuki Kimoto authored on 2011-08-06
1843
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1844
is($result->type_rule_on->fetch_first->[0], '1de');
clenup test
Yuki Kimoto authored on 2011-08-06
1845

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1846
$dbi = DBIx::Custom->connect;
1847
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1848
$dbi->type_rule(
1849
    into1 => {
1850
        date => sub { $_[0] . 'b' }
cleanup test
Yuki Kimoto authored on 2011-08-06
1851
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
1852
    into2 => {
1853
        date => sub { $_[0] . 'c' }
cleanup test
Yuki Kimoto authored on 2011-08-06
1854
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
1855
    from1 => {
1856
        date => sub { $_[0] . 'd' }
cleanup test
Yuki Kimoto authored on 2011-08-06
1857
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
1858
    from2 => {
1859
        date => sub { $_[0] . 'e' }
cleanup test
Yuki Kimoto authored on 2011-08-06
1860
    }
1861
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1862
$dbi->insert({key1 => '1'}, table => 'table1', type_rule1_off => 1);
1863
$result = $dbi->select(table => 'table1');
1864
is($result->type_rule1_off->fetch_first->[0], '1ce');
1865
$result = $dbi->select(table => 'table1');
1866
is($result->type_rule1_on->fetch_first->[0], '1cde');
cleanup test
Yuki Kimoto authored on 2011-08-06
1867

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1868
$dbi = DBIx::Custom->connect;
1869
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1870
$dbi->type_rule(
1871
    into1 => {
1872
        date => sub { $_[0] . 'b' }
1873
    },
1874
    into2 => {
1875
        date => sub { $_[0] . 'c' }
1876
    },
1877
    from1 => {
1878
        date => sub { $_[0] . 'd' }
1879
    },
1880
    from2 => {
1881
        date => sub { $_[0] . 'e' }
cleanup test
Yuki Kimoto authored on 2011-08-06
1882
    }
1883
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1884
$dbi->insert({key1 => '1'}, table => 'table1', type_rule2_off => 1);
1885
$result = $dbi->select(table => 'table1');
1886
is($result->type_rule2_off->fetch_first->[0], '1bd');
1887
$result = $dbi->select(table => 'table1');
1888
is($result->type_rule2_on->fetch_first->[0], '1bde');
test cleanup
Yuki Kimoto authored on 2011-08-10
1889

            
1890
test 'prefix';
1891
$dbi = DBIx::Custom->connect;
1892
eval { $dbi->execute('drop table table1') };
1893
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
1894
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1895
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace');
1896
$result = $dbi->execute('select * from table1;');
1897
$rows   = $result->all;
1898
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
1899

            
1900
$dbi = DBIx::Custom->connect;
1901
eval { $dbi->execute('drop table table1') };
1902
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
1903
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1904
$dbi->update(table => 'table1', param => {key2 => 4},
1905
  where => {key1 => 1}, prefix => 'or replace');
1906
$result = $dbi->execute('select * from table1;');
1907
$rows   = $result->all;
1908
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
1909

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1910
test 'Model class';
1911
use MyDBI1;
1912
$dbi = MyDBI1->connect;
1913
eval { $dbi->execute('drop table book') };
1914
$dbi->execute("create table book (title, author)");
1915
$model = $dbi->model('book');
1916
$model->insert({title => 'a', author => 'b'});
1917
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
1918
$dbi->execute("create table company (name)");
1919
$model = $dbi->model('company');
1920
$model->insert({name => 'a'});
1921
is_deeply($model->list->all, [{name => 'a'}], 'basic');
1922
is($dbi->models->{'book'}, $dbi->model('book'));
1923
is($dbi->models->{'company'}, $dbi->model('company'));
1924

            
1925
$dbi = MyDBI4->connect;
1926
eval { $dbi->execute('drop table book') };
1927
$dbi->execute("create table book (title, author)");
1928
$model = $dbi->model('book');
1929
$model->insert({title => 'a', author => 'b'});
1930
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
1931
$dbi->execute("create table company (name)");
1932
$model = $dbi->model('company');
1933
$model->insert({name => 'a'});
1934
is_deeply($model->list->all, [{name => 'a'}], 'basic');
test cleanup
Yuki Kimoto authored on 2011-08-10
1935

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1936
$dbi = MyDBI5->connect;
1937
eval { $dbi->execute('drop table company') };
1938
eval { $dbi->execute('drop table table1') };
1939
$dbi->execute("create table company (name)");
1940
$dbi->execute("create table table1 (key1)");
1941
$model = $dbi->model('company');
1942
$model->insert({name => 'a'});
1943
is_deeply($model->list->all, [{name => 'a'}], 'include all model');
1944
$dbi->insert(table => 'table1', param => {key1 => 1});
1945
$model = $dbi->model('book');
1946
is_deeply($model->list->all, [{key1 => 1}], 'include all model');
test cleanup
Yuki Kimoto authored on 2011-08-10
1947

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1948
test 'primary_key';
1949
use MyDBI1;
1950
$dbi = MyDBI1->connect;
1951
$model = $dbi->model('book');
1952
$model->primary_key(['id', 'number']);
1953
is_deeply($model->primary_key, ['id', 'number']);
test cleanup
Yuki Kimoto authored on 2011-08-10
1954

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1955
test 'columns';
1956
use MyDBI1;
1957
$dbi = MyDBI1->connect;
1958
$model = $dbi->model('book');
1959
$model->columns(['id', 'number']);
1960
is_deeply($model->columns, ['id', 'number']);
test cleanup
Yuki Kimoto authored on 2011-08-10
1961

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1962
test 'setup_model';
1963
use MyDBI1;
1964
$dbi = MyDBI1->connect;
1965
eval { $dbi->execute('drop table book') };
1966
eval { $dbi->execute('drop table company') };
1967
eval { $dbi->execute('drop table test') };
test cleanup
Yuki Kimoto authored on 2011-08-10
1968

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1969
$dbi->execute('create table book (id)');
1970
$dbi->execute('create table company (id, name);');
1971
$dbi->execute('create table test (id, name, primary key (id, name));');
1972
$dbi->setup_model;
1973
is_deeply($dbi->model('book')->columns, ['id']);
1974
is_deeply($dbi->model('company')->columns, ['id', 'name']);
test cleanup
Yuki Kimoto authored on 2011-08-10
1975

            
1976

            
1977

            
1978

            
1979

            
1980

            
1981

            
1982

            
1983

            
1984

            
1985

            
1986

            
1987

            
1988

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1989
### SQLite only test
1990
test 'quote';
1991
$dbi = DBIx::Custom->connect;
1992
$dbi->quote('"');
1993
eval { $dbi->execute("drop table ${q}table$p") };
1994
$dbi->execute($create_table_reserved);
1995
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
1996
$dbi->insert(table => 'table', param => {select => 1});
1997
$dbi->delete(table => 'table', where => {select => 1});
1998
$result = $dbi->execute("select * from ${q}table$p");
1999
$rows   = $result->all;
2000
is_deeply($rows, [], "reserved word");
2001

            
2002

            
2003

            
2004

            
2005

            
2006

            
2007

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

            
2009

            
2010

            
2011

            
2012

            
2013

            
2014

            
2015

            
2016

            
2017

            
2018
# DEPRECATED! test
2019
test 'filter __ expression';
2020
$dbi = DBIx::Custom->connect;
2021
eval { $dbi->execute('drop table company') };
2022
eval { $dbi->execute('drop table location') };
2023
$dbi->execute('create table company (id, name, location_id)');
2024
$dbi->execute('create table location (id, name)');
2025
$dbi->apply_filter('location',
2026
  name => {in => sub { uc $_[0] } }
2027
);
2028

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

            
2032
$result = $dbi->select(
2033
    table => ['company', 'location'], relation => {'company.location_id' => 'location.id'},
2034
    column => ['location.name as location__name']
2035
);
2036
is($result->fetch_first->[0], 'B');
2037

            
2038
$result = $dbi->select(
2039
    table => 'company', relation => {'company.location_id' => 'location.id'},
2040
    column => ['location.name as location__name']
2041
);
2042
is($result->fetch_first->[0], 'B');
2043

            
2044
$result = $dbi->select(
2045
    table => 'company', relation => {'company.location_id' => 'location.id'},
2046
    column => ['location.name as "location.name"']
2047
);
2048
is($result->fetch_first->[0], 'B');
test cleanup
Yuki Kimoto authored on 2011-08-10
2049

            
2050
test 'reserved_word_quote';
2051
$dbi = DBIx::Custom->connect;
2052
eval { $dbi->execute("drop table ${q}table$p") };
2053
$dbi->reserved_word_quote('"');
2054
$dbi->execute($create_table_reserved);
2055
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
2056
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
2057
$dbi->insert(table => 'table', param => {select => 1});
2058
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
2059
$result = $dbi->execute("select * from ${q}table$p");
2060
$rows   = $result->all;
2061
is_deeply($rows, [{select => 2, update => 6}], "reserved word");