DBIx-Custom / t / common.t /
Newer Older
3514 lines | 112.637kb
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";
7

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

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

            
14
plan 'no_plan';
15

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

            
20
    use strict;
21
    use warnings;
22

            
23
    use base 'DBIx::Custom';
24

            
25
    sub connect {
26
        my $self = shift->SUPER::connect(@_);
27
        
28
        $self->include_model(
29
            MyModel2 => [
30
                'book',
31
                {class => 'Company', name => 'company'}
32
            ]
33
        );
34
    }
35

            
36
    package MyModel2::Base1;
37

            
38
    use strict;
39
    use warnings;
40

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

            
43
    package MyModel2::book;
44

            
45
    use strict;
46
    use warnings;
47

            
48
    use base 'MyModel2::Base1';
49

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

            
56
    sub list { shift->select; }
57

            
58
    package MyModel2::Company;
59

            
60
    use strict;
61
    use warnings;
62

            
63
    use base 'MyModel2::Base1';
64

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

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

            
76
    use strict;
77
    use warnings;
78

            
79
    use base 'DBIx::Custom';
80

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

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

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

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

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

            
198

            
199

            
200

            
201

            
202

            
203

            
204

            
205

            
206

            
207

            
208

            
209

            
210

            
211
test 'type_rule into';
212
$dbi = DBIx::Custom->connect;
213
eval { $dbi->execute('drop table table1') };
214
$dbi->execute($create_table1_type);
215
$dbi->type_rule(
216
    into1 => {
217
        $date_typename => sub { '2010-' . $_[0] }
218
    }
219
);
220
$dbi->insert({key1 => '01-01'}, table => 'table1');
221
$result = $dbi->select(table => 'table1');
222
is($result->one->{key1}, '2010-01-01');
223

            
224
$dbi = DBIx::Custom->connect;
225
eval { $dbi->execute('drop table table1') };
226
$dbi->execute($create_table1_type);
227
$dbi->type_rule(
228
    into1 => [
229
         [$date_typename, $datetime_typename] => sub {
230
            my $value = shift;
231
            $value =~ s/02/03/g;
232
            return $value;
233
         }
234
    ]
235
);
236
$dbi->insert({key1 => '2010-01-02', key2 => '2010-01-01 01:01:02'}, table => 'table1');
237
$result = $dbi->select(table => 'table1');
238
$row = $result->one;
239
is($row->{key1}, '2010-01-03');
240
is($row->{key2}, '2010-01-01 01:01:03');
241

            
242
$dbi = DBIx::Custom->connect;
243
eval { $dbi->execute('drop table table1') };
244
$dbi->execute($create_table1_type);
245
$dbi->insert({key1 => '2010-01-03', key2 => '2010-01-01 01:01:03'}, table => 'table1');
246
$dbi->type_rule(
247
    into1 => [
248
        [$date_typename, $datetime_typename] => sub {
249
            my $value = shift;
250
            $value =~ s/02/03/g;
251
            return $value;
252
        }
253
    ]
254
);
255
$result = $dbi->execute(
256
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
257
    param => {key1 => '2010-01-03', 'table1.key2' => '2010-01-01 01:01:02'}
258
);
259
$row = $result->one;
260
is($row->{key1}, '2010-01-03');
261
is($row->{key2}, '2010-01-01 01:01:03');
262

            
263
$dbi = DBIx::Custom->connect;
264
eval { $dbi->execute('drop table table1') };
265
$dbi->execute($create_table1_type);
266
$dbi->insert({key1 => '2010-01-03', key2 => '2010-01-01 01:01:03'}, table => 'table1');
267
$dbi->type_rule(
268
    into1 => [
269
        [$date_typename, $datetime_typename] => sub {
270
            my $value = shift;
271
            $value =~ s/02/03/g;
272
            return $value;
273
        }
274
    ]
275
);
276
$result = $dbi->execute(
277
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
278
    param => {key1 => '2010-01-02', 'table1.key2' => '2010-01-01 01:01:02'},
279
    table => 'table1'
280
);
281
$row = $result->one;
282
is($row->{key1}, '2010-01-03');
283
is($row->{key2}, '2010-01-01 01:01:03');
284

            
285
$dbi = DBIx::Custom->connect;
286
eval { $dbi->execute('drop table table1') };
287
$dbi->execute($create_table1_type);
288
$dbi->register_filter(convert => sub {
289
    my $value = shift;
290
    $value =~ s/02/03/;
291
    return $value;
292
});
293
$dbi->type_rule(
294
    from1 => {
295
        $date_datatype => 'convert',
296
    },
297
    into1 => {
298
        $date_typename => 'convert',
299
    }
300
);
301
$dbi->insert({key1 => '2010-02-02'}, table => 'table1');
302
$result = $dbi->select(table => 'table1');
303
is($result->fetch->[0], '2010-03-03');
304

            
305

            
306

            
307

            
308

            
309

            
310

            
311

            
312

            
313

            
314

            
315

            
316

            
317

            
318

            
319

            
320

            
321

            
322

            
323

            
324

            
325

            
326

            
327

            
328

            
329

            
330

            
331

            
332

            
333

            
334

            
335

            
336

            
337

            
338

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

            
340
# Create table
test cleanup
Yuki Kimoto authored on 2011-08-10
341
eval { $dbi->execute('drop table table1') };
added common test executing ...
Yuki Kimoto authored on 2011-08-07
342
$dbi->execute($create_table1);
343
$model = $dbi->create_model(table => 'table1');
344
$model->insert({key1 => 1, key2 => 2});
345
is_deeply($model->select->all, [{key1 => 1, key2 => 2}]);
346

            
cleanup test
Yuki Kimoto authored on 2011-08-08
347
test 'DBIx::Custom::Result test';
348
$dbi->delete_all(table => 'table1');
349
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
350
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
351
$source = "select key1, key2 from table1";
352
$query = $dbi->create_query($source);
353
$result = $dbi->execute($query);
354

            
355
@rows = ();
356
while (my $row = $result->fetch) {
357
    push @rows, [@$row];
358
}
359
is_deeply(\@rows, [[1, 2], [3, 4]], "fetch");
360

            
361
$result = $dbi->execute($query);
362
@rows = ();
363
while (my $row = $result->fetch_hash) {
364
    push @rows, {%$row};
365
}
366
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "fetch_hash");
367

            
368
$result = $dbi->execute($query);
369
$rows = $result->fetch_all;
370
is_deeply($rows, [[1, 2], [3, 4]], "fetch_all");
371

            
372
$result = $dbi->execute($query);
373
$rows = $result->fetch_hash_all;
374
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "all");
375

            
376
test 'Insert query return value';
377
$source = "insert into table1 {insert_param key1 key2}";
378
$query = $dbi->execute($source, {}, query => 1);
379
$ret_val = $dbi->execute($query, param => {key1 => 1, key2 => 2});
380
ok($ret_val);
381

            
382
test 'Direct query';
383
$dbi->delete_all(table => 'table1');
384
$insert_source = "insert into table1 {insert_param key1 key2}";
385
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2});
386
$result = $dbi->execute('select * from table1;');
387
$rows = $result->all;
388
is_deeply($rows, [{key1 => 1, key2 => 2}]);
389

            
390
test 'Filter basic';
391
$dbi->delete_all(table => 'table1');
392
$dbi->register_filter(twice       => sub { $_[0] * 2}, 
393
                    three_times => sub { $_[0] * 3});
394

            
395
$insert_source  = "insert into table1 {insert_param key1 key2};";
396
$insert_query = $dbi->execute($insert_source, {}, query => 1);
397
$insert_query->filter({key1 => 'twice'});
398
$dbi->execute($insert_query, param => {key1 => 1, key2 => 2});
399
$result = $dbi->execute('select * from table1;');
400
$rows = $result->filter({key2 => 'three_times'})->all;
401
is_deeply($rows, [{key1 => 2, key2 => 6}], "filter fetch_filter");
402

            
403
test 'Filter in';
404
$dbi->delete_all(table => 'table1');
405
$insert_source  = "insert into table1 {insert_param key1 key2};";
406
$insert_query = $dbi->execute($insert_source, {}, query => 1);
407
$dbi->execute($insert_query, param => {key1 => 2, key2 => 4});
408
$select_source = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}";
409
$select_query = $dbi->execute($select_source,{}, query => 1);
410
$select_query->filter({'table1.key1' => 'twice'});
411
$result = $dbi->execute($select_query, param => {'table1.key1' => [1,5], 'table1.key2' => [2,4]});
412
$rows = $result->all;
413
is_deeply($rows, [{key1 => 2, key2 => 4}], "filter");
414

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

            
421
$source = "select * from table1 where key1 = :key1 and {<> key2} and {< key3} and {> key4} and {>= key5};";
422
$query = $dbi->execute($source, {}, query => 1);
423
$result = $dbi->execute($query, param => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
424
$rows = $result->all;
425
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1");
426

            
427
$source = "select * from table1 where key1 = :key1 and {<> key2} and {< key3} and {> key4} and {>= key5};";
428
$query = $dbi->execute($source, {}, query => 1);
429
$result = $dbi->execute($query, {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
430
$rows = $result->all;
431
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1");
432

            
433
$source = "select * from table1 where {<= key1} and {like key2};";
434
$query = $dbi->execute($source, {}, query => 1);
435
$result = $dbi->execute($query, param => {key1 => 1, key2 => '%2%'});
436
$rows = $result->all;
437
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag2");
438

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

            
445
$source = "select * from table1 where {in key1 2};";
446
$query = $dbi->execute($source, {}, query => 1);
447
$result = $dbi->execute($query, param => {key1 => [9, 1]});
448
$rows = $result->all;
449
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic");
450

            
451
test 'DBIx::Custom::SQLTemplate insert tag';
452
$dbi->delete_all(table => 'table1');
453
$insert_source = 'insert into table1 {insert_param key1 key2 key3 key4 key5}';
454
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
455

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

            
460
test 'DBIx::Custom::SQLTemplate update tag';
461
$dbi->delete_all(table => 'table1');
462
$insert_source = "insert into table1 {insert_param key1 key2 key3 key4 key5}";
463
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
464
$dbi->execute($insert_source, param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
465

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

            
469
$result = $dbi->execute('select * from table1 order by key1;');
470
$rows = $result->all;
471
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5},
472
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "basic");
473

            
cleanup test
Yuki Kimoto authored on 2011-08-08
474
test 'Named placeholder';
475
$dbi->execute('drop table table1');
476
$dbi->execute($create_table1_2);
477
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
478
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
479

            
480
$source = "select * from table1 where key1 = :key1 and key2 = :key2";
481
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
482
$rows = $result->all;
483
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
484

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

            
490
$source = "select * from table1 where key1 = :key1 or key1 = :key1";
491
$result = $dbi->execute($source, param => {key1 => [1, 2]});
492
$rows = $result->all;
493
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
494

            
495
$source = "select * from table1 where key1 = :table1.key1 and key2 = :table1.key2";
496
$result = $dbi->execute(
497
    $source,
498
    param => {'table1.key1' => 1, 'table1.key2' => 1},
499
    filter => {'table1.key2' => sub { $_[0] * 2 }}
500
);
501
$rows = $result->all;
502
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
503

            
504
$dbi->execute('drop table table1');
505
$dbi->execute($create_table1);
506
$dbi->insert(table => 'table1', param => {key1 => '2011-10-14 12:19:18', key2 => 2});
507
$source = "select * from table1 where key1 = '2011-10-14 12:19:18' and key2 = :key2";
508
$result = $dbi->execute(
509
    $source,
510
    param => {'key2' => 2},
511
);
512

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

            
516
$dbi->delete_all(table => 'table1');
517
$dbi->insert(table => 'table1', param => {key1 => 'a:b c:d', key2 => 2});
518
$source = "select * from table1 where key1 = 'a\\:b c\\:d' and key2 = :key2";
519
$result = $dbi->execute(
520
    $source,
521
    param => {'key2' => 2},
522
);
523
$rows = $result->all;
524
is_deeply($rows, [{key1 => 'a:b c:d', key2 => 2}]);
525

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
533
test 'insert';
cleanup test
Yuki Kimoto authored on 2011-08-10
534
eval { $dbi->execute('drop table table1') };
535
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
536
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
537
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
538
$result = $dbi->execute('select * from table1;');
539
$rows   = $result->all;
540
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
541

            
542
$dbi->execute('delete from table1');
543
$dbi->register_filter(
544
    twice       => sub { $_[0] * 2 },
545
    three_times => sub { $_[0] * 3 }
546
);
547
$dbi->default_bind_filter('twice');
548
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => 'three_times'});
549
$result = $dbi->execute('select * from table1;');
550
$rows   = $result->all;
551
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter");
552
$dbi->default_bind_filter(undef);
553

            
cleanup test
Yuki Kimoto authored on 2011-08-10
554
$dbi->execute('drop table table1');
555
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
556
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, append => '   ');
557
$rows = $dbi->select(table => 'table1')->all;
558
is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append');
559

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

            
563
eval{$dbi->insert(table => 'table', param => {';' => 1})};
564
like($@, qr/safety/);
565

            
cleanup test
Yuki Kimoto authored on 2011-08-10
566
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
567
$dbi->execute($create_table_reserved);
cleanup test
Yuki Kimoto authored on 2011-08-10
568
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
569
$dbi->insert(table => 'table', param => {select => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
570
$result = $dbi->execute("select * from ${q}table$p");
cleanup test
Yuki Kimoto authored on 2011-08-10
571
$rows   = $result->all;
test cleanup
Yuki Kimoto authored on 2011-08-10
572
is_deeply($rows, [{select => 2, update => undef}], "reserved word");
cleanup test
Yuki Kimoto authored on 2011-08-10
573

            
cleanup test
Yuki Kimoto authored on 2011-08-10
574
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
575
$dbi->execute($create_table1);
576
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
577
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
578
$result = $dbi->execute('select * from table1;');
579
$rows   = $result->all;
580
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
581

            
cleanup test
Yuki Kimoto authored on 2011-08-10
582
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
583
$dbi->execute($create_table1);
584
$dbi->insert(table => 'table1', param => {key1 => \"'1'", key2 => 2});
585
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
586
$result = $dbi->execute('select * from table1;');
587
$rows   = $result->all;
588
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
589

            
test cleanup
Yuki Kimoto authored on 2011-08-10
590
test 'update';
591
eval { $dbi->execute('drop table table1') };
592
$dbi->execute($create_table1_2);
593
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
594
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
595
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
596
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
597
$rows   = $result->all;
598
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
599
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
600
                  "basic");
601
                  
602
$dbi->execute("delete from table1");
603
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
604
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
605
$dbi->update(table => 'table1', param => {key2 => 12}, where => {key2 => 2, key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
606
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
607
$rows   = $result->all;
608
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
609
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
610
                  "update key same as search key");
611

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

            
619
$dbi->execute("delete from table1");
620
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
621
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
622
$dbi->register_filter(twice => sub { $_[0] * 2 });
623
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1},
624
              filter => {key2 => sub { $_[0] * 2 }});
