DBIx-Custom / t / common.t /
Newer Older
3858 lines | 124.522kb
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 $time_typename = $dbi->time_typename;
159
my $datetime_typename = $dbi->datetime_typename;
160
my $date_datatype = $dbi->date_datatype;
161
my $time_datatype = $dbi->time_datatype;
162
my $datetime_datatype = $dbi->datetime_datatype;
added common test executing ...
Yuki Kimoto authored on 2011-08-07
163

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

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

            
199
test 'type_rule into';
200
$dbi = DBIx::Custom->connect;
201
eval { $dbi->execute('drop table table1') };
202
$dbi->execute($create_table1_type);
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
$rows = $dbi->select(table => 'table1', append => "order by key1 desc limit 1")->all;
1147
is_deeply($rows, [{key1 => 3, key2 => 4}], "append statement");
1148

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

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

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

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

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

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

            
1198
test 'filters';
1199
$dbi = DBIx::Custom->new;
1200

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

            
1204
is($dbi->filters->{encode_utf8}->('あ'),
1205
   encode_utf8('あ'), "encode_utf8");
1206

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

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

            
1226
$result = $dbi->select(table => 'table1');
1227
ok(! $result->fetch_first, "rollback");
1228

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

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

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

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

            
1263

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

            
1269
$dbi->begin_work;
1270

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

            
1277
$dbi->rollback if $@;
1278

            
1279
$result = $dbi->select(table => 'table1');
1280
$rows = $result->all;
1281
is_deeply($rows, [], "rollback");
1282

            
1283
$dbi->begin_work;
1284

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

            
1290
$dbi->commit unless $@;
1291

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

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

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

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

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

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

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

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

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

            
1365
is($dbi->one, 1, "first");
1366
is($dbi->two, 2, "second");
1367
is($dbi->twice(5), 10 , "second");
1368

            
1369
eval {$dbi->XXXXXX};
1370
ok($@, "not exists");
1371

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1556
test 'limit';
1557
$dbi = DBIx::Custom->connect;
1558
eval { $dbi->execute('drop table table1') };
1559
$dbi->execute($create_table1);
1560
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1561
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
1562
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
1563
$dbi->register_tag(
1564
    limit => sub {
1565
        my ($count, $offset) = @_;
1566
        
1567
        my $s = '';
1568
        $s .= "limit $count";
1569
        $s .= " offset $offset" if defined $offset;
1570
        
1571
        return [$s, []];
1572
    }
1573
);
1574
$rows = $dbi->select(
1575
  table => 'table1',
1576
  where => {key1 => 1},
1577
  append => "order by key2 {limit 1 0}"
1578
)->all;
1579
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1580
$rows = $dbi->select(
1581
  table => 'table1',
1582
  where => {key1 => 1},
1583
  append => "order by key2 {limit 2 1}"
1584
)->all;
1585
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
1586
$rows = $dbi->select(
1587
  table => 'table1',
1588
  where => {key1 => 1},
1589
  append => "order by key2 {limit 1}"
1590
)->all;
1591
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1592

            
1593
test 'connect super';
test cleanup
Yuki Kimoto authored on 2011-08-10
1594
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1595
eval { $dbi->execute('drop table table1') };
1596
$dbi->execute($create_table1);
1597
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1598
is($dbi->select(table => 'table1')->one->{key1}, 1);
1599

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1600
$dbi = DBIx::Custom->new;
test cleanup
Yuki Kimoto authored on 2011-08-10
1601
$dbi->connect;
1602
eval { $dbi->execute('drop table table1') };
1603
$dbi->execute($create_table1);
1604
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1605
is($dbi->select(table => 'table1')->one->{key1}, 1);
1606

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1607
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1608
eval { $dbi->execute('drop table table1') };
1609
$dbi->execute($create_table1);
1610
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1611
is($dbi->select(table => 'table1')->one->{key1}, 1);
1612

            
1613
test 'end_filter';
1614
$dbi = DBIx::Custom->connect;
1615
eval { $dbi->execute('drop table table1') };
1616
$dbi->execute($create_table1);
1617
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1618
$result = $dbi->select(table => 'table1');
1619
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1620
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 });
1621
$row = $result->fetch_first;
1622
is_deeply($row, [6, 40]);
1623

            
1624
$dbi = DBIx::Custom->connect;
1625
eval { $dbi->execute('drop table table1') };
1626
$dbi->execute($create_table1);
1627
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1628
$result = $dbi->select(table => 'table1');
1629
$result->filter([qw/key1 key2/] => sub { $_[0] * 2 });
1630
$result->end_filter([[qw/key1 key2/] => sub { $_[0] * 3 }]);
1631
$row = $result->fetch_first;
1632
is_deeply($row, [6, 12]);
1633

            
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
$result->filter([[qw/key1 key2/] => sub { $_[0] * 2 }]);
1640
$result->end_filter([qw/key1 key2/] => sub { $_[0] * 3 });
1641
$row = $result->fetch_first;
1642
is_deeply($row, [6, 12]);
1643

            
1644
$dbi->register_filter(five_times => sub { $_[0] * 5 });
1645
$result = $dbi->select(table => 'table1');
1646
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1647
$result->end_filter({key1 => sub { $_[0] * 3 }, key2 => 'five_times' });
1648
$row = $result->one;
1649
is_deeply($row, {key1 => 6, key2 => 40});
1650

            
1651
$dbi->register_filter(five_times => sub { $_[0] * 5 });
1652
$dbi->apply_filter('table1',
1653
    key1 => {end => sub { $_[0] * 3 } },
1654
    key2 => {end => 'five_times'}
1655
);
1656
$result = $dbi->select(table => 'table1');
1657
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1658
$row = $result->one;
1659
is_deeply($row, {key1 => 6, key2 => 40}, 'apply_filter');
1660

            
1661
$dbi->register_filter(five_times => sub { $_[0] * 5 });
1662
$dbi->apply_filter('table1',
1663
    key1 => {end => sub { $_[0] * 3 } },
1664
    key2 => {end => 'five_times'}
1665
);
1666
$result = $dbi->select(table => 'table1');
1667
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1668
$result->filter(key1 => undef);
1669
$result->end_filter(key1 => undef);
1670
$row = $result->one;
1671
is_deeply($row, {key1 => 1, key2 => 40}, 'apply_filter overwrite');
1672

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1673
test 'remove_end_filter and remove_filter';
1674
$dbi = DBIx::Custom->connect;
1675
eval { $dbi->execute('drop table table1') };
1676
$dbi->execute($create_table1);
1677
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1678
$result = $dbi->select(table => 'table1');
1679
$row = $result
1680
       ->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 })
1681
       ->remove_filter
1682
       ->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 })
1683
       ->remove_end_filter
1684
       ->fetch_first;
1685
is_deeply($row, [1, 2]);
1686

            
1687
test 'empty where select';
1688
$dbi = DBIx::Custom->connect;
1689
eval { $dbi->execute('drop table table1') };
1690
$dbi->execute($create_table1);
1691
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1692
$result = $dbi->select(table => 'table1', where => {});
1693
$row = $result->one;
1694
is_deeply($row, {key1 => 1, key2 => 2});
1695

            
1696
test 'select query option';
1697
$dbi = DBIx::Custom->connect;
1698
eval { $dbi->execute('drop table table1') };
1699
$dbi->execute($create_table1);
1700
$query = $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, query => 1);
1701
is(ref $query, 'DBIx::Custom::Query');
1702
$query = $dbi->update(table => 'table1', where => {key1 => 1}, param => {key2 => 2}, query => 1);
1703
is(ref $query, 'DBIx::Custom::Query');
1704
$query = $dbi->delete(table => 'table1', where => {key1 => 1}, query => 1);
1705
is(ref $query, 'DBIx::Custom::Query');
1706
$query = $dbi->select(table => 'table1', where => {key1 => 1, key2 => 2}, query => 1);
1707
is(ref $query, 'DBIx::Custom::Query');
1708

            
1709
test 'where';
1710
$dbi = DBIx::Custom->connect;
1711
eval { $dbi->execute('drop table table1') };
1712
$dbi->execute($create_table1);
1713
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1714
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1715
$where = $dbi->where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
1716
is("$where", "where ( key1 = :key1 and key2 = :key2 )", 'no param');
1717

            
1718
$where = $dbi->where
1719
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
1720
             ->param({key1 => 1});
1721

            
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
$result = $dbi->select(
1730
    table => 'table1',
1731
    where => [
1732
        ['and', 'key1 = :key1', 'key2 = :key2'],
1733
        {key1 => 1}
1734
    ]
1735
);
1736
$row = $result->all;
1737
is_deeply($row, [{key1 => 1, key2 => 2}]);
1738

            
1739
$where = $dbi->where
1740
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
1741
             ->param({key1 => 1, key2 => 2});
1742
$result = $dbi->select(
1743
    table => 'table1',
1744
    where => $where
1745
);
1746
$row = $result->all;
1747
is_deeply($row, [{key1 => 1, key2 => 2}]);
1748

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

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

            
1769
$where = $dbi->where;
1770
$result = $dbi->select(
1771
    table => 'table1',
1772
    where => $where
1773
);
1774
$row = $result->all;
1775
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1776

            
1777
eval {
1778
$where = $dbi->where
1779
             ->clause(['uuu']);
1780
$result = $dbi->select(
1781
    table => 'table1',
1782
    where => $where
1783
);
1784
};
1785
ok($@);
1786

            
1787
$where = $dbi->where;
1788
is("$where", '');
1789

            
1790
$where = $dbi->where
1791
             ->clause(['or', ('key1 = :key1') x 2])
1792
             ->param({key1 => [1, 3]});
1793
$result = $dbi->select(
1794
    table => 'table1',
1795
    where => $where,
1796
);
1797
$row = $result->all;
1798
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1799

            
1800
$where = $dbi->where
1801
             ->clause(['or', ('key1 = :key1') x 2])
