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

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

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

            
15
plan 'no_plan';
16

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

            
21
    use strict;
22
    use warnings;
23

            
24
    use base 'DBIx::Custom';
25

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

            
37
    package MyModel2::Base1;
38

            
39
    use strict;
40
    use warnings;
41

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

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

            
46
    use strict;
47
    use warnings;
48

            
49
    use base 'MyModel2::Base1';
50

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

            
57
    sub list { shift->select; }
58

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

            
61
    use strict;
62
    use warnings;
63

            
64
    use base 'MyModel2::Base1';
65

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

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

            
77
    use strict;
78
    use warnings;
79

            
80
    use base 'DBIx::Custom';
81

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

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

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

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

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

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

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

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

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

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

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

            
275
$dbi = DBIx::Custom->connect;
276
eval { $dbi->execute('drop table table1') };
277
$dbi->execute($create_table1_type);
278
$dbi->register_filter(convert => sub {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
279
    my $value = shift || '';
test cleanup
Yuki Kimoto authored on 2011-08-10
280
    $value =~ s/02/03/;
281
    return $value;
282
});
283
$dbi->type_rule(
284
    from1 => {
285
        $date_datatype => 'convert',
286
    },
287
    into1 => {
288
        $date_typename => 'convert',
289
    }
290
);
291
$dbi->insert({key1 => '2010-02-02'}, table => 'table1');
292
$result = $dbi->select(table => 'table1');
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
293
like($result->fetch->[0], qr/^2010-03-03/);
test cleanup
Yuki Kimoto authored on 2011-08-10
294

            
test cleanup
Yuki Kimoto authored on 2011-08-10
295
test 'type_rule and filter order';
296
$dbi = DBIx::Custom->connect;
297
eval { $dbi->execute('drop table table1') };
298
$dbi->execute($create_table1_type);
299
$dbi->type_rule(
300
    into1 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
301
        $date_typename => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
302
    },
303
    into2 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
304
        $date_typename => sub { my $v = shift || ''; $v =~ s/5/6/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
305
    },
306
    from1 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
307
        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/7/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
308
    },
309
    from2 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
310
        $date_datatype => sub { my $v = shift || ''; $v =~ s/7/8/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
311
    }
312
);
313
$dbi->insert({key1 => '2010-01-03'}, 
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
314
  table => 'table1', filter => {key1 => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }});
test cleanup
Yuki Kimoto authored on 2011-08-10
315
$result = $dbi->select(table => 'table1');
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
316
$result->filter(key1 => sub { my $v = shift || ''; $v =~ s/8/9/; return $v });
317
like($result->fetch_first->[0], qr/^2010-01-09/);
test cleanup
Yuki Kimoto authored on 2011-08-10
318

            
319

            
test cleanup
Yuki Kimoto authored on 2011-08-10
320
$dbi = DBIx::Custom->connect;
321
eval { $dbi->execute('drop table table1') };
322
$dbi->execute($create_table1_type);
323
$dbi->type_rule(
324
    from1 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
325
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
326
    },
327
    from2 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
328
        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
329
    },
330
);
331
$dbi->insert({key1 => '2010-01-03'}, table => 'table1');
332
$result = $dbi->select(table => 'table1');
333
$result->type_rule(
334
    from1 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
335
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/6/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
336
    },
337
    from2 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
338
        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/8/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
339
    }
340
);
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
341
$result->filter(key1 => sub { my $v = shift || ''; $v =~ s/8/9/; return $v });
342
like($result->fetch_first->[0], qr/^2010-01-09/);
test cleanup
Yuki Kimoto authored on 2011-08-10
343

            
test cleanup
Yuki Kimoto authored on 2011-08-10
344
test 'type_rule_off';
345
$dbi = DBIx::Custom->connect;
346
eval { $dbi->execute('drop table table1') };
347
$dbi->execute($create_table1_type);
348
$dbi->type_rule(
349
    from1 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
350
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
351
    },
352
    into1 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
353
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
354
    }
355
);
356
$dbi->insert({key1 => '2010-01-03'}, table => 'table1', type_rule_off => 1);
357
$result = $dbi->select(table => 'table1', type_rule_off => 1);
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
358
like($result->type_rule_off->fetch->[0], qr/^2010-01-03/);
test cleanup
Yuki Kimoto authored on 2011-08-10
359

            
test cleanup
Yuki Kimoto authored on 2011-08-10
360
$dbi = DBIx::Custom->connect;
361
eval { $dbi->execute('drop table table1') };
362
$dbi->execute($create_table1_type);
363
$dbi->type_rule(
364
    from1 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
365
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
366
    },
367
    into1 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
368
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
369
    }
370
);
371
$dbi->insert({key1 => '2010-01-03'}, table => 'table1', type_rule_off => 1);
372
$result = $dbi->select(table => 'table1', type_rule_off => 1);
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
373
like($result->one->{key1}, qr/^2010-01-04/);
test cleanup
Yuki Kimoto authored on 2011-08-10
374

            
test cleanup
Yuki Kimoto authored on 2011-08-10
375
$dbi = DBIx::Custom->connect;
376
eval { $dbi->execute('drop table table1') };
377
$dbi->execute($create_table1_type);
378
$dbi->type_rule(
379
    from1 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
380
        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
381
    },
382
    into1 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
383
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
384
    }
385
);
386
$dbi->insert({key1 => '2010-01-03'}, table => 'table1');
387
$result = $dbi->select(table => 'table1');
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
388
like($result->one->{key1}, qr/^2010-01-05/);
test cleanup
Yuki Kimoto authored on 2011-08-10
389

            
test cleanup
Yuki Kimoto authored on 2011-08-10
390
$dbi = DBIx::Custom->connect;
391
eval { $dbi->execute('drop table table1') };
392
$dbi->execute($create_table1_type);
393
$dbi->type_rule(
394
    from1 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
395
        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
396
    },
397
    into1 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
398
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
399
    }
400
);
401
$dbi->insert({key1 => '2010-01-03'}, table => 'table1');
402
$result = $dbi->select(table => 'table1');
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
403
like($result->fetch->[0], qr/2010-01-05/);
test cleanup
Yuki Kimoto authored on 2011-08-10
404

            
test cleanup
Yuki Kimoto authored on 2011-08-10
405
$dbi = DBIx::Custom->connect;
406
eval { $dbi->execute('drop table table1') };
407
$dbi->execute($create_table1_type);
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
408
$dbi->register_filter(ppp => sub { my $v = shift || ''; $v =~ s/3/4/; return $v });
test cleanup
Yuki Kimoto authored on 2011-08-10
409
$dbi->type_rule(
410
    into1 => {
411
        $date_typename => 'ppp'
412
    }
413
);
414
$dbi->insert({key1 => '2010-01-03'}, table => 'table1');
415
$result = $dbi->select(table => 'table1');
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
416
like($result->one->{key1}, qr/^2010-01-04/);
test cleanup
Yuki Kimoto authored on 2011-08-10
417

            
test cleanup
Yuki Kimoto authored on 2011-08-10
418
eval{$dbi->type_rule(
419
    into1 => {
420
        $date_typename => 'pp'
421
    }
422
)};
423
like($@, qr/not registered/);
test cleanup
Yuki Kimoto authored on 2011-08-10
424

            
test cleanup
Yuki Kimoto authored on 2011-08-10
425
$dbi = DBIx::Custom->connect;
426
eval { $dbi->execute('drop table table1') };
427
$dbi->execute($create_table1_type);
428
eval {
429
    $dbi->type_rule(
430
        from1 => {
431
            Date => sub { $_[0] * 2 },
432
        }
433
    );
434
};
435
like($@, qr/lower/);
test cleanup
Yuki Kimoto authored on 2011-08-10
436

            
test cleanup
Yuki Kimoto authored on 2011-08-10
437
eval {
438
    $dbi->type_rule(
439
        into1 => {
440
            Date => sub { $_[0] * 2 },
441
        }
442
    );
443
};
444
like($@, qr/lower/);
test cleanup
Yuki Kimoto authored on 2011-08-10
445

            
test cleanup
Yuki Kimoto authored on 2011-08-10
446
$dbi = DBIx::Custom->connect;
447
eval { $dbi->execute('drop table table1') };
448
$dbi->execute($create_table1_type);
449
$dbi->type_rule(
450
    from1 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
451
        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
452
    },
453
    into1 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
454
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
455
    }
456
);
457
$dbi->insert({key1 => '2010-01-03'}, table => 'table1');
458
$result = $dbi->select(table => 'table1');
459
$result->type_rule_off;
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
460
like($result->one->{key1}, qr/^2010-01-04/);
test cleanup
Yuki Kimoto authored on 2011-08-10
461

            
test cleanup
Yuki Kimoto authored on 2011-08-10
462
$dbi = DBIx::Custom->connect;
463
eval { $dbi->execute('drop table table1') };
464
$dbi->execute($create_table1_type);
465
$dbi->type_rule(
466
    from1 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
467
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v },
468
        $datetime_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
469
    },
470
);
471
$dbi->insert({key1 => '2010-01-03', key2 => '2010-01-01 01:01:03'}, table => 'table1');
472
$result = $dbi->select(table => 'table1');
473
$result->type_rule(
474
    from1 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
475
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
476
    }
477
);
478
$row = $result->one;
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
479
like($row->{key1}, qr/^2010-01-05/);
480
like($row->{key2}, qr/^2010-01-01 01:01:03/);
test cleanup
Yuki Kimoto authored on 2011-08-10
481

            
test cleanup
Yuki Kimoto authored on 2011-08-10
482
$result = $dbi->select(table => 'table1');
483
$result->type_rule(
484
    from1 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
485
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
486
    }
487
);
488
$row = $result->one;
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
489
like($row->{key1}, qr/2010-01-05/);
490
like($row->{key2}, qr/2010-01-01 01:01:03/);
test cleanup
Yuki Kimoto authored on 2011-08-10
491

            
test cleanup
Yuki Kimoto authored on 2011-08-10
492
$result = $dbi->select(table => 'table1');
493
$result->type_rule(
494
    from1 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
495
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
496
    }