test cleanup
Yuki Kimoto authored on 2011-08-10
625
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
626
$rows   = $result->all;
627
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5},
628
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
629
                  "filter");
630

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

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

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

            
639
eval { $dbi->execute('drop table table1') };
640
$dbi->execute($create_table1);
641
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
642
$where = $dbi->where;
643
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
644
$where->param({key1 => 1, key2 => 2});
645
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
646
$result = $dbi->select(table => 'table1');
647
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
648

            
649
eval { $dbi->execute('drop table table1') };
650
$dbi->execute($create_table1);
651
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
652
$dbi->update(
653
    table => 'table1',
654
    param => {key1 => 3},
655
    where => [
656
        ['and', 'key1 = :key1', 'key2 = :key2'],
657
        {key1 => 1, key2 => 2}
658
    ]
659
);
660
$result = $dbi->select(table => 'table1');
661
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
662

            
663
eval { $dbi->execute('drop table table1') };
664
$dbi->execute($create_table1);
665
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
666
$where = $dbi->where;
667
$where->clause(['and', 'key2 = :key2']);
668
$where->param({key2 => 2});
669
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
670
$result = $dbi->select(table => 'table1');
671
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
672

            
673
eval{$dbi->update(table => 'table1', param => {';' => 1})};
674
like($@, qr/safety/);
675

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

            
679
eval { $dbi->execute('drop table table1') };
680
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
681
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
682
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
683
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
684
$dbi->insert(table => 'table', param => {select => 1});
685
$dbi->update(table => 'table', where => {select => 1}, param => {update => 2});
686
$result = $dbi->execute("select * from ${q}table$p");
687
$rows   = $result->all;
688
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
689

            
690
eval {$dbi->update_all(table => 'table', param => {';' => 2}) };
691
like($@, qr/safety/);
692

            
693
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
694
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
695
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
696
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
697
$dbi->insert(table => 'table', param => {select => 1});
698
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
699
$result = $dbi->execute("select * from ${q}table$p");
700
$rows   = $result->all;
701
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
702

            
703
eval { $dbi->execute('drop table table1') };
704
$dbi->execute($create_table1_2);
705
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
706
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
707
$dbi->update({key2 => 11}, table => 'table1', where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
708
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
709
$rows   = $result->all;
710
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
711
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
712
                  "basic");
713

            
714
eval { $dbi->execute('drop table table1') };
715
$dbi->execute($create_table1_2);
716
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
717
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
718
$dbi->update(table => 'table1', param => {key2 => \"'11'"}, where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
719
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
720
$rows   = $result->all;
721
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
722
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
723
                  "basic");
724

            
725
test 'update_all';
726
eval { $dbi->execute('drop table table1') };
727
$dbi->execute($create_table1_2);
728
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
729
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
730
$dbi->register_filter(twice => sub { $_[0] * 2 });
731
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'});
732
$result = $dbi->execute('select * from table1;');
733
$rows   = $result->all;
734
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
735
                  {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
736
                  "filter");
737

            
738

            
739
test 'delete';
740
eval { $dbi->execute('drop table table1') };
741
$dbi->execute($create_table1);
742
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
743
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
744
$dbi->delete(table => 'table1', where => {key1 => 1});
745
$result = $dbi->execute('select * from table1;');
746
$rows   = $result->all;
747
is_deeply($rows, [{key1 => 3, key2 => 4}], "basic");
748

            
749
$dbi->execute("delete from table1;");
750
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
751
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
752
$dbi->register_filter(twice => sub { $_[0] * 2 });
753
$dbi->delete(table => 'table1', where => {key2 => 1}, filter => {key2 => 'twice'});
754
$result = $dbi->execute('select * from table1;');
755
$rows   = $result->all;
756
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter");
757

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

            
760
$dbi->delete_all(table => 'table1');
761
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
762
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
763
$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2});
764
$rows = $dbi->select(table => 'table1')->all;
765
is_deeply($rows, [{key1 => 3, key2 => 4}], "delete multi key");
766

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

            
770
eval { $dbi->execute('drop table table1') };
771
$dbi->execute($create_table1);
772
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
773
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
774
$where = $dbi->where;
775
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
776
$where->param({ke1 => 1, key2 => 2});
777
$dbi->delete(table => 'table1', where => $where);
778
$result = $dbi->select(table => 'table1');
779
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
780

            
781
eval { $dbi->execute('drop table table1') };
782
$dbi->execute($create_table1);
783
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
784
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
785
$dbi->delete(
786
    table => 'table1',
787
    where => [
788
        ['and', 'key1 = :key1', 'key2 = :key2'],
789
        {ke1 => 1, key2 => 2}
790
    ]
791
);
792
$result = $dbi->select(table => 'table1');
793
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
794

            
795
eval { $dbi->execute('drop table table1') };
test cleanup
Yuki Kimoto authored on 2011-08-10
796
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
797
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
798
$dbi->delete(table => 'table1', where => {key1 => 1}, prefix => '    ');
799
$result = $dbi->execute('select * from table1;');
800
$rows   = $result->all;
801
is_deeply($rows, [], "basic");
802

            
803
test 'delete error';
804
eval { $dbi->execute('drop table table1') };
805
$dbi->execute($create_table1);
806
eval{$dbi->delete(table => 'table1')};
807
like($@, qr/"where" must be specified/,
808
         "where key-value pairs not specified");
809

            
810
eval{$dbi->delete(table => 'table1', where => {';' => 1})};
811
like($@, qr/safety/);
812

            
test cleanup
Yuki Kimoto authored on 2011-08-10
813
$dbi = undef;
test cleanup
Yuki Kimoto authored on 2011-08-10
814
$dbi = DBIx::Custom->connect;
815
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
816
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
817
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
818
$dbi->insert(table => 'table', param => {select => 1});
819
$dbi->delete(table => 'table', where => {select => 1});
820
$result = $dbi->execute("select * from ${q}table$p");
821
$rows   = $result->all;
822
is_deeply($rows, [], "reserved word");
823

            
824
test 'delete_all';
825
eval { $dbi->execute('drop table table1') };
826
$dbi->execute($create_table1);
827
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
828
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
829
$dbi->delete_all(table => 'table1');
830
$result = $dbi->execute('select * from table1;');
831
$rows   = $result->all;
832
is_deeply($rows, [], "basic");
833

            
834

            
835
test 'select';
836
eval { $dbi->execute('drop table table1') };
837
$dbi->execute($create_table1);
838
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
839
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
840
$rows = $dbi->select(table => 'table1')->all;
841
is_deeply($rows, [{key1 => 1, key2 => 2},
842
                  {key1 => 3, key2 => 4}], "table");
843

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

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

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

            
853
$rows = $dbi->select(table => 'table1', append => "order by key1 desc limit 1")->all;
854
is_deeply($rows, [{key1 => 3, key2 => 4}], "append statement");
855

            
856
$dbi->register_filter(decrement => sub { $_[0] - 1 });
857
$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'})
858
            ->all;
859
is_deeply($rows, [{key1 => 1, key2 => 2}], "filter");
860

            
test cleanup
Yuki Kimoto authored on 2011-08-10
861
eval { $dbi->execute("drop table table2") };
862
$dbi->execute($create_table2);
test cleanup
Yuki Kimoto authored on 2011-08-10
863
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
864
$rows = $dbi->select(
865
    table => [qw/table1 table2/],
866
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
867
    where   => {'table1.key2' => 2},
868
    relation  => {'table1.key1' => 'table2.key1'}
869
)->all;
870
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : exists where");
871

            
872
$rows = $dbi->select(
873
    table => [qw/table1 table2/],
874
    column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
875
    relation  => {'table1.key1' => 'table2.key1'}
876
)->all;
877
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : no exists where");
878

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

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

            
891
test 'fetch filter';
892
eval { $dbi->execute('drop table table1') };
893
$dbi->register_filter(
894
    twice       => sub { $_[0] * 2 },
895
    three_times => sub { $_[0] * 3 }
896
);
897
$dbi->default_fetch_filter('twice');
898
$dbi->execute($create_table1);
899
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
900
$result = $dbi->select(table => 'table1');
901
$result->filter({key1 => 'three_times'});
902
$row = $result->one;
903
is_deeply($row, {key1 => 3, key2 => 4}, "default_fetch_filter and filter");
904

            
905
test 'filters';
906
$dbi = DBIx::Custom->new;
907

            
908
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')),
909
   'あ', "decode_utf8");
910

            
911
is($dbi->filters->{encode_utf8}->('あ'),
912
   encode_utf8('あ'), "encode_utf8");
913

            
cleanup test
Yuki Kimoto authored on 2011-08-10
914
test 'transaction1';
test cleanup
Yuki Kimoto authored on 2011-08-10
915
$dbi = DBIx::Custom->connect;
916
eval { $dbi->execute('drop table table1') };
917
$dbi->execute($create_table1);
918
$dbi->dbh->begin_work;
919
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
920
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
921
$dbi->dbh->commit;
922
$result = $dbi->select(table => 'table1');
923
is_deeply(scalar $result->all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}],
924
          "commit");
925

            
926
$dbi = DBIx::Custom->connect;
927
eval { $dbi->execute('drop table table1') };
928
$dbi->execute($create_table1);
929
$dbi->dbh->begin_work(0);
930
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
931
$dbi->dbh->rollback;
932

            
933
$result = $dbi->select(table => 'table1');
934
ok(! $result->fetch_first, "rollback");
935

            
936
test 'execute';
937
eval { $dbi->execute('drop table table1') };
938
$dbi->execute($create_table1);
939
{
940
    local $Carp::Verbose = 0;
941
    eval{$dbi->execute('select * frm table1')};
942
    like($@, qr/\Qselect * frm table1;/, "fail prepare");
943
    like($@, qr/\.t /, "fail : not verbose");
944
}
945
{
946
    local $Carp::Verbose = 1;
947
    eval{$dbi->execute('select * frm table1')};
948
    like($@, qr/Custom.*\.t /s, "fail : verbose");
949
}
950

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

            
954
$query = $dbi->execute('select * from table1 where key1 = :key1', {}, query => 1);
955
$dbi->dbh->disconnect;
956
eval{$dbi->execute($query, param => {key1 => {a => 1}})};
957
ok($@, "execute fail");
958

            
959
{
960
    local $Carp::Verbose = 0;
961
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
962
    like($@, qr/\Q.t /, "caller spec : not vebose");
963
}
964
{
965
    local $Carp::Verbose = 1;
966
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
967
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
968
}
969

            
970

            
cleanup test
Yuki Kimoto authored on 2011-08-10
971
test 'transaction2';
test cleanup
Yuki Kimoto authored on 2011-08-10
972
$dbi = DBIx::Custom->connect;
973
eval { $dbi->execute('drop table table1') };
974
$dbi->execute($create_table1);
975

            
976
$dbi->begin_work;
977

            
978
eval {
979
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
980
    die "Error";
981
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
982
};
983

            
984
$dbi->rollback if $@;
985

            
986
$result = $dbi->select(table => 'table1');
987
$rows = $result->all;
988
is_deeply($rows, [], "rollback");
989

            
990
$dbi->begin_work;
991

            
992
eval {
993
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
994
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
995
};
996

            
997
$dbi->commit unless $@;
998

            
999
$result = $dbi->select(table => 'table1');
1000
$rows = $result->all;
1001
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "commit");
1002

            
1003
$dbi->dbh->{AutoCommit} = 0;
1004
eval{ $dbi->begin_work };
1005
ok($@, "exception");
1006
$dbi->dbh->{AutoCommit} = 1;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
1007

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1008
test 'cache';
1009
eval { $dbi->execute('drop table table1') };
1010
$dbi->cache(1);
1011
$dbi->execute($create_table1);
1012
$source = 'select * from table1 where key1 = :key1 and key2 = :key2;';
1013
$dbi->execute($source, {}, query => 1);
1014
is_deeply($dbi->{_cached}->{$source}, 
1015
          {sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2'], tables => []}, "cache");
1016

            
1017
eval { $dbi->execute('drop table table1') };
1018
$dbi->execute($create_table1);
1019
$dbi->{_cached} = {};
1020
$dbi->cache(0);
1021
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1022
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
1023

            
1024
test 'execute';
1025
eval { $dbi->execute('drop table table1') };
1026
$dbi->execute($create_table1);
1027
{
1028
    local $Carp::Verbose = 0;
1029
    eval{$dbi->execute('select * frm table1')};
1030
    like($@, qr/\Qselect * frm table1;/, "fail prepare");
1031
    like($@, qr/\.t /, "fail : not verbose");
1032
}
1033
{
1034
    local $Carp::Verbose = 1;
1035
    eval{$dbi->execute('select * frm table1')};
1036
    like($@, qr/Custom.*\.t /s, "fail : verbose");
1037
}
1038

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

            
1042
$query = $dbi->execute('select * from table1 where key1 = :key1', {}, query => 1);
1043
$dbi->dbh->disconnect;
1044
eval{$dbi->execute($query, param => {key1 => {a => 1}})};
1045
ok($@, "execute fail");
1046

            
1047
{
1048
    local $Carp::Verbose = 0;
1049
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
1050
    like($@, qr/\Q.t /, "caller spec : not vebose");
1051
}
1052
{
1053
    local $Carp::Verbose = 1;
1054
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
1055
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
1056
}
1057

            
1058
test 'method';
1059
$dbi->method(
1060
    one => sub { 1 }
1061
);
1062
$dbi->method(
1063
    two => sub { 2 }
1064
);
1065
$dbi->method({
1066
    twice => sub {
1067
        my $self = shift;
1068
        return $_[0] * 2;
1069
    }
1070
});
1071

            
1072
is($dbi->one, 1, "first");
1073
is($dbi->two, 2, "second");
1074
is($dbi->twice(5), 10 , "second");
1075

            
1076
eval {$dbi->XXXXXX};
1077
ok($@, "not exists");
1078

            
1079
test 'out filter';
1080
$dbi = DBIx::Custom->connect;
1081
eval { $dbi->execute('drop table table1') };
1082
$dbi->execute($create_table1);
1083
$dbi->register_filter(twice => sub { $_[0] * 2 });
1084
$dbi->register_filter(three_times => sub { $_[0] * 3});
1085
$dbi->apply_filter(
1086
    'table1', 'key1' => {out => 'twice', in => 'three_times'}, 
1087
              'key2' => {out => 'three_times', in => 'twice'});
1088
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1089
$result = $dbi->execute('select * from table1;');
1090
$row   = $result->fetch_hash_first;
1091
is_deeply($row, {key1 => 2, key2 => 6}, "insert");
1092
$result = $dbi->select(table => 'table1');
1093
$row   = $result->one;
1094
is_deeply($row, {key1 => 6, key2 => 12}, "insert");
1095

            
1096
$dbi = DBIx::Custom->connect;
1097
eval { $dbi->execute('drop table table1') };
1098
$dbi->execute($create_table1);
1099
$dbi->register_filter(twice => sub { $_[0] * 2 });
1100
$dbi->register_filter(three_times => sub { $_[0] * 3});
1101
$dbi->apply_filter(
1102
    'table1', 'key1' => {out => 'twice', in => 'three_times'}, 
1103
              'key2' => {out => 'three_times', in => 'twice'});
1104
$dbi->apply_filter(
1105
    'table1', 'key1' => {out => undef}
1106
); 
1107
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1108
$result = $dbi->execute('select * from table1;');
1109
$row   = $result->one;
1110
is_deeply($row, {key1 => 1, key2 => 6}, "insert");
1111

            
1112
$dbi = DBIx::Custom->connect;
1113
eval { $dbi->execute('drop table table1') };
1114
$dbi->execute($create_table1);
1115
$dbi->register_filter(twice => sub { $_[0] * 2 });
1116
$dbi->apply_filter(
1117
    'table1', 'key1' => {out => 'twice', in => 'twice'}
1118
);
1119
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => undef});
1120
$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2});
1121
$result = $dbi->execute('select * from table1;');
1122
$row   = $result->one;
1123
is_deeply($row, {key1 => 4, key2 => 2}, "update");
1124

            
1125
$dbi = DBIx::Custom->connect;
1126
eval { $dbi->execute('drop table table1') };
1127
$dbi->execute($create_table1);
1128
$dbi->register_filter(twice => sub { $_[0] * 2 });
1129
$dbi->apply_filter(
1130
    'table1', 'key1' => {out => 'twice', in => 'twice'}
1131
);
1132
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1=> undef});
1133
$dbi->delete(table => 'table1', where => {key1 => 1});
1134
$result = $dbi->execute('select * from table1;');
1135
$rows   = $result->all;
1136
is_deeply($rows, [], "delete");
1137

            
1138
$dbi = DBIx::Custom->connect;
1139
eval { $dbi->execute('drop table table1') };
1140
$dbi->execute($create_table1);
1141
$dbi->register_filter(twice => sub { $_[0] * 2 });
1142
$dbi->apply_filter(
1143
    'table1', 'key1' => {out => 'twice', in => 'twice'}
1144
);
1145
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
1146
$result = $dbi->select(table => 'table1', where => {key1 => 1});
1147
$result->filter({'key2' => 'twice'});
1148
$rows   = $result->all;
1149
is_deeply($rows, [{key1 => 4, key2 => 4}], "select");
1150

            
1151
$dbi = DBIx::Custom->connect;
1152
eval { $dbi->execute('drop table table1') };
1153
$dbi->execute($create_table1);
1154
$dbi->register_filter(twice => sub { $_[0] * 2 });
1155
$dbi->apply_filter(
1156
    'table1', 'key1' => {out => 'twice', in => 'twice'}
1157
);
1158
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
1159
$result = $dbi->execute("select * from table1 where key1 = :key1 and key2 = :key2;",
1160
                        param => {key1 => 1, key2 => 2},
1161
                        table => ['table1']);
