DBIx-Custom / t / sqlite.t /
Newer Older
2531 lines | 75.961kb
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
{
23
    package DBIx::Custom;
24
    has dsn => sub { 'dbi:SQLite:dbname=:memory:' }
25
}
26

            
cleanup test
Yuki Kimoto authored on 2011-08-06
27
# Constant
cleanup test
Yuki Kimoto authored on 2011-08-10
28
my $create_table1 = 'create table table1 (key1 char(255), key2 char(255));';
29
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
30
my $create_table2 = 'create table table2 (key1 char(255), key3 char(255));';
cleanup test
Yuki Kimoto authored on 2011-08-10
31
my $create_table2_2 = "create table table2 (key1, key2, key3)";
32
my $create_table3 = "create table table3 (key1, key2, key3)";
test cleanup
Yuki Kimoto authored on 2011-08-10
33
my $create_table_reserved = 'create table "table" ("select", "update")';
34

            
test cleanup
Yuki Kimoto authored on 2011-08-10
35
my $q = '"';
36
my $p = '"';
cleanup test
Yuki Kimoto authored on 2011-08-06
37

            
cleanup test
Yuki Kimoto authored on 2011-08-06
38
# Variables
cleanup test
Yuki Kimoto authored on 2011-08-06
39
my $builder;
40
my $datas;
cleanup test
Yuki Kimoto authored on 2011-08-06
41
my $dbi;
42
my $sth;
43
my $source;
44
my @sources;
cleanup test
Yuki Kimoto authored on 2011-08-06
45
my $select_source;
46
my $insert_source;
47
my $update_source;
cleanup test
Yuki Kimoto authored on 2011-08-06
48
my $param;
49
my $params;
50
my $sql;
51
my $result;
52
my $row;
53
my @rows;
54
my $rows;
55
my $query;
56
my @queries;
57
my $select_query;
58
my $insert_query;
59
my $update_query;
60
my $ret_val;
61
my $infos;
62
my $model;
63
my $model2;
64
my $where;
65
my $update_param;
66
my $insert_param;
cleanup test
Yuki Kimoto authored on 2011-08-06
67
my $join;
cleanup test
Yuki Kimoto authored on 2011-08-10
68
my $binary;
cleanup test
Yuki Kimoto authored on 2011-08-06
69

            
70
# Prepare table
test cleanup
Yuki Kimoto authored on 2011-08-10
71
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
72

            
73

            
74

            
75

            
76
test 'more tests';
test cleanup
Yuki Kimoto authored on 2011-08-10
77
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
78
eval{$dbi->apply_filter('table', 'column', [])};
79
like($@, qr/apply_filter/);
80

            
81
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
82
like($@, qr/apply_filter/);
83

            
84
$dbi->apply_filter(
85

            
86
);
test cleanup
Yuki Kimoto authored on 2011-08-10
87
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
88
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
89
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
90
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
91
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
92
$dbi->apply_filter('table1', 'key2', 
93
                   {in => sub { $_[0] * 3 }, out => sub { $_[0] * 2 }});
94
$rows = $dbi->select(table => 'table1', where => {key2 => 1})->all;
95
is_deeply($rows, [{key1 => 1, key2 => 6}]);
96

            
test cleanup
Yuki Kimoto authored on 2011-08-10
97
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
98
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
99
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
100
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
101
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
102
$dbi->apply_filter('table1', 'key2', {});
103
$rows = $dbi->select(table => 'table1', where => {key2 => 2})->all;
104
is_deeply($rows, [{key1 => 1, key2 => 2}]);
105

            
test cleanup
Yuki Kimoto authored on 2011-08-10
106
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
107
eval {$dbi->apply_filter('table1', 'key2', {out => 'no'})};
108
like($@, qr/not registered/);
109
eval {$dbi->apply_filter('table1', 'key2', {in => 'no'})};
110
like($@, qr/not registered/);
111
$dbi->method({one => sub { 1 }});
112
is($dbi->one, 1);
113

            
test cleanup
Yuki Kimoto authored on 2011-08-10
114
eval{DBIx::Custom->connect(dsn => undef)};
cleanup test
Yuki Kimoto authored on 2011-08-06
115
like($@, qr/_connect/);
116

            
test cleanup
Yuki Kimoto authored on 2011-08-10
117
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
118
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
119
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
120
$dbi->register_filter(twice => sub { $_[0] * 2 });
121
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
122
             filter => {key1 => 'twice'});
123
$row = $dbi->select(table => 'table1')->one;
124
is_deeply($row, {key1 => 2, key2 => 2});
125
eval {$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
126
             filter => {key1 => 'no'}) };
