DBIx-Custom / t / common.t /
Newer Older
3501 lines | 112.624kb
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
test 'type_rule into';
199
$dbi = DBIx::Custom->connect;
200
eval { $dbi->execute('drop table table1') };
201
$dbi->execute($create_table1_type);
202
$dbi->type_rule(
203
    into1 => {
204
        $date_typename => sub { '2010-' . $_[0] }
205
    }
206
);
207
$dbi->insert({key1 => '01-01'}, table => 'table1');
208
$result = $dbi->select(table => 'table1');
209
is($result->one->{key1}, '2010-01-01');
210

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

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

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

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

            
292

            
293

            
294

            
295

            
296

            
297

            
298

            
299

            
300

            
301

            
302

            
303

            
304

            
305

            
306

            
307

            
308

            
309

            
310

            
311

            
312

            
313

            
314

            
315

            
316

            
317

            
318

            
319

            
320

            
321

            
322

            
323

            
324

            
325

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

            
327
# Create table
test cleanup
Yuki Kimoto authored on 2011-08-10
328
eval { $dbi->execute('drop table table1') };
added common test executing ...
Yuki Kimoto authored on 2011-08-07
329
$dbi->execute($create_table1);
330
$model = $dbi->create_model(table => 'table1');
331
$model->insert({key1 => 1, key2 => 2});
332
is_deeply($model->select->all, [{key1 => 1, key2 => 2}]);
333

            
cleanup test
Yuki Kimoto authored on 2011-08-08
334
test 'DBIx::Custom::Result test';
335
$dbi->delete_all(table => 'table1');
336
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
337
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
338
$source = "select key1, key2 from table1";
339
$query = $dbi->create_query($source);
340
$result = $dbi->execute($query);
341

            
342
@rows = ();
343
while (my $row = $result->fetch) {
344
    push @rows, [@$row];
345
}
346
is_deeply(\@rows, [[1, 2], [3, 4]], "fetch");
347

            
348
$result = $dbi->execute($query);
349
@rows = ();
350
while (my $row = $result->fetch_hash) {
351
    push @rows, {%$row};
352
}
353
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "fetch_hash");
354

            
355
$result = $dbi->execute($query);
356
$rows = $result->fetch_all;
357
is_deeply($rows, [[1, 2], [3, 4]], "fetch_all");
358

            
359
$result = $dbi->execute($query);
360
$rows = $result->fetch_hash_all;
361
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "all");
362

            
363
test 'Insert query return value';
364
$source = "insert into table1 {insert_param key1 key2}";
365
$query = $dbi->execute($source, {}, query => 1);
366
$ret_val = $dbi->execute($query, param => {key1 => 1, key2 => 2});
367
ok($ret_val);
368

            
369
test 'Direct query';
370
$dbi->delete_all(table => 'table1');
371
$insert_source = "insert into table1 {insert_param key1 key2}";
372
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2});
373
$result = $dbi->execute('select * from table1;');
374
$rows = $result->all;
375
is_deeply($rows, [{key1 => 1, key2 => 2}]);
376

            
377
test 'Filter basic';
378
$dbi->delete_all(table => 'table1');
379
$dbi->register_filter(twice       => sub { $_[0] * 2}, 
380
                    three_times => sub { $_[0] * 3});
381

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

            
390
test 'Filter in';
391
$dbi->delete_all(table => 'table1');
392
$insert_source  = "insert into table1 {insert_param key1 key2};";
393
$insert_query = $dbi->execute($insert_source, {}, query => 1);
394
$dbi->execute($insert_query, param => {key1 => 2, key2 => 4});
395
$select_source = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}";
396
$select_query = $dbi->execute($select_source,{}, query => 1);
397
$select_query->filter({'table1.key1' => 'twice'});
398
$result = $dbi->execute($select_query, param => {'table1.key1' => [1,5], 'table1.key2' => [2,4]});
399
$rows = $result->all;
400
is_deeply($rows, [{key1 => 2, key2 => 4}], "filter");
401

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

            
408
$source = "select * from table1 where key1 = :key1 and {<> key2} and {< key3} and {> key4} and {>= key5};";
409
$query = $dbi->execute($source, {}, query => 1);
410
$result = $dbi->execute($query, param => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
411
$rows = $result->all;
412
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1");
413

            
414
$source = "select * from table1 where key1 = :key1 and {<> key2} and {< key3} and {> key4} and {>= key5};";
415
$query = $dbi->execute($source, {}, query => 1);
416
$result = $dbi->execute($query, {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
417
$rows = $result->all;
418
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1");
419

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

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

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

            
438
test 'DBIx::Custom::SQLTemplate insert tag';
439
$dbi->delete_all(table => 'table1');
440
$insert_source = 'insert into table1 {insert_param key1 key2 key3 key4 key5}';
441
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
442

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

            
447
test 'DBIx::Custom::SQLTemplate update tag';
448
$dbi->delete_all(table => 'table1');
449
$insert_source = "insert into table1 {insert_param key1 key2 key3 key4 key5}";
450
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
451
$dbi->execute($insert_source, param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
452

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

            
456
$result = $dbi->execute('select * from table1 order by key1;');
457
$rows = $result->all;
458
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5},
459
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "basic");
460

            
cleanup test
Yuki Kimoto authored on 2011-08-08
461
test 'Named placeholder';
462
$dbi->execute('drop table table1');
463
$dbi->execute($create_table1_2);
464
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
465
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
466

            
467
$source = "select * from table1 where key1 = :key1 and key2 = :key2";
468
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
469
$rows = $result->all;
470
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
471

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

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

            
482
$source = "select * from table1 where key1 = :table1.key1 and key2 = :table1.key2";
483
$result = $dbi->execute(
484
    $source,
485
    param => {'table1.key1' => 1, 'table1.key2' => 1},
486
    filter => {'table1.key2' => sub { $_[0] * 2 }}
487
);
488
$rows = $result->all;
489
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
490

            
491
$dbi->execute('drop table table1');
492
$dbi->execute($create_table1);
493
$dbi->insert(table => 'table1', param => {key1 => '2011-10-14 12:19:18', key2 => 2});
494
$source = "select * from table1 where key1 = '2011-10-14 12:19:18' and key2 = :key2";
495
$result = $dbi->execute(
496
    $source,
497
    param => {'key2' => 2},
498
);
499

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

            
503
$dbi->delete_all(table => 'table1');
504
$dbi->insert(table => 'table1', param => {key1 => 'a:b c:d', key2 => 2});
505
$source = "select * from table1 where key1 = 'a\\:b c\\:d' and key2 = :key2";
506
$result = $dbi->execute(
507
    $source,
508
    param => {'key2' => 2},
509
);
510
$rows = $result->all;
511
is_deeply($rows, [{key1 => 'a:b c:d', key2 => 2}]);
512

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
520
test 'insert';
cleanup test
Yuki Kimoto authored on 2011-08-10
521
eval { $dbi->execute('drop table table1') };
522
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
523
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
524
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
525
$result = $dbi->execute('select * from table1;');
526
$rows   = $result->all;
527
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
528

            
529
$dbi->execute('delete from table1');
530
$dbi->register_filter(
531
    twice       => sub { $_[0] * 2 },
532
    three_times => sub { $_[0] * 3 }
533
);
534
$dbi->default_bind_filter('twice');
535
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => 'three_times'});
536
$result = $dbi->execute('select * from table1;');
537
$rows   = $result->all;
538
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter");
539
$dbi->default_bind_filter(undef);
540

            
cleanup test
Yuki Kimoto authored on 2011-08-10
541
$dbi->execute('drop table table1');
542
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
543
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, append => '   ');
544
$rows = $dbi->select(table => 'table1')->all;
545
is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append');
546

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

            
550
eval{$dbi->insert(table => 'table', param => {';' => 1})};
551
like($@, qr/safety/);
552

            
cleanup test
Yuki Kimoto authored on 2011-08-10
553
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
554
$dbi->execute($create_table_reserved);
cleanup test
Yuki Kimoto authored on 2011-08-10
555
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
556
$dbi->insert(table => 'table', param => {select => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
557
$result = $dbi->execute("select * from ${q}table$p");
cleanup test
Yuki Kimoto authored on 2011-08-10
558
$rows   = $result->all;
test cleanup
Yuki Kimoto authored on 2011-08-10
559
is_deeply($rows, [{select => 2, update => undef}], "reserved word");
cleanup test
Yuki Kimoto authored on 2011-08-10
560

            
cleanup test
Yuki Kimoto authored on 2011-08-10
561
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
562
$dbi->execute($create_table1);
563
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
564
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
565
$result = $dbi->execute('select * from table1;');
566
$rows   = $result->all;
567
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
568

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
577
test 'update';
578
eval { $dbi->execute('drop table table1') };
579
$dbi->execute($create_table1_2);
580
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
581
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
582
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
583
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
584
$rows   = $result->all;
585
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
586
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
587
                  "basic");
588
                  
589
$dbi->execute("delete from table1");
590
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
591
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
592
$dbi->update(table => 'table1', param => {key2 => 12}, where => {key2 => 2, key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
593
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
594
$rows   = $result->all;
595
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
596
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
597
                  "update key same as search key");
598

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

            
606
$dbi->execute("delete from table1");
607
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
608
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
609
$dbi->register_filter(twice => sub { $_[0] * 2 });
610
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1},
611
              filter => {key2 => sub { $_[0] * 2 }});
test cleanup
Yuki Kimoto authored on 2011-08-10
612
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
613
$rows   = $result->all;
614
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5},
615
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
616
                  "filter");
617

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

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

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

            
626
eval { $dbi->execute('drop table table1') };
627
$dbi->execute($create_table1);
628
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
629
$where = $dbi->where;
630
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
631
$where->param({key1 => 1, key2 => 2});
632
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
633
$result = $dbi->select(table => 'table1');
634
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
635

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

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

            
660
eval{$dbi->update(table => 'table1', param => {';' => 1})};
661
like($@, qr/safety/);
662

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

            
666
eval { $dbi->execute('drop table table1') };
667
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
668
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
669
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
670
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
671
$dbi->insert(table => 'table', param => {select => 1});
672
$dbi->update(table => 'table', where => {select => 1}, param => {update => 2});
673
$result = $dbi->execute("select * from ${q}table$p");
674
$rows   = $result->all;
675
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
676

            
677
eval {$dbi->update_all(table => 'table', param => {';' => 2}) };
678
like($@, qr/safety/);
679

            
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 => {'table.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->execute('drop table table1') };
691
$dbi->execute($create_table1_2);
692
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
693
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
694
$dbi->update({key2 => 11}, table => 'table1', where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
695
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
696
$rows   = $result->all;
697
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
698
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
699
                  "basic");
700

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

            
712
test 'update_all';
713
eval { $dbi->execute('drop table table1') };
714
$dbi->execute($create_table1_2);
715
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
716
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
717
$dbi->register_filter(twice => sub { $_[0] * 2 });
718
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'});
719
$result = $dbi->execute('select * from table1;');
720
$rows   = $result->all;
721
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
722
                  {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
723
                  "filter");
724

            
725

            
726
test 'delete';
727
eval { $dbi->execute('drop table table1') };
728
$dbi->execute($create_table1);
729
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
730
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
731
$dbi->delete(table => 'table1', where => {key1 => 1});
732
$result = $dbi->execute('select * from table1;');
733
$rows   = $result->all;
734
is_deeply($rows, [{key1 => 3, key2 => 4}], "basic");
735

            
736
$dbi->execute("delete from table1;");
737
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
738
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
739
$dbi->register_filter(twice => sub { $_[0] * 2 });
740
$dbi->delete(table => 'table1', where => {key2 => 1}, filter => {key2 => 'twice'});
741
$result = $dbi->execute('select * from table1;');
742
$rows   = $result->all;
743
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter");
744

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

            
747
$dbi->delete_all(table => 'table1');
748
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
749
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
750
$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2});
751
$rows = $dbi->select(table => 'table1')->all;
752
is_deeply($rows, [{key1 => 3, key2 => 4}], "delete multi key");
753

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

            
757
eval { $dbi->execute('drop table table1') };
758
$dbi->execute($create_table1);
759
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
760
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
761
$where = $dbi->where;
762
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
763
$where->param({ke1 => 1, key2 => 2});
764
$dbi->delete(table => 'table1', where => $where);
765
$result = $dbi->select(table => 'table1');
766
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
767

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

            
782
eval { $dbi->execute('drop table table1') };
test cleanup
Yuki Kimoto authored on 2011-08-10
783
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
784
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
785
$dbi->delete(table => 'table1', where => {key1 => 1}, prefix => '    ');
786
$result = $dbi->execute('select * from table1;');
787
$rows   = $result->all;
788
is_deeply($rows, [], "basic");
789

            
790
test 'delete error';
791
eval { $dbi->execute('drop table table1') };
792
$dbi->execute($create_table1);
793
eval{$dbi->delete(table => 'table1')};
794
like($@, qr/"where" must be specified/,
795
         "where key-value pairs not specified");