1162
$rows   = $result->all;
1163
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute");
1164

            
1165
$dbi = DBIx::Custom->connect;
1166
eval { $dbi->execute('drop table table1') };
1167
$dbi->execute($create_table1);
1168
$dbi->register_filter(twice => sub { $_[0] * 2 });
1169
$dbi->apply_filter(
1170
    'table1', 'key1' => {out => 'twice', in => 'twice'}
1171
);
1172
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
1173
$result = $dbi->execute("select * from {table table1} where key1 = :key1 and key2 = :key2;",
1174
                        param => {key1 => 1, key2 => 2});
1175
$rows   = $result->all;
1176
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute table tag");
1177

            
1178
$dbi = DBIx::Custom->connect;
1179
eval { $dbi->execute('drop table table1') };
1180
eval { $dbi->execute('drop table table2') };
1181
$dbi->execute($create_table1);
1182
$dbi->execute($create_table2);
1183
$dbi->register_filter(twice => sub { $_[0] * 2 });
1184
$dbi->register_filter(three_times => sub { $_[0] * 3 });
1185
$dbi->apply_filter(
1186
    'table1', 'key2' => {out => 'twice', in => 'twice'}
1187
);
1188
$dbi->apply_filter(
1189
    'table2', 'key3' => {out => 'three_times', in => 'three_times'}
1190
);
1191
$dbi->insert(table => 'table1', param => {key1 => 5, key2 => 2}, filter => {key2 => undef});
1192
$dbi->insert(table => 'table2', param => {key1 => 5, key3 => 6}, filter => {key3 => undef});
1193
$result = $dbi->select(
1194
     table => ['table1', 'table2'],
1195
     column => ['key2', 'key3'],
1196
     where => {'table1.key2' => 1, 'table2.key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
1197

            
1198
$result->filter({'key2' => 'twice'});
1199
$rows   = $result->all;
1200
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join");
1201

            
1202
$result = $dbi->select(
1203
     table => ['table1', 'table2'],
1204
     column => ['key2', 'key3'],
1205
     where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
1206

            
1207
$result->filter({'key2' => 'twice'});
1208
$rows   = $result->all;
1209
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join : omit");
1210

            
1211
test 'each_column';
1212
$dbi = DBIx::Custom->connect;
1213
eval { $dbi->execute("drop table ${q}table$p") };
1214
eval { $dbi->execute('drop table table1') };
1215
eval { $dbi->execute('drop table table2') };
test cleranup
Yuki Kimoto authored on 2011-08-10
1216
eval { $dbi->execute('drop table table3') };
test cleanup
Yuki Kimoto authored on 2011-08-10
1217
$dbi->execute($create_table1_type);
1218
$dbi->execute($create_table2);
1219

            
1220
$infos = [];
1221
$dbi->each_column(sub {
1222
    my ($self, $table, $column, $cinfo) = @_;
1223
    
1224
    if ($table =~ /^table\d/) {
1225
         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
1226
         push @$infos, $info;
1227
    }
1228
});
1229
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
1230
is_deeply($infos, 
1231
    [
1232
        ['table1', 'key1', 'key1'],
1233
        ['table1', 'key2', 'key2'],
1234
        ['table2', 'key1', 'key1'],
1235
        ['table2', 'key3', 'key3']
1236
    ]
1237
    
1238
);
1239
test 'each_table';
1240
$dbi = DBIx::Custom->connect;
1241
eval { $dbi->execute('drop table table1') };
1242
eval { $dbi->execute('drop table table2') };
1243
$dbi->execute($create_table2);
1244
$dbi->execute($create_table1_type);
1245

            
1246
$infos = [];
1247
$dbi->each_table(sub {
1248
    my ($self, $table, $table_info) = @_;
1249
    
1250
    if ($table =~ /^table\d/) {
1251
         my $info = [$table, $table_info->{TABLE_NAME}];
1252
         push @$infos, $info;
1253
    }
1254
});
1255
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
1256
is_deeply($infos, 
1257
    [
1258
        ['table1', 'table1'],
1259
        ['table2', 'table2'],
1260
    ]
1261
);
1262

            
1263
test 'limit';
1264
$dbi = DBIx::Custom->connect;
1265
eval { $dbi->execute('drop table table1') };
1266
$dbi->execute($create_table1);
1267
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1268
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
1269
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
1270
$dbi->register_tag(
1271
    limit => sub {
1272
        my ($count, $offset) = @_;
1273
        
1274
        my $s = '';
1275
        $s .= "limit $count";
1276
        $s .= " offset $offset" if defined $offset;
1277
        
1278
        return [$s, []];
1279
    }
1280
);
1281
$rows = $dbi->select(
1282
  table => 'table1',
1283
  where => {key1 => 1},
1284
  append => "order by key2 {limit 1 0}"
1285
)->all;
1286
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1287
$rows = $dbi->select(
1288
  table => 'table1',
1289
  where => {key1 => 1},
1290
  append => "order by key2 {limit 2 1}"
1291
)->all;
1292
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
1293
$rows = $dbi->select(
1294
  table => 'table1',
1295
  where => {key1 => 1},
1296
  append => "order by key2 {limit 1}"
1297
)->all;
1298
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1299

            
1300
test 'connect super';
test cleanup
Yuki Kimoto authored on 2011-08-10
1301
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1302
eval { $dbi->execute('drop table table1') };
1303
$dbi->execute($create_table1);
1304
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1305
is($dbi->select(table => 'table1')->one->{key1}, 1);
1306

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1307
$dbi = DBIx::Custom->new;
test cleanup
Yuki Kimoto authored on 2011-08-10
1308
$dbi->connect;
1309
eval { $dbi->execute('drop table table1') };
1310
$dbi->execute($create_table1);
1311
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1312
is($dbi->select(table => 'table1')->one->{key1}, 1);
1313

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1314
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1315
eval { $dbi->execute('drop table table1') };
1316
$dbi->execute($create_table1);
1317
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1318
is($dbi->select(table => 'table1')->one->{key1}, 1);
1319

            
1320
test 'end_filter';
1321
$dbi = DBIx::Custom->connect;
1322
eval { $dbi->execute('drop table table1') };
1323
$dbi->execute($create_table1);
1324
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1325
$result = $dbi->select(table => 'table1');
1326
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1327
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 });
1328
$row = $result->fetch_first;
1329
is_deeply($row, [6, 40]);
1330

            
1331
$dbi = DBIx::Custom->connect;
1332
eval { $dbi->execute('drop table table1') };
1333
$dbi->execute($create_table1);
1334
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1335
$result = $dbi->select(table => 'table1');
1336
$result->filter([qw/key1 key2/] => sub { $_[0] * 2 });
1337
$result->end_filter([[qw/key1 key2/] => sub { $_[0] * 3 }]);
1338
$row = $result->fetch_first;
1339
is_deeply($row, [6, 12]);
1340

            
1341
$dbi = DBIx::Custom->connect;
1342
eval { $dbi->execute('drop table table1') };
1343
$dbi->execute($create_table1);
1344
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1345
$result = $dbi->select(table => 'table1');
1346
$result->filter([[qw/key1 key2/] => sub { $_[0] * 2 }]);
1347
$result->end_filter([qw/key1 key2/] => sub { $_[0] * 3 });
1348
$row = $result->fetch_first;
1349
is_deeply($row, [6, 12]);
1350

            
1351
$dbi->register_filter(five_times => sub { $_[0] * 5 });
1352
$result = $dbi->select(table => 'table1');
1353
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1354
$result->end_filter({key1 => sub { $_[0] * 3 }, key2 => 'five_times' });
1355
$row = $result->one;
1356
is_deeply($row, {key1 => 6, key2 => 40});
1357

            
1358
$dbi->register_filter(five_times => sub { $_[0] * 5 });
1359
$dbi->apply_filter('table1',
1360
    key1 => {end => sub { $_[0] * 3 } },
1361
    key2 => {end => 'five_times'}
1362
);
1363
$result = $dbi->select(table => 'table1');
1364
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1365
$row = $result->one;
1366
is_deeply($row, {key1 => 6, key2 => 40}, 'apply_filter');
1367

            
1368
$dbi->register_filter(five_times => sub { $_[0] * 5 });
1369
$dbi->apply_filter('table1',
1370
    key1 => {end => sub { $_[0] * 3 } },
1371
    key2 => {end => 'five_times'}
1372
);
1373
$result = $dbi->select(table => 'table1');
1374
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1375
$result->filter(key1 => undef);
1376
$result->end_filter(key1 => undef);
1377
$row = $result->one;
1378
is_deeply($row, {key1 => 1, key2 => 40}, 'apply_filter overwrite');
1379

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1380
test 'remove_end_filter and remove_filter';
1381
$dbi = DBIx::Custom->connect;
1382
eval { $dbi->execute('drop table table1') };
1383
$dbi->execute($create_table1);
1384
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1385
$result = $dbi->select(table => 'table1');
1386
$row = $result
1387
       ->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 })
1388
       ->remove_filter
1389
       ->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 })
1390
       ->remove_end_filter
1391
       ->fetch_first;
1392
is_deeply($row, [1, 2]);
1393

            
1394
test 'empty where select';
1395
$dbi = DBIx::Custom->connect;
1396
eval { $dbi->execute('drop table table1') };
1397
$dbi->execute($create_table1);
1398
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1399
$result = $dbi->select(table => 'table1', where => {});
1400
$row = $result->one;
1401
is_deeply($row, {key1 => 1, key2 => 2});
1402

            
1403
test 'select query option';
1404
$dbi = DBIx::Custom->connect;
1405
eval { $dbi->execute('drop table table1') };
1406
$dbi->execute($create_table1);
1407
$query = $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, query => 1);
1408
is(ref $query, 'DBIx::Custom::Query');
1409
$query = $dbi->update(table => 'table1', where => {key1 => 1}, param => {key2 => 2}, query => 1);
1410
is(ref $query, 'DBIx::Custom::Query');
1411
$query = $dbi->delete(table => 'table1', where => {key1 => 1}, query => 1);
1412
is(ref $query, 'DBIx::Custom::Query');
1413
$query = $dbi->select(table => 'table1', where => {key1 => 1, key2 => 2}, query => 1);
1414
is(ref $query, 'DBIx::Custom::Query');
1415

            
1416
test 'where';
1417
$dbi = DBIx::Custom->connect;
1418
eval { $dbi->execute('drop table table1') };
1419
$dbi->execute($create_table1);
1420
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1421
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1422
$where = $dbi->where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
1423
is("$where", "where ( key1 = :key1 and key2 = :key2 )", 'no param');
1424

            
1425
$where = $dbi->where
1426
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
1427
             ->param({key1 => 1});
1428

            
1429
$result = $dbi->select(
1430
    table => 'table1',
1431
    where => $where
1432
);
1433
$row = $result->all;
1434
is_deeply($row, [{key1 => 1, key2 => 2}]);
1435

            
1436
$result = $dbi->select(
1437
    table => 'table1',
1438
    where => [
1439
        ['and', 'key1 = :key1', 'key2 = :key2'],
1440
        {key1 => 1}
1441
    ]
1442
);
1443
$row = $result->all;
1444
is_deeply($row, [{key1 => 1, key2 => 2}]);
1445

            
1446
$where = $dbi->where
1447
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
1448
             ->param({key1 => 1, key2 => 2});