127
like($@, qr//);
128

            
129
$dbi->register_filter(one => sub { });
130
$dbi->default_fetch_filter('one');
131
ok($dbi->default_fetch_filter);
132
$dbi->default_bind_filter('one');
133
ok($dbi->default_bind_filter);
134
eval{$dbi->default_fetch_filter('no')};
135
like($@, qr/not registered/);
136
eval{$dbi->default_bind_filter('no')};
137
like($@, qr/not registered/);
138
$dbi->default_bind_filter(undef);
139
ok(!defined $dbi->default_bind_filter);
140
$dbi->default_fetch_filter(undef);
141
ok(!defined $dbi->default_fetch_filter);
142
eval {$dbi->execute('select * from table1 {} {= author') };
143
like($@, qr/Tag not finished/);
144

            
test cleanup
Yuki Kimoto authored on 2011-08-10
145
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
146
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
147
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
148
$dbi->register_filter(one => sub { 1 });
149
$result = $dbi->select(table => 'table1');
150
eval {$result->filter(key1 => 'no')};
151
like($@, qr/not registered/);
152
eval {$result->end_filter(key1 => 'no')};
153
like($@, qr/not registered/);
154
$result->default_filter(undef);
155
ok(!defined $result->default_filter);
156
$result->default_filter('one');
157
is($result->default_filter->(), 1);
158

            
159
test 'dbi_option';
test cleanup
Yuki Kimoto authored on 2011-08-10
160
$dbi = DBIx::Custom->connect(dbi_option => {PrintError => 1});
cleanup test
Yuki Kimoto authored on 2011-08-06
161
ok($dbi->dbh->{PrintError});
test cleanup
Yuki Kimoto authored on 2011-08-10
162
$dbi = DBIx::Custom->connect(dbi_options => {PrintError => 1});
cleanup test
Yuki Kimoto authored on 2011-08-06
163
ok($dbi->dbh->{PrintError});
164

            
165
test 'DBIx::Custom::Result stash()';
166
$result = DBIx::Custom::Result->new;
167
is_deeply($result->stash, {}, 'default');
168
$result->stash->{foo} = 1;
169
is($result->stash->{foo}, 1, 'get and set');
170

            
171
test 'filter __ expression';
test cleanup
Yuki Kimoto authored on 2011-08-10
172
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
173
eval { $dbi->execute('drop table company') };
174
eval { $dbi->execute('drop table location') };
cleanup test
Yuki Kimoto authored on 2011-08-06
175
$dbi->execute('create table company (id, name, location_id)');
176
$dbi->execute('create table location (id, name)');
177
$dbi->apply_filter('location',
178
  name => {in => sub { uc $_[0] } }
179
);
180

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

            
184
$result = $dbi->select(
185
    table => ['company', 'location'], relation => {'company.location_id' => 'location.id'},
186
    column => ['location.name as location__name']
187
);
188
is($result->fetch_first->[0], 'B');
189

            
190
$result = $dbi->select(
191
    table => 'company', relation => {'company.location_id' => 'location.id'},
192
    column => ['location.name as location__name']
193
);
194
is($result->fetch_first->[0], 'B');
195

            
196
$result = $dbi->select(
197
    table => 'company', relation => {'company.location_id' => 'location.id'},
198
    column => ['location.name as "location.name"']
199
);
200
is($result->fetch_first->[0], 'B');
201

            
202
test 'Model class';
203
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
204
$dbi = MyDBI1->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
205
eval { $dbi->execute('drop table book') };
cleanup test
Yuki Kimoto authored on 2011-08-06
206
$dbi->execute("create table book (title, author)");
207
$model = $dbi->model('book');
208
$model->insert({title => 'a', author => 'b'});
209
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
210
$dbi->execute("create table company (name)");
211
$model = $dbi->model('company');
212
$model->insert({name => 'a'});
213
is_deeply($model->list->all, [{name => 'a'}], 'basic');
214
is($dbi->models->{'book'}, $dbi->model('book'));
215
is($dbi->models->{'company'}, $dbi->model('company'));
216

            
217
{
218
    package MyDBI4;
219

            
220
    use strict;
221
    use warnings;
222

            
223
    use base 'DBIx::Custom';
224

            
225
    sub connect {
226
        my $self = shift->SUPER::connect(@_);
227
        
228
        $self->include_model(
229
            MyModel2 => [
230
                'book',
231
                {class => 'Company', name => 'company'}
232
            ]
233
        );
234
    }
235

            
236
    package MyModel2::Base1;
237

            
238
    use strict;
239
    use warnings;
240

            
241
    use base 'DBIx::Custom::Model';
242

            
243
    package MyModel2::book;
244

            
245
    use strict;
246
    use warnings;
247

            
248
    use base 'MyModel2::Base1';
249

            
250
    sub insert {
251
        my ($self, $param) = @_;
252
        
253
        return $self->SUPER::insert(param => $param);
254
    }
255

            
256
    sub list { shift->select; }
257

            
258
    package MyModel2::Company;
259

            
260
    use strict;
261
    use warnings;
262

            
263
    use base 'MyModel2::Base1';
264

            
265
    sub insert {
266
        my ($self, $param) = @_;
267
        
268
        return $self->SUPER::insert(param => $param);
269
    }
270

            
271
    sub list { shift->select; }
272
}
test cleanup
Yuki Kimoto authored on 2011-08-10
273
$dbi = MyDBI4->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
274
eval { $dbi->execute('drop table book') };
cleanup test
Yuki Kimoto authored on 2011-08-06
275
$dbi->execute("create table book (title, author)");
276
$model = $dbi->model('book');
277
$model->insert({title => 'a', author => 'b'});
278
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
279
$dbi->execute("create table company (name)");
280
$model = $dbi->model('company');
281
$model->insert({name => 'a'});
282
is_deeply($model->list->all, [{name => 'a'}], 'basic');
283

            
284
{
285
     package MyDBI5;
286

            
287
    use strict;
288
    use warnings;
289

            
290
    use base 'DBIx::Custom';
291

            
292
    sub connect {
293
        my $self = shift->SUPER::connect(@_);
294
        
295
        $self->include_model('MyModel4');
296
    }
297
}
test cleanup
Yuki Kimoto authored on 2011-08-10
298
$dbi = MyDBI5->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
299
eval { $dbi->execute('drop table company') };
300
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
301
$dbi->execute("create table company (name)");
302
$dbi->execute("create table table1 (key1)");
303
$model = $dbi->model('company');
304
$model->insert({name => 'a'});
305
is_deeply($model->list->all, [{name => 'a'}], 'include all model');
306
$dbi->insert(table => 'table1', param => {key1 => 1});
307
$model = $dbi->model('book');
308
is_deeply($model->list->all, [{key1 => 1}], 'include all model');
309

            
310
test 'primary_key';
311
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
312
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
313
$model = $dbi->model('book');
314
$model->primary_key(['id', 'number']);
315
is_deeply($model->primary_key, ['id', 'number']);
316

            
317
test 'columns';
318
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
319
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
320
$model = $dbi->model('book');
321
$model->columns(['id', 'number']);
322
is_deeply($model->columns, ['id', 'number']);
323

            
324
test 'setup_model';
325
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
326
$dbi = MyDBI1->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
327
eval { $dbi->execute('drop table book') };
328
eval { $dbi->execute('drop table company') };
329
eval { $dbi->execute('drop table test') };
330

            
cleanup test
Yuki Kimoto authored on 2011-08-06
331
$dbi->execute('create table book (id)');
332
$dbi->execute('create table company (id, name);');
333
$dbi->execute('create table test (id, name, primary key (id, name));');
334
$dbi->setup_model;
335
is_deeply($dbi->model('book')->columns, ['id']);
336
is_deeply($dbi->model('company')->columns, ['id', 'name']);
337

            
338
test 'delete_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
339
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
340
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
341
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
342
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
343
$dbi->delete_at(
344
    table => 'table1',
345
    primary_key => ['key1', 'key2'],
346
    where => [1, 2],
347
);
348
is_deeply($dbi->select(table => 'table1')->all, []);
349

            
350
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
351
$dbi->delete_at(
352
    table => 'table1',
353
    primary_key => 'key1',
354
    where => 1,
355
);
356
is_deeply($dbi->select(table => 'table1')->all, []);
357

            
358
test 'insert_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
359
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
360
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
361
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
362
$dbi->insert_at(
363
    primary_key => ['key1', 'key2'], 
364
    table => 'table1',
365
    where => [1, 2],
366
    param => {key3 => 3}
367
);
368
is($dbi->select(table => 'table1')->one->{key1}, 1);
369
is($dbi->select(table => 'table1')->one->{key2}, 2);
370
is($dbi->select(table => 'table1')->one->{key3}, 3);
371

            
372
$dbi->delete_all(table => 'table1');
373
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
374
$dbi->insert_at(
375
    primary_key => 'key1', 
376
    table => 'table1',
377
    where => 1,
378
    param => {key2 => 2, key3 => 3}
379
);
380

            
381
is($dbi->select(table => 'table1')->one->{key1}, 1);
382
is($dbi->select(table => 'table1')->one->{key2}, 2);
383
is($dbi->select(table => 'table1')->one->{key3}, 3);
384

            
385
eval {
386
    $dbi->insert_at(
387
        table => 'table1',
388
        primary_key => ['key1', 'key2'],
389
        where => {},
390
        param => {key1 => 1, key2 => 2, key3 => 3},
391
    );
392
};
393
like($@, qr/must be/);
394

            
test cleanup
Yuki Kimoto authored on 2011-08-10
395
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
396
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
397
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
398
$dbi->insert_at(
399
    {key3 => 3},
400
    primary_key => ['key1', 'key2'], 
401
    table => 'table1',
402
    where => [1, 2],
403
);
404
is($dbi->select(table => 'table1')->one->{key1}, 1);
405
is($dbi->select(table => 'table1')->one->{key2}, 2);
406
is($dbi->select(table => 'table1')->one->{key3}, 3);
407

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

            
423
$dbi->delete_all(table => 'table1');
424
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
425
$dbi->update_at(
426
    table => 'table1',
427
    primary_key => 'key1',
428
    where => 1,
429
    param => {key3 => 4}
430
);
431
is($dbi->select(table => 'table1')->one->{key1}, 1);
432
is($dbi->select(table => 'table1')->one->{key2}, 2);
433
is($dbi->select(table => 'table1')->one->{key3}, 4);
434

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

            
449
test 'select_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
450
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
451
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
452
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
453
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
454
$result = $dbi->select_at(
455
    table => 'table1',
456
    primary_key => ['key1', 'key2'],
457
    where => [1, 2]
458
);
459
$row = $result->one;
460
is($row->{key1}, 1);
461
is($row->{key2}, 2);
462
is($row->{key3}, 3);
463

            
464
$dbi->delete_all(table => 'table1');
465
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
466
$result = $dbi->select_at(
467
    table => 'table1',
468
    primary_key => 'key1',
469
    where => 1,
470
);
471
$row = $result->one;
472
is($row->{key1}, 1);
473
is($row->{key2}, 2);
474
is($row->{key3}, 3);
475

            
476
$dbi->delete_all(table => 'table1');
477
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
478
$result = $dbi->select_at(
479
    table => 'table1',
480
    primary_key => ['key1', 'key2'],
481
    where => [1, 2]
482
);
483
$row = $result->one;
484
is($row->{key1}, 1);
485
is($row->{key2}, 2);
486
is($row->{key3}, 3);
487

            
488
eval {
489
    $result = $dbi->select_at(
490
        table => 'table1',
491
        primary_key => ['key1', 'key2'],
492
        where => {},
493
    );
494
};
495
like($@, qr/must be/);
496

            
497
eval {
498
    $result = $dbi->select_at(
499
        table => 'table1',
500
        primary_key => ['key1', 'key2'],
501
        where => [1],
502
    );
503
};
504
like($@, qr/same/);
505

            
506
eval {
507
    $result = $dbi->update_at(
508
        table => 'table1',
509
        primary_key => ['key1', 'key2'],
510
        where => {},
511
        param => {key1 => 1, key2 => 2},
512
    );
513
};
514
like($@, qr/must be/);
515

            
516
eval {
517
    $result = $dbi->delete_at(
518
        table => 'table1',
519
        primary_key => ['key1', 'key2'],
520
        where => {},
521
    );
522
};
523
like($@, qr/must be/);
524

            
525
test 'columns';
526
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
527
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
528
$model = $dbi->model('book');
529

            
530

            
531
test 'model delete_at';
532
{
533
    package MyDBI6;
534
    
535
    use base 'DBIx::Custom';
536
    
537
    sub connect {
538
        my $self = shift->SUPER::connect(@_);
539
        
540
        $self->include_model('MyModel5');
541
        
542
        return $self;
543
    }
544
}
test cleanup
Yuki Kimoto authored on 2011-08-10
545
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
546
eval { $dbi->execute('drop table table1') };
547
eval { $dbi->execute('drop table table2') };
548
eval { $dbi->execute('drop table table3') };
cleanup test
Yuki Kimoto authored on 2011-08-10
549
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-10
550
$dbi->execute($create_table2_2);
551
$dbi->execute($create_table3);
cleanup test
Yuki Kimoto authored on 2011-08-06
552
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
553
$dbi->model('table1')->delete_at(where => [1, 2]);
554
is_deeply($dbi->select(table => 'table1')->all, []);
555
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
556
$dbi->model('table1_1')->delete_at(where => [1, 2]);
557
is_deeply($dbi->select(table => 'table1')->all, []);
558
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
559
$dbi->model('table1_3')->delete_at(where => [1, 2]);
560
is_deeply($dbi->select(table => 'table1')->all, []);
561

            
562
test 'model insert_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
563
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
564
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
565
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
566
$dbi->model('table1')->insert_at(
567
    where => [1, 2],
568
    param => {key3 => 3}
569
);
570
$result = $dbi->model('table1')->select;
571
$row = $result->one;
572
is($row->{key1}, 1);
573
is($row->{key2}, 2);
574
is($row->{key3}, 3);
575

            
576
test 'model update_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
577
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
578
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
579
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
580
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
581
$dbi->model('table1')->update_at(
582
    where => [1, 2],
583
    param => {key3 => 4}
584
);
585
$result = $dbi->model('table1')->select;
586
$row = $result->one;
587
is($row->{key1}, 1);
588
is($row->{key2}, 2);
589
is($row->{key3}, 4);
590

            
591
test 'model select_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
592
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
593
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
594
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
595
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
596
$result = $dbi->model('table1')->select_at(where => [1, 2]);
597
$row = $result->one;
598
is($row->{key1}, 1);
599
is($row->{key2}, 2);
600
is($row->{key3}, 3);
601

            
602

            
603
test 'mycolumn and column';
604
{
605
    package MyDBI7;
606
    
607
    use base 'DBIx::Custom';
608
    
609
    sub connect {
610
        my $self = shift->SUPER::connect(@_);
611
        
612
        $self->include_model('MyModel6');
613
        
614
        
615
        return $self;
616
    }
617
}
test cleanup
Yuki Kimoto authored on 2011-08-10
618
$dbi = MyDBI7->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
619
eval { $dbi->execute('drop table table1') };
620
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
621
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
622
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
623
$dbi->separator('__');
624
$dbi->setup_model;
625
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
626
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
627
$model = $dbi->model('table1');
628
$result = $model->select(
629
    column => [$model->mycolumn, $model->column('table2')],
630
    where => {'table1.key1' => 1}
631
);
632
is_deeply($result->one,
633
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
634

            
635
test 'update_param';
test cleanup
Yuki Kimoto authored on 2011-08-10
636
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
637
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
638
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
639
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
640
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
641

            
642
$param = {key2 => 11};
643
$update_param = $dbi->update_param($param);
644
$sql = <<"EOS";
645
update table1 $update_param
646
where key1 = 1
647
EOS
648
$dbi->execute($sql, param => $param);
test cleanup
Yuki Kimoto authored on 2011-08-06
649
$result = $dbi->execute('select * from table1;', table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
650
$rows   = $result->all;
651
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
652
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
653
                  "basic");
654

            
655

            
test cleanup
Yuki Kimoto authored on 2011-08-10
656
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
657
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
658
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
659
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
660
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
661

            
662
$param = {key2 => 11, key3 => 33};
663
$update_param = $dbi->update_param($param);
664
$sql = <<"EOS";
665
update table1 $update_param
666
where key1 = 1
667
EOS
668
$dbi->execute($sql, param => $param);
test cleanup
Yuki Kimoto authored on 2011-08-06
669
$result = $dbi->execute('select * from table1;', table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
670
$rows   = $result->all;
671
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
672
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
673
                  "basic");
674

            
test cleanup
Yuki Kimoto authored on 2011-08-10
675
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
676
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
677
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
678
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
679
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
680

            
681
$param = {key2 => 11, key3 => 33};
682
$update_param = $dbi->update_param($param, {no_set => 1});
683
$sql = <<"EOS";
684
update table1 set $update_param
685
where key1 = 1
686
EOS
687
$dbi->execute($sql, param => $param);
test cleanup
Yuki Kimoto authored on 2011-08-06
688
$result = $dbi->execute('select * from table1;', table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
689
$rows   = $result->all;
690
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
691
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
692
                  "update param no_set");
693

            
694
            
695
eval { $dbi->update_param({";" => 1}) };
696
like($@, qr/not safety/);
697

            
698

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

            
706
$param = {key2 => 11};
707
$update_param = $dbi->assign_param($param);
708
$sql = <<"EOS";
709
update table1 set $update_param
710
where key1 = 1
711
EOS
712
$dbi->execute($sql, param => $param, table => 'table1');
test cleanup
Yuki Kimoto authored on 2011-08-06
713
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
714
$rows   = $result->all;
715
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
716
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
717
                  "basic");
718

            
719

            
720
test 'insert_param';
test cleanup
Yuki Kimoto authored on 2011-08-10
721
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
722
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
723
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
724
$param = {key1 => 1, key2 => 2};
725
$insert_param = $dbi->insert_param($param);
726
$sql = <<"EOS";
727
insert into table1 $insert_param
728
EOS
729
$dbi->execute($sql, param => $param, table => 'table1');
730
is($dbi->select(table => 'table1')->one->{key1}, 1);
731
is($dbi->select(table => 'table1')->one->{key2}, 2);
732

            
test cleanup
Yuki Kimoto authored on 2011-08-10
733
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
734
$dbi->quote('"');
cleanup test
Yuki Kimoto authored on 2011-08-10
735
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
736
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
737
$param = {key1 => 1, key2 => 2};
738
$insert_param = $dbi->insert_param($param);
739
$sql = <<"EOS";
740
insert into table1 $insert_param
741
EOS
742
$dbi->execute($sql, param => $param, table => 'table1');
743
is($dbi->select(table => 'table1')->one->{key1}, 1);
744
is($dbi->select(table => 'table1')->one->{key2}, 2);
745

            
746
eval { $dbi->insert_param({";" => 1}) };
747
like($@, qr/not safety/);
748

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

            
750
test 'join';
test cleanup
Yuki Kimoto authored on 2011-08-10
751
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
752
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
753
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
754
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
755
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-10
756
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
757
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
cleanup test
Yuki Kimoto authored on 2011-08-06
758
$dbi->execute('create table table3 (key3 int, key4 int);');
cleanup test
Yuki Kimoto authored on 2011-08-06
759
$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4});
760
$rows = $dbi->select(
761
    table => 'table1',
762
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
763
    where   => {'table1.key2' => 2},
764
    join  => ['left outer join table2 on table1.key1 = table2.key1']
765
)->all;
766
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]);
767

            
768
$rows = $dbi->select(
769
    table => 'table1',
770
    where   => {'key1' => 1},
771
    join  => ['left outer join table2 on table1.key1 = table2.key1']
772
)->all;
773
is_deeply($rows, [{key1 => 1, key2 => 2}]);
774

            
775
eval {
776
    $rows = $dbi->select(
777
        table => 'table1',
778
        column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
779
        where   => {'table1.key2' => 2},
780
        join  => {'table1.key1' => 'table2.key1'}
781
    );
782
};
783
like ($@, qr/array/);
784

            
785
$rows = $dbi->select(
786
    table => 'table1',
787
    where   => {'key1' => 1},
788
    join  => ['left outer join table2 on table1.key1 = table2.key1',
789
              'left outer join table3 on table2.key3 = table3.key3']
790
)->all;
791
is_deeply($rows, [{key1 => 1, key2 => 2}]);
792

            
793
$rows = $dbi->select(
794
    column => 'table3.key4 as table3__key4',
795
    table => 'table1',
796
    where   => {'table1.key1' => 1},
797
    join  => ['left outer join table2 on table1.key1 = table2.key1',
798
              'left outer join table3 on table2.key3 = table3.key3']
799
)->all;
800
is_deeply($rows, [{table3__key4 => 4}]);
801

            
802
$rows = $dbi->select(
803
    column => 'table1.key1 as table1__key1',
804
    table => 'table1',
805
    where   => {'table3.key4' => 4},
806
    join  => ['left outer join table2 on table1.key1 = table2.key1',
807
              'left outer join table3 on table2.key3 = table3.key3']
808
)->all;
809
is_deeply($rows, [{table1__key1 => 1}]);
810

            
test cleanup
Yuki Kimoto authored on 2011-08-10
811
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
812
$dbi->quote('"');
cleanup test
Yuki Kimoto authored on 2011-08-10
813
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
814
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
815
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
816
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
817
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
818
$rows = $dbi->select(
819
    table => 'table1',
820
    column => '"table1"."key1" as "table1_key1", "table2"."key1" as "table2_key1", "key2", "key3"',
821
    where   => {'table1.key2' => 2},
822
    join  => ['left outer join "table2" on "table1"."key1" = "table2"."key1"'],
823
)->all;
824
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}],
825
          'quote');