497
);
498
$row = $result->one;
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
499
like($row->{key1}, qr/2010-01-05/);
500
like($row->{key2}, qr/2010-01-01 01:01:03/);
test cleanup
Yuki Kimoto authored on 2011-08-10
501

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

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
519
$result = $dbi->select(table => 'table1');
520
$result->type_rule(
521
    from1 => [$date_datatype => undef]
522
);
523
$row = $result->one;
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
524
like($row->{key1}, qr/^2010-01-03/);
525
like($row->{key2}, qr/^2010-01-01 01:01:03/);
test cleanup
Yuki Kimoto authored on 2011-08-10
526

            
test cleanup
Yuki Kimoto authored on 2011-08-10
527
$dbi = DBIx::Custom->connect;
528
eval { $dbi->execute('drop table table1') };
529
$dbi->execute($create_table1_type);
530
$dbi->type_rule(
531
    from1 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
532
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v },
test cleanup
Yuki Kimoto authored on 2011-08-10
533
    },
534
);
535
$dbi->insert({key1 => '2010-01-03'}, table => 'table1');
536
$result = $dbi->select(table => 'table1');
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
537
$result->filter(key1 => sub { my $v = shift || ''; $v =~ s/4/5/; return $v });
538
like($result->one->{key1}, qr/^2010-01-05/);
test cleanup
Yuki Kimoto authored on 2011-08-10
539

            
test cleanup
Yuki Kimoto authored on 2011-08-10
540
$dbi = DBIx::Custom->connect;
541
eval { $dbi->execute('drop table table1') };
542
$dbi->execute($create_table1_type);
543
$dbi->type_rule(
544
    from1 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
545
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
546
    },
547
);
548
$dbi->insert({key1 => '2010-01-03'}, table => 'table1');
549
$result = $dbi->select(table => 'table1');
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
550
$result->filter(key1 => sub { my $v = shift || ''; $v =~ s/4/5/; return $v });
551
like($result->fetch->[0], qr/^2010-01-05/);
test cleanup
Yuki Kimoto authored on 2011-08-10
552

            
test cleanup
Yuki Kimoto authored on 2011-08-10
553
$dbi = DBIx::Custom->connect;
554
eval { $dbi->execute('drop table table1') };
555
$dbi->execute($create_table1_type);
556
$dbi->type_rule(
557
    into1 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
558
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
559
    },
560
    into2 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
561
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
562
    },
563
    from1 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
564
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/6/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
565
    },
566
    from2 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
567
        $date_datatype => sub { my $v = shift || ''; $v =~ s/(3|6)/7/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
568
    }
569
);
570
$dbi->insert({key1 => '2010-01-03'}, table => 'table1', type_rule_off => 1);
571
$result = $dbi->select(table => 'table1');
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
572
like($result->type_rule_off->fetch_first->[0], qr/^2010-01-03/);
test cleanup
Yuki Kimoto authored on 2011-08-10
573
$result = $dbi->select(table => 'table1');
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
574
like($result->type_rule_on->fetch_first->[0], qr/^2010-01-07/);
test cleanup
Yuki Kimoto authored on 2011-08-10
575

            
test cleanup
Yuki Kimoto authored on 2011-08-10
576
$dbi = DBIx::Custom->connect;
577
eval { $dbi->execute('drop table table1') };
578
$dbi->execute($create_table1_type);
579
$dbi->type_rule(
580
    into1 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
581
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
582
    },
583
    into2 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
584
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
585
    },
586
    from1 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
587
        $date_datatype => sub { my $v = shift || ''; $v =~ s/(3|5)/6/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
588
    },
589
    from2 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
590
        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/7/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
591
    }
592
);
593
$dbi->insert({key1 => '2010-01-03'}, table => 'table1', type_rule1_off => 1);
594
$result = $dbi->select(table => 'table1');
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
595
like($result->type_rule1_off->fetch_first->[0], qr/^2010-01-05/);
test cleanup
Yuki Kimoto authored on 2011-08-10
596
$result = $dbi->select(table => 'table1');
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
597
like($result->type_rule1_on->fetch_first->[0], qr/^2010-01-07/);
test cleanup
Yuki Kimoto authored on 2011-08-10
598

            
test cleanup
Yuki Kimoto authored on 2011-08-10
599
$dbi = DBIx::Custom->connect;
600
eval { $dbi->execute('drop table table1') };
601
$dbi->execute($create_table1_type);
602
$dbi->type_rule(
603
    into1 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
604
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
605
    },
606
    into2 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
607
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
608
    },
609
    from1 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
610
        $date_datatype => sub { my $v = shift || ''; $v =~ s/5/6/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
611
    },
612
    from2 => {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
613
        $date_datatype => sub { my $v = shift || ''; $v =~ s/(3|6)/7/; return $v }
test cleanup
Yuki Kimoto authored on 2011-08-10
614
    }
615
);
616
$dbi->insert({key1 => '2010-01-03'}, table => 'table1', type_rule2_off => 1);
617
$result = $dbi->select(table => 'table1');
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
618
like($result->type_rule2_off->fetch_first->[0], qr/^2010-01-06/);
test cleanup
Yuki Kimoto authored on 2011-08-10
619
$result = $dbi->select(table => 'table1');
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
620
like($result->type_rule2_on->fetch_first->[0], qr/^2010-01-07/);
test cleanup
Yuki Kimoto authored on 2011-08-10
621

            
622

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

            
624
# Create table
test cleanup
Yuki Kimoto authored on 2011-08-10
625
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
626
eval { $dbi->execute('drop table table1') };
added common test executing ...
Yuki Kimoto authored on 2011-08-07
627
$dbi->execute($create_table1);
628
$model = $dbi->create_model(table => 'table1');
629
$model->insert({key1 => 1, key2 => 2});
630
is_deeply($model->select->all, [{key1 => 1, key2 => 2}]);
631

            
cleanup test
Yuki Kimoto authored on 2011-08-08
632
test 'DBIx::Custom::Result test';
633
$dbi->delete_all(table => 'table1');
634
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
635
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
636
$source = "select key1, key2 from table1";
637
$query = $dbi->create_query($source);
638
$result = $dbi->execute($query);
639

            
640
@rows = ();
641
while (my $row = $result->fetch) {
642
    push @rows, [@$row];
643
}
644
is_deeply(\@rows, [[1, 2], [3, 4]], "fetch");
645

            
646
$result = $dbi->execute($query);
647
@rows = ();
648
while (my $row = $result->fetch_hash) {
649
    push @rows, {%$row};
650
}
651
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "fetch_hash");
652

            
653
$result = $dbi->execute($query);
654
$rows = $result->fetch_all;
655
is_deeply($rows, [[1, 2], [3, 4]], "fetch_all");
656

            
657
$result = $dbi->execute($query);
658
$rows = $result->fetch_hash_all;
659
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "all");
660

            
661
test 'Insert query return value';
662
$source = "insert into table1 {insert_param key1 key2}";
663
$query = $dbi->execute($source, {}, query => 1);
664
$ret_val = $dbi->execute($query, param => {key1 => 1, key2 => 2});
665
ok($ret_val);
666

            
667
test 'Direct query';
668
$dbi->delete_all(table => 'table1');
669
$insert_source = "insert into table1 {insert_param key1 key2}";
670
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2});
671
$result = $dbi->execute('select * from table1;');
672
$rows = $result->all;
673
is_deeply($rows, [{key1 => 1, key2 => 2}]);
674

            
675
test 'Filter basic';
676
$dbi->delete_all(table => 'table1');
677
$dbi->register_filter(twice       => sub { $_[0] * 2}, 
678
                    three_times => sub { $_[0] * 3});