1449
$result = $dbi->select(
1450
    table => 'table1',
1451
    where => $where
1452
);
1453
$row = $result->all;
1454
is_deeply($row, [{key1 => 1, key2 => 2}]);
1455

            
1456
$where = $dbi->where
1457
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
1458
             ->param({});
1459
$result = $dbi->select(
1460
    table => 'table1',
1461
    where => $where,
1462
);
1463
$row = $result->all;
1464
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1465

            
1466
$where = $dbi->where
1467
             ->clause(['and', ['or', 'key1 > :key1', 'key1 < :key1'], 'key2 = :key2'])
1468
             ->param({key1 => [0, 3], key2 => 2});
1469
$result = $dbi->select(
1470
    table => 'table1',
1471
    where => $where,
1472
); 
1473
$row = $result->all;
1474
is_deeply($row, [{key1 => 1, key2 => 2}]);
1475

            
1476
$where = $dbi->where;
1477
$result = $dbi->select(
1478
    table => 'table1',
1479
    where => $where
1480
);
1481
$row = $result->all;
1482
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1483

            
1484
eval {
1485
$where = $dbi->where
1486
             ->clause(['uuu']);
1487
$result = $dbi->select(
1488
    table => 'table1',
1489
    where => $where
1490
);
1491
};
1492
ok($@);
1493

            
1494
$where = $dbi->where;
1495
is("$where", '');
1496

            
1497
$where = $dbi->where
1498
             ->clause(['or', ('key1 = :key1') x 2])
1499
             ->param({key1 => [1, 3]});
1500
$result = $dbi->select(
1501
    table => 'table1',
1502
    where => $where,
1503
);
1504
$row = $result->all;
1505
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1506

            
1507
$where = $dbi->where
1508
             ->clause(['or', ('key1 = :key1') x 2])
1509
             ->param({key1 => [1]});
1510
$result = $dbi->select(
1511
    table => 'table1',
1512
    where => $where,
1513
);
1514
$row = $result->all;
1515
is_deeply($row, [{key1 => 1, key2 => 2}]);
1516

            
1517
$where = $dbi->where
1518
             ->clause(['or', ('key1 = :key1') x 2])
1519
             ->param({key1 => 1});
1520
$result = $dbi->select(
1521
    table => 'table1',
1522
    where => $where,
1523
);
1524
$row = $result->all;
1525
is_deeply($row, [{key1 => 1, key2 => 2}]);
1526

            
1527
$where = $dbi->where
1528
             ->clause('key1 = :key1')
1529
             ->param({key1 => 1});
1530
$result = $dbi->select(
1531
    table => 'table1',
1532
    where => $where,
1533
);
1534
$row = $result->all;
1535
is_deeply($row, [{key1 => 1, key2 => 2}]);
1536

            
1537
$where = $dbi->where
1538
             ->clause('key1 = :key1 key2 = :key2')
1539
             ->param({key1 => 1});
1540
eval{$where->to_string};
1541
like($@, qr/one column/);
1542

            
1543
$where = $dbi->where
1544
             ->clause(['or', ('key1 = :key1') x 3])
1545
             ->param({key1 => [$dbi->not_exists, 1, 3]});
1546
$result = $dbi->select(
1547
    table => 'table1',
1548
    where => $where,
1549
);
1550
$row = $result->all;
1551
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1552

            
1553
$where = $dbi->where
1554
             ->clause(['or', ('key1 = :key1') x 3])
1555
             ->param({key1 => [1, $dbi->not_exists, 3]});
1556
$result = $dbi->select(
1557
    table => 'table1',
1558
    where => $where,
1559
);
1560
$row = $result->all;
1561
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1562

            
1563
$where = $dbi->where
1564
             ->clause(['or', ('key1 = :key1') x 3])
1565
             ->param({key1 => [1, 3, $dbi->not_exists]});
1566
$result = $dbi->select(
1567
    table => 'table1',
1568
    where => $where,
1569
);
1570
$row = $result->all;
1571
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1572

            
1573
$where = $dbi->where
1574
             ->clause(['or', ('key1 = :key1') x 3])
1575
             ->param({key1 => [1, $dbi->not_exists, $dbi->not_exists]});
1576
$result = $dbi->select(
1577
    table => 'table1',
1578
    where => $where,
1579
);
1580
$row = $result->all;
1581
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1582

            
1583
$where = $dbi->where
1584
             ->clause(['or', ('key1 = :key1') x 3])
1585
             ->param({key1 => [$dbi->not_exists, 1, $dbi->not_exists]});
1586
$result = $dbi->select(
1587
    table => 'table1',
1588
    where => $where,
1589
);
1590
$row = $result->all;
1591
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1592

            
1593
$where = $dbi->where
1594
             ->clause(['or', ('key1 = :key1') x 3])
1595
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists, 1]});
1596
$result = $dbi->select(
1597
    table => 'table1',
1598
    where => $where,
1599
);
1600
$row = $result->all;
1601
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1602

            
1603
$where = $dbi->where
1604
             ->clause(['or', ('key1 = :key1') x 3])
1605
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists, $dbi->not_exists]});
1606
$result = $dbi->select(
1607
    table => 'table1',
1608
    where => $where,
1609
);
1610
$row = $result->all;
1611
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1612

            
1613
$where = $dbi->where
1614
             ->clause(['or', ('key1 = :key1') x 3])
1615
             ->param({key1 => []});
1616
$result = $dbi->select(
1617
    table => 'table1',
1618
    where => $where,
1619
);
1620
$row = $result->all;
1621
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1622

            
1623
$where = $dbi->where
1624
             ->clause(['and', '{> key1}', '{< key1}' ])
1625
             ->param({key1 => [2, $dbi->not_exists]});
1626
$result = $dbi->select(
1627
    table => 'table1',
1628
    where => $where,
1629
);
1630
$row = $result->all;
1631
is_deeply($row, [{key1 => 3, key2 => 4}], 'not_exists');
1632

            
1633
$where = $dbi->where
1634
             ->clause(['and', '{> key1}', '{< key1}' ])
1635
             ->param({key1 => [$dbi->not_exists, 2]});
1636
$result = $dbi->select(
1637
    table => 'table1',
1638
    where => $where,
1639
);
1640
$row = $result->all;
1641
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1642

            
1643
$where = $dbi->where
1644
             ->clause(['and', '{> key1}', '{< key1}' ])
1645
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists]});
1646
$result = $dbi->select(
1647
    table => 'table1',
1648
    where => $where,
1649
);
1650
$row = $result->all;
1651
is_deeply($row, [{key1 => 1, key2 => 2},{key1 => 3, key2 => 4}], 'not_exists');
1652

            
1653
$where = $dbi->where
1654
             ->clause(['and', '{> key1}', '{< key1}' ])
1655
             ->param({key1 => [0, 2]});
1656
$result = $dbi->select(
1657
    table => 'table1',
1658
    where => $where,
1659
);
1660
$row = $result->all;
1661
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1662

            
1663
$where = $dbi->where
1664
             ->clause(['and', 'key1 is not null', 'key2 is not null' ]);
1665
$result = $dbi->select(
1666
    table => 'table1',
1667
    where => $where,
1668
);
1669
$row = $result->all;
1670
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1671

            
1672
eval {$dbi->where(ppp => 1) };
1673
like($@, qr/invalid/);
1674

            
1675
$where = $dbi->where(
1676
    clause => ['and', ['or'], ['and', 'key1 = :key1', 'key2 = :key2']],
1677
    param => {key1 => 1, key2 => 2}
1678
);
1679
$result = $dbi->select(
1680
    table => 'table1',
1681
    where => $where,
1682
);
1683
$row = $result->all;
1684
is_deeply($row, [{key1 => 1, key2 => 2}]);
1685

            
1686

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

            
1698
$where = $dbi->where;
1699
$where->clause(['and', ':key1{=}']);
1700
$where->param({key1 => undef});
1701
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1702
$row = $result->all;
1703
is_deeply($row, [{key1 => 1, key2 => 2}]);
1704

            
1705
$where = $dbi->where;
1706
$where->clause(['and', ':key1{=}']);
1707
$where->param({key1 => undef});
1708
$where->if('defined');
1709
$where->map;
1710
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1711
$row = $result->all;
1712
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1713

            
1714
$where = $dbi->where;
1715
$where->clause(['or', ':key1{=}', ':key1{=}']);
1716
$where->param({key1 => [undef, undef]});
1717
$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]});
1718
$row = $result->all;
1719
is_deeply($row, [{key1 => 1, key2 => 2}]);
1720
$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]});
1721
$row = $result->all;
1722
is_deeply($row, [{key1 => 1, key2 => 2}]);
1723

            
1724
$where = $dbi->where;
1725
$where->clause(['and', ':key1{=}']);
1726
$where->param({key1 => [undef, undef]});
1727
$where->if('defined');
1728
$where->map;
1729
$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]});
1730
$row = $result->all;
1731
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1732
$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]});
1733
$row = $result->all;
1734
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1735

            
1736
$where = $dbi->where;
1737
$where->clause(['and', ':key1{=}']);
1738
$where->param({key1 => 0});
1739
$where->if('length');
1740
$where->map;
1741
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1742
$row = $result->all;
1743
is_deeply($row, [{key1 => 1, key2 => 2}]);
1744

            
1745
$where = $dbi->where;
1746
$where->clause(['and', ':key1{=}']);
1747
$where->param({key1 => ''});
1748
$where->if('length');
1749
$where->map;
1750
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1751
$row = $result->all;
1752
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1753

            
1754
$where = $dbi->where;
1755
$where->clause(['and', ':key1{=}']);
1756
$where->param({key1 => 5});
1757
$where->if(sub { ($_[0] || '') eq 5 });
1758
$where->map;
1759
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1760
$row = $result->all;
1761
is_deeply($row, [{key1 => 1, key2 => 2}]);
1762

            
1763
$where = $dbi->where;
1764
$where->clause(['and', ':key1{=}']);
1765
$where->param({key1 => 7});
1766
$where->if(sub { ($_[0] || '') eq 5 });
1767
$where->map;
1768
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1769
$row = $result->all;
1770
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1771

            
1772
$where = $dbi->where;
1773
$where->param({id => 1, author => 'Ken', price => 1900});
1774
$where->map(id => 'book.id',
1775
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1776
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1777
);
1778
is_deeply($where->param, {'book.id' => 1, 'book.author' => '%Ken%',
1779
  'book.price' => 1900});
1780

            
1781
$where = $dbi->where;
1782
$where->param({id => 0, author => 0, price => 0});
1783
$where->map(
1784
    id => 'book.id',
1785
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1786
    price => ['book.price', sub { '%' . $_[0] . '%' },
1787
      {if => sub { $_[0] eq 0 }}]
1788
);
1789
is_deeply($where->param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
1790

            
1791
$where = $dbi->where;
1792
$where->param({id => '', author => '', price => ''});
1793
$where->if('length');
1794
$where->map(
1795
    id => 'book.id',
1796
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1797
    price => ['book.price', sub { '%' . $_[0] . '%' },
1798
      {if => sub { $_[0] eq 1 }}]
1799
);
1800
is_deeply($where->param, {});
1801

            
1802
$where = $dbi->where;
1803
$where->param({id => undef, author => undef, price => undef});
1804
$where->if('length');
1805
$where->map(
1806
    id => 'book.id',
1807
    price => ['book.price', {if => 'exists'}]
1808
);
1809
is_deeply($where->param, {'book.price' => undef});
1810

            
1811
$where = $dbi->where;
1812
$where->param({price => 'a'});
1813
$where->if('length');
1814
$where->map(
1815
    id => ['book.id', {if => 'exists'}],
1816
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
1817
);
1818
is_deeply($where->param, {'book.price' => '%a'});
1819

            
1820
$where = $dbi->where;
1821
$where->param({id => [1, 2], author => 'Ken', price => 1900});
1822
$where->map(
1823
    id => 'book.id',
1824
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1825
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1826
);
1827
is_deeply($where->param, {'book.id' => [1, 2], 'book.author' => '%Ken%',
1828
  'book.price' => 1900});
1829

            
1830
$where = $dbi->where;
1831
$where->if('length');
1832
$where->param({id => ['', ''], author => 'Ken', price => 1900});
1833
$where->map(
1834
    id => 'book.id',
1835
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1836
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1837
);
1838
is_deeply($where->param, {'book.id' => [$dbi->not_exists, $dbi->not_exists], 'book.author' => '%Ken%',
1839
  'book.price' => 1900});
1840

            
1841
$where = $dbi->where;
1842
$where->param({id => ['', ''], author => 'Ken', price => 1900});
1843
$where->map(
1844
    id => ['book.id', {if => 'length'}],
1845
    author => ['book.author', sub { '%' . $_[0] . '%' }, {if => 'defined'}],
1846
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1847
);
1848
is_deeply($where->param, {'book.id' => [$dbi->not_exists, $dbi->not_exists], 'book.author' => '%Ken%',
1849
  'book.price' => 1900});
1850

            
1851
test 'dbi_option default';
1852
$dbi = DBIx::Custom->new;
1853
is_deeply($dbi->dbi_option, {});
1854

            
1855
test 'register_tag_processor';
1856
$dbi = DBIx::Custom->connect;
1857
$dbi->register_tag_processor(
1858
    a => sub { 1 }
1859
);
test cleanup
Yuki Kimoto authored on 2011-08-10
1860
is($dbi->{_tags}->{a}->(), 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1861

            
1862
test 'register_tag';
1863
$dbi = DBIx::Custom->connect;
1864
$dbi->register_tag(
1865
    b => sub { 2 }
1866
);
test cleanup
Yuki Kimoto authored on 2011-08-10
1867
is($dbi->{_tags}->{b}->(), 2);
test cleanup
Yuki Kimoto authored on 2011-08-10
1868

            
1869
test 'table not specify exception';
1870
$dbi = DBIx::Custom->connect;
1871
eval {$dbi->insert};
1872
like($@, qr/table/);
1873
eval {$dbi->update};
1874
like($@, qr/table/);
1875
eval {$dbi->delete};
1876
like($@, qr/table/);
1877
eval {$dbi->select};
1878
like($@, qr/table/);
test cleanup
Yuki Kimoto authored on 2011-08-10
1879

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1880
test 'more tests';
1881
$dbi = DBIx::Custom->connect;
1882
eval{$dbi->apply_filter('table', 'column', [])};
1883
like($@, qr/apply_filter/);
1884

            
1885
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
1886
like($@, qr/apply_filter/);
1887

            
1888
$dbi->apply_filter(
1889

            
1890
);
1891
$dbi = DBIx::Custom->connect;
1892
eval { $dbi->execute('drop table table1') };
1893
$dbi->execute($create_table1);
1894
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1895
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1896
$dbi->apply_filter('table1', 'key2', 
1897
                   {in => sub { $_[0] * 3 }, out => sub { $_[0] * 2 }});
1898
$rows = $dbi->select(table => 'table1', where => {key2 => 1})->all;
1899
is_deeply($rows, [{key1 => 1, key2 => 6}]);
1900

            
1901
$dbi = DBIx::Custom->connect;
1902
eval { $dbi->execute('drop table table1') };
1903
$dbi->execute($create_table1);
1904
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1905
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1906
$dbi->apply_filter('table1', 'key2', {});
1907
$rows = $dbi->select(table => 'table1', where => {key2 => 2})->all;
1908
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1909

            
1910
$dbi = DBIx::Custom->connect;
1911
eval {$dbi->apply_filter('table1', 'key2', {out => 'no'})};
1912
like($@, qr/not registered/);
1913
eval {$dbi->apply_filter('table1', 'key2', {in => 'no'})};
1914
like($@, qr/not registered/);
1915
$dbi->method({one => sub { 1 }});
1916
is($dbi->one, 1);
1917

            
1918
eval{DBIx::Custom->connect(dsn => undef)};
1919
like($@, qr/_connect/);
1920

            
1921
$dbi = DBIx::Custom->connect;
1922
eval { $dbi->execute('drop table table1') };
1923
$dbi->execute($create_table1);
1924
$dbi->register_filter(twice => sub { $_[0] * 2 });
1925
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
1926
             filter => {key1 => 'twice'});
1927
$row = $dbi->select(table => 'table1')->one;
1928
is_deeply($row, {key1 => 2, key2 => 2});
1929
eval {$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
1930
             filter => {key1 => 'no'}) };
