DBIx-Custom / t / sqlite.t /
Newer Older
2061 lines | 59.92kb
cleanup test
Yuki Kimoto authored on 2011-08-06
1
use Test::More;
2
use strict;
3
use warnings;
4
use utf8;
5
use Encode qw/encode_utf8 decode_utf8/;
test cleanup
Yuki Kimoto authored on 2011-08-06
6
use FindBin;
cleanup test
Yuki Kimoto authored on 2011-08-10
7
use lib "$FindBin::Bin/common";
cleanup test
Yuki Kimoto authored on 2011-08-06
8

            
9
BEGIN {
10
    eval { require DBD::SQLite; 1 }
11
        or plan skip_all => 'DBD::SQLite required';
12
    eval { DBD::SQLite->VERSION >= 1.25 }
13
        or plan skip_all => 'DBD::SQLite >= 1.25 required';
14

            
15
    plan 'no_plan';
16
    use_ok('DBIx::Custom');
17
}
18

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
22
use DBIx::Custom;
test cleanup
Yuki Kimoto authored on 2011-08-10
23
{
24
    package DBIx::Custom;
25
    has dsn => sub { 'dbi:SQLite:dbname=:memory:' }
26
}
test cleanup
Yuki Kimoto authored on 2011-08-10
27
use MyDBI1;
28
{
29
    package MyDBI4;
30

            
31
    use strict;
32
    use warnings;
33

            
34
    use base 'DBIx::Custom';
35

            
36
    sub connect {
37
        my $self = shift->SUPER::connect(@_);
38
        
39
        $self->include_model(
40
            MyModel2 => [
41
                'book',
42
                {class => 'Company', name => 'company'}
43
            ]
44
        );
45
    }
46

            
47
    package MyModel2::Base1;
48

            
49
    use strict;
50
    use warnings;
51

            
52
    use base 'DBIx::Custom::Model';
53

            
54
    package MyModel2::book;
55

            
56
    use strict;
57
    use warnings;
58

            
59
    use base 'MyModel2::Base1';
60

            
61
    sub insert {
62
        my ($self, $param) = @_;
63
        
64
        return $self->SUPER::insert(param => $param);
65
    }
66

            
67
    sub list { shift->select; }
68

            
69
    package MyModel2::Company;
70

            
71
    use strict;
72
    use warnings;
73

            
74
    use base 'MyModel2::Base1';
75

            
76
    sub insert {
77
        my ($self, $param) = @_;
78
        
79
        return $self->SUPER::insert(param => $param);
80
    }
81

            
82
    sub list { shift->select; }
83
}
84
{
85
     package MyDBI5;
86

            
87
    use strict;
88
    use warnings;
89

            
90
    use base 'DBIx::Custom';
91

            
92
    sub connect {
93
        my $self = shift->SUPER::connect(@_);
94
        
95
        $self->include_model('MyModel4');
96
    }
97
}
98
{
99
    package MyDBI6;
100
    
101
    use base 'DBIx::Custom';
102
    
103
    sub connect {
104
        my $self = shift->SUPER::connect(@_);
105
        
106
        $self->include_model('MyModel5');
107
        
108
        return $self;
109
    }
110
}
111
{
112
    package MyDBI7;
113
    
114
    use base 'DBIx::Custom';
115
    
116
    sub connect {
117
        my $self = shift->SUPER::connect(@_);
118
        
119
        $self->include_model('MyModel6');
120
        
121
        
122
        return $self;
123
    }
124
}
125
{
126
    package MyDBI8;
127
    
128
    use base 'DBIx::Custom';
129
    
130
    sub connect {
131
        my $self = shift->SUPER::connect(@_);
132
        
133
        $self->include_model('MyModel7');
134
        
135
        return $self;
136
    }
137
}
138

            
139
{
140
    package MyDBI9;
141
    
142
    use base 'DBIx::Custom';
143
    
144
    sub connect {
145
        my $self = shift->SUPER::connect(@_);
146
        
147
        $self->include_model('MyModel8')->setup_model;
148
        
149
        return $self;
150
    }
151
}
test cleanup
Yuki Kimoto authored on 2011-08-10
152

            
cleanup test
Yuki Kimoto authored on 2011-08-06
153
# Constant
cleanup test
Yuki Kimoto authored on 2011-08-10
154
my $create_table1 = 'create table table1 (key1 char(255), key2 char(255));';
155
my $create_table1_2 = 'create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));';
test cleanup
Yuki Kimoto authored on 2011-08-10
156
my $create_table2 = 'create table table2 (key1 char(255), key3 char(255));';
cleanup test
Yuki Kimoto authored on 2011-08-10
157
my $create_table2_2 = "create table table2 (key1, key2, key3)";
158
my $create_table3 = "create table table3 (key1, key2, key3)";
test cleanup
Yuki Kimoto authored on 2011-08-10
159
my $create_table_reserved = 'create table "table" ("select", "update")';
160

            
test cleanup
Yuki Kimoto authored on 2011-08-10
161
my $q = '"';
162
my $p = '"';
cleanup test
Yuki Kimoto authored on 2011-08-06
163

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

            
196
# Prepare table
test cleanup
Yuki Kimoto authored on 2011-08-10
197
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
198

            
199
test 'merge_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
200
$dbi = DBIx::Custom->new;
201
$params = [
202
    {key1 => 1, key2 => 2, key3 => 3},
203
    {key1 => 1, key2 => 2},
204
    {key1 => 1}
205
];
206
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]);
207
is_deeply($param, {key1 => [1, 1, 1], key2 => [2, 2], key3 => 3});
208

            
209
$params = [
210
    {key1 => [1, 2], key2 => 1, key3 => [1, 2]},
211
    {key1 => [3, 4], key2 => [2, 3], key3 => 3}
212
];
213
$param = $dbi->merge_param($params->[0], $params->[1]);
214
is_deeply($param, {key1 => [1, 2, 3, 4], key2 => [1, 2, 3], key3 => [1, 2, 3]});
cleanup test
Yuki Kimoto authored on 2011-08-06
215

            
216
test 'select() param option';
test cleanup
Yuki Kimoto authored on 2011-08-10
217
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
218
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
219
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
220
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
221
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
222
eval { $dbi->execute('drop table table2') };
test cleanup
Yuki Kimoto authored on 2011-08-10
223
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
224
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
225
$dbi->insert(table => 'table2', param => {key1 => 2, key3 => 5});
226
$rows = $dbi->select(
227
    table => 'table1',
228
    column => 'table1.key1 as table1_key1, key2, key3',
229
    where   => {'table1.key2' => 3},
230
    join  => ['inner join (select * from table2 where {= table2.key3})' . 
231
              ' as table2 on table1.key1 = table2.key1'],
232
    param => {'table2.key3' => 5}
233
)->all;
234
is_deeply($rows, [{table1_key1 => 2, key2 => 3, key3 => 5}]);
235

            
236

            
237
test 'select() wrap option';
test cleanup
Yuki Kimoto authored on 2011-08-10
238
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
239
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
240
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
241
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
242
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
243
$rows = $dbi->select(
244
    table => 'table1',
245
    column => 'key1',
246
    wrap => ['select * from (', ') as t where key1 = 1']
247
)->all;
248
is_deeply($rows, [{key1 => 1}]);
249

            
250
eval {
251
$dbi->select(
252
    table => 'table1',
253
    column => 'key1',
254
    wrap => 'select * from ('
255
)
256
};
257
like($@, qr/array/);
258

            
259
test 'select() string where';
test cleanup
Yuki Kimoto authored on 2011-08-10
260
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
261
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
262
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
263
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
264
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
265
$rows = $dbi->select(
266
    table => 'table1',
267
    where => 'key1 = :key1 and key2 = :key2',
268
    where_param => {key1 => 1, key2 => 2}
269
)->all;
270
is_deeply($rows, [{key1 => 1, key2 => 2}]);
271

            
test cleanup
Yuki Kimoto authored on 2011-08-10
272
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
273
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
274
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
275
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
276
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
277
$rows = $dbi->select(
278
    table => 'table1',
279
    where => [
280
        'key1 = :key1 and key2 = :key2',
281
        {key1 => 1, key2 => 2}
282
    ]
283
)->all;
284
is_deeply($rows, [{key1 => 1, key2 => 2}]);
285

            
286
test 'delete() string where';
test cleanup
Yuki Kimoto authored on 2011-08-10
287
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
288
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
289
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
290
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
291
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
292
$dbi->delete(
293
    table => 'table1',
294
    where => 'key1 = :key1 and key2 = :key2',
295
    where_param => {key1 => 1, key2 => 2}
296
);
297
$rows = $dbi->select(table => 'table1')->all;
298
is_deeply($rows, [{key1 => 2, key2 => 3}]);
299

            
test cleanup
Yuki Kimoto authored on 2011-08-10
300
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
301
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
302
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
303
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
304
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
305
$dbi->delete(
306
    table => 'table1',
307
    where => [
308
        'key1 = :key1 and key2 = :key2',
309
         {key1 => 1, key2 => 2}
310
    ]
311
);
312
$rows = $dbi->select(table => 'table1')->all;
313
is_deeply($rows, [{key1 => 2, key2 => 3}]);
314

            
315

            
316
test 'update() string where';
test cleanup
Yuki Kimoto authored on 2011-08-10
317
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
318
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
319
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
320
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
321
$dbi->update(
322
    table => 'table1',
323
    param => {key1 => 5},
324
    where => 'key1 = :key1 and key2 = :key2',
325
    where_param => {key1 => 1, key2 => 2}
326
);
327
$rows = $dbi->select(table => 'table1')->all;
328
is_deeply($rows, [{key1 => 5, key2 => 2}]);
329

            
test cleanup
Yuki Kimoto authored on 2011-08-10
330
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
331
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
332
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
333
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
334
$dbi->update(
335
    table => 'table1',
336
    param => {key1 => 5},
337
    where => [
338
        'key1 = :key1 and key2 = :key2',
339
        {key1 => 1, key2 => 2}
340
    ]
341
);
342
$rows = $dbi->select(table => 'table1')->all;
343
is_deeply($rows, [{key1 => 5, key2 => 2}]);
344

            
345
test 'insert id and primary_key option';
test cleanup
Yuki Kimoto authored on 2011-08-10
346
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
347
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
348
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
349
$dbi->insert(
350
    primary_key => ['key1', 'key2'], 
351
    table => 'table1',
352
    id => [1, 2],
353
    param => {key3 => 3}
354
);
355
is($dbi->select(table => 'table1')->one->{key1}, 1);
356
is($dbi->select(table => 'table1')->one->{key2}, 2);
357
is($dbi->select(table => 'table1')->one->{key3}, 3);
358

            
359
$dbi->delete_all(table => 'table1');
360
$dbi->insert(
361
    primary_key => 'key1', 
362
    table => 'table1',
363
    id => 0,
364
    param => {key2 => 2, key3 => 3}
365
);
366

            
367
is($dbi->select(table => 'table1')->one->{key1}, 0);
368
is($dbi->select(table => 'table1')->one->{key2}, 2);
369
is($dbi->select(table => 'table1')->one->{key3}, 3);
370

            
test cleanup
Yuki Kimoto authored on 2011-08-10
371
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
372
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
373
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
374
$dbi->insert(
375
    {key3 => 3},
376
    primary_key => ['key1', 'key2'], 
377
    table => 'table1',
378
    id => [1, 2],
379
);
380
is($dbi->select(table => 'table1')->one->{key1}, 1);
381
is($dbi->select(table => 'table1')->one->{key2}, 2);
382
is($dbi->select(table => 'table1')->one->{key3}, 3);
383

            
384

            
385
test 'model insert id and primary_key option';
test cleanup
Yuki Kimoto authored on 2011-08-10
386
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
387
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
388
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
389
$dbi->model('table1')->insert(
390
    id => [1, 2],
391
    param => {key3 => 3}
392
);
393
$result = $dbi->model('table1')->select;
394
$row = $result->one;
395
is($row->{key1}, 1);
396
is($row->{key2}, 2);
397
is($row->{key3}, 3);
398

            
test cleanup
Yuki Kimoto authored on 2011-08-10
399
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
400
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
401
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
402
$dbi->model('table1')->insert(
403
    {key3 => 3},
404
    id => [1, 2]
405
);
406
$result = $dbi->model('table1')->select;
407
$row = $result->one;
408
is($row->{key1}, 1);
409
is($row->{key2}, 2);
410
is($row->{key3}, 3);
411

            
412
test 'update and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
413
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
414
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
415
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
416
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
417
$dbi->update(
418
    table => 'table1',
419
    primary_key => ['key1', 'key2'],
420
    id => [1, 2],
421
    param => {key3 => 4}
422
);
423
is($dbi->select(table => 'table1')->one->{key1}, 1);
424
is($dbi->select(table => 'table1')->one->{key2}, 2);
425
is($dbi->select(table => 'table1')->one->{key3}, 4);
426

            
427
$dbi->delete_all(table => 'table1');
428
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
429
$dbi->update(
430
    table => 'table1',
431
    primary_key => 'key1',
432
    id => 0,
433
    param => {key3 => 4}
434
);
435
is($dbi->select(table => 'table1')->one->{key1}, 0);
436
is($dbi->select(table => 'table1')->one->{key2}, 2);
437
is($dbi->select(table => 'table1')->one->{key3}, 4);
438

            
test cleanup
Yuki Kimoto authored on 2011-08-10
439
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
440
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
441
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
442
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
443
$dbi->update(
444
    {key3 => 4},
445
    table => 'table1',
446
    primary_key => ['key1', 'key2'],
447
    id => [1, 2]
448
);
449
is($dbi->select(table => 'table1')->one->{key1}, 1);
450
is($dbi->select(table => 'table1')->one->{key2}, 2);
451
is($dbi->select(table => 'table1')->one->{key3}, 4);
452

            
453

            
454
test 'model update and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
455
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
456
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
457
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
458
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
459
$dbi->model('table1')->update(
460
    id => [1, 2],
461
    param => {key3 => 4}
462
);
463
$result = $dbi->model('table1')->select;
464
$row = $result->one;
465
is($row->{key1}, 1);
466
is($row->{key2}, 2);
467
is($row->{key3}, 4);
468

            
469

            
470
test 'delete and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
471
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
472
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
473
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
474
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
475
$dbi->delete(
476
    table => 'table1',
477
    primary_key => ['key1', 'key2'],
478
    id => [1, 2],
479
);
480
is_deeply($dbi->select(table => 'table1')->all, []);
481

            
482
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
483
$dbi->delete(
484
    table => 'table1',
485
    primary_key => 'key1',
486
    id => 0,
487
);
488
is_deeply($dbi->select(table => 'table1')->all, []);
489

            
490

            
491
test 'model delete and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
492
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
493
eval { $dbi->execute('drop table table1') };
494
eval { $dbi->execute('drop table table2') };
495
eval { $dbi->execute('drop table table3') };
cleanup test
Yuki Kimoto authored on 2011-08-10
496
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-10
497
$dbi->execute($create_table2_2);
498
$dbi->execute($create_table3);
cleanup test
Yuki Kimoto authored on 2011-08-06
499
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
500
$dbi->model('table1')->delete(id => [1, 2]);
501
is_deeply($dbi->select(table => 'table1')->all, []);
502
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
503
$dbi->model('table1_1')->delete(id => [1, 2]);
504
is_deeply($dbi->select(table => 'table1')->all, []);
505
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
506
$dbi->model('table1_3')->delete(id => [1, 2]);
507
is_deeply($dbi->select(table => 'table1')->all, []);
508

            
509

            
510
test 'select and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
511
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
512
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
513
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
514
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
515
$result = $dbi->select(
516
    table => 'table1',
517
    primary_key => ['key1', 'key2'],
518
    id => [1, 2]
519
);
520
$row = $result->one;
521
is($row->{key1}, 1);
522
is($row->{key2}, 2);
523
is($row->{key3}, 3);
524

            
525
$dbi->delete_all(table => 'table1');
526
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
527
$result = $dbi->select(
528
    table => 'table1',
529
    primary_key => 'key1',
530
    id => 0,
531
);
532
$row = $result->one;
533
is($row->{key1}, 0);
534
is($row->{key2}, 2);
535
is($row->{key3}, 3);
536

            
537
$dbi->delete_all(table => 'table1');
538
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
539
$result = $dbi->select(
540
    table => 'table1',
541
    primary_key => ['key1', 'key2'],
542
    id => [1, 2]
543
);
544
$row = $result->one;
545
is($row->{key1}, 1);
546
is($row->{key2}, 2);
547
is($row->{key3}, 3);
548

            
549

            
550
test 'model select_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
551
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
552
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
553
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
554
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
555
$result = $dbi->model('table1')->select(id => [1, 2]);
556
$row = $result->one;
557
is($row->{key1}, 1);
558
is($row->{key2}, 2);
559
is($row->{key3}, 3);
560

            
561
test 'column separator is default .';
test cleanup
Yuki Kimoto authored on 2011-08-10
562
$dbi = MyDBI7->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
563
eval { $dbi->execute('drop table table1') };
564
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
565
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
566
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
567
$dbi->setup_model;
568
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
569
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
570
$model = $dbi->model('table1');
571
$result = $model->select(
572
    column => [$model->column('table2')],
573
    where => {'table1.key1' => 1}
574
);
575
is_deeply($result->one,
576
          {'table2.key1' => 1, 'table2.key3' => 3});