679

            
680
$insert_source  = "insert into table1 {insert_param key1 key2};";
681
$insert_query = $dbi->execute($insert_source, {}, query => 1);
682
$insert_query->filter({key1 => 'twice'});
683
$dbi->execute($insert_query, param => {key1 => 1, key2 => 2});
684
$result = $dbi->execute('select * from table1;');
685
$rows = $result->filter({key2 => 'three_times'})->all;
686
is_deeply($rows, [{key1 => 2, key2 => 6}], "filter fetch_filter");
687

            
688
test 'Filter in';
689
$dbi->delete_all(table => 'table1');
690
$insert_source  = "insert into table1 {insert_param key1 key2};";
691
$insert_query = $dbi->execute($insert_source, {}, query => 1);
692
$dbi->execute($insert_query, param => {key1 => 2, key2 => 4});
693
$select_source = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}";
694
$select_query = $dbi->execute($select_source,{}, query => 1);
695
$select_query->filter({'table1.key1' => 'twice'});
696
$result = $dbi->execute($select_query, param => {'table1.key1' => [1,5], 'table1.key2' => [2,4]});
697
$rows = $result->all;
698
is_deeply($rows, [{key1 => 2, key2 => 4}], "filter");
699

            
cleanup test
Yuki Kimoto authored on 2011-08-08
700
test 'DBIx::Custom::SQLTemplate basic tag';
701
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-08
702
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-08
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

            
706
$source = "select * from table1 where key1 = :key1 and {<> key2} and {< key3} and {> key4} and {>= key5};";
707
$query = $dbi->execute($source, {}, query => 1);
708
$result = $dbi->execute($query, param => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
709
$rows = $result->all;
710
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1");
711

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

            
718
$source = "select * from table1 where {<= key1} and {like key2};";
719
$query = $dbi->execute($source, {}, query => 1);
720
$result = $dbi->execute($query, param => {key1 => 1, key2 => '%2%'});
721
$rows = $result->all;
722
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag2");
723

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

            
730
$source = "select * from table1 where {in key1 2};";
731
$query = $dbi->execute($source, {}, query => 1);
732
$result = $dbi->execute($query, param => {key1 => [9, 1]});
733
$rows = $result->all;
734
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic");
735

            
736
test 'DBIx::Custom::SQLTemplate insert tag';
737
$dbi->delete_all(table => 'table1');
738
$insert_source = 'insert into table1 {insert_param key1 key2 key3 key4 key5}';
739
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
740

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

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

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

            
754
$result = $dbi->execute('select * from table1 order by key1;');
755
$rows = $result->all;
756
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5},
757
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "basic");
758

            
cleanup test
Yuki Kimoto authored on 2011-08-08
759
test 'Named placeholder';
760
$dbi->execute('drop table table1');
761
$dbi->execute($create_table1_2);
762
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
763
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
764

            
765
$source = "select * from table1 where key1 = :key1 and key2 = :key2";
766
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
767
$rows = $result->all;
768
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
769

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

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

            
780
$source = "select * from table1 where key1 = :table1.key1 and key2 = :table1.key2";
781
$result = $dbi->execute(
782
    $source,
783
    param => {'table1.key1' => 1, 'table1.key2' => 1},
784
    filter => {'table1.key2' => sub { $_[0] * 2 }}
785
);
786
$rows = $result->all;
787
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
788

            
789
$dbi->execute('drop table table1');
790
$dbi->execute($create_table1);
791
$dbi->insert(table => 'table1', param => {key1 => '2011-10-14 12:19:18', key2 => 2});
792
$source = "select * from table1 where key1 = '2011-10-14 12:19:18' and key2 = :key2";
793
$result = $dbi->execute(
794
    $source,
795
    param => {'key2' => 2},
796
);
797

            
798
$rows = $result->all;
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
799
like($rows->[0]->{key1}, qr/2011-10-14 12:19:18/);
800
is($rows->[0]->{key2}, 2);
cleanup test
Yuki Kimoto authored on 2011-08-08
801

            
802
$dbi->delete_all(table => 'table1');
803
$dbi->insert(table => 'table1', param => {key1 => 'a:b c:d', key2 => 2});
804
$source = "select * from table1 where key1 = 'a\\:b c\\:d' and key2 = :key2";
805
$result = $dbi->execute(
806
    $source,
807
    param => {'key2' => 2},
808
);
809
$rows = $result->all;
810
is_deeply($rows, [{key1 => 'a:b c:d', key2 => 2}]);
811

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
819
test 'insert';
cleanup test
Yuki Kimoto authored on 2011-08-10
820
eval { $dbi->execute('drop table table1') };
821
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
822
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
823
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
824
$result = $dbi->execute('select * from table1;');
825
$rows   = $result->all;
826
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
827

            
828
$dbi->execute('delete from table1');
829
$dbi->register_filter(
830
    twice       => sub { $_[0] * 2 },
831
    three_times => sub { $_[0] * 3 }
832
);
833
$dbi->default_bind_filter('twice');
834
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => 'three_times'});
835
$result = $dbi->execute('select * from table1;');
836
$rows   = $result->all;
837
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter");
838
$dbi->default_bind_filter(undef);
839

            
cleanup test
Yuki Kimoto authored on 2011-08-10
840
$dbi->execute('drop table table1');
841
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
842
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, append => '   ');
843
$rows = $dbi->select(table => 'table1')->all;
844
is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append');
845

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

            
849
eval{$dbi->insert(table => 'table', param => {';' => 1})};
850
like($@, qr/safety/);
851

            
cleanup test
Yuki Kimoto authored on 2011-08-10
852
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
853
$dbi->execute($create_table_reserved);
cleanup test
Yuki Kimoto authored on 2011-08-10
854
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
855
$dbi->insert(table => 'table', param => {select => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
856
$result = $dbi->execute("select * from ${q}table$p");
cleanup test
Yuki Kimoto authored on 2011-08-10
857
$rows   = $result->all;
test cleanup
Yuki Kimoto authored on 2011-08-10
858
is_deeply($rows, [{select => 2, update => undef}], "reserved word");
cleanup test
Yuki Kimoto authored on 2011-08-10
859

            
cleanup test
Yuki Kimoto authored on 2011-08-10
860
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
861
$dbi->execute($create_table1);
862
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
863
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
864
$result = $dbi->execute('select * from table1;');
865
$rows   = $result->all;
866
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
867

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

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

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

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

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

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

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

            
925
eval { $dbi->execute('drop table table1') };
926
$dbi->execute($create_table1);
927
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
928
$where = $dbi->where;
929
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
930
$where->param({key1 => 1, key2 => 2});
931
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
932
$result = $dbi->select(table => 'table1');
933
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
934

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

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

            
959
eval{$dbi->update(table => 'table1', param => {';' => 1})};
960
like($@, qr/safety/);
961

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

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

            
976
eval {$dbi->update_all(table => 'table', param => {';' => 2}) };
977
like($@, qr/safety/);
978

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

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

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

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

            
1024

            
1025
test 'delete';
1026
eval { $dbi->execute('drop table table1') };
1027
$dbi->execute($create_table1);
1028
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1029
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1030
$dbi->delete(table => 'table1', where => {key1 => 1});
1031
$result = $dbi->execute('select * from table1;');
1032
$rows   = $result->all;
1033
is_deeply($rows, [{key1 => 3, key2 => 4}], "basic");
1034

            
1035
$dbi->execute("delete from table1;");
1036
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1037
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1038
$dbi->register_filter(twice => sub { $_[0] * 2 });
1039
$dbi->delete(table => 'table1', where => {key2 => 1}, filter => {key2 => 'twice'});
1040
$result = $dbi->execute('select * from table1;');
1041
$rows   = $result->all;
1042
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter");
1043

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

            
1046
$dbi->delete_all(table => 'table1');
1047
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1048
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1049
$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2});
1050
$rows = $dbi->select(table => 'table1')->all;
1051
is_deeply($rows, [{key1 => 3, key2 => 4}], "delete multi key");
1052

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

            
1056
eval { $dbi->execute('drop table table1') };
1057
$dbi->execute($create_table1);
1058
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1059
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1060
$where = $dbi->where;
1061
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
1062
$where->param({ke1 => 1, key2 => 2});
1063
$dbi->delete(table => 'table1', where => $where);
1064
$result = $dbi->select(table => 'table1');
1065
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
1066

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

            
1081
eval { $dbi->execute('drop table table1') };
test cleanup
Yuki Kimoto authored on 2011-08-10
1082
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1083
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1084
$dbi->delete(table => 'table1', where => {key1 => 1}, prefix => '    ');
1085
$result = $dbi->execute('select * from table1;');
1086
$rows   = $result->all;
1087
is_deeply($rows, [], "basic");
1088

            
1089
test 'delete error';
1090
eval { $dbi->execute('drop table table1') };
1091
$dbi->execute($create_table1);
1092
eval{$dbi->delete(table => 'table1')};
1093
like($@, qr/"where" must be specified/,
1094
         "where key-value pairs not specified");
1095

            
1096
eval{$dbi->delete(table => 'table1', where => {';' => 1})};
1097
like($@, qr/safety/);
1098

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

            
1110
test 'delete_all';
1111
eval { $dbi->execute('drop table table1') };
1112
$dbi->execute($create_table1);
1113
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1114
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1115
$dbi->delete_all(table => 'table1');
1116
$result = $dbi->execute('select * from table1;');
1117
$rows   = $result->all;
1118
is_deeply($rows, [], "basic");
1119

            
1120

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

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

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

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

            
1139
$dbi->register_filter(decrement => sub { $_[0] - 1 });
1140
$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'})
1141
            ->all;
1142
is_deeply($rows, [{key1 => 1, key2 => 2}], "filter");
1143

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

            
1155
$rows = $dbi->select(
1156
    table => [qw/table1 table2/],
1157
    column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
1158
    relation  => {'table1.key1' => 'table2.key1'}
1159
)->all;
1160
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : no exists where");
1161

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

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

            
1174
test 'fetch filter';
1175
eval { $dbi->execute('drop table table1') };
1176
$dbi->register_filter(
1177
    twice       => sub { $_[0] * 2 },
1178
    three_times => sub { $_[0] * 3 }
1179
);
1180
$dbi->default_fetch_filter('twice');
1181
$dbi->execute($create_table1);
1182
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1183
$result = $dbi->select(table => 'table1');
1184
$result->filter({key1 => 'three_times'});
1185
$row = $result->one;
1186
is_deeply($row, {key1 => 3, key2 => 4}, "default_fetch_filter and filter");
1187

            
1188
test 'filters';
1189
$dbi = DBIx::Custom->new;
1190

            
1191
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')),
1192
   'あ', "decode_utf8");
1193

            
1194
is($dbi->filters->{encode_utf8}->('あ'),
1195
   encode_utf8('あ'), "encode_utf8");
1196

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1197
test 'transaction1';
test cleanup
Yuki Kimoto authored on 2011-08-10
1198
$dbi = DBIx::Custom->connect;
1199
eval { $dbi->execute('drop table table1') };
1200
$dbi->execute($create_table1);
1201
$dbi->dbh->begin_work;
1202
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1203
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
1204
$dbi->dbh->commit;
1205
$result = $dbi->select(table => 'table1');
1206
is_deeply(scalar $result->all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}],
1207
          "commit");