1931
like($@, qr//);
1932

            
1933
$dbi->register_filter(one => sub { });
1934
$dbi->default_fetch_filter('one');
1935
ok($dbi->default_fetch_filter);
1936
$dbi->default_bind_filter('one');
1937
ok($dbi->default_bind_filter);
1938
eval{$dbi->default_fetch_filter('no')};
1939
like($@, qr/not registered/);
1940
eval{$dbi->default_bind_filter('no')};
1941
like($@, qr/not registered/);
1942
$dbi->default_bind_filter(undef);
1943
ok(!defined $dbi->default_bind_filter);
1944
$dbi->default_fetch_filter(undef);
1945
ok(!defined $dbi->default_fetch_filter);
1946
eval {$dbi->execute('select * from table1 {} {= author') };
1947
like($@, qr/Tag not finished/);
1948

            
1949
$dbi = DBIx::Custom->connect;
1950
eval { $dbi->execute('drop table table1') };
1951
$dbi->execute($create_table1);
1952
$dbi->register_filter(one => sub { 1 });
1953
$result = $dbi->select(table => 'table1');
1954
eval {$result->filter(key1 => 'no')};
1955
like($@, qr/not registered/);
1956
eval {$result->end_filter(key1 => 'no')};
1957
like($@, qr/not registered/);
1958
$result->default_filter(undef);
1959
ok(!defined $result->default_filter);
1960
$result->default_filter('one');
1961
is($result->default_filter->(), 1);
1962

            
1963
test 'dbi_option';
1964
$dbi = DBIx::Custom->connect(dbi_option => {PrintError => 1});
1965
ok($dbi->dbh->{PrintError});
1966
$dbi = DBIx::Custom->connect(dbi_options => {PrintError => 1});
1967
ok($dbi->dbh->{PrintError});
1968

            
1969
test 'DBIx::Custom::Result stash()';
1970
$result = DBIx::Custom::Result->new;
1971
is_deeply($result->stash, {}, 'default');
1972
$result->stash->{foo} = 1;
1973
is($result->stash->{foo}, 1, 'get and set');
test cleanup
Yuki Kimoto authored on 2011-08-10
1974

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1975
test 'delete_at';
1976
$dbi = DBIx::Custom->connect;
1977
eval { $dbi->execute('drop table table1') };
1978
$dbi->execute($create_table1_2);
1979
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1980
$dbi->delete_at(
1981
    table => 'table1',
1982
    primary_key => ['key1', 'key2'],
1983
    where => [1, 2],
1984
);
1985
is_deeply($dbi->select(table => 'table1')->all, []);
1986

            
1987
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1988
$dbi->delete_at(
1989
    table => 'table1',
1990
    primary_key => 'key1',
1991
    where => 1,
1992
);
1993
is_deeply($dbi->select(table => 'table1')->all, []);
1994

            
1995
test 'insert_at';
1996
$dbi = DBIx::Custom->connect;
1997
eval { $dbi->execute('drop table table1') };
1998
$dbi->execute($create_table1_2);
1999
$dbi->insert_at(
2000
    primary_key => ['key1', 'key2'], 
2001
    table => 'table1',
2002
    where => [1, 2],
2003
    param => {key3 => 3}
2004
);
2005
is($dbi->select(table => 'table1')->one->{key1}, 1);
2006
is($dbi->select(table => 'table1')->one->{key2}, 2);
2007
is($dbi->select(table => 'table1')->one->{key3}, 3);
2008

            
2009
$dbi->delete_all(table => 'table1');
2010
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2011
$dbi->insert_at(
2012
    primary_key => 'key1', 
2013
    table => 'table1',
2014
    where => 1,
2015
    param => {key2 => 2, key3 => 3}
2016
);
2017

            
2018
is($dbi->select(table => 'table1')->one->{key1}, 1);
2019
is($dbi->select(table => 'table1')->one->{key2}, 2);
2020
is($dbi->select(table => 'table1')->one->{key3}, 3);
2021

            
2022
eval {
2023
    $dbi->insert_at(
2024
        table => 'table1',
2025
        primary_key => ['key1', 'key2'],
2026
        where => {},
2027
        param => {key1 => 1, key2 => 2, key3 => 3},
2028
    );
2029
};
2030
like($@, qr/must be/);
2031

            
2032
$dbi = DBIx::Custom->connect;
2033
eval { $dbi->execute('drop table table1') };
2034
$dbi->execute($create_table1_2);
2035
$dbi->insert_at(
2036
    {key3 => 3},
2037
    primary_key => ['key1', 'key2'], 
2038
    table => 'table1',
2039
    where => [1, 2],
2040
);
2041
is($dbi->select(table => 'table1')->one->{key1}, 1);
2042
is($dbi->select(table => 'table1')->one->{key2}, 2);
2043
is($dbi->select(table => 'table1')->one->{key3}, 3);
2044

            
2045
test 'update_at';
2046
$dbi = DBIx::Custom->connect;
2047
eval { $dbi->execute('drop table table1') };
2048
$dbi->execute($create_table1_2);
2049
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2050
$dbi->update_at(
2051
    table => 'table1',
2052
    primary_key => ['key1', 'key2'],
2053
    where => [1, 2],
2054
    param => {key3 => 4}
2055
);
2056
is($dbi->select(table => 'table1')->one->{key1}, 1);
2057
is($dbi->select(table => 'table1')->one->{key2}, 2);
2058
is($dbi->select(table => 'table1')->one->{key3}, 4);
2059

            
2060
$dbi->delete_all(table => 'table1');
2061
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2062
$dbi->update_at(
2063
    table => 'table1',
2064
    primary_key => 'key1',
2065
    where => 1,
2066
    param => {key3 => 4}
2067
);
2068
is($dbi->select(table => 'table1')->one->{key1}, 1);
2069
is($dbi->select(table => 'table1')->one->{key2}, 2);
2070
is($dbi->select(table => 'table1')->one->{key3}, 4);
2071

            
2072
$dbi = DBIx::Custom->connect;
2073
eval { $dbi->execute('drop table table1') };
2074
$dbi->execute($create_table1_2);
2075
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2076
$dbi->update_at(
2077
    {key3 => 4},
2078
    table => 'table1',
2079
    primary_key => ['key1', 'key2'],
2080
    where => [1, 2]
2081
);
2082
is($dbi->select(table => 'table1')->one->{key1}, 1);
2083
is($dbi->select(table => 'table1')->one->{key2}, 2);
2084
is($dbi->select(table => 'table1')->one->{key3}, 4);
2085

            
2086
test 'select_at';
2087
$dbi = DBIx::Custom->connect;
2088
eval { $dbi->execute('drop table table1') };
2089
$dbi->execute($create_table1_2);
2090
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2091
$result = $dbi->select_at(
2092
    table => 'table1',
2093
    primary_key => ['key1', 'key2'],
2094
    where => [1, 2]
2095
);
2096
$row = $result->one;
2097
is($row->{key1}, 1);
2098
is($row->{key2}, 2);
2099
is($row->{key3}, 3);
2100

            
2101
$dbi->delete_all(table => 'table1');
2102
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2103
$result = $dbi->select_at(
2104
    table => 'table1',
2105
    primary_key => 'key1',
2106
    where => 1,
2107
);
2108
$row = $result->one;
2109
is($row->{key1}, 1);
2110
is($row->{key2}, 2);
2111
is($row->{key3}, 3);
2112

            
2113
$dbi->delete_all(table => 'table1');
2114
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2115
$result = $dbi->select_at(
2116
    table => 'table1',
2117
    primary_key => ['key1', 'key2'],
2118
    where => [1, 2]
2119
);
2120
$row = $result->one;
2121
is($row->{key1}, 1);
2122
is($row->{key2}, 2);
2123
is($row->{key3}, 3);
2124

            
2125
eval {
2126
    $result = $dbi->select_at(
2127
        table => 'table1',
2128
        primary_key => ['key1', 'key2'],
2129
        where => {},
2130
    );
2131
};
2132
like($@, qr/must be/);
2133

            
2134
eval {
2135
    $result = $dbi->select_at(
2136
        table => 'table1',
2137
        primary_key => ['key1', 'key2'],
2138
        where => [1],
2139
    );
2140
};
2141
like($@, qr/same/);
2142

            
2143
eval {
2144
    $result = $dbi->update_at(
2145
        table => 'table1',
2146
        primary_key => ['key1', 'key2'],
2147
        where => {},
2148
        param => {key1 => 1, key2 => 2},
2149
    );
2150
};
2151
like($@, qr/must be/);
2152

            
2153
eval {
2154
    $result = $dbi->delete_at(
2155
        table => 'table1',
2156
        primary_key => ['key1', 'key2'],
2157
        where => {},
2158
    );
2159
};
2160
like($@, qr/must be/);
2161

            
2162
test 'columns';
2163
use MyDBI1;
2164
$dbi = MyDBI1->connect;
2165
$model = $dbi->model('book');
2166

            
2167

            
2168
test 'model delete_at';
2169
$dbi = MyDBI6->connect;
2170
eval { $dbi->execute('drop table table1') };
2171
eval { $dbi->execute('drop table table2') };
2172
eval { $dbi->execute('drop table table3') };
2173
$dbi->execute($create_table1_2);
2174
$dbi->execute($create_table2_2);
2175
$dbi->execute($create_table3);
2176
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2177
$dbi->model('table1')->delete_at(where => [1, 2]);
2178
is_deeply($dbi->select(table => 'table1')->all, []);
2179
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
2180
$dbi->model('table1_1')->delete_at(where => [1, 2]);
2181
is_deeply($dbi->select(table => 'table1')->all, []);
2182
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
2183
$dbi->model('table1_3')->delete_at(where => [1, 2]);
2184
is_deeply($dbi->select(table => 'table1')->all, []);
2185

            
2186
test 'model insert_at';
2187
$dbi = MyDBI6->connect;
2188
eval { $dbi->execute('drop table table1') };
2189
$dbi->execute($create_table1_2);
2190
$dbi->model('table1')->insert_at(
2191
    where => [1, 2],
2192
    param => {key3 => 3}
2193
);
2194
$result = $dbi->model('table1')->select;
2195
$row = $result->one;
2196
is($row->{key1}, 1);
2197
is($row->{key2}, 2);
2198
is($row->{key3}, 3);
2199

            
2200
test 'model update_at';
2201
$dbi = MyDBI6->connect;
2202
eval { $dbi->execute('drop table table1') };
2203
$dbi->execute($create_table1_2);
2204
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2205
$dbi->model('table1')->update_at(
2206
    where => [1, 2],
2207
    param => {key3 => 4}
2208
);
2209
$result = $dbi->model('table1')->select;
2210
$row = $result->one;
2211
is($row->{key1}, 1);
2212
is($row->{key2}, 2);
2213
is($row->{key3}, 4);
2214

            
2215
test 'model select_at';
2216
$dbi = MyDBI6->connect;
2217
eval { $dbi->execute('drop table table1') };
2218
$dbi->execute($create_table1_2);
2219
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2220
$result = $dbi->model('table1')->select_at(where => [1, 2]);
2221
$row = $result->one;
2222
is($row->{key1}, 1);
2223
is($row->{key2}, 2);
2224
is($row->{key3}, 3);
2225

            
2226

            
2227
test 'mycolumn and column';
2228
$dbi = MyDBI7->connect;
2229
eval { $dbi->execute('drop table table1') };
2230
eval { $dbi->execute('drop table table2') };
2231
$dbi->execute($create_table1);
2232
$dbi->execute($create_table2);
2233
$dbi->separator('__');
2234
$dbi->setup_model;
2235
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2236
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2237
$model = $dbi->model('table1');
2238
$result = $model->select(
2239
    column => [$model->mycolumn, $model->column('table2')],
2240
    where => {'table1.key1' => 1}
2241
);
2242
is_deeply($result->one,
2243
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2244

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2245
test 'insert_param';
2246
$dbi = DBIx::Custom->connect;
2247
eval { $dbi->execute('drop table table1') };
2248
$dbi->execute($create_table1_2);
2249
$param = {key1 => 1, key2 => 2};
2250
$insert_param = $dbi->insert_param($param);
2251
$sql = <<"EOS";
2252
insert into table1 $insert_param
2253
EOS
2254
$dbi->execute($sql, param => $param, table => 'table1');
2255
is($dbi->select(table => 'table1')->one->{key1}, 1);
2256
is($dbi->select(table => 'table1')->one->{key2}, 2);
2257

            
2258
$dbi = DBIx::Custom->connect;
2259
eval { $dbi->execute('drop table table1') };
2260
$dbi->execute($create_table1_2);
2261
$param = {key1 => 1, key2 => 2};
2262
$insert_param = $dbi->insert_param($param);
2263
$sql = <<"EOS";
2264
insert into table1 $insert_param
2265
EOS
2266
$dbi->execute($sql, param => $param, table => 'table1');
2267
is($dbi->select(table => 'table1')->one->{key1}, 1);
2268
is($dbi->select(table => 'table1')->one->{key2}, 2);
2269

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2273
test 'mycolumn';
2274
$dbi = MyDBI8->connect;
2275
eval { $dbi->execute('drop table table1') };
2276
eval { $dbi->execute('drop table table2') };
2277
$dbi->execute($create_table1);
2278
$dbi->execute($create_table2);
2279
$dbi->setup_model;
2280
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2281
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2282
$model = $dbi->model('table1');
2283
$result = $model->select_at(
2284
    column => [
2285
        $model->mycolumn,
2286
        $model->column('table2')
2287
    ]
2288
);
2289
is_deeply($result->one,
2290
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
2291

            
2292
$result = $model->select_at(
2293
    column => [
2294
        $model->mycolumn(['key1']),
2295
        $model->column(table2 => ['key1'])
2296
    ]
2297
);
2298
is_deeply($result->one,
2299
          {key1 => 1, 'table2.key1' => 1});
2300
$result = $model->select_at(
2301
    column => [
2302
        $model->mycolumn(['key1']),
2303
        {table2 => ['key1']}
2304
    ]
2305
);
2306
is_deeply($result->one,
2307
          {key1 => 1, 'table2.key1' => 1});
2308

            
2309
$result = $model->select_at(
2310
    column => [
2311
        $model->mycolumn(['key1']),
2312
        ['table2.key1', as => 'table2.key1']
2313
    ]
2314
);
2315
is_deeply($result->one,
2316
          {key1 => 1, 'table2.key1' => 1});
2317

            
2318
$result = $model->select_at(
2319
    column => [
2320
        $model->mycolumn(['key1']),
2321
        ['table2.key1' => 'table2.key1']
2322
    ]
2323
);
2324
is_deeply($result->one,
2325
          {key1 => 1, 'table2.key1' => 1});
2326

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2327
test 'merge_param';
2328
$dbi = DBIx::Custom->new;
2329
$params = [
2330
    {key1 => 1, key2 => 2, key3 => 3},
2331
    {key1 => 1, key2 => 2},
2332
    {key1 => 1}
2333
];
2334
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]);
2335
is_deeply($param, {key1 => [1, 1, 1], key2 => [2, 2], key3 => 3});
2336

            
2337
$params = [
2338
    {key1 => [1, 2], key2 => 1, key3 => [1, 2]},
2339
    {key1 => [3, 4], key2 => [2, 3], key3 => 3}
2340
];
2341
$param = $dbi->merge_param($params->[0], $params->[1]);
2342
is_deeply($param, {key1 => [1, 2, 3, 4], key2 => [1, 2, 3], key3 => [1, 2, 3]});
2343

            
2344
test 'select() param option';
2345
$dbi = DBIx::Custom->connect;
2346
eval { $dbi->execute('drop table table1') };
2347
$dbi->execute($create_table1);
2348
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2349
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2350
eval { $dbi->execute('drop table table2') };
2351
$dbi->execute($create_table2);
2352
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2353
$dbi->insert(table => 'table2', param => {key1 => 2, key3 => 5});
2354
$rows = $dbi->select(
2355
    table => 'table1',
2356
    column => 'table1.key1 as table1_key1, key2, key3',
2357
    where   => {'table1.key2' => 3},
2358
    join  => ['inner join (select * from table2 where {= table2.key3})' . 
2359
              ' as table2 on table1.key1 = table2.key1'],
2360
    param => {'table2.key3' => 5}
2361
)->all;
2362
is_deeply($rows, [{table1_key1 => 2, key2 => 3, key3 => 5}]);
2363

            
2364
test 'select() string where';
2365
$dbi = DBIx::Custom->connect;
2366
eval { $dbi->execute('drop table table1') };
2367
$dbi->execute($create_table1);
2368
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2369
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2370
$rows = $dbi->select(
2371
    table => 'table1',
2372
    where => 'key1 = :key1 and key2 = :key2',
2373
    where_param => {key1 => 1, key2 => 2}
2374
)->all;
2375
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2376

            
2377
$dbi = DBIx::Custom->connect;
2378
eval { $dbi->execute('drop table table1') };
2379
$dbi->execute($create_table1);
2380
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2381
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2382
$rows = $dbi->select(
2383
    table => 'table1',
2384
    where => [
2385
        'key1 = :key1 and key2 = :key2',
2386
        {key1 => 1, key2 => 2}
2387
    ]
2388
)->all;
2389
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2390

            
2391
test 'delete() string where';
2392
$dbi = DBIx::Custom->connect;
2393
eval { $dbi->execute('drop table table1') };
2394
$dbi->execute($create_table1);
2395
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2396
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2397
$dbi->delete(
2398
    table => 'table1',
2399
    where => 'key1 = :key1 and key2 = :key2',
2400
    where_param => {key1 => 1, key2 => 2}
2401
);
2402
$rows = $dbi->select(table => 'table1')->all;
2403
is_deeply($rows, [{key1 => 2, key2 => 3}]);
2404

            
2405
$dbi = DBIx::Custom->connect;
2406
eval { $dbi->execute('drop table table1') };
2407
$dbi->execute($create_table1);
2408
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2409
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2410
$dbi->delete(
2411
    table => 'table1',
2412
    where => [
2413
        'key1 = :key1 and key2 = :key2',
2414
         {key1 => 1, key2 => 2}
2415
    ]
2416
);
2417
$rows = $dbi->select(table => 'table1')->all;
2418
is_deeply($rows, [{key1 => 2, key2 => 3}]);
2419

            
2420

            
2421
test 'update() string where';
2422
$dbi = DBIx::Custom->connect;
2423
eval { $dbi->execute('drop table table1') };
2424
$dbi->execute($create_table1);
2425
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2426
$dbi->update(
2427
    table => 'table1',
2428
    param => {key1 => 5},
2429
    where => 'key1 = :key1 and key2 = :key2',
2430
    where_param => {key1 => 1, key2 => 2}
2431
);
2432
$rows = $dbi->select(table => 'table1')->all;
2433
is_deeply($rows, [{key1 => 5, key2 => 2}]);
2434

            
2435
$dbi = DBIx::Custom->connect;
2436
eval { $dbi->execute('drop table table1') };
2437
$dbi->execute($create_table1);
2438
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2439
$dbi->update(
2440
    table => 'table1',
2441
    param => {key1 => 5},
2442
    where => [
2443
        'key1 = :key1 and key2 = :key2',
2444
        {key1 => 1, key2 => 2}
2445
    ]
2446
);
2447
$rows = $dbi->select(table => 'table1')->all;
2448
is_deeply($rows, [{key1 => 5, key2 => 2}]);
2449

            
2450
test 'insert id and primary_key option';
2451
$dbi = DBIx::Custom->connect;
2452
eval { $dbi->execute('drop table table1') };
2453
$dbi->execute($create_table1_2);
2454
$dbi->insert(
2455
    primary_key => ['key1', 'key2'], 
2456
    table => 'table1',
2457
    id => [1, 2],
2458
    param => {key3 => 3}
2459
);
2460
is($dbi->select(table => 'table1')->one->{key1}, 1);
2461
is($dbi->select(table => 'table1')->one->{key2}, 2);
2462
is($dbi->select(table => 'table1')->one->{key3}, 3);
2463

            
2464
$dbi->delete_all(table => 'table1');
2465
$dbi->insert(
2466
    primary_key => 'key1', 
2467
    table => 'table1',
2468
    id => 0,
2469
    param => {key2 => 2, key3 => 3}
2470
);
2471

            
2472
is($dbi->select(table => 'table1')->one->{key1}, 0);
2473
is($dbi->select(table => 'table1')->one->{key2}, 2);
2474
is($dbi->select(table => 'table1')->one->{key3}, 3);
2475

            
2476
$dbi = DBIx::Custom->connect;
2477
eval { $dbi->execute('drop table table1') };
2478
$dbi->execute($create_table1_2);
2479
$dbi->insert(
2480
    {key3 => 3},
2481
    primary_key => ['key1', 'key2'], 
2482
    table => 'table1',
2483
    id => [1, 2],
2484
);
2485
is($dbi->select(table => 'table1')->one->{key1}, 1);
2486
is($dbi->select(table => 'table1')->one->{key2}, 2);
2487
is($dbi->select(table => 'table1')->one->{key3}, 3);
test cleanup
Yuki Kimoto authored on 2011-08-10
2488

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2489
test 'model insert id and primary_key option';
2490
$dbi = MyDBI6->connect;
2491
eval { $dbi->execute('drop table table1') };
2492
$dbi->execute($create_table1_2);
2493
$dbi->model('table1')->insert(
2494
    id => [1, 2],
2495
    param => {key3 => 3}
2496
);
2497
$result = $dbi->model('table1')->select;
2498
$row = $result->one;
2499
is($row->{key1}, 1);
2500
is($row->{key2}, 2);
2501
is($row->{key3}, 3);
2502

            
2503
$dbi = MyDBI6->connect;
2504
eval { $dbi->execute('drop table table1') };
2505
$dbi->execute($create_table1_2);
2506
$dbi->model('table1')->insert(
2507
    {key3 => 3},
2508
    id => [1, 2]
2509
);
2510
$result = $dbi->model('table1')->select;
2511
$row = $result->one;
2512
is($row->{key1}, 1);
2513
is($row->{key2}, 2);
2514
is($row->{key3}, 3);
2515

            
2516
test 'update and id option';
2517
$dbi = DBIx::Custom->connect;
2518
eval { $dbi->execute('drop table table1') };
2519
$dbi->execute($create_table1_2);
2520
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2521
$dbi->update(
2522
    table => 'table1',
2523
    primary_key => ['key1', 'key2'],
2524
    id => [1, 2],
2525
    param => {key3 => 4}
2526
);
2527
is($dbi->select(table => 'table1')->one->{key1}, 1);
2528
is($dbi->select(table => 'table1')->one->{key2}, 2);
2529
is($dbi->select(table => 'table1')->one->{key3}, 4);
2530

            
2531
$dbi->delete_all(table => 'table1');
2532
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2533
$dbi->update(
2534
    table => 'table1',
2535
    primary_key => 'key1',
2536
    id => 0,
2537
    param => {key3 => 4}
2538
);
2539
is($dbi->select(table => 'table1')->one->{key1}, 0);
2540
is($dbi->select(table => 'table1')->one->{key2}, 2);
2541
is($dbi->select(table => 'table1')->one->{key3}, 4);
2542

            
2543
$dbi = DBIx::Custom->connect;
2544
eval { $dbi->execute('drop table table1') };
2545
$dbi->execute($create_table1_2);
2546
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2547
$dbi->update(
2548
    {key3 => 4},
2549
    table => 'table1',
2550
    primary_key => ['key1', 'key2'],
2551
    id => [1, 2]
2552
);
2553
is($dbi->select(table => 'table1')->one->{key1}, 1);
2554
is($dbi->select(table => 'table1')->one->{key2}, 2);
2555
is($dbi->select(table => 'table1')->one->{key3}, 4);
2556

            
2557

            
2558
test 'model update and id option';
2559
$dbi = MyDBI6->connect;
2560
eval { $dbi->execute('drop table table1') };
2561
$dbi->execute($create_table1_2);
2562
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2563
$dbi->model('table1')->update(
2564
    id => [1, 2],
2565
    param => {key3 => 4}
2566
);
2567
$result = $dbi->model('table1')->select;
2568
$row = $result->one;
2569
is($row->{key1}, 1);
2570
is($row->{key2}, 2);
2571
is($row->{key3}, 4);
2572

            
2573

            
2574
test 'delete and id option';
2575
$dbi = DBIx::Custom->connect;
2576
eval { $dbi->execute('drop table table1') };
2577
$dbi->execute($create_table1_2);
2578
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2579
$dbi->delete(
2580
    table => 'table1',
2581
    primary_key => ['key1', 'key2'],
2582
    id => [1, 2],
2583
);
2584
is_deeply($dbi->select(table => 'table1')->all, []);
2585

            
2586
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2587
$dbi->delete(
2588
    table => 'table1',
2589
    primary_key => 'key1',
2590
    id => 0,
2591
);
2592
is_deeply($dbi->select(table => 'table1')->all, []);
2593

            
2594

            
2595
test 'model delete and id option';
2596
$dbi = MyDBI6->connect;
2597
eval { $dbi->execute('drop table table1') };
2598
eval { $dbi->execute('drop table table2') };
2599
eval { $dbi->execute('drop table table3') };
2600
$dbi->execute($create_table1_2);
2601
$dbi->execute($create_table2_2);
2602
$dbi->execute($create_table3);
2603
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2604
$dbi->model('table1')->delete(id => [1, 2]);
2605
is_deeply($dbi->select(table => 'table1')->all, []);
2606
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
2607
$dbi->model('table1_1')->delete(id => [1, 2]);
2608
is_deeply($dbi->select(table => 'table1')->all, []);
2609
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
2610
$dbi->model('table1_3')->delete(id => [1, 2]);
2611
is_deeply($dbi->select(table => 'table1')->all, []);
2612

            
2613

            
2614
test 'select and id option';
2615
$dbi = DBIx::Custom->connect;
2616
eval { $dbi->execute('drop table table1') };
2617
$dbi->execute($create_table1_2);
2618
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2619
$result = $dbi->select(
2620
    table => 'table1',
2621
    primary_key => ['key1', 'key2'],
2622
    id => [1, 2]
2623
);
2624
$row = $result->one;
2625
is($row->{key1}, 1);
2626
is($row->{key2}, 2);
2627
is($row->{key3}, 3);
2628

            
2629
$dbi->delete_all(table => 'table1');
2630
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2631
$result = $dbi->select(
2632
    table => 'table1',
2633
    primary_key => 'key1',
2634
    id => 0,
2635
);
2636
$row = $result->one;
2637
is($row->{key1}, 0);
2638
is($row->{key2}, 2);
2639
is($row->{key3}, 3);
2640

            
2641
$dbi->delete_all(table => 'table1');
2642
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2643
$result = $dbi->select(
2644
    table => 'table1',
2645
    primary_key => ['key1', 'key2'],
2646
    id => [1, 2]
2647
);
2648
$row = $result->one;
2649
is($row->{key1}, 1);
2650
is($row->{key2}, 2);
2651
is($row->{key3}, 3);
2652

            
2653

            
2654
test 'model select_at';
2655
$dbi = MyDBI6->connect;
2656
eval { $dbi->execute('drop table table1') };
2657
$dbi->execute($create_table1_2);
2658
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2659
$result = $dbi->model('table1')->select(id => [1, 2]);
2660
$row = $result->one;
2661
is($row->{key1}, 1);
2662
is($row->{key2}, 2);
2663
is($row->{key3}, 3);
2664

            
2665
test 'column separator is default .';
2666
$dbi = MyDBI7->connect;
2667
eval { $dbi->execute('drop table table1') };
2668
eval { $dbi->execute('drop table table2') };
2669
$dbi->execute($create_table1);
2670
$dbi->execute($create_table2);
2671
$dbi->setup_model;
2672
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2673
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2674
$model = $dbi->model('table1');
2675
$result = $model->select(
2676
    column => [$model->column('table2')],
2677
    where => {'table1.key1' => 1}
2678
);
2679
is_deeply($result->one,
2680
          {'table2.key1' => 1, 'table2.key3' => 3});
2681

            
2682
$result = $model->select(
2683
    column => [$model->column('table2' => [qw/key1 key3/])],
2684
    where => {'table1.key1' => 1}
2685
);
2686
is_deeply($result->one,
2687
          {'table2.key1' => 1, 'table2.key3' => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2688

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2689
test 'separator';
2690
$dbi = DBIx::Custom->connect;
2691
eval { $dbi->execute('drop table table1') };
2692
eval { $dbi->execute('drop table table2') };
2693
$dbi->execute($create_table1);
2694
$dbi->execute($create_table2);
test cleanup
Yuki Kimoto authored on 2011-08-10
2695

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2696
$dbi->create_model(
2697
    table => 'table1',
2698
    join => [
2699
       'left outer join table2 on table1.key1 = table2.key1'
2700
    ],
2701
    primary_key => ['key1'],
2702
);
2703
$model2 = $dbi->create_model(
2704
    table => 'table2',
2705
);
2706
$dbi->setup_model;
2707
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2708
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2709
$model = $dbi->model('table1');
2710
$result = $model->select(
2711
    column => [
2712
        $model->mycolumn,
2713
        {table2 => [qw/key1 key3/]}
2714
    ],
2715
    where => {'table1.key1' => 1}
2716
);
2717
is_deeply($result->one,
2718
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
2719
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2720

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2721
$dbi->separator('__');
2722
$model = $dbi->model('table1');
2723
$result = $model->select(
2724
    column => [
2725
        $model->mycolumn,
2726
        {table2 => [qw/key1 key3/]}
2727
    ],
2728
    where => {'table1.key1' => 1}
2729
);
2730
is_deeply($result->one,
2731
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
2732
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2733

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2734
$dbi->separator('-');
2735
$model = $dbi->model('table1');
2736
$result = $model->select(
2737
    column => [
2738
        $model->mycolumn,
2739
        {table2 => [qw/key1 key3/]}
2740
    ],
2741
    where => {'table1.key1' => 1}
2742
);
2743
is_deeply($result->one,
2744
          {key1 => 1, key2 => 2, 'table2-key1' => 1, 'table2-key3' => 3});
2745
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2746

            
2747

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2748
test 'filter_off';
2749
$dbi = DBIx::Custom->connect;
2750
eval { $dbi->execute('drop table table1') };
2751
eval { $dbi->execute('drop table table2') };
2752
$dbi->execute($create_table1);
2753
$dbi->execute($create_table2);
test cleanup
Yuki Kimoto authored on 2011-08-10
2754

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2755
$dbi->create_model(
2756
    table => 'table1',
2757
    join => [
2758
       'left outer join table2 on table1.key1 = table2.key1'
2759
    ],
2760
    primary_key => ['key1'],
2761
);
2762
$dbi->setup_model;
2763
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2764
$model = $dbi->model('table1');
2765
$result = $model->select(column => 'key1');
2766
$result->filter(key1 => sub { $_[0] * 2 });
2767
is_deeply($result->one, {key1 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
2768

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2769
test 'available_datetype';
2770
$dbi = DBIx::Custom->connect;
2771
ok($dbi->can('available_datatype'));
test cleanup
Yuki Kimoto authored on 2011-08-10
2772

            
2773

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2774
test 'select prefix option';
2775
$dbi = DBIx::Custom->connect;
2776
eval { $dbi->execute('drop table table1') };
2777
$dbi->execute($create_table1);
2778
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2779
$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all;
2780
is_deeply($rows, [{key1 => 1, key2 => 2}], "table");
test cleanup
Yuki Kimoto authored on 2011-08-10
2781

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2782
test 'map_param';
2783
$dbi = DBIx::Custom->connect;
2784
$param = $dbi->map_param(
2785
    {id => 1, author => 'Ken', price => 1900},
2786
    id => 'book.id',
2787
    author => ['book.author', sub { '%' . $_[0] . '%' }],
2788
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
2789
);
2790
is_deeply($param, {'book.id' => 1, 'book.author' => '%Ken%',
2791
  'book.price' => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
2792

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2793
$param = $dbi->map_param(
2794
    {id => 0, author => 0, price => 0},
2795
    id => 'book.id',
2796
    author => ['book.author', sub { '%' . $_[0] . '%' }],
2797
    price => ['book.price', sub { '%' . $_[0] . '%' },
2798
      {if => sub { $_[0] eq 0 }}]
2799
);
2800
is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
test cleanup
Yuki Kimoto authored on 2011-08-10
2801

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2802
$param = $dbi->map_param(
2803
    {id => '', author => '', price => ''},
2804
    id => 'book.id',
2805
    author => ['book.author', sub { '%' . $_[0] . '%' }],
2806
    price => ['book.price', sub { '%' . $_[0] . '%' },
2807
      {if => sub { $_[0] eq 1 }}]
2808
);
2809
is_deeply($param, {});
test cleanup
Yuki Kimoto authored on 2011-08-10
2810

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2811
$param = $dbi->map_param(
2812
    {id => undef, author => undef, price => undef},
2813
    id => 'book.id',
2814
    price => ['book.price', {if => 'exists'}]
2815
);
2816
is_deeply($param, {'book.price' => undef});
test cleanup
Yuki Kimoto authored on 2011-08-10
2817

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2818
$param = $dbi->map_param(
2819
    {price => 'a'},
2820
    id => ['book.id', {if => 'exists'}],
2821
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
2822
);
2823
is_deeply($param, {'book.price' => '%a'});
test cleanup
Yuki Kimoto authored on 2011-08-10
2824

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2825
test 'order';
2826
$dbi = DBIx::Custom->connect;
2827
eval { $dbi->execute('drop table table1') };
2828
$dbi->execute($create_table1);
2829
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
2830
$dbi->insert({key1 => 1, key2 => 3}, table => 'table1');
2831
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
2832
$dbi->insert({key1 => 2, key2 => 4}, table => 'table1');
2833
my $order = $dbi->order;
2834
$order->prepend('key1', 'key2 desc');
2835
$result = $dbi->select(table => 'table1', append => "$order");
2836
is_deeply($result->all, [{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1},
2837
  {key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}]);
2838
$order->prepend('key1 desc');
2839
$result = $dbi->select(table => 'table1', append => "$order");
2840
is_deeply($result->all, [{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2},
2841
  {key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}]);
2842

            
2843
$order = $dbi->order;
2844
$order->prepend(['table1-key1'], [qw/table1-key2 desc/]);
2845
$result = $dbi->select(table => 'table1',
2846
  column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']],
2847
  append => "$order");
2848
is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3},
2849
  {'table1-key1' => 1, 'table1-key2' => 1},
2850
  {'table1-key1' => 2, 'table1-key2' => 4},
2851
  {'table1-key1' => 2, 'table1-key2' => 2}]);
2852

            
2853
test 'tag_parse';
2854
$dbi = DBIx::Custom->connect;
2855
$dbi->tag_parse(0);
2856
eval { $dbi->execute('drop table table1') };
2857
$dbi->execute($create_table1);
2858
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
2859
eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})};
2860
ok($@);
2861

            
2862
test 'last_sql';
2863
$dbi = DBIx::Custom->connect;
2864
eval { $dbi->execute('drop table table1') };
2865
$dbi->execute($create_table1);
2866
$dbi->execute('select * from table1');
2867
is($dbi->last_sql, 'select * from table1;');
2868

            
2869
eval{$dbi->execute("aaa")};
2870
is($dbi->last_sql, 'aaa;');
2871

            
2872
test 'DBIx::Custom header';
2873
$dbi = DBIx::Custom->connect;
2874
eval { $dbi->execute('drop table table1') };
2875
$dbi->execute($create_table1);
2876
$result = $dbi->execute('select key1 as h1, key2 as h2 from table1');
2877
is_deeply($result->header, [qw/h1 h2/]);
2878

            
2879
test 'Named placeholder :name(operater) syntax';
2880
$dbi->execute('drop table table1');
2881
$dbi->execute($create_table1_2);
2882
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
2883
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
2884

            
2885
$source = "select * from table1 where :key1{=} and :key2{=}";
2886
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
2887
$rows = $result->all;
2888
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
2889

            
2890
$source = "select * from table1 where :key1{ = } and :key2{=}";
2891
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
2892
$rows = $result->all;
2893
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
2894

            
2895
$source = "select * from table1 where :key1{<} and :key2{=}";
2896
$result = $dbi->execute($source, param => {key1 => 5, key2 => 2});
2897
$rows = $result->all;
2898
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
2899

            
2900
$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}";
2901
$result = $dbi->execute(
2902
    $source,
2903
    param => {'table1.key1' => 1, 'table1.key2' => 1},
2904
    filter => {'table1.key2' => sub { $_[0] * 2 }}
2905
);
2906
$rows = $result->all;
2907
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
2908

            
2909
test 'high perfomance way';
2910
$dbi->execute('drop table table1');
2911
$dbi->execute($create_table1_highperformance);
2912
$rows = [
2913
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
2914
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
2915
];
2916
{
2917
    my $query;
2918
    foreach my $row (@$rows) {
2919
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
2920
      $dbi->execute($query, $row, filter => {ab => sub { $_[0] * 2 }});
2921
    }
2922
    is_deeply($dbi->select(table => 'table1')->all,
2923
      [
2924
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
2925
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
2926
      ]
2927
    );
2928
}
2929

            
2930
$dbi->execute('drop table table1');
2931
$dbi->execute($create_table1_highperformance);
2932
$rows = [
2933
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
2934
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
2935
];
2936
{
2937
    my $query;
2938
    my $sth;
2939
    foreach my $row (@$rows) {
2940
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
2941
      $sth ||= $query->sth;
2942
      $sth->execute(map { $row->{$_} } sort keys %$row);
2943
    }
2944
    is_deeply($dbi->select(table => 'table1')->all,
2945
      [
2946
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
2947
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
2948
      ]
2949
    );
2950
}
2951

            
2952
test 'result';
2953
$dbi = DBIx::Custom->connect;
2954
eval { $dbi->execute('drop table table1') };
2955
$dbi->execute($create_table1);
2956
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
2957
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
2958

            
2959
$result = $dbi->select(table => 'table1');
2960
@rows = ();
2961
while (my $row = $result->fetch) {
2962
    push @rows, [@$row];
2963
}
2964
is_deeply(\@rows, [[1, 2], [3, 4]]);
2965

            
2966
$result = $dbi->select(table => 'table1');
2967
@rows = ();
2968
while (my $row = $result->fetch_hash) {
2969
    push @rows, {%$row};
2970
}
2971
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
2972

            
2973
$dbi = DBIx::Custom->connect;
2974
eval { $dbi->execute('drop table table1') };
2975
$dbi->execute($create_table1);
2976
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
2977
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
2978

            
2979
test 'fetch_all';
2980
$result = $dbi->select(table => 'table1');
2981
$rows = $result->fetch_all;
2982
is_deeply($rows, [[1, 2], [3, 4]]);
2983

            
2984
$result = $dbi->select(table => 'table1');
2985
$rows = $result->fetch_hash_all;
2986
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
2987

            
2988
$result = $dbi->select(table => 'table1');
2989
$result->dbi->filters({three_times => sub { $_[0] * 3}});
2990
$result->filter({key1 => 'three_times'});
2991

            
2992
$rows = $result->fetch_all;
2993
is_deeply($rows, [[3, 2], [9, 4]], "array");
2994

            
2995
$result = $dbi->select(table => 'table1');
2996
$result->dbi->filters({three_times => sub { $_[0] * 3}});
2997
$result->filter({key1 => 'three_times'});
2998
$rows = $result->fetch_hash_all;
2999
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash");
3000

            
3001
test "query_builder";
3002
$datas = [
3003
    # Basic tests
3004
    {   name            => 'placeholder basic',
3005
        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
3006
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;",
3007
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
3008
    },
3009
    {
3010
        name            => 'placeholder in',
3011
        source            => "{in k1 3};",
3012
        sql_expected    => "k1 in (?, ?, ?);",
3013
        columns_expected   => [qw/k1 k1 k1/]
3014
    },
3015
    
3016
    # Table name
3017
    {
3018
        name            => 'placeholder with table name',
3019
        source            => "{= a.k1} {= a.k2}",
3020
        sql_expected    => "a.k1 = ? a.k2 = ?;",
3021
        columns_expected  => [qw/a.k1 a.k2/]
3022
    },
3023
    {   
3024
        name            => 'placeholder in with table name',
3025
        source            => "{in a.k1 2} {in b.k2 2}",
3026
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?);",
3027
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
3028
    },
3029
    {
3030
        name            => 'not contain tag',
3031
        source            => "aaa",
3032
        sql_expected    => "aaa;",
3033
        columns_expected  => [],
3034
    }
3035
];
3036

            
3037
for (my $i = 0; $i < @$datas; $i++) {
3038
    my $data = $datas->[$i];
3039
    my $builder = DBIx::Custom->new->query_builder;
3040
    my $query = $builder->build_query($data->{source});
3041
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
3042
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
3043
}
3044

            
3045
$builder = DBIx::Custom->new->query_builder;
3046
$ret_val = $builder->register_tag(
3047
    p => sub {
3048
        my @args = @_;
3049
        
3050
        my $expand    = "? $args[0] $args[1]";
3051
        my $columns = [2];
3052
        return [$expand, $columns];
3053
    }
3054
);
3055

            
3056
$query = $builder->build_query("{p a b}");
3057
is($query->{sql}, "? a b;", "register_tag sql");
3058
is_deeply($query->{columns}, [2], "register_tag columns");
3059
isa_ok($ret_val, 'DBIx::Custom::QueryBuilder');
3060

            
3061
$builder = DBIx::Custom->new->query_builder;
3062

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

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

            
3069
$builder->register_tag({
3070
    q => 'string'
3071
});
3072

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

            
3076
$builder->register_tag({
3077
   r => sub {} 
3078
});
3079

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

            
3083
$builder->register_tag({
3084
   s => sub { return ["a", ""]} 
3085
});
3086

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

            
3090
$builder->register_tag(
3091
    t => sub {return ["a", []]}
3092
);
3093

            
3094

            
3095
test 'General error case';
3096
$builder = DBIx::Custom->new->query_builder;
3097
$builder->register_tag(
3098
    a => sub {
3099
        return ["? ? ?", ['']];
3100
    }
3101
);
3102
eval{$builder->build_query("{a}")};
3103
like($@, qr/\QPlaceholder count/, "placeholder count is invalid");
3104

            
3105

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

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

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

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

            
3121
test 'variouse source';
3122
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;";
3123
$query = $builder->build_query($source);
3124
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1");
3125

            
3126
$source = "abc;";
3127
$query = $builder->build_query($source);
3128
is($query->sql, 'abc;', "basic : 2");
3129

            
3130
$source = "{= a}";
3131
$query = $builder->build_query($source);
3132
is($query->sql, 'a = ?;', "only tag");
3133

            
3134
$source = "000;";
3135
$query = $builder->build_query($source);
3136
is($query->sql, '000;', "contain 0 value");
3137

            
3138
$source = "a {= b} }";
3139
eval{$builder->build_query($source)};
3140
like($@, qr/unexpected "}"/, "error : 1");
3141

            
3142
$source = "a {= {}";
3143
eval{$builder->build_query($source)};
3144
like($@, qr/unexpected "{"/, "error : 2");
3145

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3146
test 'select() wrap option';
3147
$dbi = DBIx::Custom->connect;
3148
eval { $dbi->execute('drop table table1') };
3149
$dbi->execute($create_table1);
3150
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3151
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
3152
$rows = $dbi->select(
3153
    table => 'table1',
3154
    column => 'key1',
3155
    wrap => ['select * from (', ') as t where key1 = 1']
3156
)->all;
3157
is_deeply($rows, [{key1 => 1}]);
3158

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3159
eval {
3160
$dbi->select(
3161
    table => 'table1',
3162
    column => 'key1',
3163
    wrap => 'select * from ('
3164
)
3165
};
3166
like($@, qr/array/);
3167

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3168
test 'dbi method from model';
3169
$dbi = MyDBI9->connect;
3170
eval { $dbi->execute('drop table table1') };
3171
$dbi->execute($create_table1);
3172
$dbi->setup_model;
3173
$model = $dbi->model('table1');
3174
eval{$model->execute('select * from table1')};
3175
ok(!$@);
3176

            
3177
test 'column table option';
3178
$dbi = MyDBI9->connect;
3179
eval { $dbi->execute('drop table table1') };
3180
$dbi->execute($create_table1);
3181
eval { $dbi->execute('drop table table2') };
3182
$dbi->execute($create_table2);
3183
$dbi->setup_model;
3184
$dbi->execute('insert into table1 (key1, key2) values (1, 2);');
3185
$dbi->execute('insert into table2 (key1, key3) values (1, 4);');
3186
$model = $dbi->model('table1');
3187
$result = $model->select(
3188
    column => [
3189
        $model->column('table2', {alias => 'table2_alias'})
3190
    ],
3191
    where => {'table2_alias.key3' => 4}
3192
);
3193
is_deeply($result->one, 
3194
          {'table2_alias.key1' => 1, 'table2_alias.key3' => 4});
3195

            
3196
$dbi->separator('__');
3197
$result = $model->select(
3198
    column => [
3199
        $model->column('table2', {alias => 'table2_alias'})
3200
    ],
3201
    where => {'table2_alias.key3' => 4}
3202
);
3203
is_deeply($result->one, 
3204
          {'table2_alias__key1' => 1, 'table2_alias__key3' => 4});
3205

            
3206
$dbi->separator('-');
3207
$result = $model->select(
3208
    column => [
3209
        $model->column('table2', {alias => 'table2_alias'})
3210
    ],
3211
    where => {'table2_alias.key3' => 4}
3212
);
3213
is_deeply($result->one, 
3214
          {'table2_alias-key1' => 1, 'table2_alias-key3' => 4});
3215

            
3216
test 'create_model';
3217
$dbi = DBIx::Custom->connect;
3218
eval { $dbi->execute('drop table table1') };
3219
eval { $dbi->execute('drop table table2') };
3220
$dbi->execute($create_table1);
3221
$dbi->execute($create_table2);
3222

            
3223
$dbi->create_model(
3224
    table => 'table1',
3225
    join => [
3226
       'left outer join table2 on table1.key1 = table2.key1'
3227
    ],
3228
    primary_key => ['key1']
3229
);
3230
$model2 = $dbi->create_model(
3231
    table => 'table2'
3232
);
3233
$dbi->create_model(
3234
    table => 'table3',
3235
    filter => [
3236
        key1 => {in => sub { uc $_[0] }}
3237
    ]
3238
);
3239
$dbi->setup_model;
3240
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3241
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
3242
$model = $dbi->model('table1');
3243
$result = $model->select(
3244
    column => [$model->mycolumn, $model->column('table2')],
3245
    where => {'table1.key1' => 1}
3246
);
3247
is_deeply($result->one,
3248
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
3249
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3250

            
3251
test 'model method';
3252
$dbi = DBIx::Custom->connect;
3253
eval { $dbi->execute('drop table table2') };
3254
$dbi->execute($create_table2);
3255
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
3256
$model = $dbi->create_model(
3257
    table => 'table2'
3258
);
3259
$model->method(foo => sub { shift->select(@_) });
3260
is_deeply($model->foo->one, {key1 => 1, key3 => 3});
3261

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3262
test 'update_param';
3263
$dbi = DBIx::Custom->connect;
3264
eval { $dbi->execute('drop table table1') };
3265
$dbi->execute($create_table1_2);
3266
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3267
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3268

            
3269
$param = {key2 => 11};
3270
$update_param = $dbi->update_param($param);
3271
$sql = <<"EOS";
3272
update table1 $update_param
3273
where key1 = 1
3274
EOS
3275
$dbi->execute($sql, param => $param);
3276
$result = $dbi->execute('select * from table1 order by key1;', table => 'table1');
3277
$rows   = $result->all;
3278
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
3279
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
3280
                  "basic");
3281

            
3282

            
3283
$dbi = DBIx::Custom->connect;
3284
eval { $dbi->execute('drop table table1') };
3285
$dbi->execute($create_table1_2);
3286
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3287
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3288

            
3289
$param = {key2 => 11, key3 => 33};
3290
$update_param = $dbi->update_param($param);
3291
$sql = <<"EOS";
3292
update table1 $update_param
3293
where key1 = 1
3294
EOS
3295
$dbi->execute($sql, param => $param);
3296
$result = $dbi->execute('select * from table1 order by key1;', table => 'table1');
3297
$rows   = $result->all;
3298
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
3299
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
3300
                  "basic");