826

            
827
{
828
    package MyDBI8;
829
    
830
    use base 'DBIx::Custom';
831
    
832
    sub connect {
833
        my $self = shift->SUPER::connect(@_);
834
        
835
        $self->include_model('MyModel7');
836
        
837
        return $self;
838
    }
839
}
cleanup test
Yuki Kimoto authored on 2011-08-06
840

            
test cleanup
Yuki Kimoto authored on 2011-08-10
841
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
842
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
843
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
844
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
845
$sql = <<"EOS";
cleanup test
Yuki Kimoto authored on 2011-08-06
846
left outer join (
847
  select * from table1 as t1
848
  where t1.key2 = (
849
    select max(t2.key2) from table1 as t2
850
    where t1.key1 = t2.key1
851
  )
852
) as latest_table1 on table1.key1 = latest_table1.key1
853
EOS
cleanup test
Yuki Kimoto authored on 2011-08-06
854
$join = [$sql];
855
$rows = $dbi->select(
856
    table => 'table1',
857
    column => 'latest_table1.key1 as latest_table1__key1',
858
    join  => $join
859
)->all;
860
is_deeply($rows, [{latest_table1__key1 => 1}]);
861

            
test cleanup
Yuki Kimoto authored on 2011-08-10
862
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
863
eval { $dbi->execute('drop table table1') };
864
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
865
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
866
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
867
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
868
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
869
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
870
$result = $dbi->select(
871
    table => 'table1',
872
    join => [
873
        "left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1"
874
    ]
875
);
876
is_deeply($result->all, [{key1 => 1, key2 => 2}]);
877
$result = $dbi->select(
878
    table => 'table1',
879
    column => [{table2 => ['key3']}],
880
    join => [
881
        "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1"
882
    ]
883
);
884
is_deeply($result->all, [{'table2.key3' => 4}]);
885
$result = $dbi->select(
886
    table => 'table1',
887
    column => [{table2 => ['key3']}],
888
    join => [
889
        "left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'"
890
    ]
891
);
892
is_deeply($result->all, [{'table2.key3' => 4}]);
893

            
test cleanup
Yuki Kimoto authored on 2011-08-10
894
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
895
eval { $dbi->execute('drop table table1') };
896
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
897
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
898
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
899
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
900
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
901
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
902
$result = $dbi->select(
903
    table => 'table1',
904
    column => [{table2 => ['key3']}],
905
    join => [
906
        {
907
            clause => "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1",
908
            table => ['table1', 'table2']
909
        }
910
    ]
911
);
912
is_deeply($result->all, [{'table2.key3' => 4}]);
913

            
914
test 'mycolumn';
test cleanup
Yuki Kimoto authored on 2011-08-10
915
$dbi = MyDBI8->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
916
eval { $dbi->execute('drop table table1') };
917
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
918
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
919
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
920
$dbi->setup_model;
921
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
922
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
923
$model = $dbi->model('table1');
924
$result = $model->select_at(
925
    column => [
926
        $model->mycolumn,
927
        $model->column('table2')
928
    ]
929
);
930
is_deeply($result->one,
931
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
932

            
933
$result = $model->select_at(
934
    column => [
935
        $model->mycolumn(['key1']),
936
        $model->column(table2 => ['key1'])
937
    ]
938
);
939
is_deeply($result->one,
940
          {key1 => 1, 'table2.key1' => 1});
941
$result = $model->select_at(
942
    column => [
943
        $model->mycolumn(['key1']),
944
        {table2 => ['key1']}
945
    ]
946
);
947
is_deeply($result->one,
948
          {key1 => 1, 'table2.key1' => 1});
949

            
950
$result = $model->select_at(
951
    column => [
952
        $model->mycolumn(['key1']),
953
        ['table2.key1', as => 'table2.key1']
954
    ]
955
);
956
is_deeply($result->one,
957
          {key1 => 1, 'table2.key1' => 1});
958

            
959
$result = $model->select_at(
960
    column => [
961
        $model->mycolumn(['key1']),
962
        ['table2.key1' => 'table2.key1']
963
    ]
964
);
965
is_deeply($result->one,
966
          {key1 => 1, 'table2.key1' => 1});
967

            
968
test 'dbi method from model';
969
{
970
    package MyDBI9;
971
    
972
    use base 'DBIx::Custom';
973
    
974
    sub connect {
975
        my $self = shift->SUPER::connect(@_);
976
        
977
        $self->include_model('MyModel8')->setup_model;
978
        
979
        return $self;
980
    }
981
}
test cleanup
Yuki Kimoto authored on 2011-08-10
982
$dbi = MyDBI9->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
983
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
984
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
985
$model = $dbi->model('table1');
986
eval{$model->execute('select * from table1')};
987
ok(!$@);
988

            
989
test 'column table option';
test cleanup
Yuki Kimoto authored on 2011-08-10
990
$dbi = MyDBI9->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
991
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
992
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
993
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
994
$dbi->setup_model;
995
$dbi->execute('insert into table1 (key1, key2) values (1, 2);');
996
$dbi->execute('insert into table2 (key1, key3) values (1, 4);');
997
$model = $dbi->model('table1');
998
$result = $model->select(
999
    column => [
1000
        $model->column('table2', {alias => 'table2_alias'})
1001
    ],
1002
    where => {'table2_alias.key3' => 4}
1003
);
1004
is_deeply($result->one, 
1005
          {'table2_alias.key1' => 1, 'table2_alias.key3' => 4});
1006

            
1007
$dbi->separator('__');
1008
$result = $model->select(
1009
    column => [
1010
        $model->column('table2', {alias => 'table2_alias'})
1011
    ],
1012
    where => {'table2_alias.key3' => 4}
1013
);
1014
is_deeply($result->one, 
1015
          {'table2_alias__key1' => 1, 'table2_alias__key3' => 4});
1016

            
1017
$dbi->separator('-');
1018
$result = $model->select(
1019
    column => [
1020
        $model->column('table2', {alias => 'table2_alias'})
1021
    ],
1022
    where => {'table2_alias.key3' => 4}
1023
);
1024
is_deeply($result->one, 
1025
          {'table2_alias-key1' => 1, 'table2_alias-key3' => 4});
1026

            
1027
test 'type option'; # DEPRECATED!
1028
$dbi = DBIx::Custom->connect(
1029
    dbi_option => {
1030
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
1031
    }
1032
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1033
$binary = pack("I3", 1, 2, 3);
1034
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
1035
$dbi->execute('create table table1(key1, key2)');
1036
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
1037
$result = $dbi->select(table => 'table1');
1038
$row   = $result->one;
1039
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
1040
$result = $dbi->execute('select length(key1) as key1_length from table1');
1041
$row = $result->one;
1042
is($row->{key1_length}, length $binary);
1043

            
1044
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [['key1'] => DBI::SQL_BLOB]);
1045
$result = $dbi->select(table => 'table1');
1046
$row   = $result->one;
1047
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
1048
$result = $dbi->execute('select length(key1) as key1_length from table1');
1049
$row = $result->one;
1050
is($row->{key1_length}, length $binary);
1051

            
1052

            
1053
test 'bind_type option';
1054
$dbi = DBIx::Custom->connect(
1055
    dbi_option => {
1056
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
1057
    }
1058
);
1059
$binary = pack("I3", 1, 2, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
1060
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
1061
$dbi->execute('create table table1(key1, key2)');
1062
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, bind_type => [key1 => DBI::SQL_BLOB]);
1063
$result = $dbi->select(table => 'table1');
1064
$row   = $result->one;
1065
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
1066
$result = $dbi->execute('select length(key1) as key1_length from table1');
1067
$row = $result->one;
1068
is($row->{key1_length}, length $binary);
1069

            
1070
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, bind_type => [['key1'] => DBI::SQL_BLOB]);
1071
$result = $dbi->select(table => 'table1');
1072
$row   = $result->one;
1073
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
1074
$result = $dbi->execute('select length(key1) as key1_length from table1');
1075
$row = $result->one;
1076
is($row->{key1_length}, length $binary);
1077

            
1078
test 'model type attribute';
1079
$dbi = DBIx::Custom->connect(
1080
    dbi_option => {
1081
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
1082
    }
1083
);
1084
$binary = pack("I3", 1, 2, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
1085
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
1086
$dbi->execute('create table table1(key1, key2)');
1087
$model = $dbi->create_model(table => 'table1', bind_type => [key1 => DBI::SQL_BLOB]);
1088
$model->insert(param => {key1 => $binary, key2 => 'あ'});
1089
$result = $dbi->select(table => 'table1');
1090
$row   = $result->one;
1091
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
1092
$result = $dbi->execute('select length(key1) as key1_length from table1');
1093
$row = $result->one;
1094
is($row->{key1_length}, length $binary);
1095

            
1096
test 'create_model';
test cleanup
Yuki Kimoto authored on 2011-08-10
1097
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1098
eval { $dbi->execute('drop table table1') };
1099
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1100
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1101
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1102

            
1103
$dbi->create_model(
1104
    table => 'table1',
1105
    join => [
1106
       'left outer join table2 on table1.key1 = table2.key1'
1107
    ],
1108
    primary_key => ['key1']
1109
);
1110
$model2 = $dbi->create_model(
1111
    table => 'table2'
1112
);
1113
$dbi->create_model(
1114
    table => 'table3',
1115
    filter => [
1116
        key1 => {in => sub { uc $_[0] }}
1117
    ]
1118
);
1119
$dbi->setup_model;
1120
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1121
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1122
$model = $dbi->model('table1');
1123
$result = $model->select(
1124
    column => [$model->mycolumn, $model->column('table2')],
1125
    where => {'table1.key1' => 1}
1126
);
1127
is_deeply($result->one,
1128
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
1129
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
1130

            
1131
test 'model method';
1132
test 'create_model';
test cleanup
Yuki Kimoto authored on 2011-08-10
1133
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1134
eval { $dbi->execute('drop table table2') };
test cleanup
Yuki Kimoto authored on 2011-08-10
1135
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1136
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1137
$model = $dbi->create_model(
1138
    table => 'table2'
1139
);
1140
$model->method(foo => sub { shift->select(@_) });
1141
is_deeply($model->foo->one, {key1 => 1, key3 => 3});
1142

            
1143
test 'merge_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
1144
$dbi = DBIx::Custom->new;
1145
$params = [
1146
    {key1 => 1, key2 => 2, key3 => 3},
1147
    {key1 => 1, key2 => 2},
1148
    {key1 => 1}
1149
];
1150
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]);
1151
is_deeply($param, {key1 => [1, 1, 1], key2 => [2, 2], key3 => 3});
1152

            
1153
$params = [
1154
    {key1 => [1, 2], key2 => 1, key3 => [1, 2]},
1155
    {key1 => [3, 4], key2 => [2, 3], key3 => 3}
1156
];
1157
$param = $dbi->merge_param($params->[0], $params->[1]);
1158
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
1159

            
1160
test 'select() param option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1161
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1162
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1163
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1164
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1165
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
1166
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1167
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
1168
$dbi->insert(table => 'table2', param => {key1 => 2, key3 => 5});
1169
$rows = $dbi->select(
1170
    table => 'table1',
1171
    column => 'table1.key1 as table1_key1, key2, key3',
1172
    where   => {'table1.key2' => 3},
1173
    join  => ['inner join (select * from table2 where {= table2.key3})' . 
1174
              ' as table2 on table1.key1 = table2.key1'],
1175
    param => {'table2.key3' => 5}
1176
)->all;
1177
is_deeply($rows, [{table1_key1 => 2, key2 => 3, key3 => 5}]);
1178

            
1179

            
1180
test 'select() wrap option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1181
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1182
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1183
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1184
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1185
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
1186
$rows = $dbi->select(
1187
    table => 'table1',
1188
    column => 'key1',
1189
    wrap => ['select * from (', ') as t where key1 = 1']
1190
)->all;
1191
is_deeply($rows, [{key1 => 1}]);
1192

            
1193
eval {
1194
$dbi->select(
1195
    table => 'table1',
1196
    column => 'key1',
1197
    wrap => 'select * from ('
1198
)
1199
};
1200
like($@, qr/array/);
1201

            
1202
test 'select() string where';
test cleanup
Yuki Kimoto authored on 2011-08-10
1203
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1204
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1205
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1206
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1207
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
1208
$rows = $dbi->select(
1209
    table => 'table1',
1210
    where => 'key1 = :key1 and key2 = :key2',
1211
    where_param => {key1 => 1, key2 => 2}
1212
)->all;
1213
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1214

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1215
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1216
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1217
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1218
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1219
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
1220
$rows = $dbi->select(
1221
    table => 'table1',
1222
    where => [
1223
        'key1 = :key1 and key2 = :key2',
1224
        {key1 => 1, key2 => 2}
1225
    ]
1226
)->all;
1227
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1228

            
1229
test 'delete() string where';
test cleanup
Yuki Kimoto authored on 2011-08-10
1230
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1231
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1232
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1233
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1234
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
1235
$dbi->delete(
1236
    table => 'table1',
1237
    where => 'key1 = :key1 and key2 = :key2',
1238
    where_param => {key1 => 1, key2 => 2}
1239
);
1240
$rows = $dbi->select(table => 'table1')->all;
1241
is_deeply($rows, [{key1 => 2, key2 => 3}]);
1242

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1243
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1244
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1245
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1246
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1247
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
1248
$dbi->delete(
1249
    table => 'table1',
1250
    where => [
1251
        'key1 = :key1 and key2 = :key2',
1252
         {key1 => 1, key2 => 2}
1253
    ]
1254
);
1255
$rows = $dbi->select(table => 'table1')->all;
1256
is_deeply($rows, [{key1 => 2, key2 => 3}]);
1257

            
1258

            
1259
test 'update() string where';
test cleanup
Yuki Kimoto authored on 2011-08-10
1260
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1261
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1262
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1263
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1264
$dbi->update(
1265
    table => 'table1',
1266
    param => {key1 => 5},
1267
    where => 'key1 = :key1 and key2 = :key2',
1268
    where_param => {key1 => 1, key2 => 2}
1269
);
1270
$rows = $dbi->select(table => 'table1')->all;
1271
is_deeply($rows, [{key1 => 5, key2 => 2}]);
1272

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1273
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1274
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1275
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1276
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1277
$dbi->update(
1278
    table => 'table1',
1279
    param => {key1 => 5},
1280
    where => [
1281
        'key1 = :key1 and key2 = :key2',
1282
        {key1 => 1, key2 => 2}
1283
    ]
1284
);
1285
$rows = $dbi->select(table => 'table1')->all;
1286
is_deeply($rows, [{key1 => 5, key2 => 2}]);
1287

            
1288
test 'insert id and primary_key option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1289
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1290
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1291
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1292
$dbi->insert(
1293
    primary_key => ['key1', 'key2'], 
1294
    table => 'table1',
1295
    id => [1, 2],
1296
    param => {key3 => 3}
1297
);
1298
is($dbi->select(table => 'table1')->one->{key1}, 1);
1299
is($dbi->select(table => 'table1')->one->{key2}, 2);
1300
is($dbi->select(table => 'table1')->one->{key3}, 3);
1301

            
1302
$dbi->delete_all(table => 'table1');
1303
$dbi->insert(
1304
    primary_key => 'key1', 
1305
    table => 'table1',
1306
    id => 0,
1307
    param => {key2 => 2, key3 => 3}
1308
);
1309

            
1310
is($dbi->select(table => 'table1')->one->{key1}, 0);
1311
is($dbi->select(table => 'table1')->one->{key2}, 2);
1312
is($dbi->select(table => 'table1')->one->{key3}, 3);
1313

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1314
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1315
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1316
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1317
$dbi->insert(
1318
    {key3 => 3},
1319
    primary_key => ['key1', 'key2'], 
1320
    table => 'table1',
1321
    id => [1, 2],
1322
);
1323
is($dbi->select(table => 'table1')->one->{key1}, 1);
1324
is($dbi->select(table => 'table1')->one->{key2}, 2);
1325
is($dbi->select(table => 'table1')->one->{key3}, 3);
1326

            
1327

            
1328
test 'model insert id and primary_key option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1329
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1330
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1331
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1332
$dbi->model('table1')->insert(
1333
    id => [1, 2],
1334
    param => {key3 => 3}
1335
);
1336
$result = $dbi->model('table1')->select;
1337
$row = $result->one;
1338
is($row->{key1}, 1);
1339
is($row->{key2}, 2);
1340
is($row->{key3}, 3);
1341

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1342
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1343
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1344
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1345
$dbi->model('table1')->insert(
1346
    {key3 => 3},
1347
    id => [1, 2]
1348
);
1349
$result = $dbi->model('table1')->select;
1350
$row = $result->one;
1351
is($row->{key1}, 1);
1352
is($row->{key2}, 2);
1353
is($row->{key3}, 3);
1354

            
1355
test 'update and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1356
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1357
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1358
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1359
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1360
$dbi->update(
1361
    table => 'table1',
1362
    primary_key => ['key1', 'key2'],
1363
    id => [1, 2],
1364
    param => {key3 => 4}
1365
);
1366
is($dbi->select(table => 'table1')->one->{key1}, 1);
1367
is($dbi->select(table => 'table1')->one->{key2}, 2);
1368
is($dbi->select(table => 'table1')->one->{key3}, 4);
1369

            
1370
$dbi->delete_all(table => 'table1');
1371
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
1372
$dbi->update(
1373
    table => 'table1',
1374
    primary_key => 'key1',
1375
    id => 0,
1376
    param => {key3 => 4}
1377
);
1378
is($dbi->select(table => 'table1')->one->{key1}, 0);
1379
is($dbi->select(table => 'table1')->one->{key2}, 2);
1380
is($dbi->select(table => 'table1')->one->{key3}, 4);
1381

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1382
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1383
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1384
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1385
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1386
$dbi->update(
1387
    {key3 => 4},
1388
    table => 'table1',
1389
    primary_key => ['key1', 'key2'],
1390
    id => [1, 2]
1391
);
1392
is($dbi->select(table => 'table1')->one->{key1}, 1);
1393
is($dbi->select(table => 'table1')->one->{key2}, 2);
1394
is($dbi->select(table => 'table1')->one->{key3}, 4);
1395

            
1396

            
1397
test 'model update and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1398
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1399
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1400
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1401
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1402
$dbi->model('table1')->update(
1403
    id => [1, 2],
1404
    param => {key3 => 4}
1405
);
1406
$result = $dbi->model('table1')->select;
1407
$row = $result->one;
1408
is($row->{key1}, 1);
1409
is($row->{key2}, 2);
1410
is($row->{key3}, 4);
1411

            
1412

            
1413
test 'delete and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1414
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1415
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1416
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1417
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1418
$dbi->delete(
1419
    table => 'table1',
1420
    primary_key => ['key1', 'key2'],
1421
    id => [1, 2],
1422
);
1423
is_deeply($dbi->select(table => 'table1')->all, []);
1424

            
1425
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
1426
$dbi->delete(
1427
    table => 'table1',
1428
    primary_key => 'key1',
1429
    id => 0,
1430
);
1431
is_deeply($dbi->select(table => 'table1')->all, []);
1432

            
1433

            
1434
test 'model delete and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1435
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1436
eval { $dbi->execute('drop table table1') };
1437
eval { $dbi->execute('drop table table2') };
1438
eval { $dbi->execute('drop table table3') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1439
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-10
1440
$dbi->execute($create_table2_2);
1441
$dbi->execute($create_table3);
cleanup test
Yuki Kimoto authored on 2011-08-06
1442
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1443
$dbi->model('table1')->delete(id => [1, 2]);
1444
is_deeply($dbi->select(table => 'table1')->all, []);
1445
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
1446
$dbi->model('table1_1')->delete(id => [1, 2]);
1447
is_deeply($dbi->select(table => 'table1')->all, []);
1448
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
1449
$dbi->model('table1_3')->delete(id => [1, 2]);
1450
is_deeply($dbi->select(table => 'table1')->all, []);
1451

            
1452

            
1453
test 'select and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1454
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1455
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1456
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1457
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1458
$result = $dbi->select(
1459
    table => 'table1',
1460
    primary_key => ['key1', 'key2'],
1461
    id => [1, 2]
1462
);
1463
$row = $result->one;
1464
is($row->{key1}, 1);
1465
is($row->{key2}, 2);
1466
is($row->{key3}, 3);
1467

            
1468
$dbi->delete_all(table => 'table1');
1469
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
1470
$result = $dbi->select(
1471
    table => 'table1',
1472
    primary_key => 'key1',
1473
    id => 0,
1474
);
1475
$row = $result->one;
1476
is($row->{key1}, 0);
1477
is($row->{key2}, 2);
1478
is($row->{key3}, 3);
1479

            
1480
$dbi->delete_all(table => 'table1');
1481
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1482
$result = $dbi->select(
1483
    table => 'table1',
1484
    primary_key => ['key1', 'key2'],
1485
    id => [1, 2]
1486
);
1487
$row = $result->one;
1488
is($row->{key1}, 1);
1489
is($row->{key2}, 2);
1490
is($row->{key3}, 3);
1491

            
1492

            
1493
test 'model select_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
1494
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1495
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1496
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1497
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1498
$result = $dbi->model('table1')->select(id => [1, 2]);
1499
$row = $result->one;
1500
is($row->{key1}, 1);
1501
is($row->{key2}, 2);
1502
is($row->{key3}, 3);
1503

            
1504
test 'column separator is default .';
test cleanup
Yuki Kimoto authored on 2011-08-10
1505
$dbi = MyDBI7->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1506
eval { $dbi->execute('drop table table1') };
1507
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1508
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1509
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1510
$dbi->setup_model;
1511
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1512
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1513
$model = $dbi->model('table1');
1514
$result = $model->select(
1515
    column => [$model->column('table2')],
1516
    where => {'table1.key1' => 1}
1517
);
1518
is_deeply($result->one,
1519
          {'table2.key1' => 1, 'table2.key3' => 3});
1520

            
1521
$result = $model->select(
1522
    column => [$model->column('table2' => [qw/key1 key3/])],
1523
    where => {'table1.key1' => 1}
1524
);
1525
is_deeply($result->one,
1526
          {'table2.key1' => 1, 'table2.key3' => 3});
1527

            
1528

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

            
1530
test 'separator';
test cleanup
Yuki Kimoto authored on 2011-08-10
1531
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1532
eval { $dbi->execute('drop table table1') };
1533
eval { $dbi->execute('drop table table2') };
1534
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1535
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-10
1536

            
1537
$dbi->create_model(
1538
    table => 'table1',
1539
    join => [
1540
       'left outer join table2 on table1.key1 = table2.key1'
1541
    ],
1542
    primary_key => ['key1'],
cleanup test
Yuki Kimoto authored on 2011-08-06
1543
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1544
$model2 = $dbi->create_model(
1545
    table => 'table2',
1546
);
1547
$dbi->setup_model;
1548
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1549
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1550
$model = $dbi->model('table1');
1551
$result = $model->select(
1552
    column => [
1553
        $model->mycolumn,
1554
        {table2 => [qw/key1 key3/]}
1555
    ],
1556
    where => {'table1.key1' => 1}
1557
);
1558
is_deeply($result->one,
1559
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
1560
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
1561

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1562
$dbi->separator('__');
1563
$model = $dbi->model('table1');
1564
$result = $model->select(
1565
    column => [
1566
        $model->mycolumn,
1567
        {table2 => [qw/key1 key3/]}
1568
    ],
1569
    where => {'table1.key1' => 1}
1570
);
1571
is_deeply($result->one,
1572
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
1573
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
1574

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1575
$dbi->separator('-');
1576
$model = $dbi->model('table1');
1577
$result = $model->select(
1578
    column => [
1579
        $model->mycolumn,
1580
        {table2 => [qw/key1 key3/]}
1581
    ],
1582
    where => {'table1.key1' => 1}
1583
);
1584
is_deeply($result->one,
1585
          {key1 => 1, key2 => 2, 'table2-key1' => 1, 'table2-key3' => 3});
1586
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
1587

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

            
1589
test 'filter_off';
test cleanup
Yuki Kimoto authored on 2011-08-10
1590
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1591
eval { $dbi->execute('drop table table1') };
1592
eval { $dbi->execute('drop table table2') };
1593
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1594
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-10
1595

            
1596
$dbi->create_model(
1597
    table => 'table1',
1598
    join => [
1599
       'left outer join table2 on table1.key1 = table2.key1'
1600
    ],
1601
    primary_key => ['key1'],
cleanup test
Yuki Kimoto authored on 2011-08-06
1602
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1603
$dbi->setup_model;
1604
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1605
$model = $dbi->model('table1');
1606
$result = $model->select(column => 'key1');
1607
$result->filter(key1 => sub { $_[0] * 2 });
1608
is_deeply($result->one, {key1 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-06
1609

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1610
test 'available_datetype';
test cleanup
Yuki Kimoto authored on 2011-08-10
1611
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1612
ok($dbi->can('available_datatype'));
1613

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1615
test 'select prefix option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1616
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1617
eval { $dbi->execute('drop table table1') };
1618
$dbi->execute($create_table1);
1619
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1620
$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all;
1621
is_deeply($rows, [{key1 => 1, key2 => 2}], "table");
1622

            
1623

            
1624
test 'separator';
1625
$dbi = DBIx::Custom->connect;
1626
is($dbi->separator, '.');
1627
$dbi->separator('-');
1628
is($dbi->separator, '-');
1629
$dbi->separator('__');
1630
is($dbi->separator, '__');
1631
eval { $dbi->separator('?') };
1632
like($@, qr/Separator/);
1633

            
1634

            
1635
test 'map_param';
1636
$dbi = DBIx::Custom->connect;
1637
$param = $dbi->map_param(
1638
    {id => 1, author => 'Ken', price => 1900},
1639
    id => 'book.id',
1640
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1641
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
cleanup test
Yuki Kimoto authored on 2011-08-06
1642
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1643
is_deeply($param, {'book.id' => 1, 'book.author' => '%Ken%',
1644
  'book.price' => 1900});
1645

            
1646
$param = $dbi->map_param(
1647
    {id => 0, author => 0, price => 0},
1648
    id => 'book.id',
1649
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1650
    price => ['book.price', sub { '%' . $_[0] . '%' },
1651
      {if => sub { $_[0] eq 0 }}]
cleanup test
Yuki Kimoto authored on 2011-08-06
1652
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1653
is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
cleanup test
Yuki Kimoto authored on 2011-08-06
1654

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1655
$param = $dbi->map_param(
1656
    {id => '', author => '', price => ''},
1657
    id => 'book.id',
1658
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1659
    price => ['book.price', sub { '%' . $_[0] . '%' },
1660
      {if => sub { $_[0] eq 1 }}]
cleanup test
Yuki Kimoto authored on 2011-08-06
1661
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1662
is_deeply($param, {});
1663

            
1664
$param = $dbi->map_param(
1665
    {id => undef, author => undef, price => undef},
1666
    id => 'book.id',
1667
    price => ['book.price', {if => 'exists'}]
cleanup test
Yuki Kimoto authored on 2011-08-06
1668
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1669
is_deeply($param, {'book.price' => undef});
1670

            
1671
$param = $dbi->map_param(
1672
    {price => 'a'},
1673
    id => ['book.id', {if => 'exists'}],
1674
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
1675
);
1676
is_deeply($param, {'book.price' => '%a'});
cleanup test
Yuki Kimoto authored on 2011-08-06
1677

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

            
1679
test 'table_alias';
test cleanup
Yuki Kimoto authored on 2011-08-10
1680
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1681
eval { $dbi->execute('drop table table1') };
1682
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
cleanup test
Yuki Kimoto authored on 2011-08-06
1683
$dbi->type_rule(
1684
    into1 => {
cleanup test
Yuki Kimoto authored on 2011-08-10
1685
        date => sub { uc $_[0] }
cleanup test
Yuki Kimoto authored on 2011-08-06
1686
    }
1687
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1688
$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'},
1689
  table_alias => {table2 => 'table1'});
cleanup test
Yuki Kimoto authored on 2011-08-06
1690
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1691
is($result->one->{key1}, 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
1692

            
1693

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1694
test 'order';
test cleanup
Yuki Kimoto authored on 2011-08-10
1695
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1696
eval { $dbi->execute('drop table table1') };
1697
$dbi->execute("create table table1 (key1, key2)");
1698
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
1699
$dbi->insert({key1 => 1, key2 => 3}, table => 'table1');
1700
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
1701
$dbi->insert({key1 => 2, key2 => 4}, table => 'table1');
1702
my $order = $dbi->order;
1703
$order->prepend('key1', 'key2 desc');
1704
$result = $dbi->select(table => 'table1', append => "$order");
1705
is_deeply($result->all, [{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1},
1706
  {key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}]);
1707
$order->prepend('key1 desc');
1708
$result = $dbi->select(table => 'table1', append => "$order");
1709
is_deeply($result->all, [{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2},
1710
  {key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
1711

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1712
$order = $dbi->order;
1713
$order->prepend(['table1-key1'], [qw/table1-key2 desc/]);
1714
$result = $dbi->select(table => 'table1',
1715
  column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']],
1716
  append => "$order");
1717
is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3},
1718
  {'table1-key1' => 1, 'table1-key2' => 1},
1719
  {'table1-key1' => 2, 'table1-key2' => 4},
1720
  {'table1-key1' => 2, 'table1-key2' => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
1721

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1722
test 'tag_parse';
test cleanup
Yuki Kimoto authored on 2011-08-10
1723
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1724
$dbi->tag_parse(0);
1725
eval { $dbi->execute('drop table table1') };
1726
$dbi->execute("create table table1 (key1, key2)");
1727
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
1728
eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})};
1729
ok($@);
cleanup test
Yuki Kimoto authored on 2011-08-06
1730

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1731
test 'last_sql';
test cleanup
Yuki Kimoto authored on 2011-08-10
1732
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1733
eval { $dbi->execute('drop table table1') };
1734
$dbi->execute("create table table1 (key1, key2)");
1735
$dbi->execute('select * from table1');
1736
is($dbi->last_sql, 'select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
1737

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1741
test 'DBIx::Custom header';
test cleanup
Yuki Kimoto authored on 2011-08-10
1742
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1743
eval { $dbi->execute('drop table table1') };
1744
$dbi->execute("create table table1 (key1, key2)");
1745
$result = $dbi->execute('select key1 as h1, key2 as h2 from table1');
1746
is_deeply($result->header, [qw/h1 h2/]);
cleanup test
Yuki Kimoto authored on 2011-08-06
1747

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

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1769
$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}";
1770
$result = $dbi->execute(
1771
    $source,
1772
    param => {'table1.key1' => 1, 'table1.key2' => 1},
1773
    filter => {'table1.key2' => sub { $_[0] * 2 }}
cleanup test
Yuki Kimoto authored on 2011-08-06
1774
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1775
$rows = $result->all;
1776
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
1777

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1778
test 'high perfomance way';
1779
$dbi->execute('drop table table1');
1780
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
1781
$rows = [
1782
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
1783
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
1784
];
1785
{
1786
    my $query;
1787
    foreach my $row (@$rows) {
1788
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
1789
      $dbi->execute($query, $row, filter => {ab => sub { $_[0] * 2 }});
cleanup test
Yuki Kimoto authored on 2011-08-06
1790
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
1791
    is_deeply($dbi->select(table => 'table1')->all,
1792
      [
1793
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
1794
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
1795
      ]
1796
    );
1797
}
1798

            
1799
$dbi->execute('drop table table1');
1800
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
1801
$rows = [
1802
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
1803
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
1804
];
1805
{
1806
    my $query;
1807
    my $sth;
1808
    foreach my $row (@$rows) {
1809
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
1810
      $sth ||= $query->sth;
1811
      $sth->execute(map { $row->{$_} } sort keys %$row);
cleanup test
Yuki Kimoto authored on 2011-08-06
1812
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
1813
    is_deeply($dbi->select(table => 'table1')->all,
1814
      [
1815
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
1816
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
1817
      ]
1818
    );
1819
}
cleanup test
Yuki Kimoto authored on 2011-08-06
1820

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1821
test 'result';
test cleanup
Yuki Kimoto authored on 2011-08-10
1822
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1823
eval { $dbi->execute('drop table table1') };
1824
$dbi->execute($create_table1);
1825
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
1826
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
1827

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1828
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1829
@rows = ();
1830
while (my $row = $result->fetch) {
1831
    push @rows, [@$row];
1832
}
1833
is_deeply(\@rows, [[1, 2], [3, 4]]);
cleanup test
Yuki Kimoto authored on 2011-08-06
1834

            
1835
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1836
@rows = ();
1837
while (my $row = $result->fetch_hash) {
1838
    push @rows, {%$row};
1839
}
1840
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
1841

            
1842
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1843
$row = $result->fetch_first;
1844
is_deeply($row, [1, 2], "row");
1845
$row = $result->fetch;
1846
ok(!$row, "finished");
1847

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1848
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1849
$row = $result->fetch_hash_first;
1850
is_deeply($row, {key1 => 1, key2 => 2}, "row");
1851
$row = $result->fetch_hash;
1852
ok(!$row, "finished");
1853

            
1854
$dbi->execute('create table table2 (key1, key2);');
1855
$result = $dbi->select(table => 'table2');
1856
$row = $result->fetch_hash_first;
1857
ok(!$row, "no row fetch");
cleanup test
Yuki Kimoto authored on 2011-08-06
1858

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1859
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1860
eval { $dbi->execute('drop table table1') };
1861
$dbi->execute($create_table1);
1862
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
1863
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
1864
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1');
1865
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1');
1866
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
1867
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1868
$rows = $result->fetch_multi(2);
1869
is_deeply($rows, [[1, 2],
1870
                  [3, 4]], "fetch_multi first");
1871
$rows = $result->fetch_multi(2);
1872
is_deeply($rows, [[5, 6],
1873
                  [7, 8]], "fetch_multi secound");
1874
$rows = $result->fetch_multi(2);
1875
is_deeply($rows, [[9, 10]], "fetch_multi third");
1876
$rows = $result->fetch_multi(2);
1877
ok(!$rows);
1878

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

            
1883
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1884
$rows = $result->fetch_hash_multi(2);
1885
is_deeply($rows, [{key1 => 1, key2 => 2},
1886
                  {key1 => 3, key2 => 4}], "fetch_multi first");
1887
$rows = $result->fetch_hash_multi(2);
1888
is_deeply($rows, [{key1 => 5, key2 => 6},
1889
                  {key1 => 7, key2 => 8}], "fetch_multi secound");
1890
$rows = $result->fetch_hash_multi(2);
1891
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third");
1892
$rows = $result->fetch_hash_multi(2);
1893
ok(!$rows);
1894

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1899
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1900
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1901
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
1902
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
1903
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
1904

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1905
test 'fetch_all';
1906
$result = $dbi->select(table => 'table1');
1907
$rows = $result->fetch_all;
1908
is_deeply($rows, [[1, 2], [3, 4]]);
cleanup test
Yuki Kimoto authored on 2011-08-06
1909

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1921
$result = $dbi->select(table => 'table1');
1922
$result->dbi->filters({three_times => sub { $_[0] * 3}});
1923
$result->filter({key1 => 'three_times'});
1924
$rows = $result->fetch_hash_all;
1925
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash");
1926

            
1927
test "query_builder";
1928
$datas = [
1929
    # Basic tests
1930
    {   name            => 'placeholder basic',
1931
        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
1932
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;",
1933
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
1934
    },
1935
    {
1936
        name            => 'placeholder in',
1937
        source            => "{in k1 3};",
1938
        sql_expected    => "k1 in (?, ?, ?);",
1939
        columns_expected   => [qw/k1 k1 k1/]
1940
    },
1941
    
1942
    # Table name
1943
    {
1944
        name            => 'placeholder with table name',
1945
        source            => "{= a.k1} {= a.k2}",
1946
        sql_expected    => "a.k1 = ? a.k2 = ?;",
1947
        columns_expected  => [qw/a.k1 a.k2/]
1948
    },
1949
    {   
1950
        name            => 'placeholder in with table name',
1951
        source            => "{in a.k1 2} {in b.k2 2}",
1952
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?);",
1953
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
1954
    },
1955
    {
1956
        name            => 'not contain tag',
1957
        source            => "aaa",
1958
        sql_expected    => "aaa;",
1959
        columns_expected  => [],
1960
    }
1961
];
1962

            
1963
for (my $i = 0; $i < @$datas; $i++) {
1964
    my $data = $datas->[$i];
1965
    my $builder = DBIx::Custom->new->query_builder;
1966
    my $query = $builder->build_query($data->{source});
1967
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
1968
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
1969
}
1970

            
1971
$builder = DBIx::Custom->new->query_builder;
1972
$ret_val = $builder->register_tag(
1973
    p => sub {
1974
        my @args = @_;
1975
        
1976
        my $expand    = "? $args[0] $args[1]";
1977
        my $columns = [2];
1978
        return [$expand, $columns];
1979
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
1980
);
1981

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

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1995
$builder->register_tag({
1996
    q => 'string'
1997
});
cleanup test
Yuki Kimoto authored on 2011-08-06
1998

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2002
$builder->register_tag({
2003
   r => sub {} 
2004
});
cleanup test
Yuki Kimoto authored on 2011-08-06
2005

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

            
2009
$builder->register_tag({
2010
   s => sub { return ["a", ""]} 
2011
});
2012

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

            
2016
$builder->register_tag(
2017
    t => sub {return ["a", []]}
cleanup test
Yuki Kimoto authored on 2011-08-06
2018
);
2019

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

            
2021
test 'General error case';
2022
$builder = DBIx::Custom->new->query_builder;
2023
$builder->register_tag(
2024
    a => sub {
2025
        return ["? ? ?", ['']];
2026
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
2027
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2028
eval{$builder->build_query("{a}")};
2029
like($@, qr/\QPlaceholder count/, "placeholder count is invalid");
cleanup test
Yuki Kimoto authored on 2011-08-06
2030

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

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

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

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

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

            
2047
test 'variouse source';
2048
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;";
2049
$query = $builder->build_query($source);
2050
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1");
2051

            
2052
$source = "abc;";
2053
$query = $builder->build_query($source);
2054
is($query->sql, 'abc;', "basic : 2");
2055

            
2056
$source = "{= a}";
2057
$query = $builder->build_query($source);
2058
is($query->sql, 'a = ?;', "only tag");
2059

            
2060
$source = "000;";
2061
$query = $builder->build_query($source);
2062
is($query->sql, '000;', "contain 0 value");
2063

            
2064
$source = "a {= b} }";
2065
eval{$builder->build_query($source)};
2066
like($@, qr/unexpected "}"/, "error : 1");
2067

            
2068
$source = "a {= {}";
2069
eval{$builder->build_query($source)};
2070
like($@, qr/unexpected "{"/, "error : 2");
2071

            
2072
### SQLite test
2073
test 'type option'; # DEPRECATED!
2074
$dbi = DBIx::Custom->connect(
2075
    data_source => 'dbi:SQLite:dbname=:memory:',
2076
    dbi_option => {
2077
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
2078
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
2079
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2080
$binary = pack("I3", 1, 2, 3);
2081
eval { $dbi->execute('drop table table1') };
2082
$dbi->execute('create table table1(key1, key2)');
2083
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
2084
$result = $dbi->select(table => 'table1');
2085
$row   = $result->one;
2086
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2087
$result = $dbi->execute('select length(key1) as key1_length from table1');
2088
$row = $result->one;
2089
is($row->{key1_length}, length $binary);
cleanup test
Yuki Kimoto authored on 2011-08-06
2090

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2091
test 'type_rule from';
2092
$dbi = DBIx::Custom->connect;
2093
$dbi->type_rule(
2094
    from1 => {
2095
        date => sub { uc $_[0] }
2096
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
2097
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2098
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2099
$dbi->insert({key1 => 'a'}, table => 'table1');
2100
$result = $dbi->select(table => 'table1');
2101
is($result->fetch_first->[0], 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
2102

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

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

            
2107
test 'type_rule into';
test cleanup
Yuki Kimoto authored on 2011-08-10
2108
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
2109
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2110
$dbi->type_rule(
2111
    into1 => {
2112
        date => sub { uc $_[0] }
2113
    }
2114
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2115
$dbi->insert({key1 => 'a'}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
2116
$result = $dbi->select(table => 'table1');
2117
is($result->one->{key1}, 'A');
2118

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2119
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2120
$dbi->execute("create table table1 (key1 date, key2 datetime)");
2121
$dbi->type_rule(
2122
    into1 => [
2123
         [qw/date datetime/] => sub { uc $_[0] }
2124
    ]
2125
);
2126
$dbi->insert({key1 => 'a', key2 => 'b'}, table => 'table1');
2127
$result = $dbi->select(table => 'table1');
2128
$row = $result->one;
2129
is($row->{key1}, 'A');
2130
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
2131

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2132
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2133
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2134
$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1');
2135
$dbi->type_rule(
2136
    into1 => [
2137
        [qw/date datetime/] => sub { uc $_[0] }
2138
    ]
2139
);
2140
$result = $dbi->execute(
2141
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
2142
    param => {key1 => 'a', 'table1.key2' => 'b'}
2143
);
2144
$row = $result->one;
2145
is($row->{key1}, 'a');
2146
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
2147

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2148
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2149
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2150
$dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1');
2151
$dbi->type_rule(
2152
    into1 => [
2153
        [qw/date datetime/] => sub { uc $_[0] }
2154
    ]
2155
);
2156
$result = $dbi->execute(
2157
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
2158
    param => {key1 => 'a', 'table1.key2' => 'b'},
2159
    table => 'table1'
2160
);
2161
$row = $result->one;
2162
is($row->{key1}, 'A');
2163
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
2164

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2165
$dbi = DBIx::Custom->connect;
2166
$dbi->execute("create table table1 (key1 date, key2 datetime)");
2167
$dbi->register_filter(twice => sub { $_[0] * 2 });
2168
$dbi->type_rule(
2169
    from1 => {
2170
        date => 'twice',
2171
    },
2172
    into1 => {
2173
        date => 'twice',
2174
    }
2175
);
2176
$dbi->insert({key1 => 2}, table => 'table1');
2177
$result = $dbi->select(table => 'table1');
2178
is($result->fetch->[0], 8);
cleanup test
Yuki Kimoto authored on 2011-08-06
2179

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2180
test 'type_rule and filter order';
test cleanup
Yuki Kimoto authored on 2011-08-10
2181
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2182
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2183
$dbi->type_rule(
2184
    into1 => {
2185
        date => sub { $_[0] . 'b' }
2186
    },
2187
    into2 => {
2188
        date => sub { $_[0] . 'c' }
2189
    },
2190
    from1 => {
2191
        date => sub { $_[0] . 'd' }
2192
    },
2193
    from2 => {
2194
        date => sub { $_[0] . 'e' }
2195
    }
2196
);
2197
$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }});
2198
$result = $dbi->select(table => 'table1');
2199
$result->filter(key1 => sub { $_[0] . 'f' });
2200
is($result->fetch_first->[0], '1abcdef');
cleanup test
Yuki Kimoto authored on 2011-08-06
2201

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2202
$dbi = DBIx::Custom->connect;
2203
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2204
$dbi->type_rule(
2205
    from1 => {
2206
        date => sub { $_[0] . 'p' }
2207
    },
2208
    from2 => {
2209
        date => sub { $_[0] . 'q' }
2210
    },
2211
);
2212
$dbi->insert({key1 => '1'}, table => 'table1');
2213
$result = $dbi->select(table => 'table1');
2214
$result->type_rule(
2215
    from1 => {
2216
        date => sub { $_[0] . 'd' }
2217
    },
2218
    from2 => {
2219
        date => sub { $_[0] . 'e' }
2220
    }
2221
);
2222
$result->filter(key1 => sub { $_[0] . 'f' });
2223
is($result->fetch_first->[0], '1def');
cleanup test
Yuki Kimoto authored on 2011-08-06
2224

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2225
test 'type_rule_off';
2226
$dbi = DBIx::Custom->connect;
2227
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2228
$dbi->type_rule(
2229
    from1 => {
2230
        date => sub { $_[0] * 2 },
2231
    },
2232
    into1 => {
2233
        date => sub { $_[0] * 2 },
2234
    }
2235
);
2236
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
2237
$result = $dbi->select(table => 'table1', type_rule_off => 1);
2238
is($result->type_rule_off->fetch->[0], 2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2239

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2240
$dbi = DBIx::Custom->connect;
2241
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2242
$dbi->type_rule(
2243
    from1 => {
2244
        date => sub { $_[0] * 2 },
2245
    },
2246
    into1 => {
2247
        date => sub { $_[0] * 3 },
2248
    }
2249
);
2250
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
2251
$result = $dbi->select(table => 'table1', type_rule_off => 1);
2252
is($result->one->{key1}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-06
2253

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2254
$dbi = DBIx::Custom->connect;
2255
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2256
$dbi->type_rule(
2257
    from1 => {
2258
        date => sub { $_[0] * 2 },
2259
    },
2260
    into1 => {
2261
        date => sub { $_[0] * 3 },
2262
    }
2263
);
2264
$dbi->insert({key1 => 2}, table => 'table1');
2265
$result = $dbi->select(table => 'table1');
2266
is($result->one->{key1}, 12);
cleanup test
Yuki Kimoto authored on 2011-08-06
2267

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2268
$dbi = DBIx::Custom->connect;
2269
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2270
$dbi->type_rule(
2271
    from1 => {
2272
        date => sub { $_[0] * 2 },
2273
    },
2274
    into1 => {
2275
        date => sub { $_[0] * 3 },
2276
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
2277
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2278
$dbi->insert({key1 => 2}, table => 'table1');
2279
$result = $dbi->select(table => 'table1');
2280
is($result->fetch->[0], 12);
cleanup test
Yuki Kimoto authored on 2011-08-06
2281

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2282
$dbi = DBIx::Custom->connect;
2283
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2284
$dbi->register_filter(ppp => sub { uc $_[0] });
2285
$dbi->type_rule(
2286
    into1 => {
2287
        date => 'ppp'
cleanup test
Yuki Kimoto authored on 2011-08-06
2288
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
2289
);
2290
$dbi->insert({key1 => 'a'}, table => 'table1');
2291
$result = $dbi->select(table => 'table1');
2292
is($result->one->{key1}, 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
2293

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2294
eval{$dbi->type_rule(
2295
    into1 => {
2296
        date => 'pp'
cleanup test
Yuki Kimoto authored on 2011-08-06
2297
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
2298
)};
2299
like($@, qr/not registered/);
cleanup test
Yuki Kimoto authored on 2011-08-06
2300

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2301
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2302
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2303
eval {
2304
    $dbi->type_rule(
2305
        from1 => {
2306
            Date => sub { $_[0] * 2 },
2307
        }
2308
    );
2309
};
2310
like($@, qr/lower/);
clenup test
Yuki Kimoto authored on 2011-08-06
2311

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2312
eval {
2313
    $dbi->type_rule(
2314
        into1 => {
2315
            Date => sub { $_[0] * 2 },
2316
        }
2317
    );
2318
};
2319
like($@, qr/lower/);
clenup test
Yuki Kimoto authored on 2011-08-06
2320

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2321
$dbi = DBIx::Custom->connect;
2322
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2323
$dbi->type_rule(
2324
    from1 => {
2325
        date => sub { $_[0] * 2 },
2326
    },
2327
    into1 => {
2328
        date => sub { $_[0] * 3 },
2329
    }
2330
);
2331
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
2332
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2333
$result->type_rule_off;
2334
is($result->one->{key1}, 6);
clenup test
Yuki Kimoto authored on 2011-08-06
2335

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2336
$dbi = DBIx::Custom->connect;
2337
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2338
$dbi->type_rule(
2339
    from1 => {
2340
        date => sub { $_[0] * 2 },
2341
        datetime => sub { $_[0] * 4 },
2342
    },
2343
);
2344
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
2345
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2346
$result->type_rule(
2347
    from1 => {
2348
        date => sub { $_[0] * 3 }
2349
    }
2350
);
2351
$row = $result->one;
2352
is($row->{key1}, 6);
2353
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
2354

            
2355
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2356
$result->type_rule(
2357
    from1 => {
2358
        date => sub { $_[0] * 3 }
2359
    }
2360
);
2361
$row = $result->one;
2362
is($row->{key1}, 6);
2363
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
2364

            
2365
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2366
$result->type_rule(
2367
    from1 => {
2368
        date => sub { $_[0] * 3 }
2369
    }
2370
);
2371
$row = $result->one;
2372
is($row->{key1}, 6);
2373
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
2374
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2375
$result->type_rule(
2376
    from1 => [date => sub { $_[0] * 3 }]
2377
);
2378
$row = $result->one;
2379
is($row->{key1}, 6);
2380
is($row->{key2}, 2);
2381
$dbi->register_filter(fivetimes => sub { $_[0] * 5});
clenup test
Yuki Kimoto authored on 2011-08-06
2382
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2383
$result->type_rule(
2384
    from1 => [date => 'fivetimes']
2385
);
2386
$row = $result->one;
2387
is($row->{key1}, 10);
2388
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
2389
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2390
$result->type_rule(
2391
    from1 => [date => undef]
2392
);
2393
$row = $result->one;
2394
is($row->{key1}, 2);
2395
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
2396

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2397
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2398
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2399
$dbi->type_rule(
2400
    from1 => {
2401
        date => sub { $_[0] * 2 },
2402
    },
2403
);
2404
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
2405
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2406
$result->filter(key1 => sub { $_[0] * 3 });
2407
is($result->one->{key1}, 12);
clenup test
Yuki Kimoto authored on 2011-08-06
2408

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2409
$dbi = DBIx::Custom->connect;
2410
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2411
$dbi->type_rule(
2412
    from1 => {
2413
        date => sub { $_[0] * 2 },
2414
    },
2415
);
2416
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
2417
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2418
$result->filter(key1 => sub { $_[0] * 3 });
2419
is($result->fetch->[0], 12);
clenup test
Yuki Kimoto authored on 2011-08-06
2420

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2421
$dbi = DBIx::Custom->connect;
2422
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2423
$dbi->type_rule(
2424
    into1 => {
2425
        date => sub { $_[0] . 'b' }
2426
    },
2427
    into2 => {
2428
        date => sub { $_[0] . 'c' }
2429
    },
2430
    from1 => {
2431
        date => sub { $_[0] . 'd' }
2432
    },
2433
    from2 => {
2434
        date => sub { $_[0] . 'e' }
2435
    }
2436
);
2437
$dbi->insert({key1 => '1'}, table => 'table1', type_rule_off => 1);
clenup test
Yuki Kimoto authored on 2011-08-06
2438
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2439
is($result->type_rule_off->fetch_first->[0], '1');
clenup test
Yuki Kimoto authored on 2011-08-06
2440
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2441
is($result->type_rule_on->fetch_first->[0], '1de');
clenup test
Yuki Kimoto authored on 2011-08-06
2442

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2443
$dbi = DBIx::Custom->connect;
2444
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2445
$dbi->type_rule(
2446
    into1 => {
2447
        date => sub { $_[0] . 'b' }
cleanup test
Yuki Kimoto authored on 2011-08-06
2448
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
2449
    into2 => {
2450
        date => sub { $_[0] . 'c' }
cleanup test
Yuki Kimoto authored on 2011-08-06
2451
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
2452
    from1 => {
2453
        date => sub { $_[0] . 'd' }
cleanup test
Yuki Kimoto authored on 2011-08-06
2454
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
2455
    from2 => {
2456
        date => sub { $_[0] . 'e' }
cleanup test
Yuki Kimoto authored on 2011-08-06
2457
    }
2458
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2459
$dbi->insert({key1 => '1'}, table => 'table1', type_rule1_off => 1);
2460
$result = $dbi->select(table => 'table1');
2461
is($result->type_rule1_off->fetch_first->[0], '1ce');
2462
$result = $dbi->select(table => 'table1');
2463
is($result->type_rule1_on->fetch_first->[0], '1cde');
cleanup test
Yuki Kimoto authored on 2011-08-06
2464

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2465
$dbi = DBIx::Custom->connect;
2466
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2467
$dbi->type_rule(
2468
    into1 => {
2469
        date => sub { $_[0] . 'b' }
2470
    },
2471
    into2 => {
2472
        date => sub { $_[0] . 'c' }
2473
    },
2474
    from1 => {
2475
        date => sub { $_[0] . 'd' }
2476
    },
2477
    from2 => {
2478
        date => sub { $_[0] . 'e' }
cleanup test
Yuki Kimoto authored on 2011-08-06
2479
    }
2480
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2481
$dbi->insert({key1 => '1'}, table => 'table1', type_rule2_off => 1);
2482
$result = $dbi->select(table => 'table1');
2483
is($result->type_rule2_off->fetch_first->[0], '1bd');
2484
$result = $dbi->select(table => 'table1');
2485
is($result->type_rule2_on->fetch_first->[0], '1bde');
test cleanup
Yuki Kimoto authored on 2011-08-10
2486

            
2487
test 'prefix';
2488
$dbi = DBIx::Custom->connect;
2489
eval { $dbi->execute('drop table table1') };
2490
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
2491
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2492
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace');
2493
$result = $dbi->execute('select * from table1;');
2494
$rows   = $result->all;
2495
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
2496

            
2497
$dbi = DBIx::Custom->connect;
2498
eval { $dbi->execute('drop table table1') };
2499
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
2500
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2501
$dbi->update(table => 'table1', param => {key2 => 4},
2502
  where => {key1 => 1}, prefix => 'or replace');
2503
$result = $dbi->execute('select * from table1;');
2504
$rows   = $result->all;
2505
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
2506

            
2507

            
2508
test 'reserved_word_quote';
2509
$dbi = DBIx::Custom->connect;
2510
eval { $dbi->execute("drop table ${q}table$p") };
2511
$dbi->reserved_word_quote('"');
2512
$dbi->execute($create_table_reserved);
2513
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
2514
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
2515
$dbi->insert(table => 'table', param => {select => 1});
2516
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
2517
$result = $dbi->execute("select * from ${q}table$p");
2518
$rows   = $result->all;
2519
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
2520

            
2521
test 'quote';
2522
$dbi = DBIx::Custom->connect;
2523
$dbi->quote('"');
2524
eval { $dbi->execute("drop table ${q}table$p") };
2525
$dbi->execute($create_table_reserved);
2526
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
2527
$dbi->insert(table => 'table', param => {select => 1});
2528
$dbi->delete(table => 'table', where => {select => 1});
2529
$result = $dbi->execute("select * from ${q}table$p");
2530
$rows   = $result->all;
2531
is_deeply($rows, [], "reserved word");