DBIx-Custom / t / common.t /
Newer Older
3818 lines | 123.262kb
added common test executing ...
Yuki Kimoto authored on 2011-08-07
1
use Test::More;
2
use strict;
3
use warnings;
test cleanup
Yuki Kimoto authored on 2011-08-10
4
use Encode qw/encode_utf8/;
cleanup test
Yuki Kimoto authored on 2011-08-10
5
use FindBin;
6
use lib "$FindBin::Bin/common";
cleanup
Yuki Kimoto authored on 2011-08-13
7
use Scalar::Util 'isweak';
cleanup test
Yuki Kimoto authored on 2011-08-10
8

            
added common test executing ...
Yuki Kimoto authored on 2011-08-07
9
my $dbi;
10

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

            
15
plan 'no_plan';
16

            
test cleanup
Yuki Kimoto authored on 2011-08-10
17
use MyDBI1;
18
{
19
    package MyDBI4;
20

            
21
    use strict;
22
    use warnings;
23

            
24
    use base 'DBIx::Custom';
25

            
26
    sub connect {
27
        my $self = shift->SUPER::connect(@_);
28
        
29
        $self->include_model(
30
            MyModel2 => [
test cleanup
Yuki Kimoto authored on 2011-08-10
31
                'table1',
cleanup
Yuki Kimoto authored on 2011-08-10
32
                {class => 'table2', name => 'table2'}
test cleanup
Yuki Kimoto authored on 2011-08-10
33
            ]
34
        );
35
    }
36

            
37
    package MyModel2::Base1;
38

            
39
    use strict;
40
    use warnings;
41

            
42
    use base 'DBIx::Custom::Model';
43

            
test cleanup
Yuki Kimoto authored on 2011-08-10
44
    package MyModel2::table1;
test cleanup
Yuki Kimoto authored on 2011-08-10
45

            
46
    use strict;
47
    use warnings;
48

            
49
    use base 'MyModel2::Base1';
50

            
51
    sub insert {
52
        my ($self, $param) = @_;
53
        
54
        return $self->SUPER::insert(param => $param);
55
    }
56

            
57
    sub list { shift->select; }
58

            
test cleanup
Yuki Kimoto authored on 2011-08-10
59
    package MyModel2::table2;
test cleanup
Yuki Kimoto authored on 2011-08-10
60

            
61
    use strict;
62
    use warnings;
63

            
64
    use base 'MyModel2::Base1';
65

            
66
    sub insert {
67
        my ($self, $param) = @_;
68
        
69
        return $self->SUPER::insert(param => $param);
70
    }
71

            
72
    sub list { shift->select; }
73
}
74
{
75
     package MyDBI5;
76

            
77
    use strict;
78
    use warnings;
79

            
80
    use base 'DBIx::Custom';
81

            
82
    sub connect {
83
        my $self = shift->SUPER::connect(@_);
84
        
85
        $self->include_model('MyModel4');
86
    }
87
}
88
{
89
    package MyDBI6;
90
    
91
    use base 'DBIx::Custom';
92
    
93
    sub connect {
94
        my $self = shift->SUPER::connect(@_);
95
        
96
        $self->include_model('MyModel5');
97
        
98
        return $self;
99
    }
100
}
101
{
102
    package MyDBI7;
103
    
104
    use base 'DBIx::Custom';
105
    
106
    sub connect {
107
        my $self = shift->SUPER::connect(@_);
108
        
109
        $self->include_model('MyModel6');
110
        
111
        
112
        return $self;
113
    }
114
}
115
{
116
    package MyDBI8;
117
    
118
    use base 'DBIx::Custom';
119
    
120
    sub connect {
121
        my $self = shift->SUPER::connect(@_);
122
        
123
        $self->include_model('MyModel7');
124
        
125
        return $self;
126
    }
127
}
128

            
129
{
130
    package MyDBI9;
131
    
132
    use base 'DBIx::Custom';
133
    
134
    sub connect {
135
        my $self = shift->SUPER::connect(@_);
136
        
cleanup test
Yuki Kimoto authored on 2011-08-10
137
        $self->include_model('MyModel8');
test cleanup
Yuki Kimoto authored on 2011-08-10
138
        
139
        return $self;
140
    }
141
}
142

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

            
added common test executing ...
Yuki Kimoto authored on 2011-08-07
146
# Constant
147
my $create_table1 = $dbi->create_table1;
cleanup test
Yuki Kimoto authored on 2011-08-08
148
my $create_table1_2 = $dbi->create_table1_2;
test cleanup
Yuki Kimoto authored on 2011-08-10
149
my $create_table1_type = $dbi->create_table1_type;
test cleanup
Yuki Kimoto authored on 2011-08-10
150
my $create_table1_highperformance = $dbi->create_table1_highperformance;
test cleanup
Yuki Kimoto authored on 2011-08-10
151
my $create_table2 = $dbi->create_table2;
test cleanup
Yuki Kimoto authored on 2011-08-10
152
my $create_table2_2 = $dbi->create_table2_2;
153
my $create_table3 = $dbi->create_table3;
test cleanup
Yuki Kimoto authored on 2011-08-10
154
my $create_table_reserved = $dbi->create_table_reserved;
cleanup test
Yuki Kimoto authored on 2011-08-10
155
my $q = substr($dbi->quote, 0, 1);
156
my $p = substr($dbi->quote, 1, 1) || $q;
test cleanup
Yuki Kimoto authored on 2011-08-10
157
my $date_typename = $dbi->date_typename;
158
my $datetime_typename = $dbi->datetime_typename;
159
my $date_datatype = $dbi->date_datatype;
160
my $datetime_datatype = $dbi->datetime_datatype;
added common test executing ...
Yuki Kimoto authored on 2011-08-07
161

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

            
added SQL Server test
Yuki Kimoto authored on 2011-08-14
194

            
added common test executing ...
Yuki Kimoto authored on 2011-08-07
195
# Drop table
196
eval { $dbi->execute('drop table table1') };
test cleanup
Yuki Kimoto authored on 2011-08-10
197

            
198
test 'type_rule into';
199
$dbi = DBIx::Custom->connect;
200
eval { $dbi->execute('drop table table1') };
201
$dbi->execute($create_table1_type);
added SQL Server test
Yuki Kimoto authored on 2011-08-14
202
$DB::single = 1;
test cleanup
Yuki Kimoto authored on 2011-08-10
203
$dbi->type_rule(
204
    into1 => {
205
        $date_typename => sub { '2010-' . $_[0] }
206
    }
207
);
208
$dbi->insert({key1 => '01-01'}, table => 'table1');
209
$result = $dbi->select(table => 'table1');
210
is($result->one->{key1}, '2010-01-01');
211

            
cleanup
Yuki Kimoto authored on 2011-08-13
212
$DB::single = 1;
test cleanup
Yuki Kimoto authored on 2011-08-10
213
$dbi = DBIx::Custom->connect;
214
eval { $dbi->execute('drop table table1') };
215
$dbi->execute($create_table1_type);
216
$dbi->type_rule(
217
    into1 => [
218
         [$date_typename, $datetime_typename] => sub {
219
            my $value = shift;
220
            $value =~ s/02/03/g;
221
            return $value;
222
         }
223
    ]
224
);
225
$dbi->insert({key1 => '2010-01-02', key2 => '2010-01-01 01:01:02'}, table => 'table1');
226
$result = $dbi->select(table => 'table1');
227
$row = $result->one;
228
is($row->{key1}, '2010-01-03');
229
is($row->{key2}, '2010-01-01 01:01:03');
230

            
231
$dbi = DBIx::Custom->connect;
232
eval { $dbi->execute('drop table table1') };
233
$dbi->execute($create_table1_type);
234
$dbi->insert({key1 => '2010-01-03', key2 => '2010-01-01 01:01:03'}, table => 'table1');
235
$dbi->type_rule(
236
    into1 => [
237
        [$date_typename, $datetime_typename] => sub {
238
            my $value = shift;
239
            $value =~ s/02/03/g;
240
            return $value;
241
        }
242
    ]
243
);
244
$result = $dbi->execute(
245
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
246
    param => {key1 => '2010-01-03', 'table1.key2' => '2010-01-01 01:01:02'}
247
);
248
$row = $result->one;
249
is($row->{key1}, '2010-01-03');
250
is($row->{key2}, '2010-01-01 01:01:03');
251

            
252
$dbi = DBIx::Custom->connect;
253
eval { $dbi->execute('drop table table1') };
254
$dbi->execute($create_table1_type);
255
$dbi->insert({key1 => '2010-01-03', key2 => '2010-01-01 01:01:03'}, table => 'table1');
256
$dbi->type_rule(
257
    into1 => [
258
        [$date_typename, $datetime_typename] => sub {
259
            my $value = shift;
260
            $value =~ s/02/03/g;
261
            return $value;
262
        }
263
    ]
264
);
265
$result = $dbi->execute(
266
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
267
    param => {key1 => '2010-01-02', 'table1.key2' => '2010-01-01 01:01:02'},
268
    table => 'table1'
269
);
270
$row = $result->one;
271
is($row->{key1}, '2010-01-03');
272
is($row->{key2}, '2010-01-01 01:01:03');
273

            
274
$dbi = DBIx::Custom->connect;
275
eval { $dbi->execute('drop table table1') };
276
$dbi->execute($create_table1_type);
277
$dbi->register_filter(convert => sub {
278
    my $value = shift;
279
    $value =~ s/02/03/;
280
    return $value;
281
});
282
$dbi->type_rule(
283
    from1 => {
284
        $date_datatype => 'convert',
285
    },
286
    into1 => {
287
        $date_typename => 'convert',
288
    }
289
);
290
$dbi->insert({key1 => '2010-02-02'}, table => 'table1');
291
$result = $dbi->select(table => 'table1');
292
is($result->fetch->[0], '2010-03-03');
293

            
294

            
295

            
296

            
297

            
298

            
299

            
300

            
301

            
302

            
test cleanup
Yuki Kimoto authored on 2011-08-10
303
test 'type_rule and filter order';
304
$dbi = DBIx::Custom->connect;
305
eval { $dbi->execute('drop table table1') };
306
$dbi->execute($create_table1_type);
307
$dbi->type_rule(
308
    into1 => {
309
        $date_typename => sub { my $v = shift; $v =~ s/4/5/; return $v }
310
    },
311
    into2 => {
312
        $date_typename => sub { my $v = shift; $v =~ s/5/6/; return $v }
313
    },
314
    from1 => {
315
        $date_datatype => sub { my $v = shift; $v =~ s/6/7/; return $v }
316
    },
317
    from2 => {
318
        $date_datatype => sub { my $v = shift; $v =~ s/7/8/; return $v }
319
    }
320
);
321
$dbi->insert({key1 => '2010-01-03'}, 
322
  table => 'table1', filter => {key1 => sub { my $v = shift; $v =~ s/3/4/; return $v }});