3301

            
3302
$dbi = DBIx::Custom->connect;
3303
eval { $dbi->execute('drop table table1') };
3304
$dbi->execute($create_table1_2);
3305
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3306
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3307

            
3308
$param = {key2 => 11, key3 => 33};
3309
$update_param = $dbi->update_param($param, {no_set => 1});
3310
$sql = <<"EOS";
3311
update table1 set $update_param
3312
where key1 = 1
3313
EOS
3314
$dbi->execute($sql, param => $param);
3315
$result = $dbi->execute('select * from table1 order by key1;', table => 'table1');
3316
$rows   = $result->all;
3317
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
3318
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
3319
                  "update param no_set");
3320

            
3321
            
3322
eval { $dbi->update_param({";" => 1}) };
3323
like($@, qr/not safety/);
3324

            
3325

            
3326
test 'update_param';
3327
$dbi = DBIx::Custom->connect;
3328
eval { $dbi->execute('drop table table1') };
3329
$dbi->execute($create_table1_2);
3330
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3331
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3332

            
3333
$param = {key2 => 11};
3334
$update_param = $dbi->assign_param($param);
3335
$sql = <<"EOS";
3336
update table1 set $update_param
3337
where key1 = 1
3338
EOS
3339
$dbi->execute($sql, param => $param, table => 'table1');
3340
$result = $dbi->execute('select * from table1 order by key1;');
3341
$rows   = $result->all;
3342
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
3343
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
3344
                  "basic");
