DBIx-Custom / t / common.t /
Newer Older
3830 lines | 124.292kb
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;
added common test executing ...
Yuki Kimoto authored on 2011-08-07
161

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

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

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

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

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

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

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

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

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

            
317

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
620

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1022

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

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

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

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

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

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

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

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

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

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

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

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

            
1118

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

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

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

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

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

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

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

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

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

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

            
1186
test 'filters';
1187
$dbi = DBIx::Custom->new;
1188

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1195
test 'transaction1';
test cleanup
Yuki Kimoto authored on 2011-08-10
1196
$dbi = DBIx::Custom->connect;
1197
eval { $dbi->execute('drop table table1') };
1198
$dbi->execute($create_table1);
fixed transaction test
Yuki Kimoto authored on 2011-08-14
1199
$dbi->begin_work;
1200
$dbi->dbh->{AutoCommit} = 0;
test cleanup
Yuki Kimoto authored on 2011-08-10
1201
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
fixed transaction test
Yuki Kimoto authored on 2011-08-14
1202
$dbi->rollback;
1203
$dbi->dbh->{AutoCommit} = 1;
1204

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1205
$result = $dbi->select(table => 'table1');
fixed transaction test
Yuki Kimoto authored on 2011-08-14
1206
ok(! $result->fetch_first, "rollback");
1207

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

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

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

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

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

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

            
1256

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

            
1262
$dbi->begin_work;
1263

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

            
1270
$dbi->rollback if $@;
1271

            
1272
$result = $dbi->select(table => 'table1');
1273
$rows = $result->all;
1274
is_deeply($rows, [], "rollback");
1275

            
1276
$dbi->begin_work;
1277

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

            
1283
$dbi->commit unless $@;
1284

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

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

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

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

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

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

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

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

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

            
1358
is($dbi->one, 1, "first");
1359
is($dbi->two, 2, "second");
1360
is($dbi->twice(5), 10 , "second");
1361

            
1362
eval {$dbi->XXXXXX};
1363
ok($@, "not exists");
1364

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1674
$where = $dbi->where
1675
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
1676
             ->param({key1 => 1});
1677

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

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

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

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

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

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

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

            
1743
$where = $dbi->where;
1744
is("$where", '');
1745

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1921
eval {$dbi->where(ppp => 1) };
1922
like($@, qr/invalid/);
1923

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

            
1935

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2100
test 'dbi_option default';
2101
$dbi = DBIx::Custom->new;
2102
is_deeply($dbi->dbi_option, {});
2103

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

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

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

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

            
2134
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
2135
like($@, qr/apply_filter/);
2136

            
2137
$dbi->apply_filter(
2138

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

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

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

            
2167
eval{DBIx::Custom->connect(dsn => undef)};
2168
like($@, qr/_connect/);
2169

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2416

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

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

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

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

            
2475

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2669

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

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

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

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

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

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

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

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

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

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

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

            
2806

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

            
2822

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

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

            
2843

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

            
2862

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

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

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

            
2902

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

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

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

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

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

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

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

            
2996

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

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

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

            
3022

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

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

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

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

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

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

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

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

            
3102
test 'tag_parse';
3103
$dbi = DBIx::Custom->connect;
3104
$dbi->tag_parse(0);
3105
eval { $dbi->execute('drop table table1') };
3106
$dbi->execute($create_table1);
3107
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
3108
eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})};
3109
ok($@);
3110

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
3342

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

            
3351

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

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

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

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

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

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

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

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

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

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

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

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

            
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
3414
test 'select() sqlfilter option';
3415
$dbi = DBIx::Custom->connect;
3416
eval { $dbi->execute('drop table table1') };
3417
$dbi->execute($create_table1);
3418
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3419
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
3420
$rows = $dbi->select(
3421
    table => 'table1',
3422
    column => 'key1',
3423
    sqlfilter => sub {
3424
        my $sql = shift;
3425
        $sql = "select * from ( $sql ) as t where key1 = 1";
3426
        return $sql;
3427
    }
3428
)->all;
3429
is_deeply($rows, [{key1 => 1}]);
3430

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3431
test 'dbi method from model';
3432
$dbi = MyDBI9->connect;
3433
eval { $dbi->execute('drop table table1') };
3434
$dbi->execute($create_table1);
3435
$dbi->setup_model;
3436
$model = $dbi->model('table1');
3437
eval{$model->execute('select * from table1')};
3438
ok(!$@);
3439

            
3440
test 'column table option';
3441
$dbi = MyDBI9->connect;
3442
eval { $dbi->execute('drop table table1') };
3443
$dbi->execute($create_table1);
3444
eval { $dbi->execute('drop table table2') };
3445
$dbi->execute($create_table2);
3446
$dbi->setup_model;
3447
$dbi->execute('insert into table1 (key1, key2) values (1, 2);');
3448
$dbi->execute('insert into table2 (key1, key3) values (1, 4);');
3449
$model = $dbi->model('table1');
3450
$result = $model->select(
3451
    column => [
3452
        $model->column('table2', {alias => 'table2_alias'})
3453
    ],
3454
    where => {'table2_alias.key3' => 4}
3455
);
3456
is_deeply($result->one, 
3457
          {'table2_alias.key1' => 1, 'table2_alias.key3' => 4});
