DBIx-Custom / t / sqlite.t /
Newer Older
2465 lines | 72.688kb
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
test 'Model class';
74
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
75
$dbi = MyDBI1->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
76
eval { $dbi->execute('drop table book') };
cleanup test
Yuki Kimoto authored on 2011-08-06
77
$dbi->execute("create table book (title, author)");
78
$model = $dbi->model('book');
79
$model->insert({title => 'a', author => 'b'});
80
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
81
$dbi->execute("create table company (name)");
82
$model = $dbi->model('company');
83
$model->insert({name => 'a'});
84
is_deeply($model->list->all, [{name => 'a'}], 'basic');
85
is($dbi->models->{'book'}, $dbi->model('book'));
86
is($dbi->models->{'company'}, $dbi->model('company'));
87

            
88
{
89
    package MyDBI4;
90

            
91
    use strict;
92
    use warnings;
93

            
94
    use base 'DBIx::Custom';
95

            
96
    sub connect {
97
        my $self = shift->SUPER::connect(@_);
98
        
99
        $self->include_model(
100
            MyModel2 => [
101
                'book',
102
                {class => 'Company', name => 'company'}
103
            ]
104
        );
105
    }
106

            
107
    package MyModel2::Base1;
108

            
109
    use strict;
110
    use warnings;
111

            
112
    use base 'DBIx::Custom::Model';
113

            
114
    package MyModel2::book;
115

            
116
    use strict;
117
    use warnings;
118

            
119
    use base 'MyModel2::Base1';
120

            
121
    sub insert {
122
        my ($self, $param) = @_;
123
        
124
        return $self->SUPER::insert(param => $param);
125
    }
126

            
127
    sub list { shift->select; }
128

            
129
    package MyModel2::Company;
130

            
131
    use strict;
132
    use warnings;
133

            
134
    use base 'MyModel2::Base1';
135

            
136
    sub insert {
137
        my ($self, $param) = @_;
138
        
139
        return $self->SUPER::insert(param => $param);
140
    }
141

            
142
    sub list { shift->select; }
143
}
test cleanup
Yuki Kimoto authored on 2011-08-10
144
$dbi = MyDBI4->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
145
eval { $dbi->execute('drop table book') };
cleanup test
Yuki Kimoto authored on 2011-08-06
146
$dbi->execute("create table book (title, author)");
147
$model = $dbi->model('book');
148
$model->insert({title => 'a', author => 'b'});
149
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
150
$dbi->execute("create table company (name)");
151
$model = $dbi->model('company');
152
$model->insert({name => 'a'});
153
is_deeply($model->list->all, [{name => 'a'}], 'basic');
154

            
155
{
156
     package MyDBI5;
157

            
158
    use strict;
159
    use warnings;
160

            
161
    use base 'DBIx::Custom';
162

            
163
    sub connect {
164
        my $self = shift->SUPER::connect(@_);
165
        
166
        $self->include_model('MyModel4');
167
    }
168
}
test cleanup
Yuki Kimoto authored on 2011-08-10
169
$dbi = MyDBI5->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
170
eval { $dbi->execute('drop table company') };
171
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
172
$dbi->execute("create table company (name)");
173
$dbi->execute("create table table1 (key1)");
174
$model = $dbi->model('company');
175
$model->insert({name => 'a'});
176
is_deeply($model->list->all, [{name => 'a'}], 'include all model');
177
$dbi->insert(table => 'table1', param => {key1 => 1});
178
$model = $dbi->model('book');
179
is_deeply($model->list->all, [{key1 => 1}], 'include all model');
180

            
181
test 'primary_key';
182
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
183
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
184
$model = $dbi->model('book');
185
$model->primary_key(['id', 'number']);
186
is_deeply($model->primary_key, ['id', 'number']);
187

            
188
test 'columns';
189
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
190
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
191
$model = $dbi->model('book');
192
$model->columns(['id', 'number']);
193
is_deeply($model->columns, ['id', 'number']);
194

            
195
test 'setup_model';
196
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
197
$dbi = MyDBI1->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
198
eval { $dbi->execute('drop table book') };
199
eval { $dbi->execute('drop table company') };
200
eval { $dbi->execute('drop table test') };
201

            
cleanup test
Yuki Kimoto authored on 2011-08-06
202
$dbi->execute('create table book (id)');
203
$dbi->execute('create table company (id, name);');
204
$dbi->execute('create table test (id, name, primary key (id, name));');
205
$dbi->setup_model;
206
is_deeply($dbi->model('book')->columns, ['id']);
207
is_deeply($dbi->model('company')->columns, ['id', 'name']);
208

            
209
test 'delete_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
210
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
211
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
212
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
213
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
214
$dbi->delete_at(
215
    table => 'table1',
216
    primary_key => ['key1', 'key2'],
217
    where => [1, 2],
218
);
219
is_deeply($dbi->select(table => 'table1')->all, []);
220

            
221
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
222
$dbi->delete_at(
223
    table => 'table1',
224
    primary_key => 'key1',
225
    where => 1,
226
);
227
is_deeply($dbi->select(table => 'table1')->all, []);
228

            
229
test 'insert_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
230
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
231
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
232
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
233
$dbi->insert_at(
234
    primary_key => ['key1', 'key2'], 
235
    table => 'table1',
236
    where => [1, 2],
237
    param => {key3 => 3}
238
);
239
is($dbi->select(table => 'table1')->one->{key1}, 1);
240
is($dbi->select(table => 'table1')->one->{key2}, 2);
241
is($dbi->select(table => 'table1')->one->{key3}, 3);
242

            
243
$dbi->delete_all(table => 'table1');
244
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
245
$dbi->insert_at(
246
    primary_key => 'key1', 
247
    table => 'table1',
248
    where => 1,
249
    param => {key2 => 2, key3 => 3}
250
);
251

            
252
is($dbi->select(table => 'table1')->one->{key1}, 1);
253
is($dbi->select(table => 'table1')->one->{key2}, 2);
254
is($dbi->select(table => 'table1')->one->{key3}, 3);
255

            
256
eval {
257
    $dbi->insert_at(
258
        table => 'table1',
259
        primary_key => ['key1', 'key2'],
260
        where => {},
261
        param => {key1 => 1, key2 => 2, key3 => 3},
262
    );
263
};
264
like($@, qr/must be/);
265

            
test cleanup
Yuki Kimoto authored on 2011-08-10
266
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
267
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
268
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
269
$dbi->insert_at(
270
    {key3 => 3},
271
    primary_key => ['key1', 'key2'], 
272
    table => 'table1',
273
    where => [1, 2],
274
);
275
is($dbi->select(table => 'table1')->one->{key1}, 1);
276
is($dbi->select(table => 'table1')->one->{key2}, 2);
277
is($dbi->select(table => 'table1')->one->{key3}, 3);
278

            
279
test 'update_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
280
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
281
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
282
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
283
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
284
$dbi->update_at(
285
    table => 'table1',
286
    primary_key => ['key1', 'key2'],
287
    where => [1, 2],
288
    param => {key3 => 4}
289
);
290
is($dbi->select(table => 'table1')->one->{key1}, 1);
291
is($dbi->select(table => 'table1')->one->{key2}, 2);
292
is($dbi->select(table => 'table1')->one->{key3}, 4);
293

            
294
$dbi->delete_all(table => 'table1');
295
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
296
$dbi->update_at(
297
    table => 'table1',
298
    primary_key => 'key1',
299
    where => 1,
300
    param => {key3 => 4}
301
);
302
is($dbi->select(table => 'table1')->one->{key1}, 1);
303
is($dbi->select(table => 'table1')->one->{key2}, 2);
304
is($dbi->select(table => 'table1')->one->{key3}, 4);
305

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

            
320
test 'select_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
321
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
322
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
323
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
324
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
325
$result = $dbi->select_at(
326
    table => 'table1',
327
    primary_key => ['key1', 'key2'],
328
    where => [1, 2]
329
);
330
$row = $result->one;
331
is($row->{key1}, 1);
332
is($row->{key2}, 2);
333
is($row->{key3}, 3);
334

            
335
$dbi->delete_all(table => 'table1');
336
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
337
$result = $dbi->select_at(
338
    table => 'table1',
339
    primary_key => 'key1',
340
    where => 1,
341
);
342
$row = $result->one;
343
is($row->{key1}, 1);
344
is($row->{key2}, 2);
345
is($row->{key3}, 3);
346

            
347
$dbi->delete_all(table => 'table1');
348
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
349
$result = $dbi->select_at(
350
    table => 'table1',
351
    primary_key => ['key1', 'key2'],
352
    where => [1, 2]
353
);
354
$row = $result->one;
355
is($row->{key1}, 1);
356
is($row->{key2}, 2);
357
is($row->{key3}, 3);
358

            
359
eval {
360
    $result = $dbi->select_at(
361
        table => 'table1',
362
        primary_key => ['key1', 'key2'],
363
        where => {},
364
    );
365
};
366
like($@, qr/must be/);
367

            
368
eval {
369
    $result = $dbi->select_at(
370
        table => 'table1',
371
        primary_key => ['key1', 'key2'],
372
        where => [1],
373
    );
374
};
375
like($@, qr/same/);
376

            
377
eval {
378
    $result = $dbi->update_at(
379
        table => 'table1',
380
        primary_key => ['key1', 'key2'],
381
        where => {},
382
        param => {key1 => 1, key2 => 2},
383
    );
384
};
385
like($@, qr/must be/);
386

            
387
eval {
388
    $result = $dbi->delete_at(
389
        table => 'table1',
390
        primary_key => ['key1', 'key2'],
391
        where => {},
392
    );
393
};
394
like($@, qr/must be/);
395

            
396
test 'columns';
397
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
398
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
399
$model = $dbi->model('book');
400

            
401

            
402
test 'model delete_at';
403
{
404
    package MyDBI6;
405
    
406
    use base 'DBIx::Custom';
407
    
408
    sub connect {
409
        my $self = shift->SUPER::connect(@_);
410
        
411
        $self->include_model('MyModel5');
412
        
413
        return $self;
414
    }
415
}
test cleanup
Yuki Kimoto authored on 2011-08-10
416
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
417
eval { $dbi->execute('drop table table1') };
418
eval { $dbi->execute('drop table table2') };
419
eval { $dbi->execute('drop table table3') };
cleanup test
Yuki Kimoto authored on 2011-08-10
420
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-10
421
$dbi->execute($create_table2_2);
422
$dbi->execute($create_table3);
cleanup test
Yuki Kimoto authored on 2011-08-06
423
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
424
$dbi->model('table1')->delete_at(where => [1, 2]);
425
is_deeply($dbi->select(table => 'table1')->all, []);
426
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
427
$dbi->model('table1_1')->delete_at(where => [1, 2]);
428
is_deeply($dbi->select(table => 'table1')->all, []);
429
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
430
$dbi->model('table1_3')->delete_at(where => [1, 2]);
431
is_deeply($dbi->select(table => 'table1')->all, []);
432

            
433
test 'model insert_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
434
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
435
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
436
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
437
$dbi->model('table1')->insert_at(
438
    where => [1, 2],
439
    param => {key3 => 3}
440
);
441
$result = $dbi->model('table1')->select;
442
$row = $result->one;
443
is($row->{key1}, 1);
444
is($row->{key2}, 2);
445
is($row->{key3}, 3);
446

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

            
462
test 'model select_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
463
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
464
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
465
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
466
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
467
$result = $dbi->model('table1')->select_at(where => [1, 2]);
468
$row = $result->one;
469
is($row->{key1}, 1);
470
is($row->{key2}, 2);
471
is($row->{key3}, 3);
472

            
473

            
474
test 'mycolumn and column';
475
{
476
    package MyDBI7;
477
    
478
    use base 'DBIx::Custom';
479
    
480
    sub connect {
481
        my $self = shift->SUPER::connect(@_);
482
        
483
        $self->include_model('MyModel6');
484
        
485
        
486
        return $self;
487
    }
488
}
test cleanup
Yuki Kimoto authored on 2011-08-10
489
$dbi = MyDBI7->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
490
eval { $dbi->execute('drop table table1') };
491
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
492
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
493
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
494
$dbi->separator('__');
495
$dbi->setup_model;
496
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
497
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
498
$model = $dbi->model('table1');
499
$result = $model->select(
500
    column => [$model->mycolumn, $model->column('table2')],
501
    where => {'table1.key1' => 1}
502
);
503
is_deeply($result->one,
504
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
505

            
506
test 'update_param';
test cleanup
Yuki Kimoto authored on 2011-08-10
507
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
508
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
509
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
510
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
511
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
512

            
513
$param = {key2 => 11};
514
$update_param = $dbi->update_param($param);
515
$sql = <<"EOS";
516
update table1 $update_param
517
where key1 = 1
518
EOS
519
$dbi->execute($sql, param => $param);
test cleanup
Yuki Kimoto authored on 2011-08-06
520
$result = $dbi->execute('select * from table1;', table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
521
$rows   = $result->all;
522
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
523
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
524
                  "basic");
525

            
526

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

            
533
$param = {key2 => 11, key3 => 33};
534
$update_param = $dbi->update_param($param);
535
$sql = <<"EOS";
536
update table1 $update_param
537
where key1 = 1
538
EOS
539
$dbi->execute($sql, param => $param);
test cleanup
Yuki Kimoto authored on 2011-08-06
540
$result = $dbi->execute('select * from table1;', table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
541
$rows   = $result->all;
542
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
543
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
544
                  "basic");
545

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

            
552
$param = {key2 => 11, key3 => 33};
553
$update_param = $dbi->update_param($param, {no_set => 1});
554
$sql = <<"EOS";
555
update table1 set $update_param
556
where key1 = 1
557
EOS
558
$dbi->execute($sql, param => $param);
test cleanup
Yuki Kimoto authored on 2011-08-06
559
$result = $dbi->execute('select * from table1;', table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
560
$rows   = $result->all;
561
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
562
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
563
                  "update param no_set");
564

            
565
            
566
eval { $dbi->update_param({";" => 1}) };
567
like($@, qr/not safety/);
568

            
569

            
570
test 'update_param';
test cleanup
Yuki Kimoto authored on 2011-08-10
571
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
572
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
573
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
574
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
575
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
576

            
577
$param = {key2 => 11};
578
$update_param = $dbi->assign_param($param);
579
$sql = <<"EOS";
580
update table1 set $update_param
581
where key1 = 1
582
EOS
583
$dbi->execute($sql, param => $param, table => 'table1');
test cleanup
Yuki Kimoto authored on 2011-08-06
584
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
585
$rows   = $result->all;
586
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
587
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
588
                  "basic");