577

            
578
$result = $model->select(
579
    column => [$model->column('table2' => [qw/key1 key3/])],
580
    where => {'table1.key1' => 1}
581
);
582
is_deeply($result->one,
583
          {'table2.key1' => 1, 'table2.key3' => 3});
584

            
585

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

            
587
test 'separator';
test cleanup
Yuki Kimoto authored on 2011-08-10
588
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
589
eval { $dbi->execute('drop table table1') };
590
eval { $dbi->execute('drop table table2') };
591
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
592
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-10
593

            
594
$dbi->create_model(
595
    table => 'table1',
596
    join => [
597
       'left outer join table2 on table1.key1 = table2.key1'
598
    ],
599
    primary_key => ['key1'],
cleanup test
Yuki Kimoto authored on 2011-08-06
600
);
cleanup test
Yuki Kimoto authored on 2011-08-10
601
$model2 = $dbi->create_model(
602
    table => 'table2',
603
);
604
$dbi->setup_model;
605
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
606
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
607
$model = $dbi->model('table1');
608
$result = $model->select(
609
    column => [
610
        $model->mycolumn,
611
        {table2 => [qw/key1 key3/]}
612
    ],
613
    where => {'table1.key1' => 1}
614
);
615
is_deeply($result->one,
616
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
617
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
618

            
cleanup test
Yuki Kimoto authored on 2011-08-10
619
$dbi->separator('__');
620
$model = $dbi->model('table1');
621
$result = $model->select(
622
    column => [
623
        $model->mycolumn,
624
        {table2 => [qw/key1 key3/]}
625
    ],
626
    where => {'table1.key1' => 1}
627
);
628
is_deeply($result->one,
629
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
630
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
631

            
cleanup test
Yuki Kimoto authored on 2011-08-10
632
$dbi->separator('-');
633
$model = $dbi->model('table1');
634
$result = $model->select(
635
    column => [
636
        $model->mycolumn,
637
        {table2 => [qw/key1 key3/]}
638
    ],
639
    where => {'table1.key1' => 1}
640
);
641
is_deeply($result->one,
642
          {key1 => 1, key2 => 2, 'table2-key1' => 1, 'table2-key3' => 3});
643
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
644

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

            
646
test 'filter_off';
test cleanup
Yuki Kimoto authored on 2011-08-10
647
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
648
eval { $dbi->execute('drop table table1') };
649
eval { $dbi->execute('drop table table2') };
650
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
651
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-10
652

            
653
$dbi->create_model(
654
    table => 'table1',
655
    join => [
656
       'left outer join table2 on table1.key1 = table2.key1'
657
    ],
658
    primary_key => ['key1'],
cleanup test
Yuki Kimoto authored on 2011-08-06
659
);
cleanup test
Yuki Kimoto authored on 2011-08-10
660
$dbi->setup_model;
661
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
662
$model = $dbi->model('table1');
663
$result = $model->select(column => 'key1');
664
$result->filter(key1 => sub { $_[0] * 2 });
665
is_deeply($result->one, {key1 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-06
666

            
cleanup test
Yuki Kimoto authored on 2011-08-10
667
test 'available_datetype';
test cleanup
Yuki Kimoto authored on 2011-08-10
668
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
669
ok($dbi->can('available_datatype'));
670

            
cleanup test
Yuki Kimoto authored on 2011-08-06
671

            
cleanup test
Yuki Kimoto authored on 2011-08-10
672
test 'select prefix option';
test cleanup
Yuki Kimoto authored on 2011-08-10
673
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
674
eval { $dbi->execute('drop table table1') };
675
$dbi->execute($create_table1);
676
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
677
$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all;
678
is_deeply($rows, [{key1 => 1, key2 => 2}], "table");
679

            
680
test 'map_param';
681
$dbi = DBIx::Custom->connect;
682
$param = $dbi->map_param(
683
    {id => 1, author => 'Ken', price => 1900},
684
    id => 'book.id',
685
    author => ['book.author', sub { '%' . $_[0] . '%' }],
686
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
cleanup test
Yuki Kimoto authored on 2011-08-06
687
);
cleanup test
Yuki Kimoto authored on 2011-08-10
688
is_deeply($param, {'book.id' => 1, 'book.author' => '%Ken%',
689
  'book.price' => 1900});
690

            
691
$param = $dbi->map_param(
692
    {id => 0, author => 0, price => 0},
693
    id => 'book.id',
694
    author => ['book.author', sub { '%' . $_[0] . '%' }],
695
    price => ['book.price', sub { '%' . $_[0] . '%' },
696
      {if => sub { $_[0] eq 0 }}]
cleanup test
Yuki Kimoto authored on 2011-08-06
697
);
cleanup test
Yuki Kimoto authored on 2011-08-10
698
is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
cleanup test
Yuki Kimoto authored on 2011-08-06
699

            
cleanup test
Yuki Kimoto authored on 2011-08-10
700
$param = $dbi->map_param(
701
    {id => '', author => '', price => ''},
702
    id => 'book.id',
703
    author => ['book.author', sub { '%' . $_[0] . '%' }],
704
    price => ['book.price', sub { '%' . $_[0] . '%' },
705
      {if => sub { $_[0] eq 1 }}]
cleanup test
Yuki Kimoto authored on 2011-08-06
706
);
cleanup test
Yuki Kimoto authored on 2011-08-10
707
is_deeply($param, {});
708

            
709
$param = $dbi->map_param(
710
    {id => undef, author => undef, price => undef},
711
    id => 'book.id',
712
    price => ['book.price', {if => 'exists'}]
cleanup test
Yuki Kimoto authored on 2011-08-06
713
);
cleanup test
Yuki Kimoto authored on 2011-08-10
714
is_deeply($param, {'book.price' => undef});
715

            
716
$param = $dbi->map_param(
717
    {price => 'a'},
718
    id => ['book.id', {if => 'exists'}],
719
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
720
);
721
is_deeply($param, {'book.price' => '%a'});
cleanup test
Yuki Kimoto authored on 2011-08-06
722

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

            
724
test 'table_alias';
test cleanup
Yuki Kimoto authored on 2011-08-10
725
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
726
eval { $dbi->execute('drop table table1') };
727
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
cleanup test
Yuki Kimoto authored on 2011-08-06
728
$dbi->type_rule(
729
    into1 => {
cleanup test
Yuki Kimoto authored on 2011-08-10
730
        date => sub { uc $_[0] }
cleanup test
Yuki Kimoto authored on 2011-08-06
731
    }
732
);
cleanup test
Yuki Kimoto authored on 2011-08-10
733
$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'},
734
  table_alias => {table2 => 'table1'});
cleanup test
Yuki Kimoto authored on 2011-08-06
735
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
736
is($result->one->{key1}, 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
737

            
738

            
cleanup test
Yuki Kimoto authored on 2011-08-10
739
test 'order';
test cleanup
Yuki Kimoto authored on 2011-08-10
740
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
741
eval { $dbi->execute('drop table table1') };
742
$dbi->execute("create table table1 (key1, key2)");
743
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
744
$dbi->insert({key1 => 1, key2 => 3}, table => 'table1');
745
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
746
$dbi->insert({key1 => 2, key2 => 4}, table => 'table1');
747
my $order = $dbi->order;
748
$order->prepend('key1', 'key2 desc');
749
$result = $dbi->select(table => 'table1', append => "$order");
750
is_deeply($result->all, [{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1},
751
  {key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}]);
752
$order->prepend('key1 desc');
753
$result = $dbi->select(table => 'table1', append => "$order");
754
is_deeply($result->all, [{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2},
755
  {key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
756

            
cleanup test
Yuki Kimoto authored on 2011-08-10
757
$order = $dbi->order;
758
$order->prepend(['table1-key1'], [qw/table1-key2 desc/]);
759
$result = $dbi->select(table => 'table1',
760
  column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']],
761
  append => "$order");
762
is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3},
763
  {'table1-key1' => 1, 'table1-key2' => 1},
764
  {'table1-key1' => 2, 'table1-key2' => 4},
765
  {'table1-key1' => 2, 'table1-key2' => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
766

            
cleanup test
Yuki Kimoto authored on 2011-08-10
767
test 'tag_parse';
test cleanup
Yuki Kimoto authored on 2011-08-10
768
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
769
$dbi->tag_parse(0);
770
eval { $dbi->execute('drop table table1') };
771
$dbi->execute("create table table1 (key1, key2)");
772
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
773
eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})};
774
ok($@);
cleanup test
Yuki Kimoto authored on 2011-08-06
775

            
cleanup test
Yuki Kimoto authored on 2011-08-10
776
test 'last_sql';
test cleanup
Yuki Kimoto authored on 2011-08-10
777
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
778
eval { $dbi->execute('drop table table1') };
779
$dbi->execute("create table table1 (key1, key2)");
780
$dbi->execute('select * from table1');
781
is($dbi->last_sql, 'select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
782

            
cleanup test
Yuki Kimoto authored on 2011-08-10
783
eval{$dbi->execute("aaa")};
784
is($dbi->last_sql, 'aaa;');
cleanup test
Yuki Kimoto authored on 2011-08-06
785

            
cleanup test
Yuki Kimoto authored on 2011-08-10
786
test 'DBIx::Custom header';
test cleanup
Yuki Kimoto authored on 2011-08-10
787
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
788
eval { $dbi->execute('drop table table1') };
789
$dbi->execute("create table table1 (key1, key2)");
790
$result = $dbi->execute('select key1 as h1, key2 as h2 from table1');
791
is_deeply($result->header, [qw/h1 h2/]);
cleanup test
Yuki Kimoto authored on 2011-08-06
792

            
cleanup test
Yuki Kimoto authored on 2011-08-10
793
test 'Named placeholder :name(operater) syntax';
794
$dbi->execute('drop table table1');
795
$dbi->execute($create_table1_2);
796
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
797
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
cleanup test
Yuki Kimoto authored on 2011-08-06
798

            
cleanup test
Yuki Kimoto authored on 2011-08-10
799
$source = "select * from table1 where :key1{=} and :key2{=}";
800
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
801
$rows = $result->all;
802
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
803

            
cleanup test
Yuki Kimoto authored on 2011-08-10
804
$source = "select * from table1 where :key1{ = } and :key2{=}";
805
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
806
$rows = $result->all;
807
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
808

            
cleanup test
Yuki Kimoto authored on 2011-08-10
809
$source = "select * from table1 where :key1{<} and :key2{=}";
810
$result = $dbi->execute($source, param => {key1 => 5, key2 => 2});
811
$rows = $result->all;
812
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
813

            
cleanup test
Yuki Kimoto authored on 2011-08-10
814
$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}";
815
$result = $dbi->execute(
816
    $source,
817
    param => {'table1.key1' => 1, 'table1.key2' => 1},
818
    filter => {'table1.key2' => sub { $_[0] * 2 }}
cleanup test
Yuki Kimoto authored on 2011-08-06
819
);
cleanup test
Yuki Kimoto authored on 2011-08-10
820
$rows = $result->all;
821
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
822

            
cleanup test
Yuki Kimoto authored on 2011-08-10
823
test 'high perfomance way';
824
$dbi->execute('drop table table1');
825
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
826
$rows = [
827
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
828
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
829
];
830
{
831
    my $query;
832
    foreach my $row (@$rows) {
833
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
834
      $dbi->execute($query, $row, filter => {ab => sub { $_[0] * 2 }});
cleanup test
Yuki Kimoto authored on 2011-08-06
835
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
836
    is_deeply($dbi->select(table => 'table1')->all,
837
      [
838
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
839
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
840
      ]
841
    );
842
}
843

            
844
$dbi->execute('drop table table1');
845
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
846
$rows = [
847
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
848
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
849
];
850
{
851
    my $query;
852
    my $sth;
853
    foreach my $row (@$rows) {
854
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
855
      $sth ||= $query->sth;
856
      $sth->execute(map { $row->{$_} } sort keys %$row);
cleanup test
Yuki Kimoto authored on 2011-08-06
857
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
858
    is_deeply($dbi->select(table => 'table1')->all,
859
      [
860
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
861
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
862
      ]
863
    );
864
}
cleanup test
Yuki Kimoto authored on 2011-08-06
865

            
cleanup test
Yuki Kimoto authored on 2011-08-10
866
test 'result';
test cleanup
Yuki Kimoto authored on 2011-08-10
867
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
868
eval { $dbi->execute('drop table table1') };
869
$dbi->execute($create_table1);
870
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
871
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
872

            
cleanup test
Yuki Kimoto authored on 2011-08-06
873
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
874
@rows = ();
875
while (my $row = $result->fetch) {
876
    push @rows, [@$row];
877
}
878
is_deeply(\@rows, [[1, 2], [3, 4]]);
cleanup test
Yuki Kimoto authored on 2011-08-06
879

            
880
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
881
@rows = ();
882
while (my $row = $result->fetch_hash) {
883
    push @rows, {%$row};
884
}
885
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
886

            
887
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
888
$row = $result->fetch_first;
889
is_deeply($row, [1, 2], "row");
890
$row = $result->fetch;
891
ok(!$row, "finished");
892

            
cleanup test
Yuki Kimoto authored on 2011-08-06
893
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
894
$row = $result->fetch_hash_first;
895
is_deeply($row, {key1 => 1, key2 => 2}, "row");
896
$row = $result->fetch_hash;
897
ok(!$row, "finished");
898

            
899
$dbi->execute('create table table2 (key1, key2);');
900
$result = $dbi->select(table => 'table2');
901
$row = $result->fetch_hash_first;
902
ok(!$row, "no row fetch");
cleanup test
Yuki Kimoto authored on 2011-08-06
903

            
test cleanup
Yuki Kimoto authored on 2011-08-10
904
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
905
eval { $dbi->execute('drop table table1') };
906
$dbi->execute($create_table1);
907
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
908
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
909
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1');
910
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1');
911
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
912
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
913
$rows = $result->fetch_multi(2);
914
is_deeply($rows, [[1, 2],
915
                  [3, 4]], "fetch_multi first");
916
$rows = $result->fetch_multi(2);
917
is_deeply($rows, [[5, 6],
918
                  [7, 8]], "fetch_multi secound");
919
$rows = $result->fetch_multi(2);
920
is_deeply($rows, [[9, 10]], "fetch_multi third");
921
$rows = $result->fetch_multi(2);
922
ok(!$rows);
923

            
cleanup test
Yuki Kimoto authored on 2011-08-06
924
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
925
eval {$result->fetch_multi};
926
like($@, qr/Row count must be specified/, "Not specified row count");
cleanup test
Yuki Kimoto authored on 2011-08-06
927

            
928
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
929
$rows = $result->fetch_hash_multi(2);
930
is_deeply($rows, [{key1 => 1, key2 => 2},
931
                  {key1 => 3, key2 => 4}], "fetch_multi first");
932
$rows = $result->fetch_hash_multi(2);
933
is_deeply($rows, [{key1 => 5, key2 => 6},
934
                  {key1 => 7, key2 => 8}], "fetch_multi secound");
935
$rows = $result->fetch_hash_multi(2);
936
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third");
937
$rows = $result->fetch_hash_multi(2);
938
ok(!$rows);
939

            
cleanup test
Yuki Kimoto authored on 2011-08-06
940
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
941
eval {$result->fetch_hash_multi};
942
like($@, qr/Row count must be specified/, "Not specified row count");
cleanup test
Yuki Kimoto authored on 2011-08-06
943

            
test cleanup
Yuki Kimoto authored on 2011-08-10
944
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
945
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
946
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
947
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
948
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
949

            
cleanup test
Yuki Kimoto authored on 2011-08-10
950
test 'fetch_all';
951
$result = $dbi->select(table => 'table1');
952
$rows = $result->fetch_all;
953
is_deeply($rows, [[1, 2], [3, 4]]);
cleanup test
Yuki Kimoto authored on 2011-08-06
954

            
cleanup test
Yuki Kimoto authored on 2011-08-10
955
$result = $dbi->select(table => 'table1');
956
$rows = $result->fetch_hash_all;
957
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
958

            
cleanup test
Yuki Kimoto authored on 2011-08-10
959
$result = $dbi->select(table => 'table1');
960
$result->dbi->filters({three_times => sub { $_[0] * 3}});
961
$result->filter({key1 => 'three_times'});
cleanup test
Yuki Kimoto authored on 2011-08-06
962

            
cleanup test
Yuki Kimoto authored on 2011-08-10
963
$rows = $result->fetch_all;
964
is_deeply($rows, [[3, 2], [9, 4]], "array");
cleanup test
Yuki Kimoto authored on 2011-08-06
965

            
cleanup test
Yuki Kimoto authored on 2011-08-10
966
$result = $dbi->select(table => 'table1');
967
$result->dbi->filters({three_times => sub { $_[0] * 3}});
968
$result->filter({key1 => 'three_times'});
969
$rows = $result->fetch_hash_all;
970
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash");
971

            
972
test "query_builder";
973
$datas = [
974
    # Basic tests
975
    {   name            => 'placeholder basic',
976
        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
977
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;",
978
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
979
    },
980
    {
981
        name            => 'placeholder in',
982
        source            => "{in k1 3};",
983
        sql_expected    => "k1 in (?, ?, ?);",
984
        columns_expected   => [qw/k1 k1 k1/]
985
    },
986
    
987
    # Table name
988
    {
989
        name            => 'placeholder with table name',
990
        source            => "{= a.k1} {= a.k2}",
991
        sql_expected    => "a.k1 = ? a.k2 = ?;",
992
        columns_expected  => [qw/a.k1 a.k2/]
993
    },
994
    {   
995
        name            => 'placeholder in with table name',
996
        source            => "{in a.k1 2} {in b.k2 2}",
997
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?);",
998
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
999
    },
1000
    {
1001
        name            => 'not contain tag',
1002
        source            => "aaa",
1003
        sql_expected    => "aaa;",
1004
        columns_expected  => [],
1005
    }
1006
];
1007

            
1008
for (my $i = 0; $i < @$datas; $i++) {
1009
    my $data = $datas->[$i];
1010
    my $builder = DBIx::Custom->new->query_builder;
1011
    my $query = $builder->build_query($data->{source});
1012
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
1013
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
1014
}
1015

            
1016
$builder = DBIx::Custom->new->query_builder;
1017
$ret_val = $builder->register_tag(
1018
    p => sub {
1019
        my @args = @_;
1020
        
1021
        my $expand    = "? $args[0] $args[1]";
1022
        my $columns = [2];
1023
        return [$expand, $columns];
1024
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
1025
);
1026

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1027
$query = $builder->build_query("{p a b}");
1028
is($query->{sql}, "? a b;", "register_tag sql");
1029
is_deeply($query->{columns}, [2], "register_tag columns");
1030
isa_ok($ret_val, 'DBIx::Custom::QueryBuilder');
cleanup test
Yuki Kimoto authored on 2011-08-06
1031

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1032
$builder = DBIx::Custom->new->query_builder;
cleanup test
Yuki Kimoto authored on 2011-08-06
1033

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1034
eval{$builder->build_query('{? }')};
1035
like($@, qr/\QColumn name must be specified in tag "{? }"/, "? not arguments");
cleanup test
Yuki Kimoto authored on 2011-08-06
1036

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1037
eval{$builder->build_query("{a }")};
1038
like($@, qr/\QTag "a" is not registered/, "tag not exist");
cleanup test
Yuki Kimoto authored on 2011-08-06
1039

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1040
$builder->register_tag({
1041
    q => 'string'
1042
});
cleanup test
Yuki Kimoto authored on 2011-08-06
1043

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1044
eval{$builder->build_query("{q}", {})};
1045
like($@, qr/Tag "q" must be sub reference/, "tag not code ref");
cleanup test
Yuki Kimoto authored on 2011-08-06
1046

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1047
$builder->register_tag({
1048
   r => sub {} 
1049
});
cleanup test
Yuki Kimoto authored on 2011-08-06
1050

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1051
eval{$builder->build_query("{r}")};
1052
like($@, qr/\QTag "r" must return [STRING, ARRAY_REFERENCE]/, "tag return noting");
1053

            
1054
$builder->register_tag({
1055
   s => sub { return ["a", ""]} 
1056
});
1057

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

            
1061
$builder->register_tag(
1062
    t => sub {return ["a", []]}
cleanup test
Yuki Kimoto authored on 2011-08-06
1063
);
1064

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

            
1066
test 'General error case';
1067
$builder = DBIx::Custom->new->query_builder;
1068
$builder->register_tag(
1069
    a => sub {
1070
        return ["? ? ?", ['']];
1071
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
1072
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1073
eval{$builder->build_query("{a}")};
1074
like($@, qr/\QPlaceholder count/, "placeholder count is invalid");
cleanup test
Yuki Kimoto authored on 2011-08-06
1075

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

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

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

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

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

            
1092
test 'variouse source';
1093
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;";
1094
$query = $builder->build_query($source);
1095
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1");
1096

            
1097
$source = "abc;";
1098
$query = $builder->build_query($source);
1099
is($query->sql, 'abc;', "basic : 2");
1100

            
1101
$source = "{= a}";
1102
$query = $builder->build_query($source);
1103
is($query->sql, 'a = ?;', "only tag");
1104

            
1105
$source = "000;";
1106
$query = $builder->build_query($source);
1107
is($query->sql, '000;', "contain 0 value");
1108

            
1109
$source = "a {= b} }";
1110
eval{$builder->build_query($source)};
1111
like($@, qr/unexpected "}"/, "error : 1");
1112

            
1113
$source = "a {= {}";
1114
eval{$builder->build_query($source)};
1115
like($@, qr/unexpected "{"/, "error : 2");
1116

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

            
1118

            
1119

            
1120

            
1121

            
1122

            
1123

            
1124

            
1125

            
1126

            
1127

            
1128

            
1129

            
1130

            
1131

            
1132

            
1133

            
1134

            
1135

            
1136

            
1137

            
1138

            
1139

            
1140

            
1141

            
1142

            
1143
### a little complex test
cleanup test
Yuki Kimoto authored on 2011-08-10
1144
test 'dbi method from model';
1145
$dbi = MyDBI9->connect;
1146
eval { $dbi->execute('drop table table1') };
1147
$dbi->execute($create_table1);
1148
$dbi->setup_model;
1149
$model = $dbi->model('table1');
1150
eval{$model->execute('select * from table1')};
1151
ok(!$@);
1152

            
1153
test 'column table option';
1154
$dbi = MyDBI9->connect;
1155
eval { $dbi->execute('drop table table1') };
1156
$dbi->execute($create_table1);
1157
eval { $dbi->execute('drop table table2') };
1158
$dbi->execute($create_table2);
1159
$dbi->setup_model;
1160
$dbi->execute('insert into table1 (key1, key2) values (1, 2);');
1161
$dbi->execute('insert into table2 (key1, key3) values (1, 4);');
1162
$model = $dbi->model('table1');
1163
$result = $model->select(
1164
    column => [
1165
        $model->column('table2', {alias => 'table2_alias'})
1166
    ],
1167
    where => {'table2_alias.key3' => 4}
1168
);
1169
is_deeply($result->one, 
1170
          {'table2_alias.key1' => 1, 'table2_alias.key3' => 4});
1171

            
1172
$dbi->separator('__');
1173
$result = $model->select(
1174
    column => [
1175
        $model->column('table2', {alias => 'table2_alias'})
1176
    ],
1177
    where => {'table2_alias.key3' => 4}
1178
);
1179
is_deeply($result->one, 
1180
          {'table2_alias__key1' => 1, 'table2_alias__key3' => 4});
1181

            
1182
$dbi->separator('-');
1183
$result = $model->select(
1184
    column => [
1185
        $model->column('table2', {alias => 'table2_alias'})
1186
    ],
1187
    where => {'table2_alias.key3' => 4}
1188
);
1189
is_deeply($result->one, 
1190
          {'table2_alias-key1' => 1, 'table2_alias-key3' => 4});
1191

            
1192
test 'create_model';
1193
$dbi = DBIx::Custom->connect;
1194
eval { $dbi->execute('drop table table1') };
1195
eval { $dbi->execute('drop table table2') };
1196
$dbi->execute($create_table1);
1197
$dbi->execute($create_table2);
1198

            
1199
$dbi->create_model(
1200
    table => 'table1',
1201
    join => [
1202
       'left outer join table2 on table1.key1 = table2.key1'
1203
    ],
1204
    primary_key => ['key1']
1205
);
1206
$model2 = $dbi->create_model(
1207
    table => 'table2'
1208
);
1209
$dbi->create_model(
1210
    table => 'table3',
1211
    filter => [
1212
        key1 => {in => sub { uc $_[0] }}
1213
    ]
1214
);
1215
$dbi->setup_model;
1216
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1217
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1218
$model = $dbi->model('table1');
1219
$result = $model->select(
1220
    column => [$model->mycolumn, $model->column('table2')],
1221
    where => {'table1.key1' => 1}
1222
);
1223
is_deeply($result->one,
1224
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
1225
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
1226

            
1227
test 'model method';
1228
test 'create_model';
1229
$dbi = DBIx::Custom->connect;
1230
eval { $dbi->execute('drop table table2') };
1231
$dbi->execute($create_table2);
1232
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1233
$model = $dbi->create_model(
1234
    table => 'table2'
1235
);
1236
$model->method(foo => sub { shift->select(@_) });
1237
is_deeply($model->foo->one, {key1 => 1, key3 => 3});
1238

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1239
test 'join';
1240
$dbi = DBIx::Custom->connect;
1241
eval { $dbi->execute('drop table table1') };
1242
$dbi->execute($create_table1);
1243
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1244
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1245
$dbi->execute($create_table2);
1246
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
1247
$dbi->execute('create table table3 (key3 int, key4 int);');
1248
$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4});
1249
$rows = $dbi->select(
1250
    table => 'table1',
1251
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
1252
    where   => {'table1.key2' => 2},
1253
    join  => ['left outer join table2 on table1.key1 = table2.key1']
1254
)->all;
1255
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]);
1256

            
1257
$rows = $dbi->select(
1258
    table => 'table1',
1259
    where   => {'key1' => 1},
1260
    join  => ['left outer join table2 on table1.key1 = table2.key1']
1261
)->all;
1262
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1263

            
1264
eval {
1265
    $rows = $dbi->select(
1266
        table => 'table1',
1267
        column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
1268
        where   => {'table1.key2' => 2},
1269
        join  => {'table1.key1' => 'table2.key1'}
1270
    );
1271
};
1272
like ($@, qr/array/);
1273

            
1274
$rows = $dbi->select(
1275
    table => 'table1',
1276
    where   => {'key1' => 1},
1277
    join  => ['left outer join table2 on table1.key1 = table2.key1',
1278
              'left outer join table3 on table2.key3 = table3.key3']
1279
)->all;
1280
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1281

            
1282
$rows = $dbi->select(
1283
    column => 'table3.key4 as table3__key4',
1284
    table => 'table1',
1285
    where   => {'table1.key1' => 1},
1286
    join  => ['left outer join table2 on table1.key1 = table2.key1',
1287
              'left outer join table3 on table2.key3 = table3.key3']
1288
)->all;
1289
is_deeply($rows, [{table3__key4 => 4}]);
1290

            
1291
$rows = $dbi->select(
1292
    column => 'table1.key1 as table1__key1',
1293
    table => 'table1',
1294
    where   => {'table3.key4' => 4},
1295
    join  => ['left outer join table2 on table1.key1 = table2.key1',
1296
              'left outer join table3 on table2.key3 = table3.key3']
1297
)->all;
1298
is_deeply($rows, [{table1__key1 => 1}]);
1299

            
1300
$dbi = DBIx::Custom->connect;
1301
$dbi->quote('"');
1302
eval { $dbi->execute('drop table table1') };
1303
$dbi->execute($create_table1);
1304
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1305
$dbi->execute($create_table2);
1306
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
1307
$rows = $dbi->select(
1308
    table => 'table1',
1309
    column => '"table1"."key1" as "table1_key1", "table2"."key1" as "table2_key1", "key2", "key3"',
1310
    where   => {'table1.key2' => 2},
1311
    join  => ['left outer join "table2" on "table1"."key1" = "table2"."key1"'],
1312
)->all;
1313
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}],
1314
          'quote');
1315

            
1316

            
1317
$dbi = DBIx::Custom->connect;
1318
eval { $dbi->execute('drop table table1') };
1319
$dbi->execute($create_table1);
1320
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1321
$sql = <<"EOS";
1322
left outer join (
1323
  select * from table1 as t1
1324
  where t1.key2 = (
1325
    select max(t2.key2) from table1 as t2
1326
    where t1.key1 = t2.key1
1327
  )
1328
) as latest_table1 on table1.key1 = latest_table1.key1
1329
EOS
1330
$join = [$sql];
1331
$rows = $dbi->select(
1332
    table => 'table1',
1333
    column => 'latest_table1.key1 as latest_table1__key1',
1334
    join  => $join
1335
)->all;
1336
is_deeply($rows, [{latest_table1__key1 => 1}]);
1337

            
1338
$dbi = DBIx::Custom->connect;
1339
eval { $dbi->execute('drop table table1') };
1340
eval { $dbi->execute('drop table table2') };
1341
$dbi->execute($create_table1);
1342
$dbi->execute($create_table2);
1343
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1344
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
1345
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
1346
$result = $dbi->select(
1347
    table => 'table1',
1348
    join => [
1349
        "left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1"
1350
    ]
1351
);
1352
is_deeply($result->all, [{key1 => 1, key2 => 2}]);
1353
$result = $dbi->select(
1354
    table => 'table1',
1355
    column => [{table2 => ['key3']}],
1356
    join => [
1357
        "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1"
1358
    ]
1359
);
1360
is_deeply($result->all, [{'table2.key3' => 4}]);
1361
$result = $dbi->select(
1362
    table => 'table1',
1363
    column => [{table2 => ['key3']}],
1364
    join => [
1365
        "left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'"
1366
    ]
1367
);
1368
is_deeply($result->all, [{'table2.key3' => 4}]);
1369

            
1370
$dbi = DBIx::Custom->connect;
1371
eval { $dbi->execute('drop table table1') };
1372
eval { $dbi->execute('drop table table2') };
1373
$dbi->execute($create_table1);
1374
$dbi->execute($create_table2);
1375
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1376
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
1377
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
1378
$result = $dbi->select(
1379
    table => 'table1',
1380
    column => [{table2 => ['key3']}],
1381
    join => [
1382
        {
1383
            clause => "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1",
1384
            table => ['table1', 'table2']
1385
        }
1386
    ]
1387
);
1388
is_deeply($result->all, [{'table2.key3' => 4}]);
1389

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

            
1391
test 'update_param';
1392
$dbi = DBIx::Custom->connect;
1393
eval { $dbi->execute('drop table table1') };
1394
$dbi->execute($create_table1_2);
1395
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1396
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1397

            
1398
$param = {key2 => 11};
1399
$update_param = $dbi->update_param($param);
1400
$sql = <<"EOS";
1401
update table1 $update_param
1402
where key1 = 1
1403
EOS
1404
$dbi->execute($sql, param => $param);
1405
$result = $dbi->execute('select * from table1;', table => 'table1');
1406
$rows   = $result->all;
1407
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
1408
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1409
                  "basic");
1410

            
1411

            
1412
$dbi = DBIx::Custom->connect;
1413
eval { $dbi->execute('drop table table1') };
1414
$dbi->execute($create_table1_2);
1415
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1416
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1417

            
1418
$param = {key2 => 11, key3 => 33};
1419
$update_param = $dbi->update_param($param);
1420
$sql = <<"EOS";
1421
update table1 $update_param
1422
where key1 = 1
1423
EOS
1424
$dbi->execute($sql, param => $param);
1425
$result = $dbi->execute('select * from table1;', table => 'table1');
1426
$rows   = $result->all;
1427
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
1428
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1429
                  "basic");