1208

            
1209
$dbi = DBIx::Custom->connect;
1210
eval { $dbi->execute('drop table table1') };
1211
$dbi->execute($create_table1);
1212
$dbi->dbh->begin_work(0);
1213
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1214
$dbi->dbh->rollback;
1215

            
1216
$result = $dbi->select(table => 'table1');
1217
ok(! $result->fetch_first, "rollback");
1218

            
1219
test 'execute';
1220
eval { $dbi->execute('drop table table1') };
1221
$dbi->execute($create_table1);
1222
{
1223
    local $Carp::Verbose = 0;
1224
    eval{$dbi->execute('select * frm table1')};
1225
    like($@, qr/\Qselect * frm table1;/, "fail prepare");
1226
    like($@, qr/\.t /, "fail : not verbose");
1227
}
1228
{
1229
    local $Carp::Verbose = 1;
1230
    eval{$dbi->execute('select * frm table1')};
1231
    like($@, qr/Custom.*\.t /s, "fail : verbose");
1232
}
1233

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

            
1237
$query = $dbi->execute('select * from table1 where key1 = :key1', {}, query => 1);
1238
$dbi->dbh->disconnect;
1239
eval{$dbi->execute($query, param => {key1 => {a => 1}})};
1240
ok($@, "execute fail");
1241

            
1242
{
1243
    local $Carp::Verbose = 0;
1244
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
1245
    like($@, qr/\Q.t /, "caller spec : not vebose");
1246
}
1247
{
1248
    local $Carp::Verbose = 1;
1249
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
1250
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
1251
}
1252

            
1253

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1254
test 'transaction2';
test cleanup
Yuki Kimoto authored on 2011-08-10
1255
$dbi = DBIx::Custom->connect;
1256
eval { $dbi->execute('drop table table1') };
1257
$dbi->execute($create_table1);
1258

            
1259
$dbi->begin_work;
1260

            
1261
eval {
1262
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1263
    die "Error";
1264
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1265
};
1266

            
1267
$dbi->rollback if $@;
1268

            
1269
$result = $dbi->select(table => 'table1');
1270
$rows = $result->all;
1271
is_deeply($rows, [], "rollback");
1272

            
1273
$dbi->begin_work;
1274

            
1275
eval {
1276
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1277
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1278
};
1279

            
1280
$dbi->commit unless $@;
1281

            
1282
$result = $dbi->select(table => 'table1');
1283
$rows = $result->all;
1284
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "commit");
1285

            
1286
$dbi->dbh->{AutoCommit} = 0;
1287
eval{ $dbi->begin_work };
1288
ok($@, "exception");
1289
$dbi->dbh->{AutoCommit} = 1;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
1290

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1291
test 'cache';
1292
eval { $dbi->execute('drop table table1') };
1293
$dbi->cache(1);
1294
$dbi->execute($create_table1);
1295
$source = 'select * from table1 where key1 = :key1 and key2 = :key2;';
1296
$dbi->execute($source, {}, query => 1);
1297
is_deeply($dbi->{_cached}->{$source}, 
1298
          {sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2'], tables => []}, "cache");
1299

            
1300
eval { $dbi->execute('drop table table1') };
1301
$dbi->execute($create_table1);
1302
$dbi->{_cached} = {};
1303
$dbi->cache(0);
1304
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1305
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
1306

            
1307
test 'execute';
1308
eval { $dbi->execute('drop table table1') };
1309
$dbi->execute($create_table1);
1310
{
1311
    local $Carp::Verbose = 0;
1312
    eval{$dbi->execute('select * frm table1')};
1313
    like($@, qr/\Qselect * frm table1;/, "fail prepare");
1314
    like($@, qr/\.t /, "fail : not verbose");
1315
}
1316
{
1317
    local $Carp::Verbose = 1;
1318
    eval{$dbi->execute('select * frm table1')};
1319
    like($@, qr/Custom.*\.t /s, "fail : verbose");
1320
}
1321

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

            
1325
$query = $dbi->execute('select * from table1 where key1 = :key1', {}, query => 1);
1326
$dbi->dbh->disconnect;
1327
eval{$dbi->execute($query, param => {key1 => {a => 1}})};
1328
ok($@, "execute fail");
1329

            
1330
{
1331
    local $Carp::Verbose = 0;
1332
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
1333
    like($@, qr/\Q.t /, "caller spec : not vebose");
1334
}
1335
{
1336
    local $Carp::Verbose = 1;
1337
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
1338
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
1339
}
1340

            
1341
test 'method';
1342
$dbi->method(
1343
    one => sub { 1 }
1344
);
1345
$dbi->method(
1346
    two => sub { 2 }
1347
);
1348
$dbi->method({
1349
    twice => sub {
1350
        my $self = shift;
1351
        return $_[0] * 2;
1352
    }
1353
});
1354

            
1355
is($dbi->one, 1, "first");
1356
is($dbi->two, 2, "second");
1357
is($dbi->twice(5), 10 , "second");
1358

            
1359
eval {$dbi->XXXXXX};
1360
ok($@, "not exists");
1361

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

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

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

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

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

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

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

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

            
1481
$result->filter({'key2' => 'twice'});
1482
$rows   = $result->all;
1483
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join");
1484

            
1485
$result = $dbi->select(
1486
     table => ['table1', 'table2'],
1487
     column => ['key2', 'key3'],
1488
     where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
1489

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

            
1494
test 'each_column';
1495
$dbi = DBIx::Custom->connect;
1496
eval { $dbi->execute("drop table ${q}table$p") };
1497
eval { $dbi->execute('drop table table1') };
1498
eval { $dbi->execute('drop table table2') };
test cleranup
Yuki Kimoto authored on 2011-08-10
1499
eval { $dbi->execute('drop table table3') };
test cleanup
Yuki Kimoto authored on 2011-08-10
1500
$dbi->execute($create_table1_type);
1501
$dbi->execute($create_table2);
1502

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

            
1529
$infos = [];
1530
$dbi->each_table(sub {
1531
    my ($self, $table, $table_info) = @_;
1532
    
1533
    if ($table =~ /^table\d/) {
1534
         my $info = [$table, $table_info->{TABLE_NAME}];
1535
         push @$infos, $info;
1536
    }
1537
});
1538
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
1539
is_deeply($infos, 
1540
    [
1541
        ['table1', 'table1'],
1542
        ['table2', 'table2'],
1543
    ]
1544
);
1545

            
1546
test 'connect super';
test cleanup
Yuki Kimoto authored on 2011-08-10
1547
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1548
eval { $dbi->execute('drop table table1') };
1549
$dbi->execute($create_table1);
1550
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1551
is($dbi->select(table => 'table1')->one->{key1}, 1);
1552

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

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

            
1566
test 'end_filter';
1567
$dbi = DBIx::Custom->connect;
1568
eval { $dbi->execute('drop table table1') };
1569
$dbi->execute($create_table1);
1570
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1571
$result = $dbi->select(table => 'table1');
1572
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1573
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 });
1574
$row = $result->fetch_first;
1575
is_deeply($row, [6, 40]);
1576

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

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

            
1597
$dbi->register_filter(five_times => sub { $_[0] * 5 });
1598
$result = $dbi->select(table => 'table1');
1599
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1600
$result->end_filter({key1 => sub { $_[0] * 3 }, key2 => 'five_times' });
1601
$row = $result->one;
1602
is_deeply($row, {key1 => 6, key2 => 40});
1603

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

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

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

            
1640
test 'empty where select';
1641
$dbi = DBIx::Custom->connect;
1642
eval { $dbi->execute('drop table table1') };
1643
$dbi->execute($create_table1);
1644
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1645
$result = $dbi->select(table => 'table1', where => {});
1646
$row = $result->one;
1647
is_deeply($row, {key1 => 1, key2 => 2});
1648

            
1649
test 'select query option';
1650
$dbi = DBIx::Custom->connect;
1651
eval { $dbi->execute('drop table table1') };
1652
$dbi->execute($create_table1);
1653
$query = $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, query => 1);
1654
is(ref $query, 'DBIx::Custom::Query');
1655
$query = $dbi->update(table => 'table1', where => {key1 => 1}, param => {key2 => 2}, query => 1);
1656
is(ref $query, 'DBIx::Custom::Query');
1657
$query = $dbi->delete(table => 'table1', where => {key1 => 1}, query => 1);
1658
is(ref $query, 'DBIx::Custom::Query');
1659
$query = $dbi->select(table => 'table1', where => {key1 => 1, key2 => 2}, query => 1);
1660
is(ref $query, 'DBIx::Custom::Query');
1661

            
1662
test 'where';
1663
$dbi = DBIx::Custom->connect;
1664
eval { $dbi->execute('drop table table1') };
1665
$dbi->execute($create_table1);
1666
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1667
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1668
$where = $dbi->where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
1669
is("$where", "where ( key1 = :key1 and key2 = :key2 )", 'no param');
1670

            
1671
$where = $dbi->where
1672
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
1673
             ->param({key1 => 1});
1674

            
1675
$result = $dbi->select(
1676
    table => 'table1',
1677
    where => $where
1678
);
1679
$row = $result->all;
1680
is_deeply($row, [{key1 => 1, key2 => 2}]);
1681

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

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

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

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

            
1722
$where = $dbi->where;
1723
$result = $dbi->select(
1724
    table => 'table1',
1725
    where => $where
1726
);
1727
$row = $result->all;
1728
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1729

            
1730
eval {
1731
$where = $dbi->where
1732
             ->clause(['uuu']);
1733
$result = $dbi->select(
1734
    table => 'table1',
1735
    where => $where
1736
);
1737
};
1738
ok($@);
1739

            
1740
$where = $dbi->where;
1741
is("$where", '');
1742

            
1743
$where = $dbi->where
1744
             ->clause(['or', ('key1 = :key1') x 2])
1745
             ->param({key1 => [1, 3]});
1746
$result = $dbi->select(
1747
    table => 'table1',
1748
    where => $where,
1749
);
1750
$row = $result->all;
1751
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1752

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

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

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

            
1783
$where = $dbi->where
1784
             ->clause('key1 = :key1 key2 = :key2')
1785
             ->param({key1 => 1});
1786
eval{$where->to_string};
1787
like($@, qr/one column/);
1788

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

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

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

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

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

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

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

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

            
1869
$where = $dbi->where
1870
             ->clause(['and', '{> key1}', '{< key1}' ])
1871
             ->param({key1 => [2, $dbi->not_exists]});
1872
$result = $dbi->select(
1873
    table => 'table1',
1874
    where => $where,
1875
);
1876
$row = $result->all;
1877
is_deeply($row, [{key1 => 3, key2 => 4}], 'not_exists');
1878

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

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

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

            
1909
$where = $dbi->where
1910
             ->clause(['and', 'key1 is not null', 'key2 is not null' ]);