796

            
797
eval{$dbi->delete(table => 'table1', where => {';' => 1})};
798
like($@, qr/safety/);
799

            
test cleanup
Yuki Kimoto authored on 2011-08-10
800
$dbi = undef;
test cleanup
Yuki Kimoto authored on 2011-08-10
801
$dbi = DBIx::Custom->connect;
802
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
803
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
804
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
805
$dbi->insert(table => 'table', param => {select => 1});
806
$dbi->delete(table => 'table', where => {select => 1});
807
$result = $dbi->execute("select * from ${q}table$p");
808
$rows   = $result->all;
809
is_deeply($rows, [], "reserved word");
810

            
811
test 'delete_all';
812
eval { $dbi->execute('drop table table1') };
813
$dbi->execute($create_table1);
814
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
815
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
816
$dbi->delete_all(table => 'table1');
817
$result = $dbi->execute('select * from table1;');
818
$rows   = $result->all;
819
is_deeply($rows, [], "basic");
820

            
821

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

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

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

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

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

            
843
$dbi->register_filter(decrement => sub { $_[0] - 1 });
844
$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'})
845
            ->all;
846
is_deeply($rows, [{key1 => 1, key2 => 2}], "filter");
847

            
test cleanup
Yuki Kimoto authored on 2011-08-10
848
eval { $dbi->execute("drop table table2") };
849
$dbi->execute($create_table2);
test cleanup
Yuki Kimoto authored on 2011-08-10
850
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
851
$rows = $dbi->select(
852
    table => [qw/table1 table2/],
853
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
854
    where   => {'table1.key2' => 2},
855
    relation  => {'table1.key1' => 'table2.key1'}
856
)->all;
857
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : exists where");
858

            
859
$rows = $dbi->select(
860
    table => [qw/table1 table2/],
861
    column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
862
    relation  => {'table1.key1' => 'table2.key1'}
863
)->all;
864
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : no exists where");
865

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

            
869
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
870
eval { $dbi->execute("drop table ${q}table$p") };
871
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
872
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
873
$dbi->insert(table => 'table', param => {select => 1, update => 2});
874
$result = $dbi->select(table => 'table', where => {select => 1});
875
$rows   = $result->all;
876
is_deeply($rows, [{select => 2, update => 2}], "reserved word");
877

            
878
test 'fetch filter';
879
eval { $dbi->execute('drop table table1') };
880
$dbi->register_filter(
881
    twice       => sub { $_[0] * 2 },
882
    three_times => sub { $_[0] * 3 }
883
);
884
$dbi->default_fetch_filter('twice');
885
$dbi->execute($create_table1);
886
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
887
$result = $dbi->select(table => 'table1');
888
$result->filter({key1 => 'three_times'});
889
$row = $result->one;
890
is_deeply($row, {key1 => 3, key2 => 4}, "default_fetch_filter and filter");
891

            
892
test 'filters';
893
$dbi = DBIx::Custom->new;
894

            
895
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')),
896
   'あ', "decode_utf8");
897

            
898
is($dbi->filters->{encode_utf8}->('あ'),
899
   encode_utf8('あ'), "encode_utf8");
900

            
cleanup test
Yuki Kimoto authored on 2011-08-10
901
test 'transaction1';
test cleanup
Yuki Kimoto authored on 2011-08-10
902
$dbi = DBIx::Custom->connect;
903
eval { $dbi->execute('drop table table1') };
904
$dbi->execute($create_table1);
905
$dbi->dbh->begin_work;
906
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
907
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
908
$dbi->dbh->commit;
909
$result = $dbi->select(table => 'table1');
910
is_deeply(scalar $result->all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}],
911
          "commit");
912

            
913
$dbi = DBIx::Custom->connect;
914
eval { $dbi->execute('drop table table1') };
915
$dbi->execute($create_table1);
916
$dbi->dbh->begin_work(0);
917
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
918
$dbi->dbh->rollback;
919

            
920
$result = $dbi->select(table => 'table1');
921
ok(! $result->fetch_first, "rollback");
922

            
923
test 'execute';
924
eval { $dbi->execute('drop table table1') };
925
$dbi->execute($create_table1);
926
{
927
    local $Carp::Verbose = 0;
928
    eval{$dbi->execute('select * frm table1')};
929
    like($@, qr/\Qselect * frm table1;/, "fail prepare");
930
    like($@, qr/\.t /, "fail : not verbose");
931
}
932
{
933
    local $Carp::Verbose = 1;
934
    eval{$dbi->execute('select * frm table1')};
935
    like($@, qr/Custom.*\.t /s, "fail : verbose");
936
}
937

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

            
941
$query = $dbi->execute('select * from table1 where key1 = :key1', {}, query => 1);
942
$dbi->dbh->disconnect;
943
eval{$dbi->execute($query, param => {key1 => {a => 1}})};
944
ok($@, "execute fail");
945

            
946
{
947
    local $Carp::Verbose = 0;
948
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
949
    like($@, qr/\Q.t /, "caller spec : not vebose");
950
}
951
{
952
    local $Carp::Verbose = 1;
953
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
954
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
955
}
956

            
957

            
cleanup test
Yuki Kimoto authored on 2011-08-10
958
test 'transaction2';
test cleanup
Yuki Kimoto authored on 2011-08-10
959
$dbi = DBIx::Custom->connect;
960
eval { $dbi->execute('drop table table1') };
961
$dbi->execute($create_table1);
962

            
963
$dbi->begin_work;
964

            
965
eval {
966
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
967
    die "Error";
968
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
969
};
970

            
971
$dbi->rollback if $@;
972

            
973
$result = $dbi->select(table => 'table1');
974
$rows = $result->all;
975
is_deeply($rows, [], "rollback");
976

            
977
$dbi->begin_work;
978

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

            
984
$dbi->commit unless $@;
985

            
986
$result = $dbi->select(table => 'table1');
987
$rows = $result->all;
988
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "commit");
989

            
990
$dbi->dbh->{AutoCommit} = 0;
991
eval{ $dbi->begin_work };
992
ok($@, "exception");
993
$dbi->dbh->{AutoCommit} = 1;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
994

            
test cleanup
Yuki Kimoto authored on 2011-08-10
995
test 'cache';
996
eval { $dbi->execute('drop table table1') };
997
$dbi->cache(1);
998
$dbi->execute($create_table1);
999
$source = 'select * from table1 where key1 = :key1 and key2 = :key2;';
1000
$dbi->execute($source, {}, query => 1);
1001
is_deeply($dbi->{_cached}->{$source}, 
1002
          {sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2'], tables => []}, "cache");
1003

            
1004
eval { $dbi->execute('drop table table1') };
1005
$dbi->execute($create_table1);
1006
$dbi->{_cached} = {};
1007
$dbi->cache(0);
1008
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1009
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
1010

            
1011
test 'execute';
1012
eval { $dbi->execute('drop table table1') };
1013
$dbi->execute($create_table1);
1014
{
1015
    local $Carp::Verbose = 0;
1016
    eval{$dbi->execute('select * frm table1')};
1017
    like($@, qr/\Qselect * frm table1;/, "fail prepare");
1018
    like($@, qr/\.t /, "fail : not verbose");
1019
}
1020
{
1021
    local $Carp::Verbose = 1;
1022
    eval{$dbi->execute('select * frm table1')};
1023
    like($@, qr/Custom.*\.t /s, "fail : verbose");
1024
}
1025

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

            
1029
$query = $dbi->execute('select * from table1 where key1 = :key1', {}, query => 1);
1030
$dbi->dbh->disconnect;
1031
eval{$dbi->execute($query, param => {key1 => {a => 1}})};
1032
ok($@, "execute fail");
1033

            
1034
{
1035
    local $Carp::Verbose = 0;
1036
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
1037
    like($@, qr/\Q.t /, "caller spec : not vebose");
1038
}
1039
{
1040
    local $Carp::Verbose = 1;
1041
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
1042
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
1043
}
1044

            
1045
test 'method';
1046
$dbi->method(
1047
    one => sub { 1 }
1048
);
1049
$dbi->method(
1050
    two => sub { 2 }
1051
);
1052
$dbi->method({
1053
    twice => sub {
1054
        my $self = shift;
1055
        return $_[0] * 2;
1056
    }
1057
});
1058

            
1059
is($dbi->one, 1, "first");
1060
is($dbi->two, 2, "second");
1061
is($dbi->twice(5), 10 , "second");
1062

            
1063
eval {$dbi->XXXXXX};
1064
ok($@, "not exists");
1065

            
1066
test 'out filter';
1067
$dbi = DBIx::Custom->connect;
1068
eval { $dbi->execute('drop table table1') };
1069
$dbi->execute($create_table1);
1070
$dbi->register_filter(twice => sub { $_[0] * 2 });
1071
$dbi->register_filter(three_times => sub { $_[0] * 3});
1072
$dbi->apply_filter(
1073
    'table1', 'key1' => {out => 'twice', in => 'three_times'}, 
1074
              'key2' => {out => 'three_times', in => 'twice'});
1075
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1076
$result = $dbi->execute('select * from table1;');
1077
$row   = $result->fetch_hash_first;
1078
is_deeply($row, {key1 => 2, key2 => 6}, "insert");
1079
$result = $dbi->select(table => 'table1');
1080
$row   = $result->one;
1081
is_deeply($row, {key1 => 6, key2 => 12}, "insert");
1082

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

            
1099
$dbi = DBIx::Custom->connect;
1100
eval { $dbi->execute('drop table table1') };
1101
$dbi->execute($create_table1);
1102
$dbi->register_filter(twice => sub { $_[0] * 2 });
1103
$dbi->apply_filter(
1104
    'table1', 'key1' => {out => 'twice', in => 'twice'}
1105
);
1106
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => undef});
1107
$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2});
1108
$result = $dbi->execute('select * from table1;');
1109
$row   = $result->one;
1110
is_deeply($row, {key1 => 4, key2 => 2}, "update");
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 => 2, key2 => 2}, filter => {key1=> undef});
1120
$dbi->delete(table => 'table1', where => {key1 => 1});
1121
$result = $dbi->execute('select * from table1;');
1122
$rows   = $result->all;
1123
is_deeply($rows, [], "delete");
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
$result = $dbi->select(table => 'table1', where => {key1 => 1});
1134
$result->filter({'key2' => 'twice'});
1135
$rows   = $result->all;
1136
is_deeply($rows, [{key1 => 4, key2 => 4}], "select");
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->execute("select * from table1 where key1 = :key1 and key2 = :key2;",
1147
                        param => {key1 => 1, key2 => 2},
1148
                        table => ['table1']);