1430

            
1431
$dbi = DBIx::Custom->connect;
1432
eval { $dbi->execute('drop table table1') };
1433
$dbi->execute($create_table1_2);
1434
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1435
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1436

            
1437
$param = {key2 => 11, key3 => 33};
1438
$update_param = $dbi->update_param($param, {no_set => 1});
1439
$sql = <<"EOS";
1440
update table1 set $update_param
1441
where key1 = 1
1442
EOS
1443
$dbi->execute($sql, param => $param);
1444
$result = $dbi->execute('select * from table1;', table => 'table1');
1445
$rows   = $result->all;
1446
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
1447
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1448
                  "update param no_set");
1449

            
1450
            
1451
eval { $dbi->update_param({";" => 1}) };
1452
like($@, qr/not safety/);
1453

            
1454

            
1455
test 'update_param';
1456
$dbi = DBIx::Custom->connect;
1457
eval { $dbi->execute('drop table table1') };
1458
$dbi->execute($create_table1_2);
1459
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1460
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1461

            
1462
$param = {key2 => 11};
1463
$update_param = $dbi->assign_param($param);
1464
$sql = <<"EOS";
1465
update table1 set $update_param
1466
where key1 = 1
1467
EOS
1468
$dbi->execute($sql, param => $param, table => 'table1');
1469
$result = $dbi->execute('select * from table1;');
1470
$rows   = $result->all;
1471
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
1472
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1473
                  "basic");