3345

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3346
test 'join';
3347
$dbi = DBIx::Custom->connect;
3348
eval { $dbi->execute('drop table table1') };
3349
$dbi->execute($create_table1);
3350
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3351
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
3352
eval { $dbi->execute('drop table table2') };
3353
$dbi->execute($create_table2);
3354
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
3355
eval { $dbi->execute('drop table table3') };
3356
$dbi->execute('create table table3 (key3 int, key4 int);');
3357
$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4});
3358
$rows = $dbi->select(
3359
    table => 'table1',
3360
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
3361
    where   => {'table1.key2' => 2},
3362
    join  => ['left outer join table2 on table1.key1 = table2.key1']
3363
)->all;
3364
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]);
3365

            
3366
$rows = $dbi->select(
3367
    table => 'table1',
3368
    where   => {'key1' => 1},
3369
    join  => ['left outer join table2 on table1.key1 = table2.key1']
3370
)->all;
3371
is_deeply($rows, [{key1 => 1, key2 => 2}]);
3372

            
3373
eval {
3374
    $rows = $dbi->select(
3375
        table => 'table1',
3376
        column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
3377
        where   => {'table1.key2' => 2},
3378
        join  => {'table1.key1' => 'table2.key1'}
3379
    );
3380
};
3381
like ($@, qr/array/);
3382

            
3383
$rows = $dbi->select(
3384
    table => 'table1',
3385
    where   => {'key1' => 1},
3386
    join  => ['left outer join table2 on table1.key1 = table2.key1',
3387
              'left outer join table3 on table2.key3 = table3.key3']
3388
)->all;
3389
is_deeply($rows, [{key1 => 1, key2 => 2}]);
3390

            
3391
$rows = $dbi->select(
3392
    column => 'table3.key4 as table3__key4',
3393
    table => 'table1',
3394
    where   => {'table1.key1' => 1},
3395
    join  => ['left outer join table2 on table1.key1 = table2.key1',
3396
              'left outer join table3 on table2.key3 = table3.key3']
3397
)->all;
3398
is_deeply($rows, [{table3__key4 => 4}]);
3399

            
3400
$rows = $dbi->select(
3401
    column => 'table1.key1 as table1__key1',
3402
    table => 'table1',
3403
    where   => {'table3.key4' => 4},
3404
    join  => ['left outer join table2 on table1.key1 = table2.key1',
3405
              'left outer join table3 on table2.key3 = table3.key3']
3406
)->all;
3407
is_deeply($rows, [{table1__key1 => 1}]);
3408

            
3409
$dbi = DBIx::Custom->connect;
3410
eval { $dbi->execute('drop table table1') };
3411
$dbi->execute($create_table1);
3412
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3413
eval { $dbi->execute('drop table table2') };
3414
$dbi->execute($create_table2);
3415
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
3416
$rows = $dbi->select(
3417
    table => 'table1',
3418
    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",
3419
    where   => {'table1.key2' => 2},
3420
    join  => ["left outer join ${q}table2$p on ${q}table1$p.${q}key1$p = ${q}table2$p.${q}key1$p"],
3421
)->all;
3422
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}],
3423
          'quote');