1911
$result = $dbi->select(
1912
    table => 'table1',
1913
    where => $where,
1914
);
1915
$row = $result->all;
1916
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1917

            
1918
eval {$dbi->where(ppp => 1) };
1919
like($@, qr/invalid/);
1920

            
1921
$where = $dbi->where(
1922
    clause => ['and', ['or'], ['and', 'key1 = :key1', 'key2 = :key2']],
1923
    param => {key1 => 1, key2 => 2}
1924
);
1925
$result = $dbi->select(
1926
    table => 'table1',
1927
    where => $where,
1928
);
1929
$row = $result->all;
1930
is_deeply($row, [{key1 => 1, key2 => 2}]);
1931

            
1932

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

            
1944
$where = $dbi->where;
1945
$where->clause(['and', ':key1{=}']);
1946
$where->param({key1 => undef});
1947
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1948
$row = $result->all;
1949
is_deeply($row, [{key1 => 1, key2 => 2}]);
1950

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

            
1960
$where = $dbi->where;
1961
$where->clause(['or', ':key1{=}', ':key1{=}']);
1962
$where->param({key1 => [undef, undef]});
1963
$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]});
1964
$row = $result->all;
1965
is_deeply($row, [{key1 => 1, key2 => 2}]);
1966
$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]});
1967
$row = $result->all;
1968
is_deeply($row, [{key1 => 1, key2 => 2}]);
1969

            
1970
$where = $dbi->where;
1971
$where->clause(['and', ':key1{=}']);
1972
$where->param({key1 => [undef, undef]});
1973
$where->if('defined');
1974
$where->map;
1975
$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]});
1976
$row = $result->all;
1977
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1978
$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]});
1979
$row = $result->all;
1980
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1981

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

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

            
2000
$where = $dbi->where;
2001
$where->clause(['and', ':key1{=}']);
2002
$where->param({key1 => 5});
2003
$where->if(sub { ($_[0] || '') eq 5 });
2004
$where->map;
2005
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
2006
$row = $result->all;
2007
is_deeply($row, [{key1 => 1, key2 => 2}]);
2008

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

            
2018
$where = $dbi->where;
2019
$where->param({id => 1, author => 'Ken', price => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
2020
$where->map(id => 'table1.id',
2021
    author => ['table1.author', sub { '%' . $_[0] . '%' }],
2022
    price => ['table1.price', {if => sub { $_[0] eq 1900 }}]
test cleanup
Yuki Kimoto authored on 2011-08-10
2023
);
test cleanup
Yuki Kimoto authored on 2011-08-10
2024
is_deeply($where->param, {'table1.id' => 1, 'table1.author' => '%Ken%',
2025
  'table1.price' => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
2026

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

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

            
2048
$where = $dbi->where;
2049
$where->param({id => undef, author => undef, price => undef});
2050
$where->if('length');
2051
$where->map(
test cleanup
Yuki Kimoto authored on 2011-08-10
2052
    id => 'table1.id',
2053
    price => ['table1.price', {if => 'exists'}]
test cleanup
Yuki Kimoto authored on 2011-08-10
2054
);
test cleanup
Yuki Kimoto authored on 2011-08-10
2055
is_deeply($where->param, {'table1.price' => undef});
test cleanup
Yuki Kimoto authored on 2011-08-10
2056

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

            
2066
$where = $dbi->where;
2067
$where->param({id => [1, 2], author => 'Ken', price => 1900});
2068
$where->map(
test cleanup
Yuki Kimoto authored on 2011-08-10
2069
    id => 'table1.id',
2070
    author => ['table1.author', sub { '%' . $_[0] . '%' }],
2071
    price => ['table1.price', {if => sub { $_[0] eq 1900 }}]
test cleanup
Yuki Kimoto authored on 2011-08-10
2072
);
test cleanup
Yuki Kimoto authored on 2011-08-10
2073
is_deeply($where->param, {'table1.id' => [1, 2], 'table1.author' => '%Ken%',
2074
  'table1.price' => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
2075

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

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

            
2097
test 'dbi_option default';
2098
$dbi = DBIx::Custom->new;
2099
is_deeply($dbi->dbi_option, {});
2100

            
2101
test 'register_tag_processor';
2102
$dbi = DBIx::Custom->connect;
2103
$dbi->register_tag_processor(
2104
    a => sub { 1 }
2105
);
test cleanup
Yuki Kimoto authored on 2011-08-10
2106
is($dbi->{_tags}->{a}->(), 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2107

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

            
2115
test 'table not specify exception';
2116
$dbi = DBIx::Custom->connect;
2117
eval {$dbi->insert};
2118
like($@, qr/table/);
2119
eval {$dbi->update};
2120
like($@, qr/table/);
2121
eval {$dbi->delete};
2122
like($@, qr/table/);
2123
eval {$dbi->select};
2124
like($@, qr/table/);
test cleanup
Yuki Kimoto authored on 2011-08-10
2125

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2126
test 'more tests';
2127
$dbi = DBIx::Custom->connect;
2128
eval{$dbi->apply_filter('table', 'column', [])};
2129
like($@, qr/apply_filter/);
2130

            
2131
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
2132
like($@, qr/apply_filter/);
2133

            
2134
$dbi->apply_filter(
2135

            
2136
);
2137
$dbi = DBIx::Custom->connect;
2138
eval { $dbi->execute('drop table table1') };
2139
$dbi->execute($create_table1);
2140
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2141
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
2142
$dbi->apply_filter('table1', 'key2', 
2143
                   {in => sub { $_[0] * 3 }, out => sub { $_[0] * 2 }});
2144
$rows = $dbi->select(table => 'table1', where => {key2 => 1})->all;
2145
is_deeply($rows, [{key1 => 1, key2 => 6}]);
2146

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

            
2156
$dbi = DBIx::Custom->connect;
2157
eval {$dbi->apply_filter('table1', 'key2', {out => 'no'})};
2158
like($@, qr/not registered/);
2159
eval {$dbi->apply_filter('table1', 'key2', {in => 'no'})};
2160
like($@, qr/not registered/);
2161
$dbi->method({one => sub { 1 }});
2162
is($dbi->one, 1);
2163

            
2164
eval{DBIx::Custom->connect(dsn => undef)};
2165
like($@, qr/_connect/);
2166

            
2167
$dbi = DBIx::Custom->connect;
2168
eval { $dbi->execute('drop table table1') };
2169
$dbi->execute($create_table1);
2170
$dbi->register_filter(twice => sub { $_[0] * 2 });
2171
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
2172
             filter => {key1 => 'twice'});
2173
$row = $dbi->select(table => 'table1')->one;
2174
is_deeply($row, {key1 => 2, key2 => 2});
2175
eval {$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
2176
             filter => {key1 => 'no'}) };
2177
like($@, qr//);
2178

            
2179
$dbi->register_filter(one => sub { });
2180
$dbi->default_fetch_filter('one');
2181
ok($dbi->default_fetch_filter);
2182
$dbi->default_bind_filter('one');
2183
ok($dbi->default_bind_filter);
2184
eval{$dbi->default_fetch_filter('no')};
2185
like($@, qr/not registered/);
2186
eval{$dbi->default_bind_filter('no')};
2187
like($@, qr/not registered/);
2188
$dbi->default_bind_filter(undef);
2189
ok(!defined $dbi->default_bind_filter);
2190
$dbi->default_fetch_filter(undef);
2191
ok(!defined $dbi->default_fetch_filter);
2192
eval {$dbi->execute('select * from table1 {} {= author') };
2193
like($@, qr/Tag not finished/);
2194

            
2195
$dbi = DBIx::Custom->connect;
2196
eval { $dbi->execute('drop table table1') };
2197
$dbi->execute($create_table1);
2198
$dbi->register_filter(one => sub { 1 });
2199
$result = $dbi->select(table => 'table1');
2200
eval {$result->filter(key1 => 'no')};
2201
like($@, qr/not registered/);
2202
eval {$result->end_filter(key1 => 'no')};
2203
like($@, qr/not registered/);
2204
$result->default_filter(undef);
2205
ok(!defined $result->default_filter);
2206
$result->default_filter('one');
2207
is($result->default_filter->(), 1);
2208

            
2209
test 'dbi_option';
2210
$dbi = DBIx::Custom->connect(dbi_option => {PrintError => 1});
2211
ok($dbi->dbh->{PrintError});
2212
$dbi = DBIx::Custom->connect(dbi_options => {PrintError => 1});
2213
ok($dbi->dbh->{PrintError});
2214

            
2215
test 'DBIx::Custom::Result stash()';
2216
$result = DBIx::Custom::Result->new;
2217
is_deeply($result->stash, {}, 'default');
2218
$result->stash->{foo} = 1;
2219
is($result->stash->{foo}, 1, 'get and set');
test cleanup
Yuki Kimoto authored on 2011-08-10
2220

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2221
test 'delete_at';
2222
$dbi = DBIx::Custom->connect;
2223
eval { $dbi->execute('drop table table1') };
2224
$dbi->execute($create_table1_2);
2225
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2226
$dbi->delete_at(
2227
    table => 'table1',
2228
    primary_key => ['key1', 'key2'],
2229
    where => [1, 2],
2230
);
2231
is_deeply($dbi->select(table => 'table1')->all, []);
2232

            
2233
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2234
$dbi->delete_at(
2235
    table => 'table1',
2236
    primary_key => 'key1',
2237
    where => 1,
2238
);
2239
is_deeply($dbi->select(table => 'table1')->all, []);
2240

            
2241
test 'insert_at';
2242
$dbi = DBIx::Custom->connect;
2243
eval { $dbi->execute('drop table table1') };
2244
$dbi->execute($create_table1_2);
2245
$dbi->insert_at(
2246
    primary_key => ['key1', 'key2'], 
2247
    table => 'table1',
2248
    where => [1, 2],
2249
    param => {key3 => 3}
2250
);
2251
is($dbi->select(table => 'table1')->one->{key1}, 1);
2252
is($dbi->select(table => 'table1')->one->{key2}, 2);
2253
is($dbi->select(table => 'table1')->one->{key3}, 3);
2254

            
2255
$dbi->delete_all(table => 'table1');
2256
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2257
$dbi->insert_at(
2258
    primary_key => 'key1', 
2259
    table => 'table1',
2260
    where => 1,
2261
    param => {key2 => 2, key3 => 3}
2262
);
2263

            
2264
is($dbi->select(table => 'table1')->one->{key1}, 1);
2265
is($dbi->select(table => 'table1')->one->{key2}, 2);
2266
is($dbi->select(table => 'table1')->one->{key3}, 3);
2267

            
2268
eval {
2269
    $dbi->insert_at(
2270
        table => 'table1',
2271
        primary_key => ['key1', 'key2'],
2272
        where => {},
2273
        param => {key1 => 1, key2 => 2, key3 => 3},
2274
    );
2275
};
2276
like($@, qr/must be/);
2277

            
2278
$dbi = DBIx::Custom->connect;
2279
eval { $dbi->execute('drop table table1') };
2280
$dbi->execute($create_table1_2);
2281
$dbi->insert_at(
2282
    {key3 => 3},
2283
    primary_key => ['key1', 'key2'], 
2284
    table => 'table1',
2285
    where => [1, 2],
2286
);
2287
is($dbi->select(table => 'table1')->one->{key1}, 1);
2288
is($dbi->select(table => 'table1')->one->{key2}, 2);
2289
is($dbi->select(table => 'table1')->one->{key3}, 3);
2290

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

            
2306
$dbi->delete_all(table => 'table1');
2307
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2308
$dbi->update_at(
2309
    table => 'table1',
2310
    primary_key => 'key1',
2311
    where => 1,
2312
    param => {key3 => 4}
2313
);
2314
is($dbi->select(table => 'table1')->one->{key1}, 1);
2315
is($dbi->select(table => 'table1')->one->{key2}, 2);
2316
is($dbi->select(table => 'table1')->one->{key3}, 4);
2317

            
2318
$dbi = DBIx::Custom->connect;
2319
eval { $dbi->execute('drop table table1') };
2320
$dbi->execute($create_table1_2);
2321
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2322
$dbi->update_at(
2323
    {key3 => 4},
2324
    table => 'table1',
2325
    primary_key => ['key1', 'key2'],
2326
    where => [1, 2]
2327
);
2328
is($dbi->select(table => 'table1')->one->{key1}, 1);
2329
is($dbi->select(table => 'table1')->one->{key2}, 2);
2330
is($dbi->select(table => 'table1')->one->{key3}, 4);
2331

            
2332
test 'select_at';
2333
$dbi = DBIx::Custom->connect;
2334
eval { $dbi->execute('drop table table1') };
2335
$dbi->execute($create_table1_2);
2336
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2337
$result = $dbi->select_at(
2338
    table => 'table1',
2339
    primary_key => ['key1', 'key2'],
2340
    where => [1, 2]
2341
);
2342
$row = $result->one;
2343
is($row->{key1}, 1);
2344
is($row->{key2}, 2);
2345
is($row->{key3}, 3);
2346

            
2347
$dbi->delete_all(table => 'table1');
2348
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2349
$result = $dbi->select_at(
2350
    table => 'table1',
2351
    primary_key => 'key1',
2352
    where => 1,
2353
);
2354
$row = $result->one;
2355
is($row->{key1}, 1);
2356
is($row->{key2}, 2);
2357
is($row->{key3}, 3);
2358

            
2359
$dbi->delete_all(table => 'table1');
2360
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2361
$result = $dbi->select_at(
2362
    table => 'table1',
2363
    primary_key => ['key1', 'key2'],
2364
    where => [1, 2]
2365
);
2366
$row = $result->one;
2367
is($row->{key1}, 1);
2368
is($row->{key2}, 2);
2369
is($row->{key3}, 3);
2370

            
2371
eval {
2372
    $result = $dbi->select_at(
2373
        table => 'table1',
2374
        primary_key => ['key1', 'key2'],
2375
        where => {},
2376
    );
2377
};
2378
like($@, qr/must be/);
2379

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

            
2389
eval {
2390
    $result = $dbi->update_at(
2391
        table => 'table1',
2392
        primary_key => ['key1', 'key2'],
2393
        where => {},
2394
        param => {key1 => 1, key2 => 2},
2395
    );
2396
};
2397
like($@, qr/must be/);
2398

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

            
2408
test 'columns';
2409
use MyDBI1;
2410
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2411
$model = $dbi->model('table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2412

            
2413

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

            
2432
test 'model insert_at';
2433
$dbi = MyDBI6->connect;
2434
eval { $dbi->execute('drop table table1') };
2435
$dbi->execute($create_table1_2);
2436
$dbi->model('table1')->insert_at(
2437
    where => [1, 2],
2438
    param => {key3 => 3}
2439
);
2440
$result = $dbi->model('table1')->select;
2441
$row = $result->one;
2442
is($row->{key1}, 1);
2443
is($row->{key2}, 2);
2444
is($row->{key3}, 3);
2445

            
2446
test 'model update_at';
2447
$dbi = MyDBI6->connect;
2448
eval { $dbi->execute('drop table table1') };
2449
$dbi->execute($create_table1_2);
2450
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2451
$dbi->model('table1')->update_at(
2452
    where => [1, 2],
2453
    param => {key3 => 4}
2454
);
2455
$result = $dbi->model('table1')->select;
2456
$row = $result->one;
2457
is($row->{key1}, 1);
2458
is($row->{key2}, 2);
2459
is($row->{key3}, 4);
2460

            
2461
test 'model select_at';
2462
$dbi = MyDBI6->connect;
2463
eval { $dbi->execute('drop table table1') };
2464
$dbi->execute($create_table1_2);
2465
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2466
$result = $dbi->model('table1')->select_at(where => [1, 2]);
2467
$row = $result->one;
2468
is($row->{key1}, 1);
2469
is($row->{key2}, 2);
2470
is($row->{key3}, 3);
2471

            
2472

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2491
test 'insert_param';
2492
$dbi = DBIx::Custom->connect;
2493
eval { $dbi->execute('drop table table1') };
2494
$dbi->execute($create_table1_2);
2495
$param = {key1 => 1, key2 => 2};
2496
$insert_param = $dbi->insert_param($param);
2497
$sql = <<"EOS";
2498
insert into table1 $insert_param
2499
EOS
2500
$dbi->execute($sql, param => $param, table => 'table1');
2501
is($dbi->select(table => 'table1')->one->{key1}, 1);
2502
is($dbi->select(table => 'table1')->one->{key2}, 2);
2503

            
2504
$dbi = DBIx::Custom->connect;
2505
eval { $dbi->execute('drop table table1') };
2506
$dbi->execute($create_table1_2);
2507
$param = {key1 => 1, key2 => 2};
2508
$insert_param = $dbi->insert_param($param);
2509
$sql = <<"EOS";
2510
insert into table1 $insert_param
2511
EOS
2512
$dbi->execute($sql, param => $param, table => 'table1');
2513
is($dbi->select(table => 'table1')->one->{key1}, 1);
2514
is($dbi->select(table => 'table1')->one->{key2}, 2);
2515

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

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

            
2538
$result = $model->select_at(
2539
    column => [
2540
        $model->mycolumn(['key1']),
2541
        $model->column(table2 => ['key1'])
2542
    ]
2543
);
2544
is_deeply($result->one,
2545
          {key1 => 1, 'table2.key1' => 1});
2546
$result = $model->select_at(
2547
    column => [
2548
        $model->mycolumn(['key1']),
2549
        {table2 => ['key1']}
2550
    ]
2551
);
2552
is_deeply($result->one,
2553
          {key1 => 1, 'table2.key1' => 1});
2554

            
2555
$result = $model->select_at(
2556
    column => [
2557
        $model->mycolumn(['key1']),
2558
        ['table2.key1', as => 'table2.key1']
2559
    ]
2560
);
2561
is_deeply($result->one,
2562
          {key1 => 1, 'table2.key1' => 1});
2563

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2573
test 'merge_param';
2574
$dbi = DBIx::Custom->new;
2575
$params = [
2576
    {key1 => 1, key2 => 2, key3 => 3},
2577
    {key1 => 1, key2 => 2},
2578
    {key1 => 1}
2579
];
2580
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]);
2581
is_deeply($param, {key1 => [1, 1, 1], key2 => [2, 2], key3 => 3});
2582

            
2583
$params = [
2584
    {key1 => [1, 2], key2 => 1, key3 => [1, 2]},
2585
    {key1 => [3, 4], key2 => [2, 3], key3 => 3}
2586
];
2587
$param = $dbi->merge_param($params->[0], $params->[1]);
2588
is_deeply($param, {key1 => [1, 2, 3, 4], key2 => [1, 2, 3], key3 => [1, 2, 3]});
2589

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

            
2610
test 'select() string where';
2611
$dbi = DBIx::Custom->connect;
2612
eval { $dbi->execute('drop table table1') };
2613
$dbi->execute($create_table1);
2614
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2615
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2616
$rows = $dbi->select(
2617
    table => 'table1',
2618
    where => 'key1 = :key1 and key2 = :key2',
2619
    where_param => {key1 => 1, key2 => 2}
2620
)->all;
2621
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2622

            
2623
$dbi = DBIx::Custom->connect;
2624
eval { $dbi->execute('drop table table1') };
2625
$dbi->execute($create_table1);
2626
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2627
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2628
$rows = $dbi->select(
2629
    table => 'table1',
2630
    where => [
2631
        'key1 = :key1 and key2 = :key2',
2632
        {key1 => 1, key2 => 2}
2633
    ]
2634
)->all;
2635
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2636

            
2637
test 'delete() string where';
2638
$dbi = DBIx::Custom->connect;
2639
eval { $dbi->execute('drop table table1') };
2640
$dbi->execute($create_table1);
2641
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2642
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2643
$dbi->delete(
2644
    table => 'table1',
2645
    where => 'key1 = :key1 and key2 = :key2',
2646
    where_param => {key1 => 1, key2 => 2}
2647
);
2648
$rows = $dbi->select(table => 'table1')->all;
2649
is_deeply($rows, [{key1 => 2, key2 => 3}]);
2650

            
2651
$dbi = DBIx::Custom->connect;
2652
eval { $dbi->execute('drop table table1') };
2653
$dbi->execute($create_table1);
2654
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2655
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2656
$dbi->delete(
2657
    table => 'table1',
2658
    where => [
2659
        'key1 = :key1 and key2 = :key2',
2660
         {key1 => 1, key2 => 2}
2661
    ]
2662
);
2663
$rows = $dbi->select(table => 'table1')->all;
2664
is_deeply($rows, [{key1 => 2, key2 => 3}]);
2665

            
2666

            
2667
test 'update() string where';
2668
$dbi = DBIx::Custom->connect;
2669
eval { $dbi->execute('drop table table1') };
2670
$dbi->execute($create_table1);
2671
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2672
$dbi->update(
2673
    table => 'table1',
2674
    param => {key1 => 5},
2675
    where => 'key1 = :key1 and key2 = :key2',
2676
    where_param => {key1 => 1, key2 => 2}
2677
);
2678
$rows = $dbi->select(table => 'table1')->all;
2679
is_deeply($rows, [{key1 => 5, key2 => 2}]);
2680

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

            
2696
test 'insert id and primary_key option';
2697
$dbi = DBIx::Custom->connect;
2698
eval { $dbi->execute('drop table table1') };
2699
$dbi->execute($create_table1_2);
2700
$dbi->insert(
2701
    primary_key => ['key1', 'key2'], 
2702
    table => 'table1',
2703
    id => [1, 2],
2704
    param => {key3 => 3}
2705
);
2706
is($dbi->select(table => 'table1')->one->{key1}, 1);
2707
is($dbi->select(table => 'table1')->one->{key2}, 2);
2708
is($dbi->select(table => 'table1')->one->{key3}, 3);
2709

            
2710
$dbi->delete_all(table => 'table1');
2711
$dbi->insert(
2712
    primary_key => 'key1', 
2713
    table => 'table1',
2714
    id => 0,
2715
    param => {key2 => 2, key3 => 3}
2716
);
2717

            
2718
is($dbi->select(table => 'table1')->one->{key1}, 0);
2719
is($dbi->select(table => 'table1')->one->{key2}, 2);
2720
is($dbi->select(table => 'table1')->one->{key3}, 3);
2721

            
2722
$dbi = DBIx::Custom->connect;
2723
eval { $dbi->execute('drop table table1') };
2724
$dbi->execute($create_table1_2);
2725
$dbi->insert(
2726
    {key3 => 3},
2727
    primary_key => ['key1', 'key2'], 
2728
    table => 'table1',
2729
    id => [1, 2],
2730
);
2731
is($dbi->select(table => 'table1')->one->{key1}, 1);
2732
is($dbi->select(table => 'table1')->one->{key2}, 2);
2733
is($dbi->select(table => 'table1')->one->{key3}, 3);
test cleanup
Yuki Kimoto authored on 2011-08-10
2734

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

            
2749
$dbi = MyDBI6->connect;
2750
eval { $dbi->execute('drop table table1') };
2751
$dbi->execute($create_table1_2);
2752
$dbi->model('table1')->insert(
2753
    {key3 => 3},
2754
    id => [1, 2]
2755
);
2756
$result = $dbi->model('table1')->select;
2757
$row = $result->one;
2758
is($row->{key1}, 1);
2759
is($row->{key2}, 2);
2760
is($row->{key3}, 3);
2761

            
2762
test 'update and id option';
2763
$dbi = DBIx::Custom->connect;
2764
eval { $dbi->execute('drop table table1') };
2765
$dbi->execute($create_table1_2);
2766
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2767
$dbi->update(
2768
    table => 'table1',
2769
    primary_key => ['key1', 'key2'],
2770
    id => [1, 2],
2771
    param => {key3 => 4}
2772
);
2773
is($dbi->select(table => 'table1')->one->{key1}, 1);
2774
is($dbi->select(table => 'table1')->one->{key2}, 2);
2775
is($dbi->select(table => 'table1')->one->{key3}, 4);
2776

            
2777
$dbi->delete_all(table => 'table1');
2778
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2779
$dbi->update(
2780
    table => 'table1',
2781
    primary_key => 'key1',
2782
    id => 0,
2783
    param => {key3 => 4}
2784
);
2785
is($dbi->select(table => 'table1')->one->{key1}, 0);
2786
is($dbi->select(table => 'table1')->one->{key2}, 2);
2787
is($dbi->select(table => 'table1')->one->{key3}, 4);
2788

            
2789
$dbi = DBIx::Custom->connect;
2790
eval { $dbi->execute('drop table table1') };
2791
$dbi->execute($create_table1_2);
2792
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2793
$dbi->update(
2794
    {key3 => 4},
2795
    table => 'table1',
2796
    primary_key => ['key1', 'key2'],
2797
    id => [1, 2]
2798
);
2799
is($dbi->select(table => 'table1')->one->{key1}, 1);
2800
is($dbi->select(table => 'table1')->one->{key2}, 2);
2801
is($dbi->select(table => 'table1')->one->{key3}, 4);
2802

            
2803

            
2804
test 'model update and id option';
2805
$dbi = MyDBI6->connect;
2806
eval { $dbi->execute('drop table table1') };
2807
$dbi->execute($create_table1_2);
2808
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2809
$dbi->model('table1')->update(
2810
    id => [1, 2],
2811
    param => {key3 => 4}
2812
);
2813
$result = $dbi->model('table1')->select;
2814
$row = $result->one;
2815
is($row->{key1}, 1);
2816
is($row->{key2}, 2);
2817
is($row->{key3}, 4);
2818

            
2819

            
2820
test 'delete and id option';
2821
$dbi = DBIx::Custom->connect;
2822
eval { $dbi->execute('drop table table1') };
2823
$dbi->execute($create_table1_2);
2824
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2825
$dbi->delete(
2826
    table => 'table1',
2827
    primary_key => ['key1', 'key2'],
2828
    id => [1, 2],
2829
);
2830
is_deeply($dbi->select(table => 'table1')->all, []);
2831

            
2832
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2833
$dbi->delete(
2834
    table => 'table1',
2835
    primary_key => 'key1',
2836
    id => 0,
2837
);
2838
is_deeply($dbi->select(table => 'table1')->all, []);
2839

            
2840

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

            
2859

            
2860
test 'select and id option';
2861
$dbi = DBIx::Custom->connect;
2862
eval { $dbi->execute('drop table table1') };
2863
$dbi->execute($create_table1_2);
2864
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2865
$result = $dbi->select(
2866
    table => 'table1',
2867
    primary_key => ['key1', 'key2'],
2868
    id => [1, 2]
2869
);
2870
$row = $result->one;
2871
is($row->{key1}, 1);
2872
is($row->{key2}, 2);
2873
is($row->{key3}, 3);
2874

            
2875
$dbi->delete_all(table => 'table1');
2876
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2877
$result = $dbi->select(
2878
    table => 'table1',
2879
    primary_key => 'key1',
2880
    id => 0,
2881
);
2882
$row = $result->one;
2883
is($row->{key1}, 0);
2884
is($row->{key2}, 2);
2885
is($row->{key3}, 3);
2886

            
2887
$dbi->delete_all(table => 'table1');
2888
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2889
$result = $dbi->select(
2890
    table => 'table1',
2891
    primary_key => ['key1', 'key2'],
2892
    id => [1, 2]
2893
);
2894
$row = $result->one;
2895
is($row->{key1}, 1);
2896
is($row->{key2}, 2);
2897
is($row->{key3}, 3);
2898

            
2899

            
2900
test 'model select_at';
2901
$dbi = MyDBI6->connect;
2902
eval { $dbi->execute('drop table table1') };
2903
$dbi->execute($create_table1_2);
2904
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2905
$result = $dbi->model('table1')->select(id => [1, 2]);
2906
$row = $result->one;
2907
is($row->{key1}, 1);
2908
is($row->{key2}, 2);
2909
is($row->{key3}, 3);
2910

            
2911
test 'column separator is default .';
2912
$dbi = MyDBI7->connect;
2913
eval { $dbi->execute('drop table table1') };
2914
eval { $dbi->execute('drop table table2') };
2915
$dbi->execute($create_table1);
2916
$dbi->execute($create_table2);
2917
$dbi->setup_model;
2918
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2919
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2920
$model = $dbi->model('table1');
2921
$result = $model->select(
2922
    column => [$model->column('table2')],
2923
    where => {'table1.key1' => 1}
2924
);
2925
is_deeply($result->one,
2926
          {'table2.key1' => 1, 'table2.key3' => 3});
2927

            
2928
$result = $model->select(
2929
    column => [$model->column('table2' => [qw/key1 key3/])],
2930
    where => {'table1.key1' => 1}
2931
);
2932
is_deeply($result->one,
2933
          {'table2.key1' => 1, 'table2.key3' => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2934

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2935
test 'separator';
2936
$dbi = DBIx::Custom->connect;
2937
eval { $dbi->execute('drop table table1') };
2938
eval { $dbi->execute('drop table table2') };
2939
$dbi->execute($create_table1);
2940
$dbi->execute($create_table2);
test cleanup
Yuki Kimoto authored on 2011-08-10
2941

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2967
$dbi->separator('__');
2968
$model = $dbi->model('table1');
2969
$result = $model->select(
2970
    column => [
2971
        $model->mycolumn,
2972
        {table2 => [qw/key1 key3/]}
2973
    ],
2974
    where => {'table1.key1' => 1}
2975
);
2976
is_deeply($result->one,
2977
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
2978
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2979

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

            
2993

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2994
test 'filter_off';
2995
$dbi = DBIx::Custom->connect;
2996
eval { $dbi->execute('drop table table1') };
2997
eval { $dbi->execute('drop table table2') };
2998
$dbi->execute($create_table1);
2999
$dbi->execute($create_table2);
test cleanup
Yuki Kimoto authored on 2011-08-10
3000

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3015
test 'available_datetype';
3016
$dbi = DBIx::Custom->connect;
3017
ok($dbi->can('available_datatype'));
test cleanup
Yuki Kimoto authored on 2011-08-10
3018

            
3019

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3020
test 'select prefix option';
3021
$dbi = DBIx::Custom->connect;
3022
eval { $dbi->execute('drop table table1') };
3023
$dbi->execute($create_table1);
3024
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3025
$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all;
3026
is_deeply($rows, [{key1 => 1, key2 => 2}], "table");
test cleanup
Yuki Kimoto authored on 2011-08-10
3027

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3028
test 'map_param';
3029
$dbi = DBIx::Custom->connect;
3030
$param = $dbi->map_param(
3031
    {id => 1, author => 'Ken', price => 1900},
test cleanup
Yuki Kimoto authored on 2011-08-10
3032
    id => 'table1.id',
3033
    author => ['table1.author', sub { '%' . $_[0] . '%' }],
3034
    price => ['table1.price', {if => sub { $_[0] eq 1900 }}]
cleanup test
Yuki Kimoto authored on 2011-08-10
3035
);
test cleanup
Yuki Kimoto authored on 2011-08-10
3036
is_deeply($param, {'table1.id' => 1, 'table1.author' => '%Ken%',
3037
  'table1.price' => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
3038

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3057
$param = $dbi->map_param(
3058
    {id => undef, author => undef, price => undef},
test cleanup
Yuki Kimoto authored on 2011-08-10
3059
    id => 'table1.id',
3060
    price => ['table1.price', {if => 'exists'}]
cleanup test
Yuki Kimoto authored on 2011-08-10
3061
);
test cleanup
Yuki Kimoto authored on 2011-08-10
3062
is_deeply($param, {'table1.price' => undef});
test cleanup
Yuki Kimoto authored on 2011-08-10
3063

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

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

            
3089
$order = $dbi->order;
3090
$order->prepend(['table1-key1'], [qw/table1-key2 desc/]);
3091
$result = $dbi->select(table => 'table1',
3092
  column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']],
3093
  append => "$order");
3094
is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3},
3095
  {'table1-key1' => 1, 'table1-key2' => 1},
3096
  {'table1-key1' => 2, 'table1-key2' => 4},
3097
  {'table1-key1' => 2, 'table1-key2' => 2}]);
3098

            
3099
test 'tag_parse';
cleanup
Yuki Kimoto authored on 2011-08-13
3100
$DB::single = 1;
test cleanup
Yuki Kimoto authored on 2011-08-10
3101
$dbi = DBIx::Custom->connect;
3102
$dbi->tag_parse(0);
3103
eval { $dbi->execute('drop table table1') };
3104
$dbi->execute($create_table1);
3105
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
3106
eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})};
3107
ok($@);
3108

            
3109
test 'last_sql';
3110
$dbi = DBIx::Custom->connect;
3111
eval { $dbi->execute('drop table table1') };
3112
$dbi->execute($create_table1);
3113
$dbi->execute('select * from table1');
3114
is($dbi->last_sql, 'select * from table1;');
3115

            
3116
eval{$dbi->execute("aaa")};
3117
is($dbi->last_sql, 'aaa;');
3118

            
3119
test 'DBIx::Custom header';
3120
$dbi = DBIx::Custom->connect;
3121
eval { $dbi->execute('drop table table1') };
3122
$dbi->execute($create_table1);
3123
$result = $dbi->execute('select key1 as h1, key2 as h2 from table1');
3124
is_deeply($result->header, [qw/h1 h2/]);
3125

            
3126
test 'Named placeholder :name(operater) syntax';
3127
$dbi->execute('drop table table1');
3128
$dbi->execute($create_table1_2);
3129
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3130
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3131

            
3132
$source = "select * from table1 where :key1{=} and :key2{=}";
3133
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
3134
$rows = $result->all;
3135
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3136

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

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

            
3147
$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}";
3148
$result = $dbi->execute(
3149
    $source,
3150
    param => {'table1.key1' => 1, 'table1.key2' => 1},
3151
    filter => {'table1.key2' => sub { $_[0] * 2 }}
3152
);
3153
$rows = $result->all;
3154
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3155

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

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

            
3199
test 'result';
3200
$dbi = DBIx::Custom->connect;
3201
eval { $dbi->execute('drop table table1') };
3202
$dbi->execute($create_table1);
3203
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3204
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3205

            
3206
$result = $dbi->select(table => 'table1');
3207
@rows = ();
3208
while (my $row = $result->fetch) {
3209
    push @rows, [@$row];
3210
}
3211
is_deeply(\@rows, [[1, 2], [3, 4]]);
3212

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

            
3220
$dbi = DBIx::Custom->connect;
3221
eval { $dbi->execute('drop table table1') };
3222
$dbi->execute($create_table1);
3223
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3224
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3225

            
3226
test 'fetch_all';
3227
$result = $dbi->select(table => 'table1');
3228
$rows = $result->fetch_all;
3229
is_deeply($rows, [[1, 2], [3, 4]]);
3230

            
3231
$result = $dbi->select(table => 'table1');
3232
$rows = $result->fetch_hash_all;
3233
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
3234

            
3235
$result = $dbi->select(table => 'table1');
3236
$result->dbi->filters({three_times => sub { $_[0] * 3}});
3237
$result->filter({key1 => 'three_times'});
3238

            
3239
$rows = $result->fetch_all;
3240
is_deeply($rows, [[3, 2], [9, 4]], "array");
3241

            
3242
$result = $dbi->select(table => 'table1');
3243
$result->dbi->filters({three_times => sub { $_[0] * 3}});
3244
$result->filter({key1 => 'three_times'});
3245
$rows = $result->fetch_hash_all;
3246
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash");
3247

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

            
3284
for (my $i = 0; $i < @$datas; $i++) {
3285
    my $data = $datas->[$i];
cleanup
Yuki Kimoto authored on 2011-08-13
3286
    my $dbi = DBIx::Custom->new;
3287
    my $builder = $dbi->query_builder;
test cleanup
Yuki Kimoto authored on 2011-08-10
3288
    my $query = $builder->build_query($data->{source});
3289
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
3290
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
3291
}
3292

            
cleanup
Yuki Kimoto authored on 2011-08-13
3293
$dbi = DBIx::Custom->new;
3294
$builder = $dbi->query_builder;
3295
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
3296
    p => sub {
3297
        my @args = @_;
3298
        
3299
        my $expand    = "? $args[0] $args[1]";
3300
        my $columns = [2];
3301
        return [$expand, $columns];
3302
    }
3303
);
3304

            
3305
$query = $builder->build_query("{p a b}");
3306
is($query->{sql}, "? a b;", "register_tag sql");
3307
is_deeply($query->{columns}, [2], "register_tag columns");
3308

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

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
3315
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
3316
    q => 'string'
3317
});
3318

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
3329
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
3330
   s => sub { return ["a", ""]} 