3458

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

            
3469
$dbi->separator('-');
3470
$result = $model->select(
3471
    column => [
3472
        $model->column('table2', {alias => 'table2_alias'})
3473
    ],
3474
    where => {'table2_alias.key3' => 4}
3475
);
3476
is_deeply($result->one, 
3477
          {'table2_alias-key1' => 1, 'table2_alias-key3' => 4});
3478

            
3479
test 'create_model';
3480
$dbi = DBIx::Custom->connect;
3481
eval { $dbi->execute('drop table table1') };
3482
eval { $dbi->execute('drop table table2') };
3483
$dbi->execute($create_table1);
3484
$dbi->execute($create_table2);
3485

            
3486
$dbi->create_model(
3487
    table => 'table1',
3488
    join => [
3489
       'left outer join table2 on table1.key1 = table2.key1'
3490
    ],
3491
    primary_key => ['key1']
3492
);
3493
$model2 = $dbi->create_model(
3494
    table => 'table2'
3495
);
3496
$dbi->create_model(
3497
    table => 'table3',
3498
    filter => [
3499
        key1 => {in => sub { uc $_[0] }}
3500
    ]
3501
);
3502
$dbi->setup_model;
3503
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3504
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
3505
$model = $dbi->model('table1');
3506
$result = $model->select(
3507
    column => [$model->mycolumn, $model->column('table2')],
3508
    where => {'table1.key1' => 1}
3509
);
3510
is_deeply($result->one,
3511
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
3512
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3513

            
3514
test 'model method';
3515
$dbi = DBIx::Custom->connect;
3516
eval { $dbi->execute('drop table table2') };
3517
$dbi->execute($create_table2);
3518
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
3519
$model = $dbi->create_model(
3520
    table => 'table2'
3521
);
3522
$model->method(foo => sub { shift->select(@_) });
3523
is_deeply($model->foo->one, {key1 => 1, key3 => 3});
3524

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3525
test 'update_param';
3526
$dbi = DBIx::Custom->connect;
3527
eval { $dbi->execute('drop table table1') };
3528
$dbi->execute($create_table1_2);
3529
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3530
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3531

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

            
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);
3554
$sql = <<"EOS";
3555
update table1 $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
                  "basic");
3564

            
3565
$dbi = DBIx::Custom->connect;
3566
eval { $dbi->execute('drop table table1') };
3567
$dbi->execute($create_table1_2);
3568
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3569
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3570

            
3571
$param = {key2 => 11, key3 => 33};
3572
$update_param = $dbi->update_param($param, {no_set => 1});
3573
$sql = <<"EOS";
3574
update table1 set $update_param
3575
where key1 = 1
3576
EOS
3577
$dbi->execute($sql, param => $param);
3578
$result = $dbi->execute('select * from table1 order by key1;', table => 'table1');
3579
$rows   = $result->all;
3580
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
3581
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
3582
                  "update param no_set");
3583

            
3584
            
3585
eval { $dbi->update_param({";" => 1}) };
3586
like($@, qr/not safety/);
3587

            
3588

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3609
test 'join';
3610
$dbi = DBIx::Custom->connect;
3611
eval { $dbi->execute('drop table table1') };
3612
$dbi->execute($create_table1);
3613
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3614
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
3615
eval { $dbi->execute('drop table table2') };
3616
$dbi->execute($create_table2);
3617
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
3618
eval { $dbi->execute('drop table table3') };
3619
$dbi->execute('create table table3 (key3 int, key4 int);');
3620
$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4});
3621
$rows = $dbi->select(
3622
    table => 'table1',
3623
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
3624
    where   => {'table1.key2' => 2},
3625
    join  => ['left outer join table2 on table1.key1 = table2.key1']
3626
)->all;
3627
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]);
3628

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

            
3636
eval {
3637
    $rows = $dbi->select(
3638
        table => 'table1',
3639
        column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
3640
        where   => {'table1.key2' => 2},
3641
        join  => {'table1.key1' => 'table2.key1'}
3642
    );
3643
};
3644
like ($@, qr/array/);
3645

            
3646
$rows = $dbi->select(
3647
    table => 'table1',
3648
    where   => {'key1' => 1},
3649
    join  => ['left outer join table2 on table1.key1 = table2.key1',
3650
              'left outer join table3 on table2.key3 = table3.key3']
3651
)->all;
3652
is_deeply($rows, [{key1 => 1, key2 => 2}]);
3653

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

            
3663
$rows = $dbi->select(
3664
    column => 'table1.key1 as table1__key1',
3665
    table => 'table1',
3666
    where   => {'table3.key4' => 4},
3667
    join  => ['left outer join table2 on table1.key1 = table2.key1',
3668
              'left outer join table3 on table2.key3 = table3.key3']
3669
)->all;
3670
is_deeply($rows, [{table1__key1 => 1}]);
3671

            
3672
$dbi = DBIx::Custom->connect;
3673
eval { $dbi->execute('drop table table1') };
3674
$dbi->execute($create_table1);
3675
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3676
eval { $dbi->execute('drop table table2') };
3677
$dbi->execute($create_table2);
3678
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
3679
$rows = $dbi->select(
3680
    table => 'table1',
3681
    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",
3682
    where   => {'table1.key2' => 2},
3683
    join  => ["left outer join ${q}table2$p on ${q}table1$p.${q}key1$p = ${q}table2$p.${q}key1$p"],
3684
)->all;
3685
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}],
3686
          'quote');