1149
$rows   = $result->all;
1150
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute");
1151

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

            
1165
$dbi = DBIx::Custom->connect;
1166
eval { $dbi->execute('drop table table1') };
1167
eval { $dbi->execute('drop table table2') };
1168
$dbi->execute($create_table1);
1169
$dbi->execute($create_table2);
1170
$dbi->register_filter(twice => sub { $_[0] * 2 });
1171
$dbi->register_filter(three_times => sub { $_[0] * 3 });
1172
$dbi->apply_filter(
1173
    'table1', 'key2' => {out => 'twice', in => 'twice'}
1174
);
1175
$dbi->apply_filter(
1176
    'table2', 'key3' => {out => 'three_times', in => 'three_times'}
1177
);
1178
$dbi->insert(table => 'table1', param => {key1 => 5, key2 => 2}, filter => {key2 => undef});
1179
$dbi->insert(table => 'table2', param => {key1 => 5, key3 => 6}, filter => {key3 => undef});
1180
$result = $dbi->select(
1181
     table => ['table1', 'table2'],
1182
     column => ['key2', 'key3'],
1183
     where => {'table1.key2' => 1, 'table2.key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
1184

            
1185
$result->filter({'key2' => 'twice'});
1186
$rows   = $result->all;
1187
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join");
1188

            
1189
$result = $dbi->select(
1190
     table => ['table1', 'table2'],
1191
     column => ['key2', 'key3'],
1192
     where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
1193

            
1194
$result->filter({'key2' => 'twice'});
1195
$rows   = $result->all;
1196
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join : omit");
1197

            
1198
test 'each_column';
1199
$dbi = DBIx::Custom->connect;
1200
eval { $dbi->execute("drop table ${q}table$p") };
1201
eval { $dbi->execute('drop table table1') };
1202
eval { $dbi->execute('drop table table2') };
test cleranup
Yuki Kimoto authored on 2011-08-10
1203
eval { $dbi->execute('drop table table3') };
test cleanup
Yuki Kimoto authored on 2011-08-10
1204
$dbi->execute($create_table1_type);
1205
$dbi->execute($create_table2);
1206

            
1207
$infos = [];
1208
$dbi->each_column(sub {
1209
    my ($self, $table, $column, $cinfo) = @_;
1210
    
1211
    if ($table =~ /^table\d/) {
1212
         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
1213
         push @$infos, $info;
1214
    }
1215
});
1216
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
1217
is_deeply($infos, 
1218
    [
1219
        ['table1', 'key1', 'key1'],
1220
        ['table1', 'key2', 'key2'],
1221
        ['table2', 'key1', 'key1'],
1222
        ['table2', 'key3', 'key3']
1223
    ]
1224
    
1225
);
1226
test 'each_table';
1227
$dbi = DBIx::Custom->connect;
1228
eval { $dbi->execute('drop table table1') };
1229
eval { $dbi->execute('drop table table2') };
1230
$dbi->execute($create_table2);
1231
$dbi->execute($create_table1_type);
1232

            
1233
$infos = [];
1234
$dbi->each_table(sub {
1235
    my ($self, $table, $table_info) = @_;
1236
    
1237
    if ($table =~ /^table\d/) {
1238
         my $info = [$table, $table_info->{TABLE_NAME}];
1239
         push @$infos, $info;
1240
    }
1241
});
1242
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
1243
is_deeply($infos, 
1244
    [
1245
        ['table1', 'table1'],
1246
        ['table2', 'table2'],
1247
    ]
1248
);
1249

            
1250
test 'limit';
1251
$dbi = DBIx::Custom->connect;
1252
eval { $dbi->execute('drop table table1') };
1253
$dbi->execute($create_table1);
1254
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1255
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
1256
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
1257
$dbi->register_tag(
1258
    limit => sub {
1259
        my ($count, $offset) = @_;
1260
        
1261
        my $s = '';
1262
        $s .= "limit $count";
1263
        $s .= " offset $offset" if defined $offset;
1264
        
1265
        return [$s, []];
1266
    }
1267
);
1268
$rows = $dbi->select(
1269
  table => 'table1',
1270
  where => {key1 => 1},
1271
  append => "order by key2 {limit 1 0}"
1272
)->all;
1273
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1274
$rows = $dbi->select(
1275
  table => 'table1',
1276
  where => {key1 => 1},
1277
  append => "order by key2 {limit 2 1}"
1278
)->all;
1279
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
1280
$rows = $dbi->select(
1281
  table => 'table1',
1282
  where => {key1 => 1},
1283
  append => "order by key2 {limit 1}"
1284
)->all;
1285
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1286

            
1287
test 'connect super';
test cleanup
Yuki Kimoto authored on 2011-08-10
1288
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1289
eval { $dbi->execute('drop table table1') };
1290
$dbi->execute($create_table1);
1291
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1292
is($dbi->select(table => 'table1')->one->{key1}, 1);
1293

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1294
$dbi = DBIx::Custom->new;
test cleanup
Yuki Kimoto authored on 2011-08-10
1295
$dbi->connect;
1296
eval { $dbi->execute('drop table table1') };
1297
$dbi->execute($create_table1);
1298
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1299
is($dbi->select(table => 'table1')->one->{key1}, 1);
1300

            
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

            
1307
test 'end_filter';
1308
$dbi = DBIx::Custom->connect;
1309
eval { $dbi->execute('drop table table1') };
1310
$dbi->execute($create_table1);
1311
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1312
$result = $dbi->select(table => 'table1');
1313
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1314
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 });
1315
$row = $result->fetch_first;
1316
is_deeply($row, [6, 40]);
1317

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

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

            
1338
$dbi->register_filter(five_times => sub { $_[0] * 5 });
1339
$result = $dbi->select(table => 'table1');
1340
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1341
$result->end_filter({key1 => sub { $_[0] * 3 }, key2 => 'five_times' });
1342
$row = $result->one;
1343
is_deeply($row, {key1 => 6, key2 => 40});
1344

            
1345
$dbi->register_filter(five_times => sub { $_[0] * 5 });
1346
$dbi->apply_filter('table1',
1347
    key1 => {end => sub { $_[0] * 3 } },
1348
    key2 => {end => 'five_times'}
1349
);
1350
$result = $dbi->select(table => 'table1');
1351
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1352
$row = $result->one;
1353
is_deeply($row, {key1 => 6, key2 => 40}, 'apply_filter');
1354

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1367
test 'remove_end_filter and remove_filter';
1368
$dbi = DBIx::Custom->connect;
1369
eval { $dbi->execute('drop table table1') };
1370
$dbi->execute($create_table1);
1371
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1372
$result = $dbi->select(table => 'table1');
1373
$row = $result
1374
       ->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 })
1375
       ->remove_filter
1376
       ->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 })
1377
       ->remove_end_filter
1378
       ->fetch_first;
1379
is_deeply($row, [1, 2]);
1380

            
1381
test 'empty where select';
1382
$dbi = DBIx::Custom->connect;
1383
eval { $dbi->execute('drop table table1') };
1384
$dbi->execute($create_table1);
1385
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1386
$result = $dbi->select(table => 'table1', where => {});
1387
$row = $result->one;
1388
is_deeply($row, {key1 => 1, key2 => 2});
1389

            
1390
test 'select query option';
1391
$dbi = DBIx::Custom->connect;
1392
eval { $dbi->execute('drop table table1') };
1393
$dbi->execute($create_table1);
1394
$query = $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, query => 1);
1395
is(ref $query, 'DBIx::Custom::Query');
1396
$query = $dbi->update(table => 'table1', where => {key1 => 1}, param => {key2 => 2}, query => 1);
1397
is(ref $query, 'DBIx::Custom::Query');
1398
$query = $dbi->delete(table => 'table1', where => {key1 => 1}, query => 1);
1399
is(ref $query, 'DBIx::Custom::Query');
1400
$query = $dbi->select(table => 'table1', where => {key1 => 1, key2 => 2}, query => 1);
1401
is(ref $query, 'DBIx::Custom::Query');
1402

            
1403
test 'where';
1404
$dbi = DBIx::Custom->connect;
1405
eval { $dbi->execute('drop table table1') };
1406
$dbi->execute($create_table1);
1407
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1408
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1409
$where = $dbi->where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
1410
is("$where", "where ( key1 = :key1 and key2 = :key2 )", 'no param');
1411

            
1412
$where = $dbi->where
1413
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
1414
             ->param({key1 => 1});
1415

            
1416
$result = $dbi->select(
1417
    table => 'table1',
1418
    where => $where
1419
);
1420
$row = $result->all;
1421
is_deeply($row, [{key1 => 1, key2 => 2}]);
1422

            
1423
$result = $dbi->select(
1424
    table => 'table1',
1425
    where => [
1426
        ['and', 'key1 = :key1', 'key2 = :key2'],
1427
        {key1 => 1}
1428
    ]
1429
);
1430
$row = $result->all;
1431
is_deeply($row, [{key1 => 1, key2 => 2}]);
1432

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

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

            
1453
$where = $dbi->where
1454
             ->clause(['and', ['or', 'key1 > :key1', 'key1 < :key1'], 'key2 = :key2'])
1455
             ->param({key1 => [0, 3], key2 => 2});
1456
$result = $dbi->select(
1457
    table => 'table1',
1458
    where => $where,
1459
); 
1460
$row = $result->all;
1461
is_deeply($row, [{key1 => 1, key2 => 2}]);
1462

            
1463
$where = $dbi->where;
1464
$result = $dbi->select(
1465
    table => 'table1',
1466
    where => $where
1467
);
1468
$row = $result->all;
1469
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1470

            
1471
eval {
1472
$where = $dbi->where
1473
             ->clause(['uuu']);
1474
$result = $dbi->select(
1475
    table => 'table1',
1476
    where => $where
1477
);
1478
};
1479
ok($@);
1480

            
1481
$where = $dbi->where;
1482
is("$where", '');
1483

            
1484
$where = $dbi->where
1485
             ->clause(['or', ('key1 = :key1') x 2])
1486
             ->param({key1 => [1, 3]});
1487
$result = $dbi->select(
1488
    table => 'table1',
1489
    where => $where,
1490
);
1491
$row = $result->all;
1492
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1493

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

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

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

            
1524
$where = $dbi->where
1525
             ->clause('key1 = :key1 key2 = :key2')
1526
             ->param({key1 => 1});
