DBIx-Custom / t / sqlite.t /
Newer Older
1001 lines | 27.743kb
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 varchar, key2 varchar);';
155
my $create_table1_2 = 'create table table1 (key1 varchar, key2 varchar, key3 varchar, key4 varchar, key5 varchar);';
156
my $create_table2 = 'create table table2 (key1 varchar, key3 varchar);';
157
my $create_table2_2 = "create table table2 (key1 varchar, key2 varchar, key3 varchar)";
158
my $create_table3 = "create table table3 (key1 varchar, key2 varchar, key3 varchar)";
159
my $create_table_reserved = 'create table "table" ("select" varchar, "update" varchar)';
test cleanup
Yuki Kimoto authored on 2011-08-10
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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
199
### a little complex test
cleanup test
Yuki Kimoto authored on 2011-08-10
200
test 'table_alias';
201
$dbi = DBIx::Custom->connect;
202
eval { $dbi->execute('drop table table1') };
203
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
204
$dbi->type_rule(
205
    into1 => {
206
        date => sub { uc $_[0] }
207
    }
208
);
209
$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'},
210
  table_alias => {table2 => 'table1'});
211
$result = $dbi->select(table => 'table1');
212
is($result->one->{key1}, 'A');
213

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
215
test 'join';
216
$dbi = DBIx::Custom->connect;
217
eval { $dbi->execute('drop table table1') };
218
$dbi->execute($create_table1);
219
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
220
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
221
$dbi->execute($create_table2);
222
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
223
$dbi->execute('create table table3 (key3 int, key4 int);');
224
$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4});
225
$rows = $dbi->select(
226
    table => 'table1',
227
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
228
    where   => {'table1.key2' => 2},
229
    join  => ['left outer join table2 on table1.key1 = table2.key1']
230
)->all;
231
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]);
232

            
233
$rows = $dbi->select(
234
    table => 'table1',
235
    where   => {'key1' => 1},
236
    join  => ['left outer join table2 on table1.key1 = table2.key1']
237
)->all;
238
is_deeply($rows, [{key1 => 1, key2 => 2}]);
239

            
240
eval {
241
    $rows = $dbi->select(
242
        table => 'table1',
243
        column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
244
        where   => {'table1.key2' => 2},
245
        join  => {'table1.key1' => 'table2.key1'}
246
    );
247
};
248
like ($@, qr/array/);
249

            
250
$rows = $dbi->select(
251
    table => 'table1',
252
    where   => {'key1' => 1},
253
    join  => ['left outer join table2 on table1.key1 = table2.key1',
254
              'left outer join table3 on table2.key3 = table3.key3']
255
)->all;
256
is_deeply($rows, [{key1 => 1, key2 => 2}]);
257

            
258
$rows = $dbi->select(
259
    column => 'table3.key4 as table3__key4',
260
    table => 'table1',
261
    where   => {'table1.key1' => 1},
262
    join  => ['left outer join table2 on table1.key1 = table2.key1',
263
              'left outer join table3 on table2.key3 = table3.key3']
264
)->all;
265
is_deeply($rows, [{table3__key4 => 4}]);
266

            
267
$rows = $dbi->select(
268
    column => 'table1.key1 as table1__key1',
269
    table => 'table1',
270
    where   => {'table3.key4' => 4},
271
    join  => ['left outer join table2 on table1.key1 = table2.key1',
272
              'left outer join table3 on table2.key3 = table3.key3']
273
)->all;
274
is_deeply($rows, [{table1__key1 => 1}]);
275

            
276
$dbi = DBIx::Custom->connect;
277
$dbi->quote('"');
278
eval { $dbi->execute('drop table table1') };
279
$dbi->execute($create_table1);
280
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
281
$dbi->execute($create_table2);
282
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
283
$rows = $dbi->select(
284
    table => 'table1',
285
    column => '"table1"."key1" as "table1_key1", "table2"."key1" as "table2_key1", "key2", "key3"',
286
    where   => {'table1.key2' => 2},
287
    join  => ['left outer join "table2" on "table1"."key1" = "table2"."key1"'],
288
)->all;
289
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}],
290
          'quote');