1802
             ->param({key1 => [1]});
1803
$result = $dbi->select(
1804
    table => 'table1',
1805
    where => $where,
1806
);
1807
$row = $result->all;
1808
is_deeply($row, [{key1 => 1, key2 => 2}]);
1809

            
1810
$where = $dbi->where
1811
             ->clause(['or', ('key1 = :key1') x 2])
1812
             ->param({key1 => 1});
1813
$result = $dbi->select(
1814
    table => 'table1',
1815
    where => $where,
1816
);
1817
$row = $result->all;
1818
is_deeply($row, [{key1 => 1, key2 => 2}]);
1819

            
1820
$where = $dbi->where
1821
             ->clause('key1 = :key1')
1822
             ->param({key1 => 1});
1823
$result = $dbi->select(
1824
    table => 'table1',
1825
    where => $where,
1826
);
1827
$row = $result->all;
1828
is_deeply($row, [{key1 => 1, key2 => 2}]);
1829

            
1830
$where = $dbi->where
1831
             ->clause('key1 = :key1 key2 = :key2')
1832
             ->param({key1 => 1});
1833
eval{$where->to_string};
1834
like($@, qr/one column/);
1835

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

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

            
1856
$where = $dbi->where
1857
             ->clause(['or', ('key1 = :key1') x 3])
1858
             ->param({key1 => [1, 3, $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 => [1, $dbi->not_exists, $dbi->not_exists]});
1869
$result = $dbi->select(
1870
    table => 'table1',
1871
    where => $where,
1872
);
1873
$row = $result->all;
1874
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1875

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

            
1886
$where = $dbi->where
1887
             ->clause(['or', ('key1 = :key1') x 3])
1888
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists, 1]});
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(['or', ('key1 = :key1') x 3])
1898
             ->param({key1 => [$dbi->not_exists, $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(['or', ('key1 = :key1') x 3])
1908
             ->param({key1 => []});
1909
$result = $dbi->select(
1910
    table => 'table1',
1911
    where => $where,
1912
);
1913
$row = $result->all;
1914
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1915

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

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

            
1936
$where = $dbi->where
1937
             ->clause(['and', '{> key1}', '{< key1}' ])
1938
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists]});
1939
$result = $dbi->select(
1940
    table => 'table1',
1941
    where => $where,
1942
);
1943
$row = $result->all;
1944
is_deeply($row, [{key1 => 1, key2 => 2},{key1 => 3, key2 => 4}], 'not_exists');
1945

            
1946
$where = $dbi->where
1947
             ->clause(['and', '{> key1}', '{< key1}' ])
1948
             ->param({key1 => [0, 2]});
1949
$result = $dbi->select(
1950
    table => 'table1',
1951
    where => $where,
1952
);
1953
$row = $result->all;
1954
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1955

            
1956
$where = $dbi->where
1957
             ->clause(['and', 'key1 is not null', 'key2 is not null' ]);