1527
eval{$where->to_string};
1528
like($@, qr/one column/);
1529

            
1530
$where = $dbi->where
1531
             ->clause(['or', ('key1 = :key1') x 3])
1532
             ->param({key1 => [$dbi->not_exists, 1, 3]});
1533
$result = $dbi->select(
1534
    table => 'table1',
1535
    where => $where,
1536
);
1537
$row = $result->all;
1538
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1539

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

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

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

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

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

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

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

            
1610
$where = $dbi->where
1611
             ->clause(['and', '{> key1}', '{< key1}' ])
1612
             ->param({key1 => [2, $dbi->not_exists]});
1613
$result = $dbi->select(
1614
    table => 'table1',
1615
    where => $where,
1616
);
1617
$row = $result->all;
1618
is_deeply($row, [{key1 => 3, key2 => 4}], 'not_exists');
1619

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

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

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

            
1650
$where = $dbi->where
1651
             ->clause(['and', 'key1 is not null', 'key2 is not null' ]);
1652
$result = $dbi->select(
1653
    table => 'table1',
1654
    where => $where,
1655
);
1656
$row = $result->all;
1657
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1658

            
1659
eval {$dbi->where(ppp => 1) };
1660
like($@, qr/invalid/);
1661

            
1662
$where = $dbi->where(
1663
    clause => ['and', ['or'], ['and', 'key1 = :key1', 'key2 = :key2']],
1664
    param => {key1 => 1, key2 => 2}
1665
);
1666
$result = $dbi->select(
1667
    table => 'table1',
1668
    where => $where,
1669
);
1670
$row = $result->all;
1671
is_deeply($row, [{key1 => 1, key2 => 2}]);
1672

            
1673

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

            
1685
$where = $dbi->where;
1686
$where->clause(['and', ':key1{=}']);
1687
$where->param({key1 => undef});
1688
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1689
$row = $result->all;
1690
is_deeply($row, [{key1 => 1, key2 => 2}]);
1691

            
1692
$where = $dbi->where;
1693
$where->clause(['and', ':key1{=}']);
1694
$where->param({key1 => undef});
1695
$where->if('defined');
1696
$where->map;
1697
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1698
$row = $result->all;
1699
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1700

            
1701
$where = $dbi->where;
1702
$where->clause(['or', ':key1{=}', ':key1{=}']);
1703
$where->param({key1 => [undef, undef]});
1704
$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]});
1705
$row = $result->all;
1706
is_deeply($row, [{key1 => 1, key2 => 2}]);
1707
$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]});
1708
$row = $result->all;
1709
is_deeply($row, [{key1 => 1, key2 => 2}]);
1710

            
1711
$where = $dbi->where;
1712
$where->clause(['and', ':key1{=}']);
1713
$where->param({key1 => [undef, undef]});
1714
$where->if('defined');
1715
$where->map;
1716
$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]});
1717
$row = $result->all;
1718
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1719
$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]});
1720
$row = $result->all;
1721
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1722

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

            
1732
$where = $dbi->where;
1733
$where->clause(['and', ':key1{=}']);
1734
$where->param({key1 => ''});
1735
$where->if('length');
1736
$where->map;
1737
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1738
$row = $result->all;
1739
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1740

            
1741
$where = $dbi->where;
1742
$where->clause(['and', ':key1{=}']);
1743
$where->param({key1 => 5});
1744
$where->if(sub { ($_[0] || '') eq 5 });
1745
$where->map;
1746
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1747
$row = $result->all;
1748
is_deeply($row, [{key1 => 1, key2 => 2}]);
1749

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

            
1759
$where = $dbi->where;
1760
$where->param({id => 1, author => 'Ken', price => 1900});
1761
$where->map(id => 'book.id',
1762
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1763
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1764
);
1765
is_deeply($where->param, {'book.id' => 1, 'book.author' => '%Ken%',
1766
  'book.price' => 1900});
1767

            
1768
$where = $dbi->where;
1769
$where->param({id => 0, author => 0, price => 0});
1770
$where->map(
1771
    id => 'book.id',
1772
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1773
    price => ['book.price', sub { '%' . $_[0] . '%' },
1774
      {if => sub { $_[0] eq 0 }}]
1775
);
1776
is_deeply($where->param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
1777

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

            
1789
$where = $dbi->where;
1790
$where->param({id => undef, author => undef, price => undef});
1791
$where->if('length');
1792
$where->map(
1793
    id => 'book.id',
1794
    price => ['book.price', {if => 'exists'}]
1795
);
1796
is_deeply($where->param, {'book.price' => undef});
1797

            
1798
$where = $dbi->where;
1799
$where->param({price => 'a'});
1800
$where->if('length');
1801
$where->map(
1802
    id => ['book.id', {if => 'exists'}],
1803
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
1804
);
1805
is_deeply($where->param, {'book.price' => '%a'});
1806

            
1807
$where = $dbi->where;
1808
$where->param({id => [1, 2], author => 'Ken', price => 1900});
1809
$where->map(
1810
    id => 'book.id',
1811
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1812
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1813
);
1814
is_deeply($where->param, {'book.id' => [1, 2], 'book.author' => '%Ken%',
1815
  'book.price' => 1900});
1816

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

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

            
1838
test 'dbi_option default';
1839
$dbi = DBIx::Custom->new;
1840
is_deeply($dbi->dbi_option, {});
1841

            
1842
test 'register_tag_processor';
1843
$dbi = DBIx::Custom->connect;
1844
$dbi->register_tag_processor(
1845
    a => sub { 1 }
1846
);
test cleanup
Yuki Kimoto authored on 2011-08-10
1847
is($dbi->{_tags}->{a}->(), 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1848

            
1849
test 'register_tag';
1850
$dbi = DBIx::Custom->connect;
1851
$dbi->register_tag(
1852
    b => sub { 2 }
1853
);
test cleanup
Yuki Kimoto authored on 2011-08-10
1854
is($dbi->{_tags}->{b}->(), 2);
test cleanup
Yuki Kimoto authored on 2011-08-10
1855

            
1856
test 'table not specify exception';
1857
$dbi = DBIx::Custom->connect;
1858
eval {$dbi->insert};
1859
like($@, qr/table/);
1860
eval {$dbi->update};
1861
like($@, qr/table/);
1862
eval {$dbi->delete};
1863
like($@, qr/table/);
1864
eval {$dbi->select};
1865
like($@, qr/table/);
test cleanup
Yuki Kimoto authored on 2011-08-10
1866

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1867
test 'more tests';
1868
$dbi = DBIx::Custom->connect;
1869
eval{$dbi->apply_filter('table', 'column', [])};
1870
like($@, qr/apply_filter/);
1871

            
1872
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
1873
like($@, qr/apply_filter/);
1874

            
1875
$dbi->apply_filter(
1876

            
1877
);
1878
$dbi = DBIx::Custom->connect;
1879
eval { $dbi->execute('drop table table1') };
1880
$dbi->execute($create_table1);
1881
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1882
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1883
$dbi->apply_filter('table1', 'key2', 
1884
                   {in => sub { $_[0] * 3 }, out => sub { $_[0] * 2 }});
1885
$rows = $dbi->select(table => 'table1', where => {key2 => 1})->all;
1886
is_deeply($rows, [{key1 => 1, key2 => 6}]);
1887

            
1888
$dbi = DBIx::Custom->connect;
1889
eval { $dbi->execute('drop table table1') };
1890
$dbi->execute($create_table1);
1891
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1892
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1893
$dbi->apply_filter('table1', 'key2', {});
1894
$rows = $dbi->select(table => 'table1', where => {key2 => 2})->all;
1895
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1896

            
1897
$dbi = DBIx::Custom->connect;
1898
eval {$dbi->apply_filter('table1', 'key2', {out => 'no'})};
1899
like($@, qr/not registered/);
1900
eval {$dbi->apply_filter('table1', 'key2', {in => 'no'})};
1901
like($@, qr/not registered/);
1902
$dbi->method({one => sub { 1 }});
1903
is($dbi->one, 1);
1904

            
1905
eval{DBIx::Custom->connect(dsn => undef)};
1906
like($@, qr/_connect/);
1907

            
1908
$dbi = DBIx::Custom->connect;
1909
eval { $dbi->execute('drop table table1') };
1910
$dbi->execute($create_table1);
1911
$dbi->register_filter(twice => sub { $_[0] * 2 });
1912
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
1913
             filter => {key1 => 'twice'});
1914
$row = $dbi->select(table => 'table1')->one;
1915
is_deeply($row, {key1 => 2, key2 => 2});
1916
eval {$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
1917
             filter => {key1 => 'no'}) };
