DBIx-Custom / t / sqlite.t /
Newer Older
2564 lines | 76.013kb
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

            
172
test 'Model class';
173
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
174
$dbi = MyDBI1->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
175
eval { $dbi->execute('drop table book') };
cleanup test
Yuki Kimoto authored on 2011-08-06
176
$dbi->execute("create table book (title, author)");
177
$model = $dbi->model('book');
178
$model->insert({title => 'a', author => 'b'});
179
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
180
$dbi->execute("create table company (name)");
181
$model = $dbi->model('company');
182
$model->insert({name => 'a'});
183
is_deeply($model->list->all, [{name => 'a'}], 'basic');
184
is($dbi->models->{'book'}, $dbi->model('book'));
185
is($dbi->models->{'company'}, $dbi->model('company'));
186

            
187
{
188
    package MyDBI4;
189

            
190
    use strict;
191
    use warnings;
192

            
193
    use base 'DBIx::Custom';
194

            
195
    sub connect {
196
        my $self = shift->SUPER::connect(@_);
197
        
198
        $self->include_model(
199
            MyModel2 => [
200
                'book',
201
                {class => 'Company', name => 'company'}
202
            ]
203
        );
204
    }
205

            
206
    package MyModel2::Base1;
207

            
208
    use strict;
209
    use warnings;
210

            
211
    use base 'DBIx::Custom::Model';
212

            
213
    package MyModel2::book;
214

            
215
    use strict;
216
    use warnings;
217

            
218
    use base 'MyModel2::Base1';
219

            
220
    sub insert {
221
        my ($self, $param) = @_;
222
        
223
        return $self->SUPER::insert(param => $param);
224
    }
225

            
226
    sub list { shift->select; }
227

            
228
    package MyModel2::Company;
229

            
230
    use strict;
231
    use warnings;
232

            
233
    use base 'MyModel2::Base1';
234

            
235
    sub insert {
236
        my ($self, $param) = @_;
237
        
238
        return $self->SUPER::insert(param => $param);
239
    }
240

            
241
    sub list { shift->select; }
242
}
test cleanup
Yuki Kimoto authored on 2011-08-10
243
$dbi = MyDBI4->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
244
eval { $dbi->execute('drop table book') };
cleanup test
Yuki Kimoto authored on 2011-08-06
245
$dbi->execute("create table book (title, author)");
246
$model = $dbi->model('book');
247
$model->insert({title => 'a', author => 'b'});
248
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
249
$dbi->execute("create table company (name)");
250
$model = $dbi->model('company');
251
$model->insert({name => 'a'});
252
is_deeply($model->list->all, [{name => 'a'}], 'basic');
253

            
254
{
255
     package MyDBI5;
256

            
257
    use strict;
258
    use warnings;
259

            
260
    use base 'DBIx::Custom';
261

            
262
    sub connect {
263
        my $self = shift->SUPER::connect(@_);
264
        
265
        $self->include_model('MyModel4');
266
    }
267
}
test cleanup
Yuki Kimoto authored on 2011-08-10
268
$dbi = MyDBI5->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
269
eval { $dbi->execute('drop table company') };
270
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
271
$dbi->execute("create table company (name)");
272
$dbi->execute("create table table1 (key1)");
273
$model = $dbi->model('company');
274
$model->insert({name => 'a'});
275
is_deeply($model->list->all, [{name => 'a'}], 'include all model');
276
$dbi->insert(table => 'table1', param => {key1 => 1});
277
$model = $dbi->model('book');
278
is_deeply($model->list->all, [{key1 => 1}], 'include all model');
279

            
280
test 'primary_key';
281
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
282
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
283
$model = $dbi->model('book');
284
$model->primary_key(['id', 'number']);
285
is_deeply($model->primary_key, ['id', 'number']);
286

            
287
test 'columns';
288
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
289
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
290
$model = $dbi->model('book');
291
$model->columns(['id', 'number']);
292
is_deeply($model->columns, ['id', 'number']);
293

            
294
test 'setup_model';
295
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
296
$dbi = MyDBI1->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
297
eval { $dbi->execute('drop table book') };
298
eval { $dbi->execute('drop table company') };
299
eval { $dbi->execute('drop table test') };
300

            
cleanup test
Yuki Kimoto authored on 2011-08-06
301
$dbi->execute('create table book (id)');
302
$dbi->execute('create table company (id, name);');
303
$dbi->execute('create table test (id, name, primary key (id, name));');
304
$dbi->setup_model;
305
is_deeply($dbi->model('book')->columns, ['id']);
306
is_deeply($dbi->model('company')->columns, ['id', 'name']);
307

            
308
test 'delete_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
309
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
310
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
311
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
312
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
313
$dbi->delete_at(
314
    table => 'table1',
315
    primary_key => ['key1', 'key2'],
316
    where => [1, 2],
317
);
318
is_deeply($dbi->select(table => 'table1')->all, []);
319

            
320
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
321
$dbi->delete_at(
322
    table => 'table1',
323
    primary_key => 'key1',
324
    where => 1,
325
);
326
is_deeply($dbi->select(table => 'table1')->all, []);
327

            
328
test 'insert_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
329
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
330
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
331
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
332
$dbi->insert_at(
333
    primary_key => ['key1', 'key2'], 
334
    table => 'table1',
335
    where => [1, 2],
336
    param => {key3 => 3}
337
);
338
is($dbi->select(table => 'table1')->one->{key1}, 1);
339
is($dbi->select(table => 'table1')->one->{key2}, 2);
340
is($dbi->select(table => 'table1')->one->{key3}, 3);
341

            
342
$dbi->delete_all(table => 'table1');
343
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
344
$dbi->insert_at(
345
    primary_key => 'key1', 
346
    table => 'table1',
347
    where => 1,
348
    param => {key2 => 2, key3 => 3}
349
);
350

            
351
is($dbi->select(table => 'table1')->one->{key1}, 1);
352
is($dbi->select(table => 'table1')->one->{key2}, 2);
353
is($dbi->select(table => 'table1')->one->{key3}, 3);
354

            
355
eval {
356
    $dbi->insert_at(
357
        table => 'table1',
358
        primary_key => ['key1', 'key2'],
359
        where => {},
360
        param => {key1 => 1, key2 => 2, key3 => 3},
361
    );
362
};
363
like($@, qr/must be/);
364

            
test cleanup
Yuki Kimoto authored on 2011-08-10
365
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
366
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
367
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
368
$dbi->insert_at(
369
    {key3 => 3},
370
    primary_key => ['key1', 'key2'], 
371
    table => 'table1',
372
    where => [1, 2],
373
);
374
is($dbi->select(table => 'table1')->one->{key1}, 1);
375
is($dbi->select(table => 'table1')->one->{key2}, 2);
376
is($dbi->select(table => 'table1')->one->{key3}, 3);
377

            
378
test 'update_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
379
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
380
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
381
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
382
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
383
$dbi->update_at(
384
    table => 'table1',
385
    primary_key => ['key1', 'key2'],
386
    where => [1, 2],
387
    param => {key3 => 4}
388
);
389
is($dbi->select(table => 'table1')->one->{key1}, 1);
390
is($dbi->select(table => 'table1')->one->{key2}, 2);
391
is($dbi->select(table => 'table1')->one->{key3}, 4);
392

            
393
$dbi->delete_all(table => 'table1');
394
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
395
$dbi->update_at(
396
    table => 'table1',
397
    primary_key => 'key1',
398
    where => 1,
399
    param => {key3 => 4}
400
);
401
is($dbi->select(table => 'table1')->one->{key1}, 1);
402
is($dbi->select(table => 'table1')->one->{key2}, 2);
403
is($dbi->select(table => 'table1')->one->{key3}, 4);
404

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

            
419
test 'select_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
420
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
421
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
422
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
423
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
424
$result = $dbi->select_at(
425
    table => 'table1',
426
    primary_key => ['key1', 'key2'],
427
    where => [1, 2]
428
);
429
$row = $result->one;
430
is($row->{key1}, 1);
431
is($row->{key2}, 2);
432
is($row->{key3}, 3);
433

            
434
$dbi->delete_all(table => 'table1');
435
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
436
$result = $dbi->select_at(
437
    table => 'table1',
438
    primary_key => 'key1',
439
    where => 1,
440
);
441
$row = $result->one;
442
is($row->{key1}, 1);
443
is($row->{key2}, 2);
444
is($row->{key3}, 3);
445

            
446
$dbi->delete_all(table => 'table1');
447
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
448
$result = $dbi->select_at(
449
    table => 'table1',
450
    primary_key => ['key1', 'key2'],
451
    where => [1, 2]
452
);
453
$row = $result->one;
454
is($row->{key1}, 1);
455
is($row->{key2}, 2);
456
is($row->{key3}, 3);
457

            
458
eval {
459
    $result = $dbi->select_at(
460
        table => 'table1',
461
        primary_key => ['key1', 'key2'],
462
        where => {},
463
    );
464
};
465
like($@, qr/must be/);
466

            
467
eval {
468
    $result = $dbi->select_at(
469
        table => 'table1',
470
        primary_key => ['key1', 'key2'],
471
        where => [1],
472
    );
473
};
474
like($@, qr/same/);
475

            
476
eval {
477
    $result = $dbi->update_at(
478
        table => 'table1',
479
        primary_key => ['key1', 'key2'],
480
        where => {},
481
        param => {key1 => 1, key2 => 2},
482
    );
483
};
484
like($@, qr/must be/);
485

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

            
495
test 'columns';
496
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
497
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
498
$model = $dbi->model('book');
499

            
500

            
501
test 'model delete_at';
502
{
503
    package MyDBI6;
504
    
505
    use base 'DBIx::Custom';
506
    
507
    sub connect {
508
        my $self = shift->SUPER::connect(@_);
509
        
510
        $self->include_model('MyModel5');
511
        
512
        return $self;
513
    }
514
}
test cleanup
Yuki Kimoto authored on 2011-08-10
515
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
516
eval { $dbi->execute('drop table table1') };
517
eval { $dbi->execute('drop table table2') };
518
eval { $dbi->execute('drop table table3') };
cleanup test
Yuki Kimoto authored on 2011-08-10
519
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-10
520
$dbi->execute($create_table2_2);
521
$dbi->execute($create_table3);
cleanup test
Yuki Kimoto authored on 2011-08-06
522
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
523
$dbi->model('table1')->delete_at(where => [1, 2]);
524
is_deeply($dbi->select(table => 'table1')->all, []);
525
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
526
$dbi->model('table1_1')->delete_at(where => [1, 2]);
527
is_deeply($dbi->select(table => 'table1')->all, []);
528
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
529
$dbi->model('table1_3')->delete_at(where => [1, 2]);
530
is_deeply($dbi->select(table => 'table1')->all, []);
531

            
532
test 'model insert_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
533
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
534
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
535
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
536
$dbi->model('table1')->insert_at(
537
    where => [1, 2],
538
    param => {key3 => 3}
539
);
540
$result = $dbi->model('table1')->select;
541
$row = $result->one;
542
is($row->{key1}, 1);
543
is($row->{key2}, 2);
544
is($row->{key3}, 3);
545

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

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

            
572

            
573
test 'mycolumn and column';
574
{
575
    package MyDBI7;
576
    
577
    use base 'DBIx::Custom';
578
    
579
    sub connect {
580
        my $self = shift->SUPER::connect(@_);
581
        
582
        $self->include_model('MyModel6');
583
        
584
        
585
        return $self;
586
    }
587
}
test cleanup
Yuki Kimoto authored on 2011-08-10
588
$dbi = MyDBI7->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
589
eval { $dbi->execute('drop table table1') };
590
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
591
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
592
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
593
$dbi->separator('__');
594
$dbi->setup_model;
595
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
596
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
597
$model = $dbi->model('table1');
598
$result = $model->select(
599
    column => [$model->mycolumn, $model->column('table2')],
600
    where => {'table1.key1' => 1}
601
);
602
is_deeply($result->one,
603
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
604

            
605
test 'update_param';
test cleanup
Yuki Kimoto authored on 2011-08-10
606
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
607
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
608
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
609
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
610
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
611

            
612
$param = {key2 => 11};
613
$update_param = $dbi->update_param($param);
614
$sql = <<"EOS";
615
update table1 $update_param
616
where key1 = 1
617
EOS
618
$dbi->execute($sql, param => $param);
test cleanup
Yuki Kimoto authored on 2011-08-06
619
$result = $dbi->execute('select * from table1;', table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
620
$rows   = $result->all;
621
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
622
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
623
                  "basic");
624

            
625

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

            
632
$param = {key2 => 11, key3 => 33};
633
$update_param = $dbi->update_param($param);
634
$sql = <<"EOS";
635
update table1 $update_param
636
where key1 = 1
637
EOS
638
$dbi->execute($sql, param => $param);
test cleanup
Yuki Kimoto authored on 2011-08-06
639
$result = $dbi->execute('select * from table1;', table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
640
$rows   = $result->all;
641
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
642
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
643
                  "basic");
644

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

            
651
$param = {key2 => 11, key3 => 33};
652
$update_param = $dbi->update_param($param, {no_set => 1});
653
$sql = <<"EOS";
654
update table1 set $update_param
655
where key1 = 1
656
EOS
657
$dbi->execute($sql, param => $param);
test cleanup
Yuki Kimoto authored on 2011-08-06
658
$result = $dbi->execute('select * from table1;', table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
659
$rows   = $result->all;
660
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
661
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
662
                  "update param no_set");
663

            
664
            
665
eval { $dbi->update_param({";" => 1}) };
666
like($@, qr/not safety/);
667

            
668

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

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

            
689

            
690
test 'insert_param';
test cleanup
Yuki Kimoto authored on 2011-08-10
691
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
692
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
693
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
694
$param = {key1 => 1, key2 => 2};
695
$insert_param = $dbi->insert_param($param);
696
$sql = <<"EOS";
697
insert into table1 $insert_param
698
EOS
699
$dbi->execute($sql, param => $param, table => 'table1');
700
is($dbi->select(table => 'table1')->one->{key1}, 1);
701
is($dbi->select(table => 'table1')->one->{key2}, 2);
702

            
test cleanup
Yuki Kimoto authored on 2011-08-10
703
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
704
$dbi->quote('"');
cleanup test
Yuki Kimoto authored on 2011-08-10
705
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
706
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
707
$param = {key1 => 1, key2 => 2};
708
$insert_param = $dbi->insert_param($param);
709
$sql = <<"EOS";
710
insert into table1 $insert_param
711
EOS
712
$dbi->execute($sql, param => $param, table => 'table1');
713
is($dbi->select(table => 'table1')->one->{key1}, 1);
714
is($dbi->select(table => 'table1')->one->{key2}, 2);
715

            
716
eval { $dbi->insert_param({";" => 1}) };
717
like($@, qr/not safety/);
718

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

            
720
test 'join';
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);
cleanup test
Yuki Kimoto authored on 2011-08-06
724
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
725
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-10
726
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
727
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
cleanup test
Yuki Kimoto authored on 2011-08-06
728
$dbi->execute('create table table3 (key3 int, key4 int);');
cleanup test
Yuki Kimoto authored on 2011-08-06
729
$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4});
730
$rows = $dbi->select(
731
    table => 'table1',
732
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
733
    where   => {'table1.key2' => 2},
734
    join  => ['left outer join table2 on table1.key1 = table2.key1']
735
)->all;
736
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]);
737

            
738
$rows = $dbi->select(
739
    table => 'table1',
740
    where   => {'key1' => 1},
741
    join  => ['left outer join table2 on table1.key1 = table2.key1']
742
)->all;
743
is_deeply($rows, [{key1 => 1, key2 => 2}]);
744

            
745
eval {
746
    $rows = $dbi->select(
747
        table => 'table1',
748
        column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
749
        where   => {'table1.key2' => 2},
750
        join  => {'table1.key1' => 'table2.key1'}
751
    );
752
};
753
like ($@, qr/array/);
754

            
755
$rows = $dbi->select(
756
    table => 'table1',
757
    where   => {'key1' => 1},
758
    join  => ['left outer join table2 on table1.key1 = table2.key1',
759
              'left outer join table3 on table2.key3 = table3.key3']
760
)->all;
761
is_deeply($rows, [{key1 => 1, key2 => 2}]);
762

            
763
$rows = $dbi->select(
764
    column => 'table3.key4 as table3__key4',
765
    table => 'table1',
766
    where   => {'table1.key1' => 1},
767
    join  => ['left outer join table2 on table1.key1 = table2.key1',
768
              'left outer join table3 on table2.key3 = table3.key3']
769
)->all;
770
is_deeply($rows, [{table3__key4 => 4}]);
771

            
772
$rows = $dbi->select(
773
    column => 'table1.key1 as table1__key1',
774
    table => 'table1',
775
    where   => {'table3.key4' => 4},
776
    join  => ['left outer join table2 on table1.key1 = table2.key1',
777
              'left outer join table3 on table2.key3 = table3.key3']
778
)->all;
779
is_deeply($rows, [{table1__key1 => 1}]);
780

            
test cleanup
Yuki Kimoto authored on 2011-08-10
781
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
782
$dbi->quote('"');
cleanup test
Yuki Kimoto authored on 2011-08-10
783
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
784
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
785
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
786
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
787
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
788
$rows = $dbi->select(
789
    table => 'table1',
790
    column => '"table1"."key1" as "table1_key1", "table2"."key1" as "table2_key1", "key2", "key3"',
791
    where   => {'table1.key2' => 2},
792
    join  => ['left outer join "table2" on "table1"."key1" = "table2"."key1"'],
793
)->all;
794
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}],
795
          'quote');