1958
$result = $dbi->select(
1959
    table => 'table1',
1960
    where => $where,
1961
);
1962
$row = $result->all;
1963
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1964

            
1965
eval {$dbi->where(ppp => 1) };
1966
like($@, qr/invalid/);
1967

            
1968
$where = $dbi->where(
1969
    clause => ['and', ['or'], ['and', 'key1 = :key1', 'key2 = :key2']],
1970
    param => {key1 => 1, key2 => 2}
1971
);
1972
$result = $dbi->select(
1973
    table => 'table1',
1974
    where => $where,
1975
);
1976
$row = $result->all;
1977
is_deeply($row, [{key1 => 1, key2 => 2}]);
1978

            
1979

            
1980
$where = $dbi->where(
1981
    clause => ['and', ['or'], ['or', ':key1', ':key2']],
1982
    param => {}
1983
);
1984
$result = $dbi->select(
1985
    table => 'table1',
1986
    where => $where,
1987
);
1988
$row = $result->all;
1989
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1990

            
1991
$where = $dbi->where;
1992
$where->clause(['and', ':key1{=}']);
1993
$where->param({key1 => undef});
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 => undef});
2001
$where->if('defined');
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(['or', ':key1{=}', ':key1{=}']);
2009
$where->param({key1 => [undef, undef]});
2010
$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]});
2011
$row = $result->all;
2012
is_deeply($row, [{key1 => 1, key2 => 2}]);
2013
$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]});
2014
$row = $result->all;
2015
is_deeply($row, [{key1 => 1, key2 => 2}]);
2016

            
2017
$where = $dbi->where;
2018
$where->clause(['and', ':key1{=}']);
2019
$where->param({key1 => [undef, undef]});
2020
$where->if('defined');
2021
$where->map;
2022
$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]});
2023
$row = $result->all;
2024
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
2025
$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]});
2026
$row = $result->all;
2027
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
2028

            
2029
$where = $dbi->where;
2030
$where->clause(['and', ':key1{=}']);
2031
$where->param({key1 => 0});
2032
$where->if('length');
2033
$where->map;
2034
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
2035
$row = $result->all;
2036
is_deeply($row, [{key1 => 1, key2 => 2}]);
2037

            
2038
$where = $dbi->where;
2039
$where->clause(['and', ':key1{=}']);
2040
$where->param({key1 => ''});
2041
$where->if('length');
2042
$where->map;
2043
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
2044
$row = $result->all;
2045
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
2046

            
2047
$where = $dbi->where;
2048
$where->clause(['and', ':key1{=}']);
2049
$where->param({key1 => 5});
2050
$where->if(sub { ($_[0] || '') eq 5 });
2051
$where->map;
2052
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
2053
$row = $result->all;
2054
is_deeply($row, [{key1 => 1, key2 => 2}]);
2055

            
2056
$where = $dbi->where;
2057
$where->clause(['and', ':key1{=}']);
2058
$where->param({key1 => 7});
2059
$where->if(sub { ($_[0] || '') eq 5 });
2060
$where->map;
2061
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
2062
$row = $result->all;
2063
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
2064

            
2065
$where = $dbi->where;
2066
$where->param({id => 1, author => 'Ken', price => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
2067
$where->map(id => 'table1.id',
2068
    author => ['table1.author', sub { '%' . $_[0] . '%' }],
2069
    price => ['table1.price', {if => sub { $_[0] eq 1900 }}]
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.id' => 1, 'table1.author' => '%Ken%',
2072
  'table1.price' => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
2073

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

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

            
2095
$where = $dbi->where;
2096
$where->param({id => undef, author => undef, price => undef});
2097
$where->if('length');
2098
$where->map(
test cleanup
Yuki Kimoto authored on 2011-08-10
2099
    id => 'table1.id',
2100
    price => ['table1.price', {if => 'exists'}]
test cleanup
Yuki Kimoto authored on 2011-08-10
2101
);
test cleanup
Yuki Kimoto authored on 2011-08-10
2102
is_deeply($where->param, {'table1.price' => undef});
test cleanup
Yuki Kimoto authored on 2011-08-10
2103

            
2104
$where = $dbi->where;
2105
$where->param({price => 'a'});
2106
$where->if('length');
2107
$where->map(
test cleanup
Yuki Kimoto authored on 2011-08-10
2108
    id => ['table1.id', {if => 'exists'}],
2109
    price => ['table1.price', sub { '%' . $_[0] }, {if => 'exists'}]
test cleanup
Yuki Kimoto authored on 2011-08-10
2110
);
test cleanup
Yuki Kimoto authored on 2011-08-10
2111
is_deeply($where->param, {'table1.price' => '%a'});
test cleanup
Yuki Kimoto authored on 2011-08-10
2112

            
2113
$where = $dbi->where;
2114
$where->param({id => [1, 2], author => 'Ken', price => 1900});
2115
$where->map(
test cleanup
Yuki Kimoto authored on 2011-08-10
2116
    id => 'table1.id',
2117
    author => ['table1.author', sub { '%' . $_[0] . '%' }],
2118
    price => ['table1.price', {if => sub { $_[0] eq 1900 }}]
test cleanup
Yuki Kimoto authored on 2011-08-10
2119
);
test cleanup
Yuki Kimoto authored on 2011-08-10
2120
is_deeply($where->param, {'table1.id' => [1, 2], 'table1.author' => '%Ken%',
2121
  'table1.price' => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
2122

            
2123
$where = $dbi->where;
2124
$where->if('length');
2125
$where->param({id => ['', ''], author => 'Ken', price => 1900});
2126
$where->map(
test cleanup
Yuki Kimoto authored on 2011-08-10
2127
    id => 'table1.id',
2128
    author => ['table1.author', sub { '%' . $_[0] . '%' }],
2129
    price => ['table1.price', {if => sub { $_[0] eq 1900 }}]
test cleanup
Yuki Kimoto authored on 2011-08-10
2130
);
test cleanup
Yuki Kimoto authored on 2011-08-10
2131
is_deeply($where->param, {'table1.id' => [$dbi->not_exists, $dbi->not_exists], 'table1.author' => '%Ken%',
2132
  'table1.price' => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
2133

            
2134
$where = $dbi->where;
2135
$where->param({id => ['', ''], author => 'Ken', price => 1900});
2136
$where->map(
test cleanup
Yuki Kimoto authored on 2011-08-10
2137
    id => ['table1.id', {if => 'length'}],
2138
    author => ['table1.author', sub { '%' . $_[0] . '%' }, {if => 'defined'}],
2139
    price => ['table1.price', {if => sub { $_[0] eq 1900 }}]
test cleanup
Yuki Kimoto authored on 2011-08-10
2140
);
test cleanup
Yuki Kimoto authored on 2011-08-10
2141
is_deeply($where->param, {'table1.id' => [$dbi->not_exists, $dbi->not_exists], 'table1.author' => '%Ken%',
2142
  'table1.price' => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
2143

            
2144
test 'dbi_option default';
2145
$dbi = DBIx::Custom->new;
2146
is_deeply($dbi->dbi_option, {});
2147

            
2148
test 'register_tag_processor';
2149
$dbi = DBIx::Custom->connect;
2150
$dbi->register_tag_processor(
2151
    a => sub { 1 }
2152
);
test cleanup
Yuki Kimoto authored on 2011-08-10
2153
is($dbi->{_tags}->{a}->(), 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2154

            
2155
test 'register_tag';
2156
$dbi = DBIx::Custom->connect;
2157
$dbi->register_tag(
2158
    b => sub { 2 }
2159
);
test cleanup
Yuki Kimoto authored on 2011-08-10
2160
is($dbi->{_tags}->{b}->(), 2);
test cleanup
Yuki Kimoto authored on 2011-08-10
2161

            
2162
test 'table not specify exception';
2163
$dbi = DBIx::Custom->connect;
2164
eval {$dbi->insert};
2165
like($@, qr/table/);
2166
eval {$dbi->update};
2167
like($@, qr/table/);
2168
eval {$dbi->delete};
2169
like($@, qr/table/);
2170
eval {$dbi->select};
2171
like($@, qr/table/);
test cleanup
Yuki Kimoto authored on 2011-08-10
2172

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2173
test 'more tests';
2174
$dbi = DBIx::Custom->connect;
2175
eval{$dbi->apply_filter('table', 'column', [])};
2176
like($@, qr/apply_filter/);
2177

            
2178
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
2179
like($@, qr/apply_filter/);
2180

            
2181
$dbi->apply_filter(
2182

            
2183
);
2184
$dbi = DBIx::Custom->connect;
2185
eval { $dbi->execute('drop table table1') };
2186
$dbi->execute($create_table1);
2187
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2188
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
2189
$dbi->apply_filter('table1', 'key2', 
2190
                   {in => sub { $_[0] * 3 }, out => sub { $_[0] * 2 }});
2191
$rows = $dbi->select(table => 'table1', where => {key2 => 1})->all;
2192
is_deeply($rows, [{key1 => 1, key2 => 6}]);
2193

            
2194
$dbi = DBIx::Custom->connect;
2195
eval { $dbi->execute('drop table table1') };
2196
$dbi->execute($create_table1);
2197
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2198
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
2199
$dbi->apply_filter('table1', 'key2', {});
2200
$rows = $dbi->select(table => 'table1', where => {key2 => 2})->all;
2201
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2202

            
2203
$dbi = DBIx::Custom->connect;
2204
eval {$dbi->apply_filter('table1', 'key2', {out => 'no'})};
2205
like($@, qr/not registered/);
2206
eval {$dbi->apply_filter('table1', 'key2', {in => 'no'})};
2207
like($@, qr/not registered/);
2208
$dbi->method({one => sub { 1 }});
2209
is($dbi->one, 1);
2210

            
2211
eval{DBIx::Custom->connect(dsn => undef)};
2212
like($@, qr/_connect/);
2213

            
2214
$dbi = DBIx::Custom->connect;
2215
eval { $dbi->execute('drop table table1') };
2216
$dbi->execute($create_table1);
2217
$dbi->register_filter(twice => sub { $_[0] * 2 });
2218
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
2219
             filter => {key1 => 'twice'});
2220
$row = $dbi->select(table => 'table1')->one;
2221
is_deeply($row, {key1 => 2, key2 => 2});
2222
eval {$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
2223
             filter => {key1 => 'no'}) };
2224
like($@, qr//);
2225

            
2226
$dbi->register_filter(one => sub { });
2227
$dbi->default_fetch_filter('one');
2228
ok($dbi->default_fetch_filter);
2229
$dbi->default_bind_filter('one');
2230
ok($dbi->default_bind_filter);
2231
eval{$dbi->default_fetch_filter('no')};
2232
like($@, qr/not registered/);
2233
eval{$dbi->default_bind_filter('no')};
2234
like($@, qr/not registered/);
2235
$dbi->default_bind_filter(undef);
2236
ok(!defined $dbi->default_bind_filter);
2237
$dbi->default_fetch_filter(undef);
2238
ok(!defined $dbi->default_fetch_filter);
2239
eval {$dbi->execute('select * from table1 {} {= author') };
2240
like($@, qr/Tag not finished/);
2241

            
2242
$dbi = DBIx::Custom->connect;
2243
eval { $dbi->execute('drop table table1') };
2244
$dbi->execute($create_table1);
2245
$dbi->register_filter(one => sub { 1 });
2246
$result = $dbi->select(table => 'table1');
2247
eval {$result->filter(key1 => 'no')};
2248
like($@, qr/not registered/);
2249
eval {$result->end_filter(key1 => 'no')};
2250
like($@, qr/not registered/);
2251
$result->default_filter(undef);
2252
ok(!defined $result->default_filter);
2253
$result->default_filter('one');
2254
is($result->default_filter->(), 1);
2255

            
2256
test 'dbi_option';
2257
$dbi = DBIx::Custom->connect(dbi_option => {PrintError => 1});
2258
ok($dbi->dbh->{PrintError});
2259
$dbi = DBIx::Custom->connect(dbi_options => {PrintError => 1});
2260
ok($dbi->dbh->{PrintError});
2261

            
2262
test 'DBIx::Custom::Result stash()';
2263
$result = DBIx::Custom::Result->new;
2264
is_deeply($result->stash, {}, 'default');
2265
$result->stash->{foo} = 1;
2266
is($result->stash->{foo}, 1, 'get and set');
test cleanup
Yuki Kimoto authored on 2011-08-10
2267

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2268
test 'delete_at';
2269
$dbi = DBIx::Custom->connect;
2270
eval { $dbi->execute('drop table table1') };
2271
$dbi->execute($create_table1_2);
2272
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2273
$dbi->delete_at(
2274
    table => 'table1',
2275
    primary_key => ['key1', 'key2'],
2276
    where => [1, 2],
2277
);
2278
is_deeply($dbi->select(table => 'table1')->all, []);
2279

            
2280
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2281
$dbi->delete_at(
2282
    table => 'table1',
2283
    primary_key => 'key1',
2284
    where => 1,
2285
);
2286
is_deeply($dbi->select(table => 'table1')->all, []);
2287

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

            
2302
$dbi->delete_all(table => 'table1');
2303
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2304
$dbi->insert_at(
2305
    primary_key => 'key1', 
2306
    table => 'table1',
2307
    where => 1,
2308
    param => {key2 => 2, key3 => 3}
2309
);
2310

            
2311
is($dbi->select(table => 'table1')->one->{key1}, 1);
2312
is($dbi->select(table => 'table1')->one->{key2}, 2);
2313
is($dbi->select(table => 'table1')->one->{key3}, 3);
2314

            
2315
eval {
2316
    $dbi->insert_at(
2317
        table => 'table1',
2318
        primary_key => ['key1', 'key2'],
2319
        where => {},
2320
        param => {key1 => 1, key2 => 2, key3 => 3},
2321
    );
2322
};
2323
like($@, qr/must be/);
2324

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

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

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

            
2365
$dbi = DBIx::Custom->connect;
2366
eval { $dbi->execute('drop table table1') };
2367
$dbi->execute($create_table1_2);
2368
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2369
$dbi->update_at(
2370
    {key3 => 4},
2371
    table => 'table1',
2372
    primary_key => ['key1', 'key2'],
2373
    where => [1, 2]
2374
);
2375
is($dbi->select(table => 'table1')->one->{key1}, 1);
2376
is($dbi->select(table => 'table1')->one->{key2}, 2);
2377
is($dbi->select(table => 'table1')->one->{key3}, 4);
2378

            
2379
test 'select_at';
2380
$dbi = DBIx::Custom->connect;
2381
eval { $dbi->execute('drop table table1') };
2382
$dbi->execute($create_table1_2);
2383
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2384
$result = $dbi->select_at(
2385
    table => 'table1',
2386
    primary_key => ['key1', 'key2'],
2387
    where => [1, 2]
2388
);
2389
$row = $result->one;
2390
is($row->{key1}, 1);
2391
is($row->{key2}, 2);
2392
is($row->{key3}, 3);
2393

            
2394
$dbi->delete_all(table => 'table1');
2395
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2396
$result = $dbi->select_at(
2397
    table => 'table1',
2398
    primary_key => 'key1',
2399
    where => 1,
2400
);
2401
$row = $result->one;
2402
is($row->{key1}, 1);
2403
is($row->{key2}, 2);
2404
is($row->{key3}, 3);
2405

            
2406
$dbi->delete_all(table => 'table1');
2407
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2408
$result = $dbi->select_at(
2409
    table => 'table1',
2410
    primary_key => ['key1', 'key2'],
2411
    where => [1, 2]
2412
);
2413
$row = $result->one;
2414
is($row->{key1}, 1);
2415
is($row->{key2}, 2);
2416
is($row->{key3}, 3);
2417

            
2418
eval {
2419
    $result = $dbi->select_at(
2420
        table => 'table1',
2421
        primary_key => ['key1', 'key2'],
2422
        where => {},
2423
    );
2424
};
2425
like($@, qr/must be/);
2426

            
2427
eval {
2428
    $result = $dbi->select_at(
2429
        table => 'table1',
2430
        primary_key => ['key1', 'key2'],
2431
        where => [1],
2432
    );
2433
};
2434
like($@, qr/same/);
2435

            
2436
eval {
2437
    $result = $dbi->update_at(
2438
        table => 'table1',
2439
        primary_key => ['key1', 'key2'],
2440
        where => {},
2441
        param => {key1 => 1, key2 => 2},
2442
    );
2443
};
2444
like($@, qr/must be/);
2445

            
2446
eval {
2447
    $result = $dbi->delete_at(
2448
        table => 'table1',
2449
        primary_key => ['key1', 'key2'],
2450
        where => {},
2451
    );
2452
};
2453
like($@, qr/must be/);
2454

            
2455
test 'columns';
2456
use MyDBI1;
2457
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2458
$model = $dbi->model('table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2459

            
2460

            
2461
test 'model delete_at';
2462
$dbi = MyDBI6->connect;
2463
eval { $dbi->execute('drop table table1') };
2464
eval { $dbi->execute('drop table table2') };
2465
eval { $dbi->execute('drop table table3') };
2466
$dbi->execute($create_table1_2);
2467
$dbi->execute($create_table2_2);
2468
$dbi->execute($create_table3);
2469
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2470
$dbi->model('table1')->delete_at(where => [1, 2]);
2471
is_deeply($dbi->select(table => 'table1')->all, []);
2472
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2473
$dbi->model('table1')->delete_at(where => [1, 2]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2474
is_deeply($dbi->select(table => 'table1')->all, []);
2475
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2476
$dbi->model('table3')->delete_at(where => [1, 2]);
2477
is_deeply($dbi->select(table => 'table3')->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
2478

            
2479
test 'model insert_at';
2480
$dbi = MyDBI6->connect;
2481
eval { $dbi->execute('drop table table1') };
2482
$dbi->execute($create_table1_2);
2483
$dbi->model('table1')->insert_at(
2484
    where => [1, 2],
2485
    param => {key3 => 3}
2486
);
2487
$result = $dbi->model('table1')->select;
2488
$row = $result->one;
2489
is($row->{key1}, 1);
2490
is($row->{key2}, 2);
2491
is($row->{key3}, 3);
2492

            
2493
test 'model update_at';
2494
$dbi = MyDBI6->connect;
2495
eval { $dbi->execute('drop table table1') };
2496
$dbi->execute($create_table1_2);
2497
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2498
$dbi->model('table1')->update_at(
2499
    where => [1, 2],
2500
    param => {key3 => 4}
2501
);
2502
$result = $dbi->model('table1')->select;
2503
$row = $result->one;
2504
is($row->{key1}, 1);
2505
is($row->{key2}, 2);
2506
is($row->{key3}, 4);
2507

            
2508
test 'model select_at';
2509
$dbi = MyDBI6->connect;
2510
eval { $dbi->execute('drop table table1') };
2511
$dbi->execute($create_table1_2);
2512
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2513
$result = $dbi->model('table1')->select_at(where => [1, 2]);
2514
$row = $result->one;
2515
is($row->{key1}, 1);
2516
is($row->{key2}, 2);
2517
is($row->{key3}, 3);
2518

            
2519

            
2520
test 'mycolumn and column';
2521
$dbi = MyDBI7->connect;
2522
eval { $dbi->execute('drop table table1') };
2523
eval { $dbi->execute('drop table table2') };
2524
$dbi->execute($create_table1);
2525
$dbi->execute($create_table2);
2526
$dbi->separator('__');
2527
$dbi->setup_model;
2528
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2529
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2530
$model = $dbi->model('table1');
2531
$result = $model->select(
2532
    column => [$model->mycolumn, $model->column('table2')],
2533
    where => {'table1.key1' => 1}
2534
);
2535
is_deeply($result->one,
2536
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2537

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2538
test 'insert_param';
2539
$dbi = DBIx::Custom->connect;
2540
eval { $dbi->execute('drop table table1') };
2541
$dbi->execute($create_table1_2);
2542
$param = {key1 => 1, key2 => 2};
2543
$insert_param = $dbi->insert_param($param);
2544
$sql = <<"EOS";
2545
insert into table1 $insert_param
2546
EOS
2547
$dbi->execute($sql, param => $param, table => 'table1');
2548
is($dbi->select(table => 'table1')->one->{key1}, 1);
2549
is($dbi->select(table => 'table1')->one->{key2}, 2);
2550

            
2551
$dbi = DBIx::Custom->connect;
2552
eval { $dbi->execute('drop table table1') };
2553
$dbi->execute($create_table1_2);
2554
$param = {key1 => 1, key2 => 2};
2555
$insert_param = $dbi->insert_param($param);
2556
$sql = <<"EOS";
2557
insert into table1 $insert_param
2558
EOS
2559
$dbi->execute($sql, param => $param, table => 'table1');
2560
is($dbi->select(table => 'table1')->one->{key1}, 1);
2561
is($dbi->select(table => 'table1')->one->{key2}, 2);
2562

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2566
test 'mycolumn';
2567
$dbi = MyDBI8->connect;
2568
eval { $dbi->execute('drop table table1') };
2569
eval { $dbi->execute('drop table table2') };
2570
$dbi->execute($create_table1);
2571
$dbi->execute($create_table2);
2572
$dbi->setup_model;
2573
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2574
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2575
$model = $dbi->model('table1');
2576
$result = $model->select_at(
2577
    column => [
2578
        $model->mycolumn,
2579
        $model->column('table2')
2580
    ]
2581
);
2582
is_deeply($result->one,
2583
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
2584

            
2585
$result = $model->select_at(
2586
    column => [
2587
        $model->mycolumn(['key1']),
2588
        $model->column(table2 => ['key1'])
2589
    ]
2590
);
2591
is_deeply($result->one,
2592
          {key1 => 1, 'table2.key1' => 1});
2593
$result = $model->select_at(
2594
    column => [
2595
        $model->mycolumn(['key1']),
2596
        {table2 => ['key1']}
2597
    ]
2598
);
2599
is_deeply($result->one,
2600
          {key1 => 1, 'table2.key1' => 1});
2601

            
2602
$result = $model->select_at(
2603
    column => [
2604
        $model->mycolumn(['key1']),
2605
        ['table2.key1', as => 'table2.key1']
2606
    ]
2607
);
2608
is_deeply($result->one,
2609
          {key1 => 1, 'table2.key1' => 1});
2610

            
2611
$result = $model->select_at(
2612
    column => [
2613
        $model->mycolumn(['key1']),
2614
        ['table2.key1' => 'table2.key1']
2615
    ]
2616
);
2617
is_deeply($result->one,
2618
          {key1 => 1, 'table2.key1' => 1});
2619

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2620
test 'merge_param';
2621
$dbi = DBIx::Custom->new;
2622
$params = [
2623
    {key1 => 1, key2 => 2, key3 => 3},
2624
    {key1 => 1, key2 => 2},
2625
    {key1 => 1}
2626
];
2627
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]);
2628
is_deeply($param, {key1 => [1, 1, 1], key2 => [2, 2], key3 => 3});
2629

            
2630
$params = [
2631
    {key1 => [1, 2], key2 => 1, key3 => [1, 2]},
2632
    {key1 => [3, 4], key2 => [2, 3], key3 => 3}
2633
];
2634
$param = $dbi->merge_param($params->[0], $params->[1]);
2635
is_deeply($param, {key1 => [1, 2, 3, 4], key2 => [1, 2, 3], key3 => [1, 2, 3]});
2636

            
2637
test 'select() param option';
2638
$dbi = DBIx::Custom->connect;
2639
eval { $dbi->execute('drop table table1') };
2640
$dbi->execute($create_table1);
2641
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2642
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2643
eval { $dbi->execute('drop table table2') };
2644
$dbi->execute($create_table2);
2645
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2646
$dbi->insert(table => 'table2', param => {key1 => 2, key3 => 5});
2647
$rows = $dbi->select(
2648
    table => 'table1',
2649
    column => 'table1.key1 as table1_key1, key2, key3',
2650
    where   => {'table1.key2' => 3},
2651
    join  => ['inner join (select * from table2 where {= table2.key3})' . 
2652
              ' as table2 on table1.key1 = table2.key1'],
2653
    param => {'table2.key3' => 5}
2654
)->all;
2655
is_deeply($rows, [{table1_key1 => 2, key2 => 3, key3 => 5}]);
2656

            
2657
test 'select() string where';
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
$rows = $dbi->select(
2664
    table => 'table1',
2665
    where => 'key1 = :key1 and key2 = :key2',
2666
    where_param => {key1 => 1, key2 => 2}
2667
)->all;
2668
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2669

            
2670
$dbi = DBIx::Custom->connect;
2671
eval { $dbi->execute('drop table table1') };
2672
$dbi->execute($create_table1);
2673
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2674
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2675
$rows = $dbi->select(
2676
    table => 'table1',
2677
    where => [
2678
        'key1 = :key1 and key2 = :key2',
2679
        {key1 => 1, key2 => 2}
2680
    ]
2681
)->all;
2682
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2683

            
2684
test 'delete() string where';
2685
$dbi = DBIx::Custom->connect;
2686
eval { $dbi->execute('drop table table1') };
2687
$dbi->execute($create_table1);
2688
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2689
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2690
$dbi->delete(
2691
    table => 'table1',
2692
    where => 'key1 = :key1 and key2 = :key2',
2693
    where_param => {key1 => 1, key2 => 2}
2694
);
2695
$rows = $dbi->select(table => 'table1')->all;
2696
is_deeply($rows, [{key1 => 2, key2 => 3}]);
2697

            
2698
$dbi = DBIx::Custom->connect;
2699
eval { $dbi->execute('drop table table1') };
2700
$dbi->execute($create_table1);
2701
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2702
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2703
$dbi->delete(
2704
    table => 'table1',
2705
    where => [
2706
        'key1 = :key1 and key2 = :key2',
2707
         {key1 => 1, key2 => 2}
2708
    ]
2709
);
2710
$rows = $dbi->select(table => 'table1')->all;
2711
is_deeply($rows, [{key1 => 2, key2 => 3}]);
2712

            
2713

            
2714
test 'update() string where';
2715
$dbi = DBIx::Custom->connect;
2716
eval { $dbi->execute('drop table table1') };
2717
$dbi->execute($create_table1);
2718
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2719
$dbi->update(
2720
    table => 'table1',
2721
    param => {key1 => 5},
2722
    where => 'key1 = :key1 and key2 = :key2',
2723
    where_param => {key1 => 1, key2 => 2}
2724
);
2725
$rows = $dbi->select(table => 'table1')->all;
2726
is_deeply($rows, [{key1 => 5, key2 => 2}]);
2727

            
2728
$dbi = DBIx::Custom->connect;
2729
eval { $dbi->execute('drop table table1') };
2730
$dbi->execute($create_table1);
2731
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2732
$dbi->update(
2733
    table => 'table1',
2734
    param => {key1 => 5},
2735
    where => [
2736
        'key1 = :key1 and key2 = :key2',
2737
        {key1 => 1, key2 => 2}
2738
    ]
2739
);
2740
$rows = $dbi->select(table => 'table1')->all;
2741
is_deeply($rows, [{key1 => 5, key2 => 2}]);
2742

            
2743
test 'insert id and primary_key option';
2744
$dbi = DBIx::Custom->connect;
2745
eval { $dbi->execute('drop table table1') };
2746
$dbi->execute($create_table1_2);
2747
$dbi->insert(
2748
    primary_key => ['key1', 'key2'], 
2749
    table => 'table1',
2750
    id => [1, 2],
2751
    param => {key3 => 3}
2752
);
2753
is($dbi->select(table => 'table1')->one->{key1}, 1);
2754
is($dbi->select(table => 'table1')->one->{key2}, 2);
2755
is($dbi->select(table => 'table1')->one->{key3}, 3);
2756

            
2757
$dbi->delete_all(table => 'table1');
2758
$dbi->insert(
2759
    primary_key => 'key1', 
2760
    table => 'table1',
2761
    id => 0,
2762
    param => {key2 => 2, key3 => 3}
2763
);
2764

            
2765
is($dbi->select(table => 'table1')->one->{key1}, 0);
2766
is($dbi->select(table => 'table1')->one->{key2}, 2);
2767
is($dbi->select(table => 'table1')->one->{key3}, 3);
2768

            
2769
$dbi = DBIx::Custom->connect;
2770
eval { $dbi->execute('drop table table1') };
2771
$dbi->execute($create_table1_2);
2772
$dbi->insert(
2773
    {key3 => 3},
2774
    primary_key => ['key1', 'key2'], 
2775
    table => 'table1',
2776
    id => [1, 2],
2777
);
2778
is($dbi->select(table => 'table1')->one->{key1}, 1);
2779
is($dbi->select(table => 'table1')->one->{key2}, 2);
2780
is($dbi->select(table => 'table1')->one->{key3}, 3);
test cleanup
Yuki Kimoto authored on 2011-08-10
2781

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2782
test 'model insert id and primary_key option';
2783
$dbi = MyDBI6->connect;
2784
eval { $dbi->execute('drop table table1') };
2785
$dbi->execute($create_table1_2);
2786
$dbi->model('table1')->insert(
2787
    id => [1, 2],
2788
    param => {key3 => 3}
2789
);
2790
$result = $dbi->model('table1')->select;
2791
$row = $result->one;
2792
is($row->{key1}, 1);
2793
is($row->{key2}, 2);
2794
is($row->{key3}, 3);
2795

            
2796
$dbi = MyDBI6->connect;
2797
eval { $dbi->execute('drop table table1') };
2798
$dbi->execute($create_table1_2);
2799
$dbi->model('table1')->insert(
2800
    {key3 => 3},
2801
    id => [1, 2]
2802
);
2803
$result = $dbi->model('table1')->select;
2804
$row = $result->one;
2805
is($row->{key1}, 1);
2806
is($row->{key2}, 2);
2807
is($row->{key3}, 3);
2808

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

            
2824
$dbi->delete_all(table => 'table1');
2825
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2826
$dbi->update(
2827
    table => 'table1',
2828
    primary_key => 'key1',
2829
    id => 0,
2830
    param => {key3 => 4}
2831
);
2832
is($dbi->select(table => 'table1')->one->{key1}, 0);
2833
is($dbi->select(table => 'table1')->one->{key2}, 2);
2834
is($dbi->select(table => 'table1')->one->{key3}, 4);
2835

            
2836
$dbi = DBIx::Custom->connect;
2837
eval { $dbi->execute('drop table table1') };
2838
$dbi->execute($create_table1_2);
2839
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2840
$dbi->update(
2841
    {key3 => 4},
2842
    table => 'table1',
2843
    primary_key => ['key1', 'key2'],
2844
    id => [1, 2]
2845
);
2846
is($dbi->select(table => 'table1')->one->{key1}, 1);
2847
is($dbi->select(table => 'table1')->one->{key2}, 2);
2848
is($dbi->select(table => 'table1')->one->{key3}, 4);
2849

            
2850

            
2851
test 'model update and id option';
2852
$dbi = MyDBI6->connect;
2853
eval { $dbi->execute('drop table table1') };
2854
$dbi->execute($create_table1_2);
2855
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2856
$dbi->model('table1')->update(
2857
    id => [1, 2],
2858
    param => {key3 => 4}
2859
);
2860
$result = $dbi->model('table1')->select;
2861
$row = $result->one;
2862
is($row->{key1}, 1);
2863
is($row->{key2}, 2);
2864
is($row->{key3}, 4);
2865

            
2866

            
2867
test 'delete 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
$dbi->delete(
2873
    table => 'table1',
2874
    primary_key => ['key1', 'key2'],
2875
    id => [1, 2],
2876
);
2877
is_deeply($dbi->select(table => 'table1')->all, []);
2878

            
2879
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2880
$dbi->delete(
2881
    table => 'table1',
2882
    primary_key => 'key1',
2883
    id => 0,
2884
);
2885
is_deeply($dbi->select(table => 'table1')->all, []);
2886

            
2887

            
2888
test 'model delete and id option';
2889
$dbi = MyDBI6->connect;
2890
eval { $dbi->execute('drop table table1') };
2891
eval { $dbi->execute('drop table table2') };
2892
eval { $dbi->execute('drop table table3') };
2893
$dbi->execute($create_table1_2);
2894
$dbi->execute($create_table2_2);
2895
$dbi->execute($create_table3);
2896
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2897
$dbi->model('table1')->delete(id => [1, 2]);
2898
is_deeply($dbi->select(table => 'table1')->all, []);
2899
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2900
$dbi->model('table1')->delete(id => [1, 2]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2901
is_deeply($dbi->select(table => 'table1')->all, []);
2902
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2903
$dbi->model('table3')->delete(id => [1, 2]);
2904
is_deeply($dbi->select(table => 'table3')->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
2905

            
2906

            
2907
test 'select and id option';
2908
$dbi = DBIx::Custom->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->select(
2913
    table => 'table1',
2914
    primary_key => ['key1', 'key2'],
2915
    id => [1, 2]
2916
);
2917
$row = $result->one;
2918
is($row->{key1}, 1);
2919
is($row->{key2}, 2);
2920
is($row->{key3}, 3);
2921

            
2922
$dbi->delete_all(table => 'table1');
2923
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2924
$result = $dbi->select(
2925
    table => 'table1',
2926
    primary_key => 'key1',
2927
    id => 0,
2928
);
2929
$row = $result->one;
2930
is($row->{key1}, 0);
2931
is($row->{key2}, 2);
2932
is($row->{key3}, 3);
2933

            
2934
$dbi->delete_all(table => 'table1');
2935
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2936
$result = $dbi->select(
2937
    table => 'table1',
2938
    primary_key => ['key1', 'key2'],
2939
    id => [1, 2]
2940
);
2941
$row = $result->one;
2942
is($row->{key1}, 1);
2943
is($row->{key2}, 2);
2944
is($row->{key3}, 3);
2945

            
2946

            
2947
test 'model select_at';
2948
$dbi = MyDBI6->connect;
2949
eval { $dbi->execute('drop table table1') };
2950
$dbi->execute($create_table1_2);
2951
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2952
$result = $dbi->model('table1')->select(id => [1, 2]);
2953
$row = $result->one;
2954
is($row->{key1}, 1);
2955
is($row->{key2}, 2);
2956
is($row->{key3}, 3);
2957

            
2958
test 'column separator is default .';
2959
$dbi = MyDBI7->connect;
2960
eval { $dbi->execute('drop table table1') };
2961
eval { $dbi->execute('drop table table2') };
2962
$dbi->execute($create_table1);
2963
$dbi->execute($create_table2);
2964
$dbi->setup_model;
2965
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2966
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2967
$model = $dbi->model('table1');
2968
$result = $model->select(
2969
    column => [$model->column('table2')],
2970
    where => {'table1.key1' => 1}
2971
);
2972
is_deeply($result->one,
2973
          {'table2.key1' => 1, 'table2.key3' => 3});
2974

            
2975
$result = $model->select(
2976
    column => [$model->column('table2' => [qw/key1 key3/])],
2977
    where => {'table1.key1' => 1}
2978
);
2979
is_deeply($result->one,
2980
          {'table2.key1' => 1, 'table2.key3' => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2981

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2982
test 'separator';
2983
$dbi = DBIx::Custom->connect;
2984
eval { $dbi->execute('drop table table1') };
2985
eval { $dbi->execute('drop table table2') };
2986
$dbi->execute($create_table1);
2987
$dbi->execute($create_table2);
test cleanup
Yuki Kimoto authored on 2011-08-10
2988

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2989
$dbi->create_model(
2990
    table => 'table1',
2991
    join => [
2992
       'left outer join table2 on table1.key1 = table2.key1'
2993
    ],
2994
    primary_key => ['key1'],
2995
);
2996
$model2 = $dbi->create_model(
2997
    table => 'table2',
2998
);
2999
$dbi->setup_model;
3000
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3001
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
3002
$model = $dbi->model('table1');
3003
$result = $model->select(
3004
    column => [
3005
        $model->mycolumn,
3006
        {table2 => [qw/key1 key3/]}
3007
    ],
3008
    where => {'table1.key1' => 1}
3009
);
3010
is_deeply($result->one,
3011
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
3012
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
3013

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3014
$dbi->separator('__');
3015
$model = $dbi->model('table1');
3016
$result = $model->select(
3017
    column => [
3018
        $model->mycolumn,
3019
        {table2 => [qw/key1 key3/]}
3020
    ],
3021
    where => {'table1.key1' => 1}
3022
);
3023
is_deeply($result->one,
3024
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
3025
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
3026

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3027
$dbi->separator('-');
3028
$model = $dbi->model('table1');
3029
$result = $model->select(
3030
    column => [
3031
        $model->mycolumn,
3032
        {table2 => [qw/key1 key3/]}
3033
    ],
3034
    where => {'table1.key1' => 1}
3035
);
3036
is_deeply($result->one,
3037
          {key1 => 1, key2 => 2, 'table2-key1' => 1, 'table2-key3' => 3});
3038
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
3039

            
3040

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3041
test 'filter_off';
3042
$dbi = DBIx::Custom->connect;
3043
eval { $dbi->execute('drop table table1') };
3044
eval { $dbi->execute('drop table table2') };
3045
$dbi->execute($create_table1);
3046
$dbi->execute($create_table2);
test cleanup
Yuki Kimoto authored on 2011-08-10
3047

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3048
$dbi->create_model(
3049
    table => 'table1',
3050
    join => [
3051
       'left outer join table2 on table1.key1 = table2.key1'
3052
    ],
3053
    primary_key => ['key1'],
3054
);
3055
$dbi->setup_model;
3056
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3057
$model = $dbi->model('table1');
3058
$result = $model->select(column => 'key1');
3059
$result->filter(key1 => sub { $_[0] * 2 });
3060
is_deeply($result->one, {key1 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
3061

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3062
test 'available_datetype';
3063
$dbi = DBIx::Custom->connect;
3064
ok($dbi->can('available_datatype'));
test cleanup
Yuki Kimoto authored on 2011-08-10
3065

            
3066

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3067
test 'select prefix option';
3068
$dbi = DBIx::Custom->connect;
3069
eval { $dbi->execute('drop table table1') };
3070
$dbi->execute($create_table1);
3071
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3072
$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all;
3073
is_deeply($rows, [{key1 => 1, key2 => 2}], "table");
test cleanup
Yuki Kimoto authored on 2011-08-10
3074

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3075
test 'map_param';
3076
$dbi = DBIx::Custom->connect;
3077
$param = $dbi->map_param(
3078
    {id => 1, author => 'Ken', price => 1900},
test cleanup
Yuki Kimoto authored on 2011-08-10
3079
    id => 'table1.id',
3080
    author => ['table1.author', sub { '%' . $_[0] . '%' }],
3081
    price => ['table1.price', {if => sub { $_[0] eq 1900 }}]
cleanup test
Yuki Kimoto authored on 2011-08-10
3082
);
test cleanup
Yuki Kimoto authored on 2011-08-10
3083
is_deeply($param, {'table1.id' => 1, 'table1.author' => '%Ken%',
3084
  'table1.price' => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
3085

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3086
$param = $dbi->map_param(
3087
    {id => 0, author => 0, price => 0},
test cleanup
Yuki Kimoto authored on 2011-08-10
3088
    id => 'table1.id',
3089
    author => ['table1.author', sub { '%' . $_[0] . '%' }],
3090
    price => ['table1.price', sub { '%' . $_[0] . '%' },
cleanup test
Yuki Kimoto authored on 2011-08-10
3091
      {if => sub { $_[0] eq 0 }}]
3092
);
test cleanup
Yuki Kimoto authored on 2011-08-10
3093
is_deeply($param, {'table1.id' => 0, 'table1.author' => '%0%', 'table1.price' => '%0%'});
test cleanup
Yuki Kimoto authored on 2011-08-10
3094

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3095
$param = $dbi->map_param(
3096
    {id => '', author => '', price => ''},
test cleanup
Yuki Kimoto authored on 2011-08-10
3097
    id => 'table1.id',
3098
    author => ['table1.author', sub { '%' . $_[0] . '%' }],
3099
    price => ['table1.price', sub { '%' . $_[0] . '%' },
cleanup test
Yuki Kimoto authored on 2011-08-10
3100
      {if => sub { $_[0] eq 1 }}]
3101
);
3102
is_deeply($param, {});
test cleanup
Yuki Kimoto authored on 2011-08-10
3103

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3104
$param = $dbi->map_param(
3105
    {id => undef, author => undef, price => undef},
test cleanup
Yuki Kimoto authored on 2011-08-10
3106
    id => 'table1.id',
3107
    price => ['table1.price', {if => 'exists'}]
cleanup test
Yuki Kimoto authored on 2011-08-10
3108
);
test cleanup
Yuki Kimoto authored on 2011-08-10
3109
is_deeply($param, {'table1.price' => undef});
test cleanup
Yuki Kimoto authored on 2011-08-10
3110

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3111
$param = $dbi->map_param(
3112
    {price => 'a'},
test cleanup
Yuki Kimoto authored on 2011-08-10
3113
    id => ['table1.id', {if => 'exists'}],
3114
    price => ['table1.price', sub { '%' . $_[0] }, {if => 'exists'}]
cleanup test
Yuki Kimoto authored on 2011-08-10
3115
);
test cleanup
Yuki Kimoto authored on 2011-08-10
3116
is_deeply($param, {'table1.price' => '%a'});
test cleanup
Yuki Kimoto authored on 2011-08-10
3117

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3118
test 'order';
3119
$dbi = DBIx::Custom->connect;
3120
eval { $dbi->execute('drop table table1') };
3121
$dbi->execute($create_table1);
3122
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
3123
$dbi->insert({key1 => 1, key2 => 3}, table => 'table1');
3124
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
3125
$dbi->insert({key1 => 2, key2 => 4}, table => 'table1');
3126
my $order = $dbi->order;
3127
$order->prepend('key1', 'key2 desc');
3128
$result = $dbi->select(table => 'table1', append => "$order");
3129
is_deeply($result->all, [{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1},
3130
  {key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}]);
3131
$order->prepend('key1 desc');
3132
$result = $dbi->select(table => 'table1', append => "$order");
3133
is_deeply($result->all, [{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2},
3134
  {key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}]);
3135

            
3136
$order = $dbi->order;
3137
$order->prepend(['table1-key1'], [qw/table1-key2 desc/]);
3138
$result = $dbi->select(table => 'table1',
3139
  column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']],
3140
  append => "$order");
3141
is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3},
3142
  {'table1-key1' => 1, 'table1-key2' => 1},
3143
  {'table1-key1' => 2, 'table1-key2' => 4},
3144
  {'table1-key1' => 2, 'table1-key2' => 2}]);
3145

            
3146
test 'tag_parse';
cleanup
Yuki Kimoto authored on 2011-08-13
3147
$DB::single = 1;
test cleanup
Yuki Kimoto authored on 2011-08-10
3148
$dbi = DBIx::Custom->connect;
3149
$dbi->tag_parse(0);
3150
eval { $dbi->execute('drop table table1') };
3151
$dbi->execute($create_table1);
3152
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
3153
eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})};
3154
ok($@);
3155

            
3156
test 'last_sql';
3157
$dbi = DBIx::Custom->connect;
3158
eval { $dbi->execute('drop table table1') };
3159
$dbi->execute($create_table1);
3160
$dbi->execute('select * from table1');
3161
is($dbi->last_sql, 'select * from table1;');
3162

            
3163
eval{$dbi->execute("aaa")};
3164
is($dbi->last_sql, 'aaa;');
3165

            
3166
test 'DBIx::Custom header';
3167
$dbi = DBIx::Custom->connect;
3168
eval { $dbi->execute('drop table table1') };
3169
$dbi->execute($create_table1);
3170
$result = $dbi->execute('select key1 as h1, key2 as h2 from table1');
3171
is_deeply($result->header, [qw/h1 h2/]);
3172

            
3173
test 'Named placeholder :name(operater) syntax';
3174
$dbi->execute('drop table table1');
3175
$dbi->execute($create_table1_2);
3176
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3177
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3178

            
3179
$source = "select * from table1 where :key1{=} and :key2{=}";
3180
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
3181
$rows = $result->all;
3182
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3183

            
3184
$source = "select * from table1 where :key1{ = } and :key2{=}";
3185
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
3186
$rows = $result->all;
3187
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3188

            
3189
$source = "select * from table1 where :key1{<} and :key2{=}";
3190
$result = $dbi->execute($source, param => {key1 => 5, key2 => 2});
3191
$rows = $result->all;
3192
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3193

            
3194
$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}";
3195
$result = $dbi->execute(
3196
    $source,
3197
    param => {'table1.key1' => 1, 'table1.key2' => 1},
3198
    filter => {'table1.key2' => sub { $_[0] * 2 }}
3199
);
3200
$rows = $result->all;
3201
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3202

            
3203
test 'high perfomance way';
3204
$dbi->execute('drop table table1');
3205
$dbi->execute($create_table1_highperformance);
3206
$rows = [
3207
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3208
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3209
];
3210
{
3211
    my $query;
3212
    foreach my $row (@$rows) {
3213
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
3214
      $dbi->execute($query, $row, filter => {ab => sub { $_[0] * 2 }});
3215
    }
3216
    is_deeply($dbi->select(table => 'table1')->all,
3217
      [
3218
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3219
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3220
      ]
3221
    );
3222
}
3223

            
3224
$dbi->execute('drop table table1');
3225
$dbi->execute($create_table1_highperformance);
3226
$rows = [
3227
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3228
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3229
];
3230
{
3231
    my $query;
3232
    my $sth;
3233
    foreach my $row (@$rows) {
3234
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
3235
      $sth ||= $query->sth;
3236
      $sth->execute(map { $row->{$_} } sort keys %$row);
3237
    }
3238
    is_deeply($dbi->select(table => 'table1')->all,
3239
      [
3240
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3241
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3242
      ]
3243
    );
3244
}
3245

            
3246
test 'result';
3247
$dbi = DBIx::Custom->connect;
3248
eval { $dbi->execute('drop table table1') };
3249
$dbi->execute($create_table1);
3250
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3251
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3252

            
3253
$result = $dbi->select(table => 'table1');
3254
@rows = ();
3255
while (my $row = $result->fetch) {
3256
    push @rows, [@$row];
3257
}
3258
is_deeply(\@rows, [[1, 2], [3, 4]]);
3259

            
3260
$result = $dbi->select(table => 'table1');
3261
@rows = ();
3262
while (my $row = $result->fetch_hash) {
3263
    push @rows, {%$row};
3264
}
3265
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
3266

            
3267
$dbi = DBIx::Custom->connect;
3268
eval { $dbi->execute('drop table table1') };
3269
$dbi->execute($create_table1);
3270
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3271
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3272

            
3273
test 'fetch_all';
3274
$result = $dbi->select(table => 'table1');
3275
$rows = $result->fetch_all;
3276
is_deeply($rows, [[1, 2], [3, 4]]);
3277

            
3278
$result = $dbi->select(table => 'table1');
3279
$rows = $result->fetch_hash_all;
3280
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
3281

            
3282
$result = $dbi->select(table => 'table1');
3283
$result->dbi->filters({three_times => sub { $_[0] * 3}});
3284
$result->filter({key1 => 'three_times'});
3285

            
3286
$rows = $result->fetch_all;
3287
is_deeply($rows, [[3, 2], [9, 4]], "array");
3288

            
3289
$result = $dbi->select(table => 'table1');
3290
$result->dbi->filters({three_times => sub { $_[0] * 3}});
3291
$result->filter({key1 => 'three_times'});
3292
$rows = $result->fetch_hash_all;
3293
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash");
3294

            
3295
test "query_builder";
3296
$datas = [
3297
    # Basic tests
3298
    {   name            => 'placeholder basic',
3299
        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
3300
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;",
3301
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
3302
    },
3303
    {
3304
        name            => 'placeholder in',
3305
        source            => "{in k1 3};",
3306
        sql_expected    => "k1 in (?, ?, ?);",
3307
        columns_expected   => [qw/k1 k1 k1/]
3308
    },
3309
    
3310
    # Table name
3311
    {
3312
        name            => 'placeholder with table name',
3313
        source            => "{= a.k1} {= a.k2}",
3314
        sql_expected    => "a.k1 = ? a.k2 = ?;",
3315
        columns_expected  => [qw/a.k1 a.k2/]
3316
    },
3317
    {   
3318
        name            => 'placeholder in with table name',
3319
        source            => "{in a.k1 2} {in b.k2 2}",
3320
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?);",
3321
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
3322
    },
3323
    {
3324
        name            => 'not contain tag',
3325
        source            => "aaa",
3326
        sql_expected    => "aaa;",
3327
        columns_expected  => [],
3328
    }
3329
];
3330

            
3331
for (my $i = 0; $i < @$datas; $i++) {
3332
    my $data = $datas->[$i];
cleanup
Yuki Kimoto authored on 2011-08-13
3333
    my $dbi = DBIx::Custom->new;
3334
    my $builder = $dbi->query_builder;
test cleanup
Yuki Kimoto authored on 2011-08-10
3335
    my $query = $builder->build_query($data->{source});
3336
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
3337
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
3338
}
3339

            
cleanup
Yuki Kimoto authored on 2011-08-13
3340
$dbi = DBIx::Custom->new;
3341
$builder = $dbi->query_builder;
3342
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
3343
    p => sub {
3344
        my @args = @_;
3345
        
3346
        my $expand    = "? $args[0] $args[1]";
3347
        my $columns = [2];
3348
        return [$expand, $columns];
3349
    }
3350
);
3351

            
3352
$query = $builder->build_query("{p a b}");
3353
is($query->{sql}, "? a b;", "register_tag sql");
3354
is_deeply($query->{columns}, [2], "register_tag columns");
3355

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

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
3362
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
3363
    q => 'string'
3364
});
3365

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
3369
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
3370
   r => sub {} 
3371
});
3372

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
3376
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
3377
   s => sub { return ["a", ""]} 
3378
});
3379

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
3383
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
3384
    t => sub {return ["a", []]}
3385
);
3386

            
3387

            
cleanup
Yuki Kimoto authored on 2011-08-13
3388
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
3389
    a => sub {
3390
        return ["? ? ?", ['']];
3391
    }
3392
);
3393
eval{$builder->build_query("{a}")};
3394
like($@, qr/\QPlaceholder count/, "placeholder count is invalid");
3395

            
3396

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

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

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

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

            
3412
test 'variouse source';
3413
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;";
3414
$query = $builder->build_query($source);
3415
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1");
3416

            
3417
$source = "abc;";
3418
$query = $builder->build_query($source);
3419
is($query->sql, 'abc;', "basic : 2");
3420

            
3421
$source = "{= a}";
3422
$query = $builder->build_query($source);
3423
is($query->sql, 'a = ?;', "only tag");
3424

            
3425
$source = "000;";
3426
$query = $builder->build_query($source);
3427
is($query->sql, '000;', "contain 0 value");
3428

            
3429
$source = "a {= b} }";
3430
eval{$builder->build_query($source)};
3431
like($@, qr/unexpected "}"/, "error : 1");
3432

            
3433
$source = "a {= {}";
3434
eval{$builder->build_query($source)};
3435
like($@, qr/unexpected "{"/, "error : 2");
3436

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3437
test 'select() wrap option';
3438
$dbi = DBIx::Custom->connect;
3439
eval { $dbi->execute('drop table table1') };
3440
$dbi->execute($create_table1);
3441
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3442
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
3443
$rows = $dbi->select(
3444
    table => 'table1',
3445
    column => 'key1',
3446
    wrap => ['select * from (', ') as t where key1 = 1']
3447
)->all;
3448
is_deeply($rows, [{key1 => 1}]);
3449

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3450
eval {
3451
$dbi->select(
3452
    table => 'table1',
3453
    column => 'key1',
3454
    wrap => 'select * from ('
3455
)
3456
};
3457
like($@, qr/array/);
3458

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3459
test 'dbi method from model';
3460
$dbi = MyDBI9->connect;
3461
eval { $dbi->execute('drop table table1') };
3462
$dbi->execute($create_table1);
3463
$dbi->setup_model;
3464
$model = $dbi->model('table1');
3465
eval{$model->execute('select * from table1')};
3466
ok(!$@);
3467

            
3468
test 'column table option';
3469
$dbi = MyDBI9->connect;
3470
eval { $dbi->execute('drop table table1') };
3471
$dbi->execute($create_table1);
3472
eval { $dbi->execute('drop table table2') };
3473
$dbi->execute($create_table2);
3474
$dbi->setup_model;
3475
$dbi->execute('insert into table1 (key1, key2) values (1, 2);');
3476
$dbi->execute('insert into table2 (key1, key3) values (1, 4);');
3477
$model = $dbi->model('table1');
3478
$result = $model->select(
3479
    column => [
3480
        $model->column('table2', {alias => 'table2_alias'})
3481
    ],
3482
    where => {'table2_alias.key3' => 4}
3483
);
3484
is_deeply($result->one, 
3485
          {'table2_alias.key1' => 1, 'table2_alias.key3' => 4});
3486

            
3487
$dbi->separator('__');
3488
$result = $model->select(
3489
    column => [
3490
        $model->column('table2', {alias => 'table2_alias'})
3491
    ],
3492
    where => {'table2_alias.key3' => 4}
3493
);
3494
is_deeply($result->one, 
3495
          {'table2_alias__key1' => 1, 'table2_alias__key3' => 4});
3496

            
3497
$dbi->separator('-');
3498
$result = $model->select(
3499
    column => [
3500
        $model->column('table2', {alias => 'table2_alias'})
3501
    ],
3502
    where => {'table2_alias.key3' => 4}
3503
);
3504
is_deeply($result->one, 
3505
          {'table2_alias-key1' => 1, 'table2_alias-key3' => 4});
3506

            
3507
test 'create_model';
3508
$dbi = DBIx::Custom->connect;
3509
eval { $dbi->execute('drop table table1') };
3510
eval { $dbi->execute('drop table table2') };
3511
$dbi->execute($create_table1);
3512
$dbi->execute($create_table2);
3513

            
3514
$dbi->create_model(
3515
    table => 'table1',
3516
    join => [
3517
       'left outer join table2 on table1.key1 = table2.key1'
3518
    ],
3519
    primary_key => ['key1']
3520
);
3521
$model2 = $dbi->create_model(
3522
    table => 'table2'
3523
);
3524
$dbi->create_model(
3525
    table => 'table3',
3526
    filter => [
3527
        key1 => {in => sub { uc $_[0] }}
3528
    ]
3529
);
3530
$dbi->setup_model;
3531
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3532
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
3533
$model = $dbi->model('table1');
3534
$result = $model->select(
3535
    column => [$model->mycolumn, $model->column('table2')],
3536
    where => {'table1.key1' => 1}
3537
);
3538
is_deeply($result->one,
3539
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
3540
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3541

            
3542
test 'model method';
3543
$dbi = DBIx::Custom->connect;
3544
eval { $dbi->execute('drop table table2') };
3545
$dbi->execute($create_table2);
3546
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
3547
$model = $dbi->create_model(
3548
    table => 'table2'
3549
);
3550
$model->method(foo => sub { shift->select(@_) });
3551
is_deeply($model->foo->one, {key1 => 1, key3 => 3});
3552

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

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

            
3573

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

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

            
3593
$dbi = DBIx::Custom->connect;
3594
eval { $dbi->execute('drop table table1') };
3595
$dbi->execute($create_table1_2);
3596
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3597
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3598

            
3599
$param = {key2 => 11, key3 => 33};
3600
$update_param = $dbi->update_param($param, {no_set => 1});
3601
$sql = <<"EOS";
3602
update table1 set $update_param
3603
where key1 = 1
3604
EOS
3605
$dbi->execute($sql, param => $param);
3606
$result = $dbi->execute('select * from table1 order by key1;', table => 'table1');
3607
$rows   = $result->all;
3608
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
3609
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
3610
                  "update param no_set");
3611

            
3612
            
3613
eval { $dbi->update_param({";" => 1}) };
3614
like($@, qr/not safety/);
3615

            
3616

            
3617
test 'update_param';
3618
$dbi = DBIx::Custom->connect;
3619
eval { $dbi->execute('drop table table1') };
3620
$dbi->execute($create_table1_2);
3621
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3622
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3623

            
3624
$param = {key2 => 11};
3625
$update_param = $dbi->assign_param($param);
3626
$sql = <<"EOS";
3627
update table1 set $update_param
3628
where key1 = 1
3629
EOS
3630
$dbi->execute($sql, param => $param, table => 'table1');
3631
$result = $dbi->execute('select * from table1 order by key1;');
3632
$rows   = $result->all;
3633
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
3634
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
3635
                  "basic");
3636

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3637
test 'join';
3638
$dbi = DBIx::Custom->connect;
3639
eval { $dbi->execute('drop table table1') };
3640
$dbi->execute($create_table1);
3641
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3642
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
3643
eval { $dbi->execute('drop table table2') };
3644
$dbi->execute($create_table2);
3645
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
3646
eval { $dbi->execute('drop table table3') };
3647
$dbi->execute('create table table3 (key3 int, key4 int);');
3648
$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4});
3649
$rows = $dbi->select(
3650
    table => 'table1',
3651
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
3652
    where   => {'table1.key2' => 2},
3653
    join  => ['left outer join table2 on table1.key1 = table2.key1']
3654
)->all;
3655
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]);
3656

            
3657
$rows = $dbi->select(
3658
    table => 'table1',
3659
    where   => {'key1' => 1},
3660
    join  => ['left outer join table2 on table1.key1 = table2.key1']
3661
)->all;
3662
is_deeply($rows, [{key1 => 1, key2 => 2}]);
3663

            
3664
eval {
3665
    $rows = $dbi->select(
3666
        table => 'table1',
3667
        column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
3668
        where   => {'table1.key2' => 2},
3669
        join  => {'table1.key1' => 'table2.key1'}
3670
    );
3671
};
3672
like ($@, qr/array/);
3673

            
3674
$rows = $dbi->select(
3675
    table => 'table1',
3676
    where   => {'key1' => 1},
3677
    join  => ['left outer join table2 on table1.key1 = table2.key1',
3678
              'left outer join table3 on table2.key3 = table3.key3']
3679
)->all;
3680
is_deeply($rows, [{key1 => 1, key2 => 2}]);
3681

            
3682
$rows = $dbi->select(
3683
    column => 'table3.key4 as table3__key4',
3684
    table => 'table1',
3685
    where   => {'table1.key1' => 1},
3686
    join  => ['left outer join table2 on table1.key1 = table2.key1',
3687
              'left outer join table3 on table2.key3 = table3.key3']
3688
)->all;
3689
is_deeply($rows, [{table3__key4 => 4}]);
3690

            
3691
$rows = $dbi->select(
3692
    column => 'table1.key1 as table1__key1',
3693
    table => 'table1',
3694
    where   => {'table3.key4' => 4},
3695
    join  => ['left outer join table2 on table1.key1 = table2.key1',
3696
              'left outer join table3 on table2.key3 = table3.key3']
3697
)->all;
3698
is_deeply($rows, [{table1__key1 => 1}]);
3699

            
3700
$dbi = DBIx::Custom->connect;
3701
eval { $dbi->execute('drop table table1') };
3702
$dbi->execute($create_table1);
3703
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3704
eval { $dbi->execute('drop table table2') };
3705
$dbi->execute($create_table2);
3706
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
3707
$rows = $dbi->select(
3708
    table => 'table1',
3709
    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",
3710
    where   => {'table1.key2' => 2},
3711
    join  => ["left outer join ${q}table2$p on ${q}table1$p.${q}key1$p = ${q}table2$p.${q}key1$p"],
3712
)->all;
3713
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}],
3714
          'quote');