1918
like($@, qr//);
1919

            
1920
$dbi->register_filter(one => sub { });
1921
$dbi->default_fetch_filter('one');
1922
ok($dbi->default_fetch_filter);
1923
$dbi->default_bind_filter('one');
1924
ok($dbi->default_bind_filter);
1925
eval{$dbi->default_fetch_filter('no')};
1926
like($@, qr/not registered/);
1927
eval{$dbi->default_bind_filter('no')};
1928
like($@, qr/not registered/);
1929
$dbi->default_bind_filter(undef);
1930
ok(!defined $dbi->default_bind_filter);
1931
$dbi->default_fetch_filter(undef);
1932
ok(!defined $dbi->default_fetch_filter);
1933
eval {$dbi->execute('select * from table1 {} {= author') };
1934
like($@, qr/Tag not finished/);
1935

            
1936
$dbi = DBIx::Custom->connect;
1937
eval { $dbi->execute('drop table table1') };
1938
$dbi->execute($create_table1);
1939
$dbi->register_filter(one => sub { 1 });
1940
$result = $dbi->select(table => 'table1');
1941
eval {$result->filter(key1 => 'no')};
1942
like($@, qr/not registered/);
1943
eval {$result->end_filter(key1 => 'no')};
1944
like($@, qr/not registered/);
1945
$result->default_filter(undef);
1946
ok(!defined $result->default_filter);
1947
$result->default_filter('one');
1948
is($result->default_filter->(), 1);
1949

            
1950
test 'dbi_option';
1951
$dbi = DBIx::Custom->connect(dbi_option => {PrintError => 1});
1952
ok($dbi->dbh->{PrintError});
1953
$dbi = DBIx::Custom->connect(dbi_options => {PrintError => 1});
1954
ok($dbi->dbh->{PrintError});
1955

            
1956
test 'DBIx::Custom::Result stash()';
1957
$result = DBIx::Custom::Result->new;
1958
is_deeply($result->stash, {}, 'default');
1959
$result->stash->{foo} = 1;
1960
is($result->stash->{foo}, 1, 'get and set');
test cleanup
Yuki Kimoto authored on 2011-08-10
1961

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1962
test 'delete_at';
1963
$dbi = DBIx::Custom->connect;
1964
eval { $dbi->execute('drop table table1') };
1965
$dbi->execute($create_table1_2);
1966
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1967
$dbi->delete_at(
1968
    table => 'table1',
1969
    primary_key => ['key1', 'key2'],
1970
    where => [1, 2],
1971
);
1972
is_deeply($dbi->select(table => 'table1')->all, []);
1973

            
1974
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1975
$dbi->delete_at(
1976
    table => 'table1',
1977
    primary_key => 'key1',
1978
    where => 1,
1979
);
1980
is_deeply($dbi->select(table => 'table1')->all, []);
1981

            
1982
test 'insert_at';
1983
$dbi = DBIx::Custom->connect;
1984
eval { $dbi->execute('drop table table1') };
1985
$dbi->execute($create_table1_2);
1986
$dbi->insert_at(
1987
    primary_key => ['key1', 'key2'], 
1988
    table => 'table1',
1989
    where => [1, 2],
1990
    param => {key3 => 3}
1991
);
1992
is($dbi->select(table => 'table1')->one->{key1}, 1);
1993
is($dbi->select(table => 'table1')->one->{key2}, 2);
1994
is($dbi->select(table => 'table1')->one->{key3}, 3);
1995

            
1996
$dbi->delete_all(table => 'table1');
1997
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1998
$dbi->insert_at(
1999
    primary_key => 'key1', 
2000
    table => 'table1',
2001
    where => 1,
2002
    param => {key2 => 2, key3 => 3}
2003
);
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
eval {
2010
    $dbi->insert_at(
2011
        table => 'table1',
2012
        primary_key => ['key1', 'key2'],
2013
        where => {},
2014
        param => {key1 => 1, key2 => 2, key3 => 3},
2015
    );
2016
};
2017
like($@, qr/must be/);
2018

            
2019
$dbi = DBIx::Custom->connect;
2020
eval { $dbi->execute('drop table table1') };
2021
$dbi->execute($create_table1_2);
2022
$dbi->insert_at(
2023
    {key3 => 3},
2024
    primary_key => ['key1', 'key2'], 
2025
    table => 'table1',
2026
    where => [1, 2],
2027
);
2028
is($dbi->select(table => 'table1')->one->{key1}, 1);
2029
is($dbi->select(table => 'table1')->one->{key2}, 2);
2030
is($dbi->select(table => 'table1')->one->{key3}, 3);
2031

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

            
2047
$dbi->delete_all(table => 'table1');
2048
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2049
$dbi->update_at(
2050
    table => 'table1',
2051
    primary_key => 'key1',
2052
    where => 1,
2053
    param => {key3 => 4}
2054
);
2055
is($dbi->select(table => 'table1')->one->{key1}, 1);
2056
is($dbi->select(table => 'table1')->one->{key2}, 2);
2057
is($dbi->select(table => 'table1')->one->{key3}, 4);
2058

            
2059
$dbi = DBIx::Custom->connect;
2060
eval { $dbi->execute('drop table table1') };
2061
$dbi->execute($create_table1_2);
2062
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2063
$dbi->update_at(
2064
    {key3 => 4},
2065
    table => 'table1',
2066
    primary_key => ['key1', 'key2'],
2067
    where => [1, 2]
2068
);
2069
is($dbi->select(table => 'table1')->one->{key1}, 1);
2070
is($dbi->select(table => 'table1')->one->{key2}, 2);
2071
is($dbi->select(table => 'table1')->one->{key3}, 4);
2072

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

            
2088
$dbi->delete_all(table => 'table1');
2089
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2090
$result = $dbi->select_at(
2091
    table => 'table1',
2092
    primary_key => 'key1',
2093
    where => 1,
2094
);
2095
$row = $result->one;
2096
is($row->{key1}, 1);
2097
is($row->{key2}, 2);
2098
is($row->{key3}, 3);
2099

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

            
2112
eval {
2113
    $result = $dbi->select_at(
2114
        table => 'table1',
2115
        primary_key => ['key1', 'key2'],
2116
        where => {},
2117
    );
2118
};
2119
like($@, qr/must be/);
2120

            
2121
eval {
2122
    $result = $dbi->select_at(
2123
        table => 'table1',
2124
        primary_key => ['key1', 'key2'],
2125
        where => [1],
2126
    );
2127
};
2128
like($@, qr/same/);
2129

            
2130
eval {
2131
    $result = $dbi->update_at(
2132
        table => 'table1',
2133
        primary_key => ['key1', 'key2'],
2134
        where => {},
2135
        param => {key1 => 1, key2 => 2},
2136
    );
2137
};
2138
like($@, qr/must be/);
2139

            
2140
eval {
2141
    $result = $dbi->delete_at(
2142
        table => 'table1',
2143
        primary_key => ['key1', 'key2'],
2144
        where => {},
2145
    );
2146
};
2147
like($@, qr/must be/);
2148

            
2149
test 'columns';
2150
use MyDBI1;
2151
$dbi = MyDBI1->connect;
2152
$model = $dbi->model('book');
2153

            
2154

            
2155
test 'model delete_at';
2156
$dbi = MyDBI6->connect;
2157
eval { $dbi->execute('drop table table1') };
2158
eval { $dbi->execute('drop table table2') };
2159
eval { $dbi->execute('drop table table3') };
2160
$dbi->execute($create_table1_2);
2161
$dbi->execute($create_table2_2);
2162
$dbi->execute($create_table3);
2163
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2164
$dbi->model('table1')->delete_at(where => [1, 2]);
2165
is_deeply($dbi->select(table => 'table1')->all, []);
2166
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
2167
$dbi->model('table1_1')->delete_at(where => [1, 2]);
2168
is_deeply($dbi->select(table => 'table1')->all, []);
2169
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
2170
$dbi->model('table1_3')->delete_at(where => [1, 2]);
2171
is_deeply($dbi->select(table => 'table1')->all, []);
2172

            
2173
test 'model insert_at';
2174
$dbi = MyDBI6->connect;
2175
eval { $dbi->execute('drop table table1') };
2176
$dbi->execute($create_table1_2);
2177
$dbi->model('table1')->insert_at(
2178
    where => [1, 2],
2179
    param => {key3 => 3}
2180
);
2181
$result = $dbi->model('table1')->select;
2182
$row = $result->one;
2183
is($row->{key1}, 1);
2184
is($row->{key2}, 2);
2185
is($row->{key3}, 3);
2186

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

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

            
2213

            
2214
test 'mycolumn and column';
2215
$dbi = MyDBI7->connect;
2216
eval { $dbi->execute('drop table table1') };
2217
eval { $dbi->execute('drop table table2') };
2218
$dbi->execute($create_table1);
2219
$dbi->execute($create_table2);
2220
$dbi->separator('__');
2221
$dbi->setup_model;
2222
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2223
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2224
$model = $dbi->model('table1');
2225
$result = $model->select(
2226
    column => [$model->mycolumn, $model->column('table2')],
2227
    where => {'table1.key1' => 1}
2228
);
2229
is_deeply($result->one,
2230
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2231

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2232
test 'insert_param';
2233
$dbi = DBIx::Custom->connect;
2234
eval { $dbi->execute('drop table table1') };
2235
$dbi->execute($create_table1_2);
2236
$param = {key1 => 1, key2 => 2};
2237
$insert_param = $dbi->insert_param($param);
2238
$sql = <<"EOS";
2239
insert into table1 $insert_param
2240
EOS
2241
$dbi->execute($sql, param => $param, table => 'table1');
2242
is($dbi->select(table => 'table1')->one->{key1}, 1);
2243
is($dbi->select(table => 'table1')->one->{key2}, 2);
2244

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2260
test 'mycolumn';
2261
$dbi = MyDBI8->connect;
2262
eval { $dbi->execute('drop table table1') };
2263
eval { $dbi->execute('drop table table2') };
2264
$dbi->execute($create_table1);
2265
$dbi->execute($create_table2);
2266
$dbi->setup_model;
2267
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2268
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2269
$model = $dbi->model('table1');
2270
$result = $model->select_at(
2271
    column => [
2272
        $model->mycolumn,
2273
        $model->column('table2')
2274
    ]
2275
);
2276
is_deeply($result->one,
2277
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
2278

            
2279
$result = $model->select_at(
2280
    column => [
2281
        $model->mycolumn(['key1']),
2282
        $model->column(table2 => ['key1'])
2283
    ]
2284
);
2285
is_deeply($result->one,
2286
          {key1 => 1, 'table2.key1' => 1});
2287
$result = $model->select_at(
2288
    column => [
2289
        $model->mycolumn(['key1']),
2290
        {table2 => ['key1']}
2291
    ]
2292
);
2293
is_deeply($result->one,
2294
          {key1 => 1, 'table2.key1' => 1});
2295

            
2296
$result = $model->select_at(
2297
    column => [
2298
        $model->mycolumn(['key1']),
2299
        ['table2.key1', as => 'table2.key1']
2300
    ]
2301
);
2302
is_deeply($result->one,
2303
          {key1 => 1, 'table2.key1' => 1});
2304

            
2305
$result = $model->select_at(
2306
    column => [
2307
        $model->mycolumn(['key1']),
2308
        ['table2.key1' => 'table2.key1']
2309
    ]
2310
);
2311
is_deeply($result->one,
2312
          {key1 => 1, 'table2.key1' => 1});
2313

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2314
test 'merge_param';
2315
$dbi = DBIx::Custom->new;
2316
$params = [
2317
    {key1 => 1, key2 => 2, key3 => 3},
2318
    {key1 => 1, key2 => 2},
2319
    {key1 => 1}
2320
];
2321
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]);
2322
is_deeply($param, {key1 => [1, 1, 1], key2 => [2, 2], key3 => 3});
2323

            
2324
$params = [
2325
    {key1 => [1, 2], key2 => 1, key3 => [1, 2]},
2326
    {key1 => [3, 4], key2 => [2, 3], key3 => 3}
2327
];
2328
$param = $dbi->merge_param($params->[0], $params->[1]);
2329
is_deeply($param, {key1 => [1, 2, 3, 4], key2 => [1, 2, 3], key3 => [1, 2, 3]});
2330

            
2331
test 'select() param option';
2332
$dbi = DBIx::Custom->connect;
2333
eval { $dbi->execute('drop table table1') };
2334
$dbi->execute($create_table1);
2335
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2336
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2337
eval { $dbi->execute('drop table table2') };
2338
$dbi->execute($create_table2);
2339
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2340
$dbi->insert(table => 'table2', param => {key1 => 2, key3 => 5});
2341
$rows = $dbi->select(
2342
    table => 'table1',
2343
    column => 'table1.key1 as table1_key1, key2, key3',
2344
    where   => {'table1.key2' => 3},
2345
    join  => ['inner join (select * from table2 where {= table2.key3})' . 
2346
              ' as table2 on table1.key1 = table2.key1'],
2347
    param => {'table2.key3' => 5}
2348
)->all;
2349
is_deeply($rows, [{table1_key1 => 2, key2 => 3, key3 => 5}]);
2350

            
2351
test 'select() string where';
2352
$dbi = DBIx::Custom->connect;
2353
eval { $dbi->execute('drop table table1') };
2354
$dbi->execute($create_table1);
2355
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2356
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2357
$rows = $dbi->select(
2358
    table => 'table1',
2359
    where => 'key1 = :key1 and key2 = :key2',
2360
    where_param => {key1 => 1, key2 => 2}
2361
)->all;
2362
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2363

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

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

            
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 => [
2400
        'key1 = :key1 and key2 = :key2',
2401
         {key1 => 1, key2 => 2}
2402
    ]
2403
);
2404
$rows = $dbi->select(table => 'table1')->all;
2405
is_deeply($rows, [{key1 => 2, key2 => 3}]);
2406

            
2407

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

            
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 => [
2430
        'key1 = :key1 and key2 = :key2',
2431
        {key1 => 1, key2 => 2}
2432
    ]