323
$result = $dbi->select(table => 'table1');
324
$result->filter(key1 => sub { my $v = shift; $v =~ s/8/9/; return $v });
325
is($result->fetch_first->[0], '2010-01-09');
test cleanup
Yuki Kimoto authored on 2011-08-10
326

            
327

            
test cleanup
Yuki Kimoto authored on 2011-08-10
328
$dbi = DBIx::Custom->connect;
329
eval { $dbi->execute('drop table table1') };
330
$dbi->execute($create_table1_type);
331
$dbi->type_rule(
332
    from1 => {
333
        $date_datatype => sub { my $v = shift; $v =~ s/3/4/; return $v }
334
    },
335
    from2 => {
336
        $date_datatype => sub { my $v = shift; $v =~ s/4/5/; return $v }
337
    },
338
);
339
$dbi->insert({key1 => '2010-01-03'}, table => 'table1');
340
$result = $dbi->select(table => 'table1');
341
$result->type_rule(
342
    from1 => {
343
        $date_datatype => sub { my $v = shift; $v =~ s/3/6/; return $v }
344
    },
345
    from2 => {
346
        $date_datatype => sub { my $v = shift; $v =~ s/6/8/; return $v }
347
    }
348
);
349
$result->filter(key1 => sub { my $v = shift; $v =~ s/8/9/; return $v });
350
is($result->fetch_first->[0], '2010-01-09');
test cleanup
Yuki Kimoto authored on 2011-08-10
351

            
test cleanup
Yuki Kimoto authored on 2011-08-10
352
test 'type_rule_off';
353
$dbi = DBIx::Custom->connect;
354
eval { $dbi->execute('drop table table1') };
355
$dbi->execute($create_table1_type);
356
$dbi->type_rule(
357
    from1 => {
358
        $date_datatype => sub { my $v = shift; $v =~ s/3/5/; return $v }
359
    },
360
    into1 => {
361
        $date_typename => sub { my $v = shift; $v =~ s/3/4/; return $v }
362
    }
363
);
364
$dbi->insert({key1 => '2010-01-03'}, table => 'table1', type_rule_off => 1);
365
$result = $dbi->select(table => 'table1', type_rule_off => 1);
366
is($result->type_rule_off->fetch->[0], '2010-01-03');
test cleanup
Yuki Kimoto authored on 2011-08-10
367

            
test cleanup
Yuki Kimoto authored on 2011-08-10
368
$dbi = DBIx::Custom->connect;
369
eval { $dbi->execute('drop table table1') };
370
$dbi->execute($create_table1_type);
371
$dbi->type_rule(
372
    from1 => {
373
        $date_datatype => sub { my $v = shift; $v =~ s/3/4/; return $v }
374
    },
375
    into1 => {
376
        $date_typename => sub { my $v = shift; $v =~ s/3/5/; return $v }
377
    }
378
);
379
$dbi->insert({key1 => '2010-01-03'}, table => 'table1', type_rule_off => 1);
380
$result = $dbi->select(table => 'table1', type_rule_off => 1);
381
is($result->one->{key1}, '2010-01-04');
test cleanup
Yuki Kimoto authored on 2011-08-10
382

            
test cleanup
Yuki Kimoto authored on 2011-08-10
383
$dbi = DBIx::Custom->connect;
384
eval { $dbi->execute('drop table table1') };
385
$dbi->execute($create_table1_type);
386
$dbi->type_rule(
387
    from1 => {
388
        $date_datatype => sub { my $v = shift; $v =~ s/4/5/; return $v }
389
    },
390
    into1 => {
391
        $date_typename => sub { my $v = shift; $v =~ s/3/4/; return $v }
392
    }
393
);
394
$dbi->insert({key1 => '2010-01-03'}, table => 'table1');
395
$result = $dbi->select(table => 'table1');
396
is($result->one->{key1}, '2010-01-05');
test cleanup
Yuki Kimoto authored on 2011-08-10
397

            
test cleanup
Yuki Kimoto authored on 2011-08-10
398
$dbi = DBIx::Custom->connect;
399
eval { $dbi->execute('drop table table1') };
400
$dbi->execute($create_table1_type);
401
$dbi->type_rule(
402
    from1 => {
403
        $date_datatype => sub { my $v = shift; $v =~ s/4/5/; return $v }
404
    },
405
    into1 => {
406
        $date_typename => sub { my $v = shift; $v =~ s/3/4/; return $v }
407
    }
408
);
409
$dbi->insert({key1 => '2010-01-03'}, table => 'table1');
410
$result = $dbi->select(table => 'table1');
411
is($result->fetch->[0], '2010-01-05');
test cleanup
Yuki Kimoto authored on 2011-08-10
412

            
test cleanup
Yuki Kimoto authored on 2011-08-10
413
$dbi = DBIx::Custom->connect;
414
eval { $dbi->execute('drop table table1') };
415
$dbi->execute($create_table1_type);
416
$dbi->register_filter(ppp => sub { my $v = shift; $v =~ s/3/4/; return $v });
417
$dbi->type_rule(
418
    into1 => {
419
        $date_typename => 'ppp'
420
    }
421
);
422
$dbi->insert({key1 => '2010-01-03'}, table => 'table1');
423
$result = $dbi->select(table => 'table1');
424
is($result->one->{key1}, '2010-01-04');
test cleanup
Yuki Kimoto authored on 2011-08-10
425

            
test cleanup
Yuki Kimoto authored on 2011-08-10
426
eval{$dbi->type_rule(
427
    into1 => {
428
        $date_typename => 'pp'
429
    }
430
)};
431
like($@, qr/not registered/);
test cleanup
Yuki Kimoto authored on 2011-08-10
432

            
test cleanup
Yuki Kimoto authored on 2011-08-10
433
$dbi = DBIx::Custom->connect;
434
eval { $dbi->execute('drop table table1') };
435
$dbi->execute($create_table1_type);
436
eval {
437
    $dbi->type_rule(
438
        from1 => {
439
            Date => sub { $_[0] * 2 },
440
        }
441
    );
442
};
443
like($@, qr/lower/);
test cleanup
Yuki Kimoto authored on 2011-08-10
444

            
test cleanup
Yuki Kimoto authored on 2011-08-10
445
eval {
446
    $dbi->type_rule(
447
        into1 => {
448
            Date => sub { $_[0] * 2 },
449
        }
450
    );
451
};
452
like($@, qr/lower/);
test cleanup
Yuki Kimoto authored on 2011-08-10
453

            
test cleanup
Yuki Kimoto authored on 2011-08-10
454
$dbi = DBIx::Custom->connect;
455
eval { $dbi->execute('drop table table1') };
456
$dbi->execute($create_table1_type);
457
$dbi->type_rule(
458
    from1 => {
459
        $date_datatype => sub { my $v = shift; $v =~ s/4/5/; return $v }
460
    },
461
    into1 => {
462
        $date_typename => sub { my $v = shift; $v =~ s/3/4/; return $v }
463
    }
464
);
465
$dbi->insert({key1 => '2010-01-03'}, table => 'table1');
466
$result = $dbi->select(table => 'table1');
467
$result->type_rule_off;
468
is($result->one->{key1}, '2010-01-04');
test cleanup
Yuki Kimoto authored on 2011-08-10
469

            
test cleanup
Yuki Kimoto authored on 2011-08-10
470
$dbi = DBIx::Custom->connect;
471
eval { $dbi->execute('drop table table1') };
472
$dbi->execute($create_table1_type);
473
$dbi->type_rule(
474
    from1 => {
475
        $date_datatype => sub { my $v = shift; $v =~ s/3/4/; return $v },
476
        $datetime_datatype => sub { my $v = shift; $v =~ s/3/4/; return $v }
477
    },
478
);
479
$dbi->insert({key1 => '2010-01-03', key2 => '2010-01-01 01:01:03'}, table => 'table1');
480
$result = $dbi->select(table => 'table1');
481
$result->type_rule(
482
    from1 => {
483
        $date_datatype => sub { my $v = shift; $v =~ s/3/5/; return $v }
484
    }
485
);
486
$row = $result->one;
487
is($row->{key1}, '2010-01-05');
488
is($row->{key2}, '2010-01-01 01:01:03');
test cleanup
Yuki Kimoto authored on 2011-08-10
489

            
test cleanup
Yuki Kimoto authored on 2011-08-10
490
$result = $dbi->select(table => 'table1');
491
$result->type_rule(
492
    from1 => {
493
        $date_datatype => sub { my $v = shift; $v =~ s/3/5/; return $v }
494
    }
495
);
496
$row = $result->one;
497
is($row->{key1}, '2010-01-05');
498
is($row->{key2}, '2010-01-01 01:01:03');
test cleanup
Yuki Kimoto authored on 2011-08-10
499

            
test cleanup
Yuki Kimoto authored on 2011-08-10
500
$result = $dbi->select(table => 'table1');
501
$result->type_rule(
502
    from1 => {
503
        $date_datatype => sub { my $v = shift; $v =~ s/3/5/; return $v }
504
    }
505
);
506
$row = $result->one;
507
is($row->{key1}, '2010-01-05');
508
is($row->{key2}, '2010-01-01 01:01:03');
test cleanup
Yuki Kimoto authored on 2011-08-10
509

            
test cleanup
Yuki Kimoto authored on 2011-08-10
510
$result = $dbi->select(table => 'table1');
511
$result->type_rule(
512
    from1 => [$date_datatype => sub { my $v = shift; $v =~ s/3/5/; return $v }]
513
);
514
$row = $result->one;
515
is($row->{key1}, '2010-01-05');
516
is($row->{key2}, '2010-01-01 01:01:03');
test cleanup
Yuki Kimoto authored on 2011-08-10
517

            
test cleanup
Yuki Kimoto authored on 2011-08-10
518
$dbi->register_filter(five => sub { my $v = shift; $v =~ s/3/5/; return $v });
519
$result = $dbi->select(table => 'table1');
520
$result->type_rule(
521
    from1 => [$date_datatype => 'five']
522
);
523
$row = $result->one;
524
is($row->{key1}, '2010-01-05');
525
is($row->{key2}, '2010-01-01 01:01:03');
test cleanup
Yuki Kimoto authored on 2011-08-10
526

            
test cleanup
Yuki Kimoto authored on 2011-08-10
527
$result = $dbi->select(table => 'table1');
528
$result->type_rule(
529
    from1 => [$date_datatype => undef]
530
);
531
$row = $result->one;
532
is($row->{key1}, '2010-01-03');
533
is($row->{key2}, '2010-01-01 01:01:03');
test cleanup
Yuki Kimoto authored on 2011-08-10
534

            
test cleanup
Yuki Kimoto authored on 2011-08-10
535
$dbi = DBIx::Custom->connect;
536
eval { $dbi->execute('drop table table1') };
537
$dbi->execute($create_table1_type);
538
$dbi->type_rule(
539
    from1 => {
540
        $date_datatype => sub { my $v = shift; $v =~ s/3/4/; return $v },
541
    },
542
);
543
$dbi->insert({key1 => '2010-01-03'}, table => 'table1');
544
$result = $dbi->select(table => 'table1');
545
$result->filter(key1 => sub { my $v = shift; $v =~ s/4/5/; return $v });
546
is($result->one->{key1}, '2010-01-05');
test cleanup
Yuki Kimoto authored on 2011-08-10
547

            
test cleanup
Yuki Kimoto authored on 2011-08-10
548
$dbi = DBIx::Custom->connect;
549
eval { $dbi->execute('drop table table1') };
550
$dbi->execute($create_table1_type);
551
$dbi->type_rule(
552
    from1 => {
553
        $date_datatype => sub { my $v = shift; $v =~ s/3/4/; return $v }
554
    },
555
);
556
$dbi->insert({key1 => '2010-01-03'}, table => 'table1');
557
$result = $dbi->select(table => 'table1');
558
$result->filter(key1 => sub { my $v = shift; $v =~ s/4/5/; return $v });
559
is($result->fetch->[0], '2010-01-05');
test cleanup
Yuki Kimoto authored on 2011-08-10
560

            
test cleanup
Yuki Kimoto authored on 2011-08-10
561
$dbi = DBIx::Custom->connect;
562
eval { $dbi->execute('drop table table1') };
563
$dbi->execute($create_table1_type);
564
$dbi->type_rule(
565
    into1 => {
566
        $date_typename => sub { my $v = shift; $v =~ s/3/4/; return $v }
567
    },
568
    into2 => {
569
        $date_typename => sub { my $v = shift; $v =~ s/3/5/; return $v }
570
    },
571
    from1 => {
572
        $date_datatype => sub { my $v = shift; $v =~ s/3/6/; return $v }
573
    },
574
    from2 => {
575
        $date_datatype => sub { my $v = shift; $v =~ s/(3|6)/7/; return $v }
576
    }
577
);
578
$dbi->insert({key1 => '2010-01-03'}, table => 'table1', type_rule_off => 1);
579
$result = $dbi->select(table => 'table1');
580
is($result->type_rule_off->fetch_first->[0], '2010-01-03');
581
$result = $dbi->select(table => 'table1');
582
is($result->type_rule_on->fetch_first->[0], '2010-01-07');
test cleanup
Yuki Kimoto authored on 2011-08-10
583

            
test cleanup
Yuki Kimoto authored on 2011-08-10
584
$dbi = DBIx::Custom->connect;
585
eval { $dbi->execute('drop table table1') };
586
$dbi->execute($create_table1_type);
587
$dbi->type_rule(
588
    into1 => {
589
        $date_typename => sub { my $v = shift; $v =~ s/3/4/; return $v }
590
    },
591
    into2 => {
592
        $date_typename => sub { my $v = shift; $v =~ s/3/5/; return $v }
593
    },
594
    from1 => {
595
        $date_datatype => sub { my $v = shift; $v =~ s/(3|5)/6/; return $v }
596
    },
597
    from2 => {
598
        $date_datatype => sub { my $v = shift; $v =~ s/6/7/; return $v }
599
    }
600
);
601
$dbi->insert({key1 => '2010-01-03'}, table => 'table1', type_rule1_off => 1);
602
$result = $dbi->select(table => 'table1');
603
is($result->type_rule1_off->fetch_first->[0], '2010-01-05');
604
$result = $dbi->select(table => 'table1');
605
is($result->type_rule1_on->fetch_first->[0], '2010-01-07');
test cleanup
Yuki Kimoto authored on 2011-08-10
606

            
test cleanup
Yuki Kimoto authored on 2011-08-10
607
$dbi = DBIx::Custom->connect;
608
eval { $dbi->execute('drop table table1') };
609
$dbi->execute($create_table1_type);
610
$dbi->type_rule(
611
    into1 => {
612
        $date_typename => sub { my $v = shift; $v =~ s/3/5/; return $v }
613
    },
614
    into2 => {
615
        $date_typename => sub { my $v = shift; $v =~ s/3/4/; return $v }
616
    },
617
    from1 => {
618
        $date_datatype => sub { my $v = shift; $v =~ s/5/6/; return $v }
619
    },
620
    from2 => {
621
        $date_datatype => sub { my $v = shift; $v =~ s/(3|6)/7/; return $v }
622
    }
623
);
624
$dbi->insert({key1 => '2010-01-03'}, table => 'table1', type_rule2_off => 1);
625
$result = $dbi->select(table => 'table1');
626
is($result->type_rule2_off->fetch_first->[0], '2010-01-06');
627
$result = $dbi->select(table => 'table1');
628
is($result->type_rule2_on->fetch_first->[0], '2010-01-07');
test cleanup
Yuki Kimoto authored on 2011-08-10
629

            
630

            
added common test executing ...
Yuki Kimoto authored on 2011-08-07
631

            
632
# Create table
test cleanup
Yuki Kimoto authored on 2011-08-10
633
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
634
eval { $dbi->execute('drop table table1') };
added common test executing ...
Yuki Kimoto authored on 2011-08-07
635
$dbi->execute($create_table1);
636
$model = $dbi->create_model(table => 'table1');
637
$model->insert({key1 => 1, key2 => 2});
638
is_deeply($model->select->all, [{key1 => 1, key2 => 2}]);
639

            
cleanup test
Yuki Kimoto authored on 2011-08-08
640
test 'DBIx::Custom::Result test';
641
$dbi->delete_all(table => 'table1');
642
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
643
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
644
$source = "select key1, key2 from table1";
645
$query = $dbi->create_query($source);
646
$result = $dbi->execute($query);
647

            
648
@rows = ();
649
while (my $row = $result->fetch) {
650
    push @rows, [@$row];
651
}
652
is_deeply(\@rows, [[1, 2], [3, 4]], "fetch");
653

            
654
$result = $dbi->execute($query);
655
@rows = ();
656
while (my $row = $result->fetch_hash) {
657
    push @rows, {%$row};
658
}
659
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "fetch_hash");
660

            
661
$result = $dbi->execute($query);
662
$rows = $result->fetch_all;
663
is_deeply($rows, [[1, 2], [3, 4]], "fetch_all");
664

            
665
$result = $dbi->execute($query);
666
$rows = $result->fetch_hash_all;
667
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "all");
668

            
669
test 'Insert query return value';
670
$source = "insert into table1 {insert_param key1 key2}";
671
$query = $dbi->execute($source, {}, query => 1);
672
$ret_val = $dbi->execute($query, param => {key1 => 1, key2 => 2});
673
ok($ret_val);
674

            
675
test 'Direct query';
676
$dbi->delete_all(table => 'table1');
677
$insert_source = "insert into table1 {insert_param key1 key2}";
678
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2});
679
$result = $dbi->execute('select * from table1;');
680
$rows = $result->all;
681
is_deeply($rows, [{key1 => 1, key2 => 2}]);
682

            
683
test 'Filter basic';
684
$dbi->delete_all(table => 'table1');
685
$dbi->register_filter(twice       => sub { $_[0] * 2}, 
686
                    three_times => sub { $_[0] * 3});
687

            
688
$insert_source  = "insert into table1 {insert_param key1 key2};";
689
$insert_query = $dbi->execute($insert_source, {}, query => 1);
690
$insert_query->filter({key1 => 'twice'});
691
$dbi->execute($insert_query, param => {key1 => 1, key2 => 2});
692
$result = $dbi->execute('select * from table1;');
693
$rows = $result->filter({key2 => 'three_times'})->all;
694
is_deeply($rows, [{key1 => 2, key2 => 6}], "filter fetch_filter");
695

            
696
test 'Filter in';
697
$dbi->delete_all(table => 'table1');
698
$insert_source  = "insert into table1 {insert_param key1 key2};";
699
$insert_query = $dbi->execute($insert_source, {}, query => 1);
700
$dbi->execute($insert_query, param => {key1 => 2, key2 => 4});
701
$select_source = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}";
702
$select_query = $dbi->execute($select_source,{}, query => 1);
703
$select_query->filter({'table1.key1' => 'twice'});
704
$result = $dbi->execute($select_query, param => {'table1.key1' => [1,5], 'table1.key2' => [2,4]});
705
$rows = $result->all;
706
is_deeply($rows, [{key1 => 2, key2 => 4}], "filter");
707

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

            
714
$source = "select * from table1 where key1 = :key1 and {<> key2} and {< key3} and {> key4} and {>= key5};";
715
$query = $dbi->execute($source, {}, query => 1);
716
$result = $dbi->execute($query, param => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
717
$rows = $result->all;
718
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1");
719

            
720
$source = "select * from table1 where key1 = :key1 and {<> key2} and {< key3} and {> key4} and {>= key5};";
721
$query = $dbi->execute($source, {}, query => 1);
722
$result = $dbi->execute($query, {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
723
$rows = $result->all;
724
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1");
725

            
726
$source = "select * from table1 where {<= key1} and {like key2};";
727
$query = $dbi->execute($source, {}, query => 1);
728
$result = $dbi->execute($query, param => {key1 => 1, key2 => '%2%'});
729
$rows = $result->all;
730
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag2");
731

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

            
738
$source = "select * from table1 where {in key1 2};";
739
$query = $dbi->execute($source, {}, query => 1);
740
$result = $dbi->execute($query, param => {key1 => [9, 1]});
741
$rows = $result->all;
742
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic");
743

            
744
test 'DBIx::Custom::SQLTemplate insert tag';
745
$dbi->delete_all(table => 'table1');
746
$insert_source = 'insert into table1 {insert_param key1 key2 key3 key4 key5}';
747
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
748

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

            
753
test 'DBIx::Custom::SQLTemplate update tag';
754
$dbi->delete_all(table => 'table1');
755
$insert_source = "insert into table1 {insert_param key1 key2 key3 key4 key5}";
756
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
757
$dbi->execute($insert_source, param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
758

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

            
762
$result = $dbi->execute('select * from table1 order by key1;');
763
$rows = $result->all;
764
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5},
765
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "basic");
766

            
cleanup test
Yuki Kimoto authored on 2011-08-08
767
test 'Named placeholder';
768
$dbi->execute('drop table table1');
769
$dbi->execute($create_table1_2);
770
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
771
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
772

            
773
$source = "select * from table1 where key1 = :key1 and key2 = :key2";
774
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
775
$rows = $result->all;
776
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
777

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

            
783
$source = "select * from table1 where key1 = :key1 or key1 = :key1";
784
$result = $dbi->execute($source, param => {key1 => [1, 2]});
785
$rows = $result->all;
786
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
787

            
788
$source = "select * from table1 where key1 = :table1.key1 and key2 = :table1.key2";
789
$result = $dbi->execute(
790
    $source,
791
    param => {'table1.key1' => 1, 'table1.key2' => 1},
792
    filter => {'table1.key2' => sub { $_[0] * 2 }}
793
);
794
$rows = $result->all;
795
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
796

            
797
$dbi->execute('drop table table1');
798
$dbi->execute($create_table1);
799
$dbi->insert(table => 'table1', param => {key1 => '2011-10-14 12:19:18', key2 => 2});
800
$source = "select * from table1 where key1 = '2011-10-14 12:19:18' and key2 = :key2";
801
$result = $dbi->execute(
802
    $source,
803
    param => {'key2' => 2},
804
);
805

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

            
809
$dbi->delete_all(table => 'table1');
810
$dbi->insert(table => 'table1', param => {key1 => 'a:b c:d', key2 => 2});
811
$source = "select * from table1 where key1 = 'a\\:b c\\:d' and key2 = :key2";
812
$result = $dbi->execute(
813
    $source,
814
    param => {'key2' => 2},
815
);
816
$rows = $result->all;
817
is_deeply($rows, [{key1 => 'a:b c:d', key2 => 2}]);
818

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
826
test 'insert';
cleanup test
Yuki Kimoto authored on 2011-08-10
827
eval { $dbi->execute('drop table table1') };
828
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
829
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
830
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
831
$result = $dbi->execute('select * from table1;');
832
$rows   = $result->all;
833
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
834

            
835
$dbi->execute('delete from table1');
836
$dbi->register_filter(
837
    twice       => sub { $_[0] * 2 },
838
    three_times => sub { $_[0] * 3 }
839
);
840
$dbi->default_bind_filter('twice');
841
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => 'three_times'});
842
$result = $dbi->execute('select * from table1;');
843
$rows   = $result->all;
844
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter");
845
$dbi->default_bind_filter(undef);
846

            
cleanup test
Yuki Kimoto authored on 2011-08-10
847
$dbi->execute('drop table table1');
848
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
849
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, append => '   ');
850
$rows = $dbi->select(table => 'table1')->all;
851
is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append');
852

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

            
856
eval{$dbi->insert(table => 'table', param => {';' => 1})};
857
like($@, qr/safety/);
858

            
cleanup test
Yuki Kimoto authored on 2011-08-10
859
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
860
$dbi->execute($create_table_reserved);
cleanup test
Yuki Kimoto authored on 2011-08-10
861
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
862
$dbi->insert(table => 'table', param => {select => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
863
$result = $dbi->execute("select * from ${q}table$p");
cleanup test
Yuki Kimoto authored on 2011-08-10
864
$rows   = $result->all;
test cleanup
Yuki Kimoto authored on 2011-08-10
865
is_deeply($rows, [{select => 2, update => undef}], "reserved word");
cleanup test
Yuki Kimoto authored on 2011-08-10
866

            
cleanup test
Yuki Kimoto authored on 2011-08-10
867
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
868
$dbi->execute($create_table1);
869
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
870
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
871
$result = $dbi->execute('select * from table1;');
872
$rows   = $result->all;
873
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
874

            
cleanup test
Yuki Kimoto authored on 2011-08-10
875
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
876
$dbi->execute($create_table1);
877
$dbi->insert(table => 'table1', param => {key1 => \"'1'", key2 => 2});
878
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
879
$result = $dbi->execute('select * from table1;');
880
$rows   = $result->all;
881
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
882

            
test cleanup
Yuki Kimoto authored on 2011-08-10
883
test 'update';
884
eval { $dbi->execute('drop table table1') };
885
$dbi->execute($create_table1_2);
886
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
887
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
888
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
889
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
890
$rows   = $result->all;
891
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
892
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
893
                  "basic");
894
                  
895
$dbi->execute("delete from table1");
896
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
897
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
898
$dbi->update(table => 'table1', param => {key2 => 12}, where => {key2 => 2, key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
899
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
900
$rows   = $result->all;
901
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
902
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
903
                  "update key same as search key");
904

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

            
912
$dbi->execute("delete from table1");
913
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
914
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
915
$dbi->register_filter(twice => sub { $_[0] * 2 });
916
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1},
917
              filter => {key2 => sub { $_[0] * 2 }});