3331
});
3332

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

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

            
3340

            
cleanup
Yuki Kimoto authored on 2011-08-13
3341
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
3342
    a => sub {
3343
        return ["? ? ?", ['']];
3344
    }
3345
);
3346
eval{$builder->build_query("{a}")};
3347
like($@, qr/\QPlaceholder count/, "placeholder count is invalid");
3348

            
3349

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

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

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

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

            
3365
test 'variouse source';
3366
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;";
3367
$query = $builder->build_query($source);
3368
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1");
3369

            
3370
$source = "abc;";
3371
$query = $builder->build_query($source);
3372
is($query->sql, 'abc;', "basic : 2");
3373

            
3374
$source = "{= a}";
3375
$query = $builder->build_query($source);
3376
is($query->sql, 'a = ?;', "only tag");
3377

            
3378
$source = "000;";
3379
$query = $builder->build_query($source);
3380
is($query->sql, '000;', "contain 0 value");
3381

            
3382
$source = "a {= b} }";
3383
eval{$builder->build_query($source)};
3384
like($@, qr/unexpected "}"/, "error : 1");
3385

            
3386
$source = "a {= {}";
3387
eval{$builder->build_query($source)};
3388
like($@, qr/unexpected "{"/, "error : 2");
3389

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3403
eval {
3404
$dbi->select(
3405
    table => 'table1',
3406
    column => 'key1',
3407
    wrap => 'select * from ('
3408
)
3409
};
3410
like($@, qr/array/);
3411

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3412
test 'dbi method from model';
3413
$dbi = MyDBI9->connect;
3414
eval { $dbi->execute('drop table table1') };
3415
$dbi->execute($create_table1);
3416
$dbi->setup_model;
3417
$model = $dbi->model('table1');
3418
eval{$model->execute('select * from table1')};
3419
ok(!$@);
3420

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

            
3440
$dbi->separator('__');
3441
$result = $model->select(
3442
    column => [
3443
        $model->column('table2', {alias => 'table2_alias'})
3444
    ],
3445
    where => {'table2_alias.key3' => 4}
3446
);
3447
is_deeply($result->one, 
3448
          {'table2_alias__key1' => 1, 'table2_alias__key3' => 4});