291

            
292

            
293
$dbi = DBIx::Custom->connect;
294
eval { $dbi->execute('drop table table1') };
295
$dbi->execute($create_table1);
296
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
297
$sql = <<"EOS";
298
left outer join (
299
  select * from table1 as t1
300
  where t1.key2 = (
301
    select max(t2.key2) from table1 as t2
302
    where t1.key1 = t2.key1
303
  )
304
) as latest_table1 on table1.key1 = latest_table1.key1
305
EOS
306
$join = [$sql];
307
$rows = $dbi->select(
308
    table => 'table1',
309
    column => 'latest_table1.key1 as latest_table1__key1',
310
    join  => $join
311
)->all;
312
is_deeply($rows, [{latest_table1__key1 => 1}]);
313

            
314
$dbi = DBIx::Custom->connect;
315
eval { $dbi->execute('drop table table1') };
316
eval { $dbi->execute('drop table table2') };
317
$dbi->execute($create_table1);
318
$dbi->execute($create_table2);
319
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
320
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
321
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
322
$result = $dbi->select(
323
    table => 'table1',
324
    join => [
325
        "left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1"
326
    ]
327
);
328
is_deeply($result->all, [{key1 => 1, key2 => 2}]);
329
$result = $dbi->select(
330
    table => 'table1',
331
    column => [{table2 => ['key3']}],
332
    join => [
333
        "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1"
334
    ]
335
);
336
is_deeply($result->all, [{'table2.key3' => 4}]);
337
$result = $dbi->select(
338
    table => 'table1',
339
    column => [{table2 => ['key3']}],
340
    join => [
341
        "left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'"
342
    ]
343
);
344
is_deeply($result->all, [{'table2.key3' => 4}]);
345

            
346
$dbi = DBIx::Custom->connect;
347
eval { $dbi->execute('drop table table1') };
348
eval { $dbi->execute('drop table table2') };
349
$dbi->execute($create_table1);
350
$dbi->execute($create_table2);
351
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
352
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
353
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
354
$result = $dbi->select(
355
    table => 'table1',
356
    column => [{table2 => ['key3']}],
357
    join => [
358
        {
359
            clause => "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1",
360
            table => ['table1', 'table2']
361
        }
362
    ]
363
);
364
is_deeply($result->all, [{'table2.key3' => 4}]);
365

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

            
367

            
368

            
cleanup test
Yuki Kimoto authored on 2011-08-10
369
test 'type option'; # DEPRECATED!
370
$dbi = DBIx::Custom->connect(
371
    data_source => 'dbi:SQLite:dbname=:memory:',
372
    dbi_option => {
373
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
374
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
375
);
cleanup test
Yuki Kimoto authored on 2011-08-10
376
$binary = pack("I3", 1, 2, 3);
377
eval { $dbi->execute('drop table table1') };
378
$dbi->execute('create table table1(key1, key2)');
379
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
380
$result = $dbi->select(table => 'table1');
381
$row   = $result->one;
382
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
383
$result = $dbi->execute('select length(key1) as key1_length from table1');
384
$row = $result->one;
385
is($row->{key1_length}, length $binary);
cleanup test
Yuki Kimoto authored on 2011-08-06
386

            
cleanup test
Yuki Kimoto authored on 2011-08-10
387
test 'type_rule from';
388
$dbi = DBIx::Custom->connect;
389
$dbi->type_rule(
390
    from1 => {
391
        date => sub { uc $_[0] }
392
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
393
);
cleanup test
Yuki Kimoto authored on 2011-08-10
394
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
395
$dbi->insert({key1 => 'a'}, table => 'table1');
396
$result = $dbi->select(table => 'table1');
397
is($result->fetch_first->[0], 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
398

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

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

            
403
test 'type_rule into';
test cleanup
Yuki Kimoto authored on 2011-08-10
404
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
405
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
406
$dbi->type_rule(
407
    into1 => {
408
        date => sub { uc $_[0] }
409
    }
410
);
cleanup test
Yuki Kimoto authored on 2011-08-10
411
$dbi->insert({key1 => 'a'}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
412
$result = $dbi->select(table => 'table1');
413
is($result->one->{key1}, 'A');
414

            
test cleanup
Yuki Kimoto authored on 2011-08-10
415
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
416
$dbi->execute("create table table1 (key1 date, key2 datetime)");
417
$dbi->type_rule(
418
    into1 => [
419
         [qw/date datetime/] => sub { uc $_[0] }
420
    ]
421
);
422
$dbi->insert({key1 => 'a', key2 => 'b'}, table => 'table1');
423
$result = $dbi->select(table => 'table1');
424
$row = $result->one;
425
is($row->{key1}, 'A');
426
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
427

            
test cleanup
Yuki Kimoto authored on 2011-08-10
428
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
429
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
430
$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1');
431
$dbi->type_rule(
432
    into1 => [
433
        [qw/date datetime/] => sub { uc $_[0] }
434
    ]
435
);
436
$result = $dbi->execute(
437
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
438
    param => {key1 => 'a', 'table1.key2' => 'b'}
439
);
440
$row = $result->one;
441
is($row->{key1}, 'a');
442
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
443

            
test cleanup
Yuki Kimoto authored on 2011-08-10
444
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
445
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
446
$dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1');
447
$dbi->type_rule(
448
    into1 => [
449
        [qw/date datetime/] => sub { uc $_[0] }
450
    ]
451
);
452
$result = $dbi->execute(
453
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
454
    param => {key1 => 'a', 'table1.key2' => 'b'},
455
    table => 'table1'
456
);
457
$row = $result->one;
458
is($row->{key1}, 'A');
459
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
460

            
cleanup test
Yuki Kimoto authored on 2011-08-10
461
$dbi = DBIx::Custom->connect;
462
$dbi->execute("create table table1 (key1 date, key2 datetime)");
463
$dbi->register_filter(twice => sub { $_[0] * 2 });
464
$dbi->type_rule(
465
    from1 => {
466
        date => 'twice',
467
    },
468
    into1 => {
469
        date => 'twice',
470
    }
471
);
472
$dbi->insert({key1 => 2}, table => 'table1');
473
$result = $dbi->select(table => 'table1');
474
is($result->fetch->[0], 8);
cleanup test
Yuki Kimoto authored on 2011-08-06
475

            
cleanup test
Yuki Kimoto authored on 2011-08-10
476
test 'type_rule and filter order';
test cleanup
Yuki Kimoto authored on 2011-08-10
477
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
478
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
479
$dbi->type_rule(
480
    into1 => {
481
        date => sub { $_[0] . 'b' }
482
    },
483
    into2 => {
484
        date => sub { $_[0] . 'c' }
485
    },
486
    from1 => {
487
        date => sub { $_[0] . 'd' }
488
    },
489
    from2 => {
490
        date => sub { $_[0] . 'e' }
491
    }
492
);
493
$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }});
494
$result = $dbi->select(table => 'table1');
495
$result->filter(key1 => sub { $_[0] . 'f' });
496
is($result->fetch_first->[0], '1abcdef');
cleanup test
Yuki Kimoto authored on 2011-08-06
497

            
cleanup test
Yuki Kimoto authored on 2011-08-10
498
$dbi = DBIx::Custom->connect;
499
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
500
$dbi->type_rule(
501
    from1 => {
502
        date => sub { $_[0] . 'p' }
503
    },
504
    from2 => {
505
        date => sub { $_[0] . 'q' }
506
    },
507
);
508
$dbi->insert({key1 => '1'}, table => 'table1');
509
$result = $dbi->select(table => 'table1');
510
$result->type_rule(
511
    from1 => {
512
        date => sub { $_[0] . 'd' }
513
    },
514
    from2 => {
515
        date => sub { $_[0] . 'e' }
516
    }
517
);
518
$result->filter(key1 => sub { $_[0] . 'f' });
519
is($result->fetch_first->[0], '1def');
cleanup test
Yuki Kimoto authored on 2011-08-06
520

            
cleanup test
Yuki Kimoto authored on 2011-08-10
521
test 'type_rule_off';
522
$dbi = DBIx::Custom->connect;
523
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
524
$dbi->type_rule(
525
    from1 => {
526
        date => sub { $_[0] * 2 },
527
    },
528
    into1 => {
529
        date => sub { $_[0] * 2 },
530
    }
531
);
532
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
533
$result = $dbi->select(table => 'table1', type_rule_off => 1);
534
is($result->type_rule_off->fetch->[0], 2);
cleanup test
Yuki Kimoto authored on 2011-08-06
535

            
cleanup test
Yuki Kimoto authored on 2011-08-10
536
$dbi = DBIx::Custom->connect;
537
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
538
$dbi->type_rule(
539
    from1 => {
540
        date => sub { $_[0] * 2 },
541
    },
542
    into1 => {
543
        date => sub { $_[0] * 3 },
544
    }
545
);
546
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
547
$result = $dbi->select(table => 'table1', type_rule_off => 1);
548
is($result->one->{key1}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-06
549

            
cleanup test
Yuki Kimoto authored on 2011-08-10
550
$dbi = DBIx::Custom->connect;
551
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
552
$dbi->type_rule(
553
    from1 => {
554
        date => sub { $_[0] * 2 },
555
    },
556
    into1 => {
557
        date => sub { $_[0] * 3 },
558
    }
559
);
560
$dbi->insert({key1 => 2}, table => 'table1');
561
$result = $dbi->select(table => 'table1');
562
is($result->one->{key1}, 12);
cleanup test
Yuki Kimoto authored on 2011-08-06
563

            
cleanup test
Yuki Kimoto authored on 2011-08-10
564
$dbi = DBIx::Custom->connect;
565
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
566
$dbi->type_rule(
567
    from1 => {
568
        date => sub { $_[0] * 2 },
569
    },
570
    into1 => {
571
        date => sub { $_[0] * 3 },
572
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
573
);
cleanup test
Yuki Kimoto authored on 2011-08-10
574
$dbi->insert({key1 => 2}, table => 'table1');
575
$result = $dbi->select(table => 'table1');
576
is($result->fetch->[0], 12);
cleanup test
Yuki Kimoto authored on 2011-08-06
577

            
cleanup test
Yuki Kimoto authored on 2011-08-10
578
$dbi = DBIx::Custom->connect;
579
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
580
$dbi->register_filter(ppp => sub { uc $_[0] });
581
$dbi->type_rule(
582
    into1 => {
583
        date => 'ppp'
cleanup test
Yuki Kimoto authored on 2011-08-06
584
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
585
);
586
$dbi->insert({key1 => 'a'}, table => 'table1');
587
$result = $dbi->select(table => 'table1');
588
is($result->one->{key1}, 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
589

            
cleanup test
Yuki Kimoto authored on 2011-08-10
590
eval{$dbi->type_rule(
591
    into1 => {
592
        date => 'pp'
cleanup test
Yuki Kimoto authored on 2011-08-06
593
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
594
)};
595
like($@, qr/not registered/);
cleanup test
Yuki Kimoto authored on 2011-08-06
596

            
test cleanup
Yuki Kimoto authored on 2011-08-10
597
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
598
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
599
eval {
600
    $dbi->type_rule(
601
        from1 => {
602
            Date => sub { $_[0] * 2 },
603
        }
604
    );
605
};
606
like($@, qr/lower/);
clenup test
Yuki Kimoto authored on 2011-08-06
607

            
cleanup test
Yuki Kimoto authored on 2011-08-10
608
eval {
609
    $dbi->type_rule(
610
        into1 => {
611
            Date => sub { $_[0] * 2 },
612
        }
613
    );
614
};
615
like($@, qr/lower/);
clenup test
Yuki Kimoto authored on 2011-08-06
616

            
cleanup test
Yuki Kimoto authored on 2011-08-10
617
$dbi = DBIx::Custom->connect;
618
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
619
$dbi->type_rule(
620
    from1 => {
621
        date => sub { $_[0] * 2 },
622
    },
623
    into1 => {
624
        date => sub { $_[0] * 3 },
625
    }
626
);
627
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
628
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
629
$result->type_rule_off;
630
is($result->one->{key1}, 6);
clenup test
Yuki Kimoto authored on 2011-08-06
631

            
cleanup test
Yuki Kimoto authored on 2011-08-10
632
$dbi = DBIx::Custom->connect;
633
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
634
$dbi->type_rule(
635
    from1 => {
636
        date => sub { $_[0] * 2 },
637
        datetime => sub { $_[0] * 4 },
638
    },
639
);
640
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
641
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
642
$result->type_rule(
643
    from1 => {
644
        date => sub { $_[0] * 3 }
645
    }
646
);
647
$row = $result->one;
648
is($row->{key1}, 6);
649
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
650

            
651
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
652
$result->type_rule(
653
    from1 => {
654
        date => sub { $_[0] * 3 }
655
    }
656
);
657
$row = $result->one;
658
is($row->{key1}, 6);
659
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
660

            
661
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
662
$result->type_rule(
663
    from1 => {
664
        date => sub { $_[0] * 3 }
665
    }
666
);
667
$row = $result->one;
668
is($row->{key1}, 6);
669
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
670
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
671
$result->type_rule(
672
    from1 => [date => sub { $_[0] * 3 }]
673
);
674
$row = $result->one;
675
is($row->{key1}, 6);
676
is($row->{key2}, 2);
677
$dbi->register_filter(fivetimes => sub { $_[0] * 5});
clenup test
Yuki Kimoto authored on 2011-08-06
678
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
679
$result->type_rule(
680
    from1 => [date => 'fivetimes']
681
);
682
$row = $result->one;
683
is($row->{key1}, 10);
684
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
685
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
686
$result->type_rule(
687
    from1 => [date => undef]
688
);
689
$row = $result->one;
690
is($row->{key1}, 2);
691
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
692

            
test cleanup
Yuki Kimoto authored on 2011-08-10
693
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
694
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
695
$dbi->type_rule(
696
    from1 => {
697
        date => sub { $_[0] * 2 },
698
    },
699
);
700
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
701
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
702
$result->filter(key1 => sub { $_[0] * 3 });
703
is($result->one->{key1}, 12);
clenup test
Yuki Kimoto authored on 2011-08-06
704

            
cleanup test
Yuki Kimoto authored on 2011-08-10
705
$dbi = DBIx::Custom->connect;
706
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
707
$dbi->type_rule(
708
    from1 => {
709
        date => sub { $_[0] * 2 },
710
    },
711
);
712
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
713
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
714
$result->filter(key1 => sub { $_[0] * 3 });
715
is($result->fetch->[0], 12);
clenup test
Yuki Kimoto authored on 2011-08-06
716

            
cleanup test
Yuki Kimoto authored on 2011-08-10
717
$dbi = DBIx::Custom->connect;
718
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
719
$dbi->type_rule(
720
    into1 => {
721
        date => sub { $_[0] . 'b' }
722
    },
723
    into2 => {
724
        date => sub { $_[0] . 'c' }
725
    },
726
    from1 => {
727
        date => sub { $_[0] . 'd' }
728
    },
729
    from2 => {
730
        date => sub { $_[0] . 'e' }
731
    }
732
);
733
$dbi->insert({key1 => '1'}, table => 'table1', type_rule_off => 1);
clenup test
Yuki Kimoto authored on 2011-08-06
734
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
735
is($result->type_rule_off->fetch_first->[0], '1');
clenup test
Yuki Kimoto authored on 2011-08-06
736
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
737
is($result->type_rule_on->fetch_first->[0], '1de');
clenup test
Yuki Kimoto authored on 2011-08-06
738

            
cleanup test
Yuki Kimoto authored on 2011-08-10
739
$dbi = DBIx::Custom->connect;
740
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
741
$dbi->type_rule(
742
    into1 => {
743
        date => sub { $_[0] . 'b' }
cleanup test
Yuki Kimoto authored on 2011-08-06
744
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
745
    into2 => {
746
        date => sub { $_[0] . 'c' }
cleanup test
Yuki Kimoto authored on 2011-08-06
747
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
748
    from1 => {
749
        date => sub { $_[0] . 'd' }
cleanup test
Yuki Kimoto authored on 2011-08-06
750
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
751
    from2 => {
752
        date => sub { $_[0] . 'e' }
cleanup test
Yuki Kimoto authored on 2011-08-06
753
    }
754
);
cleanup test
Yuki Kimoto authored on 2011-08-10
755
$dbi->insert({key1 => '1'}, table => 'table1', type_rule1_off => 1);
756
$result = $dbi->select(table => 'table1');
757
is($result->type_rule1_off->fetch_first->[0], '1ce');
758
$result = $dbi->select(table => 'table1');
759
is($result->type_rule1_on->fetch_first->[0], '1cde');
cleanup test
Yuki Kimoto authored on 2011-08-06
760

            
cleanup test
Yuki Kimoto authored on 2011-08-10
761
$dbi = DBIx::Custom->connect;
762
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
763
$dbi->type_rule(
764
    into1 => {
765
        date => sub { $_[0] . 'b' }
766
    },
767
    into2 => {
768
        date => sub { $_[0] . 'c' }
769
    },
770
    from1 => {
771
        date => sub { $_[0] . 'd' }
772
    },
773
    from2 => {
774
        date => sub { $_[0] . 'e' }
cleanup test
Yuki Kimoto authored on 2011-08-06
775
    }
776
);
cleanup test
Yuki Kimoto authored on 2011-08-10
777
$dbi->insert({key1 => '1'}, table => 'table1', type_rule2_off => 1);
778
$result = $dbi->select(table => 'table1');
779
is($result->type_rule2_off->fetch_first->[0], '1bd');
780
$result = $dbi->select(table => 'table1');
781
is($result->type_rule2_on->fetch_first->[0], '1bde');
test cleanup
Yuki Kimoto authored on 2011-08-10
782

            
783
test 'prefix';
784
$dbi = DBIx::Custom->connect;
785
eval { $dbi->execute('drop table table1') };
786
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
787
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
788
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace');
789
$result = $dbi->execute('select * from table1;');
790
$rows   = $result->all;
791
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
792

            
793
$dbi = DBIx::Custom->connect;
794
eval { $dbi->execute('drop table table1') };
795
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
796
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
797
$dbi->update(table => 'table1', param => {key2 => 4},
798
  where => {key1 => 1}, prefix => 'or replace');
799
$result = $dbi->execute('select * from table1;');
800
$rows   = $result->all;
801
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
802

            
test cleanup
Yuki Kimoto authored on 2011-08-10
803
test 'Model class';
804
use MyDBI1;
805
$dbi = MyDBI1->connect;
806
eval { $dbi->execute('drop table book') };
807
$dbi->execute("create table book (title, author)");
808
$model = $dbi->model('book');
809
$model->insert({title => 'a', author => 'b'});
810
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
811
$dbi->execute("create table company (name)");
812
$model = $dbi->model('company');
813
$model->insert({name => 'a'});
814
is_deeply($model->list->all, [{name => 'a'}], 'basic');
815
is($dbi->models->{'book'}, $dbi->model('book'));
816
is($dbi->models->{'company'}, $dbi->model('company'));
817

            
818
$dbi = MyDBI4->connect;
819
eval { $dbi->execute('drop table book') };
820
$dbi->execute("create table book (title, author)");
821
$model = $dbi->model('book');
822
$model->insert({title => 'a', author => 'b'});
823
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
824
$dbi->execute("create table company (name)");
825
$model = $dbi->model('company');
826
$model->insert({name => 'a'});
827
is_deeply($model->list->all, [{name => 'a'}], 'basic');
test cleanup
Yuki Kimoto authored on 2011-08-10
828

            
test cleanup
Yuki Kimoto authored on 2011-08-10
829
$dbi = MyDBI5->connect;
830
eval { $dbi->execute('drop table company') };
831
eval { $dbi->execute('drop table table1') };
832
$dbi->execute("create table company (name)");
833
$dbi->execute("create table table1 (key1)");
834
$model = $dbi->model('company');
835
$model->insert({name => 'a'});
836
is_deeply($model->list->all, [{name => 'a'}], 'include all model');
837
$dbi->insert(table => 'table1', param => {key1 => 1});
838
$model = $dbi->model('book');
839
is_deeply($model->list->all, [{key1 => 1}], 'include all model');
test cleanup
Yuki Kimoto authored on 2011-08-10
840

            
test cleanup
Yuki Kimoto authored on 2011-08-10
841
test 'primary_key';
842
use MyDBI1;
843
$dbi = MyDBI1->connect;
844
$model = $dbi->model('book');
845
$model->primary_key(['id', 'number']);
846
is_deeply($model->primary_key, ['id', 'number']);
test cleanup
Yuki Kimoto authored on 2011-08-10
847

            
test cleanup
Yuki Kimoto authored on 2011-08-10
848
test 'columns';
849
use MyDBI1;
850
$dbi = MyDBI1->connect;
851
$model = $dbi->model('book');
852
$model->columns(['id', 'number']);
853
is_deeply($model->columns, ['id', 'number']);
test cleanup
Yuki Kimoto authored on 2011-08-10
854

            
test cleanup
Yuki Kimoto authored on 2011-08-10
855
test 'setup_model';
856
use MyDBI1;
857
$dbi = MyDBI1->connect;
858
eval { $dbi->execute('drop table book') };
859
eval { $dbi->execute('drop table company') };
860
eval { $dbi->execute('drop table test') };
test cleanup
Yuki Kimoto authored on 2011-08-10
861

            
test cleanup
Yuki Kimoto authored on 2011-08-10
862
$dbi->execute('create table book (id)');
863
$dbi->execute('create table company (id, name);');
864
$dbi->execute('create table test (id, name, primary key (id, name));');
865
$dbi->setup_model;
866
is_deeply($dbi->model('book')->columns, ['id']);
867
is_deeply($dbi->model('company')->columns, ['id', 'name']);
test cleanup
Yuki Kimoto authored on 2011-08-10
868

            
869

            
870

            
871

            
872

            
873

            
874

            
875

            
876

            
877

            
878

            
879

            
880

            
881

            
test cleanup
Yuki Kimoto authored on 2011-08-10
882
### SQLite only test
883
test 'quote';
884
$dbi = DBIx::Custom->connect;
885
$dbi->quote('"');
886
eval { $dbi->execute("drop table ${q}table$p") };
887
$dbi->execute($create_table_reserved);
888
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
889
$dbi->insert(table => 'table', param => {select => 1});
890
$dbi->delete(table => 'table', where => {select => 1});
891
$result = $dbi->execute("select * from ${q}table$p");
892
$rows   = $result->all;
893
is_deeply($rows, [], "reserved word");
894

            
test cleanup
Yuki Kimoto authored on 2011-08-10
895
test 'finish statement handle';
896
$dbi = DBIx::Custom->connect;
897
$dbi->execute($create_table1);
898
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
899
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
900

            
901
$result = $dbi->select(table => 'table1');
902
$row = $result->fetch_first;
903
is_deeply($row, [1, 2], "row");
904
$row = $result->fetch;
905
ok(!$row, "finished");
906

            
907
$result = $dbi->select(table => 'table1');
908
$row = $result->fetch_hash_first;
909
is_deeply($row, {key1 => 1, key2 => 2}, "row");
910
$row = $result->fetch_hash;
911
ok(!$row, "finished");
912

            
913
$dbi->execute('create table table2 (key1, key2);');
914
$result = $dbi->select(table => 'table2');
915
$row = $result->fetch_hash_first;
916
ok(!$row, "no row fetch");
917

            
918
$dbi = DBIx::Custom->connect;
919
eval { $dbi->execute('drop table table1') };
920
$dbi->execute($create_table1);
921
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
922
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
923
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1');
924
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1');
925
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1');
926
$result = $dbi->select(table => 'table1');
927
$rows = $result->fetch_multi(2);
928
is_deeply($rows, [[1, 2],
929
                  [3, 4]], "fetch_multi first");
930
$rows = $result->fetch_multi(2);
931
is_deeply($rows, [[5, 6],
932
                  [7, 8]], "fetch_multi secound");
933
$rows = $result->fetch_multi(2);
934
is_deeply($rows, [[9, 10]], "fetch_multi third");
935
$rows = $result->fetch_multi(2);
936
ok(!$rows);
937

            
938
$result = $dbi->select(table => 'table1');
939
eval {$result->fetch_multi};
940
like($@, qr/Row count must be specified/, "Not specified row count");
941

            
942
$result = $dbi->select(table => 'table1');
943
$rows = $result->fetch_hash_multi(2);
944
is_deeply($rows, [{key1 => 1, key2 => 2},
945
                  {key1 => 3, key2 => 4}], "fetch_multi first");
946
$rows = $result->fetch_hash_multi(2);
947
is_deeply($rows, [{key1 => 5, key2 => 6},
948
                  {key1 => 7, key2 => 8}], "fetch_multi secound");
949
$rows = $result->fetch_hash_multi(2);
950
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third");
951
$rows = $result->fetch_hash_multi(2);
952
ok(!$rows);
953

            
954
$result = $dbi->select(table => 'table1');
955
eval {$result->fetch_hash_multi};
956
like($@, qr/Row count must be specified/, "Not specified row count");
957

            
test cleanup
Yuki Kimoto authored on 2011-08-10
958
# DEPRECATED! test
959
test 'filter __ expression';
960
$dbi = DBIx::Custom->connect;
961
eval { $dbi->execute('drop table company') };
962
eval { $dbi->execute('drop table location') };
963
$dbi->execute('create table company (id, name, location_id)');
964
$dbi->execute('create table location (id, name)');
965
$dbi->apply_filter('location',
966
  name => {in => sub { uc $_[0] } }
967
);
968

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

            
972
$result = $dbi->select(
973
    table => ['company', 'location'], relation => {'company.location_id' => 'location.id'},
974
    column => ['location.name as location__name']
975
);
976
is($result->fetch_first->[0], 'B');
977

            
978
$result = $dbi->select(
979
    table => 'company', relation => {'company.location_id' => 'location.id'},
980
    column => ['location.name as location__name']
981
);
982
is($result->fetch_first->[0], 'B');
983

            
984
$result = $dbi->select(
985
    table => 'company', relation => {'company.location_id' => 'location.id'},
986
    column => ['location.name as "location.name"']
987
);
988
is($result->fetch_first->[0], 'B');
test cleanup
Yuki Kimoto authored on 2011-08-10
989

            
990
test 'reserved_word_quote';
991
$dbi = DBIx::Custom->connect;
992
eval { $dbi->execute("drop table ${q}table$p") };
993
$dbi->reserved_word_quote('"');
994
$dbi->execute($create_table_reserved);
995
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
996
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
997
$dbi->insert(table => 'table', param => {select => 1});
998
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
999
$result = $dbi->execute("select * from ${q}table$p");
1000
$rows   = $result->all;
1001
is_deeply($rows, [{select => 2, update => 6}], "reserved word");