796

            
797
{
798
    package MyDBI8;
799
    
800
    use base 'DBIx::Custom';
801
    
802
    sub connect {
803
        my $self = shift->SUPER::connect(@_);
804
        
805
        $self->include_model('MyModel7');
806
        
807
        return $self;
808
    }
809
}
cleanup test
Yuki Kimoto authored on 2011-08-06
810

            
test cleanup
Yuki Kimoto authored on 2011-08-10
811
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
812
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
813
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
814
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
815
$sql = <<"EOS";
cleanup test
Yuki Kimoto authored on 2011-08-06
816
left outer join (
817
  select * from table1 as t1
818
  where t1.key2 = (
819
    select max(t2.key2) from table1 as t2
820
    where t1.key1 = t2.key1
821
  )
822
) as latest_table1 on table1.key1 = latest_table1.key1
823
EOS
cleanup test
Yuki Kimoto authored on 2011-08-06
824
$join = [$sql];
825
$rows = $dbi->select(
826
    table => 'table1',
827
    column => 'latest_table1.key1 as latest_table1__key1',
828
    join  => $join
829
)->all;
830
is_deeply($rows, [{latest_table1__key1 => 1}]);
831

            
test cleanup
Yuki Kimoto authored on 2011-08-10
832
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
833
eval { $dbi->execute('drop table table1') };
834
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
835
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
836
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
837
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
838
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
839
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
840
$result = $dbi->select(
841
    table => 'table1',
842
    join => [
843
        "left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1"
844
    ]
845
);
846
is_deeply($result->all, [{key1 => 1, key2 => 2}]);
847
$result = $dbi->select(
848
    table => 'table1',
849
    column => [{table2 => ['key3']}],
850
    join => [
851
        "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1"
852
    ]
853
);
854
is_deeply($result->all, [{'table2.key3' => 4}]);
855
$result = $dbi->select(
856
    table => 'table1',
857
    column => [{table2 => ['key3']}],
858
    join => [
859
        "left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'"
860
    ]
861
);
862
is_deeply($result->all, [{'table2.key3' => 4}]);
863

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

            
884
test 'mycolumn';
test cleanup
Yuki Kimoto authored on 2011-08-10
885
$dbi = MyDBI8->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
886
eval { $dbi->execute('drop table table1') };
887
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
888
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
889
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
890
$dbi->setup_model;
891
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
892
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
893
$model = $dbi->model('table1');
894
$result = $model->select_at(
895
    column => [
896
        $model->mycolumn,
897
        $model->column('table2')
898
    ]
899
);
900
is_deeply($result->one,
901
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
902

            
903
$result = $model->select_at(
904
    column => [
905
        $model->mycolumn(['key1']),
906
        $model->column(table2 => ['key1'])
907
    ]
908
);
909
is_deeply($result->one,
910
          {key1 => 1, 'table2.key1' => 1});
911
$result = $model->select_at(
912
    column => [
913
        $model->mycolumn(['key1']),
914
        {table2 => ['key1']}
915
    ]
916
);
917
is_deeply($result->one,
918
          {key1 => 1, 'table2.key1' => 1});
919

            
920
$result = $model->select_at(
921
    column => [
922
        $model->mycolumn(['key1']),
923
        ['table2.key1', as => 'table2.key1']
924
    ]
925
);
926
is_deeply($result->one,
927
          {key1 => 1, 'table2.key1' => 1});
928

            
929
$result = $model->select_at(
930
    column => [
931
        $model->mycolumn(['key1']),
932
        ['table2.key1' => 'table2.key1']
933
    ]
934
);
935
is_deeply($result->one,
936
          {key1 => 1, 'table2.key1' => 1});
937

            
938
test 'dbi method from model';
939
{
940
    package MyDBI9;
941
    
942
    use base 'DBIx::Custom';
943
    
944
    sub connect {
945
        my $self = shift->SUPER::connect(@_);
946
        
947
        $self->include_model('MyModel8')->setup_model;
948
        
949
        return $self;
950
    }
951
}
test cleanup
Yuki Kimoto authored on 2011-08-10
952
$dbi = MyDBI9->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
953
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
954
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
955
$model = $dbi->model('table1');
956
eval{$model->execute('select * from table1')};
957
ok(!$@);
958

            
959
test 'column table option';
test cleanup
Yuki Kimoto authored on 2011-08-10
960
$dbi = MyDBI9->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
961
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
962
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
963
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
964
$dbi->setup_model;
965
$dbi->execute('insert into table1 (key1, key2) values (1, 2);');
966
$dbi->execute('insert into table2 (key1, key3) values (1, 4);');
967
$model = $dbi->model('table1');
968
$result = $model->select(
969
    column => [
970
        $model->column('table2', {alias => 'table2_alias'})
971
    ],
972
    where => {'table2_alias.key3' => 4}
973
);
974
is_deeply($result->one, 
975
          {'table2_alias.key1' => 1, 'table2_alias.key3' => 4});
976

            
977
$dbi->separator('__');
978
$result = $model->select(
979
    column => [
980
        $model->column('table2', {alias => 'table2_alias'})
981
    ],
982
    where => {'table2_alias.key3' => 4}
983
);
984
is_deeply($result->one, 
985
          {'table2_alias__key1' => 1, 'table2_alias__key3' => 4});
986

            
987
$dbi->separator('-');
988
$result = $model->select(
989
    column => [
990
        $model->column('table2', {alias => 'table2_alias'})
991
    ],
992
    where => {'table2_alias.key3' => 4}
993
);
994
is_deeply($result->one, 
995
          {'table2_alias-key1' => 1, 'table2_alias-key3' => 4});
996

            
997
test 'type option'; # DEPRECATED!
998
$dbi = DBIx::Custom->connect(
999
    dbi_option => {
1000
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
1001
    }
1002
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1003
$binary = pack("I3", 1, 2, 3);
1004
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
1005
$dbi->execute('create table table1(key1, key2)');
1006
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
1007
$result = $dbi->select(table => 'table1');
1008
$row   = $result->one;
1009
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
1010
$result = $dbi->execute('select length(key1) as key1_length from table1');
1011
$row = $result->one;
1012
is($row->{key1_length}, length $binary);
1013

            
1014
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [['key1'] => DBI::SQL_BLOB]);
1015
$result = $dbi->select(table => 'table1');
1016
$row   = $result->one;
1017
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
1018
$result = $dbi->execute('select length(key1) as key1_length from table1');
1019
$row = $result->one;
1020
is($row->{key1_length}, length $binary);
1021

            
1022

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

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

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

            
1066
test 'create_model';
test cleanup
Yuki Kimoto authored on 2011-08-10
1067
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1068
eval { $dbi->execute('drop table table1') };
1069
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1070
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1071
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1072

            
1073
$dbi->create_model(
1074
    table => 'table1',
1075
    join => [
1076
       'left outer join table2 on table1.key1 = table2.key1'
1077
    ],
1078
    primary_key => ['key1']
1079
);
1080
$model2 = $dbi->create_model(
1081
    table => 'table2'
1082
);
1083
$dbi->create_model(
1084
    table => 'table3',
1085
    filter => [
1086
        key1 => {in => sub { uc $_[0] }}
1087
    ]
1088
);
1089
$dbi->setup_model;
1090
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1091
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1092
$model = $dbi->model('table1');
1093
$result = $model->select(
1094
    column => [$model->mycolumn, $model->column('table2')],
1095
    where => {'table1.key1' => 1}
1096
);
1097
is_deeply($result->one,
1098
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
1099
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
1100

            
1101
test 'model method';
1102
test 'create_model';
test cleanup
Yuki Kimoto authored on 2011-08-10
1103
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1104
eval { $dbi->execute('drop table table2') };
test cleanup
Yuki Kimoto authored on 2011-08-10
1105
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1106
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1107
$model = $dbi->create_model(
1108
    table => 'table2'
1109
);
1110
$model->method(foo => sub { shift->select(@_) });
1111
is_deeply($model->foo->one, {key1 => 1, key3 => 3});
1112

            
1113
test 'merge_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
1114
$dbi = DBIx::Custom->new;
1115
$params = [
1116
    {key1 => 1, key2 => 2, key3 => 3},
1117
    {key1 => 1, key2 => 2},
1118
    {key1 => 1}
1119
];
1120
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]);
1121
is_deeply($param, {key1 => [1, 1, 1], key2 => [2, 2], key3 => 3});
1122

            
1123
$params = [
1124
    {key1 => [1, 2], key2 => 1, key3 => [1, 2]},
1125
    {key1 => [3, 4], key2 => [2, 3], key3 => 3}
1126
];
1127
$param = $dbi->merge_param($params->[0], $params->[1]);
1128
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
1129

            
1130
test 'select() param option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1131
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1132
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1133
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1134
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1135
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
1136
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1137
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
1138
$dbi->insert(table => 'table2', param => {key1 => 2, key3 => 5});
1139
$rows = $dbi->select(
1140
    table => 'table1',
1141
    column => 'table1.key1 as table1_key1, key2, key3',
1142
    where   => {'table1.key2' => 3},
1143
    join  => ['inner join (select * from table2 where {= table2.key3})' . 
1144
              ' as table2 on table1.key1 = table2.key1'],
1145
    param => {'table2.key3' => 5}
1146
)->all;
1147
is_deeply($rows, [{table1_key1 => 2, key2 => 3, key3 => 5}]);
1148

            
1149

            
1150
test 'select() wrap option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1151
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1152
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1153
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1154
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1155
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
1156
$rows = $dbi->select(
1157
    table => 'table1',
1158
    column => 'key1',
1159
    wrap => ['select * from (', ') as t where key1 = 1']
1160
)->all;
1161
is_deeply($rows, [{key1 => 1}]);
1162

            
1163
eval {
1164
$dbi->select(
1165
    table => 'table1',
1166
    column => 'key1',
1167
    wrap => 'select * from ('
1168
)
1169
};
1170
like($@, qr/array/);
1171

            
1172
test 'select() string where';
test cleanup
Yuki Kimoto authored on 2011-08-10
1173
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1174
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1175
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1176
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1177
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
1178
$rows = $dbi->select(
1179
    table => 'table1',
1180
    where => 'key1 = :key1 and key2 = :key2',
1181
    where_param => {key1 => 1, key2 => 2}
1182
)->all;
1183
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1184

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1185
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1186
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1187
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1188
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1189
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
1190
$rows = $dbi->select(
1191
    table => 'table1',
1192
    where => [
1193
        'key1 = :key1 and key2 = :key2',
1194
        {key1 => 1, key2 => 2}
1195
    ]
1196
)->all;
1197
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1198

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

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

            
1228

            
1229
test 'update() 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->update(
1235
    table => 'table1',