3424

            
3425

            
3426
$dbi = DBIx::Custom->connect;
3427
eval { $dbi->execute('drop table table1') };
3428
$dbi->execute($create_table1);
3429
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3430
$sql = <<"EOS";
3431
left outer join (
3432
  select * from table1 as t1
3433
  where t1.key2 = (
3434
    select max(t2.key2) from table1 as t2
3435
    where t1.key1 = t2.key1
3436
  )
3437
) as latest_table1 on table1.key1 = latest_table1.key1
3438
EOS
3439
$join = [$sql];
3440
$rows = $dbi->select(
3441
    table => 'table1',
3442
    column => 'latest_table1.key1 as latest_table1__key1',
3443
    join  => $join
3444
)->all;
3445
is_deeply($rows, [{latest_table1__key1 => 1}]);
3446

            
3447
$dbi = DBIx::Custom->connect;
3448
eval { $dbi->execute('drop table table1') };
3449
eval { $dbi->execute('drop table table2') };
3450
$dbi->execute($create_table1);
3451
$dbi->execute($create_table2);
3452
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3453
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
3454
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
3455
$result = $dbi->select(
3456
    table => 'table1',
3457
    join => [
3458
        "left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1"
3459
    ]
3460
);
3461
is_deeply($result->all, [{key1 => 1, key2 => 2}]);
3462
$result = $dbi->select(
3463
    table => 'table1',
3464
    column => [{table2 => ['key3']}],
3465
    join => [
3466
        "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1"
3467
    ]
3468
);
3469
is_deeply($result->all, [{'table2.key3' => 4}]);
3470
$result = $dbi->select(
3471
    table => 'table1',
3472
    column => [{table2 => ['key3']}],
3473
    join => [
3474
        "left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'"
3475
    ]
3476
);
3477
is_deeply($result->all, [{'table2.key3' => 4}]);
3478

            
3479
$dbi = DBIx::Custom->connect;
3480
eval { $dbi->execute('drop table table1') };
3481
eval { $dbi->execute('drop table table2') };
3482
$dbi->execute($create_table1);
3483
$dbi->execute($create_table2);
3484
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3485
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
3486
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
3487
$result = $dbi->select(
3488
    table => 'table1',
3489
    column => [{table2 => ['key3']}],
3490
    join => [
3491
        {
3492
            clause => "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1",
3493
            table => ['table1', 'table2']
3494
        }
3495
    ]
3496
);
3497
is_deeply($result->all, [{'table2.key3' => 4}]);
3498

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

            
cleanup
Yuki Kimoto authored on 2011-08-10
3500
test 'table_alias';
3501
$dbi = DBIx::Custom->connect;
3502
eval { $dbi->execute('drop table table1') };
3503
$dbi->execute($create_table1_type);
3504
$dbi->type_rule(
3505
    into1 => {
test cleanup
Yuki Kimoto authored on 2011-08-10
3506
        $date_typename => sub { '2010-' . $_[0] }
cleanup
Yuki Kimoto authored on 2011-08-10
3507
    }
3508
);
3509
$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => '01-01'},
3510
  table_alias => {table2 => 'table1'});
3511
$result = $dbi->select(table => 'table1');
3512
is($result->one->{key1}, '2010-01-01');
cleanup test
Yuki Kimoto authored on 2011-08-10
3513

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