2433
);
2434
$rows = $dbi->select(table => 'table1')->all;
2435
is_deeply($rows, [{key1 => 5, key2 => 2}]);
2436

            
2437
test 'insert id and primary_key option';
2438
$dbi = DBIx::Custom->connect;
2439
eval { $dbi->execute('drop table table1') };
2440
$dbi->execute($create_table1_2);
2441
$dbi->insert(
2442
    primary_key => ['key1', 'key2'], 
2443
    table => 'table1',
2444
    id => [1, 2],
2445
    param => {key3 => 3}
2446
);
2447
is($dbi->select(table => 'table1')->one->{key1}, 1);
2448
is($dbi->select(table => 'table1')->one->{key2}, 2);
2449
is($dbi->select(table => 'table1')->one->{key3}, 3);
2450

            
2451
$dbi->delete_all(table => 'table1');
2452
$dbi->insert(
2453
    primary_key => 'key1', 
2454
    table => 'table1',
2455
    id => 0,
2456
    param => {key2 => 2, key3 => 3}
2457
);
2458

            
2459
is($dbi->select(table => 'table1')->one->{key1}, 0);
2460
is($dbi->select(table => 'table1')->one->{key2}, 2);
2461
is($dbi->select(table => 'table1')->one->{key3}, 3);
2462

            
2463
$dbi = DBIx::Custom->connect;
2464
eval { $dbi->execute('drop table table1') };
2465
$dbi->execute($create_table1_2);
2466
$dbi->insert(
2467
    {key3 => 3},
2468
    primary_key => ['key1', 'key2'], 
2469
    table => 'table1',
2470
    id => [1, 2],
2471
);
2472
is($dbi->select(table => 'table1')->one->{key1}, 1);
2473
is($dbi->select(table => 'table1')->one->{key2}, 2);
2474
is($dbi->select(table => 'table1')->one->{key3}, 3);
test cleanup
Yuki Kimoto authored on 2011-08-10
2475

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

            
2490
$dbi = MyDBI6->connect;
2491
eval { $dbi->execute('drop table table1') };
2492
$dbi->execute($create_table1_2);
2493
$dbi->model('table1')->insert(
2494
    {key3 => 3},
2495
    id => [1, 2]
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
test 'update and id option';
2504
$dbi = DBIx::Custom->connect;
2505
eval { $dbi->execute('drop table table1') };
2506
$dbi->execute($create_table1_2);
2507
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2508
$dbi->update(
2509
    table => 'table1',
2510
    primary_key => ['key1', 'key2'],
2511
    id => [1, 2],
2512
    param => {key3 => 4}
2513
);
2514
is($dbi->select(table => 'table1')->one->{key1}, 1);
2515
is($dbi->select(table => 'table1')->one->{key2}, 2);
2516
is($dbi->select(table => 'table1')->one->{key3}, 4);
2517

            
2518
$dbi->delete_all(table => 'table1');
2519
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2520
$dbi->update(
2521
    table => 'table1',
2522
    primary_key => 'key1',
2523
    id => 0,
2524
    param => {key3 => 4}
2525
);
2526
is($dbi->select(table => 'table1')->one->{key1}, 0);
2527
is($dbi->select(table => 'table1')->one->{key2}, 2);
2528
is($dbi->select(table => 'table1')->one->{key3}, 4);
2529

            
2530
$dbi = DBIx::Custom->connect;
2531
eval { $dbi->execute('drop table table1') };
2532
$dbi->execute($create_table1_2);
2533
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2534
$dbi->update(
2535
    {key3 => 4},
2536
    table => 'table1',
2537
    primary_key => ['key1', 'key2'],
2538
    id => [1, 2]
2539
);
2540
is($dbi->select(table => 'table1')->one->{key1}, 1);
2541
is($dbi->select(table => 'table1')->one->{key2}, 2);
2542
is($dbi->select(table => 'table1')->one->{key3}, 4);
2543

            
2544

            
2545
test 'model update and id option';
2546
$dbi = MyDBI6->connect;
2547
eval { $dbi->execute('drop table table1') };
2548
$dbi->execute($create_table1_2);
2549
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2550
$dbi->model('table1')->update(
2551
    id => [1, 2],
2552
    param => {key3 => 4}
2553
);
2554
$result = $dbi->model('table1')->select;
2555
$row = $result->one;
2556
is($row->{key1}, 1);
2557
is($row->{key2}, 2);
2558
is($row->{key3}, 4);
2559

            
2560

            
2561
test 'delete and id option';
2562
$dbi = DBIx::Custom->connect;
2563
eval { $dbi->execute('drop table table1') };
2564
$dbi->execute($create_table1_2);
2565
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2566
$dbi->delete(
2567
    table => 'table1',
2568
    primary_key => ['key1', 'key2'],
2569
    id => [1, 2],
2570
);
2571
is_deeply($dbi->select(table => 'table1')->all, []);
2572

            
2573
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2574
$dbi->delete(
2575
    table => 'table1',
2576
    primary_key => 'key1',
2577
    id => 0,
2578
);
2579
is_deeply($dbi->select(table => 'table1')->all, []);
2580

            
2581

            
2582
test 'model delete and id option';
2583
$dbi = MyDBI6->connect;
2584
eval { $dbi->execute('drop table table1') };
2585
eval { $dbi->execute('drop table table2') };
2586
eval { $dbi->execute('drop table table3') };
2587
$dbi->execute($create_table1_2);
2588
$dbi->execute($create_table2_2);
2589
$dbi->execute($create_table3);
2590
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2591
$dbi->model('table1')->delete(id => [1, 2]);
2592
is_deeply($dbi->select(table => 'table1')->all, []);
2593
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
2594
$dbi->model('table1_1')->delete(id => [1, 2]);
2595
is_deeply($dbi->select(table => 'table1')->all, []);
2596
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
2597
$dbi->model('table1_3')->delete(id => [1, 2]);
2598
is_deeply($dbi->select(table => 'table1')->all, []);
2599

            
2600

            
2601
test 'select and id option';
2602
$dbi = DBIx::Custom->connect;
2603
eval { $dbi->execute('drop table table1') };
2604
$dbi->execute($create_table1_2);
2605
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2606
$result = $dbi->select(
2607
    table => 'table1',
2608
    primary_key => ['key1', 'key2'],
2609
    id => [1, 2]
2610
);
2611
$row = $result->one;
2612
is($row->{key1}, 1);
2613
is($row->{key2}, 2);
2614
is($row->{key3}, 3);
2615

            
2616
$dbi->delete_all(table => 'table1');
2617
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2618
$result = $dbi->select(
2619
    table => 'table1',
2620
    primary_key => 'key1',
2621
    id => 0,
2622
);
2623
$row = $result->one;
2624
is($row->{key1}, 0);
2625
is($row->{key2}, 2);
2626
is($row->{key3}, 3);
2627

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

            
2640

            
2641
test 'model select_at';
2642
$dbi = MyDBI6->connect;
2643
eval { $dbi->execute('drop table table1') };
2644
$dbi->execute($create_table1_2);
2645
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2646
$result = $dbi->model('table1')->select(id => [1, 2]);
2647
$row = $result->one;
2648
is($row->{key1}, 1);
2649
is($row->{key2}, 2);
2650
is($row->{key3}, 3);
2651

            
2652
test 'column separator is default .';
2653
$dbi = MyDBI7->connect;
2654
eval { $dbi->execute('drop table table1') };
2655
eval { $dbi->execute('drop table table2') };
2656
$dbi->execute($create_table1);
2657
$dbi->execute($create_table2);
2658
$dbi->setup_model;
2659
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2660
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2661
$model = $dbi->model('table1');
2662
$result = $model->select(
2663
    column => [$model->column('table2')],
2664
    where => {'table1.key1' => 1}
2665
);
2666
is_deeply($result->one,
2667
          {'table2.key1' => 1, 'table2.key3' => 3});
2668

            
2669
$result = $model->select(
2670
    column => [$model->column('table2' => [qw/key1 key3/])],
2671
    where => {'table1.key1' => 1}
2672
);
2673
is_deeply($result->one,
2674
          {'table2.key1' => 1, 'table2.key3' => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2675

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2676
test 'separator';
2677
$dbi = DBIx::Custom->connect;
2678
eval { $dbi->execute('drop table table1') };
2679
eval { $dbi->execute('drop table table2') };
2680
$dbi->execute($create_table1);
2681
$dbi->execute($create_table2);
test cleanup
Yuki Kimoto authored on 2011-08-10
2682

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2683
$dbi->create_model(
2684
    table => 'table1',
2685
    join => [
2686
       'left outer join table2 on table1.key1 = table2.key1'
2687
    ],
2688
    primary_key => ['key1'],
2689
);
2690
$model2 = $dbi->create_model(
2691
    table => 'table2',
2692
);
2693
$dbi->setup_model;
2694
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2695
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2696
$model = $dbi->model('table1');
2697
$result = $model->select(
2698
    column => [
2699
        $model->mycolumn,
2700
        {table2 => [qw/key1 key3/]}
2701
    ],
2702
    where => {'table1.key1' => 1}
2703
);
2704
is_deeply($result->one,
2705
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
2706
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2707

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2708
$dbi->separator('__');
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

            
2734

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2735
test 'filter_off';
2736
$dbi = DBIx::Custom->connect;
2737
eval { $dbi->execute('drop table table1') };
2738
eval { $dbi->execute('drop table table2') };
2739
$dbi->execute($create_table1);
2740
$dbi->execute($create_table2);
test cleanup
Yuki Kimoto authored on 2011-08-10
2741

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2742
$dbi->create_model(
2743
    table => 'table1',
2744
    join => [
2745
       'left outer join table2 on table1.key1 = table2.key1'
2746
    ],
2747
    primary_key => ['key1'],
2748
);
2749
$dbi->setup_model;
2750
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2751
$model = $dbi->model('table1');
2752
$result = $model->select(column => 'key1');
2753
$result->filter(key1 => sub { $_[0] * 2 });
2754
is_deeply($result->one, {key1 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
2755

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2756
test 'available_datetype';
2757
$dbi = DBIx::Custom->connect;
2758
ok($dbi->can('available_datatype'));
test cleanup
Yuki Kimoto authored on 2011-08-10
2759

            
2760

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2761
test 'select prefix option';
2762
$dbi = DBIx::Custom->connect;
2763
eval { $dbi->execute('drop table table1') };
2764
$dbi->execute($create_table1);
2765
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2766
$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all;
2767
is_deeply($rows, [{key1 => 1, key2 => 2}], "table");
test cleanup
Yuki Kimoto authored on 2011-08-10
2768

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2769
test 'map_param';
2770
$dbi = DBIx::Custom->connect;
2771
$param = $dbi->map_param(
2772
    {id => 1, author => 'Ken', price => 1900},
2773
    id => 'book.id',
2774
    author => ['book.author', sub { '%' . $_[0] . '%' }],
2775
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
2776
);
2777
is_deeply($param, {'book.id' => 1, 'book.author' => '%Ken%',
2778
  'book.price' => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
2779

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2780
$param = $dbi->map_param(
2781
    {id => 0, author => 0, price => 0},
2782
    id => 'book.id',
2783
    author => ['book.author', sub { '%' . $_[0] . '%' }],
2784
    price => ['book.price', sub { '%' . $_[0] . '%' },
2785
      {if => sub { $_[0] eq 0 }}]
2786
);
2787
is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
test cleanup
Yuki Kimoto authored on 2011-08-10
2788

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2798
$param = $dbi->map_param(
2799
    {id => undef, author => undef, price => undef},
2800
    id => 'book.id',
2801
    price => ['book.price', {if => 'exists'}]
2802
);
2803
is_deeply($param, {'book.price' => undef});
test cleanup
Yuki Kimoto authored on 2011-08-10
2804

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2812
test 'order';
2813
$dbi = DBIx::Custom->connect;
2814
eval { $dbi->execute('drop table table1') };
2815
$dbi->execute($create_table1);
2816
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
2817
$dbi->insert({key1 => 1, key2 => 3}, table => 'table1');
2818
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
2819
$dbi->insert({key1 => 2, key2 => 4}, table => 'table1');
2820
my $order = $dbi->order;
2821
$order->prepend('key1', 'key2 desc');
2822
$result = $dbi->select(table => 'table1', append => "$order");
2823
is_deeply($result->all, [{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1},
2824
  {key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}]);
2825
$order->prepend('key1 desc');
2826
$result = $dbi->select(table => 'table1', append => "$order");
2827
is_deeply($result->all, [{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2},
2828
  {key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}]);
2829

            
2830
$order = $dbi->order;
2831
$order->prepend(['table1-key1'], [qw/table1-key2 desc/]);
2832
$result = $dbi->select(table => 'table1',
2833
  column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']],
2834
  append => "$order");
2835
is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3},
2836
  {'table1-key1' => 1, 'table1-key2' => 1},
2837
  {'table1-key1' => 2, 'table1-key2' => 4},
2838
  {'table1-key1' => 2, 'table1-key2' => 2}]);
2839

            
2840
test 'tag_parse';
2841
$dbi = DBIx::Custom->connect;
2842
$dbi->tag_parse(0);
2843
eval { $dbi->execute('drop table table1') };
2844
$dbi->execute($create_table1);
2845
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
2846
eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})};
2847
ok($@);
2848

            
2849
test 'last_sql';
2850
$dbi = DBIx::Custom->connect;
2851
eval { $dbi->execute('drop table table1') };
2852
$dbi->execute($create_table1);
2853
$dbi->execute('select * from table1');
2854
is($dbi->last_sql, 'select * from table1;');
2855

            
2856
eval{$dbi->execute("aaa")};
2857
is($dbi->last_sql, 'aaa;');
2858

            
2859
test 'DBIx::Custom header';
2860
$dbi = DBIx::Custom->connect;
2861
eval { $dbi->execute('drop table table1') };
2862
$dbi->execute($create_table1);
2863
$result = $dbi->execute('select key1 as h1, key2 as h2 from table1');
2864
is_deeply($result->header, [qw/h1 h2/]);
2865

            
2866
test 'Named placeholder :name(operater) syntax';
2867
$dbi->execute('drop table table1');
2868
$dbi->execute($create_table1_2);
2869
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
2870
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
2871

            
2872
$source = "select * from table1 where :key1{=} and :key2{=}";
2873
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
2874
$rows = $result->all;
2875
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
2876

            
2877
$source = "select * from table1 where :key1{ = } and :key2{=}";
2878
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
2879
$rows = $result->all;
2880
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
2881

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

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

            
2896
test 'high perfomance way';
2897
$dbi->execute('drop table table1');
2898
$dbi->execute($create_table1_highperformance);
2899
$rows = [
2900
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
2901
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
2902
];
2903
{
2904
    my $query;
2905
    foreach my $row (@$rows) {
2906
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
2907
      $dbi->execute($query, $row, filter => {ab => sub { $_[0] * 2 }});
2908
    }
2909
    is_deeply($dbi->select(table => 'table1')->all,
2910
      [
2911
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
2912
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
2913
      ]
2914
    );
2915
}
2916

            
2917
$dbi->execute('drop table table1');
2918
$dbi->execute($create_table1_highperformance);
2919
$rows = [
2920
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
2921
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
2922
];
2923
{
2924
    my $query;
2925
    my $sth;
2926
    foreach my $row (@$rows) {
2927
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
2928
      $sth ||= $query->sth;
2929
      $sth->execute(map { $row->{$_} } sort keys %$row);
2930
    }
2931
    is_deeply($dbi->select(table => 'table1')->all,
2932
      [
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
}
2938

            
2939
test 'result';
2940
$dbi = DBIx::Custom->connect;
2941
eval { $dbi->execute('drop table table1') };
2942
$dbi->execute($create_table1);
2943
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
2944
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
2945

            
2946
$result = $dbi->select(table => 'table1');
2947
@rows = ();
2948
while (my $row = $result->fetch) {
2949
    push @rows, [@$row];
2950
}
2951
is_deeply(\@rows, [[1, 2], [3, 4]]);
2952

            
2953
$result = $dbi->select(table => 'table1');
2954
@rows = ();
2955
while (my $row = $result->fetch_hash) {
2956
    push @rows, {%$row};
2957
}
2958
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
2959

            
2960
$dbi = DBIx::Custom->connect;
2961
eval { $dbi->execute('drop table table1') };
2962
$dbi->execute($create_table1);
2963
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
2964
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
2965

            
2966
test 'fetch_all';
2967
$result = $dbi->select(table => 'table1');
2968
$rows = $result->fetch_all;
2969
is_deeply($rows, [[1, 2], [3, 4]]);
2970

            
2971
$result = $dbi->select(table => 'table1');
2972
$rows = $result->fetch_hash_all;
2973
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
2974

            
2975
$result = $dbi->select(table => 'table1');
2976
$result->dbi->filters({three_times => sub { $_[0] * 3}});
2977
$result->filter({key1 => 'three_times'});
2978

            
2979
$rows = $result->fetch_all;
2980
is_deeply($rows, [[3, 2], [9, 4]], "array");
2981

            
2982
$result = $dbi->select(table => 'table1');
2983
$result->dbi->filters({three_times => sub { $_[0] * 3}});
2984
$result->filter({key1 => 'three_times'});
2985
$rows = $result->fetch_hash_all;
2986
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash");
2987

            
2988
test "query_builder";
2989
$datas = [
2990
    # Basic tests
2991
    {   name            => 'placeholder basic',
2992
        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
2993
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;",
2994
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
2995
    },
2996
    {
2997
        name            => 'placeholder in',
2998
        source            => "{in k1 3};",
2999
        sql_expected    => "k1 in (?, ?, ?);",
3000
        columns_expected   => [qw/k1 k1 k1/]
3001
    },
3002
    
3003
    # Table name
3004
    {
3005
        name            => 'placeholder with table name',
3006
        source            => "{= a.k1} {= a.k2}",
3007
        sql_expected    => "a.k1 = ? a.k2 = ?;",
3008
        columns_expected  => [qw/a.k1 a.k2/]
3009
    },
3010
    {   
3011
        name            => 'placeholder in with table name',
3012
        source            => "{in a.k1 2} {in b.k2 2}",
3013
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?);",
3014
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
3015
    },
3016
    {
3017
        name            => 'not contain tag',
3018
        source            => "aaa",
3019
        sql_expected    => "aaa;",
3020
        columns_expected  => [],
3021
    }
3022
];
3023

            
3024
for (my $i = 0; $i < @$datas; $i++) {
3025
    my $data = $datas->[$i];
3026
    my $builder = DBIx::Custom->new->query_builder;
3027
    my $query = $builder->build_query($data->{source});
3028
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
3029
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
3030
}
3031

            
3032
$builder = DBIx::Custom->new->query_builder;
3033
$ret_val = $builder->register_tag(
3034
    p => sub {
3035
        my @args = @_;
3036
        
3037
        my $expand    = "? $args[0] $args[1]";
3038
        my $columns = [2];
3039
        return [$expand, $columns];
3040
    }
3041
);
3042

            
3043
$query = $builder->build_query("{p a b}");
3044
is($query->{sql}, "? a b;", "register_tag sql");
3045
is_deeply($query->{columns}, [2], "register_tag columns");
3046
isa_ok($ret_val, 'DBIx::Custom::QueryBuilder');
3047

            
3048
$builder = DBIx::Custom->new->query_builder;
3049

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

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

            
3056
$builder->register_tag({
3057
    q => 'string'
3058
});
3059

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

            
3063
$builder->register_tag({
3064
   r => sub {} 
3065
});
3066

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

            
3070
$builder->register_tag({
3071
   s => sub { return ["a", ""]} 
3072
});
3073

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

            
3077
$builder->register_tag(
3078
    t => sub {return ["a", []]}
3079
);
3080

            
3081

            
3082
test 'General error case';
3083
$builder = DBIx::Custom->new->query_builder;
3084
$builder->register_tag(
3085
    a => sub {
3086
        return ["? ? ?", ['']];
3087
    }
3088
);
3089
eval{$builder->build_query("{a}")};
3090
like($@, qr/\QPlaceholder count/, "placeholder count is invalid");
3091

            
3092

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

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

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

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

            
3108
test 'variouse source';
3109
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;";
3110
$query = $builder->build_query($source);
3111
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1");
3112

            
3113
$source = "abc;";
3114
$query = $builder->build_query($source);
3115
is($query->sql, 'abc;', "basic : 2");
3116

            
3117
$source = "{= a}";
3118
$query = $builder->build_query($source);
3119
is($query->sql, 'a = ?;', "only tag");
3120

            
3121
$source = "000;";
3122
$query = $builder->build_query($source);
3123
is($query->sql, '000;', "contain 0 value");
3124

            
3125
$source = "a {= b} }";
3126
eval{$builder->build_query($source)};
3127
like($@, qr/unexpected "}"/, "error : 1");
3128

            
3129
$source = "a {= {}";
3130
eval{$builder->build_query($source)};
3131
like($@, qr/unexpected "{"/, "error : 2");
3132

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3133
test 'select() wrap option';
3134
$dbi = DBIx::Custom->connect;
3135
eval { $dbi->execute('drop table table1') };
3136
$dbi->execute($create_table1);
3137
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3138
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
3139
$rows = $dbi->select(
3140
    table => 'table1',
3141
    column => 'key1',
3142
    wrap => ['select * from (', ') as t where key1 = 1']
3143
)->all;
3144
is_deeply($rows, [{key1 => 1}]);
3145

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3146
eval {
3147
$dbi->select(
3148
    table => 'table1',
3149
    column => 'key1',
3150
    wrap => 'select * from ('
3151
)
3152
};
3153
like($@, qr/array/);
3154

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3155
test 'dbi method from model';
3156
$dbi = MyDBI9->connect;
3157
eval { $dbi->execute('drop table table1') };
3158
$dbi->execute($create_table1);
3159
$dbi->setup_model;
3160
$model = $dbi->model('table1');
3161
eval{$model->execute('select * from table1')};
3162
ok(!$@);
3163

            
3164
test 'column table option';
3165
$dbi = MyDBI9->connect;
3166
eval { $dbi->execute('drop table table1') };
3167
$dbi->execute($create_table1);
3168
eval { $dbi->execute('drop table table2') };
3169
$dbi->execute($create_table2);
3170
$dbi->setup_model;
3171
$dbi->execute('insert into table1 (key1, key2) values (1, 2);');
3172
$dbi->execute('insert into table2 (key1, key3) values (1, 4);');
3173
$model = $dbi->model('table1');
3174
$result = $model->select(
3175
    column => [
3176
        $model->column('table2', {alias => 'table2_alias'})
3177
    ],
3178
    where => {'table2_alias.key3' => 4}
3179
);
3180
is_deeply($result->one, 
3181
          {'table2_alias.key1' => 1, 'table2_alias.key3' => 4});
3182

            
3183
$dbi->separator('__');
3184
$result = $model->select(
3185
    column => [
3186
        $model->column('table2', {alias => 'table2_alias'})
3187
    ],
3188
    where => {'table2_alias.key3' => 4}
3189
);
3190
is_deeply($result->one, 
3191
          {'table2_alias__key1' => 1, 'table2_alias__key3' => 4});
3192

            
3193
$dbi->separator('-');
3194
$result = $model->select(
3195
    column => [
3196
        $model->column('table2', {alias => 'table2_alias'})
3197
    ],
3198
    where => {'table2_alias.key3' => 4}
3199
);
3200
is_deeply($result->one, 
3201
          {'table2_alias-key1' => 1, 'table2_alias-key3' => 4});
3202

            
3203
test 'create_model';
3204
$dbi = DBIx::Custom->connect;
3205
eval { $dbi->execute('drop table table1') };
3206
eval { $dbi->execute('drop table table2') };
3207
$dbi->execute($create_table1);
3208
$dbi->execute($create_table2);
3209

            
3210
$dbi->create_model(
3211
    table => 'table1',
3212
    join => [
3213
       'left outer join table2 on table1.key1 = table2.key1'
3214
    ],
3215
    primary_key => ['key1']
3216
);
3217
$model2 = $dbi->create_model(
3218
    table => 'table2'
3219
);
3220
$dbi->create_model(
3221
    table => 'table3',
3222
    filter => [
3223
        key1 => {in => sub { uc $_[0] }}
3224
    ]
3225
);
3226
$dbi->setup_model;
3227
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3228
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
3229
$model = $dbi->model('table1');
3230
$result = $model->select(
3231
    column => [$model->mycolumn, $model->column('table2')],
3232
    where => {'table1.key1' => 1}
3233
);
3234
is_deeply($result->one,
3235
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
3236
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3237

            
3238
test 'model method';
3239
$dbi = DBIx::Custom->connect;
3240
eval { $dbi->execute('drop table table2') };
3241
$dbi->execute($create_table2);
3242
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
3243
$model = $dbi->create_model(
3244
    table => 'table2'
3245
);
3246
$model->method(foo => sub { shift->select(@_) });
3247
is_deeply($model->foo->one, {key1 => 1, key3 => 3});
3248

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3249
test 'update_param';
3250
$dbi = DBIx::Custom->connect;
3251
eval { $dbi->execute('drop table table1') };
3252
$dbi->execute($create_table1_2);
3253
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3254
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3255

            
3256
$param = {key2 => 11};
3257
$update_param = $dbi->update_param($param);
3258
$sql = <<"EOS";
3259
update table1 $update_param
3260
where key1 = 1
3261
EOS
3262
$dbi->execute($sql, param => $param);
3263
$result = $dbi->execute('select * from table1 order by key1;', table => 'table1');
3264
$rows   = $result->all;
3265
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
3266
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
3267
                  "basic");
3268

            
3269

            
3270
$dbi = DBIx::Custom->connect;
3271
eval { $dbi->execute('drop table table1') };
3272
$dbi->execute($create_table1_2);
3273
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3274
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3275

            
3276
$param = {key2 => 11, key3 => 33};
3277
$update_param = $dbi->update_param($param);
3278
$sql = <<"EOS";
3279
update table1 $update_param
3280
where key1 = 1
3281
EOS
3282
$dbi->execute($sql, param => $param);
3283
$result = $dbi->execute('select * from table1 order by key1;', table => 'table1');
3284
$rows   = $result->all;
3285
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
3286
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
3287
                  "basic");
3288

            
3289
$dbi = DBIx::Custom->connect;
3290
eval { $dbi->execute('drop table table1') };
3291
$dbi->execute($create_table1_2);
3292
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3293
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3294

            
3295
$param = {key2 => 11, key3 => 33};
3296
$update_param = $dbi->update_param($param, {no_set => 1});
3297
$sql = <<"EOS";
3298
update table1 set $update_param
3299
where key1 = 1
3300
EOS
3301
$dbi->execute($sql, param => $param);
3302
$result = $dbi->execute('select * from table1 order by key1;', table => 'table1');
3303
$rows   = $result->all;
3304
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
3305
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
3306
                  "update param no_set");
3307

            
3308
            
3309
eval { $dbi->update_param({";" => 1}) };
3310
like($@, qr/not safety/);
3311

            
3312

            
3313
test 'update_param';
3314
$dbi = DBIx::Custom->connect;
3315
eval { $dbi->execute('drop table table1') };
3316
$dbi->execute($create_table1_2);
3317
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3318
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3319

            
3320
$param = {key2 => 11};
3321
$update_param = $dbi->assign_param($param);
3322
$sql = <<"EOS";
3323
update table1 set $update_param
3324
where key1 = 1
3325
EOS
3326
$dbi->execute($sql, param => $param, table => 'table1');
3327
$result = $dbi->execute('select * from table1 order by key1;');
3328
$rows   = $result->all;
3329
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
3330
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
3331
                  "basic");