3449

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

            
3460
test 'create_model';
3461
$dbi = DBIx::Custom->connect;
3462
eval { $dbi->execute('drop table table1') };
3463
eval { $dbi->execute('drop table table2') };
3464
$dbi->execute($create_table1);
3465
$dbi->execute($create_table2);
3466

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

            
3495
test 'model method';
3496
$dbi = DBIx::Custom->connect;
3497
eval { $dbi->execute('drop table table2') };
3498
$dbi->execute($create_table2);
3499
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
3500
$model = $dbi->create_model(
3501
    table => 'table2'
3502
);
3503
$model->method(foo => sub { shift->select(@_) });
3504
is_deeply($model->foo->one, {key1 => 1, key3 => 3});
3505

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3506
test 'update_param';
3507
$dbi = DBIx::Custom->connect;
3508
eval { $dbi->execute('drop table table1') };
3509
$dbi->execute($create_table1_2);
3510
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3511
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3512

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

            
3526

            
3527
$dbi = DBIx::Custom->connect;
3528
eval { $dbi->execute('drop table table1') };
3529
$dbi->execute($create_table1_2);
3530
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3531
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3532

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

            
3546
$dbi = DBIx::Custom->connect;
3547
eval { $dbi->execute('drop table table1') };
3548
$dbi->execute($create_table1_2);
3549
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3550
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3551

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

            
3565
            