1236
    param => {key1 => 5},
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 => 5, key2 => 2}]);
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->update(
1248
    table => 'table1',
1249
    param => {key1 => 5},
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 => 5, key2 => 2}]);
1257

            
1258
test 'insert id and primary_key option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1259
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1260
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1261
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1262
$dbi->insert(
1263
    primary_key => ['key1', 'key2'], 
1264
    table => 'table1',
1265
    id => [1, 2],
1266
    param => {key3 => 3}
1267
);
1268
is($dbi->select(table => 'table1')->one->{key1}, 1);
1269
is($dbi->select(table => 'table1')->one->{key2}, 2);
1270
is($dbi->select(table => 'table1')->one->{key3}, 3);
1271

            
1272
$dbi->delete_all(table => 'table1');
1273
$dbi->insert(
1274
    primary_key => 'key1', 
1275
    table => 'table1',
1276
    id => 0,
1277
    param => {key2 => 2, key3 => 3}
1278
);
1279

            
1280
is($dbi->select(table => 'table1')->one->{key1}, 0);
1281
is($dbi->select(table => 'table1')->one->{key2}, 2);
1282
is($dbi->select(table => 'table1')->one->{key3}, 3);
1283

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

            
1297

            
1298
test 'model insert id and primary_key option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1299
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1300
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1301
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1302
$dbi->model('table1')->insert(
1303
    id => [1, 2],
1304
    param => {key3 => 3}
1305
);
1306
$result = $dbi->model('table1')->select;
1307
$row = $result->one;
1308
is($row->{key1}, 1);
1309
is($row->{key2}, 2);
1310
is($row->{key3}, 3);
1311

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

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

            
1340
$dbi->delete_all(table => 'table1');
1341
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
1342
$dbi->update(
1343
    table => 'table1',
1344
    primary_key => 'key1',
1345
    id => 0,
1346
    param => {key3 => 4}
1347
);
1348
is($dbi->select(table => 'table1')->one->{key1}, 0);
1349
is($dbi->select(table => 'table1')->one->{key2}, 2);
1350
is($dbi->select(table => 'table1')->one->{key3}, 4);
1351

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

            
1366

            
1367
test 'model update and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1368
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1369
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1370
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1371
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1372
$dbi->model('table1')->update(
1373
    id => [1, 2],
1374
    param => {key3 => 4}
1375
);
1376
$result = $dbi->model('table1')->select;
1377
$row = $result->one;
1378
is($row->{key1}, 1);
1379
is($row->{key2}, 2);
1380
is($row->{key3}, 4);
1381

            
1382

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

            
1395
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
1396
$dbi->delete(
1397
    table => 'table1',
1398
    primary_key => 'key1',
1399
    id => 0,
1400
);
1401
is_deeply($dbi->select(table => 'table1')->all, []);
1402

            
1403

            
1404
test 'model delete and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1405
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1406
eval { $dbi->execute('drop table table1') };
1407
eval { $dbi->execute('drop table table2') };
1408
eval { $dbi->execute('drop table table3') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1409
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-10
1410
$dbi->execute($create_table2_2);
1411
$dbi->execute($create_table3);
cleanup test
Yuki Kimoto authored on 2011-08-06
1412
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1413
$dbi->model('table1')->delete(id => [1, 2]);
1414
is_deeply($dbi->select(table => 'table1')->all, []);
1415
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
1416
$dbi->model('table1_1')->delete(id => [1, 2]);
1417
is_deeply($dbi->select(table => 'table1')->all, []);
1418
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
1419
$dbi->model('table1_3')->delete(id => [1, 2]);
1420
is_deeply($dbi->select(table => 'table1')->all, []);
1421

            
1422

            
1423
test 'select and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1424
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1425
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1426
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1427
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1428
$result = $dbi->select(
1429
    table => 'table1',
1430
    primary_key => ['key1', 'key2'],
1431
    id => [1, 2]
1432
);
1433
$row = $result->one;
1434
is($row->{key1}, 1);
1435
is($row->{key2}, 2);
1436
is($row->{key3}, 3);
1437

            
1438
$dbi->delete_all(table => 'table1');
1439
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
1440
$result = $dbi->select(
1441
    table => 'table1',
1442
    primary_key => 'key1',
1443
    id => 0,
1444
);
1445
$row = $result->one;
1446
is($row->{key1}, 0);
1447
is($row->{key2}, 2);
1448
is($row->{key3}, 3);
1449

            
1450
$dbi->delete_all(table => 'table1');
1451
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1452
$result = $dbi->select(
1453
    table => 'table1',
1454
    primary_key => ['key1', 'key2'],
1455
    id => [1, 2]
1456
);
1457
$row = $result->one;
1458
is($row->{key1}, 1);
1459
is($row->{key2}, 2);
1460
is($row->{key3}, 3);
1461

            
1462

            
1463
test 'model select_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
1464
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1465
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1466
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1467
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1468
$result = $dbi->model('table1')->select(id => [1, 2]);
1469
$row = $result->one;
1470
is($row->{key1}, 1);
1471
is($row->{key2}, 2);
1472
is($row->{key3}, 3);
1473

            
1474
test 'column separator is default .';
test cleanup
Yuki Kimoto authored on 2011-08-10
1475
$dbi = MyDBI7->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1476
eval { $dbi->execute('drop table table1') };
1477
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1478
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1479
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1480
$dbi->setup_model;
1481
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1482
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1483
$model = $dbi->model('table1');
1484
$result = $model->select(
1485
    column => [$model->column('table2')],
1486
    where => {'table1.key1' => 1}
1487
);
1488
is_deeply($result->one,
1489
          {'table2.key1' => 1, 'table2.key3' => 3});
1490

            
1491
$result = $model->select(
1492
    column => [$model->column('table2' => [qw/key1 key3/])],
1493
    where => {'table1.key1' => 1}
1494
);
1495
is_deeply($result->one,
1496
          {'table2.key1' => 1, 'table2.key3' => 3});
1497

            
1498

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

            
1500
test 'separator';
test cleanup
Yuki Kimoto authored on 2011-08-10
1501
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1502
eval { $dbi->execute('drop table table1') };
1503
eval { $dbi->execute('drop table table2') };
1504
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1505
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-10
1506

            
1507
$dbi->create_model(
1508
    table => 'table1',
1509
    join => [
1510
       'left outer join table2 on table1.key1 = table2.key1'
1511
    ],
1512
    primary_key => ['key1'],
cleanup test
Yuki Kimoto authored on 2011-08-06
1513
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1514
$model2 = $dbi->create_model(
1515
    table => 'table2',
1516
);
1517
$dbi->setup_model;
1518
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1519
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1520
$model = $dbi->model('table1');
1521
$result = $model->select(
1522
    column => [
1523
        $model->mycolumn,
1524
        {table2 => [qw/key1 key3/]}
1525
    ],
1526
    where => {'table1.key1' => 1}
1527
);
1528
is_deeply($result->one,
1529
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
1530
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
1531

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1532
$dbi->separator('__');
1533
$model = $dbi->model('table1');
1534
$result = $model->select(
1535
    column => [
1536
        $model->mycolumn,
1537
        {table2 => [qw/key1 key3/]}
1538
    ],
1539
    where => {'table1.key1' => 1}
1540
);
1541
is_deeply($result->one,
1542
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
1543
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
1544

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1545
$dbi->separator('-');
1546
$model = $dbi->model('table1');
1547
$result = $model->select(
1548
    column => [
1549
        $model->mycolumn,
1550
        {table2 => [qw/key1 key3/]}
1551
    ],
1552
    where => {'table1.key1' => 1}
1553
);
1554
is_deeply($result->one,
1555
          {key1 => 1, key2 => 2, 'table2-key1' => 1, 'table2-key3' => 3});
1556
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
1557

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

            
1559
test 'filter_off';
test cleanup
Yuki Kimoto authored on 2011-08-10
1560
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1561
eval { $dbi->execute('drop table table1') };
1562
eval { $dbi->execute('drop table table2') };
1563
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1564
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-10
1565

            
1566
$dbi->create_model(
1567
    table => 'table1',
1568
    join => [
1569
       'left outer join table2 on table1.key1 = table2.key1'
1570
    ],
1571
    primary_key => ['key1'],
cleanup test
Yuki Kimoto authored on 2011-08-06
1572
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1573
$dbi->setup_model;
1574
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1575
$model = $dbi->model('table1');
1576
$result = $model->select(column => 'key1');
1577
$result->filter(key1 => sub { $_[0] * 2 });
1578
is_deeply($result->one, {key1 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-06
1579

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1580
test 'available_datetype';
test cleanup
Yuki Kimoto authored on 2011-08-10
1581
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1582
ok($dbi->can('available_datatype'));
1583

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1585
test 'select prefix option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1586
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1587
eval { $dbi->execute('drop table table1') };
1588
$dbi->execute($create_table1);
1589
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1590
$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all;
1591
is_deeply($rows, [{key1 => 1, key2 => 2}], "table");
1592

            
1593

            
1594
test 'separator';
1595
$dbi = DBIx::Custom->connect;
1596
is($dbi->separator, '.');
1597
$dbi->separator('-');
1598
is($dbi->separator, '-');
1599
$dbi->separator('__');
1600
is($dbi->separator, '__');
1601
eval { $dbi->separator('?') };
1602
like($@, qr/Separator/);
1603

            
1604

            
1605
test 'map_param';
1606
$dbi = DBIx::Custom->connect;
1607
$param = $dbi->map_param(
1608
    {id => 1, author => 'Ken', price => 1900},
1609
    id => 'book.id',
1610
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1611
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
cleanup test
Yuki Kimoto authored on 2011-08-06
1612
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1613
is_deeply($param, {'book.id' => 1, 'book.author' => '%Ken%',
1614
  'book.price' => 1900});
1615

            
1616
$param = $dbi->map_param(
1617
    {id => 0, author => 0, price => 0},
1618
    id => 'book.id',
1619
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1620
    price => ['book.price', sub { '%' . $_[0] . '%' },
1621
      {if => sub { $_[0] eq 0 }}]
cleanup test
Yuki Kimoto authored on 2011-08-06
1622
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1623
is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
cleanup test
Yuki Kimoto authored on 2011-08-06
1624

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1625
$param = $dbi->map_param(
1626
    {id => '', author => '', price => ''},
1627
    id => 'book.id',
1628
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1629
    price => ['book.price', sub { '%' . $_[0] . '%' },
1630
      {if => sub { $_[0] eq 1 }}]
cleanup test
Yuki Kimoto authored on 2011-08-06
1631
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1632
is_deeply($param, {});
1633

            
1634
$param = $dbi->map_param(
1635
    {id => undef, author => undef, price => undef},
1636
    id => 'book.id',
1637
    price => ['book.price', {if => 'exists'}]
cleanup test
Yuki Kimoto authored on 2011-08-06
1638
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1639
is_deeply($param, {'book.price' => undef});
1640

            
1641
$param = $dbi->map_param(
1642
    {price => 'a'},
1643
    id => ['book.id', {if => 'exists'}],
1644
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
1645
);
1646
is_deeply($param, {'book.price' => '%a'});
cleanup test
Yuki Kimoto authored on 2011-08-06
1647

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

            
1649
test 'table_alias';
test cleanup
Yuki Kimoto authored on 2011-08-10
1650
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1651
eval { $dbi->execute('drop table table1') };
1652
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
cleanup test
Yuki Kimoto authored on 2011-08-06
1653
$dbi->type_rule(
1654
    into1 => {
cleanup test
Yuki Kimoto authored on 2011-08-10
1655
        date => sub { uc $_[0] }
cleanup test
Yuki Kimoto authored on 2011-08-06
1656
    }
1657
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1658
$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'},
1659
  table_alias => {table2 => 'table1'});
cleanup test
Yuki Kimoto authored on 2011-08-06
1660
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1661
is($result->one->{key1}, 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
1662

            
1663

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1664
test 'order';
test cleanup
Yuki Kimoto authored on 2011-08-10
1665
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1666
eval { $dbi->execute('drop table table1') };
1667
$dbi->execute("create table table1 (key1, key2)");
1668
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
1669
$dbi->insert({key1 => 1, key2 => 3}, table => 'table1');
1670
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
1671
$dbi->insert({key1 => 2, key2 => 4}, table => 'table1');
1672
my $order = $dbi->order;
1673
$order->prepend('key1', 'key2 desc');
1674
$result = $dbi->select(table => 'table1', append => "$order");
1675
is_deeply($result->all, [{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1},
1676
  {key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}]);
1677
$order->prepend('key1 desc');
1678
$result = $dbi->select(table => 'table1', append => "$order");
1679
is_deeply($result->all, [{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2},
1680
  {key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
1681

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1682
$order = $dbi->order;
1683
$order->prepend(['table1-key1'], [qw/table1-key2 desc/]);
1684
$result = $dbi->select(table => 'table1',
1685
  column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']],
1686
  append => "$order");
1687
is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3},
1688
  {'table1-key1' => 1, 'table1-key2' => 1},
1689
  {'table1-key1' => 2, 'table1-key2' => 4},
1690
  {'table1-key1' => 2, 'table1-key2' => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
1691

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1692
test 'tag_parse';
test cleanup
Yuki Kimoto authored on 2011-08-10
1693
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1694
$dbi->tag_parse(0);
1695
eval { $dbi->execute('drop table table1') };
1696
$dbi->execute("create table table1 (key1, key2)");
1697
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
1698
eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})};
1699
ok($@);
cleanup test
Yuki Kimoto authored on 2011-08-06
1700

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1701
test 'last_sql';
test cleanup
Yuki Kimoto authored on 2011-08-10
1702
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1703
eval { $dbi->execute('drop table table1') };
1704
$dbi->execute("create table table1 (key1, key2)");
1705
$dbi->execute('select * from table1');
1706
is($dbi->last_sql, 'select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
1707

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1711
test 'DBIx::Custom header';
test cleanup
Yuki Kimoto authored on 2011-08-10
1712
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1713
eval { $dbi->execute('drop table table1') };
1714
$dbi->execute("create table table1 (key1, key2)");
1715
$result = $dbi->execute('select key1 as h1, key2 as h2 from table1');
1716
is_deeply($result->header, [qw/h1 h2/]);
cleanup test
Yuki Kimoto authored on 2011-08-06
1717

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

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1739
$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}";
1740
$result = $dbi->execute(
1741
    $source,
1742
    param => {'table1.key1' => 1, 'table1.key2' => 1},
1743
    filter => {'table1.key2' => sub { $_[0] * 2 }}
cleanup test
Yuki Kimoto authored on 2011-08-06
1744
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1745
$rows = $result->all;
1746
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
1747

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1748
test 'high perfomance way';
1749
$dbi->execute('drop table table1');
1750
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
1751
$rows = [
1752
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
1753
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
1754
];
1755
{
1756
    my $query;
1757
    foreach my $row (@$rows) {
1758
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
1759
      $dbi->execute($query, $row, filter => {ab => sub { $_[0] * 2 }});
cleanup test
Yuki Kimoto authored on 2011-08-06
1760
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
1761
    is_deeply($dbi->select(table => 'table1')->all,
1762
      [
1763
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
1764
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
1765
      ]
1766
    );
1767
}
1768

            
1769
$dbi->execute('drop table table1');
1770
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
1771
$rows = [
1772
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
1773
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
1774
];
1775
{
1776
    my $query;
1777
    my $sth;
1778
    foreach my $row (@$rows) {
1779
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
1780
      $sth ||= $query->sth;
1781
      $sth->execute(map { $row->{$_} } sort keys %$row);
cleanup test
Yuki Kimoto authored on 2011-08-06
1782
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
1783
    is_deeply($dbi->select(table => 'table1')->all,
1784
      [
1785
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
1786
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
1787
      ]
1788
    );
1789
}
cleanup test
Yuki Kimoto authored on 2011-08-06
1790

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1791
test 'result';
test cleanup
Yuki Kimoto authored on 2011-08-10
1792
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1793
eval { $dbi->execute('drop table table1') };
1794
$dbi->execute($create_table1);
1795
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
1796
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
1797

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1798
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1799
@rows = ();
1800
while (my $row = $result->fetch) {
1801
    push @rows, [@$row];
1802
}
1803
is_deeply(\@rows, [[1, 2], [3, 4]]);
cleanup test
Yuki Kimoto authored on 2011-08-06
1804

            
1805
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1806
@rows = ();
1807
while (my $row = $result->fetch_hash) {
1808
    push @rows, {%$row};
1809
}
1810
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
1811

            
1812
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1813
$row = $result->fetch_first;
1814
is_deeply($row, [1, 2], "row");
1815
$row = $result->fetch;
1816
ok(!$row, "finished");
1817

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1818
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1819
$row = $result->fetch_hash_first;
1820
is_deeply($row, {key1 => 1, key2 => 2}, "row");
1821
$row = $result->fetch_hash;
1822
ok(!$row, "finished");
1823

            
1824
$dbi->execute('create table table2 (key1, key2);');
1825
$result = $dbi->select(table => 'table2');
1826
$row = $result->fetch_hash_first;
1827
ok(!$row, "no row fetch");
cleanup test
Yuki Kimoto authored on 2011-08-06
1828

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1829
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1830
eval { $dbi->execute('drop table table1') };
1831
$dbi->execute($create_table1);
1832
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
1833
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
1834
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1');
1835
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1');
1836
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
1837
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1838
$rows = $result->fetch_multi(2);
1839
is_deeply($rows, [[1, 2],
1840
                  [3, 4]], "fetch_multi first");
1841
$rows = $result->fetch_multi(2);
1842
is_deeply($rows, [[5, 6],
1843
                  [7, 8]], "fetch_multi secound");
1844
$rows = $result->fetch_multi(2);
1845
is_deeply($rows, [[9, 10]], "fetch_multi third");
1846
$rows = $result->fetch_multi(2);
1847
ok(!$rows);
1848

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

            
1853
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1854
$rows = $result->fetch_hash_multi(2);
1855
is_deeply($rows, [{key1 => 1, key2 => 2},
1856
                  {key1 => 3, key2 => 4}], "fetch_multi first");
1857
$rows = $result->fetch_hash_multi(2);
1858
is_deeply($rows, [{key1 => 5, key2 => 6},
1859
                  {key1 => 7, key2 => 8}], "fetch_multi secound");
1860
$rows = $result->fetch_hash_multi(2);
1861
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third");
1862
$rows = $result->fetch_hash_multi(2);
1863
ok(!$rows);
1864

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1869
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1870
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1871
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
1872
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
1873
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
1874

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1875
test 'fetch_all';
1876
$result = $dbi->select(table => 'table1');
1877
$rows = $result->fetch_all;
1878
is_deeply($rows, [[1, 2], [3, 4]]);
cleanup test
Yuki Kimoto authored on 2011-08-06
1879

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1891
$result = $dbi->select(table => 'table1');
1892
$result->dbi->filters({three_times => sub { $_[0] * 3}});
1893
$result->filter({key1 => 'three_times'});
1894
$rows = $result->fetch_hash_all;
1895
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash");
1896

            
1897
test "query_builder";
1898
$datas = [
1899
    # Basic tests
1900
    {   name            => 'placeholder basic',
1901
        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
1902
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;",
1903
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
1904
    },
1905
    {
1906
        name            => 'placeholder in',
1907
        source            => "{in k1 3};",
1908
        sql_expected    => "k1 in (?, ?, ?);",
1909
        columns_expected   => [qw/k1 k1 k1/]
1910
    },
1911
    
1912
    # Table name
1913
    {
1914
        name            => 'placeholder with table name',
1915
        source            => "{= a.k1} {= a.k2}",
1916
        sql_expected    => "a.k1 = ? a.k2 = ?;",
1917
        columns_expected  => [qw/a.k1 a.k2/]
1918
    },
1919
    {   
1920
        name            => 'placeholder in with table name',
1921
        source            => "{in a.k1 2} {in b.k2 2}",
1922
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?);",
1923
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
1924
    },
1925
    {
1926
        name            => 'not contain tag',
1927
        source            => "aaa",
1928
        sql_expected    => "aaa;",
1929
        columns_expected  => [],
1930
    }
1931
];
1932

            
1933
for (my $i = 0; $i < @$datas; $i++) {
1934
    my $data = $datas->[$i];
1935
    my $builder = DBIx::Custom->new->query_builder;
1936
    my $query = $builder->build_query($data->{source});
1937
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
1938
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
1939
}
1940

            
1941
$builder = DBIx::Custom->new->query_builder;
1942
$ret_val = $builder->register_tag(
1943
    p => sub {
1944
        my @args = @_;
1945
        
1946
        my $expand    = "? $args[0] $args[1]";
1947
        my $columns = [2];
1948
        return [$expand, $columns];
1949
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
1950
);
1951

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

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1965
$builder->register_tag({
1966
    q => 'string'
1967
});
cleanup test
Yuki Kimoto authored on 2011-08-06
1968

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1972
$builder->register_tag({
1973
   r => sub {} 
1974
});
cleanup test
Yuki Kimoto authored on 2011-08-06
1975

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

            
1979
$builder->register_tag({
1980
   s => sub { return ["a", ""]} 
1981
});
1982

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

            
1986
$builder->register_tag(
1987
    t => sub {return ["a", []]}
cleanup test
Yuki Kimoto authored on 2011-08-06
1988
);
1989

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

            
1991
test 'General error case';
1992
$builder = DBIx::Custom->new->query_builder;
1993
$builder->register_tag(
1994
    a => sub {
1995
        return ["? ? ?", ['']];
1996
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
1997
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1998
eval{$builder->build_query("{a}")};
1999
like($@, qr/\QPlaceholder count/, "placeholder count is invalid");
cleanup test
Yuki Kimoto authored on 2011-08-06
2000

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

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

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

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

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

            
2017
test 'variouse source';
2018
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;";
2019
$query = $builder->build_query($source);
2020
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1");
2021

            
2022
$source = "abc;";
2023
$query = $builder->build_query($source);
2024
is($query->sql, 'abc;', "basic : 2");
2025

            
2026
$source = "{= a}";
2027
$query = $builder->build_query($source);
2028
is($query->sql, 'a = ?;', "only tag");
2029

            
2030
$source = "000;";
2031
$query = $builder->build_query($source);
2032
is($query->sql, '000;', "contain 0 value");
2033

            
2034
$source = "a {= b} }";
2035
eval{$builder->build_query($source)};
2036
like($@, qr/unexpected "}"/, "error : 1");
2037

            
2038
$source = "a {= {}";
2039
eval{$builder->build_query($source)};
2040
like($@, qr/unexpected "{"/, "error : 2");
2041

            
2042
### SQLite test
2043
test 'type option'; # DEPRECATED!
2044
$dbi = DBIx::Custom->connect(
2045
    data_source => 'dbi:SQLite:dbname=:memory:',
2046
    dbi_option => {
2047
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
2048
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
2049
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2050
$binary = pack("I3", 1, 2, 3);
2051
eval { $dbi->execute('drop table table1') };
2052
$dbi->execute('create table table1(key1, key2)');
2053
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
2054
$result = $dbi->select(table => 'table1');
2055
$row   = $result->one;
2056
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2057
$result = $dbi->execute('select length(key1) as key1_length from table1');
2058
$row = $result->one;
2059
is($row->{key1_length}, length $binary);
cleanup test
Yuki Kimoto authored on 2011-08-06
2060

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2061
test 'type_rule from';
2062
$dbi = DBIx::Custom->connect;
2063
$dbi->type_rule(
2064
    from1 => {
2065
        date => sub { uc $_[0] }
2066
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
2067
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2068
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2069
$dbi->insert({key1 => 'a'}, table => 'table1');
2070
$result = $dbi->select(table => 'table1');
2071
is($result->fetch_first->[0], 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
2072

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

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

            
2077
test 'type_rule into';
test cleanup
Yuki Kimoto authored on 2011-08-10
2078
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
2079
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2080
$dbi->type_rule(
2081
    into1 => {
2082
        date => sub { uc $_[0] }
2083
    }
2084
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2085
$dbi->insert({key1 => 'a'}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
2086
$result = $dbi->select(table => 'table1');
2087
is($result->one->{key1}, 'A');
2088

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2102
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2103
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2104
$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1');
2105
$dbi->type_rule(
2106
    into1 => [
2107
        [qw/date datetime/] => sub { uc $_[0] }
2108
    ]
2109
);
2110
$result = $dbi->execute(
2111
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
2112
    param => {key1 => 'a', 'table1.key2' => 'b'}
2113
);
2114
$row = $result->one;
2115
is($row->{key1}, 'a');
2116
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
2117

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2135
$dbi = DBIx::Custom->connect;
2136
$dbi->execute("create table table1 (key1 date, key2 datetime)");
2137
$dbi->register_filter(twice => sub { $_[0] * 2 });
2138
$dbi->type_rule(
2139
    from1 => {
2140
        date => 'twice',
2141
    },
2142
    into1 => {
2143
        date => 'twice',
2144
    }
2145
);
2146
$dbi->insert({key1 => 2}, table => 'table1');
2147
$result = $dbi->select(table => 'table1');
2148
is($result->fetch->[0], 8);
cleanup test
Yuki Kimoto authored on 2011-08-06
2149

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2150
test 'type_rule and filter order';
test cleanup
Yuki Kimoto authored on 2011-08-10
2151
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2152
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2153
$dbi->type_rule(
2154
    into1 => {
2155
        date => sub { $_[0] . 'b' }
2156
    },
2157
    into2 => {
2158
        date => sub { $_[0] . 'c' }
2159
    },
2160
    from1 => {
2161
        date => sub { $_[0] . 'd' }
2162
    },
2163
    from2 => {
2164
        date => sub { $_[0] . 'e' }
2165
    }
2166
);
2167
$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }});
2168
$result = $dbi->select(table => 'table1');
2169
$result->filter(key1 => sub { $_[0] . 'f' });
2170
is($result->fetch_first->[0], '1abcdef');
cleanup test
Yuki Kimoto authored on 2011-08-06
2171

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2172
$dbi = DBIx::Custom->connect;
2173
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2174
$dbi->type_rule(
2175
    from1 => {
2176
        date => sub { $_[0] . 'p' }
2177
    },
2178
    from2 => {
2179
        date => sub { $_[0] . 'q' }
2180
    },
2181
);
2182
$dbi->insert({key1 => '1'}, table => 'table1');
2183
$result = $dbi->select(table => 'table1');
2184
$result->type_rule(
2185
    from1 => {
2186
        date => sub { $_[0] . 'd' }
2187
    },
2188
    from2 => {
2189
        date => sub { $_[0] . 'e' }
2190
    }
2191
);
2192
$result->filter(key1 => sub { $_[0] . 'f' });
2193
is($result->fetch_first->[0], '1def');
cleanup test
Yuki Kimoto authored on 2011-08-06
2194

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2195
test 'type_rule_off';
2196
$dbi = DBIx::Custom->connect;
2197
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2198
$dbi->type_rule(
2199
    from1 => {
2200
        date => sub { $_[0] * 2 },
2201
    },
2202
    into1 => {
2203
        date => sub { $_[0] * 2 },
2204
    }
2205
);
2206
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
2207
$result = $dbi->select(table => 'table1', type_rule_off => 1);
2208
is($result->type_rule_off->fetch->[0], 2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2209

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2210
$dbi = DBIx::Custom->connect;
2211
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2212
$dbi->type_rule(
2213
    from1 => {
2214
        date => sub { $_[0] * 2 },
2215
    },
2216
    into1 => {
2217
        date => sub { $_[0] * 3 },
2218
    }
2219
);
2220
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
2221
$result = $dbi->select(table => 'table1', type_rule_off => 1);
2222
is($result->one->{key1}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-06
2223

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2252
$dbi = DBIx::Custom->connect;
2253
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2254
$dbi->register_filter(ppp => sub { uc $_[0] });
2255
$dbi->type_rule(
2256
    into1 => {
2257
        date => 'ppp'
cleanup test
Yuki Kimoto authored on 2011-08-06
2258
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
2259
);
2260
$dbi->insert({key1 => 'a'}, table => 'table1');
2261
$result = $dbi->select(table => 'table1');
2262
is($result->one->{key1}, 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
2263

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2264
eval{$dbi->type_rule(
2265
    into1 => {
2266
        date => 'pp'
cleanup test
Yuki Kimoto authored on 2011-08-06
2267
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
2268
)};
2269
like($@, qr/not registered/);
cleanup test
Yuki Kimoto authored on 2011-08-06
2270

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2271
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2272
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2273
eval {
2274
    $dbi->type_rule(
2275
        from1 => {
2276
            Date => sub { $_[0] * 2 },
2277
        }
2278
    );
2279
};
2280
like($@, qr/lower/);
clenup test
Yuki Kimoto authored on 2011-08-06
2281

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2282
eval {
2283
    $dbi->type_rule(
2284
        into1 => {
2285
            Date => sub { $_[0] * 2 },
2286
        }
2287
    );
2288
};
2289
like($@, qr/lower/);
clenup test
Yuki Kimoto authored on 2011-08-06
2290

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2291
$dbi = DBIx::Custom->connect;
2292
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2293
$dbi->type_rule(
2294
    from1 => {
2295
        date => sub { $_[0] * 2 },
2296
    },
2297
    into1 => {
2298
        date => sub { $_[0] * 3 },
2299
    }
2300
);
2301
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
2302
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2303
$result->type_rule_off;
2304
is($result->one->{key1}, 6);
clenup test
Yuki Kimoto authored on 2011-08-06
2305

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2306
$dbi = DBIx::Custom->connect;
2307
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2308
$dbi->type_rule(
2309
    from1 => {
2310
        date => sub { $_[0] * 2 },
2311
        datetime => sub { $_[0] * 4 },
2312
    },
2313
);
2314
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
2315
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2316
$result->type_rule(
2317
    from1 => {
2318
        date => sub { $_[0] * 3 }
2319
    }
2320
);
2321
$row = $result->one;
2322
is($row->{key1}, 6);
2323
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
2324

            
2325
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2326
$result->type_rule(
2327
    from1 => {
2328
        date => sub { $_[0] * 3 }
2329
    }
2330
);
2331
$row = $result->one;
2332
is($row->{key1}, 6);
2333
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
2334

            
2335
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2336
$result->type_rule(
2337
    from1 => {
2338
        date => sub { $_[0] * 3 }
2339
    }
2340
);
2341
$row = $result->one;
2342
is($row->{key1}, 6);
2343
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
2344
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2345
$result->type_rule(
2346
    from1 => [date => sub { $_[0] * 3 }]
2347
);
2348
$row = $result->one;
2349
is($row->{key1}, 6);
2350
is($row->{key2}, 2);
2351
$dbi->register_filter(fivetimes => sub { $_[0] * 5});
clenup test
Yuki Kimoto authored on 2011-08-06
2352
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2353
$result->type_rule(
2354
    from1 => [date => 'fivetimes']
2355
);
2356
$row = $result->one;
2357
is($row->{key1}, 10);
2358
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
2359
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2360
$result->type_rule(
2361
    from1 => [date => undef]
2362
);
2363
$row = $result->one;
2364
is($row->{key1}, 2);
2365
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
2366

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2367
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2368
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2369
$dbi->type_rule(
2370
    from1 => {
2371
        date => sub { $_[0] * 2 },
2372
    },
2373
);
2374
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
2375
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2376
$result->filter(key1 => sub { $_[0] * 3 });
2377
is($result->one->{key1}, 12);
clenup test
Yuki Kimoto authored on 2011-08-06
2378

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2379
$dbi = DBIx::Custom->connect;
2380
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2381
$dbi->type_rule(
2382
    from1 => {
2383
        date => sub { $_[0] * 2 },
2384
    },
2385
);
2386
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
2387
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2388
$result->filter(key1 => sub { $_[0] * 3 });
2389
is($result->fetch->[0], 12);
clenup test
Yuki Kimoto authored on 2011-08-06
2390

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2391
$dbi = DBIx::Custom->connect;
2392
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2393
$dbi->type_rule(
2394
    into1 => {
2395
        date => sub { $_[0] . 'b' }
2396
    },
2397
    into2 => {
2398
        date => sub { $_[0] . 'c' }
2399
    },
2400
    from1 => {
2401
        date => sub { $_[0] . 'd' }
2402
    },
2403
    from2 => {
2404
        date => sub { $_[0] . 'e' }
2405
    }
2406
);
2407
$dbi->insert({key1 => '1'}, table => 'table1', type_rule_off => 1);
clenup test
Yuki Kimoto authored on 2011-08-06
2408
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2409
is($result->type_rule_off->fetch_first->[0], '1');
clenup test
Yuki Kimoto authored on 2011-08-06
2410
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2411
is($result->type_rule_on->fetch_first->[0], '1de');
clenup test
Yuki Kimoto authored on 2011-08-06
2412

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2413
$dbi = DBIx::Custom->connect;
2414
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2415
$dbi->type_rule(
2416
    into1 => {
2417
        date => sub { $_[0] . 'b' }
cleanup test
Yuki Kimoto authored on 2011-08-06
2418
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
2419
    into2 => {
2420
        date => sub { $_[0] . 'c' }
cleanup test
Yuki Kimoto authored on 2011-08-06
2421
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
2422
    from1 => {
2423
        date => sub { $_[0] . 'd' }
cleanup test
Yuki Kimoto authored on 2011-08-06
2424
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
2425
    from2 => {
2426
        date => sub { $_[0] . 'e' }
cleanup test
Yuki Kimoto authored on 2011-08-06
2427
    }
2428
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2429
$dbi->insert({key1 => '1'}, table => 'table1', type_rule1_off => 1);
2430
$result = $dbi->select(table => 'table1');
2431
is($result->type_rule1_off->fetch_first->[0], '1ce');
2432
$result = $dbi->select(table => 'table1');
2433
is($result->type_rule1_on->fetch_first->[0], '1cde');
cleanup test
Yuki Kimoto authored on 2011-08-06
2434

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2435
$dbi = DBIx::Custom->connect;
2436
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2437
$dbi->type_rule(
2438
    into1 => {
2439
        date => sub { $_[0] . 'b' }
2440
    },
2441
    into2 => {
2442
        date => sub { $_[0] . 'c' }
2443
    },
2444
    from1 => {
2445
        date => sub { $_[0] . 'd' }
2446
    },
2447
    from2 => {
2448
        date => sub { $_[0] . 'e' }
cleanup test
Yuki Kimoto authored on 2011-08-06
2449
    }
2450
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2451
$dbi->insert({key1 => '1'}, table => 'table1', type_rule2_off => 1);
2452
$result = $dbi->select(table => 'table1');
2453
is($result->type_rule2_off->fetch_first->[0], '1bd');
2454
$result = $dbi->select(table => 'table1');
2455
is($result->type_rule2_on->fetch_first->[0], '1bde');
test cleanup
Yuki Kimoto authored on 2011-08-10
2456

            
2457
test 'prefix';
2458
$dbi = DBIx::Custom->connect;
2459
eval { $dbi->execute('drop table table1') };
2460
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
2461
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2462
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace');
2463
$result = $dbi->execute('select * from table1;');
2464
$rows   = $result->all;
2465
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
2466

            
2467
$dbi = DBIx::Custom->connect;
2468
eval { $dbi->execute('drop table table1') };
2469
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
2470
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2471
$dbi->update(table => 'table1', param => {key2 => 4},
2472
  where => {key1 => 1}, prefix => 'or replace');
2473
$result = $dbi->execute('select * from table1;');
2474
$rows   = $result->all;
2475
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
2476

            
2477

            
2478
test 'reserved_word_quote';
2479
$dbi = DBIx::Custom->connect;
2480
eval { $dbi->execute("drop table ${q}table$p") };
2481
$dbi->reserved_word_quote('"');
2482
$dbi->execute($create_table_reserved);
2483
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
2484
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
2485
$dbi->insert(table => 'table', param => {select => 1});
2486
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
2487
$result = $dbi->execute("select * from ${q}table$p");
2488
$rows   = $result->all;
2489
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
2490

            
2491
test 'quote';
2492
$dbi = DBIx::Custom->connect;
2493
$dbi->quote('"');
2494
eval { $dbi->execute("drop table ${q}table$p") };
2495
$dbi->execute($create_table_reserved);
2496
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
2497
$dbi->insert(table => 'table', param => {select => 1});
2498
$dbi->delete(table => 'table', where => {select => 1});
2499
$result = $dbi->execute("select * from ${q}table$p");
2500
$rows   = $result->all;
test cleanup
Yuki Kimoto authored on 2011-08-10
2501
is_deeply($rows, [], "reserved word");
2502

            
2503

            
2504

            
2505

            
2506

            
2507

            
2508

            
2509

            
2510

            
2511

            
2512

            
2513

            
2514

            
2515

            
2516

            
2517

            
2518

            
2519

            
2520

            
2521

            
2522

            
2523

            
2524

            
2525

            
2526

            
2527

            
2528

            
2529

            
2530

            
2531

            
2532

            
2533

            
2534
# DEPRECATED! test
2535
test 'filter __ expression';
2536
$dbi = DBIx::Custom->connect;
2537
eval { $dbi->execute('drop table company') };
2538
eval { $dbi->execute('drop table location') };
2539
$dbi->execute('create table company (id, name, location_id)');
2540
$dbi->execute('create table location (id, name)');
2541
$dbi->apply_filter('location',
2542
  name => {in => sub { uc $_[0] } }
2543
);
2544

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

            
2548
$result = $dbi->select(
2549
    table => ['company', 'location'], relation => {'company.location_id' => 'location.id'},
2550
    column => ['location.name as location__name']
2551
);
2552
is($result->fetch_first->[0], 'B');
2553

            
2554
$result = $dbi->select(
2555
    table => 'company', relation => {'company.location_id' => 'location.id'},
2556
    column => ['location.name as location__name']
2557
);
2558
is($result->fetch_first->[0], 'B');
2559

            
2560
$result = $dbi->select(
2561
    table => 'company', relation => {'company.location_id' => 'location.id'},
2562
    column => ['location.name as "location.name"']
2563
);
2564
is($result->fetch_first->[0], 'B');