1474

            
1475

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1476
test 'type option'; # DEPRECATED!
1477
$dbi = DBIx::Custom->connect(
1478
    data_source => 'dbi:SQLite:dbname=:memory:',
1479
    dbi_option => {
1480
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
1481
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
1482
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1483
$binary = pack("I3", 1, 2, 3);
1484
eval { $dbi->execute('drop table table1') };
1485
$dbi->execute('create table table1(key1, key2)');
1486
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
1487
$result = $dbi->select(table => 'table1');
1488
$row   = $result->one;
1489
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
1490
$result = $dbi->execute('select length(key1) as key1_length from table1');
1491
$row = $result->one;
1492
is($row->{key1_length}, length $binary);
cleanup test
Yuki Kimoto authored on 2011-08-06
1493

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1494
test 'type_rule from';
1495
$dbi = DBIx::Custom->connect;
1496
$dbi->type_rule(
1497
    from1 => {
1498
        date => sub { uc $_[0] }
1499
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
1500
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1501
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1502
$dbi->insert({key1 => 'a'}, table => 'table1');
1503
$result = $dbi->select(table => 'table1');
1504
is($result->fetch_first->[0], 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
1505

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1506
$result = $dbi->select(table => 'table1');
1507
is($result->one->{key1}, 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
1508

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

            
1510
test 'type_rule into';
test cleanup
Yuki Kimoto authored on 2011-08-10
1511
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1512
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1513
$dbi->type_rule(
1514
    into1 => {
1515
        date => sub { uc $_[0] }
1516
    }
1517
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1518
$dbi->insert({key1 => 'a'}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
1519
$result = $dbi->select(table => 'table1');
1520
is($result->one->{key1}, 'A');
1521

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1522
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1523
$dbi->execute("create table table1 (key1 date, key2 datetime)");
1524
$dbi->type_rule(
1525
    into1 => [
1526
         [qw/date datetime/] => sub { uc $_[0] }
1527
    ]
1528
);
1529
$dbi->insert({key1 => 'a', key2 => 'b'}, table => 'table1');
1530
$result = $dbi->select(table => 'table1');
1531
$row = $result->one;
1532
is($row->{key1}, 'A');
1533
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
1534

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1535
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1536
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1537
$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1');
1538
$dbi->type_rule(
1539
    into1 => [
1540
        [qw/date datetime/] => sub { uc $_[0] }
1541
    ]
1542
);
1543
$result = $dbi->execute(
1544
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
1545
    param => {key1 => 'a', 'table1.key2' => 'b'}
1546
);
1547
$row = $result->one;
1548
is($row->{key1}, 'a');
1549
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
1550

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1551
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1552
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1553
$dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1');
1554
$dbi->type_rule(
1555
    into1 => [
1556
        [qw/date datetime/] => sub { uc $_[0] }
1557
    ]
1558
);
1559
$result = $dbi->execute(
1560
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
1561
    param => {key1 => 'a', 'table1.key2' => 'b'},
1562
    table => 'table1'
1563
);
1564
$row = $result->one;
1565
is($row->{key1}, 'A');
1566
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
1567

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1568
$dbi = DBIx::Custom->connect;
1569
$dbi->execute("create table table1 (key1 date, key2 datetime)");
1570
$dbi->register_filter(twice => sub { $_[0] * 2 });
1571
$dbi->type_rule(
1572
    from1 => {
1573
        date => 'twice',
1574
    },
1575
    into1 => {
1576
        date => 'twice',
1577
    }
1578
);
1579
$dbi->insert({key1 => 2}, table => 'table1');
1580
$result = $dbi->select(table => 'table1');
1581
is($result->fetch->[0], 8);
cleanup test
Yuki Kimoto authored on 2011-08-06
1582

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1583
test 'type_rule and filter order';
test cleanup
Yuki Kimoto authored on 2011-08-10
1584
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1585
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1586
$dbi->type_rule(
1587
    into1 => {
1588
        date => sub { $_[0] . 'b' }
1589
    },
1590
    into2 => {
1591
        date => sub { $_[0] . 'c' }
1592
    },
1593
    from1 => {
1594
        date => sub { $_[0] . 'd' }
1595
    },
1596
    from2 => {
1597
        date => sub { $_[0] . 'e' }
1598
    }
1599
);
1600
$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }});
1601
$result = $dbi->select(table => 'table1');
1602
$result->filter(key1 => sub { $_[0] . 'f' });
1603
is($result->fetch_first->[0], '1abcdef');
cleanup test
Yuki Kimoto authored on 2011-08-06
1604

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1605
$dbi = DBIx::Custom->connect;
1606
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1607
$dbi->type_rule(
1608
    from1 => {
1609
        date => sub { $_[0] . 'p' }
1610
    },
1611
    from2 => {
1612
        date => sub { $_[0] . 'q' }
1613
    },
1614
);
1615
$dbi->insert({key1 => '1'}, table => 'table1');
1616
$result = $dbi->select(table => 'table1');
1617
$result->type_rule(
1618
    from1 => {
1619
        date => sub { $_[0] . 'd' }
1620
    },
1621
    from2 => {
1622
        date => sub { $_[0] . 'e' }
1623
    }
1624
);
1625
$result->filter(key1 => sub { $_[0] . 'f' });
1626
is($result->fetch_first->[0], '1def');
cleanup test
Yuki Kimoto authored on 2011-08-06
1627

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1628
test 'type_rule_off';
1629
$dbi = DBIx::Custom->connect;
1630
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1631
$dbi->type_rule(
1632
    from1 => {
1633
        date => sub { $_[0] * 2 },
1634
    },
1635
    into1 => {
1636
        date => sub { $_[0] * 2 },
1637
    }
1638
);
1639
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
1640
$result = $dbi->select(table => 'table1', type_rule_off => 1);
1641
is($result->type_rule_off->fetch->[0], 2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1642

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1643
$dbi = DBIx::Custom->connect;
1644
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1645
$dbi->type_rule(
1646
    from1 => {
1647
        date => sub { $_[0] * 2 },
1648
    },
1649
    into1 => {
1650
        date => sub { $_[0] * 3 },
1651
    }
1652
);
1653
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
1654
$result = $dbi->select(table => 'table1', type_rule_off => 1);
1655
is($result->one->{key1}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-06
1656

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1657
$dbi = DBIx::Custom->connect;
1658
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1659
$dbi->type_rule(
1660
    from1 => {
1661
        date => sub { $_[0] * 2 },
1662
    },
1663
    into1 => {
1664
        date => sub { $_[0] * 3 },
1665
    }
1666
);
1667
$dbi->insert({key1 => 2}, table => 'table1');
1668
$result = $dbi->select(table => 'table1');
1669
is($result->one->{key1}, 12);
cleanup test
Yuki Kimoto authored on 2011-08-06
1670

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1671
$dbi = DBIx::Custom->connect;
1672
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1673
$dbi->type_rule(
1674
    from1 => {
1675
        date => sub { $_[0] * 2 },
1676
    },
1677
    into1 => {
1678
        date => sub { $_[0] * 3 },
1679
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
1680
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1681
$dbi->insert({key1 => 2}, table => 'table1');
1682
$result = $dbi->select(table => 'table1');
1683
is($result->fetch->[0], 12);
cleanup test
Yuki Kimoto authored on 2011-08-06
1684

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1685
$dbi = DBIx::Custom->connect;
1686
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1687
$dbi->register_filter(ppp => sub { uc $_[0] });
1688
$dbi->type_rule(
1689
    into1 => {
1690
        date => 'ppp'
cleanup test
Yuki Kimoto authored on 2011-08-06
1691
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
1692
);
1693
$dbi->insert({key1 => 'a'}, table => 'table1');
1694
$result = $dbi->select(table => 'table1');
1695
is($result->one->{key1}, 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
1696

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1697
eval{$dbi->type_rule(
1698
    into1 => {
1699
        date => 'pp'
cleanup test
Yuki Kimoto authored on 2011-08-06
1700
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
1701
)};
1702
like($@, qr/not registered/);
cleanup test
Yuki Kimoto authored on 2011-08-06
1703

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1704
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1705
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1706
eval {
1707
    $dbi->type_rule(
1708
        from1 => {
1709
            Date => sub { $_[0] * 2 },
1710
        }
1711
    );
1712
};
1713
like($@, qr/lower/);
clenup test
Yuki Kimoto authored on 2011-08-06
1714

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1715
eval {
1716
    $dbi->type_rule(
1717
        into1 => {
1718
            Date => sub { $_[0] * 2 },
1719
        }
1720
    );
1721
};
1722
like($@, qr/lower/);
clenup test
Yuki Kimoto authored on 2011-08-06
1723

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1724
$dbi = DBIx::Custom->connect;
1725
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1726
$dbi->type_rule(
1727
    from1 => {
1728
        date => sub { $_[0] * 2 },
1729
    },
1730
    into1 => {
1731
        date => sub { $_[0] * 3 },
1732
    }
1733
);
1734
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
1735
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1736
$result->type_rule_off;
1737
is($result->one->{key1}, 6);
clenup test
Yuki Kimoto authored on 2011-08-06
1738

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1739
$dbi = DBIx::Custom->connect;
1740
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1741
$dbi->type_rule(
1742
    from1 => {
1743
        date => sub { $_[0] * 2 },
1744
        datetime => sub { $_[0] * 4 },
1745
    },
1746
);
1747
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
1748
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1749
$result->type_rule(
1750
    from1 => {
1751
        date => sub { $_[0] * 3 }
1752
    }
1753
);
1754
$row = $result->one;
1755
is($row->{key1}, 6);
1756
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
1757

            
1758
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1759
$result->type_rule(
1760
    from1 => {
1761
        date => sub { $_[0] * 3 }
1762
    }
1763
);
1764
$row = $result->one;
1765
is($row->{key1}, 6);
1766
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
1767

            
1768
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1769
$result->type_rule(
1770
    from1 => {
1771
        date => sub { $_[0] * 3 }
1772
    }
1773
);
1774
$row = $result->one;
1775
is($row->{key1}, 6);
1776
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
1777
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1778
$result->type_rule(
1779
    from1 => [date => sub { $_[0] * 3 }]
1780
);
1781
$row = $result->one;
1782
is($row->{key1}, 6);
1783
is($row->{key2}, 2);
1784
$dbi->register_filter(fivetimes => sub { $_[0] * 5});
clenup test
Yuki Kimoto authored on 2011-08-06
1785
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1786
$result->type_rule(
1787
    from1 => [date => 'fivetimes']
1788
);
1789
$row = $result->one;
1790
is($row->{key1}, 10);
1791
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
1792
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1793
$result->type_rule(
1794
    from1 => [date => undef]
1795
);
1796
$row = $result->one;
1797
is($row->{key1}, 2);
1798
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
1799

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1800
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1801
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1802
$dbi->type_rule(
1803
    from1 => {
1804
        date => sub { $_[0] * 2 },
1805
    },
1806
);
1807
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
1808
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1809
$result->filter(key1 => sub { $_[0] * 3 });
1810
is($result->one->{key1}, 12);
clenup test
Yuki Kimoto authored on 2011-08-06
1811

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1812
$dbi = DBIx::Custom->connect;
1813
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1814
$dbi->type_rule(
1815
    from1 => {
1816
        date => sub { $_[0] * 2 },
1817
    },
1818
);
1819
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
1820
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1821
$result->filter(key1 => sub { $_[0] * 3 });
1822
is($result->fetch->[0], 12);
clenup test
Yuki Kimoto authored on 2011-08-06
1823

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1824
$dbi = DBIx::Custom->connect;
1825
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1826
$dbi->type_rule(
1827
    into1 => {
1828
        date => sub { $_[0] . 'b' }
1829
    },
1830
    into2 => {
1831
        date => sub { $_[0] . 'c' }
1832
    },
1833
    from1 => {
1834
        date => sub { $_[0] . 'd' }
1835
    },
1836
    from2 => {
1837
        date => sub { $_[0] . 'e' }
1838
    }
1839
);
1840
$dbi->insert({key1 => '1'}, table => 'table1', type_rule_off => 1);
clenup test
Yuki Kimoto authored on 2011-08-06
1841
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1842
is($result->type_rule_off->fetch_first->[0], '1');
clenup test
Yuki Kimoto authored on 2011-08-06
1843
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1844
is($result->type_rule_on->fetch_first->[0], '1de');
clenup test
Yuki Kimoto authored on 2011-08-06
1845

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1846
$dbi = DBIx::Custom->connect;
1847
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1848
$dbi->type_rule(
1849
    into1 => {
1850
        date => sub { $_[0] . 'b' }
cleanup test
Yuki Kimoto authored on 2011-08-06
1851
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
1852
    into2 => {
1853
        date => sub { $_[0] . 'c' }
cleanup test
Yuki Kimoto authored on 2011-08-06
1854
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
1855
    from1 => {
1856
        date => sub { $_[0] . 'd' }
cleanup test
Yuki Kimoto authored on 2011-08-06
1857
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
1858
    from2 => {
1859
        date => sub { $_[0] . 'e' }
cleanup test
Yuki Kimoto authored on 2011-08-06
1860
    }
1861
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1862
$dbi->insert({key1 => '1'}, table => 'table1', type_rule1_off => 1);
1863
$result = $dbi->select(table => 'table1');
1864
is($result->type_rule1_off->fetch_first->[0], '1ce');
1865
$result = $dbi->select(table => 'table1');
1866
is($result->type_rule1_on->fetch_first->[0], '1cde');
cleanup test
Yuki Kimoto authored on 2011-08-06
1867

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1868
$dbi = DBIx::Custom->connect;
1869
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1870
$dbi->type_rule(
1871
    into1 => {
1872
        date => sub { $_[0] . 'b' }
1873
    },
1874
    into2 => {
1875
        date => sub { $_[0] . 'c' }
1876
    },
1877
    from1 => {
1878
        date => sub { $_[0] . 'd' }
1879
    },
1880
    from2 => {
1881
        date => sub { $_[0] . 'e' }
cleanup test
Yuki Kimoto authored on 2011-08-06
1882
    }
1883
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1884
$dbi->insert({key1 => '1'}, table => 'table1', type_rule2_off => 1);
1885
$result = $dbi->select(table => 'table1');
1886
is($result->type_rule2_off->fetch_first->[0], '1bd');
1887
$result = $dbi->select(table => 'table1');
1888
is($result->type_rule2_on->fetch_first->[0], '1bde');
test cleanup
Yuki Kimoto authored on 2011-08-10
1889

            
1890
test 'prefix';
1891
$dbi = DBIx::Custom->connect;
1892
eval { $dbi->execute('drop table table1') };
1893
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
1894
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1895
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace');
1896
$result = $dbi->execute('select * from table1;');
1897
$rows   = $result->all;
1898
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
1899

            
1900
$dbi = DBIx::Custom->connect;
1901
eval { $dbi->execute('drop table table1') };
1902
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
1903
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1904
$dbi->update(table => 'table1', param => {key2 => 4},
1905
  where => {key1 => 1}, prefix => 'or replace');
1906
$result = $dbi->execute('select * from table1;');
1907
$rows   = $result->all;
1908
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
1909

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1910
test 'Model class';
1911
use MyDBI1;
1912
$dbi = MyDBI1->connect;
1913
eval { $dbi->execute('drop table book') };
1914
$dbi->execute("create table book (title, author)");
1915
$model = $dbi->model('book');
1916
$model->insert({title => 'a', author => 'b'});
1917
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
1918
$dbi->execute("create table company (name)");
1919
$model = $dbi->model('company');
1920
$model->insert({name => 'a'});
1921
is_deeply($model->list->all, [{name => 'a'}], 'basic');
1922
is($dbi->models->{'book'}, $dbi->model('book'));
1923
is($dbi->models->{'company'}, $dbi->model('company'));
1924

            
1925
$dbi = MyDBI4->connect;
1926
eval { $dbi->execute('drop table book') };
1927
$dbi->execute("create table book (title, author)");
1928
$model = $dbi->model('book');
1929
$model->insert({title => 'a', author => 'b'});
1930
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
1931
$dbi->execute("create table company (name)");
1932
$model = $dbi->model('company');
1933
$model->insert({name => 'a'});
1934
is_deeply($model->list->all, [{name => 'a'}], 'basic');
test cleanup
Yuki Kimoto authored on 2011-08-10
1935

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1936
$dbi = MyDBI5->connect;
1937
eval { $dbi->execute('drop table company') };
1938
eval { $dbi->execute('drop table table1') };
1939
$dbi->execute("create table company (name)");
1940
$dbi->execute("create table table1 (key1)");
1941
$model = $dbi->model('company');
1942
$model->insert({name => 'a'});
1943
is_deeply($model->list->all, [{name => 'a'}], 'include all model');
1944
$dbi->insert(table => 'table1', param => {key1 => 1});
1945
$model = $dbi->model('book');
1946
is_deeply($model->list->all, [{key1 => 1}], 'include all model');
test cleanup
Yuki Kimoto authored on 2011-08-10
1947

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1948
test 'primary_key';
1949
use MyDBI1;
1950
$dbi = MyDBI1->connect;
1951
$model = $dbi->model('book');
1952
$model->primary_key(['id', 'number']);
1953
is_deeply($model->primary_key, ['id', 'number']);
test cleanup
Yuki Kimoto authored on 2011-08-10
1954

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1955
test 'columns';
1956
use MyDBI1;
1957
$dbi = MyDBI1->connect;
1958
$model = $dbi->model('book');
1959
$model->columns(['id', 'number']);
1960
is_deeply($model->columns, ['id', 'number']);
test cleanup
Yuki Kimoto authored on 2011-08-10
1961

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1962
test 'setup_model';
1963
use MyDBI1;
1964
$dbi = MyDBI1->connect;
1965
eval { $dbi->execute('drop table book') };
1966
eval { $dbi->execute('drop table company') };
1967
eval { $dbi->execute('drop table test') };
test cleanup
Yuki Kimoto authored on 2011-08-10
1968

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1969
$dbi->execute('create table book (id)');
1970
$dbi->execute('create table company (id, name);');
1971
$dbi->execute('create table test (id, name, primary key (id, name));');
1972
$dbi->setup_model;
1973
is_deeply($dbi->model('book')->columns, ['id']);
1974
is_deeply($dbi->model('company')->columns, ['id', 'name']);
test cleanup
Yuki Kimoto authored on 2011-08-10
1975

            
1976

            
1977

            
1978

            
1979

            
1980

            
1981

            
1982

            
1983

            
1984

            
1985

            
1986

            
1987

            
1988

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1989
### SQLite only test
1990
test 'quote';
1991
$dbi = DBIx::Custom->connect;
1992
$dbi->quote('"');
1993
eval { $dbi->execute("drop table ${q}table$p") };
1994
$dbi->execute($create_table_reserved);
1995
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
1996
$dbi->insert(table => 'table', param => {select => 1});
1997
$dbi->delete(table => 'table', where => {select => 1});
1998
$result = $dbi->execute("select * from ${q}table$p");
1999
$rows   = $result->all;
2000
is_deeply($rows, [], "reserved word");
2001

            
2002

            
2003

            
2004

            
2005

            
2006

            
2007

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

            
2009

            
2010

            
2011

            
2012

            
2013

            
2014

            
2015

            
2016

            
2017

            
2018
# DEPRECATED! test
2019
test 'filter __ expression';
2020
$dbi = DBIx::Custom->connect;
2021
eval { $dbi->execute('drop table company') };
2022
eval { $dbi->execute('drop table location') };
2023
$dbi->execute('create table company (id, name, location_id)');
2024
$dbi->execute('create table location (id, name)');
2025
$dbi->apply_filter('location',
2026
  name => {in => sub { uc $_[0] } }
2027
);
2028

            
2029
$dbi->insert(table => 'company', param => {id => 1, name => 'a', location_id => 2});
2030
$dbi->insert(table => 'location', param => {id => 2, name => 'b'});
2031

            
2032
$result = $dbi->select(
2033
    table => ['company', 'location'], relation => {'company.location_id' => 'location.id'},
2034
    column => ['location.name as location__name']
2035
);
2036
is($result->fetch_first->[0], 'B');
2037

            
2038
$result = $dbi->select(
2039
    table => 'company', relation => {'company.location_id' => 'location.id'},
2040
    column => ['location.name as location__name']
2041
);
2042
is($result->fetch_first->[0], 'B');
2043

            
2044
$result = $dbi->select(
2045
    table => 'company', relation => {'company.location_id' => 'location.id'},
2046
    column => ['location.name as "location.name"']
2047
);
2048
is($result->fetch_first->[0], 'B');
test cleanup
Yuki Kimoto authored on 2011-08-10
2049

            
2050
test 'reserved_word_quote';
2051
$dbi = DBIx::Custom->connect;
2052
eval { $dbi->execute("drop table ${q}table$p") };
2053
$dbi->reserved_word_quote('"');
2054
$dbi->execute($create_table_reserved);
2055
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
2056
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
2057
$dbi->insert(table => 'table', param => {select => 1});
2058
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
2059
$result = $dbi->execute("select * from ${q}table$p");
2060
$rows   = $result->all;
2061
is_deeply($rows, [{select => 2, update => 6}], "reserved word");