3687

            
3688

            
3689
$dbi = DBIx::Custom->connect;
3690
eval { $dbi->execute('drop table table1') };
3691
$dbi->execute($create_table1);
3692
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3693
$sql = <<"EOS";
3694
left outer join (
3695
  select * from table1 as t1
3696
  where t1.key2 = (
3697
    select max(t2.key2) from table1 as t2
3698
    where t1.key1 = t2.key1
3699
  )
3700
) as latest_table1 on table1.key1 = latest_table1.key1
3701
EOS
3702
$join = [$sql];
3703
$rows = $dbi->select(
3704
    table => 'table1',
3705
    column => 'latest_table1.key1 as latest_table1__key1',
3706
    join  => $join
3707
)->all;
3708
is_deeply($rows, [{latest_table1__key1 => 1}]);
3709

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

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3762
test 'Model class';
3763
use MyDBI1;
3764
$dbi = MyDBI1->connect;
3765
eval { $dbi->execute('drop table table1') };
3766
$dbi->execute($create_table1);
3767
$model = $dbi->model('table1');
3768
$model->insert({key1 => 'a', key2 => 'b'});
3769
is_deeply($model->list->all, [{key1 => 'a', key2 => 'b'}], 'basic');
3770
eval { $dbi->execute('drop table table2') };
3771
$dbi->execute($create_table2);
3772
$model = $dbi->model('table2');
3773
$model->insert({key1 => 'a'});
3774
is_deeply($model->list->all, [{key1 => 'a', key3 => undef}], 'basic');
3775
is($dbi->models->{'table1'}, $dbi->model('table1'));
3776
is($dbi->models->{'table2'}, $dbi->model('table2'));
3777

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

            
3790
$dbi = MyDBI5->connect;
3791
eval { $dbi->execute('drop table table1') };
3792
eval { $dbi->execute('drop table table2') };
3793
$dbi->execute($create_table1);
3794
$dbi->execute($create_table2);
3795
$model = $dbi->model('table2');
3796
$model->insert({key1 => 'a'});
3797
is_deeply($model->list->all, [{key1 => 'a', key3 => undef}], 'include all model');
3798
$dbi->insert(table => 'table1', param => {key1 => 1});
3799
$model = $dbi->model('table1');
3800
is_deeply($model->list->all, [{key1 => 1, key2 => undef}], 'include all model');
3801

            
3802
test 'primary_key';
3803
use MyDBI1;
3804
$dbi = MyDBI1->connect;
3805
$model = $dbi->model('table1');
3806
$model->primary_key(['key1', 'key2']);
3807
is_deeply($model->primary_key, ['key1', 'key2']);
3808

            
3809
test 'columns';
3810
use MyDBI1;
3811
$dbi = MyDBI1->connect;
3812
$model = $dbi->model('table1');
3813
$model->columns(['key1', 'key2']);
3814
is_deeply($model->columns, ['key1', 'key2']);
3815

            
3816
test 'setup_model';
3817
use MyDBI1;
3818
$dbi = MyDBI1->connect;
3819
eval { $dbi->execute('drop table table1') };
3820
eval { $dbi->execute('drop table table2') };
3821

            
3822
$dbi->execute($create_table1);
3823
$dbi->execute($create_table2);
3824
$dbi->setup_model;
3825
is_deeply([sort @{$dbi->model('table1')->columns}], ['key1', 'key2']);
3826
is_deeply([sort @{$dbi->model('table2')->columns}], ['key1', 'key3']);
3827

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

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

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