3332

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3333
test 'join';
3334
$dbi = DBIx::Custom->connect;
3335
eval { $dbi->execute('drop table table1') };
3336
$dbi->execute($create_table1);
3337
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3338
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
3339
eval { $dbi->execute('drop table table2') };
3340
$dbi->execute($create_table2);
3341
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
3342
eval { $dbi->execute('drop table table3') };
3343
$dbi->execute('create table table3 (key3 int, key4 int);');
3344
$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4});
3345
$rows = $dbi->select(
3346
    table => 'table1',
3347
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
3348
    where   => {'table1.key2' => 2},
3349
    join  => ['left outer join table2 on table1.key1 = table2.key1']
3350
)->all;
3351
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]);
3352

            
3353
$rows = $dbi->select(
3354
    table => 'table1',
3355
    where   => {'key1' => 1},
3356
    join  => ['left outer join table2 on table1.key1 = table2.key1']
3357
)->all;
3358
is_deeply($rows, [{key1 => 1, key2 => 2}]);
3359

            
3360
eval {
3361
    $rows = $dbi->select(
3362
        table => 'table1',
3363
        column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
3364
        where   => {'table1.key2' => 2},
3365
        join  => {'table1.key1' => 'table2.key1'}
3366
    );
3367
};
3368
like ($@, qr/array/);
3369

            
3370
$rows = $dbi->select(
3371
    table => 'table1',
3372
    where   => {'key1' => 1},
3373
    join  => ['left outer join table2 on table1.key1 = table2.key1',
3374
              'left outer join table3 on table2.key3 = table3.key3']
3375
)->all;
3376
is_deeply($rows, [{key1 => 1, key2 => 2}]);
3377

            
3378
$rows = $dbi->select(
3379
    column => 'table3.key4 as table3__key4',
3380
    table => 'table1',
3381
    where   => {'table1.key1' => 1},
3382
    join  => ['left outer join table2 on table1.key1 = table2.key1',
3383
              'left outer join table3 on table2.key3 = table3.key3']
3384
)->all;
3385
is_deeply($rows, [{table3__key4 => 4}]);
3386

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

            
3396
$dbi = DBIx::Custom->connect;
3397
eval { $dbi->execute('drop table table1') };
3398
$dbi->execute($create_table1);
3399
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3400
eval { $dbi->execute('drop table table2') };
3401
$dbi->execute($create_table2);
3402
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
3403
$rows = $dbi->select(
3404
    table => 'table1',
3405
    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",
3406
    where   => {'table1.key2' => 2},
3407
    join  => ["left outer join ${q}table2$p on ${q}table1$p.${q}key1$p = ${q}table2$p.${q}key1$p"],
3408
)->all;
3409
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}],
3410
          'quote');