589

            
590

            
591
test 'insert_param';
test cleanup
Yuki Kimoto authored on 2011-08-10
592
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
593
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
594
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
595
$param = {key1 => 1, key2 => 2};
596
$insert_param = $dbi->insert_param($param);
597
$sql = <<"EOS";
598
insert into table1 $insert_param
599
EOS
600
$dbi->execute($sql, param => $param, table => 'table1');
601
is($dbi->select(table => 'table1')->one->{key1}, 1);
602
is($dbi->select(table => 'table1')->one->{key2}, 2);
603

            
test cleanup
Yuki Kimoto authored on 2011-08-10
604
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
605
$dbi->quote('"');
cleanup test
Yuki Kimoto authored on 2011-08-10
606
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
607
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
608
$param = {key1 => 1, key2 => 2};
609
$insert_param = $dbi->insert_param($param);
610
$sql = <<"EOS";
611
insert into table1 $insert_param
612
EOS
613
$dbi->execute($sql, param => $param, table => 'table1');
614
is($dbi->select(table => 'table1')->one->{key1}, 1);
615
is($dbi->select(table => 'table1')->one->{key2}, 2);
616

            
617
eval { $dbi->insert_param({";" => 1}) };
618
like($@, qr/not safety/);
619

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

            
621
test 'join';
test cleanup
Yuki Kimoto authored on 2011-08-10
622
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
623
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
624
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
625
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
626
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-10
627
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
628
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
cleanup test
Yuki Kimoto authored on 2011-08-06
629
$dbi->execute('create table table3 (key3 int, key4 int);');
cleanup test
Yuki Kimoto authored on 2011-08-06
630
$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4});
631
$rows = $dbi->select(
632
    table => 'table1',
633
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
634
    where   => {'table1.key2' => 2},
635
    join  => ['left outer join table2 on table1.key1 = table2.key1']
636
)->all;
637
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]);
638

            
639
$rows = $dbi->select(
640
    table => 'table1',
641
    where   => {'key1' => 1},
642
    join  => ['left outer join table2 on table1.key1 = table2.key1']
643
)->all;
644
is_deeply($rows, [{key1 => 1, key2 => 2}]);
645

            
646
eval {
647
    $rows = $dbi->select(
648
        table => 'table1',
649
        column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
650
        where   => {'table1.key2' => 2},
651
        join  => {'table1.key1' => 'table2.key1'}
652
    );
653
};
654
like ($@, qr/array/);
655

            
656
$rows = $dbi->select(
657
    table => 'table1',
658
    where   => {'key1' => 1},
659
    join  => ['left outer join table2 on table1.key1 = table2.key1',
660
              'left outer join table3 on table2.key3 = table3.key3']
661
)->all;
662
is_deeply($rows, [{key1 => 1, key2 => 2}]);
663

            
664
$rows = $dbi->select(
665
    column => 'table3.key4 as table3__key4',
666
    table => 'table1',
667
    where   => {'table1.key1' => 1},
668
    join  => ['left outer join table2 on table1.key1 = table2.key1',
669
              'left outer join table3 on table2.key3 = table3.key3']
670
)->all;
671
is_deeply($rows, [{table3__key4 => 4}]);
672

            
673
$rows = $dbi->select(
674
    column => 'table1.key1 as table1__key1',
675
    table => 'table1',
676
    where   => {'table3.key4' => 4},
677
    join  => ['left outer join table2 on table1.key1 = table2.key1',
678
              'left outer join table3 on table2.key3 = table3.key3']
679
)->all;
680
is_deeply($rows, [{table1__key1 => 1}]);
681

            
test cleanup
Yuki Kimoto authored on 2011-08-10
682
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
683
$dbi->quote('"');
cleanup test
Yuki Kimoto authored on 2011-08-10
684
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
685
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
686
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
687
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
688
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
689
$rows = $dbi->select(
690
    table => 'table1',
691
    column => '"table1"."key1" as "table1_key1", "table2"."key1" as "table2_key1", "key2", "key3"',
692
    where   => {'table1.key2' => 2},
693
    join  => ['left outer join "table2" on "table1"."key1" = "table2"."key1"'],
694
)->all;
695
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}],
696
          'quote');