test cleanup
Yuki Kimoto authored on 2011-08-10
918
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
919
$rows   = $result->all;
920
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5},
921
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
922
                  "filter");
923

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

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

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

            
932
eval { $dbi->execute('drop table table1') };
933
$dbi->execute($create_table1);
934
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
935
$where = $dbi->where;
936
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
937
$where->param({key1 => 1, key2 => 2});
938
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
939
$result = $dbi->select(table => 'table1');
940
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
941

            
942
eval { $dbi->execute('drop table table1') };
943
$dbi->execute($create_table1);
944
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
945
$dbi->update(
946
    table => 'table1',
947
    param => {key1 => 3},
948
    where => [
949
        ['and', 'key1 = :key1', 'key2 = :key2'],
950
        {key1 => 1, key2 => 2}
951
    ]
952
);
953
$result = $dbi->select(table => 'table1');
954
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
955

            
956
eval { $dbi->execute('drop table table1') };
957
$dbi->execute($create_table1);
958
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
959
$where = $dbi->where;
960
$where->clause(['and', 'key2 = :key2']);
961
$where->param({key2 => 2});
962
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
963
$result = $dbi->select(table => 'table1');
964
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
965

            
966
eval{$dbi->update(table => 'table1', param => {';' => 1})};
967
like($@, qr/safety/);
968

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

            
972
eval { $dbi->execute('drop table table1') };
973
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
974
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
975
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
976
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
977
$dbi->insert(table => 'table', param => {select => 1});
978
$dbi->update(table => 'table', where => {select => 1}, param => {update => 2});
979
$result = $dbi->execute("select * from ${q}table$p");
980
$rows   = $result->all;
981
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
982

            
983
eval {$dbi->update_all(table => 'table', param => {';' => 2}) };
984
like($@, qr/safety/);
985

            
986
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
987
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
988
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
989
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
990
$dbi->insert(table => 'table', param => {select => 1});
991
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
992
$result = $dbi->execute("select * from ${q}table$p");
993
$rows   = $result->all;
994
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
995

            
996
eval { $dbi->execute('drop table table1') };
997
$dbi->execute($create_table1_2);
998
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
999
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1000
$dbi->update({key2 => 11}, table => 'table1', where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1001
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
1002
$rows   = $result->all;
1003
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
1004
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1005
                  "basic");
1006

            
1007
eval { $dbi->execute('drop table table1') };
1008
$dbi->execute($create_table1_2);
1009
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1010
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1011
$dbi->update(table => 'table1', param => {key2 => \"'11'"}, where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1012
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
1013
$rows   = $result->all;
1014
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
1015
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1016
                  "basic");
1017

            
1018
test 'update_all';
1019
eval { $dbi->execute('drop table table1') };
1020
$dbi->execute($create_table1_2);
1021
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1022
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1023
$dbi->register_filter(twice => sub { $_[0] * 2 });
1024
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'});
1025
$result = $dbi->execute('select * from table1;');
1026
$rows   = $result->all;
1027
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
1028
                  {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
1029
                  "filter");
1030

            
1031

            
1032
test 'delete';
1033
eval { $dbi->execute('drop table table1') };
1034
$dbi->execute($create_table1);
1035
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1036
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1037
$dbi->delete(table => 'table1', where => {key1 => 1});
1038
$result = $dbi->execute('select * from table1;');
1039
$rows   = $result->all;
1040
is_deeply($rows, [{key1 => 3, key2 => 4}], "basic");
1041

            
1042
$dbi->execute("delete from table1;");
1043
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1044
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1045
$dbi->register_filter(twice => sub { $_[0] * 2 });
1046
$dbi->delete(table => 'table1', where => {key2 => 1}, filter => {key2 => 'twice'});
1047
$result = $dbi->execute('select * from table1;');
1048
$rows   = $result->all;
1049
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter");
1050

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

            
1053
$dbi->delete_all(table => 'table1');
1054
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1055
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1056
$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2});
1057
$rows = $dbi->select(table => 'table1')->all;
1058
is_deeply($rows, [{key1 => 3, key2 => 4}], "delete multi key");
1059

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

            
1063
eval { $dbi->execute('drop table table1') };
1064
$dbi->execute($create_table1);
1065
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1066
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1067
$where = $dbi->where;
1068
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
1069
$where->param({ke1 => 1, key2 => 2});
1070
$dbi->delete(table => 'table1', where => $where);
1071
$result = $dbi->select(table => 'table1');
1072
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
1073

            
1074
eval { $dbi->execute('drop table table1') };
1075
$dbi->execute($create_table1);
1076
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1077
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1078
$dbi->delete(
1079
    table => 'table1',
1080
    where => [
1081
        ['and', 'key1 = :key1', 'key2 = :key2'],
1082
        {ke1 => 1, key2 => 2}
1083
    ]
1084
);
1085
$result = $dbi->select(table => 'table1');
1086
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
1087

            
1088
eval { $dbi->execute('drop table table1') };
test cleanup
Yuki Kimoto authored on 2011-08-10
1089
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1090
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1091
$dbi->delete(table => 'table1', where => {key1 => 1}, prefix => '    ');
1092
$result = $dbi->execute('select * from table1;');
1093
$rows   = $result->all;
1094
is_deeply($rows, [], "basic");
1095

            
1096
test 'delete error';
1097
eval { $dbi->execute('drop table table1') };
1098
$dbi->execute($create_table1);
1099
eval{$dbi->delete(table => 'table1')};
1100
like($@, qr/"where" must be specified/,
1101
         "where key-value pairs not specified");
1102

            
1103
eval{$dbi->delete(table => 'table1', where => {';' => 1})};
1104
like($@, qr/safety/);
1105

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1106
$dbi = undef;
test cleanup
Yuki Kimoto authored on 2011-08-10
1107
$dbi = DBIx::Custom->connect;
1108
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1109
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
1110
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
1111
$dbi->insert(table => 'table', param => {select => 1});
1112
$dbi->delete(table => 'table', where => {select => 1});
1113
$result = $dbi->execute("select * from ${q}table$p");
1114
$rows   = $result->all;
1115
is_deeply($rows, [], "reserved word");
1116

            
1117
test 'delete_all';
1118
eval { $dbi->execute('drop table table1') };
1119
$dbi->execute($create_table1);
1120
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1121
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1122
$dbi->delete_all(table => 'table1');
1123
$result = $dbi->execute('select * from table1;');
1124
$rows   = $result->all;
1125
is_deeply($rows, [], "basic");
1126

            
1127

            
1128
test 'select';
1129
eval { $dbi->execute('drop table table1') };
1130
$dbi->execute($create_table1);
1131
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1132
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1133
$rows = $dbi->select(table => 'table1')->all;
1134
is_deeply($rows, [{key1 => 1, key2 => 2},
1135
                  {key1 => 3, key2 => 4}], "table");
1136

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

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

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

            
1146
$dbi->register_filter(decrement => sub { $_[0] - 1 });
1147
$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'})
1148
            ->all;
1149
is_deeply($rows, [{key1 => 1, key2 => 2}], "filter");
1150

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1151
eval { $dbi->execute("drop table table2") };
1152
$dbi->execute($create_table2);
test cleanup
Yuki Kimoto authored on 2011-08-10
1153
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
1154
$rows = $dbi->select(
1155
    table => [qw/table1 table2/],
1156
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
1157
    where   => {'table1.key2' => 2},
1158
    relation  => {'table1.key1' => 'table2.key1'}
1159
)->all;
1160
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : exists where");
1161

            
1162
$rows = $dbi->select(
1163
    table => [qw/table1 table2/],
1164
    column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
1165
    relation  => {'table1.key1' => 'table2.key1'}
1166
)->all;
1167
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : no exists where");
1168

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

            
1172
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1173
eval { $dbi->execute("drop table ${q}table$p") };
1174
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
1175
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
1176
$dbi->insert(table => 'table', param => {select => 1, update => 2});
1177
$result = $dbi->select(table => 'table', where => {select => 1});
1178
$rows   = $result->all;
1179
is_deeply($rows, [{select => 2, update => 2}], "reserved word");
1180

            
1181
test 'fetch filter';
1182
eval { $dbi->execute('drop table table1') };
1183
$dbi->register_filter(
1184
    twice       => sub { $_[0] * 2 },
1185
    three_times => sub { $_[0] * 3 }
1186
);
1187
$dbi->default_fetch_filter('twice');
1188
$dbi->execute($create_table1);
1189
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1190
$result = $dbi->select(table => 'table1');
1191
$result->filter({key1 => 'three_times'});
1192
$row = $result->one;
1193
is_deeply($row, {key1 => 3, key2 => 4}, "default_fetch_filter and filter");
1194

            
1195
test 'filters';
1196
$dbi = DBIx::Custom->new;
1197

            
1198
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')),
1199
   'あ', "decode_utf8");
1200

            
1201
is($dbi->filters->{encode_utf8}->('あ'),
1202
   encode_utf8('あ'), "encode_utf8");
1203

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1204
test 'transaction1';
test cleanup
Yuki Kimoto authored on 2011-08-10
1205
$dbi = DBIx::Custom->connect;
1206
eval { $dbi->execute('drop table table1') };
1207
$dbi->execute($create_table1);
1208
$dbi->dbh->begin_work;
1209
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1210
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
1211
$dbi->dbh->commit;
1212
$result = $dbi->select(table => 'table1');
1213
is_deeply(scalar $result->all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}],
1214
          "commit");
1215

            
1216
$dbi = DBIx::Custom->connect;
1217
eval { $dbi->execute('drop table table1') };
1218
$dbi->execute($create_table1);
1219
$dbi->dbh->begin_work(0);
1220
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1221
$dbi->dbh->rollback;
1222

            
1223
$result = $dbi->select(table => 'table1');
1224
ok(! $result->fetch_first, "rollback");
1225

            
1226
test 'execute';
1227
eval { $dbi->execute('drop table table1') };
1228
$dbi->execute($create_table1);
1229
{
1230
    local $Carp::Verbose = 0;
1231
    eval{$dbi->execute('select * frm table1')};
1232
    like($@, qr/\Qselect * frm table1;/, "fail prepare");
1233
    like($@, qr/\.t /, "fail : not verbose");
1234
}
1235
{
1236
    local $Carp::Verbose = 1;
1237
    eval{$dbi->execute('select * frm table1')};
1238
    like($@, qr/Custom.*\.t /s, "fail : verbose");
1239
}
1240

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

            
1244
$query = $dbi->execute('select * from table1 where key1 = :key1', {}, query => 1);
1245
$dbi->dbh->disconnect;
1246
eval{$dbi->execute($query, param => {key1 => {a => 1}})};
1247
ok($@, "execute fail");
1248

            
1249
{
1250
    local $Carp::Verbose = 0;
1251
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
1252
    like($@, qr/\Q.t /, "caller spec : not vebose");
1253
}
1254
{
1255
    local $Carp::Verbose = 1;
1256
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
1257
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
1258
}
1259

            
1260

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1261
test 'transaction2';
test cleanup
Yuki Kimoto authored on 2011-08-10
1262
$dbi = DBIx::Custom->connect;
1263
eval { $dbi->execute('drop table table1') };
1264
$dbi->execute($create_table1);
1265

            
1266
$dbi->begin_work;
1267

            
1268
eval {
1269
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1270
    die "Error";
1271
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1272
};
1273

            
1274
$dbi->rollback if $@;
1275

            
1276
$result = $dbi->select(table => 'table1');
1277
$rows = $result->all;
1278
is_deeply($rows, [], "rollback");
1279

            
1280
$dbi->begin_work;
1281

            
1282
eval {
1283
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1284
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1285
};
1286

            
1287
$dbi->commit unless $@;
1288

            
1289
$result = $dbi->select(table => 'table1');
1290
$rows = $result->all;
1291
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "commit");
1292

            
1293
$dbi->dbh->{AutoCommit} = 0;
1294
eval{ $dbi->begin_work };
1295
ok($@, "exception");
1296
$dbi->dbh->{AutoCommit} = 1;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
1297

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1298
test 'cache';
1299
eval { $dbi->execute('drop table table1') };
1300
$dbi->cache(1);
1301
$dbi->execute($create_table1);
1302
$source = 'select * from table1 where key1 = :key1 and key2 = :key2;';
1303
$dbi->execute($source, {}, query => 1);
1304
is_deeply($dbi->{_cached}->{$source}, 
1305
          {sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2'], tables => []}, "cache");
1306

            
1307
eval { $dbi->execute('drop table table1') };
1308
$dbi->execute($create_table1);
1309
$dbi->{_cached} = {};
1310
$dbi->cache(0);
1311
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1312
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
1313

            
1314
test 'execute';
1315
eval { $dbi->execute('drop table table1') };
1316
$dbi->execute($create_table1);
1317
{
1318
    local $Carp::Verbose = 0;
1319
    eval{$dbi->execute('select * frm table1')};
1320
    like($@, qr/\Qselect * frm table1;/, "fail prepare");
1321
    like($@, qr/\.t /, "fail : not verbose");
1322
}
1323
{
1324
    local $Carp::Verbose = 1;
1325
    eval{$dbi->execute('select * frm table1')};
1326
    like($@, qr/Custom.*\.t /s, "fail : verbose");
1327
}
1328

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

            
1332
$query = $dbi->execute('select * from table1 where key1 = :key1', {}, query => 1);
1333
$dbi->dbh->disconnect;
1334
eval{$dbi->execute($query, param => {key1 => {a => 1}})};
1335
ok($@, "execute fail");
1336

            
1337
{
1338
    local $Carp::Verbose = 0;
1339
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
1340
    like($@, qr/\Q.t /, "caller spec : not vebose");
1341
}
1342
{
1343
    local $Carp::Verbose = 1;
1344
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
1345
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
1346
}
1347

            
1348
test 'method';
1349
$dbi->method(
1350
    one => sub { 1 }
1351
);
1352
$dbi->method(
1353
    two => sub { 2 }
1354
);
1355
$dbi->method({
1356
    twice => sub {
1357
        my $self = shift;
1358
        return $_[0] * 2;
1359
    }
1360
});
1361

            
1362
is($dbi->one, 1, "first");
1363
is($dbi->two, 2, "second");
1364
is($dbi->twice(5), 10 , "second");
1365

            
1366
eval {$dbi->XXXXXX};
1367
ok($@, "not exists");
1368

            
1369
test 'out filter';
1370
$dbi = DBIx::Custom->connect;
1371
eval { $dbi->execute('drop table table1') };
1372
$dbi->execute($create_table1);
1373
$dbi->register_filter(twice => sub { $_[0] * 2 });
1374
$dbi->register_filter(three_times => sub { $_[0] * 3});
1375
$dbi->apply_filter(
1376
    'table1', 'key1' => {out => 'twice', in => 'three_times'}, 
1377
              'key2' => {out => 'three_times', in => 'twice'});
1378
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1379
$result = $dbi->execute('select * from table1;');
1380
$row   = $result->fetch_hash_first;
1381
is_deeply($row, {key1 => 2, key2 => 6}, "insert");
1382
$result = $dbi->select(table => 'table1');
1383
$row   = $result->one;
1384
is_deeply($row, {key1 => 6, key2 => 12}, "insert");
1385

            
1386
$dbi = DBIx::Custom->connect;
1387
eval { $dbi->execute('drop table table1') };
1388
$dbi->execute($create_table1);
1389
$dbi->register_filter(twice => sub { $_[0] * 2 });
1390
$dbi->register_filter(three_times => sub { $_[0] * 3});
1391
$dbi->apply_filter(
1392
    'table1', 'key1' => {out => 'twice', in => 'three_times'}, 
1393
              'key2' => {out => 'three_times', in => 'twice'});
1394
$dbi->apply_filter(
1395
    'table1', 'key1' => {out => undef}
1396
); 
1397
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1398
$result = $dbi->execute('select * from table1;');
1399
$row   = $result->one;
1400
is_deeply($row, {key1 => 1, key2 => 6}, "insert");
1401

            
1402
$dbi = DBIx::Custom->connect;
1403
eval { $dbi->execute('drop table table1') };
1404
$dbi->execute($create_table1);
1405
$dbi->register_filter(twice => sub { $_[0] * 2 });
1406
$dbi->apply_filter(
1407
    'table1', 'key1' => {out => 'twice', in => 'twice'}
1408
);
1409
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => undef});
1410
$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2});
1411
$result = $dbi->execute('select * from table1;');
1412
$row   = $result->one;
1413
is_deeply($row, {key1 => 4, key2 => 2}, "update");
1414

            
1415
$dbi = DBIx::Custom->connect;
1416
eval { $dbi->execute('drop table table1') };
1417
$dbi->execute($create_table1);
1418
$dbi->register_filter(twice => sub { $_[0] * 2 });
1419
$dbi->apply_filter(
1420
    'table1', 'key1' => {out => 'twice', in => 'twice'}
1421
);
1422
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1=> undef});
1423
$dbi->delete(table => 'table1', where => {key1 => 1});
1424
$result = $dbi->execute('select * from table1;');
1425
$rows   = $result->all;
1426
is_deeply($rows, [], "delete");
1427

            
1428
$dbi = DBIx::Custom->connect;
1429
eval { $dbi->execute('drop table table1') };
1430
$dbi->execute($create_table1);
1431
$dbi->register_filter(twice => sub { $_[0] * 2 });
1432
$dbi->apply_filter(
1433
    'table1', 'key1' => {out => 'twice', in => 'twice'}
1434
);
1435
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
1436
$result = $dbi->select(table => 'table1', where => {key1 => 1});
1437
$result->filter({'key2' => 'twice'});
1438
$rows   = $result->all;
1439
is_deeply($rows, [{key1 => 4, key2 => 4}], "select");
1440

            
1441
$dbi = DBIx::Custom->connect;
1442
eval { $dbi->execute('drop table table1') };
1443
$dbi->execute($create_table1);
1444
$dbi->register_filter(twice => sub { $_[0] * 2 });
1445
$dbi->apply_filter(
1446
    'table1', 'key1' => {out => 'twice', in => 'twice'}
1447
);
1448
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
1449
$result = $dbi->execute("select * from table1 where key1 = :key1 and key2 = :key2;",
1450
                        param => {key1 => 1, key2 => 2},
1451
                        table => ['table1']);