3411

            
3412

            
3413
$dbi = DBIx::Custom->connect;
3414
eval { $dbi->execute('drop table table1') };
3415
$dbi->execute($create_table1);
3416
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3417
$sql = <<"EOS";
3418
left outer join (
3419
  select * from table1 as t1
3420
  where t1.key2 = (
3421
    select max(t2.key2) from table1 as t2
3422
    where t1.key1 = t2.key1
3423
  )
3424
) as latest_table1 on table1.key1 = latest_table1.key1
3425
EOS
3426
$join = [$sql];
3427
$rows = $dbi->select(
3428
    table => 'table1',
3429
    column => 'latest_table1.key1 as latest_table1__key1',
3430
    join  => $join
3431
)->all;
3432
is_deeply($rows, [{latest_table1__key1 => 1}]);
3433

            
3434
$dbi = DBIx::Custom->connect;
3435
eval { $dbi->execute('drop table table1') };
3436
eval { $dbi->execute('drop table table2') };
3437
$dbi->execute($create_table1);
3438
$dbi->execute($create_table2);
3439
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3440
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
3441
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
3442
$result = $dbi->select(
3443
    table => 'table1',
3444
    join => [
3445
        "left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1"
3446
    ]
3447
);
3448
is_deeply($result->all, [{key1 => 1, key2 => 2}]);
3449
$result = $dbi->select(
3450
    table => 'table1',
3451
    column => [{table2 => ['key3']}],
3452
    join => [
3453
        "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1"
3454
    ]
3455
);
3456
is_deeply($result->all, [{'table2.key3' => 4}]);
3457
$result = $dbi->select(
3458
    table => 'table1',
3459
    column => [{table2 => ['key3']}],
3460
    join => [
3461
        "left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'"
3462
    ]
3463
);
3464
is_deeply($result->all, [{'table2.key3' => 4}]);
3465

            
3466
$dbi = DBIx::Custom->connect;
3467
eval { $dbi->execute('drop table table1') };
3468
eval { $dbi->execute('drop table table2') };
3469
$dbi->execute($create_table1);
3470
$dbi->execute($create_table2);
3471
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3472
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
3473
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
3474
$result = $dbi->select(
3475
    table => 'table1',
3476
    column => [{table2 => ['key3']}],
3477
    join => [
3478
        {
3479
            clause => "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1",
3480
            table => ['table1', 'table2']
3481
        }
3482
    ]
3483
);
3484
is_deeply($result->all, [{'table2.key3' => 4}]);
3485

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

            
cleanup
Yuki Kimoto authored on 2011-08-10
3487
test 'table_alias';
3488
$dbi = DBIx::Custom->connect;
3489
eval { $dbi->execute('drop table table1') };
3490
$dbi->execute($create_table1_type);
3491
$dbi->type_rule(
3492
    into1 => {
test cleanup
Yuki Kimoto authored on 2011-08-10
3493
        $date_typename => sub { '2010-' . $_[0] }
cleanup
Yuki Kimoto authored on 2011-08-10
3494
    }
3495
);
3496
$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => '01-01'},
3497
  table_alias => {table2 => 'table1'});
3498
$result = $dbi->select(table => 'table1');
3499
is($result->one->{key1}, '2010-01-01');
cleanup test
Yuki Kimoto authored on 2011-08-10
3500

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