697

            
698
{
699
    package MyDBI8;
700
    
701
    use base 'DBIx::Custom';
702
    
703
    sub connect {
704
        my $self = shift->SUPER::connect(@_);
705
        
706
        $self->include_model('MyModel7');
707
        
708
        return $self;
709
    }
710
}
cleanup test
Yuki Kimoto authored on 2011-08-06
711

            
test cleanup
Yuki Kimoto authored on 2011-08-10
712
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
713
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
714
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
715
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
716
$sql = <<"EOS";
cleanup test
Yuki Kimoto authored on 2011-08-06
717
left outer join (
718
  select * from table1 as t1
719
  where t1.key2 = (
720
    select max(t2.key2) from table1 as t2
721
    where t1.key1 = t2.key1
722
  )
723
) as latest_table1 on table1.key1 = latest_table1.key1
724
EOS
cleanup test
Yuki Kimoto authored on 2011-08-06
725
$join = [$sql];
726
$rows = $dbi->select(
727
    table => 'table1',
728
    column => 'latest_table1.key1 as latest_table1__key1',
729
    join  => $join
730
)->all;
731
is_deeply($rows, [{latest_table1__key1 => 1}]);
732

            
test cleanup
Yuki Kimoto authored on 2011-08-10
733
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
734
eval { $dbi->execute('drop table table1') };
735
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
736
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
737
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
738
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
739
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
740
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
741
$result = $dbi->select(
742
    table => 'table1',
743
    join => [
744
        "left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1"
745
    ]
746
);
747
is_deeply($result->all, [{key1 => 1, key2 => 2}]);
748
$result = $dbi->select(
749
    table => 'table1',
750
    column => [{table2 => ['key3']}],
751
    join => [
752
        "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1"
753
    ]
754
);
755
is_deeply($result->all, [{'table2.key3' => 4}]);
756
$result = $dbi->select(
757
    table => 'table1',
758
    column => [{table2 => ['key3']}],
759
    join => [
760
        "left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'"
761
    ]
762
);
763
is_deeply($result->all, [{'table2.key3' => 4}]);
764

            
test cleanup
Yuki Kimoto authored on 2011-08-10
765
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
766
eval { $dbi->execute('drop table table1') };
767
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
768
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
769
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
770
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
771
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
772
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
773
$result = $dbi->select(
774
    table => 'table1',
775
    column => [{table2 => ['key3']}],
776
    join => [
777
        {
778
            clause => "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1",
779
            table => ['table1', 'table2']
780
        }
781
    ]
782
);
783
is_deeply($result->all, [{'table2.key3' => 4}]);
784

            
785
test 'mycolumn';
test cleanup
Yuki Kimoto authored on 2011-08-10
786
$dbi = MyDBI8->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
787
eval { $dbi->execute('drop table table1') };
788
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
789
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
790
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
791
$dbi->setup_model;
792
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
793
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
794
$model = $dbi->model('table1');
795
$result = $model->select_at(
796
    column => [
797
        $model->mycolumn,
798
        $model->column('table2')
799
    ]
800
);
801
is_deeply($result->one,
802
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
803

            
804
$result = $model->select_at(
805
    column => [
806
        $model->mycolumn(['key1']),
807
        $model->column(table2 => ['key1'])
808
    ]
809
);
810
is_deeply($result->one,
811
          {key1 => 1, 'table2.key1' => 1});
812
$result = $model->select_at(
813
    column => [
814
        $model->mycolumn(['key1']),
815
        {table2 => ['key1']}
816
    ]
817
);
818
is_deeply($result->one,
819
          {key1 => 1, 'table2.key1' => 1});
820

            
821
$result = $model->select_at(
822
    column => [
823
        $model->mycolumn(['key1']),
824
        ['table2.key1', as => 'table2.key1']
825
    ]
826
);
827
is_deeply($result->one,
828
          {key1 => 1, 'table2.key1' => 1});
829

            
830
$result = $model->select_at(
831
    column => [
832
        $model->mycolumn(['key1']),
833
        ['table2.key1' => 'table2.key1']
834
    ]
835
);
836
is_deeply($result->one,
837
          {key1 => 1, 'table2.key1' => 1});
838

            
839
test 'dbi method from model';
840
{
841
    package MyDBI9;
842
    
843
    use base 'DBIx::Custom';
844
    
845
    sub connect {
846
        my $self = shift->SUPER::connect(@_);
847
        
848
        $self->include_model('MyModel8')->setup_model;
849
        
850
        return $self;
851
    }
852
}
test cleanup
Yuki Kimoto authored on 2011-08-10
853
$dbi = MyDBI9->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
854
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
855
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
856
$model = $dbi->model('table1');
857
eval{$model->execute('select * from table1')};
858
ok(!$@);
859

            
860
test 'column table option';
test cleanup
Yuki Kimoto authored on 2011-08-10
861
$dbi = MyDBI9->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
862
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
863
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
864
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
865
$dbi->setup_model;
866
$dbi->execute('insert into table1 (key1, key2) values (1, 2);');
867
$dbi->execute('insert into table2 (key1, key3) values (1, 4);');
868
$model = $dbi->model('table1');
869
$result = $model->select(
870
    column => [
871
        $model->column('table2', {alias => 'table2_alias'})
872
    ],
873
    where => {'table2_alias.key3' => 4}
874
);
875
is_deeply($result->one, 
876
          {'table2_alias.key1' => 1, 'table2_alias.key3' => 4});
877

            
878
$dbi->separator('__');
879
$result = $model->select(
880
    column => [
881
        $model->column('table2', {alias => 'table2_alias'})
882
    ],
883
    where => {'table2_alias.key3' => 4}
884
);
885
is_deeply($result->one, 
886
          {'table2_alias__key1' => 1, 'table2_alias__key3' => 4});
887

            
888
$dbi->separator('-');
889
$result = $model->select(
890
    column => [
891
        $model->column('table2', {alias => 'table2_alias'})
892
    ],
893
    where => {'table2_alias.key3' => 4}
894
);
895
is_deeply($result->one, 
896
          {'table2_alias-key1' => 1, 'table2_alias-key3' => 4});
897

            
898
test 'type option'; # DEPRECATED!
899
$dbi = DBIx::Custom->connect(
900
    dbi_option => {
901
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
902
    }
903
);
cleanup test
Yuki Kimoto authored on 2011-08-10
904
$binary = pack("I3", 1, 2, 3);
905
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
906
$dbi->execute('create table table1(key1, key2)');
907
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
908
$result = $dbi->select(table => 'table1');
909
$row   = $result->one;
910
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
911
$result = $dbi->execute('select length(key1) as key1_length from table1');
912
$row = $result->one;
913
is($row->{key1_length}, length $binary);
914

            
915
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [['key1'] => DBI::SQL_BLOB]);
916
$result = $dbi->select(table => 'table1');
917
$row   = $result->one;
918
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
919
$result = $dbi->execute('select length(key1) as key1_length from table1');
920
$row = $result->one;
921
is($row->{key1_length}, length $binary);
922

            
923

            
924
test 'bind_type option';
925
$dbi = DBIx::Custom->connect(
926
    dbi_option => {
927
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
928
    }
929
);
930
$binary = pack("I3", 1, 2, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
931
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
932
$dbi->execute('create table table1(key1, key2)');
933
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, bind_type => [key1 => DBI::SQL_BLOB]);
934
$result = $dbi->select(table => 'table1');
935
$row   = $result->one;
936
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
937
$result = $dbi->execute('select length(key1) as key1_length from table1');
938
$row = $result->one;
939
is($row->{key1_length}, length $binary);
940

            
941
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, bind_type => [['key1'] => DBI::SQL_BLOB]);
942
$result = $dbi->select(table => 'table1');
943
$row   = $result->one;
944
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
945
$result = $dbi->execute('select length(key1) as key1_length from table1');
946
$row = $result->one;
947
is($row->{key1_length}, length $binary);
948

            
949
test 'model type attribute';
950
$dbi = DBIx::Custom->connect(
951
    dbi_option => {
952
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
953
    }
954
);
955
$binary = pack("I3", 1, 2, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
956
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
957
$dbi->execute('create table table1(key1, key2)');
958
$model = $dbi->create_model(table => 'table1', bind_type => [key1 => DBI::SQL_BLOB]);
959
$model->insert(param => {key1 => $binary, key2 => 'あ'});
960
$result = $dbi->select(table => 'table1');
961
$row   = $result->one;
962
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
963
$result = $dbi->execute('select length(key1) as key1_length from table1');
964
$row = $result->one;
965
is($row->{key1_length}, length $binary);
966

            
967
test 'create_model';
test cleanup
Yuki Kimoto authored on 2011-08-10
968
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
969
eval { $dbi->execute('drop table table1') };
970
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
971
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
972
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
973

            
974
$dbi->create_model(
975
    table => 'table1',
976
    join => [
977
       'left outer join table2 on table1.key1 = table2.key1'
978
    ],
979
    primary_key => ['key1']
980
);
981
$model2 = $dbi->create_model(
982
    table => 'table2'
983
);
984
$dbi->create_model(
985
    table => 'table3',
986
    filter => [
987
        key1 => {in => sub { uc $_[0] }}
988
    ]
989
);
990
$dbi->setup_model;
991
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
992
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
993
$model = $dbi->model('table1');
994
$result = $model->select(
995
    column => [$model->mycolumn, $model->column('table2')],
996
    where => {'table1.key1' => 1}
997
);
998
is_deeply($result->one,
999
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
1000
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
1001

            
1002
test 'model method';
1003
test 'create_model';
test cleanup
Yuki Kimoto authored on 2011-08-10
1004
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1005
eval { $dbi->execute('drop table table2') };
test cleanup
Yuki Kimoto authored on 2011-08-10
1006
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1007
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1008
$model = $dbi->create_model(
1009
    table => 'table2'
1010
);
1011
$model->method(foo => sub { shift->select(@_) });
1012
is_deeply($model->foo->one, {key1 => 1, key3 => 3});
1013

            
1014
test 'merge_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
1015
$dbi = DBIx::Custom->new;
1016
$params = [
1017
    {key1 => 1, key2 => 2, key3 => 3},
1018
    {key1 => 1, key2 => 2},
1019
    {key1 => 1}
1020
];
1021
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]);
1022
is_deeply($param, {key1 => [1, 1, 1], key2 => [2, 2], key3 => 3});
1023

            
1024
$params = [
1025
    {key1 => [1, 2], key2 => 1, key3 => [1, 2]},
1026
    {key1 => [3, 4], key2 => [2, 3], key3 => 3}
1027
];
1028
$param = $dbi->merge_param($params->[0], $params->[1]);
1029
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
1030

            
1031
test 'select() param option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1032
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1033
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1034
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1035
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1036
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
1037
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1038
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
1039
$dbi->insert(table => 'table2', param => {key1 => 2, key3 => 5});
1040
$rows = $dbi->select(
1041
    table => 'table1',
1042
    column => 'table1.key1 as table1_key1, key2, key3',
1043
    where   => {'table1.key2' => 3},
1044
    join  => ['inner join (select * from table2 where {= table2.key3})' . 
1045
              ' as table2 on table1.key1 = table2.key1'],
1046
    param => {'table2.key3' => 5}
1047
)->all;
1048
is_deeply($rows, [{table1_key1 => 2, key2 => 3, key3 => 5}]);
1049

            
1050

            
1051
test 'select() wrap option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1052
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1053
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1054
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1055
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1056
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
1057
$rows = $dbi->select(
1058
    table => 'table1',
1059
    column => 'key1',
1060
    wrap => ['select * from (', ') as t where key1 = 1']
1061
)->all;
1062
is_deeply($rows, [{key1 => 1}]);
1063

            
1064
eval {
1065
$dbi->select(
1066
    table => 'table1',
1067
    column => 'key1',
1068
    wrap => 'select * from ('
1069
)
1070
};
1071
like($@, qr/array/);
1072

            
1073
test 'select() string where';
test cleanup
Yuki Kimoto authored on 2011-08-10
1074
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1075
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1076
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1077
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1078
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
1079
$rows = $dbi->select(
1080
    table => 'table1',
1081
    where => 'key1 = :key1 and key2 = :key2',
1082
    where_param => {key1 => 1, key2 => 2}
1083
)->all;
1084
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1085

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1086
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1087
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1088
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1089
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1090
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
1091
$rows = $dbi->select(
1092
    table => 'table1',
1093
    where => [
1094
        'key1 = :key1 and key2 = :key2',
1095
        {key1 => 1, key2 => 2}
1096
    ]
1097
)->all;
1098
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1099

            
1100
test 'delete() string where';
test cleanup
Yuki Kimoto authored on 2011-08-10
1101
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1102
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1103
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1104
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1105
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
1106
$dbi->delete(
1107
    table => 'table1',
1108
    where => 'key1 = :key1 and key2 = :key2',
1109
    where_param => {key1 => 1, key2 => 2}
1110
);
1111
$rows = $dbi->select(table => 'table1')->all;
1112
is_deeply($rows, [{key1 => 2, key2 => 3}]);
1113

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1114
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1115
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1116
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1117
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1118
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
1119
$dbi->delete(
1120
    table => 'table1',
1121
    where => [
1122
        'key1 = :key1 and key2 = :key2',
1123
         {key1 => 1, key2 => 2}
1124
    ]
1125
);
1126
$rows = $dbi->select(table => 'table1')->all;
1127
is_deeply($rows, [{key1 => 2, key2 => 3}]);
1128

            
1129

            
1130
test 'update() string where';
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->update(
1136
    table => 'table1',
1137
    param => {key1 => 5},
1138
    where => 'key1 = :key1 and key2 = :key2',
1139
    where_param => {key1 => 1, key2 => 2}
1140
);
1141
$rows = $dbi->select(table => 'table1')->all;
1142
is_deeply($rows, [{key1 => 5, key2 => 2}]);
1143

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1144
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1145
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1146
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1147
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1148
$dbi->update(
1149
    table => 'table1',
1150
    param => {key1 => 5},
1151
    where => [
1152
        'key1 = :key1 and key2 = :key2',
1153
        {key1 => 1, key2 => 2}
1154
    ]
1155
);
1156
$rows = $dbi->select(table => 'table1')->all;
1157
is_deeply($rows, [{key1 => 5, key2 => 2}]);
1158

            
1159
test 'insert id and primary_key option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1160
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1161
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1162
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1163
$dbi->insert(
1164
    primary_key => ['key1', 'key2'], 
1165
    table => 'table1',
1166
    id => [1, 2],
1167
    param => {key3 => 3}
1168
);
1169
is($dbi->select(table => 'table1')->one->{key1}, 1);
1170
is($dbi->select(table => 'table1')->one->{key2}, 2);
1171
is($dbi->select(table => 'table1')->one->{key3}, 3);
1172

            
1173
$dbi->delete_all(table => 'table1');
1174
$dbi->insert(
1175
    primary_key => 'key1', 
1176
    table => 'table1',
1177
    id => 0,
1178
    param => {key2 => 2, key3 => 3}
1179
);
1180

            
1181
is($dbi->select(table => 'table1')->one->{key1}, 0);
1182
is($dbi->select(table => 'table1')->one->{key2}, 2);
1183
is($dbi->select(table => 'table1')->one->{key3}, 3);
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_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1188
$dbi->insert(
1189
    {key3 => 3},
1190
    primary_key => ['key1', 'key2'], 
1191
    table => 'table1',
1192
    id => [1, 2],
1193
);
1194
is($dbi->select(table => 'table1')->one->{key1}, 1);
1195
is($dbi->select(table => 'table1')->one->{key2}, 2);
1196
is($dbi->select(table => 'table1')->one->{key3}, 3);
1197

            
1198

            
1199
test 'model insert id and primary_key option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1200
$dbi = MyDBI6->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_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1203
$dbi->model('table1')->insert(
1204
    id => [1, 2],
1205
    param => {key3 => 3}
1206
);
1207
$result = $dbi->model('table1')->select;
1208
$row = $result->one;
1209
is($row->{key1}, 1);
1210
is($row->{key2}, 2);
1211
is($row->{key3}, 3);
1212

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1213
$dbi = MyDBI6->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_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1216
$dbi->model('table1')->insert(
1217
    {key3 => 3},
1218
    id => [1, 2]
1219
);
1220
$result = $dbi->model('table1')->select;
1221
$row = $result->one;
1222
is($row->{key1}, 1);
1223
is($row->{key2}, 2);
1224
is($row->{key3}, 3);
1225

            
1226
test 'update and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1227
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1228
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1229
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1230
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1231
$dbi->update(
1232
    table => 'table1',
1233
    primary_key => ['key1', 'key2'],
1234
    id => [1, 2],
1235
    param => {key3 => 4}
1236
);
1237
is($dbi->select(table => 'table1')->one->{key1}, 1);
1238
is($dbi->select(table => 'table1')->one->{key2}, 2);
1239
is($dbi->select(table => 'table1')->one->{key3}, 4);
1240

            
1241
$dbi->delete_all(table => 'table1');
1242
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
1243
$dbi->update(
1244
    table => 'table1',
1245
    primary_key => 'key1',
1246
    id => 0,
1247
    param => {key3 => 4}
1248
);
1249
is($dbi->select(table => 'table1')->one->{key1}, 0);
1250
is($dbi->select(table => 'table1')->one->{key2}, 2);
1251
is($dbi->select(table => 'table1')->one->{key3}, 4);
1252

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

            
1267

            
1268
test 'model update and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1269
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1270
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1271
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1272
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1273
$dbi->model('table1')->update(
1274
    id => [1, 2],
1275
    param => {key3 => 4}
1276
);
1277
$result = $dbi->model('table1')->select;
1278
$row = $result->one;
1279
is($row->{key1}, 1);
1280
is($row->{key2}, 2);
1281
is($row->{key3}, 4);
1282

            
1283

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

            
1296
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
1297
$dbi->delete(
1298
    table => 'table1',
1299
    primary_key => 'key1',
1300
    id => 0,
1301
);
1302
is_deeply($dbi->select(table => 'table1')->all, []);
1303

            
1304

            
1305
test 'model delete and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1306
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1307
eval { $dbi->execute('drop table table1') };
1308
eval { $dbi->execute('drop table table2') };
1309
eval { $dbi->execute('drop table table3') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1310
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-10
1311
$dbi->execute($create_table2_2);
1312
$dbi->execute($create_table3);
cleanup test
Yuki Kimoto authored on 2011-08-06
1313
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1314
$dbi->model('table1')->delete(id => [1, 2]);
1315
is_deeply($dbi->select(table => 'table1')->all, []);
1316
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
1317
$dbi->model('table1_1')->delete(id => [1, 2]);
1318
is_deeply($dbi->select(table => 'table1')->all, []);
1319
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
1320
$dbi->model('table1_3')->delete(id => [1, 2]);
1321
is_deeply($dbi->select(table => 'table1')->all, []);
1322

            
1323

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

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

            
1351
$dbi->delete_all(table => 'table1');
1352
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1353
$result = $dbi->select(
1354
    table => 'table1',
1355
    primary_key => ['key1', 'key2'],
1356
    id => [1, 2]
1357
);
1358
$row = $result->one;
1359
is($row->{key1}, 1);
1360
is($row->{key2}, 2);
1361
is($row->{key3}, 3);
1362

            
1363

            
1364
test 'model select_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
1365
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1366
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1367
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1368
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1369
$result = $dbi->model('table1')->select(id => [1, 2]);
1370
$row = $result->one;
1371
is($row->{key1}, 1);
1372
is($row->{key2}, 2);
1373
is($row->{key3}, 3);
1374

            
1375
test 'column separator is default .';
test cleanup
Yuki Kimoto authored on 2011-08-10
1376
$dbi = MyDBI7->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1377
eval { $dbi->execute('drop table table1') };
1378
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1379
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1380
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1381
$dbi->setup_model;
1382
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1383
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1384
$model = $dbi->model('table1');
1385
$result = $model->select(
1386
    column => [$model->column('table2')],
1387
    where => {'table1.key1' => 1}
1388
);
1389
is_deeply($result->one,
1390
          {'table2.key1' => 1, 'table2.key3' => 3});
1391

            
1392
$result = $model->select(
1393
    column => [$model->column('table2' => [qw/key1 key3/])],
1394
    where => {'table1.key1' => 1}
1395
);
1396
is_deeply($result->one,
1397
          {'table2.key1' => 1, 'table2.key3' => 3});
1398

            
1399

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

            
1401
test 'separator';
test cleanup
Yuki Kimoto authored on 2011-08-10
1402
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1403
eval { $dbi->execute('drop table table1') };
1404
eval { $dbi->execute('drop table table2') };
1405
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1406
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-10
1407

            
1408
$dbi->create_model(
1409
    table => 'table1',
1410
    join => [
1411
       'left outer join table2 on table1.key1 = table2.key1'
1412
    ],
1413
    primary_key => ['key1'],
cleanup test
Yuki Kimoto authored on 2011-08-06
1414
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1415
$model2 = $dbi->create_model(
1416
    table => 'table2',
1417
);
1418
$dbi->setup_model;
1419
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1420
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1421
$model = $dbi->model('table1');
1422
$result = $model->select(
1423
    column => [
1424
        $model->mycolumn,
1425
        {table2 => [qw/key1 key3/]}
1426
    ],
1427
    where => {'table1.key1' => 1}
1428
);
1429
is_deeply($result->one,
1430
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
1431
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
1432

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1433
$dbi->separator('__');
1434
$model = $dbi->model('table1');
1435
$result = $model->select(
1436
    column => [
1437
        $model->mycolumn,
1438
        {table2 => [qw/key1 key3/]}
1439
    ],
1440
    where => {'table1.key1' => 1}
1441
);
1442
is_deeply($result->one,
1443
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
1444
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
1445

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1446
$dbi->separator('-');
1447
$model = $dbi->model('table1');
1448
$result = $model->select(
1449
    column => [
1450
        $model->mycolumn,
1451
        {table2 => [qw/key1 key3/]}
1452
    ],
1453
    where => {'table1.key1' => 1}
1454
);
1455
is_deeply($result->one,
1456
          {key1 => 1, key2 => 2, 'table2-key1' => 1, 'table2-key3' => 3});
1457
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
1458

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

            
1460
test 'filter_off';
test cleanup
Yuki Kimoto authored on 2011-08-10
1461
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1462
eval { $dbi->execute('drop table table1') };
1463
eval { $dbi->execute('drop table table2') };
1464
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1465
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-10
1466

            
1467
$dbi->create_model(
1468
    table => 'table1',
1469
    join => [
1470
       'left outer join table2 on table1.key1 = table2.key1'
1471
    ],
1472
    primary_key => ['key1'],
cleanup test
Yuki Kimoto authored on 2011-08-06
1473
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1474
$dbi->setup_model;
1475
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1476
$model = $dbi->model('table1');
1477
$result = $model->select(column => 'key1');
1478
$result->filter(key1 => sub { $_[0] * 2 });
1479
is_deeply($result->one, {key1 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-06
1480

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1481
test 'available_datetype';
test cleanup
Yuki Kimoto authored on 2011-08-10
1482
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1483
ok($dbi->can('available_datatype'));
1484

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1486
test 'select prefix option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1487
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1488
eval { $dbi->execute('drop table table1') };
1489
$dbi->execute($create_table1);
1490
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1491
$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all;
1492
is_deeply($rows, [{key1 => 1, key2 => 2}], "table");
1493

            
1494

            
1495
test 'separator';
1496
$dbi = DBIx::Custom->connect;
1497
is($dbi->separator, '.');
1498
$dbi->separator('-');
1499
is($dbi->separator, '-');
1500
$dbi->separator('__');
1501
is($dbi->separator, '__');
1502
eval { $dbi->separator('?') };
1503
like($@, qr/Separator/);
1504

            
1505

            
1506
test 'map_param';
1507
$dbi = DBIx::Custom->connect;
1508
$param = $dbi->map_param(
1509
    {id => 1, author => 'Ken', price => 1900},
1510
    id => 'book.id',
1511
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1512
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
cleanup test
Yuki Kimoto authored on 2011-08-06
1513
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1514
is_deeply($param, {'book.id' => 1, 'book.author' => '%Ken%',
1515
  'book.price' => 1900});
1516

            
1517
$param = $dbi->map_param(
1518
    {id => 0, author => 0, price => 0},
1519
    id => 'book.id',
1520
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1521
    price => ['book.price', sub { '%' . $_[0] . '%' },
1522
      {if => sub { $_[0] eq 0 }}]
cleanup test
Yuki Kimoto authored on 2011-08-06
1523
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1524
is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
cleanup test
Yuki Kimoto authored on 2011-08-06
1525

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1526
$param = $dbi->map_param(
1527
    {id => '', author => '', price => ''},
1528
    id => 'book.id',
1529
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1530
    price => ['book.price', sub { '%' . $_[0] . '%' },
1531
      {if => sub { $_[0] eq 1 }}]
cleanup test
Yuki Kimoto authored on 2011-08-06
1532
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1533
is_deeply($param, {});
1534

            
1535
$param = $dbi->map_param(
1536
    {id => undef, author => undef, price => undef},
1537
    id => 'book.id',
1538
    price => ['book.price', {if => 'exists'}]
cleanup test
Yuki Kimoto authored on 2011-08-06
1539
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1540
is_deeply($param, {'book.price' => undef});
1541

            
1542
$param = $dbi->map_param(
1543
    {price => 'a'},
1544
    id => ['book.id', {if => 'exists'}],
1545
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
1546
);
1547
is_deeply($param, {'book.price' => '%a'});
cleanup test
Yuki Kimoto authored on 2011-08-06
1548

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

            
1550
test 'table_alias';
test cleanup
Yuki Kimoto authored on 2011-08-10
1551
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1552
eval { $dbi->execute('drop table table1') };
1553
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
cleanup test
Yuki Kimoto authored on 2011-08-06
1554
$dbi->type_rule(
1555
    into1 => {
cleanup test
Yuki Kimoto authored on 2011-08-10
1556
        date => sub { uc $_[0] }
cleanup test
Yuki Kimoto authored on 2011-08-06
1557
    }
1558
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1559
$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'},
1560
  table_alias => {table2 => 'table1'});
cleanup test
Yuki Kimoto authored on 2011-08-06
1561
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1562
is($result->one->{key1}, 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
1563

            
1564

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1565
test 'order';
test cleanup
Yuki Kimoto authored on 2011-08-10
1566
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1567
eval { $dbi->execute('drop table table1') };
1568
$dbi->execute("create table table1 (key1, key2)");
1569
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
1570
$dbi->insert({key1 => 1, key2 => 3}, table => 'table1');
1571
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
1572
$dbi->insert({key1 => 2, key2 => 4}, table => 'table1');
1573
my $order = $dbi->order;
1574
$order->prepend('key1', 'key2 desc');
1575
$result = $dbi->select(table => 'table1', append => "$order");
1576
is_deeply($result->all, [{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1},
1577
  {key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}]);
1578
$order->prepend('key1 desc');
1579
$result = $dbi->select(table => 'table1', append => "$order");
1580
is_deeply($result->all, [{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2},
1581
  {key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
1582

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1583
$order = $dbi->order;
1584
$order->prepend(['table1-key1'], [qw/table1-key2 desc/]);
1585
$result = $dbi->select(table => 'table1',
1586
  column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']],
1587
  append => "$order");
1588
is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3},
1589
  {'table1-key1' => 1, 'table1-key2' => 1},
1590
  {'table1-key1' => 2, 'table1-key2' => 4},
1591
  {'table1-key1' => 2, 'table1-key2' => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
1592

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1593
test 'tag_parse';
test cleanup
Yuki Kimoto authored on 2011-08-10
1594
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1595
$dbi->tag_parse(0);
1596
eval { $dbi->execute('drop table table1') };
1597
$dbi->execute("create table table1 (key1, key2)");
1598
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
1599
eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})};
1600
ok($@);
cleanup test
Yuki Kimoto authored on 2011-08-06
1601

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1602
test 'last_sql';
test cleanup
Yuki Kimoto authored on 2011-08-10
1603
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1604
eval { $dbi->execute('drop table table1') };
1605
$dbi->execute("create table table1 (key1, key2)");
1606
$dbi->execute('select * from table1');
1607
is($dbi->last_sql, 'select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
1608

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1612
test 'DBIx::Custom header';
test cleanup
Yuki Kimoto authored on 2011-08-10
1613
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1614
eval { $dbi->execute('drop table table1') };
1615
$dbi->execute("create table table1 (key1, key2)");
1616
$result = $dbi->execute('select key1 as h1, key2 as h2 from table1');
1617
is_deeply($result->header, [qw/h1 h2/]);
cleanup test
Yuki Kimoto authored on 2011-08-06
1618

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

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1640
$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}";
1641
$result = $dbi->execute(
1642
    $source,
1643
    param => {'table1.key1' => 1, 'table1.key2' => 1},
1644
    filter => {'table1.key2' => sub { $_[0] * 2 }}
cleanup test
Yuki Kimoto authored on 2011-08-06
1645
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1646
$rows = $result->all;
1647
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
1648

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1649
test 'high perfomance way';
1650
$dbi->execute('drop table table1');
1651
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
1652
$rows = [
1653
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
1654
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
1655
];
1656
{
1657
    my $query;
1658
    foreach my $row (@$rows) {
1659
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
1660
      $dbi->execute($query, $row, filter => {ab => sub { $_[0] * 2 }});
cleanup test
Yuki Kimoto authored on 2011-08-06
1661
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
1662
    is_deeply($dbi->select(table => 'table1')->all,
1663
      [
1664
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
1665
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
1666
      ]
1667
    );
1668
}
1669

            
1670
$dbi->execute('drop table table1');
1671
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
1672
$rows = [
1673
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
1674
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
1675
];
1676
{
1677
    my $query;
1678
    my $sth;
1679
    foreach my $row (@$rows) {
1680
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
1681
      $sth ||= $query->sth;
1682
      $sth->execute(map { $row->{$_} } sort keys %$row);
cleanup test
Yuki Kimoto authored on 2011-08-06
1683
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
1684
    is_deeply($dbi->select(table => 'table1')->all,
1685
      [
1686
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
1687
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
1688
      ]
1689
    );
1690
}
cleanup test
Yuki Kimoto authored on 2011-08-06
1691

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1692
test 'result';
test cleanup
Yuki Kimoto authored on 2011-08-10
1693
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1694
eval { $dbi->execute('drop table table1') };
1695
$dbi->execute($create_table1);
1696
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
1697
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
1698

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1699
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1700
@rows = ();
1701
while (my $row = $result->fetch) {
1702
    push @rows, [@$row];
1703
}
1704
is_deeply(\@rows, [[1, 2], [3, 4]]);
cleanup test
Yuki Kimoto authored on 2011-08-06
1705

            
1706
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1707
@rows = ();
1708
while (my $row = $result->fetch_hash) {
1709
    push @rows, {%$row};
1710
}
1711
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
1712

            
1713
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1714
$row = $result->fetch_first;
1715
is_deeply($row, [1, 2], "row");
1716
$row = $result->fetch;
1717
ok(!$row, "finished");
1718

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1719
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1720
$row = $result->fetch_hash_first;
1721
is_deeply($row, {key1 => 1, key2 => 2}, "row");
1722
$row = $result->fetch_hash;
1723
ok(!$row, "finished");
1724

            
1725
$dbi->execute('create table table2 (key1, key2);');
1726
$result = $dbi->select(table => 'table2');
1727
$row = $result->fetch_hash_first;
1728
ok(!$row, "no row fetch");
cleanup test
Yuki Kimoto authored on 2011-08-06
1729

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1730
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1731
eval { $dbi->execute('drop table table1') };
1732
$dbi->execute($create_table1);
1733
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
1734
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
1735
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1');
1736
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1');
1737
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
1738
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1739
$rows = $result->fetch_multi(2);
1740
is_deeply($rows, [[1, 2],
1741
                  [3, 4]], "fetch_multi first");
1742
$rows = $result->fetch_multi(2);
1743
is_deeply($rows, [[5, 6],
1744
                  [7, 8]], "fetch_multi secound");
1745
$rows = $result->fetch_multi(2);
1746
is_deeply($rows, [[9, 10]], "fetch_multi third");
1747
$rows = $result->fetch_multi(2);
1748
ok(!$rows);
1749

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

            
1754
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1755
$rows = $result->fetch_hash_multi(2);
1756
is_deeply($rows, [{key1 => 1, key2 => 2},
1757
                  {key1 => 3, key2 => 4}], "fetch_multi first");
1758
$rows = $result->fetch_hash_multi(2);
1759
is_deeply($rows, [{key1 => 5, key2 => 6},
1760
                  {key1 => 7, key2 => 8}], "fetch_multi secound");
1761
$rows = $result->fetch_hash_multi(2);
1762
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third");
1763
$rows = $result->fetch_hash_multi(2);
1764
ok(!$rows);
1765

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1770
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1771
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1772
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
1773
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
1774
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
1775

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1776
test 'fetch_all';
1777
$result = $dbi->select(table => 'table1');
1778
$rows = $result->fetch_all;
1779
is_deeply($rows, [[1, 2], [3, 4]]);
cleanup test
Yuki Kimoto authored on 2011-08-06
1780

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1792
$result = $dbi->select(table => 'table1');
1793
$result->dbi->filters({three_times => sub { $_[0] * 3}});
1794
$result->filter({key1 => 'three_times'});
1795
$rows = $result->fetch_hash_all;
1796
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash");
1797

            
1798
test "query_builder";
1799
$datas = [
1800
    # Basic tests
1801
    {   name            => 'placeholder basic',
1802
        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
1803
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;",
1804
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
1805
    },
1806
    {
1807
        name            => 'placeholder in',
1808
        source            => "{in k1 3};",
1809
        sql_expected    => "k1 in (?, ?, ?);",
1810
        columns_expected   => [qw/k1 k1 k1/]
1811
    },
1812
    
1813
    # Table name
1814
    {
1815
        name            => 'placeholder with table name',
1816
        source            => "{= a.k1} {= a.k2}",
1817
        sql_expected    => "a.k1 = ? a.k2 = ?;",
1818
        columns_expected  => [qw/a.k1 a.k2/]
1819
    },
1820
    {   
1821
        name            => 'placeholder in with table name',
1822
        source            => "{in a.k1 2} {in b.k2 2}",
1823
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?);",
1824
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
1825
    },
1826
    {
1827
        name            => 'not contain tag',
1828
        source            => "aaa",
1829
        sql_expected    => "aaa;",
1830
        columns_expected  => [],
1831
    }
1832
];
1833

            
1834
for (my $i = 0; $i < @$datas; $i++) {
1835
    my $data = $datas->[$i];
1836
    my $builder = DBIx::Custom->new->query_builder;
1837
    my $query = $builder->build_query($data->{source});
1838
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
1839
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
1840
}
1841

            
1842
$builder = DBIx::Custom->new->query_builder;
1843
$ret_val = $builder->register_tag(
1844
    p => sub {
1845
        my @args = @_;
1846
        
1847
        my $expand    = "? $args[0] $args[1]";
1848
        my $columns = [2];
1849
        return [$expand, $columns];
1850
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
1851
);
1852

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

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1866
$builder->register_tag({
1867
    q => 'string'
1868
});
cleanup test
Yuki Kimoto authored on 2011-08-06
1869

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1873
$builder->register_tag({
1874
   r => sub {} 
1875
});
cleanup test
Yuki Kimoto authored on 2011-08-06
1876

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

            
1880
$builder->register_tag({
1881
   s => sub { return ["a", ""]} 
1882
});
1883

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

            
1887
$builder->register_tag(
1888
    t => sub {return ["a", []]}
cleanup test
Yuki Kimoto authored on 2011-08-06
1889
);
1890

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

            
1892
test 'General error case';
1893
$builder = DBIx::Custom->new->query_builder;
1894
$builder->register_tag(
1895
    a => sub {
1896
        return ["? ? ?", ['']];
1897
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
1898
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1899
eval{$builder->build_query("{a}")};
1900
like($@, qr/\QPlaceholder count/, "placeholder count is invalid");
cleanup test
Yuki Kimoto authored on 2011-08-06
1901

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

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

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

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

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

            
1918
test 'variouse source';
1919
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;";
1920
$query = $builder->build_query($source);
1921
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1");
1922

            
1923
$source = "abc;";
1924
$query = $builder->build_query($source);
1925
is($query->sql, 'abc;', "basic : 2");
1926

            
1927
$source = "{= a}";
1928
$query = $builder->build_query($source);
1929
is($query->sql, 'a = ?;', "only tag");
1930

            
1931
$source = "000;";
1932
$query = $builder->build_query($source);
1933
is($query->sql, '000;', "contain 0 value");
1934

            
1935
$source = "a {= b} }";
1936
eval{$builder->build_query($source)};
1937
like($@, qr/unexpected "}"/, "error : 1");
1938

            
1939
$source = "a {= {}";
1940
eval{$builder->build_query($source)};
1941
like($@, qr/unexpected "{"/, "error : 2");
1942

            
1943
### SQLite test
1944
test 'type option'; # DEPRECATED!
1945
$dbi = DBIx::Custom->connect(
1946
    data_source => 'dbi:SQLite:dbname=:memory:',
1947
    dbi_option => {
1948
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
1949
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
1950
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1951
$binary = pack("I3", 1, 2, 3);
1952
eval { $dbi->execute('drop table table1') };
1953
$dbi->execute('create table table1(key1, key2)');
1954
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
1955
$result = $dbi->select(table => 'table1');
1956
$row   = $result->one;
1957
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
1958
$result = $dbi->execute('select length(key1) as key1_length from table1');
1959
$row = $result->one;
1960
is($row->{key1_length}, length $binary);
cleanup test
Yuki Kimoto authored on 2011-08-06
1961

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1962
test 'type_rule from';
1963
$dbi = DBIx::Custom->connect;
1964
$dbi->type_rule(
1965
    from1 => {
1966
        date => sub { uc $_[0] }
1967
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
1968
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1969
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1970
$dbi->insert({key1 => 'a'}, table => 'table1');
1971
$result = $dbi->select(table => 'table1');
1972
is($result->fetch_first->[0], 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
1973

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

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

            
1978
test 'type_rule into';
test cleanup
Yuki Kimoto authored on 2011-08-10
1979
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1980
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1981
$dbi->type_rule(
1982
    into1 => {
1983
        date => sub { uc $_[0] }
1984
    }
1985
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1986
$dbi->insert({key1 => 'a'}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
1987
$result = $dbi->select(table => 'table1');
1988
is($result->one->{key1}, 'A');
1989

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1990
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1991
$dbi->execute("create table table1 (key1 date, key2 datetime)");
1992
$dbi->type_rule(
1993
    into1 => [
1994
         [qw/date datetime/] => sub { uc $_[0] }
1995
    ]
1996
);
1997
$dbi->insert({key1 => 'a', key2 => 'b'}, table => 'table1');
1998
$result = $dbi->select(table => 'table1');
1999
$row = $result->one;
2000
is($row->{key1}, 'A');
2001
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
2002

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2003
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2004
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2005
$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1');
2006
$dbi->type_rule(
2007
    into1 => [
2008
        [qw/date datetime/] => sub { uc $_[0] }
2009
    ]
2010
);
2011
$result = $dbi->execute(
2012
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
2013
    param => {key1 => 'a', 'table1.key2' => 'b'}
2014
);
2015
$row = $result->one;
2016
is($row->{key1}, 'a');
2017
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
2018

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2019
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2020
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2021
$dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1');
2022
$dbi->type_rule(
2023
    into1 => [
2024
        [qw/date datetime/] => sub { uc $_[0] }
2025
    ]
2026
);
2027
$result = $dbi->execute(
2028
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
2029
    param => {key1 => 'a', 'table1.key2' => 'b'},
2030
    table => 'table1'
2031
);
2032
$row = $result->one;
2033
is($row->{key1}, 'A');
2034
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
2035

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2036
$dbi = DBIx::Custom->connect;
2037
$dbi->execute("create table table1 (key1 date, key2 datetime)");
2038
$dbi->register_filter(twice => sub { $_[0] * 2 });
2039
$dbi->type_rule(
2040
    from1 => {
2041
        date => 'twice',
2042
    },
2043
    into1 => {
2044
        date => 'twice',
2045
    }
2046
);
2047
$dbi->insert({key1 => 2}, table => 'table1');
2048
$result = $dbi->select(table => 'table1');
2049
is($result->fetch->[0], 8);
cleanup test
Yuki Kimoto authored on 2011-08-06
2050

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2051
test 'type_rule and filter order';
test cleanup
Yuki Kimoto authored on 2011-08-10
2052
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2053
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2054
$dbi->type_rule(
2055
    into1 => {
2056
        date => sub { $_[0] . 'b' }
2057
    },
2058
    into2 => {
2059
        date => sub { $_[0] . 'c' }
2060
    },
2061
    from1 => {
2062
        date => sub { $_[0] . 'd' }
2063
    },
2064
    from2 => {
2065
        date => sub { $_[0] . 'e' }
2066
    }
2067
);
2068
$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }});
2069
$result = $dbi->select(table => 'table1');
2070
$result->filter(key1 => sub { $_[0] . 'f' });
2071
is($result->fetch_first->[0], '1abcdef');
cleanup test
Yuki Kimoto authored on 2011-08-06
2072

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2073
$dbi = DBIx::Custom->connect;
2074
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2075
$dbi->type_rule(
2076
    from1 => {
2077
        date => sub { $_[0] . 'p' }
2078
    },
2079
    from2 => {
2080
        date => sub { $_[0] . 'q' }
2081
    },
2082
);
2083
$dbi->insert({key1 => '1'}, table => 'table1');
2084
$result = $dbi->select(table => 'table1');
2085
$result->type_rule(
2086
    from1 => {
2087
        date => sub { $_[0] . 'd' }
2088
    },
2089
    from2 => {
2090
        date => sub { $_[0] . 'e' }
2091
    }
2092
);
2093
$result->filter(key1 => sub { $_[0] . 'f' });
2094
is($result->fetch_first->[0], '1def');
cleanup test
Yuki Kimoto authored on 2011-08-06
2095

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2096
test 'type_rule_off';
2097
$dbi = DBIx::Custom->connect;
2098
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2099
$dbi->type_rule(
2100
    from1 => {
2101
        date => sub { $_[0] * 2 },
2102
    },
2103
    into1 => {
2104
        date => sub { $_[0] * 2 },
2105
    }
2106
);
2107
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
2108
$result = $dbi->select(table => 'table1', type_rule_off => 1);
2109
is($result->type_rule_off->fetch->[0], 2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2110

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2111
$dbi = DBIx::Custom->connect;
2112
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2113
$dbi->type_rule(
2114
    from1 => {
2115
        date => sub { $_[0] * 2 },
2116
    },
2117
    into1 => {
2118
        date => sub { $_[0] * 3 },
2119
    }
2120
);
2121
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
2122
$result = $dbi->select(table => 'table1', type_rule_off => 1);
2123
is($result->one->{key1}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-06
2124

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2125
$dbi = DBIx::Custom->connect;
2126
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2127
$dbi->type_rule(
2128
    from1 => {
2129
        date => sub { $_[0] * 2 },
2130
    },
2131
    into1 => {
2132
        date => sub { $_[0] * 3 },
2133
    }
2134
);
2135
$dbi->insert({key1 => 2}, table => 'table1');
2136
$result = $dbi->select(table => 'table1');
2137
is($result->one->{key1}, 12);
cleanup test
Yuki Kimoto authored on 2011-08-06
2138

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2153
$dbi = DBIx::Custom->connect;
2154
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2155
$dbi->register_filter(ppp => sub { uc $_[0] });
2156
$dbi->type_rule(
2157
    into1 => {
2158
        date => 'ppp'
cleanup test
Yuki Kimoto authored on 2011-08-06
2159
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
2160
);
2161
$dbi->insert({key1 => 'a'}, table => 'table1');
2162
$result = $dbi->select(table => 'table1');
2163
is($result->one->{key1}, 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
2164

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2165
eval{$dbi->type_rule(
2166
    into1 => {
2167
        date => 'pp'
cleanup test
Yuki Kimoto authored on 2011-08-06
2168
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
2169
)};
2170
like($@, qr/not registered/);
cleanup test
Yuki Kimoto authored on 2011-08-06
2171

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2172
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2173
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2174
eval {
2175
    $dbi->type_rule(
2176
        from1 => {
2177
            Date => sub { $_[0] * 2 },
2178
        }
2179
    );
2180
};
2181
like($@, qr/lower/);
clenup test
Yuki Kimoto authored on 2011-08-06
2182

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2183
eval {
2184
    $dbi->type_rule(
2185
        into1 => {
2186
            Date => sub { $_[0] * 2 },
2187
        }
2188
    );
2189
};
2190
like($@, qr/lower/);
clenup test
Yuki Kimoto authored on 2011-08-06
2191

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

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

            
2226
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2227
$result->type_rule(
2228
    from1 => {
2229
        date => sub { $_[0] * 3 }
2230
    }
2231
);
2232
$row = $result->one;
2233
is($row->{key1}, 6);
2234
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
2235

            
2236
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2237
$result->type_rule(
2238
    from1 => {
2239
        date => sub { $_[0] * 3 }
2240
    }
2241
);
2242
$row = $result->one;
2243
is($row->{key1}, 6);
2244
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
2245
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2246
$result->type_rule(
2247
    from1 => [date => sub { $_[0] * 3 }]
2248
);
2249
$row = $result->one;
2250
is($row->{key1}, 6);
2251
is($row->{key2}, 2);
2252
$dbi->register_filter(fivetimes => sub { $_[0] * 5});
clenup test
Yuki Kimoto authored on 2011-08-06
2253
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2254
$result->type_rule(
2255
    from1 => [date => 'fivetimes']
2256
);
2257
$row = $result->one;
2258
is($row->{key1}, 10);
2259
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
2260
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2261
$result->type_rule(
2262
    from1 => [date => undef]
2263
);
2264
$row = $result->one;
2265
is($row->{key1}, 2);
2266
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
2267

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2280
$dbi = DBIx::Custom->connect;
2281
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2282
$dbi->type_rule(
2283
    from1 => {
2284
        date => sub { $_[0] * 2 },
2285
    },
2286
);
2287
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
2288
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2289
$result->filter(key1 => sub { $_[0] * 3 });
2290
is($result->fetch->[0], 12);
clenup test
Yuki Kimoto authored on 2011-08-06
2291

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2292
$dbi = DBIx::Custom->connect;
2293
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2294
$dbi->type_rule(
2295
    into1 => {
2296
        date => sub { $_[0] . 'b' }
2297
    },
2298
    into2 => {
2299
        date => sub { $_[0] . 'c' }
2300
    },
2301
    from1 => {
2302
        date => sub { $_[0] . 'd' }
2303
    },
2304
    from2 => {
2305
        date => sub { $_[0] . 'e' }
2306
    }
2307
);
2308
$dbi->insert({key1 => '1'}, table => 'table1', type_rule_off => 1);
clenup test
Yuki Kimoto authored on 2011-08-06
2309
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2310
is($result->type_rule_off->fetch_first->[0], '1');
clenup test
Yuki Kimoto authored on 2011-08-06
2311
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2312
is($result->type_rule_on->fetch_first->[0], '1de');
clenup test
Yuki Kimoto authored on 2011-08-06
2313

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2314
$dbi = DBIx::Custom->connect;
2315
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2316
$dbi->type_rule(
2317
    into1 => {
2318
        date => sub { $_[0] . 'b' }
cleanup test
Yuki Kimoto authored on 2011-08-06
2319
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
2320
    into2 => {
2321
        date => sub { $_[0] . 'c' }
cleanup test
Yuki Kimoto authored on 2011-08-06
2322
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
2323
    from1 => {
2324
        date => sub { $_[0] . 'd' }
cleanup test
Yuki Kimoto authored on 2011-08-06
2325
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
2326
    from2 => {
2327
        date => sub { $_[0] . 'e' }
cleanup test
Yuki Kimoto authored on 2011-08-06
2328
    }
2329
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2330
$dbi->insert({key1 => '1'}, table => 'table1', type_rule1_off => 1);
2331
$result = $dbi->select(table => 'table1');
2332
is($result->type_rule1_off->fetch_first->[0], '1ce');
2333
$result = $dbi->select(table => 'table1');
2334
is($result->type_rule1_on->fetch_first->[0], '1cde');
cleanup test
Yuki Kimoto authored on 2011-08-06
2335

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2336
$dbi = DBIx::Custom->connect;
2337
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2338
$dbi->type_rule(
2339
    into1 => {
2340
        date => sub { $_[0] . 'b' }
2341
    },
2342
    into2 => {
2343
        date => sub { $_[0] . 'c' }
2344
    },
2345
    from1 => {
2346
        date => sub { $_[0] . 'd' }
2347
    },
2348
    from2 => {
2349
        date => sub { $_[0] . 'e' }
cleanup test
Yuki Kimoto authored on 2011-08-06
2350
    }
2351
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2352
$dbi->insert({key1 => '1'}, table => 'table1', type_rule2_off => 1);
2353
$result = $dbi->select(table => 'table1');
2354
is($result->type_rule2_off->fetch_first->[0], '1bd');
2355
$result = $dbi->select(table => 'table1');
2356
is($result->type_rule2_on->fetch_first->[0], '1bde');
test cleanup
Yuki Kimoto authored on 2011-08-10
2357

            
2358
test 'prefix';
2359
$dbi = DBIx::Custom->connect;
2360
eval { $dbi->execute('drop table table1') };
2361
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
2362
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2363
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace');
2364
$result = $dbi->execute('select * from table1;');
2365
$rows   = $result->all;
2366
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
2367

            
2368
$dbi = DBIx::Custom->connect;
2369
eval { $dbi->execute('drop table table1') };
2370
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
2371
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2372
$dbi->update(table => 'table1', param => {key2 => 4},
2373
  where => {key1 => 1}, prefix => 'or replace');
2374
$result = $dbi->execute('select * from table1;');
2375
$rows   = $result->all;
2376
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
2377

            
2378

            
2379
test 'reserved_word_quote';
2380
$dbi = DBIx::Custom->connect;
2381
eval { $dbi->execute("drop table ${q}table$p") };
2382
$dbi->reserved_word_quote('"');
2383
$dbi->execute($create_table_reserved);
2384
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
2385
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
2386
$dbi->insert(table => 'table', param => {select => 1});
2387
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
2388
$result = $dbi->execute("select * from ${q}table$p");
2389
$rows   = $result->all;
2390
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
2391

            
2392
test 'quote';
2393
$dbi = DBIx::Custom->connect;
2394
$dbi->quote('"');
2395
eval { $dbi->execute("drop table ${q}table$p") };
2396
$dbi->execute($create_table_reserved);
2397
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
2398
$dbi->insert(table => 'table', param => {select => 1});
2399
$dbi->delete(table => 'table', where => {select => 1});
2400
$result = $dbi->execute("select * from ${q}table$p");
2401
$rows   = $result->all;
test cleanup
Yuki Kimoto authored on 2011-08-10
2402
is_deeply($rows, [], "reserved word");
2403

            
2404

            
2405

            
2406

            
2407

            
2408

            
2409

            
2410

            
2411

            
2412

            
2413

            
2414

            
2415

            
2416

            
2417

            
2418

            
2419

            
2420

            
2421

            
2422

            
2423

            
2424

            
2425

            
2426

            
2427

            
2428

            
2429

            
2430

            
2431

            
2432

            
2433

            
2434

            
2435
# DEPRECATED! test
2436
test 'filter __ expression';
2437
$dbi = DBIx::Custom->connect;
2438
eval { $dbi->execute('drop table company') };
2439
eval { $dbi->execute('drop table location') };
2440
$dbi->execute('create table company (id, name, location_id)');
2441
$dbi->execute('create table location (id, name)');
2442
$dbi->apply_filter('location',
2443
  name => {in => sub { uc $_[0] } }
2444
);
2445

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

            
2449
$result = $dbi->select(
2450
    table => ['company', 'location'], relation => {'company.location_id' => 'location.id'},
2451
    column => ['location.name as location__name']
2452
);
2453
is($result->fetch_first->[0], 'B');
2454

            
2455
$result = $dbi->select(
2456
    table => 'company', relation => {'company.location_id' => 'location.id'},
2457
    column => ['location.name as location__name']
2458
);
2459
is($result->fetch_first->[0], 'B');
2460

            
2461
$result = $dbi->select(
2462
    table => 'company', relation => {'company.location_id' => 'location.id'},
2463
    column => ['location.name as "location.name"']
2464
);
2465
is($result->fetch_first->[0], 'B');