1452
$rows   = $result->all;
1453
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute");
1454

            
1455
$dbi = DBIx::Custom->connect;
1456
eval { $dbi->execute('drop table table1') };
1457
$dbi->execute($create_table1);
1458
$dbi->register_filter(twice => sub { $_[0] * 2 });
1459
$dbi->apply_filter(
1460
    'table1', 'key1' => {out => 'twice', in => 'twice'}
1461
);
1462
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
1463
$result = $dbi->execute("select * from {table table1} where key1 = :key1 and key2 = :key2;",
1464
                        param => {key1 => 1, key2 => 2});
1465
$rows   = $result->all;
1466
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute table tag");
1467

            
1468
$dbi = DBIx::Custom->connect;
1469
eval { $dbi->execute('drop table table1') };
1470
eval { $dbi->execute('drop table table2') };
1471
$dbi->execute($create_table1);
1472
$dbi->execute($create_table2);
1473
$dbi->register_filter(twice => sub { $_[0] * 2 });
1474
$dbi->register_filter(three_times => sub { $_[0] * 3 });
1475
$dbi->apply_filter(
1476
    'table1', 'key2' => {out => 'twice', in => 'twice'}
1477
);
1478
$dbi->apply_filter(
1479
    'table2', 'key3' => {out => 'three_times', in => 'three_times'}
1480
);
1481
$dbi->insert(table => 'table1', param => {key1 => 5, key2 => 2}, filter => {key2 => undef});
1482
$dbi->insert(table => 'table2', param => {key1 => 5, key3 => 6}, filter => {key3 => undef});
1483
$result = $dbi->select(
1484
     table => ['table1', 'table2'],
1485
     column => ['key2', 'key3'],
1486
     where => {'table1.key2' => 1, 'table2.key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
1487

            
1488
$result->filter({'key2' => 'twice'});
1489
$rows   = $result->all;
1490
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join");
1491

            
1492
$result = $dbi->select(
1493
     table => ['table1', 'table2'],
1494
     column => ['key2', 'key3'],
1495
     where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
1496

            
1497
$result->filter({'key2' => 'twice'});
1498
$rows   = $result->all;
1499
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join : omit");
1500

            
1501
test 'each_column';
1502
$dbi = DBIx::Custom->connect;
1503
eval { $dbi->execute("drop table ${q}table$p") };
1504
eval { $dbi->execute('drop table table1') };
1505
eval { $dbi->execute('drop table table2') };
test cleranup
Yuki Kimoto authored on 2011-08-10
1506
eval { $dbi->execute('drop table table3') };
test cleanup
Yuki Kimoto authored on 2011-08-10
1507
$dbi->execute($create_table1_type);
1508
$dbi->execute($create_table2);
1509

            
1510
$infos = [];
1511
$dbi->each_column(sub {
1512
    my ($self, $table, $column, $cinfo) = @_;
1513
    
1514
    if ($table =~ /^table\d/) {
1515
         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
1516
         push @$infos, $info;
1517
    }
1518
});
1519
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
1520
is_deeply($infos, 
1521
    [
1522
        ['table1', 'key1', 'key1'],
1523
        ['table1', 'key2', 'key2'],
1524
        ['table2', 'key1', 'key1'],
1525
        ['table2', 'key3', 'key3']
1526
    ]
1527
    
1528
);
1529
test 'each_table';
1530
$dbi = DBIx::Custom->connect;
1531
eval { $dbi->execute('drop table table1') };
1532
eval { $dbi->execute('drop table table2') };
1533
$dbi->execute($create_table2);
1534
$dbi->execute($create_table1_type);
1535

            
1536
$infos = [];
1537
$dbi->each_table(sub {
1538
    my ($self, $table, $table_info) = @_;
1539
    
1540
    if ($table =~ /^table\d/) {
1541
         my $info = [$table, $table_info->{TABLE_NAME}];
1542
         push @$infos, $info;
1543
    }
1544
});
1545
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
1546
is_deeply($infos, 
1547
    [
1548
        ['table1', 'table1'],
1549
        ['table2', 'table2'],
1550
    ]
1551
);
1552

            
1553
test 'connect super';
test cleanup
Yuki Kimoto authored on 2011-08-10
1554
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1555
eval { $dbi->execute('drop table table1') };
1556
$dbi->execute($create_table1);
1557
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1558
is($dbi->select(table => 'table1')->one->{key1}, 1);
1559

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1560
$dbi = DBIx::Custom->new;
test cleanup
Yuki Kimoto authored on 2011-08-10
1561
$dbi->connect;
1562
eval { $dbi->execute('drop table table1') };
1563
$dbi->execute($create_table1);
1564
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1565
is($dbi->select(table => 'table1')->one->{key1}, 1);
1566

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1567
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1568
eval { $dbi->execute('drop table table1') };
1569
$dbi->execute($create_table1);
1570
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1571
is($dbi->select(table => 'table1')->one->{key1}, 1);
1572

            
1573
test 'end_filter';
1574
$dbi = DBIx::Custom->connect;
1575
eval { $dbi->execute('drop table table1') };
1576
$dbi->execute($create_table1);
1577
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1578
$result = $dbi->select(table => 'table1');
1579
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1580
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 });
1581
$row = $result->fetch_first;
1582
is_deeply($row, [6, 40]);
1583

            
1584
$dbi = DBIx::Custom->connect;
1585
eval { $dbi->execute('drop table table1') };
1586
$dbi->execute($create_table1);
1587
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1588
$result = $dbi->select(table => 'table1');
1589
$result->filter([qw/key1 key2/] => sub { $_[0] * 2 });
1590
$result->end_filter([[qw/key1 key2/] => sub { $_[0] * 3 }]);
1591
$row = $result->fetch_first;
1592
is_deeply($row, [6, 12]);
1593

            
1594
$dbi = DBIx::Custom->connect;
1595
eval { $dbi->execute('drop table table1') };
1596
$dbi->execute($create_table1);
1597
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1598
$result = $dbi->select(table => 'table1');
1599
$result->filter([[qw/key1 key2/] => sub { $_[0] * 2 }]);
1600
$result->end_filter([qw/key1 key2/] => sub { $_[0] * 3 });
1601
$row = $result->fetch_first;
1602
is_deeply($row, [6, 12]);
1603

            
1604
$dbi->register_filter(five_times => sub { $_[0] * 5 });
1605
$result = $dbi->select(table => 'table1');
1606
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1607
$result->end_filter({key1 => sub { $_[0] * 3 }, key2 => 'five_times' });
1608
$row = $result->one;
1609
is_deeply($row, {key1 => 6, key2 => 40});
1610

            
1611
$dbi->register_filter(five_times => sub { $_[0] * 5 });
1612
$dbi->apply_filter('table1',
1613
    key1 => {end => sub { $_[0] * 3 } },
1614
    key2 => {end => 'five_times'}
1615
);
1616
$result = $dbi->select(table => 'table1');
1617
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1618
$row = $result->one;
1619
is_deeply($row, {key1 => 6, key2 => 40}, 'apply_filter');
1620

            
1621
$dbi->register_filter(five_times => sub { $_[0] * 5 });
1622
$dbi->apply_filter('table1',
1623
    key1 => {end => sub { $_[0] * 3 } },
1624
    key2 => {end => 'five_times'}
1625
);
1626
$result = $dbi->select(table => 'table1');
1627
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1628
$result->filter(key1 => undef);
1629
$result->end_filter(key1 => undef);
1630
$row = $result->one;
1631
is_deeply($row, {key1 => 1, key2 => 40}, 'apply_filter overwrite');
1632

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1633
test 'remove_end_filter and remove_filter';
1634
$dbi = DBIx::Custom->connect;
1635
eval { $dbi->execute('drop table table1') };
1636
$dbi->execute($create_table1);
1637
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1638
$result = $dbi->select(table => 'table1');
1639
$row = $result
1640
       ->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 })
1641
       ->remove_filter
1642
       ->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 })
1643
       ->remove_end_filter
1644
       ->fetch_first;
1645
is_deeply($row, [1, 2]);
1646

            
1647
test 'empty where select';
1648
$dbi = DBIx::Custom->connect;
1649
eval { $dbi->execute('drop table table1') };
1650
$dbi->execute($create_table1);
1651
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1652
$result = $dbi->select(table => 'table1', where => {});
1653
$row = $result->one;
1654
is_deeply($row, {key1 => 1, key2 => 2});
1655

            
1656
test 'select query option';
1657
$dbi = DBIx::Custom->connect;
1658
eval { $dbi->execute('drop table table1') };
1659
$dbi->execute($create_table1);
1660
$query = $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, query => 1);
1661
is(ref $query, 'DBIx::Custom::Query');
1662
$query = $dbi->update(table => 'table1', where => {key1 => 1}, param => {key2 => 2}, query => 1);
1663
is(ref $query, 'DBIx::Custom::Query');
1664
$query = $dbi->delete(table => 'table1', where => {key1 => 1}, query => 1);
1665
is(ref $query, 'DBIx::Custom::Query');
1666
$query = $dbi->select(table => 'table1', where => {key1 => 1, key2 => 2}, query => 1);
1667
is(ref $query, 'DBIx::Custom::Query');
1668

            
1669
test 'where';
1670
$dbi = DBIx::Custom->connect;
1671
eval { $dbi->execute('drop table table1') };
1672
$dbi->execute($create_table1);
1673
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1674
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1675
$where = $dbi->where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
1676
is("$where", "where ( key1 = :key1 and key2 = :key2 )", 'no param');
1677

            
1678
$where = $dbi->where
1679
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
1680
             ->param({key1 => 1});
1681

            
1682
$result = $dbi->select(
1683
    table => 'table1',
1684
    where => $where
1685
);
1686
$row = $result->all;
1687
is_deeply($row, [{key1 => 1, key2 => 2}]);
1688

            
1689
$result = $dbi->select(
1690
    table => 'table1',
1691
    where => [
1692
        ['and', 'key1 = :key1', 'key2 = :key2'],
1693
        {key1 => 1}
1694
    ]
1695
);
1696
$row = $result->all;
1697
is_deeply($row, [{key1 => 1, key2 => 2}]);
1698

            
1699
$where = $dbi->where
1700
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
1701
             ->param({key1 => 1, key2 => 2});
1702
$result = $dbi->select(
1703
    table => 'table1',
1704
    where => $where
1705
);
1706
$row = $result->all;
1707
is_deeply($row, [{key1 => 1, key2 => 2}]);
1708

            
1709
$where = $dbi->where
1710
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
1711
             ->param({});
1712
$result = $dbi->select(
1713
    table => 'table1',
1714
    where => $where,
1715
);
1716
$row = $result->all;
1717
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1718

            
1719
$where = $dbi->where
1720
             ->clause(['and', ['or', 'key1 > :key1', 'key1 < :key1'], 'key2 = :key2'])
1721
             ->param({key1 => [0, 3], key2 => 2});
1722
$result = $dbi->select(
1723
    table => 'table1',
1724
    where => $where,
1725
); 
1726
$row = $result->all;
1727
is_deeply($row, [{key1 => 1, key2 => 2}]);
1728

            
1729
$where = $dbi->where;
1730
$result = $dbi->select(
1731
    table => 'table1',
1732
    where => $where
1733
);
1734
$row = $result->all;
1735
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1736

            
1737
eval {
1738
$where = $dbi->where
1739
             ->clause(['uuu']);
1740
$result = $dbi->select(
1741
    table => 'table1',
1742
    where => $where
1743
);
1744
};
1745
ok($@);
1746

            
1747
$where = $dbi->where;
1748
is("$where", '');
1749

            
1750
$where = $dbi->where
1751
             ->clause(['or', ('key1 = :key1') x 2])
1752
             ->param({key1 => [1, 3]});
1753
$result = $dbi->select(
1754
    table => 'table1',
1755
    where => $where,
1756
);
1757
$row = $result->all;
1758
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1759

            
1760
$where = $dbi->where
1761
             ->clause(['or', ('key1 = :key1') x 2])
1762
             ->param({key1 => [1]});
1763
$result = $dbi->select(
1764
    table => 'table1',
1765
    where => $where,
1766
);
1767
$row = $result->all;
1768
is_deeply($row, [{key1 => 1, key2 => 2}]);
1769

            
1770
$where = $dbi->where
1771
             ->clause(['or', ('key1 = :key1') x 2])
1772
             ->param({key1 => 1});
1773
$result = $dbi->select(
1774
    table => 'table1',
1775
    where => $where,
1776
);
1777
$row = $result->all;
1778
is_deeply($row, [{key1 => 1, key2 => 2}]);
1779

            
1780
$where = $dbi->where
1781
             ->clause('key1 = :key1')
1782
             ->param({key1 => 1});
1783
$result = $dbi->select(
1784
    table => 'table1',
1785
    where => $where,
1786
);
1787
$row = $result->all;
1788
is_deeply($row, [{key1 => 1, key2 => 2}]);
1789

            
1790
$where = $dbi->where
1791
             ->clause('key1 = :key1 key2 = :key2')
1792
             ->param({key1 => 1});
1793
eval{$where->to_string};
1794
like($@, qr/one column/);
1795

            
1796
$where = $dbi->where
1797
             ->clause(['or', ('key1 = :key1') x 3])
1798
             ->param({key1 => [$dbi->not_exists, 1, 3]});
1799
$result = $dbi->select(
1800
    table => 'table1',
1801
    where => $where,
1802
);
1803
$row = $result->all;
1804
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1805

            
1806
$where = $dbi->where
1807
             ->clause(['or', ('key1 = :key1') x 3])
1808
             ->param({key1 => [1, $dbi->not_exists, 3]});
1809
$result = $dbi->select(
1810
    table => 'table1',
1811
    where => $where,
1812
);
1813
$row = $result->all;
1814
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1815

            
1816
$where = $dbi->where
1817
             ->clause(['or', ('key1 = :key1') x 3])
1818
             ->param({key1 => [1, 3, $dbi->not_exists]});
1819
$result = $dbi->select(
1820
    table => 'table1',
1821
    where => $where,
1822
);
1823
$row = $result->all;
1824
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1825

            
1826
$where = $dbi->where
1827
             ->clause(['or', ('key1 = :key1') x 3])
1828
             ->param({key1 => [1, $dbi->not_exists, $dbi->not_exists]});
1829
$result = $dbi->select(
1830
    table => 'table1',
1831
    where => $where,
1832
);
1833
$row = $result->all;
1834
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1835

            
1836
$where = $dbi->where
1837
             ->clause(['or', ('key1 = :key1') x 3])
1838
             ->param({key1 => [$dbi->not_exists, 1, $dbi->not_exists]});
1839
$result = $dbi->select(
1840
    table => 'table1',
1841
    where => $where,
1842
);
1843
$row = $result->all;
1844
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1845

            
1846
$where = $dbi->where
1847
             ->clause(['or', ('key1 = :key1') x 3])
1848
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists, 1]});
1849
$result = $dbi->select(
1850
    table => 'table1',
1851
    where => $where,
1852
);
1853
$row = $result->all;
1854
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1855

            
1856
$where = $dbi->where
1857
             ->clause(['or', ('key1 = :key1') x 3])
1858
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists, $dbi->not_exists]});
1859
$result = $dbi->select(
1860
    table => 'table1',
1861
    where => $where,
1862
);
1863
$row = $result->all;
1864
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1865

            
1866
$where = $dbi->where
1867
             ->clause(['or', ('key1 = :key1') x 3])
1868
             ->param({key1 => []});
1869
$result = $dbi->select(
1870
    table => 'table1',
1871
    where => $where,
1872
);
1873
$row = $result->all;
1874
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1875

            
1876
$where = $dbi->where
1877
             ->clause(['and', '{> key1}', '{< key1}' ])
1878
             ->param({key1 => [2, $dbi->not_exists]});
1879
$result = $dbi->select(
1880
    table => 'table1',
1881
    where => $where,
1882
);
1883
$row = $result->all;
1884
is_deeply($row, [{key1 => 3, key2 => 4}], 'not_exists');
1885

            
1886
$where = $dbi->where
1887
             ->clause(['and', '{> key1}', '{< key1}' ])
1888
             ->param({key1 => [$dbi->not_exists, 2]});
1889
$result = $dbi->select(
1890
    table => 'table1',
1891
    where => $where,
1892
);
1893
$row = $result->all;
1894
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1895

            
1896
$where = $dbi->where
1897
             ->clause(['and', '{> key1}', '{< key1}' ])
1898
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists]});
1899
$result = $dbi->select(
1900
    table => 'table1',
1901
    where => $where,
1902
);
1903
$row = $result->all;
1904
is_deeply($row, [{key1 => 1, key2 => 2},{key1 => 3, key2 => 4}], 'not_exists');
1905

            
1906
$where = $dbi->where
1907
             ->clause(['and', '{> key1}', '{< key1}' ])
1908
             ->param({key1 => [0, 2]});
1909
$result = $dbi->select(
1910
    table => 'table1',
1911
    where => $where,
1912
);
1913
$row = $result->all;
1914
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1915

            
1916
$where = $dbi->where
1917
             ->clause(['and', 'key1 is not null', 'key2 is not null' ]);
