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

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

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

            
15
plan 'no_plan';
16

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

            
21
    use strict;
22
    use warnings;
23

            
24
    use base 'DBIx::Custom';
25

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

            
37
    package MyModel2::Base1;
38

            
39
    use strict;
40
    use warnings;
41

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

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

            
46
    use strict;
47
    use warnings;
48

            
49
    use base 'MyModel2::Base1';
50

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

            
57
    sub list { shift->select; }
58

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

            
61
    use strict;
62
    use warnings;
63

            
64
    use base 'MyModel2::Base1';
65

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

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

            
77
    use strict;
78
    use warnings;
79

            
80
    use base 'DBIx::Custom';
81

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

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

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

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

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

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

            
added common test executing ...
Yuki Kimoto authored on 2011-08-07
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);
added SQL Server test
Yuki Kimoto authored on 2011-08-14
203
$DB::single = 1;
test cleanup
Yuki Kimoto authored on 2011-08-10
204
$dbi->type_rule(
205
    into1 => {
206
        $date_typename => sub { '2010-' . $_[0] }
207
    }
208
);
209
$dbi->insert({key1 => '01-01'}, table => 'table1');
210
$result = $dbi->select(table => 'table1');
211
is($result->one->{key1}, '2010-01-01');
212

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

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

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

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

            
295

            
296

            
297

            
298

            
299

            
300

            
301

            
302

            
303

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

            
328

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
631

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
807
$rows = $result->all;
fixed SQL Server tests
Yuki Kimoto authored on 2011-08-14
808
is_deeply($rows, [{key1 => '2011-10-14 12:19:18' . $datetime_suffix, key2 => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-08
809

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1032

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

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

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

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

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

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

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

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

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

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

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

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

            
1128

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1261

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

            
1267
$dbi->begin_work;
1268

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

            
1275
$dbi->rollback if $@;
1276

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

            
1281
$dbi->begin_work;
1282

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

            
1288
$dbi->commit unless $@;
1289

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1940

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

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

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

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

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

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

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

            
2008
$where = $dbi->where;
2009
$where->clause(['and', ':key1{=}']);
2010
$where->param({key1 => 5});
2011
$where->if(sub { ($_[0] || '') eq 5 });
2012
$where->map;
2013
$result = $dbi->execute("select * from table1 $where", {key1 => 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 => 7});
2020
$where->if(sub { ($_[0] || '') eq 5 });
2021
$where->map;
2022
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
2023
$row = $result->all;
2024
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
2025

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

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

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

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

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

            
2074
$where = $dbi->where;
2075
$where->param({id => [1, 2], author => 'Ken', price => 1900});
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', {if => sub { $_[0] eq 1900 }}]
test cleanup
Yuki Kimoto authored on 2011-08-10
2080
);
test cleanup
Yuki Kimoto authored on 2011-08-10
2081
is_deeply($where->param, {'table1.id' => [1, 2], 'table1.author' => '%Ken%',
2082
  'table1.price' => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
2083

            
2084
$where = $dbi->where;
2085
$where->if('length');
2086
$where->param({id => ['', ''], author => 'Ken', price => 1900});
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', {if => sub { $_[0] eq 1900 }}]
test cleanup
Yuki Kimoto authored on 2011-08-10
2091
);
test cleanup
Yuki Kimoto authored on 2011-08-10
2092
is_deeply($where->param, {'table1.id' => [$dbi->not_exists, $dbi->not_exists], 'table1.author' => '%Ken%',
2093
  'table1.price' => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
2094

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

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

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

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

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

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

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

            
2142
$dbi->apply_filter(
2143

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2421

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

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

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

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

            
2480

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2674

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

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

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

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

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

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

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

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

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

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

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

            
2811

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

            
2827

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

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

            
2848

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

            
2867

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

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

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

            
2907

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

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

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

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

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

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

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

            
3001

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

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

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

            
3027

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
3348

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

            
3357

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
3534

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

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

            
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, key3 => 33};
3561
$update_param = $dbi->update_param($param, {no_set => 1});
3562
$sql = <<"EOS";
3563
update table1 set $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 => 33, key4 => 4, key5 => 5},
3570
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
3571
                  "update param no_set");
3572

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

            
3577

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

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

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

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

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

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

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

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

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

            
3677

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

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

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

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

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

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

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

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

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

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

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

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

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