3715

            
3716

            
3717
$dbi = DBIx::Custom->connect;
3718
eval { $dbi->execute('drop table table1') };
3719
$dbi->execute($create_table1);
3720
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3721
$sql = <<"EOS";
3722
left outer join (
3723
  select * from table1 as t1
3724
  where t1.key2 = (
3725
    select max(t2.key2) from table1 as t2
3726
    where t1.key1 = t2.key1
3727
  )
3728
) as latest_table1 on table1.key1 = latest_table1.key1
3729
EOS
3730
$join = [$sql];
3731
$rows = $dbi->select(
3732
    table => 'table1',
3733
    column => 'latest_table1.key1 as latest_table1__key1',
3734
    join  => $join
3735
)->all;
3736
is_deeply($rows, [{latest_table1__key1 => 1}]);
3737

            
3738
$dbi = DBIx::Custom->connect;
3739
eval { $dbi->execute('drop table table1') };
3740
eval { $dbi->execute('drop table table2') };
3741
$dbi->execute($create_table1);
3742
$dbi->execute($create_table2);
3743
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3744
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
3745
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
3746
$result = $dbi->select(
3747
    table => 'table1',
3748
    join => [
3749
        "left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1"
3750
    ]
3751
);
3752
is_deeply($result->all, [{key1 => 1, key2 => 2}]);
3753
$result = $dbi->select(
3754
    table => 'table1',
3755
    column => [{table2 => ['key3']}],
3756
    join => [
3757
        "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1"
3758
    ]
3759
);
3760
is_deeply($result->all, [{'table2.key3' => 4}]);
3761
$result = $dbi->select(
3762
    table => 'table1',
3763
    column => [{table2 => ['key3']}],
3764
    join => [
3765
        "left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'"
3766
    ]
3767
);
3768
is_deeply($result->all, [{'table2.key3' => 4}]);
3769

            
3770
$dbi = DBIx::Custom->connect;
3771
eval { $dbi->execute('drop table table1') };
3772
eval { $dbi->execute('drop table table2') };
3773
$dbi->execute($create_table1);
3774
$dbi->execute($create_table2);
3775
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3776
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
3777
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
3778
$result = $dbi->select(
3779
    table => 'table1',
3780
    column => [{table2 => ['key3']}],
3781
    join => [
3782
        {
3783
            clause => "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1",
3784
            table => ['table1', 'table2']
3785
        }
3786
    ]
3787
);
3788
is_deeply($result->all, [{'table2.key3' => 4}]);
3789

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3790
test 'Model class';
3791
use MyDBI1;
3792
$dbi = MyDBI1->connect;
3793
eval { $dbi->execute('drop table table1') };
3794
$dbi->execute($create_table1);
3795
$model = $dbi->model('table1');
3796
$model->insert({key1 => 'a', key2 => 'b'});
3797
is_deeply($model->list->all, [{key1 => 'a', key2 => 'b'}], 'basic');
3798
eval { $dbi->execute('drop table table2') };
3799
$dbi->execute($create_table2);
3800
$model = $dbi->model('table2');
3801
$model->insert({key1 => 'a'});
3802
is_deeply($model->list->all, [{key1 => 'a', key3 => undef}], 'basic');
3803
is($dbi->models->{'table1'}, $dbi->model('table1'));
3804
is($dbi->models->{'table2'}, $dbi->model('table2'));
3805

            
3806
$dbi = MyDBI4->connect;
3807
eval { $dbi->execute('drop table table1') };
3808
$dbi->execute($create_table1);
3809
$model = $dbi->model('table1');
3810
$model->insert({key1 => 'a', key2 => 'b'});
3811
is_deeply($model->list->all, [{key1 => 'a', key2 => 'b'}], 'basic');
3812
eval { $dbi->execute('drop table table2') };
3813
$dbi->execute($create_table2);
3814
$model = $dbi->model('table2');
3815
$model->insert({key1 => 'a'});
3816
is_deeply($model->list->all, [{key1 => 'a', key3 => undef}], 'basic');
3817

            
3818
$dbi = MyDBI5->connect;
3819
eval { $dbi->execute('drop table table1') };
3820
eval { $dbi->execute('drop table table2') };
3821
$dbi->execute($create_table1);
3822
$dbi->execute($create_table2);
3823
$model = $dbi->model('table2');
3824
$model->insert({key1 => 'a'});
3825
is_deeply($model->list->all, [{key1 => 'a', key3 => undef}], 'include all model');
3826
$dbi->insert(table => 'table1', param => {key1 => 1});
3827
$model = $dbi->model('table1');
3828
is_deeply($model->list->all, [{key1 => 1, key2 => undef}], 'include all model');
3829

            
3830
test 'primary_key';
3831
use MyDBI1;
3832
$dbi = MyDBI1->connect;
3833
$model = $dbi->model('table1');
3834
$model->primary_key(['key1', 'key2']);
3835
is_deeply($model->primary_key, ['key1', 'key2']);
3836

            
3837
test 'columns';
3838
use MyDBI1;
3839
$dbi = MyDBI1->connect;
3840
$model = $dbi->model('table1');
3841
$model->columns(['key1', 'key2']);
3842
is_deeply($model->columns, ['key1', 'key2']);
3843

            
3844
test 'setup_model';
3845
use MyDBI1;
3846
$dbi = MyDBI1->connect;
3847
eval { $dbi->execute('drop table table1') };
3848
eval { $dbi->execute('drop table table2') };
3849

            
3850
$dbi->execute($create_table1);
3851
$dbi->execute($create_table2);
3852
$dbi->setup_model;
3853
is_deeply([sort @{$dbi->model('table1')->columns}], ['key1', 'key2']);
3854
is_deeply([sort @{$dbi->model('table2')->columns}], ['key1', 'key3']);
3855

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

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

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