1918
$result = $dbi->select(
1919
    table => 'table1',
1920
    where => $where,
1921
);
1922
$row = $result->all;
1923
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1924

            
1925
eval {$dbi->where(ppp => 1) };
1926
like($@, qr/invalid/);
1927

            
1928
$where = $dbi->where(
1929
    clause => ['and', ['or'], ['and', 'key1 = :key1', 'key2 = :key2']],
1930
    param => {key1 => 1, key2 => 2}
1931
);
1932
$result = $dbi->select(
1933
    table => 'table1',
1934
    where => $where,
1935
);
1936
$row = $result->all;
1937
is_deeply($row, [{key1 => 1, key2 => 2}]);
1938

            
1939

            
1940
$where = $dbi->where(
1941
    clause => ['and', ['or'], ['or', ':key1', ':key2']],
1942
    param => {}
1943
);
1944
$result = $dbi->select(
1945
    table => 'table1',
1946
    where => $where,
1947
);
1948
$row = $result->all;
1949
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1950

            
1951
$where = $dbi->where;
1952
$where->clause(['and', ':key1{=}']);
1953
$where->param({key1 => undef});
1954
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1955
$row = $result->all;
1956
is_deeply($row, [{key1 => 1, key2 => 2}]);
1957

            
1958
$where = $dbi->where;
1959
$where->clause(['and', ':key1{=}']);
1960
$where->param({key1 => undef});
1961
$where->if('defined');
1962
$where->map;
1963
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1964
$row = $result->all;
1965
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1966

            
1967
$where = $dbi->where;
1968
$where->clause(['or', ':key1{=}', ':key1{=}']);
1969
$where->param({key1 => [undef, undef]});
1970
$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]});
1971
$row = $result->all;
1972
is_deeply($row, [{key1 => 1, key2 => 2}]);
1973
$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]});
1974
$row = $result->all;
1975
is_deeply($row, [{key1 => 1, key2 => 2}]);
1976

            
1977
$where = $dbi->where;
1978
$where->clause(['and', ':key1{=}']);
1979
$where->param({key1 => [undef, undef]});
1980
$where->if('defined');
1981
$where->map;
1982
$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]});
1983
$row = $result->all;
1984
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1985
$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]});
1986
$row = $result->all;
1987
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1988

            
1989
$where = $dbi->where;
1990
$where->clause(['and', ':key1{=}']);
1991
$where->param({key1 => 0});
1992
$where->if('length');
1993
$where->map;
1994
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1995
$row = $result->all;
1996
is_deeply($row, [{key1 => 1, key2 => 2}]);
1997

            
1998
$where = $dbi->where;
1999
$where->clause(['and', ':key1{=}']);
2000
$where->param({key1 => ''});
2001
$where->if('length');
2002
$where->map;
2003
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
2004
$row = $result->all;
2005
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
2006

            
2007
$where = $dbi->where;
2008
$where->clause(['and', ':key1{=}']);
2009
$where->param({key1 => 5});
2010
$where->if(sub { ($_[0] || '') eq 5 });
2011
$where->map;
2012
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
2013
$row = $result->all;
2014
is_deeply($row, [{key1 => 1, key2 => 2}]);
2015

            
2016
$where = $dbi->where;
2017
$where->clause(['and', ':key1{=}']);
2018
$where->param({key1 => 7});
2019
$where->if(sub { ($_[0] || '') eq 5 });
2020
$where->map;
2021
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
2022
$row = $result->all;
2023
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
2024

            
2025
$where = $dbi->where;
2026
$where->param({id => 1, author => 'Ken', price => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
2027
$where->map(id => 'table1.id',
2028
    author => ['table1.author', sub { '%' . $_[0] . '%' }],
2029
    price => ['table1.price', {if => sub { $_[0] eq 1900 }}]
test cleanup
Yuki Kimoto authored on 2011-08-10
2030
);
test cleanup
Yuki Kimoto authored on 2011-08-10
2031
is_deeply($where->param, {'table1.id' => 1, 'table1.author' => '%Ken%',
2032
  'table1.price' => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
2033

            
2034
$where = $dbi->where;
2035
$where->param({id => 0, author => 0, price => 0});
2036
$where->map(
test cleanup
Yuki Kimoto authored on 2011-08-10
2037
    id => 'table1.id',
2038
    author => ['table1.author', sub { '%' . $_[0] . '%' }],
2039
    price => ['table1.price', sub { '%' . $_[0] . '%' },
test cleanup
Yuki Kimoto authored on 2011-08-10
2040
      {if => sub { $_[0] eq 0 }}]
2041
);
test cleanup
Yuki Kimoto authored on 2011-08-10
2042
is_deeply($where->param, {'table1.id' => 0, 'table1.author' => '%0%', 'table1.price' => '%0%'});
test cleanup
Yuki Kimoto authored on 2011-08-10
2043

            
2044
$where = $dbi->where;
2045
$where->param({id => '', author => '', price => ''});
2046
$where->if('length');
2047
$where->map(
test cleanup
Yuki Kimoto authored on 2011-08-10
2048
    id => 'table1.id',
2049
    author => ['table1.author', sub { '%' . $_[0] . '%' }],
2050
    price => ['table1.price', sub { '%' . $_[0] . '%' },
test cleanup
Yuki Kimoto authored on 2011-08-10
2051
      {if => sub { $_[0] eq 1 }}]
2052
);
2053
is_deeply($where->param, {});
2054

            
2055
$where = $dbi->where;
2056
$where->param({id => undef, author => undef, price => undef});
2057
$where->if('length');
2058
$where->map(
test cleanup
Yuki Kimoto authored on 2011-08-10
2059
    id => 'table1.id',
2060
    price => ['table1.price', {if => 'exists'}]
test cleanup
Yuki Kimoto authored on 2011-08-10
2061
);
test cleanup
Yuki Kimoto authored on 2011-08-10
2062
is_deeply($where->param, {'table1.price' => undef});
test cleanup
Yuki Kimoto authored on 2011-08-10
2063

            
2064
$where = $dbi->where;
2065
$where->param({price => 'a'});
2066
$where->if('length');
2067
$where->map(
test cleanup
Yuki Kimoto authored on 2011-08-10
2068
    id => ['table1.id', {if => 'exists'}],
2069
    price => ['table1.price', sub { '%' . $_[0] }, {if => 'exists'}]
test cleanup
Yuki Kimoto authored on 2011-08-10
2070
);
test cleanup
Yuki Kimoto authored on 2011-08-10
2071
is_deeply($where->param, {'table1.price' => '%a'});
test cleanup
Yuki Kimoto authored on 2011-08-10
2072

            
2073
$where = $dbi->where;
2074
$where->param({id => [1, 2], author => 'Ken', price => 1900});
2075
$where->map(
test cleanup
Yuki Kimoto authored on 2011-08-10
2076
    id => 'table1.id',
2077
    author => ['table1.author', sub { '%' . $_[0] . '%' }],
2078
    price => ['table1.price', {if => sub { $_[0] eq 1900 }}]
test cleanup
Yuki Kimoto authored on 2011-08-10
2079
);
test cleanup
Yuki Kimoto authored on 2011-08-10
2080
is_deeply($where->param, {'table1.id' => [1, 2], 'table1.author' => '%Ken%',
2081
  'table1.price' => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
2082

            
2083
$where = $dbi->where;
2084
$where->if('length');
2085
$where->param({id => ['', ''], author => 'Ken', price => 1900});
2086
$where->map(
test cleanup
Yuki Kimoto authored on 2011-08-10
2087
    id => 'table1.id',
2088
    author => ['table1.author', sub { '%' . $_[0] . '%' }],
2089
    price => ['table1.price', {if => sub { $_[0] eq 1900 }}]
test cleanup
Yuki Kimoto authored on 2011-08-10
2090
);
test cleanup
Yuki Kimoto authored on 2011-08-10
2091
is_deeply($where->param, {'table1.id' => [$dbi->not_exists, $dbi->not_exists], 'table1.author' => '%Ken%',
2092
  'table1.price' => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
2093

            
2094
$where = $dbi->where;
2095
$where->param({id => ['', ''], author => 'Ken', price => 1900});
2096
$where->map(
test cleanup
Yuki Kimoto authored on 2011-08-10
2097
    id => ['table1.id', {if => 'length'}],
2098
    author => ['table1.author', sub { '%' . $_[0] . '%' }, {if => 'defined'}],
2099
    price => ['table1.price', {if => sub { $_[0] eq 1900 }}]
test cleanup
Yuki Kimoto authored on 2011-08-10
2100
);
test cleanup
Yuki Kimoto authored on 2011-08-10
2101
is_deeply($where->param, {'table1.id' => [$dbi->not_exists, $dbi->not_exists], 'table1.author' => '%Ken%',
2102
  'table1.price' => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
2103

            
2104
test 'dbi_option default';
2105
$dbi = DBIx::Custom->new;
2106
is_deeply($dbi->dbi_option, {});
2107

            
2108
test 'register_tag_processor';
2109
$dbi = DBIx::Custom->connect;
2110
$dbi->register_tag_processor(
2111
    a => sub { 1 }
2112
);
test cleanup
Yuki Kimoto authored on 2011-08-10
2113
is($dbi->{_tags}->{a}->(), 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2114

            
2115
test 'register_tag';
2116
$dbi = DBIx::Custom->connect;
2117
$dbi->register_tag(
2118
    b => sub { 2 }
2119
);
test cleanup
Yuki Kimoto authored on 2011-08-10
2120
is($dbi->{_tags}->{b}->(), 2);
test cleanup
Yuki Kimoto authored on 2011-08-10
2121

            
2122
test 'table not specify exception';
2123
$dbi = DBIx::Custom->connect;
2124
eval {$dbi->insert};
2125
like($@, qr/table/);
2126
eval {$dbi->update};
2127
like($@, qr/table/);
2128
eval {$dbi->delete};
2129
like($@, qr/table/);
2130
eval {$dbi->select};
2131
like($@, qr/table/);
test cleanup
Yuki Kimoto authored on 2011-08-10
2132

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2133
test 'more tests';
2134
$dbi = DBIx::Custom->connect;
2135
eval{$dbi->apply_filter('table', 'column', [])};
2136
like($@, qr/apply_filter/);
2137

            
2138
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
2139
like($@, qr/apply_filter/);
2140

            
2141
$dbi->apply_filter(
2142

            
2143
);
2144
$dbi = DBIx::Custom->connect;
2145
eval { $dbi->execute('drop table table1') };
2146
$dbi->execute($create_table1);
2147
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2148
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
2149
$dbi->apply_filter('table1', 'key2', 
2150
                   {in => sub { $_[0] * 3 }, out => sub { $_[0] * 2 }});
2151
$rows = $dbi->select(table => 'table1', where => {key2 => 1})->all;
2152
is_deeply($rows, [{key1 => 1, key2 => 6}]);
2153

            
2154
$dbi = DBIx::Custom->connect;
2155
eval { $dbi->execute('drop table table1') };
2156
$dbi->execute($create_table1);
2157
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2158
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
2159
$dbi->apply_filter('table1', 'key2', {});
2160
$rows = $dbi->select(table => 'table1', where => {key2 => 2})->all;
2161
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2162

            
2163
$dbi = DBIx::Custom->connect;
2164
eval {$dbi->apply_filter('table1', 'key2', {out => 'no'})};
2165
like($@, qr/not registered/);
2166
eval {$dbi->apply_filter('table1', 'key2', {in => 'no'})};
2167
like($@, qr/not registered/);
2168
$dbi->method({one => sub { 1 }});
2169
is($dbi->one, 1);
2170

            
2171
eval{DBIx::Custom->connect(dsn => undef)};
2172
like($@, qr/_connect/);
2173

            
2174
$dbi = DBIx::Custom->connect;
2175
eval { $dbi->execute('drop table table1') };
2176
$dbi->execute($create_table1);
2177
$dbi->register_filter(twice => sub { $_[0] * 2 });
2178
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
2179
             filter => {key1 => 'twice'});
2180
$row = $dbi->select(table => 'table1')->one;
2181
is_deeply($row, {key1 => 2, key2 => 2});
2182
eval {$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
2183
             filter => {key1 => 'no'}) };
2184
like($@, qr//);
2185

            
2186
$dbi->register_filter(one => sub { });
2187
$dbi->default_fetch_filter('one');
2188
ok($dbi->default_fetch_filter);
2189
$dbi->default_bind_filter('one');
2190
ok($dbi->default_bind_filter);
2191
eval{$dbi->default_fetch_filter('no')};
2192
like($@, qr/not registered/);
2193
eval{$dbi->default_bind_filter('no')};
2194
like($@, qr/not registered/);
2195
$dbi->default_bind_filter(undef);
2196
ok(!defined $dbi->default_bind_filter);
2197
$dbi->default_fetch_filter(undef);
2198
ok(!defined $dbi->default_fetch_filter);
2199
eval {$dbi->execute('select * from table1 {} {= author') };
2200
like($@, qr/Tag not finished/);
2201

            
2202
$dbi = DBIx::Custom->connect;
2203
eval { $dbi->execute('drop table table1') };
2204
$dbi->execute($create_table1);
2205
$dbi->register_filter(one => sub { 1 });
2206
$result = $dbi->select(table => 'table1');
2207
eval {$result->filter(key1 => 'no')};
2208
like($@, qr/not registered/);
2209
eval {$result->end_filter(key1 => 'no')};
2210
like($@, qr/not registered/);
2211
$result->default_filter(undef);
2212
ok(!defined $result->default_filter);
2213
$result->default_filter('one');
2214
is($result->default_filter->(), 1);
2215

            
2216
test 'dbi_option';
2217
$dbi = DBIx::Custom->connect(dbi_option => {PrintError => 1});
2218
ok($dbi->dbh->{PrintError});
2219
$dbi = DBIx::Custom->connect(dbi_options => {PrintError => 1});
2220
ok($dbi->dbh->{PrintError});
2221

            
2222
test 'DBIx::Custom::Result stash()';
2223
$result = DBIx::Custom::Result->new;
2224
is_deeply($result->stash, {}, 'default');
2225
$result->stash->{foo} = 1;
2226
is($result->stash->{foo}, 1, 'get and set');
test cleanup
Yuki Kimoto authored on 2011-08-10
2227

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2228
test 'delete_at';
2229
$dbi = DBIx::Custom->connect;
2230
eval { $dbi->execute('drop table table1') };
2231
$dbi->execute($create_table1_2);
2232
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2233
$dbi->delete_at(
2234
    table => 'table1',
2235
    primary_key => ['key1', 'key2'],
2236
    where => [1, 2],
2237
);
2238
is_deeply($dbi->select(table => 'table1')->all, []);
2239

            
2240
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2241
$dbi->delete_at(
2242
    table => 'table1',
2243
    primary_key => 'key1',
2244
    where => 1,
2245
);
2246
is_deeply($dbi->select(table => 'table1')->all, []);
2247

            
2248
test 'insert_at';
2249
$dbi = DBIx::Custom->connect;
2250
eval { $dbi->execute('drop table table1') };
2251
$dbi->execute($create_table1_2);
2252
$dbi->insert_at(
2253
    primary_key => ['key1', 'key2'], 
2254
    table => 'table1',
2255
    where => [1, 2],
2256
    param => {key3 => 3}
2257
);
2258
is($dbi->select(table => 'table1')->one->{key1}, 1);
2259
is($dbi->select(table => 'table1')->one->{key2}, 2);
2260
is($dbi->select(table => 'table1')->one->{key3}, 3);
2261

            
2262
$dbi->delete_all(table => 'table1');
2263
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2264
$dbi->insert_at(
2265
    primary_key => 'key1', 
2266
    table => 'table1',
2267
    where => 1,
2268
    param => {key2 => 2, key3 => 3}
2269
);
2270

            
2271
is($dbi->select(table => 'table1')->one->{key1}, 1);
2272
is($dbi->select(table => 'table1')->one->{key2}, 2);
2273
is($dbi->select(table => 'table1')->one->{key3}, 3);
2274

            
2275
eval {
2276
    $dbi->insert_at(
2277
        table => 'table1',
2278
        primary_key => ['key1', 'key2'],
2279
        where => {},
2280
        param => {key1 => 1, key2 => 2, key3 => 3},
2281
    );
2282
};
2283
like($@, qr/must be/);
2284

            
2285
$dbi = DBIx::Custom->connect;
2286
eval { $dbi->execute('drop table table1') };
2287
$dbi->execute($create_table1_2);
2288
$dbi->insert_at(
2289
    {key3 => 3},
2290
    primary_key => ['key1', 'key2'], 
2291
    table => 'table1',
2292
    where => [1, 2],
2293
);
2294
is($dbi->select(table => 'table1')->one->{key1}, 1);
2295
is($dbi->select(table => 'table1')->one->{key2}, 2);
2296
is($dbi->select(table => 'table1')->one->{key3}, 3);
2297

            
2298
test 'update_at';
2299
$dbi = DBIx::Custom->connect;
2300
eval { $dbi->execute('drop table table1') };
2301
$dbi->execute($create_table1_2);
2302
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2303
$dbi->update_at(
2304
    table => 'table1',
2305
    primary_key => ['key1', 'key2'],
2306
    where => [1, 2],
2307
    param => {key3 => 4}
2308
);
2309
is($dbi->select(table => 'table1')->one->{key1}, 1);
2310
is($dbi->select(table => 'table1')->one->{key2}, 2);
2311
is($dbi->select(table => 'table1')->one->{key3}, 4);
2312

            
2313
$dbi->delete_all(table => 'table1');
2314
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2315
$dbi->update_at(
2316
    table => 'table1',
2317
    primary_key => 'key1',
2318
    where => 1,
2319
    param => {key3 => 4}
2320
);
2321
is($dbi->select(table => 'table1')->one->{key1}, 1);
2322
is($dbi->select(table => 'table1')->one->{key2}, 2);
2323
is($dbi->select(table => 'table1')->one->{key3}, 4);
2324

            
2325
$dbi = DBIx::Custom->connect;
2326
eval { $dbi->execute('drop table table1') };
2327
$dbi->execute($create_table1_2);
2328
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2329
$dbi->update_at(
2330
    {key3 => 4},
2331
    table => 'table1',
2332
    primary_key => ['key1', 'key2'],
2333
    where => [1, 2]
2334
);
2335
is($dbi->select(table => 'table1')->one->{key1}, 1);
2336
is($dbi->select(table => 'table1')->one->{key2}, 2);
2337
is($dbi->select(table => 'table1')->one->{key3}, 4);
2338

            
2339
test 'select_at';
2340
$dbi = DBIx::Custom->connect;
2341
eval { $dbi->execute('drop table table1') };
2342
$dbi->execute($create_table1_2);
2343
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2344
$result = $dbi->select_at(
2345
    table => 'table1',
2346
    primary_key => ['key1', 'key2'],
2347
    where => [1, 2]
2348
);
2349
$row = $result->one;
2350
is($row->{key1}, 1);
2351
is($row->{key2}, 2);
2352
is($row->{key3}, 3);
2353

            
2354
$dbi->delete_all(table => 'table1');
2355
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2356
$result = $dbi->select_at(
2357
    table => 'table1',
2358
    primary_key => 'key1',
2359
    where => 1,
2360
);
2361
$row = $result->one;
2362
is($row->{key1}, 1);
2363
is($row->{key2}, 2);
2364
is($row->{key3}, 3);
2365

            
2366
$dbi->delete_all(table => 'table1');
2367
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2368
$result = $dbi->select_at(
2369
    table => 'table1',
2370
    primary_key => ['key1', 'key2'],
2371
    where => [1, 2]
2372
);
2373
$row = $result->one;
2374
is($row->{key1}, 1);
2375
is($row->{key2}, 2);
2376
is($row->{key3}, 3);
2377

            
2378
eval {
2379
    $result = $dbi->select_at(
2380
        table => 'table1',
2381
        primary_key => ['key1', 'key2'],
2382
        where => {},
2383
    );
2384
};
2385
like($@, qr/must be/);
2386

            
2387
eval {
2388
    $result = $dbi->select_at(
2389
        table => 'table1',
2390
        primary_key => ['key1', 'key2'],
2391
        where => [1],
2392
    );
2393
};
2394
like($@, qr/same/);
2395

            
2396
eval {
2397
    $result = $dbi->update_at(
2398
        table => 'table1',
2399
        primary_key => ['key1', 'key2'],
2400
        where => {},
2401
        param => {key1 => 1, key2 => 2},
2402
    );
2403
};
2404
like($@, qr/must be/);
2405

            
2406
eval {
2407
    $result = $dbi->delete_at(
2408
        table => 'table1',
2409
        primary_key => ['key1', 'key2'],
2410
        where => {},
2411
    );
2412
};
2413
like($@, qr/must be/);
2414

            
2415
test 'columns';
2416
use MyDBI1;
2417
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2418
$model = $dbi->model('table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2419

            
2420

            
2421
test 'model delete_at';
2422
$dbi = MyDBI6->connect;
2423
eval { $dbi->execute('drop table table1') };
2424
eval { $dbi->execute('drop table table2') };
2425
eval { $dbi->execute('drop table table3') };
2426
$dbi->execute($create_table1_2);
2427
$dbi->execute($create_table2_2);
2428
$dbi->execute($create_table3);
2429
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2430
$dbi->model('table1')->delete_at(where => [1, 2]);
2431
is_deeply($dbi->select(table => 'table1')->all, []);
2432
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2433
$dbi->model('table1')->delete_at(where => [1, 2]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2434
is_deeply($dbi->select(table => 'table1')->all, []);
2435
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2436
$dbi->model('table3')->delete_at(where => [1, 2]);
2437
is_deeply($dbi->select(table => 'table3')->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
2438

            
2439
test 'model insert_at';
2440
$dbi = MyDBI6->connect;
2441
eval { $dbi->execute('drop table table1') };
2442
$dbi->execute($create_table1_2);
2443
$dbi->model('table1')->insert_at(
2444
    where => [1, 2],
2445
    param => {key3 => 3}
2446
);
2447
$result = $dbi->model('table1')->select;
2448
$row = $result->one;
2449
is($row->{key1}, 1);
2450
is($row->{key2}, 2);
2451
is($row->{key3}, 3);
2452

            
2453
test 'model update_at';
2454
$dbi = MyDBI6->connect;
2455
eval { $dbi->execute('drop table table1') };
2456
$dbi->execute($create_table1_2);
2457
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2458
$dbi->model('table1')->update_at(
2459
    where => [1, 2],
2460
    param => {key3 => 4}
2461
);
2462
$result = $dbi->model('table1')->select;
2463
$row = $result->one;
2464
is($row->{key1}, 1);
2465
is($row->{key2}, 2);
2466
is($row->{key3}, 4);
2467

            
2468
test 'model select_at';
2469
$dbi = MyDBI6->connect;
2470
eval { $dbi->execute('drop table table1') };
2471
$dbi->execute($create_table1_2);
2472
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2473
$result = $dbi->model('table1')->select_at(where => [1, 2]);
2474
$row = $result->one;
2475
is($row->{key1}, 1);
2476
is($row->{key2}, 2);
2477
is($row->{key3}, 3);
2478

            
2479

            
2480
test 'mycolumn and column';
2481
$dbi = MyDBI7->connect;
2482
eval { $dbi->execute('drop table table1') };
2483
eval { $dbi->execute('drop table table2') };
2484
$dbi->execute($create_table1);
2485
$dbi->execute($create_table2);
2486
$dbi->separator('__');
2487
$dbi->setup_model;
2488
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2489
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2490
$model = $dbi->model('table1');
2491
$result = $model->select(
2492
    column => [$model->mycolumn, $model->column('table2')],
2493
    where => {'table1.key1' => 1}
2494
);
2495
is_deeply($result->one,
2496
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2497

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2498
test 'insert_param';
2499
$dbi = DBIx::Custom->connect;
2500
eval { $dbi->execute('drop table table1') };
2501
$dbi->execute($create_table1_2);
2502
$param = {key1 => 1, key2 => 2};
2503
$insert_param = $dbi->insert_param($param);
2504
$sql = <<"EOS";
2505
insert into table1 $insert_param
2506
EOS
2507
$dbi->execute($sql, param => $param, table => 'table1');
2508
is($dbi->select(table => 'table1')->one->{key1}, 1);
2509
is($dbi->select(table => 'table1')->one->{key2}, 2);
2510

            
2511
$dbi = DBIx::Custom->connect;
2512
eval { $dbi->execute('drop table table1') };
2513
$dbi->execute($create_table1_2);
2514
$param = {key1 => 1, key2 => 2};
2515
$insert_param = $dbi->insert_param($param);
2516
$sql = <<"EOS";
2517
insert into table1 $insert_param
2518
EOS
2519
$dbi->execute($sql, param => $param, table => 'table1');
2520
is($dbi->select(table => 'table1')->one->{key1}, 1);
2521
is($dbi->select(table => 'table1')->one->{key2}, 2);
2522

            
2523
eval { $dbi->insert_param({";" => 1}) };
2524
like($@, qr/not safety/);
test cleanup
Yuki Kimoto authored on 2011-08-10
2525

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2526
test 'mycolumn';
2527
$dbi = MyDBI8->connect;
2528
eval { $dbi->execute('drop table table1') };
2529
eval { $dbi->execute('drop table table2') };
2530
$dbi->execute($create_table1);
2531
$dbi->execute($create_table2);
2532
$dbi->setup_model;
2533
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2534
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2535
$model = $dbi->model('table1');
2536
$result = $model->select_at(
2537
    column => [
2538
        $model->mycolumn,
2539
        $model->column('table2')
2540
    ]
2541
);
2542
is_deeply($result->one,
2543
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
2544

            
2545
$result = $model->select_at(
2546
    column => [
2547
        $model->mycolumn(['key1']),
2548
        $model->column(table2 => ['key1'])
2549
    ]
2550
);
2551
is_deeply($result->one,
2552
          {key1 => 1, 'table2.key1' => 1});
2553
$result = $model->select_at(
2554
    column => [
2555
        $model->mycolumn(['key1']),
2556
        {table2 => ['key1']}
2557
    ]
2558
);
2559
is_deeply($result->one,
2560
          {key1 => 1, 'table2.key1' => 1});
2561

            
2562
$result = $model->select_at(
2563
    column => [
2564
        $model->mycolumn(['key1']),
2565
        ['table2.key1', as => 'table2.key1']
2566
    ]
2567
);
2568
is_deeply($result->one,
2569
          {key1 => 1, 'table2.key1' => 1});
2570

            
2571
$result = $model->select_at(
2572
    column => [
2573
        $model->mycolumn(['key1']),
2574
        ['table2.key1' => 'table2.key1']
2575
    ]
2576
);
2577
is_deeply($result->one,
2578
          {key1 => 1, 'table2.key1' => 1});
2579

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2580
test 'merge_param';
2581
$dbi = DBIx::Custom->new;
2582
$params = [
2583
    {key1 => 1, key2 => 2, key3 => 3},
2584
    {key1 => 1, key2 => 2},
2585
    {key1 => 1}
2586
];
2587
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]);
2588
is_deeply($param, {key1 => [1, 1, 1], key2 => [2, 2], key3 => 3});
2589

            
2590
$params = [
2591
    {key1 => [1, 2], key2 => 1, key3 => [1, 2]},
2592
    {key1 => [3, 4], key2 => [2, 3], key3 => 3}
2593
];
2594
$param = $dbi->merge_param($params->[0], $params->[1]);
2595
is_deeply($param, {key1 => [1, 2, 3, 4], key2 => [1, 2, 3], key3 => [1, 2, 3]});
2596

            
2597
test 'select() param option';
2598
$dbi = DBIx::Custom->connect;
2599
eval { $dbi->execute('drop table table1') };
2600
$dbi->execute($create_table1);
2601
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2602
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2603
eval { $dbi->execute('drop table table2') };
2604
$dbi->execute($create_table2);
2605
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2606
$dbi->insert(table => 'table2', param => {key1 => 2, key3 => 5});
2607
$rows = $dbi->select(
2608
    table => 'table1',
2609
    column => 'table1.key1 as table1_key1, key2, key3',
2610
    where   => {'table1.key2' => 3},
2611
    join  => ['inner join (select * from table2 where {= table2.key3})' . 
2612
              ' as table2 on table1.key1 = table2.key1'],
2613
    param => {'table2.key3' => 5}
2614
)->all;
2615
is_deeply($rows, [{table1_key1 => 2, key2 => 3, key3 => 5}]);
2616

            
2617
test 'select() string where';
2618
$dbi = DBIx::Custom->connect;
2619
eval { $dbi->execute('drop table table1') };
2620
$dbi->execute($create_table1);
2621
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2622
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2623
$rows = $dbi->select(
2624
    table => 'table1',
2625
    where => 'key1 = :key1 and key2 = :key2',
2626
    where_param => {key1 => 1, key2 => 2}
2627
)->all;
2628
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2629

            
2630
$dbi = DBIx::Custom->connect;
2631
eval { $dbi->execute('drop table table1') };
2632
$dbi->execute($create_table1);
2633
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2634
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2635
$rows = $dbi->select(
2636
    table => 'table1',
2637
    where => [
2638
        'key1 = :key1 and key2 = :key2',
2639
        {key1 => 1, key2 => 2}
2640
    ]
2641
)->all;
2642
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2643

            
2644
test 'delete() string where';
2645
$dbi = DBIx::Custom->connect;
2646
eval { $dbi->execute('drop table table1') };
2647
$dbi->execute($create_table1);
2648
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2649
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2650
$dbi->delete(
2651
    table => 'table1',
2652
    where => 'key1 = :key1 and key2 = :key2',
2653
    where_param => {key1 => 1, key2 => 2}
2654
);
2655
$rows = $dbi->select(table => 'table1')->all;
2656
is_deeply($rows, [{key1 => 2, key2 => 3}]);
2657

            
2658
$dbi = DBIx::Custom->connect;
2659
eval { $dbi->execute('drop table table1') };
2660
$dbi->execute($create_table1);
2661
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2662
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2663
$dbi->delete(
2664
    table => 'table1',
2665
    where => [
2666
        'key1 = :key1 and key2 = :key2',
2667
         {key1 => 1, key2 => 2}
2668
    ]
2669
);
2670
$rows = $dbi->select(table => 'table1')->all;
2671
is_deeply($rows, [{key1 => 2, key2 => 3}]);
2672

            
2673

            
2674
test 'update() string where';
2675
$dbi = DBIx::Custom->connect;
2676
eval { $dbi->execute('drop table table1') };
2677
$dbi->execute($create_table1);
2678
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2679
$dbi->update(
2680
    table => 'table1',
2681
    param => {key1 => 5},
2682
    where => 'key1 = :key1 and key2 = :key2',
2683
    where_param => {key1 => 1, key2 => 2}
2684
);
2685
$rows = $dbi->select(table => 'table1')->all;
2686
is_deeply($rows, [{key1 => 5, key2 => 2}]);
2687

            
2688
$dbi = DBIx::Custom->connect;
2689
eval { $dbi->execute('drop table table1') };
2690
$dbi->execute($create_table1);
2691
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2692
$dbi->update(
2693
    table => 'table1',
2694
    param => {key1 => 5},
2695
    where => [
2696
        'key1 = :key1 and key2 = :key2',
2697
        {key1 => 1, key2 => 2}
2698
    ]
2699
);
2700
$rows = $dbi->select(table => 'table1')->all;
2701
is_deeply($rows, [{key1 => 5, key2 => 2}]);
2702

            
2703
test 'insert id and primary_key option';
2704
$dbi = DBIx::Custom->connect;
2705
eval { $dbi->execute('drop table table1') };
2706
$dbi->execute($create_table1_2);
2707
$dbi->insert(
2708
    primary_key => ['key1', 'key2'], 
2709
    table => 'table1',
2710
    id => [1, 2],
2711
    param => {key3 => 3}
2712
);
2713
is($dbi->select(table => 'table1')->one->{key1}, 1);
2714
is($dbi->select(table => 'table1')->one->{key2}, 2);
2715
is($dbi->select(table => 'table1')->one->{key3}, 3);
2716

            
2717
$dbi->delete_all(table => 'table1');
2718
$dbi->insert(
2719
    primary_key => 'key1', 
2720
    table => 'table1',
2721
    id => 0,
2722
    param => {key2 => 2, key3 => 3}
2723
);
2724

            
2725
is($dbi->select(table => 'table1')->one->{key1}, 0);
2726
is($dbi->select(table => 'table1')->one->{key2}, 2);
2727
is($dbi->select(table => 'table1')->one->{key3}, 3);
2728

            
2729
$dbi = DBIx::Custom->connect;
2730
eval { $dbi->execute('drop table table1') };
2731
$dbi->execute($create_table1_2);
2732
$dbi->insert(
2733
    {key3 => 3},
2734
    primary_key => ['key1', 'key2'], 
2735
    table => 'table1',
2736
    id => [1, 2],
2737
);
2738
is($dbi->select(table => 'table1')->one->{key1}, 1);
2739
is($dbi->select(table => 'table1')->one->{key2}, 2);
2740
is($dbi->select(table => 'table1')->one->{key3}, 3);
test cleanup
Yuki Kimoto authored on 2011-08-10
2741

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2742
test 'model insert id and primary_key option';
2743
$dbi = MyDBI6->connect;
2744
eval { $dbi->execute('drop table table1') };
2745
$dbi->execute($create_table1_2);
2746
$dbi->model('table1')->insert(
2747
    id => [1, 2],
2748
    param => {key3 => 3}
2749
);
2750
$result = $dbi->model('table1')->select;
2751
$row = $result->one;
2752
is($row->{key1}, 1);
2753
is($row->{key2}, 2);
2754
is($row->{key3}, 3);
2755

            
2756
$dbi = MyDBI6->connect;
2757
eval { $dbi->execute('drop table table1') };
2758
$dbi->execute($create_table1_2);
2759
$dbi->model('table1')->insert(
2760
    {key3 => 3},
2761
    id => [1, 2]
2762
);
2763
$result = $dbi->model('table1')->select;
2764
$row = $result->one;
2765
is($row->{key1}, 1);
2766
is($row->{key2}, 2);
2767
is($row->{key3}, 3);
2768

            
2769
test 'update and id option';
2770
$dbi = DBIx::Custom->connect;
2771
eval { $dbi->execute('drop table table1') };
2772
$dbi->execute($create_table1_2);
2773
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2774
$dbi->update(
2775
    table => 'table1',
2776
    primary_key => ['key1', 'key2'],
2777
    id => [1, 2],
2778
    param => {key3 => 4}
2779
);
2780
is($dbi->select(table => 'table1')->one->{key1}, 1);
2781
is($dbi->select(table => 'table1')->one->{key2}, 2);
2782
is($dbi->select(table => 'table1')->one->{key3}, 4);
2783

            
2784
$dbi->delete_all(table => 'table1');
2785
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2786
$dbi->update(
2787
    table => 'table1',
2788
    primary_key => 'key1',
2789
    id => 0,
2790
    param => {key3 => 4}
2791
);
2792
is($dbi->select(table => 'table1')->one->{key1}, 0);
2793
is($dbi->select(table => 'table1')->one->{key2}, 2);
2794
is($dbi->select(table => 'table1')->one->{key3}, 4);
2795

            
2796
$dbi = DBIx::Custom->connect;
2797
eval { $dbi->execute('drop table table1') };
2798
$dbi->execute($create_table1_2);
2799
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2800
$dbi->update(
2801
    {key3 => 4},
2802
    table => 'table1',
2803
    primary_key => ['key1', 'key2'],
2804
    id => [1, 2]
2805
);
2806
is($dbi->select(table => 'table1')->one->{key1}, 1);
2807
is($dbi->select(table => 'table1')->one->{key2}, 2);
2808
is($dbi->select(table => 'table1')->one->{key3}, 4);
2809

            
2810

            
2811
test 'model update and id option';
2812
$dbi = MyDBI6->connect;
2813
eval { $dbi->execute('drop table table1') };
2814
$dbi->execute($create_table1_2);
2815
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2816
$dbi->model('table1')->update(
2817
    id => [1, 2],
2818
    param => {key3 => 4}
2819
);
2820
$result = $dbi->model('table1')->select;
2821
$row = $result->one;
2822
is($row->{key1}, 1);
2823
is($row->{key2}, 2);
2824
is($row->{key3}, 4);
2825

            
2826

            
2827
test 'delete and id option';
2828
$dbi = DBIx::Custom->connect;
2829
eval { $dbi->execute('drop table table1') };
2830
$dbi->execute($create_table1_2);
2831
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2832
$dbi->delete(
2833
    table => 'table1',
2834
    primary_key => ['key1', 'key2'],
2835
    id => [1, 2],
2836
);
2837
is_deeply($dbi->select(table => 'table1')->all, []);
2838

            
2839
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2840
$dbi->delete(
2841
    table => 'table1',
2842
    primary_key => 'key1',
2843
    id => 0,
2844
);
2845
is_deeply($dbi->select(table => 'table1')->all, []);
2846

            
2847

            
2848
test 'model delete and id option';
2849
$dbi = MyDBI6->connect;
2850
eval { $dbi->execute('drop table table1') };
2851
eval { $dbi->execute('drop table table2') };
2852
eval { $dbi->execute('drop table table3') };
2853
$dbi->execute($create_table1_2);
2854
$dbi->execute($create_table2_2);
2855
$dbi->execute($create_table3);
2856
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2857
$dbi->model('table1')->delete(id => [1, 2]);
2858
is_deeply($dbi->select(table => 'table1')->all, []);
2859
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2860
$dbi->model('table1')->delete(id => [1, 2]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2861
is_deeply($dbi->select(table => 'table1')->all, []);
2862
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2863
$dbi->model('table3')->delete(id => [1, 2]);
2864
is_deeply($dbi->select(table => 'table3')->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
2865

            
2866

            
2867
test 'select and id option';
2868
$dbi = DBIx::Custom->connect;
2869
eval { $dbi->execute('drop table table1') };
2870
$dbi->execute($create_table1_2);
2871
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2872
$result = $dbi->select(
2873
    table => 'table1',
2874
    primary_key => ['key1', 'key2'],
2875
    id => [1, 2]
2876
);
2877
$row = $result->one;
2878
is($row->{key1}, 1);
2879
is($row->{key2}, 2);
2880
is($row->{key3}, 3);
2881

            
2882
$dbi->delete_all(table => 'table1');
2883
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2884
$result = $dbi->select(
2885
    table => 'table1',
2886
    primary_key => 'key1',
2887
    id => 0,
2888
);
2889
$row = $result->one;
2890
is($row->{key1}, 0);
2891
is($row->{key2}, 2);
2892
is($row->{key3}, 3);
2893

            
2894
$dbi->delete_all(table => 'table1');
2895
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2896
$result = $dbi->select(
2897
    table => 'table1',
2898
    primary_key => ['key1', 'key2'],
2899
    id => [1, 2]
2900
);
2901
$row = $result->one;
2902
is($row->{key1}, 1);
2903
is($row->{key2}, 2);
2904
is($row->{key3}, 3);
2905

            
2906

            
2907
test 'model select_at';
2908
$dbi = MyDBI6->connect;
2909
eval { $dbi->execute('drop table table1') };
2910
$dbi->execute($create_table1_2);
2911
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2912
$result = $dbi->model('table1')->select(id => [1, 2]);
2913
$row = $result->one;
2914
is($row->{key1}, 1);
2915
is($row->{key2}, 2);
2916
is($row->{key3}, 3);
2917

            
2918
test 'column separator is default .';
2919
$dbi = MyDBI7->connect;
2920
eval { $dbi->execute('drop table table1') };
2921
eval { $dbi->execute('drop table table2') };
2922
$dbi->execute($create_table1);
2923
$dbi->execute($create_table2);
2924
$dbi->setup_model;
2925
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2926
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2927
$model = $dbi->model('table1');
2928
$result = $model->select(
2929
    column => [$model->column('table2')],
2930
    where => {'table1.key1' => 1}
2931
);
2932
is_deeply($result->one,
2933
          {'table2.key1' => 1, 'table2.key3' => 3});
2934

            
2935
$result = $model->select(
2936
    column => [$model->column('table2' => [qw/key1 key3/])],
2937
    where => {'table1.key1' => 1}
2938
);
2939
is_deeply($result->one,
2940
          {'table2.key1' => 1, 'table2.key3' => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2941

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2942
test 'separator';
2943
$dbi = DBIx::Custom->connect;
2944
eval { $dbi->execute('drop table table1') };
2945
eval { $dbi->execute('drop table table2') };
2946
$dbi->execute($create_table1);
2947
$dbi->execute($create_table2);
test cleanup
Yuki Kimoto authored on 2011-08-10
2948

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2949
$dbi->create_model(
2950
    table => 'table1',
2951
    join => [
2952
       'left outer join table2 on table1.key1 = table2.key1'
2953
    ],
2954
    primary_key => ['key1'],
2955
);
2956
$model2 = $dbi->create_model(
2957
    table => 'table2',
2958
);
2959
$dbi->setup_model;
2960
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2961
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2962
$model = $dbi->model('table1');
2963
$result = $model->select(
2964
    column => [
2965
        $model->mycolumn,
2966
        {table2 => [qw/key1 key3/]}
2967
    ],
2968
    where => {'table1.key1' => 1}
2969
);
2970
is_deeply($result->one,
2971
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
2972
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2973

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2974
$dbi->separator('__');
2975
$model = $dbi->model('table1');
2976
$result = $model->select(
2977
    column => [
2978
        $model->mycolumn,
2979
        {table2 => [qw/key1 key3/]}
2980
    ],
2981
    where => {'table1.key1' => 1}
2982
);
2983
is_deeply($result->one,
2984
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
2985
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2986

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2987
$dbi->separator('-');
2988
$model = $dbi->model('table1');
2989
$result = $model->select(
2990
    column => [
2991
        $model->mycolumn,
2992
        {table2 => [qw/key1 key3/]}
2993
    ],
2994
    where => {'table1.key1' => 1}
2995
);
2996
is_deeply($result->one,
2997
          {key1 => 1, key2 => 2, 'table2-key1' => 1, 'table2-key3' => 3});
2998
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2999

            
3000

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3001
test 'filter_off';
3002
$dbi = DBIx::Custom->connect;
3003
eval { $dbi->execute('drop table table1') };
3004
eval { $dbi->execute('drop table table2') };
3005
$dbi->execute($create_table1);
3006
$dbi->execute($create_table2);
test cleanup
Yuki Kimoto authored on 2011-08-10
3007

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3008
$dbi->create_model(
3009
    table => 'table1',
3010
    join => [
3011
       'left outer join table2 on table1.key1 = table2.key1'
3012
    ],
3013
    primary_key => ['key1'],
3014
);
3015
$dbi->setup_model;
3016
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3017
$model = $dbi->model('table1');
3018
$result = $model->select(column => 'key1');
3019
$result->filter(key1 => sub { $_[0] * 2 });
3020
is_deeply($result->one, {key1 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
3021

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3022
test 'available_datetype';
3023
$dbi = DBIx::Custom->connect;
3024
ok($dbi->can('available_datatype'));
test cleanup
Yuki Kimoto authored on 2011-08-10
3025

            
3026

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3027
test 'select prefix option';
3028
$dbi = DBIx::Custom->connect;
3029
eval { $dbi->execute('drop table table1') };
3030
$dbi->execute($create_table1);
3031
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3032
$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all;
3033
is_deeply($rows, [{key1 => 1, key2 => 2}], "table");
test cleanup
Yuki Kimoto authored on 2011-08-10
3034

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3035
test 'map_param';
3036
$dbi = DBIx::Custom->connect;
3037
$param = $dbi->map_param(
3038
    {id => 1, author => 'Ken', price => 1900},
test cleanup
Yuki Kimoto authored on 2011-08-10
3039
    id => 'table1.id',
3040
    author => ['table1.author', sub { '%' . $_[0] . '%' }],
3041
    price => ['table1.price', {if => sub { $_[0] eq 1900 }}]
cleanup test
Yuki Kimoto authored on 2011-08-10
3042
);
test cleanup
Yuki Kimoto authored on 2011-08-10
3043
is_deeply($param, {'table1.id' => 1, 'table1.author' => '%Ken%',
3044
  'table1.price' => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
3045

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3046
$param = $dbi->map_param(
3047
    {id => 0, author => 0, price => 0},
test cleanup
Yuki Kimoto authored on 2011-08-10
3048
    id => 'table1.id',
3049
    author => ['table1.author', sub { '%' . $_[0] . '%' }],
3050
    price => ['table1.price', sub { '%' . $_[0] . '%' },
cleanup test
Yuki Kimoto authored on 2011-08-10
3051
      {if => sub { $_[0] eq 0 }}]
3052
);
test cleanup
Yuki Kimoto authored on 2011-08-10
3053
is_deeply($param, {'table1.id' => 0, 'table1.author' => '%0%', 'table1.price' => '%0%'});
test cleanup
Yuki Kimoto authored on 2011-08-10
3054

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3055
$param = $dbi->map_param(
3056
    {id => '', author => '', price => ''},
test cleanup
Yuki Kimoto authored on 2011-08-10
3057
    id => 'table1.id',
3058
    author => ['table1.author', sub { '%' . $_[0] . '%' }],
3059
    price => ['table1.price', sub { '%' . $_[0] . '%' },
cleanup test
Yuki Kimoto authored on 2011-08-10
3060
      {if => sub { $_[0] eq 1 }}]
3061
);
3062
is_deeply($param, {});
test cleanup
Yuki Kimoto authored on 2011-08-10
3063

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3064
$param = $dbi->map_param(
3065
    {id => undef, author => undef, price => undef},
test cleanup
Yuki Kimoto authored on 2011-08-10
3066
    id => 'table1.id',
3067
    price => ['table1.price', {if => 'exists'}]
cleanup test
Yuki Kimoto authored on 2011-08-10
3068
);
test cleanup
Yuki Kimoto authored on 2011-08-10
3069
is_deeply($param, {'table1.price' => undef});
test cleanup
Yuki Kimoto authored on 2011-08-10
3070

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3071
$param = $dbi->map_param(
3072
    {price => 'a'},
test cleanup
Yuki Kimoto authored on 2011-08-10
3073
    id => ['table1.id', {if => 'exists'}],
3074
    price => ['table1.price', sub { '%' . $_[0] }, {if => 'exists'}]
cleanup test
Yuki Kimoto authored on 2011-08-10
3075
);
test cleanup
Yuki Kimoto authored on 2011-08-10
3076
is_deeply($param, {'table1.price' => '%a'});
test cleanup
Yuki Kimoto authored on 2011-08-10
3077

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3078
test 'order';
3079
$dbi = DBIx::Custom->connect;
3080
eval { $dbi->execute('drop table table1') };
3081
$dbi->execute($create_table1);
3082
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
3083
$dbi->insert({key1 => 1, key2 => 3}, table => 'table1');
3084
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
3085
$dbi->insert({key1 => 2, key2 => 4}, table => 'table1');
3086
my $order = $dbi->order;
3087
$order->prepend('key1', 'key2 desc');
3088
$result = $dbi->select(table => 'table1', append => "$order");
3089
is_deeply($result->all, [{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1},
3090
  {key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}]);
3091
$order->prepend('key1 desc');
3092
$result = $dbi->select(table => 'table1', append => "$order");
3093
is_deeply($result->all, [{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2},
3094
  {key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}]);
3095

            
3096
$order = $dbi->order;
3097
$order->prepend(['table1-key1'], [qw/table1-key2 desc/]);
3098
$result = $dbi->select(table => 'table1',
3099
  column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']],
3100
  append => "$order");
3101
is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3},
3102
  {'table1-key1' => 1, 'table1-key2' => 1},
3103
  {'table1-key1' => 2, 'table1-key2' => 4},
3104
  {'table1-key1' => 2, 'table1-key2' => 2}]);
3105

            
3106
test 'tag_parse';
cleanup
Yuki Kimoto authored on 2011-08-13
3107
$DB::single = 1;
test cleanup
Yuki Kimoto authored on 2011-08-10
3108
$dbi = DBIx::Custom->connect;
3109
$dbi->tag_parse(0);
3110
eval { $dbi->execute('drop table table1') };
3111
$dbi->execute($create_table1);
3112
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
3113
eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})};
3114
ok($@);
3115

            
3116
test 'last_sql';
3117
$dbi = DBIx::Custom->connect;
3118
eval { $dbi->execute('drop table table1') };
3119
$dbi->execute($create_table1);
3120
$dbi->execute('select * from table1');
3121
is($dbi->last_sql, 'select * from table1;');
3122

            
3123
eval{$dbi->execute("aaa")};
3124
is($dbi->last_sql, 'aaa;');
3125

            
3126
test 'DBIx::Custom header';
3127
$dbi = DBIx::Custom->connect;
3128
eval { $dbi->execute('drop table table1') };
3129
$dbi->execute($create_table1);
3130
$result = $dbi->execute('select key1 as h1, key2 as h2 from table1');
3131
is_deeply($result->header, [qw/h1 h2/]);
3132

            
3133
test 'Named placeholder :name(operater) syntax';
3134
$dbi->execute('drop table table1');
3135
$dbi->execute($create_table1_2);
3136
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3137
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3138

            
3139
$source = "select * from table1 where :key1{=} and :key2{=}";
3140
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
3141
$rows = $result->all;
3142
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3143

            
3144
$source = "select * from table1 where :key1{ = } and :key2{=}";
3145
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
3146
$rows = $result->all;
3147
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3148

            
3149
$source = "select * from table1 where :key1{<} and :key2{=}";
3150
$result = $dbi->execute($source, param => {key1 => 5, key2 => 2});
3151
$rows = $result->all;
3152
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3153

            
3154
$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}";
3155
$result = $dbi->execute(
3156
    $source,
3157
    param => {'table1.key1' => 1, 'table1.key2' => 1},
3158
    filter => {'table1.key2' => sub { $_[0] * 2 }}
3159
);
3160
$rows = $result->all;
3161
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3162

            
3163
test 'high perfomance way';
3164
$dbi->execute('drop table table1');
3165
$dbi->execute($create_table1_highperformance);
3166
$rows = [
3167
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3168
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3169
];
3170
{
3171
    my $query;
3172
    foreach my $row (@$rows) {
3173
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
3174
      $dbi->execute($query, $row, filter => {ab => sub { $_[0] * 2 }});
3175
    }
3176
    is_deeply($dbi->select(table => 'table1')->all,
3177
      [
3178
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3179
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3180
      ]
3181
    );
3182
}
3183

            
3184
$dbi->execute('drop table table1');
3185
$dbi->execute($create_table1_highperformance);
3186
$rows = [
3187
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3188
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3189
];
3190
{
3191
    my $query;
3192
    my $sth;
3193
    foreach my $row (@$rows) {
3194
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
3195
      $sth ||= $query->sth;
3196
      $sth->execute(map { $row->{$_} } sort keys %$row);
3197
    }
3198
    is_deeply($dbi->select(table => 'table1')->all,
3199
      [
3200
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3201
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3202
      ]
3203
    );
3204
}
3205

            
3206
test 'result';
3207
$dbi = DBIx::Custom->connect;
3208
eval { $dbi->execute('drop table table1') };
3209
$dbi->execute($create_table1);
3210
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3211
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3212

            
3213
$result = $dbi->select(table => 'table1');
3214
@rows = ();
3215
while (my $row = $result->fetch) {
3216
    push @rows, [@$row];
3217
}
3218
is_deeply(\@rows, [[1, 2], [3, 4]]);
3219

            
3220
$result = $dbi->select(table => 'table1');
3221
@rows = ();
3222
while (my $row = $result->fetch_hash) {
3223
    push @rows, {%$row};
3224
}
3225
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
3226

            
3227
$dbi = DBIx::Custom->connect;
3228
eval { $dbi->execute('drop table table1') };
3229
$dbi->execute($create_table1);
3230
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3231
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3232

            
3233
test 'fetch_all';
3234
$result = $dbi->select(table => 'table1');
3235
$rows = $result->fetch_all;
3236
is_deeply($rows, [[1, 2], [3, 4]]);
3237

            
3238
$result = $dbi->select(table => 'table1');
3239
$rows = $result->fetch_hash_all;
3240
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
3241

            
3242
$result = $dbi->select(table => 'table1');
3243
$result->dbi->filters({three_times => sub { $_[0] * 3}});
3244
$result->filter({key1 => 'three_times'});
3245

            
3246
$rows = $result->fetch_all;
3247
is_deeply($rows, [[3, 2], [9, 4]], "array");
3248

            
3249
$result = $dbi->select(table => 'table1');
3250
$result->dbi->filters({three_times => sub { $_[0] * 3}});
3251
$result->filter({key1 => 'three_times'});
3252
$rows = $result->fetch_hash_all;
3253
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash");
3254

            
3255
test "query_builder";
3256
$datas = [
3257
    # Basic tests
3258
    {   name            => 'placeholder basic',
3259
        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
3260
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;",
3261
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
3262
    },
3263
    {
3264
        name            => 'placeholder in',
3265
        source            => "{in k1 3};",
3266
        sql_expected    => "k1 in (?, ?, ?);",
3267
        columns_expected   => [qw/k1 k1 k1/]
3268
    },
3269
    
3270
    # Table name
3271
    {
3272
        name            => 'placeholder with table name',
3273
        source            => "{= a.k1} {= a.k2}",
3274
        sql_expected    => "a.k1 = ? a.k2 = ?;",
3275
        columns_expected  => [qw/a.k1 a.k2/]
3276
    },
3277
    {   
3278
        name            => 'placeholder in with table name',
3279
        source            => "{in a.k1 2} {in b.k2 2}",
3280
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?);",
3281
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
3282
    },
3283
    {
3284
        name            => 'not contain tag',
3285
        source            => "aaa",
3286
        sql_expected    => "aaa;",
3287
        columns_expected  => [],
3288
    }
3289
];
3290

            
3291
for (my $i = 0; $i < @$datas; $i++) {
3292
    my $data = $datas->[$i];
cleanup
Yuki Kimoto authored on 2011-08-13
3293
    my $dbi = DBIx::Custom->new;
3294
    my $builder = $dbi->query_builder;
test cleanup
Yuki Kimoto authored on 2011-08-10
3295
    my $query = $builder->build_query($data->{source});
3296
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
3297
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
3298
}
3299

            
cleanup
Yuki Kimoto authored on 2011-08-13
3300
$dbi = DBIx::Custom->new;
3301
$builder = $dbi->query_builder;
3302
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
3303
    p => sub {
3304
        my @args = @_;
3305
        
3306
        my $expand    = "? $args[0] $args[1]";
3307
        my $columns = [2];
3308
        return [$expand, $columns];
3309
    }
3310
);
3311

            
3312
$query = $builder->build_query("{p a b}");
3313
is($query->{sql}, "? a b;", "register_tag sql");
3314
is_deeply($query->{columns}, [2], "register_tag columns");
3315

            
3316
eval{$builder->build_query('{? }')};
3317
like($@, qr/\QColumn name must be specified in tag "{? }"/, "? not arguments");
3318

            
3319
eval{$builder->build_query("{a }")};
3320
like($@, qr/\QTag "a" is not registered/, "tag not exist");
3321

            
cleanup
Yuki Kimoto authored on 2011-08-13
3322
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
3323
    q => 'string'
3324
});
3325

            
3326
eval{$builder->build_query("{q}", {})};
3327
like($@, qr/Tag "q" must be sub reference/, "tag not code ref");
3328

            
cleanup
Yuki Kimoto authored on 2011-08-13
3329
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
3330
   r => sub {} 
3331
});
3332

            
3333
eval{$builder->build_query("{r}")};
3334
like($@, qr/\QTag "r" must return [STRING, ARRAY_REFERENCE]/, "tag return noting");
3335

            
cleanup
Yuki Kimoto authored on 2011-08-13
3336
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
3337
   s => sub { return ["a", ""]} 
3338
});
3339

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
3343
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
3344
    t => sub {return ["a", []]}
3345
);
3346

            
3347

            
cleanup
Yuki Kimoto authored on 2011-08-13
3348
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
3349
    a => sub {
3350
        return ["? ? ?", ['']];
3351
    }
3352
);
3353
eval{$builder->build_query("{a}")};
3354
like($@, qr/\QPlaceholder count/, "placeholder count is invalid");
3355

            
3356

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

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

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

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

            
3372
test 'variouse source';
3373
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;";
3374
$query = $builder->build_query($source);
3375
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1");
3376

            
3377
$source = "abc;";
3378
$query = $builder->build_query($source);
3379
is($query->sql, 'abc;', "basic : 2");
3380

            
3381
$source = "{= a}";
3382
$query = $builder->build_query($source);
3383
is($query->sql, 'a = ?;', "only tag");
3384

            
3385
$source = "000;";
3386
$query = $builder->build_query($source);
3387
is($query->sql, '000;', "contain 0 value");
3388

            
3389
$source = "a {= b} }";
3390
eval{$builder->build_query($source)};
3391
like($@, qr/unexpected "}"/, "error : 1");
3392

            
3393
$source = "a {= {}";
3394
eval{$builder->build_query($source)};
3395
like($@, qr/unexpected "{"/, "error : 2");
3396

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3397
test 'select() wrap option';
3398
$dbi = DBIx::Custom->connect;
3399
eval { $dbi->execute('drop table table1') };
3400
$dbi->execute($create_table1);
3401
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3402
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
3403
$rows = $dbi->select(
3404
    table => 'table1',
3405
    column => 'key1',
3406
    wrap => ['select * from (', ') as t where key1 = 1']
3407
)->all;
3408
is_deeply($rows, [{key1 => 1}]);
3409

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3410
eval {
3411
$dbi->select(
3412
    table => 'table1',
3413
    column => 'key1',
3414
    wrap => 'select * from ('
3415
)
3416
};
3417
like($@, qr/array/);
3418

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3419
test 'dbi method from model';
3420
$dbi = MyDBI9->connect;
3421
eval { $dbi->execute('drop table table1') };
3422
$dbi->execute($create_table1);
3423
$dbi->setup_model;
3424
$model = $dbi->model('table1');
3425
eval{$model->execute('select * from table1')};
3426
ok(!$@);
3427

            
3428
test 'column table option';
3429
$dbi = MyDBI9->connect;
3430
eval { $dbi->execute('drop table table1') };
3431
$dbi->execute($create_table1);
3432
eval { $dbi->execute('drop table table2') };
3433
$dbi->execute($create_table2);
3434
$dbi->setup_model;
3435
$dbi->execute('insert into table1 (key1, key2) values (1, 2);');
3436
$dbi->execute('insert into table2 (key1, key3) values (1, 4);');
3437
$model = $dbi->model('table1');
3438
$result = $model->select(
3439
    column => [
3440
        $model->column('table2', {alias => 'table2_alias'})
3441
    ],
3442
    where => {'table2_alias.key3' => 4}
3443
);
3444
is_deeply($result->one, 
3445
          {'table2_alias.key1' => 1, 'table2_alias.key3' => 4});
3446

            
3447
$dbi->separator('__');
3448
$result = $model->select(
3449
    column => [
3450
        $model->column('table2', {alias => 'table2_alias'})
3451
    ],
3452
    where => {'table2_alias.key3' => 4}
3453
);
3454
is_deeply($result->one, 
3455
          {'table2_alias__key1' => 1, 'table2_alias__key3' => 4});
3456

            
3457
$dbi->separator('-');
3458
$result = $model->select(
3459
    column => [
3460
        $model->column('table2', {alias => 'table2_alias'})
3461
    ],
3462
    where => {'table2_alias.key3' => 4}
3463
);
3464
is_deeply($result->one, 
3465
          {'table2_alias-key1' => 1, 'table2_alias-key3' => 4});
3466

            
3467
test 'create_model';
3468
$dbi = DBIx::Custom->connect;
3469
eval { $dbi->execute('drop table table1') };
3470
eval { $dbi->execute('drop table table2') };
3471
$dbi->execute($create_table1);
3472
$dbi->execute($create_table2);
3473

            
3474
$dbi->create_model(
3475
    table => 'table1',
3476
    join => [
3477
       'left outer join table2 on table1.key1 = table2.key1'
3478
    ],
3479
    primary_key => ['key1']
3480
);
3481
$model2 = $dbi->create_model(
3482
    table => 'table2'
3483
);
3484
$dbi->create_model(
3485
    table => 'table3',
3486
    filter => [
3487
        key1 => {in => sub { uc $_[0] }}
3488
    ]
3489
);
3490
$dbi->setup_model;
3491
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3492
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
3493
$model = $dbi->model('table1');
3494
$result = $model->select(
3495
    column => [$model->mycolumn, $model->column('table2')],
3496
    where => {'table1.key1' => 1}
3497
);
3498
is_deeply($result->one,
3499
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
3500
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3501

            
3502
test 'model method';
3503
$dbi = DBIx::Custom->connect;
3504
eval { $dbi->execute('drop table table2') };
3505
$dbi->execute($create_table2);
3506
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
3507
$model = $dbi->create_model(
3508
    table => 'table2'
3509
);
3510
$model->method(foo => sub { shift->select(@_) });
3511
is_deeply($model->foo->one, {key1 => 1, key3 => 3});
3512

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3513
test 'update_param';
3514
$dbi = DBIx::Custom->connect;
3515
eval { $dbi->execute('drop table table1') };
3516
$dbi->execute($create_table1_2);
3517
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3518
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3519

            
3520
$param = {key2 => 11};
3521
$update_param = $dbi->update_param($param);
3522
$sql = <<"EOS";
3523
update table1 $update_param
3524
where key1 = 1
3525
EOS
3526
$dbi->execute($sql, param => $param);
3527
$result = $dbi->execute('select * from table1 order by key1;', table => 'table1');
3528
$rows   = $result->all;
3529
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
3530
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
3531
                  "basic");
3532

            
3533

            
3534
$dbi = DBIx::Custom->connect;
3535
eval { $dbi->execute('drop table table1') };
3536
$dbi->execute($create_table1_2);
3537
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3538
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3539

            
3540
$param = {key2 => 11, key3 => 33};
3541
$update_param = $dbi->update_param($param);
3542
$sql = <<"EOS";
3543
update table1 $update_param
3544
where key1 = 1
3545
EOS
3546
$dbi->execute($sql, param => $param);
3547
$result = $dbi->execute('select * from table1 order by key1;', table => 'table1');
3548
$rows   = $result->all;
3549
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
3550
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
3551
                  "basic");
3552

            
3553
$dbi = DBIx::Custom->connect;
3554
eval { $dbi->execute('drop table table1') };
3555
$dbi->execute($create_table1_2);
3556
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3557
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3558

            
3559
$param = {key2 => 11, key3 => 33};
3560
$update_param = $dbi->update_param($param, {no_set => 1});
3561
$sql = <<"EOS";
3562
update table1 set $update_param
3563
where key1 = 1
3564
EOS
3565
$dbi->execute($sql, param => $param);
3566
$result = $dbi->execute('select * from table1 order by key1;', table => 'table1');
3567
$rows   = $result->all;
3568
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
3569
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
3570
                  "update param no_set");
3571

            
3572
            
3573
eval { $dbi->update_param({";" => 1}) };
3574
like($@, qr/not safety/);
3575

            
3576

            
3577
test 'update_param';
3578
$dbi = DBIx::Custom->connect;
3579
eval { $dbi->execute('drop table table1') };
3580
$dbi->execute($create_table1_2);
3581
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3582
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3583

            
3584
$param = {key2 => 11};
3585
$update_param = $dbi->assign_param($param);
3586
$sql = <<"EOS";
3587
update table1 set $update_param
3588
where key1 = 1
3589
EOS
3590
$dbi->execute($sql, param => $param, table => 'table1');
3591
$result = $dbi->execute('select * from table1 order by key1;');
3592
$rows   = $result->all;
3593
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
3594
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
3595
                  "basic");
3596

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3597
test 'join';
3598
$dbi = DBIx::Custom->connect;
3599
eval { $dbi->execute('drop table table1') };
3600
$dbi->execute($create_table1);
3601
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3602
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
3603
eval { $dbi->execute('drop table table2') };
3604
$dbi->execute($create_table2);
3605
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
3606
eval { $dbi->execute('drop table table3') };
3607
$dbi->execute('create table table3 (key3 int, key4 int);');
3608
$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4});
3609
$rows = $dbi->select(
3610
    table => 'table1',
3611
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
3612
    where   => {'table1.key2' => 2},
3613
    join  => ['left outer join table2 on table1.key1 = table2.key1']
3614
)->all;
3615
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]);
3616

            
3617
$rows = $dbi->select(
3618
    table => 'table1',
3619
    where   => {'key1' => 1},
3620
    join  => ['left outer join table2 on table1.key1 = table2.key1']
3621
)->all;
3622
is_deeply($rows, [{key1 => 1, key2 => 2}]);
3623

            
3624
eval {
3625
    $rows = $dbi->select(
3626
        table => 'table1',
3627
        column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
3628
        where   => {'table1.key2' => 2},
3629
        join  => {'table1.key1' => 'table2.key1'}
3630
    );
3631
};
3632
like ($@, qr/array/);
3633

            
3634
$rows = $dbi->select(
3635
    table => 'table1',
3636
    where   => {'key1' => 1},
3637
    join  => ['left outer join table2 on table1.key1 = table2.key1',
3638
              'left outer join table3 on table2.key3 = table3.key3']
3639
)->all;
3640
is_deeply($rows, [{key1 => 1, key2 => 2}]);
3641

            
3642
$rows = $dbi->select(
3643
    column => 'table3.key4 as table3__key4',
3644
    table => 'table1',
3645
    where   => {'table1.key1' => 1},
3646
    join  => ['left outer join table2 on table1.key1 = table2.key1',
3647
              'left outer join table3 on table2.key3 = table3.key3']
3648
)->all;
3649
is_deeply($rows, [{table3__key4 => 4}]);
3650

            
3651
$rows = $dbi->select(
3652
    column => 'table1.key1 as table1__key1',
3653
    table => 'table1',
3654
    where   => {'table3.key4' => 4},
3655
    join  => ['left outer join table2 on table1.key1 = table2.key1',
3656
              'left outer join table3 on table2.key3 = table3.key3']
3657
)->all;
3658
is_deeply($rows, [{table1__key1 => 1}]);
3659

            
3660
$dbi = DBIx::Custom->connect;
3661
eval { $dbi->execute('drop table table1') };
3662
$dbi->execute($create_table1);
3663
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3664
eval { $dbi->execute('drop table table2') };
3665
$dbi->execute($create_table2);
3666
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
3667
$rows = $dbi->select(
3668
    table => 'table1',
3669
    column => "${q}table1$p.${q}key1$p as ${q}table1_key1$p, ${q}table2$p.${q}key1$p as ${q}table2_key1$p, ${q}key2$p, ${q}key3$p",
3670
    where   => {'table1.key2' => 2},
3671
    join  => ["left outer join ${q}table2$p on ${q}table1$p.${q}key1$p = ${q}table2$p.${q}key1$p"],
3672
)->all;
3673
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}],
3674
          'quote');