3566
eval { $dbi->update_param({";" => 1}) };
3567
like($@, qr/not safety/);
3568

            
3569

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

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

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

            
3610
$rows = $dbi->select(
3611
    table => 'table1',
3612
    where   => {'key1' => 1},
3613
    join  => ['left outer join table2 on table1.key1 = table2.key1']
3614
)->all;
3615
is_deeply($rows, [{key1 => 1, key2 => 2}]);
3616

            
3617
eval {
3618
    $rows = $dbi->select(
3619
        table => 'table1',
3620
        column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
3621
        where   => {'table1.key2' => 2},
3622
        join  => {'table1.key1' => 'table2.key1'}
3623
    );
3624
};
3625
like ($@, qr/array/);
3626

            
3627
$rows = $dbi->select(
3628
    table => 'table1',
3629
    where   => {'key1' => 1},
3630
    join  => ['left outer join table2 on table1.key1 = table2.key1',
3631
              'left outer join table3 on table2.key3 = table3.key3']
3632
)->all;
3633
is_deeply($rows, [{key1 => 1, key2 => 2}]);
3634

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

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

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

            
3669

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

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

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

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

            
3759
$dbi = MyDBI4->connect;
3760
eval { $dbi->execute('drop table table1') };
3761
$dbi->execute($create_table1);
3762
$model = $dbi->model('table1');
3763
$model->insert({key1 => 'a', key2 => 'b'});
3764
is_deeply($model->list->all, [{key1 => 'a', key2 => 'b'}], 'basic');
3765
eval { $dbi->execute('drop table table2') };
3766
$dbi->execute($create_table2);
3767
$model = $dbi->model('table2');
3768
$model->insert({key1 => 'a'});
3769
is_deeply($model->list->all, [{key1 => 'a', key3 => undef}], 'basic');
3770

            
3771
$dbi = MyDBI5->connect;
3772
eval { $dbi->execute('drop table table1') };
3773
eval { $dbi->execute('drop table table2') };
3774
$dbi->execute($create_table1);
3775
$dbi->execute($create_table2);
3776
$model = $dbi->model('table2');
3777
$model->insert({key1 => 'a'});
3778
is_deeply($model->list->all, [{key1 => 'a', key3 => undef}], 'include all model');
3779
$dbi->insert(table => 'table1', param => {key1 => 1});
3780
$model = $dbi->model('table1');
3781
is_deeply($model->list->all, [{key1 => 1, key2 => undef}], 'include all model');
3782

            
3783
test 'primary_key';
3784
use MyDBI1;
3785
$dbi = MyDBI1->connect;
3786
$model = $dbi->model('table1');
3787
$model->primary_key(['key1', 'key2']);
3788
is_deeply($model->primary_key, ['key1', 'key2']);
3789

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

            
3797
test 'setup_model';
3798
use MyDBI1;
3799
$dbi = MyDBI1->connect;
3800
eval { $dbi->execute('drop table table1') };
3801
eval { $dbi->execute('drop table table2') };
3802

            
3803
$dbi->execute($create_table1);
3804
$dbi->execute($create_table2);
3805
$dbi->setup_model;
3806
is_deeply([sort @{$dbi->model('table1')->columns}], ['key1', 'key2']);
3807
is_deeply([sort @{$dbi->model('table2')->columns}], ['key1', 'key3']);
3808

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

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

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