3675

            
3676

            
3677
$dbi = DBIx::Custom->connect;
3678
eval { $dbi->execute('drop table table1') };
3679
$dbi->execute($create_table1);
3680
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3681
$sql = <<"EOS";
3682
left outer join (
3683
  select * from table1 as t1
3684
  where t1.key2 = (
3685
    select max(t2.key2) from table1 as t2
3686
    where t1.key1 = t2.key1
3687
  )
3688
) as latest_table1 on table1.key1 = latest_table1.key1
3689
EOS
3690
$join = [$sql];
3691
$rows = $dbi->select(
3692
    table => 'table1',
3693
    column => 'latest_table1.key1 as latest_table1__key1',
3694
    join  => $join
3695
)->all;
3696
is_deeply($rows, [{latest_table1__key1 => 1}]);
3697

            
3698
$dbi = DBIx::Custom->connect;
3699
eval { $dbi->execute('drop table table1') };
3700
eval { $dbi->execute('drop table table2') };
3701
$dbi->execute($create_table1);
3702
$dbi->execute($create_table2);
3703
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3704
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
3705
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
3706
$result = $dbi->select(
3707
    table => 'table1',
3708
    join => [
3709
        "left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1"
3710
    ]
3711
);
3712
is_deeply($result->all, [{key1 => 1, key2 => 2}]);
3713
$result = $dbi->select(
3714
    table => 'table1',
3715
    column => [{table2 => ['key3']}],
3716
    join => [
3717
        "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1"
3718
    ]
3719
);
3720
is_deeply($result->all, [{'table2.key3' => 4}]);
3721
$result = $dbi->select(
3722
    table => 'table1',
3723
    column => [{table2 => ['key3']}],
3724
    join => [
3725
        "left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'"
3726
    ]
3727
);
3728
is_deeply($result->all, [{'table2.key3' => 4}]);
3729

            
3730
$dbi = DBIx::Custom->connect;
3731
eval { $dbi->execute('drop table table1') };
3732
eval { $dbi->execute('drop table table2') };
3733
$dbi->execute($create_table1);
3734
$dbi->execute($create_table2);
3735
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3736
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
3737
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
3738
$result = $dbi->select(
3739
    table => 'table1',
3740
    column => [{table2 => ['key3']}],
3741
    join => [
3742
        {
3743
            clause => "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1",
3744
            table => ['table1', 'table2']
3745
        }
3746
    ]
3747
);
3748
is_deeply($result->all, [{'table2.key3' => 4}]);
3749

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3750
test 'Model class';
3751
use MyDBI1;
3752
$dbi = MyDBI1->connect;
3753
eval { $dbi->execute('drop table table1') };
3754
$dbi->execute($create_table1);
3755
$model = $dbi->model('table1');
3756
$model->insert({key1 => 'a', key2 => 'b'});
3757
is_deeply($model->list->all, [{key1 => 'a', key2 => 'b'}], 'basic');
3758
eval { $dbi->execute('drop table table2') };
3759
$dbi->execute($create_table2);
3760
$model = $dbi->model('table2');
3761
$model->insert({key1 => 'a'});
3762
is_deeply($model->list->all, [{key1 => 'a', key3 => undef}], 'basic');
3763
is($dbi->models->{'table1'}, $dbi->model('table1'));
3764
is($dbi->models->{'table2'}, $dbi->model('table2'));
3765

            
3766
$dbi = MyDBI4->connect;
3767
eval { $dbi->execute('drop table table1') };
3768
$dbi->execute($create_table1);
3769
$model = $dbi->model('table1');
3770
$model->insert({key1 => 'a', key2 => 'b'});
3771
is_deeply($model->list->all, [{key1 => 'a', key2 => 'b'}], 'basic');
3772
eval { $dbi->execute('drop table table2') };
3773
$dbi->execute($create_table2);
3774
$model = $dbi->model('table2');
3775
$model->insert({key1 => 'a'});
3776
is_deeply($model->list->all, [{key1 => 'a', key3 => undef}], 'basic');
3777

            
3778
$dbi = MyDBI5->connect;
3779
eval { $dbi->execute('drop table table1') };
3780
eval { $dbi->execute('drop table table2') };
3781
$dbi->execute($create_table1);
3782
$dbi->execute($create_table2);
3783
$model = $dbi->model('table2');
3784
$model->insert({key1 => 'a'});
3785
is_deeply($model->list->all, [{key1 => 'a', key3 => undef}], 'include all model');
3786
$dbi->insert(table => 'table1', param => {key1 => 1});
3787
$model = $dbi->model('table1');
3788
is_deeply($model->list->all, [{key1 => 1, key2 => undef}], 'include all model');
3789

            
3790
test 'primary_key';
3791
use MyDBI1;
3792
$dbi = MyDBI1->connect;
3793
$model = $dbi->model('table1');
3794
$model->primary_key(['key1', 'key2']);
3795
is_deeply($model->primary_key, ['key1', 'key2']);
3796

            
3797
test 'columns';
3798
use MyDBI1;
3799
$dbi = MyDBI1->connect;
3800
$model = $dbi->model('table1');
3801
$model->columns(['key1', 'key2']);
3802
is_deeply($model->columns, ['key1', 'key2']);
3803

            
3804
test 'setup_model';
3805
use MyDBI1;
3806
$dbi = MyDBI1->connect;
3807
eval { $dbi->execute('drop table table1') };
3808
eval { $dbi->execute('drop table table2') };
3809

            
3810
$dbi->execute($create_table1);
3811
$dbi->execute($create_table2);
3812
$dbi->setup_model;
3813
is_deeply([sort @{$dbi->model('table1')->columns}], ['key1', 'key2']);
3814
is_deeply([sort @{$dbi->model('table2')->columns}], ['key1', 'key3']);
3815

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

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

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