DBIx-Custom / t / common.t /
Newer Older
2567 lines | 82.558kb
added common test executing ...
Yuki Kimoto authored on 2011-08-07
1
use Test::More;
2
use strict;
3
use warnings;
test cleanup
Yuki Kimoto authored on 2011-08-10
4
use Encode qw/encode_utf8/;
cleanup test
Yuki Kimoto authored on 2011-08-10
5
use FindBin;
6
use lib "$FindBin::Bin/common";
7

            
added common test executing ...
Yuki Kimoto authored on 2011-08-07
8
my $dbi;
9

            
10
plan skip_all => $ENV{DBIX_CUSTOM_SKIP_MESSAGE} || 'common.t is always skipped'
11
  unless $ENV{DBIX_CUSTOM_TEST_RUN}
12
    && eval { $dbi = DBIx::Custom->connect; 1 };
13

            
14
plan 'no_plan';
15

            
test cleanup
Yuki Kimoto authored on 2011-08-10
16
use MyDBI1;
17
{
18
    package MyDBI4;
19

            
20
    use strict;
21
    use warnings;
22

            
23
    use base 'DBIx::Custom';
24

            
25
    sub connect {
26
        my $self = shift->SUPER::connect(@_);
27
        
28
        $self->include_model(
29
            MyModel2 => [
30
                'book',
31
                {class => 'Company', name => 'company'}
32
            ]
33
        );
34
    }
35

            
36
    package MyModel2::Base1;
37

            
38
    use strict;
39
    use warnings;
40

            
41
    use base 'DBIx::Custom::Model';
42

            
43
    package MyModel2::book;
44

            
45
    use strict;
46
    use warnings;
47

            
48
    use base 'MyModel2::Base1';
49

            
50
    sub insert {
51
        my ($self, $param) = @_;
52
        
53
        return $self->SUPER::insert(param => $param);
54
    }
55

            
56
    sub list { shift->select; }
57

            
58
    package MyModel2::Company;
59

            
60
    use strict;
61
    use warnings;
62

            
63
    use base 'MyModel2::Base1';
64

            
65
    sub insert {
66
        my ($self, $param) = @_;
67
        
68
        return $self->SUPER::insert(param => $param);
69
    }
70

            
71
    sub list { shift->select; }
72
}
73
{
74
     package MyDBI5;
75

            
76
    use strict;
77
    use warnings;
78

            
79
    use base 'DBIx::Custom';
80

            
81
    sub connect {
82
        my $self = shift->SUPER::connect(@_);
83
        
84
        $self->include_model('MyModel4');
85
    }
86
}
87
{
88
    package MyDBI6;
89
    
90
    use base 'DBIx::Custom';
91
    
92
    sub connect {
93
        my $self = shift->SUPER::connect(@_);
94
        
95
        $self->include_model('MyModel5');
96
        
97
        return $self;
98
    }
99
}
100
{
101
    package MyDBI7;
102
    
103
    use base 'DBIx::Custom';
104
    
105
    sub connect {
106
        my $self = shift->SUPER::connect(@_);
107
        
108
        $self->include_model('MyModel6');
109
        
110
        
111
        return $self;
112
    }
113
}
114
{
115
    package MyDBI8;
116
    
117
    use base 'DBIx::Custom';
118
    
119
    sub connect {
120
        my $self = shift->SUPER::connect(@_);
121
        
122
        $self->include_model('MyModel7');
123
        
124
        return $self;
125
    }
126
}
127

            
128
{
129
    package MyDBI9;
130
    
131
    use base 'DBIx::Custom';
132
    
133
    sub connect {
134
        my $self = shift->SUPER::connect(@_);
135
        
cleanup test
Yuki Kimoto authored on 2011-08-10
136
        $self->include_model('MyModel8');
test cleanup
Yuki Kimoto authored on 2011-08-10
137
        
138
        return $self;
139
    }
140
}
141

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

            
added common test executing ...
Yuki Kimoto authored on 2011-08-07
145
# Constant
146
my $create_table1 = $dbi->create_table1;
cleanup test
Yuki Kimoto authored on 2011-08-08
147
my $create_table1_2 = $dbi->create_table1_2;
test cleanup
Yuki Kimoto authored on 2011-08-10
148
my $create_table1_type = $dbi->create_table1_type;
test cleanup
Yuki Kimoto authored on 2011-08-10
149
my $create_table2 = $dbi->create_table2;
test cleanup
Yuki Kimoto authored on 2011-08-10
150
my $create_table2_2 = $dbi->create_table2_2;
151
my $create_table3 = $dbi->create_table3;
test cleanup
Yuki Kimoto authored on 2011-08-10
152
my $create_table_reserved = $dbi->create_table_reserved;
cleanup test
Yuki Kimoto authored on 2011-08-10
153
my $q = substr($dbi->quote, 0, 1);
154
my $p = substr($dbi->quote, 1, 1) || $q;
added common test executing ...
Yuki Kimoto authored on 2011-08-07
155

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

            
188
# Drop table
189
eval { $dbi->execute('drop table table1') };
190

            
191
# Create table
192
$dbi->execute($create_table1);
193
$model = $dbi->create_model(table => 'table1');
194
$model->insert({key1 => 1, key2 => 2});
195
is_deeply($model->select->all, [{key1 => 1, key2 => 2}]);
196

            
cleanup test
Yuki Kimoto authored on 2011-08-08
197
test 'DBIx::Custom::Result test';
198
$dbi->delete_all(table => 'table1');
199
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
200
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
201
$source = "select key1, key2 from table1";
202
$query = $dbi->create_query($source);
203
$result = $dbi->execute($query);
204

            
205
@rows = ();
206
while (my $row = $result->fetch) {
207
    push @rows, [@$row];
208
}
209
is_deeply(\@rows, [[1, 2], [3, 4]], "fetch");
210

            
211
$result = $dbi->execute($query);
212
@rows = ();
213
while (my $row = $result->fetch_hash) {
214
    push @rows, {%$row};
215
}
216
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "fetch_hash");
217

            
218
$result = $dbi->execute($query);
219
$rows = $result->fetch_all;
220
is_deeply($rows, [[1, 2], [3, 4]], "fetch_all");
221

            
222
$result = $dbi->execute($query);
223
$rows = $result->fetch_hash_all;
224
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "all");
225

            
226
test 'Insert query return value';
227
$source = "insert into table1 {insert_param key1 key2}";
228
$query = $dbi->execute($source, {}, query => 1);
229
$ret_val = $dbi->execute($query, param => {key1 => 1, key2 => 2});
230
ok($ret_val);
231

            
232
test 'Direct query';
233
$dbi->delete_all(table => 'table1');
234
$insert_source = "insert into table1 {insert_param key1 key2}";
235
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2});
236
$result = $dbi->execute('select * from table1;');
237
$rows = $result->all;
238
is_deeply($rows, [{key1 => 1, key2 => 2}]);
239

            
240
test 'Filter basic';
241
$dbi->delete_all(table => 'table1');
242
$dbi->register_filter(twice       => sub { $_[0] * 2}, 
243
                    three_times => sub { $_[0] * 3});
244

            
245
$insert_source  = "insert into table1 {insert_param key1 key2};";
246
$insert_query = $dbi->execute($insert_source, {}, query => 1);
247
$insert_query->filter({key1 => 'twice'});
248
$dbi->execute($insert_query, param => {key1 => 1, key2 => 2});
249
$result = $dbi->execute('select * from table1;');
250
$rows = $result->filter({key2 => 'three_times'})->all;
251
is_deeply($rows, [{key1 => 2, key2 => 6}], "filter fetch_filter");
252

            
253
test 'Filter in';
254
$dbi->delete_all(table => 'table1');
255
$insert_source  = "insert into table1 {insert_param key1 key2};";
256
$insert_query = $dbi->execute($insert_source, {}, query => 1);
257
$dbi->execute($insert_query, param => {key1 => 2, key2 => 4});
258
$select_source = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}";
259
$select_query = $dbi->execute($select_source,{}, query => 1);
260
$select_query->filter({'table1.key1' => 'twice'});
261
$result = $dbi->execute($select_query, param => {'table1.key1' => [1,5], 'table1.key2' => [2,4]});
262
$rows = $result->all;
263
is_deeply($rows, [{key1 => 2, key2 => 4}], "filter");
264

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

            
271
$source = "select * from table1 where key1 = :key1 and {<> key2} and {< key3} and {> key4} and {>= key5};";
272
$query = $dbi->execute($source, {}, query => 1);
273
$result = $dbi->execute($query, param => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
274
$rows = $result->all;
275
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1");
276

            
277
$source = "select * from table1 where key1 = :key1 and {<> key2} and {< key3} and {> key4} and {>= key5};";
278
$query = $dbi->execute($source, {}, query => 1);
279
$result = $dbi->execute($query, {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
280
$rows = $result->all;
281
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1");
282

            
283
$source = "select * from table1 where {<= key1} and {like key2};";
284
$query = $dbi->execute($source, {}, query => 1);
285
$result = $dbi->execute($query, param => {key1 => 1, key2 => '%2%'});
286
$rows = $result->all;
287
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag2");
288

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

            
295
$source = "select * from table1 where {in key1 2};";
296
$query = $dbi->execute($source, {}, query => 1);
297
$result = $dbi->execute($query, param => {key1 => [9, 1]});
298
$rows = $result->all;
299
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic");
300

            
301
test 'DBIx::Custom::SQLTemplate insert tag';
302
$dbi->delete_all(table => 'table1');
303
$insert_source = 'insert into table1 {insert_param key1 key2 key3 key4 key5}';
304
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
305

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

            
310
test 'DBIx::Custom::SQLTemplate update tag';
311
$dbi->delete_all(table => 'table1');
312
$insert_source = "insert into table1 {insert_param key1 key2 key3 key4 key5}";
313
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
314
$dbi->execute($insert_source, param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
315

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

            
319
$result = $dbi->execute('select * from table1 order by key1;');
320
$rows = $result->all;
321
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5},
322
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "basic");
323

            
cleanup test
Yuki Kimoto authored on 2011-08-08
324
test 'Named placeholder';
325
$dbi->execute('drop table table1');
326
$dbi->execute($create_table1_2);
327
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
328
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
329

            
330
$source = "select * from table1 where key1 = :key1 and key2 = :key2";
331
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
332
$rows = $result->all;
333
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
334

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

            
340
$source = "select * from table1 where key1 = :key1 or key1 = :key1";
341
$result = $dbi->execute($source, param => {key1 => [1, 2]});
342
$rows = $result->all;
343
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
344

            
345
$source = "select * from table1 where key1 = :table1.key1 and key2 = :table1.key2";
346
$result = $dbi->execute(
347
    $source,
348
    param => {'table1.key1' => 1, 'table1.key2' => 1},
349
    filter => {'table1.key2' => sub { $_[0] * 2 }}
350
);
351
$rows = $result->all;
352
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
353

            
354
$dbi->execute('drop table table1');
355
$dbi->execute($create_table1);
356
$dbi->insert(table => 'table1', param => {key1 => '2011-10-14 12:19:18', key2 => 2});
357
$source = "select * from table1 where key1 = '2011-10-14 12:19:18' and key2 = :key2";
358
$result = $dbi->execute(
359
    $source,
360
    param => {'key2' => 2},
361
);
362

            
363
$rows = $result->all;
364
is_deeply($rows, [{key1 => '2011-10-14 12:19:18', key2 => 2}]);
365

            
366
$dbi->delete_all(table => 'table1');
367
$dbi->insert(table => 'table1', param => {key1 => 'a:b c:d', key2 => 2});
368
$source = "select * from table1 where key1 = 'a\\:b c\\:d' and key2 = :key2";
369
$result = $dbi->execute(
370
    $source,
371
    param => {'key2' => 2},
372
);
373
$rows = $result->all;
374
is_deeply($rows, [{key1 => 'a:b c:d', key2 => 2}]);
375

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
383
test 'insert';
cleanup test
Yuki Kimoto authored on 2011-08-10
384
eval { $dbi->execute('drop table table1') };
385
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
386
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
387
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
388
$result = $dbi->execute('select * from table1;');
389
$rows   = $result->all;
390
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
391

            
392
$dbi->execute('delete from table1');
393
$dbi->register_filter(
394
    twice       => sub { $_[0] * 2 },
395
    three_times => sub { $_[0] * 3 }
396
);
397
$dbi->default_bind_filter('twice');
398
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => 'three_times'});
399
$result = $dbi->execute('select * from table1;');
400
$rows   = $result->all;
401
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter");
402
$dbi->default_bind_filter(undef);
403

            
cleanup test
Yuki Kimoto authored on 2011-08-10
404
$dbi->execute('drop table table1');
405
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
406
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, append => '   ');
407
$rows = $dbi->select(table => 'table1')->all;
408
is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append');
409

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

            
413
eval{$dbi->insert(table => 'table', param => {';' => 1})};
414
like($@, qr/safety/);
415

            
cleanup test
Yuki Kimoto authored on 2011-08-10
416
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
417
$dbi->execute($create_table_reserved);
cleanup test
Yuki Kimoto authored on 2011-08-10
418
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
419
$dbi->insert(table => 'table', param => {select => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
420
$result = $dbi->execute("select * from ${q}table$p");
cleanup test
Yuki Kimoto authored on 2011-08-10
421
$rows   = $result->all;
test cleanup
Yuki Kimoto authored on 2011-08-10
422
is_deeply($rows, [{select => 2, update => undef}], "reserved word");
cleanup test
Yuki Kimoto authored on 2011-08-10
423

            
cleanup test
Yuki Kimoto authored on 2011-08-10
424
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
425
$dbi->execute($create_table1);
426
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
427
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
428
$result = $dbi->execute('select * from table1;');
429
$rows   = $result->all;
430
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
431

            
cleanup test
Yuki Kimoto authored on 2011-08-10
432
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
433
$dbi->execute($create_table1);
434
$dbi->insert(table => 'table1', param => {key1 => \"'1'", key2 => 2});
435
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
436
$result = $dbi->execute('select * from table1;');
437
$rows   = $result->all;
438
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
439

            
test cleanup
Yuki Kimoto authored on 2011-08-10
440
test 'update';
441
eval { $dbi->execute('drop table table1') };
442
$dbi->execute($create_table1_2);
443
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
444
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
445
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
446
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
447
$rows   = $result->all;
448
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
449
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
450
                  "basic");
451
                  
452
$dbi->execute("delete from table1");
453
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
454
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
455
$dbi->update(table => 'table1', param => {key2 => 12}, where => {key2 => 2, key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
456
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
457
$rows   = $result->all;
458
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
459
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
460
                  "update key same as search key");
461

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

            
469
$dbi->execute("delete from table1");
470
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
471
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
472
$dbi->register_filter(twice => sub { $_[0] * 2 });
473
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1},
474
              filter => {key2 => sub { $_[0] * 2 }});
test cleanup
Yuki Kimoto authored on 2011-08-10
475
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
476
$rows   = $result->all;
477
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5},
478
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
479
                  "filter");
480

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

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

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

            
489
eval { $dbi->execute('drop table table1') };
490
$dbi->execute($create_table1);
491
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
492
$where = $dbi->where;
493
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
494
$where->param({key1 => 1, key2 => 2});
495
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
496
$result = $dbi->select(table => 'table1');
497
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
498

            
499
eval { $dbi->execute('drop table table1') };
500
$dbi->execute($create_table1);
501
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
502
$dbi->update(
503
    table => 'table1',
504
    param => {key1 => 3},
505
    where => [
506
        ['and', 'key1 = :key1', 'key2 = :key2'],
507
        {key1 => 1, key2 => 2}
508
    ]
509
);
510
$result = $dbi->select(table => 'table1');
511
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
512

            
513
eval { $dbi->execute('drop table table1') };
514
$dbi->execute($create_table1);
515
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
516
$where = $dbi->where;
517
$where->clause(['and', 'key2 = :key2']);
518
$where->param({key2 => 2});
519
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
520
$result = $dbi->select(table => 'table1');
521
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
522

            
523
eval{$dbi->update(table => 'table1', param => {';' => 1})};
524
like($@, qr/safety/);
525

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

            
529
eval { $dbi->execute('drop table table1') };
530
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
531
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
532
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
533
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
534
$dbi->insert(table => 'table', param => {select => 1});
535
$dbi->update(table => 'table', where => {select => 1}, param => {update => 2});
536
$result = $dbi->execute("select * from ${q}table$p");
537
$rows   = $result->all;
538
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
539

            
540
eval {$dbi->update_all(table => 'table', param => {';' => 2}) };
541
like($@, qr/safety/);
542

            
543
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
544
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
545
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
546
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
547
$dbi->insert(table => 'table', param => {select => 1});
548
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
549
$result = $dbi->execute("select * from ${q}table$p");
550
$rows   = $result->all;
551
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
552

            
553
eval { $dbi->execute('drop table table1') };
554
$dbi->execute($create_table1_2);
555
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
556
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
557
$dbi->update({key2 => 11}, table => 'table1', where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
558
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
559
$rows   = $result->all;
560
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
561
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
562
                  "basic");
563

            
564
eval { $dbi->execute('drop table table1') };
565
$dbi->execute($create_table1_2);
566
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
567
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
568
$dbi->update(table => 'table1', param => {key2 => \"'11'"}, where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
569
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
570
$rows   = $result->all;
571
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
572
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
573
                  "basic");
574

            
575
test 'update_all';
576
eval { $dbi->execute('drop table table1') };
577
$dbi->execute($create_table1_2);
578
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
579
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
580
$dbi->register_filter(twice => sub { $_[0] * 2 });
581
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'});
582
$result = $dbi->execute('select * from table1;');
583
$rows   = $result->all;
584
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
585
                  {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
586
                  "filter");
587

            
588

            
589
test 'delete';
590
eval { $dbi->execute('drop table table1') };
591
$dbi->execute($create_table1);
592
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
593
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
594
$dbi->delete(table => 'table1', where => {key1 => 1});
595
$result = $dbi->execute('select * from table1;');
596
$rows   = $result->all;
597
is_deeply($rows, [{key1 => 3, key2 => 4}], "basic");
598

            
599
$dbi->execute("delete from table1;");
600
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
601
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
602
$dbi->register_filter(twice => sub { $_[0] * 2 });
603
$dbi->delete(table => 'table1', where => {key2 => 1}, filter => {key2 => 'twice'});
604
$result = $dbi->execute('select * from table1;');
605
$rows   = $result->all;
606
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter");
607

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

            
610
$dbi->delete_all(table => 'table1');
611
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
612
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
613
$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2});
614
$rows = $dbi->select(table => 'table1')->all;
615
is_deeply($rows, [{key1 => 3, key2 => 4}], "delete multi key");
616

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

            
620
eval { $dbi->execute('drop table table1') };
621
$dbi->execute($create_table1);
622
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
623
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
624
$where = $dbi->where;
625
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
626
$where->param({ke1 => 1, key2 => 2});
627
$dbi->delete(table => 'table1', where => $where);
628
$result = $dbi->select(table => 'table1');
629
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
630

            
631
eval { $dbi->execute('drop table table1') };
632
$dbi->execute($create_table1);
633
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
634
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
635
$dbi->delete(
636
    table => 'table1',
637
    where => [
638
        ['and', 'key1 = :key1', 'key2 = :key2'],
639
        {ke1 => 1, key2 => 2}
640
    ]
641
);
642
$result = $dbi->select(table => 'table1');
643
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
644

            
645
eval { $dbi->execute('drop table table1') };
test cleanup
Yuki Kimoto authored on 2011-08-10
646
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
647
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
648
$dbi->delete(table => 'table1', where => {key1 => 1}, prefix => '    ');
649
$result = $dbi->execute('select * from table1;');
650
$rows   = $result->all;
651
is_deeply($rows, [], "basic");
652

            
653
test 'delete error';
654
eval { $dbi->execute('drop table table1') };
655
$dbi->execute($create_table1);
656
eval{$dbi->delete(table => 'table1')};
657
like($@, qr/"where" must be specified/,
658
         "where key-value pairs not specified");
659

            
660
eval{$dbi->delete(table => 'table1', where => {';' => 1})};
661
like($@, qr/safety/);
662

            
663
$dbi = DBIx::Custom->connect;
664
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
665
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
666
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
667
$dbi->insert(table => 'table', param => {select => 1});
668
$dbi->delete(table => 'table', where => {select => 1});
669
$result = $dbi->execute("select * from ${q}table$p");
670
$rows   = $result->all;
671
is_deeply($rows, [], "reserved word");
672

            
673
test 'delete_all';
674
eval { $dbi->execute('drop table table1') };
675
$dbi->execute($create_table1);
676
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
677
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
678
$dbi->delete_all(table => 'table1');
679
$result = $dbi->execute('select * from table1;');
680
$rows   = $result->all;
681
is_deeply($rows, [], "basic");
682

            
683

            
684
test 'select';
685
eval { $dbi->execute('drop table table1') };
686
$dbi->execute($create_table1);
687
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
688
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
689
$rows = $dbi->select(table => 'table1')->all;
690
is_deeply($rows, [{key1 => 1, key2 => 2},
691
                  {key1 => 3, key2 => 4}], "table");
692

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

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

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

            
702
$rows = $dbi->select(table => 'table1', append => "order by key1 desc limit 1")->all;
703
is_deeply($rows, [{key1 => 3, key2 => 4}], "append statement");
704

            
705
$dbi->register_filter(decrement => sub { $_[0] - 1 });
706
$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'})
707
            ->all;
708
is_deeply($rows, [{key1 => 1, key2 => 2}], "filter");
709

            
test cleanup
Yuki Kimoto authored on 2011-08-10
710
eval { $dbi->execute("drop table table2") };
711
$dbi->execute($create_table2);
test cleanup
Yuki Kimoto authored on 2011-08-10
712
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
713
$rows = $dbi->select(
714
    table => [qw/table1 table2/],
715
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
716
    where   => {'table1.key2' => 2},
717
    relation  => {'table1.key1' => 'table2.key1'}
718
)->all;
719
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : exists where");
720

            
721
$rows = $dbi->select(
722
    table => [qw/table1 table2/],
723
    column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
724
    relation  => {'table1.key1' => 'table2.key1'}
725
)->all;
726
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : no exists where");
727

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

            
731
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
732
eval { $dbi->execute("drop table ${q}table$p") };
733
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
734
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
735
$dbi->insert(table => 'table', param => {select => 1, update => 2});
736
$result = $dbi->select(table => 'table', where => {select => 1});
737
$rows   = $result->all;
738
is_deeply($rows, [{select => 2, update => 2}], "reserved word");
739

            
740
test 'fetch filter';
741
eval { $dbi->execute('drop table table1') };
742
$dbi->register_filter(
743
    twice       => sub { $_[0] * 2 },
744
    three_times => sub { $_[0] * 3 }
745
);
746
$dbi->default_fetch_filter('twice');
747
$dbi->execute($create_table1);
748
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
749
$result = $dbi->select(table => 'table1');
750
$result->filter({key1 => 'three_times'});
751
$row = $result->one;
752
is_deeply($row, {key1 => 3, key2 => 4}, "default_fetch_filter and filter");
753

            
754
test 'filters';
755
$dbi = DBIx::Custom->new;
756

            
757
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')),
758
   'あ', "decode_utf8");
759

            
760
is($dbi->filters->{encode_utf8}->('あ'),
761
   encode_utf8('あ'), "encode_utf8");
762

            
cleanup test
Yuki Kimoto authored on 2011-08-10
763
test 'transaction1';
test cleanup
Yuki Kimoto authored on 2011-08-10
764
$dbi = DBIx::Custom->connect;
765
eval { $dbi->execute('drop table table1') };
766
$dbi->execute($create_table1);
767
$dbi->dbh->begin_work;
768
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
769
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
770
$dbi->dbh->commit;
771
$result = $dbi->select(table => 'table1');
772
is_deeply(scalar $result->all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}],
773
          "commit");
774

            
775
$dbi = DBIx::Custom->connect;
776
eval { $dbi->execute('drop table table1') };
777
$dbi->execute($create_table1);
778
$dbi->dbh->begin_work(0);
779
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
780
$dbi->dbh->rollback;
781

            
782
$result = $dbi->select(table => 'table1');
783
ok(! $result->fetch_first, "rollback");
784

            
785
test 'execute';
786
eval { $dbi->execute('drop table table1') };
787
$dbi->execute($create_table1);
788
{
789
    local $Carp::Verbose = 0;
790
    eval{$dbi->execute('select * frm table1')};
791
    like($@, qr/\Qselect * frm table1;/, "fail prepare");
792
    like($@, qr/\.t /, "fail : not verbose");
793
}
794
{
795
    local $Carp::Verbose = 1;
796
    eval{$dbi->execute('select * frm table1')};
797
    like($@, qr/Custom.*\.t /s, "fail : verbose");
798
}
799

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

            
803
$query = $dbi->execute('select * from table1 where key1 = :key1', {}, query => 1);
804
$dbi->dbh->disconnect;
805
eval{$dbi->execute($query, param => {key1 => {a => 1}})};
806
ok($@, "execute fail");
807

            
808
{
809
    local $Carp::Verbose = 0;
810
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
811
    like($@, qr/\Q.t /, "caller spec : not vebose");
812
}
813
{
814
    local $Carp::Verbose = 1;
815
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
816
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
817
}
818

            
819

            
cleanup test
Yuki Kimoto authored on 2011-08-10
820
test 'transaction2';
test cleanup
Yuki Kimoto authored on 2011-08-10
821
$dbi = DBIx::Custom->connect;
822
eval { $dbi->execute('drop table table1') };
823
$dbi->execute($create_table1);
824

            
825
$dbi->begin_work;
826

            
827
eval {
828
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
829
    die "Error";
830
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
831
};
832

            
833
$dbi->rollback if $@;
834

            
835
$result = $dbi->select(table => 'table1');
836
$rows = $result->all;
837
is_deeply($rows, [], "rollback");
838

            
839
$dbi->begin_work;
840

            
841
eval {
842
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
843
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
844
};
845

            
846
$dbi->commit unless $@;
847

            
848
$result = $dbi->select(table => 'table1');
849
$rows = $result->all;
850
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "commit");
851

            
852
$dbi->dbh->{AutoCommit} = 0;
853
eval{ $dbi->begin_work };
854
ok($@, "exception");
855
$dbi->dbh->{AutoCommit} = 1;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
856

            
test cleanup
Yuki Kimoto authored on 2011-08-10
857
test 'cache';
858
eval { $dbi->execute('drop table table1') };
859
$dbi->cache(1);
860
$dbi->execute($create_table1);
861
$source = 'select * from table1 where key1 = :key1 and key2 = :key2;';
862
$dbi->execute($source, {}, query => 1);
863
is_deeply($dbi->{_cached}->{$source}, 
864
          {sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2'], tables => []}, "cache");
865

            
866
eval { $dbi->execute('drop table table1') };
867
$dbi->execute($create_table1);
868
$dbi->{_cached} = {};
869
$dbi->cache(0);
870
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
871
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
872

            
873
test 'execute';
874
eval { $dbi->execute('drop table table1') };
875
$dbi->execute($create_table1);
876
{
877
    local $Carp::Verbose = 0;
878
    eval{$dbi->execute('select * frm table1')};
879
    like($@, qr/\Qselect * frm table1;/, "fail prepare");
880
    like($@, qr/\.t /, "fail : not verbose");
881
}
882
{
883
    local $Carp::Verbose = 1;
884
    eval{$dbi->execute('select * frm table1')};
885
    like($@, qr/Custom.*\.t /s, "fail : verbose");
886
}
887

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

            
891
$query = $dbi->execute('select * from table1 where key1 = :key1', {}, query => 1);
892
$dbi->dbh->disconnect;
893
eval{$dbi->execute($query, param => {key1 => {a => 1}})};
894
ok($@, "execute fail");
895

            
896
{
897
    local $Carp::Verbose = 0;
898
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
899
    like($@, qr/\Q.t /, "caller spec : not vebose");
900
}
901
{
902
    local $Carp::Verbose = 1;
903
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
904
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
905
}
906

            
907
test 'method';
908
$dbi->method(
909
    one => sub { 1 }
910
);
911
$dbi->method(
912
    two => sub { 2 }
913
);
914
$dbi->method({
915
    twice => sub {
916
        my $self = shift;
917
        return $_[0] * 2;
918
    }
919
});
920

            
921
is($dbi->one, 1, "first");
922
is($dbi->two, 2, "second");
923
is($dbi->twice(5), 10 , "second");
924

            
925
eval {$dbi->XXXXXX};
926
ok($@, "not exists");
927

            
928
test 'out filter';
929
$dbi = DBIx::Custom->connect;
930
eval { $dbi->execute('drop table table1') };
931
$dbi->execute($create_table1);
932
$dbi->register_filter(twice => sub { $_[0] * 2 });
933
$dbi->register_filter(three_times => sub { $_[0] * 3});
934
$dbi->apply_filter(
935
    'table1', 'key1' => {out => 'twice', in => 'three_times'}, 
936
              'key2' => {out => 'three_times', in => 'twice'});
937
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
938
$result = $dbi->execute('select * from table1;');
939
$row   = $result->fetch_hash_first;
940
is_deeply($row, {key1 => 2, key2 => 6}, "insert");
941
$result = $dbi->select(table => 'table1');
942
$row   = $result->one;
943
is_deeply($row, {key1 => 6, key2 => 12}, "insert");
944

            
945
$dbi = DBIx::Custom->connect;
946
eval { $dbi->execute('drop table table1') };
947
$dbi->execute($create_table1);
948
$dbi->register_filter(twice => sub { $_[0] * 2 });
949
$dbi->register_filter(three_times => sub { $_[0] * 3});
950
$dbi->apply_filter(
951
    'table1', 'key1' => {out => 'twice', in => 'three_times'}, 
952
              'key2' => {out => 'three_times', in => 'twice'});
953
$dbi->apply_filter(
954
    'table1', 'key1' => {out => undef}
955
); 
956
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
957
$result = $dbi->execute('select * from table1;');
958
$row   = $result->one;
959
is_deeply($row, {key1 => 1, key2 => 6}, "insert");
960

            
961
$dbi = DBIx::Custom->connect;
962
eval { $dbi->execute('drop table table1') };
963
$dbi->execute($create_table1);
964
$dbi->register_filter(twice => sub { $_[0] * 2 });
965
$dbi->apply_filter(
966
    'table1', 'key1' => {out => 'twice', in => 'twice'}
967
);
968
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => undef});
969
$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2});
970
$result = $dbi->execute('select * from table1;');
971
$row   = $result->one;
972
is_deeply($row, {key1 => 4, key2 => 2}, "update");
973

            
974
$dbi = DBIx::Custom->connect;
975
eval { $dbi->execute('drop table table1') };
976
$dbi->execute($create_table1);
977
$dbi->register_filter(twice => sub { $_[0] * 2 });
978
$dbi->apply_filter(
979
    'table1', 'key1' => {out => 'twice', in => 'twice'}
980
);
981
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1=> undef});
982
$dbi->delete(table => 'table1', where => {key1 => 1});
983
$result = $dbi->execute('select * from table1;');
984
$rows   = $result->all;
985
is_deeply($rows, [], "delete");
986

            
987
$dbi = DBIx::Custom->connect;
988
eval { $dbi->execute('drop table table1') };
989
$dbi->execute($create_table1);
990
$dbi->register_filter(twice => sub { $_[0] * 2 });
991
$dbi->apply_filter(
992
    'table1', 'key1' => {out => 'twice', in => 'twice'}
993
);
994
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
995
$result = $dbi->select(table => 'table1', where => {key1 => 1});
996
$result->filter({'key2' => 'twice'});
997
$rows   = $result->all;
998
is_deeply($rows, [{key1 => 4, key2 => 4}], "select");
999

            
1000
$dbi = DBIx::Custom->connect;
1001
eval { $dbi->execute('drop table table1') };
1002
$dbi->execute($create_table1);
1003
$dbi->register_filter(twice => sub { $_[0] * 2 });
1004
$dbi->apply_filter(
1005
    'table1', 'key1' => {out => 'twice', in => 'twice'}
1006
);
1007
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
1008
$result = $dbi->execute("select * from table1 where key1 = :key1 and key2 = :key2;",
1009
                        param => {key1 => 1, key2 => 2},
1010
                        table => ['table1']);
1011
$rows   = $result->all;
1012
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute");
1013

            
1014
$dbi = DBIx::Custom->connect;
1015
eval { $dbi->execute('drop table table1') };
1016
$dbi->execute($create_table1);
1017
$dbi->register_filter(twice => sub { $_[0] * 2 });
1018
$dbi->apply_filter(
1019
    'table1', 'key1' => {out => 'twice', in => 'twice'}
1020
);
1021
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
1022
$result = $dbi->execute("select * from {table table1} where key1 = :key1 and key2 = :key2;",
1023
                        param => {key1 => 1, key2 => 2});
1024
$rows   = $result->all;
1025
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute table tag");
1026

            
1027
$dbi = DBIx::Custom->connect;
1028
eval { $dbi->execute('drop table table1') };
1029
eval { $dbi->execute('drop table table2') };
1030
$dbi->execute($create_table1);
1031
$dbi->execute($create_table2);
1032
$dbi->register_filter(twice => sub { $_[0] * 2 });
1033
$dbi->register_filter(three_times => sub { $_[0] * 3 });
1034
$dbi->apply_filter(
1035
    'table1', 'key2' => {out => 'twice', in => 'twice'}
1036
);
1037
$dbi->apply_filter(
1038
    'table2', 'key3' => {out => 'three_times', in => 'three_times'}
1039
);
1040
$dbi->insert(table => 'table1', param => {key1 => 5, key2 => 2}, filter => {key2 => undef});
1041
$dbi->insert(table => 'table2', param => {key1 => 5, key3 => 6}, filter => {key3 => undef});
1042
$result = $dbi->select(
1043
     table => ['table1', 'table2'],
1044
     column => ['key2', 'key3'],
1045
     where => {'table1.key2' => 1, 'table2.key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
1046

            
1047
$result->filter({'key2' => 'twice'});
1048
$rows   = $result->all;
1049
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join");
1050

            
1051
$result = $dbi->select(
1052
     table => ['table1', 'table2'],
1053
     column => ['key2', 'key3'],
1054
     where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
1055

            
1056
$result->filter({'key2' => 'twice'});
1057
$rows   = $result->all;
1058
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join : omit");
1059

            
1060
test 'each_column';
1061
$dbi = DBIx::Custom->connect;
1062
eval { $dbi->execute("drop table ${q}table$p") };
1063
eval { $dbi->execute('drop table table1') };
1064
eval { $dbi->execute('drop table table2') };
test cleranup
Yuki Kimoto authored on 2011-08-10
1065
eval { $dbi->execute('drop table table3') };
test cleanup
Yuki Kimoto authored on 2011-08-10
1066
$dbi->execute($create_table1_type);
1067
$dbi->execute($create_table2);
1068

            
1069
$infos = [];
1070
$dbi->each_column(sub {
1071
    my ($self, $table, $column, $cinfo) = @_;
1072
    
1073
    if ($table =~ /^table\d/) {
1074
         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
1075
         push @$infos, $info;
1076
    }
1077
});
1078
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
1079
is_deeply($infos, 
1080
    [
1081
        ['table1', 'key1', 'key1'],
1082
        ['table1', 'key2', 'key2'],
1083
        ['table2', 'key1', 'key1'],
1084
        ['table2', 'key3', 'key3']
1085
    ]
1086
    
1087
);
1088
test 'each_table';
1089
$dbi = DBIx::Custom->connect;
1090
eval { $dbi->execute('drop table table1') };
1091
eval { $dbi->execute('drop table table2') };
1092
$dbi->execute($create_table2);
1093
$dbi->execute($create_table1_type);
1094

            
1095
$infos = [];
1096
$dbi->each_table(sub {
1097
    my ($self, $table, $table_info) = @_;
1098
    
1099
    if ($table =~ /^table\d/) {
1100
         my $info = [$table, $table_info->{TABLE_NAME}];
1101
         push @$infos, $info;
1102
    }
1103
});
1104
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
1105
is_deeply($infos, 
1106
    [
1107
        ['table1', 'table1'],
1108
        ['table2', 'table2'],
1109
    ]
1110
);
1111

            
1112
test 'limit';
1113
$dbi = DBIx::Custom->connect;
1114
eval { $dbi->execute('drop table table1') };
1115
$dbi->execute($create_table1);
1116
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1117
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
1118
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
1119
$dbi->register_tag(
1120
    limit => sub {
1121
        my ($count, $offset) = @_;
1122
        
1123
        my $s = '';
1124
        $s .= "limit $count";
1125
        $s .= " offset $offset" if defined $offset;
1126
        
1127
        return [$s, []];
1128
    }
1129
);
1130
$rows = $dbi->select(
1131
  table => 'table1',
1132
  where => {key1 => 1},
1133
  append => "order by key2 {limit 1 0}"
1134
)->all;
1135
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1136
$rows = $dbi->select(
1137
  table => 'table1',
1138
  where => {key1 => 1},
1139
  append => "order by key2 {limit 2 1}"
1140
)->all;
1141
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
1142
$rows = $dbi->select(
1143
  table => 'table1',
1144
  where => {key1 => 1},
1145
  append => "order by key2 {limit 1}"
1146
)->all;
1147
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1148

            
1149
test 'connect super';
test cleanup
Yuki Kimoto authored on 2011-08-10
1150
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1151
eval { $dbi->execute('drop table table1') };
1152
$dbi->execute($create_table1);
1153
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1154
is($dbi->select(table => 'table1')->one->{key1}, 1);
1155

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1156
$dbi = DBIx::Custom->new;
test cleanup
Yuki Kimoto authored on 2011-08-10
1157
$dbi->connect;
1158
eval { $dbi->execute('drop table table1') };
1159
$dbi->execute($create_table1);
1160
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1161
is($dbi->select(table => 'table1')->one->{key1}, 1);
1162

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1163
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1164
eval { $dbi->execute('drop table table1') };
1165
$dbi->execute($create_table1);
1166
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1167
is($dbi->select(table => 'table1')->one->{key1}, 1);
1168

            
1169
test 'end_filter';
1170
$dbi = DBIx::Custom->connect;
1171
eval { $dbi->execute('drop table table1') };
1172
$dbi->execute($create_table1);
1173
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1174
$result = $dbi->select(table => 'table1');
1175
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1176
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 });
1177
$row = $result->fetch_first;
1178
is_deeply($row, [6, 40]);
1179

            
1180
$dbi = DBIx::Custom->connect;
1181
eval { $dbi->execute('drop table table1') };
1182
$dbi->execute($create_table1);
1183
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1184
$result = $dbi->select(table => 'table1');
1185
$result->filter([qw/key1 key2/] => sub { $_[0] * 2 });
1186
$result->end_filter([[qw/key1 key2/] => sub { $_[0] * 3 }]);
1187
$row = $result->fetch_first;
1188
is_deeply($row, [6, 12]);
1189

            
1190
$dbi = DBIx::Custom->connect;
1191
eval { $dbi->execute('drop table table1') };
1192
$dbi->execute($create_table1);
1193
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1194
$result = $dbi->select(table => 'table1');
1195
$result->filter([[qw/key1 key2/] => sub { $_[0] * 2 }]);
1196
$result->end_filter([qw/key1 key2/] => sub { $_[0] * 3 });
1197
$row = $result->fetch_first;
1198
is_deeply($row, [6, 12]);
1199

            
1200
$dbi->register_filter(five_times => sub { $_[0] * 5 });
1201
$result = $dbi->select(table => 'table1');
1202
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1203
$result->end_filter({key1 => sub { $_[0] * 3 }, key2 => 'five_times' });
1204
$row = $result->one;
1205
is_deeply($row, {key1 => 6, key2 => 40});
1206

            
1207
$dbi->register_filter(five_times => sub { $_[0] * 5 });
1208
$dbi->apply_filter('table1',
1209
    key1 => {end => sub { $_[0] * 3 } },
1210
    key2 => {end => 'five_times'}
1211
);
1212
$result = $dbi->select(table => 'table1');
1213
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1214
$row = $result->one;
1215
is_deeply($row, {key1 => 6, key2 => 40}, 'apply_filter');
1216

            
1217
$dbi->register_filter(five_times => sub { $_[0] * 5 });
1218
$dbi->apply_filter('table1',
1219
    key1 => {end => sub { $_[0] * 3 } },
1220
    key2 => {end => 'five_times'}
1221
);
1222
$result = $dbi->select(table => 'table1');
1223
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1224
$result->filter(key1 => undef);
1225
$result->end_filter(key1 => undef);
1226
$row = $result->one;
1227
is_deeply($row, {key1 => 1, key2 => 40}, 'apply_filter overwrite');
1228

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1229
test 'remove_end_filter and remove_filter';
1230
$dbi = DBIx::Custom->connect;
1231
eval { $dbi->execute('drop table table1') };
1232
$dbi->execute($create_table1);
1233
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1234
$result = $dbi->select(table => 'table1');
1235
$row = $result
1236
       ->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 })
1237
       ->remove_filter
1238
       ->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 })
1239
       ->remove_end_filter
1240
       ->fetch_first;
1241
is_deeply($row, [1, 2]);
1242

            
1243
test 'empty where select';
1244
$dbi = DBIx::Custom->connect;
1245
eval { $dbi->execute('drop table table1') };
1246
$dbi->execute($create_table1);
1247
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1248
$result = $dbi->select(table => 'table1', where => {});
1249
$row = $result->one;
1250
is_deeply($row, {key1 => 1, key2 => 2});
1251

            
1252
test 'select query option';
1253
$dbi = DBIx::Custom->connect;
1254
eval { $dbi->execute('drop table table1') };
1255
$dbi->execute($create_table1);
1256
$query = $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, query => 1);
1257
is(ref $query, 'DBIx::Custom::Query');
1258
$query = $dbi->update(table => 'table1', where => {key1 => 1}, param => {key2 => 2}, query => 1);
1259
is(ref $query, 'DBIx::Custom::Query');
1260
$query = $dbi->delete(table => 'table1', where => {key1 => 1}, query => 1);
1261
is(ref $query, 'DBIx::Custom::Query');
1262
$query = $dbi->select(table => 'table1', where => {key1 => 1, key2 => 2}, query => 1);
1263
is(ref $query, 'DBIx::Custom::Query');
1264

            
1265
test 'where';
1266
$dbi = DBIx::Custom->connect;
1267
eval { $dbi->execute('drop table table1') };
1268
$dbi->execute($create_table1);
1269
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1270
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1271
$where = $dbi->where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
1272
is("$where", "where ( key1 = :key1 and key2 = :key2 )", 'no param');
1273

            
1274
$where = $dbi->where
1275
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
1276
             ->param({key1 => 1});
1277

            
1278
$result = $dbi->select(
1279
    table => 'table1',
1280
    where => $where
1281
);
1282
$row = $result->all;
1283
is_deeply($row, [{key1 => 1, key2 => 2}]);
1284

            
1285
$result = $dbi->select(
1286
    table => 'table1',
1287
    where => [
1288
        ['and', 'key1 = :key1', 'key2 = :key2'],
1289
        {key1 => 1}
1290
    ]
1291
);
1292
$row = $result->all;
1293
is_deeply($row, [{key1 => 1, key2 => 2}]);
1294

            
1295
$where = $dbi->where
1296
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
1297
             ->param({key1 => 1, key2 => 2});
1298
$result = $dbi->select(
1299
    table => 'table1',
1300
    where => $where
1301
);
1302
$row = $result->all;
1303
is_deeply($row, [{key1 => 1, key2 => 2}]);
1304

            
1305
$where = $dbi->where
1306
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
1307
             ->param({});
1308
$result = $dbi->select(
1309
    table => 'table1',
1310
    where => $where,
1311
);
1312
$row = $result->all;
1313
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1314

            
1315
$where = $dbi->where
1316
             ->clause(['and', ['or', 'key1 > :key1', 'key1 < :key1'], 'key2 = :key2'])
1317
             ->param({key1 => [0, 3], key2 => 2});
1318
$result = $dbi->select(
1319
    table => 'table1',
1320
    where => $where,
1321
); 
1322
$row = $result->all;
1323
is_deeply($row, [{key1 => 1, key2 => 2}]);
1324

            
1325
$where = $dbi->where;
1326
$result = $dbi->select(
1327
    table => 'table1',
1328
    where => $where
1329
);
1330
$row = $result->all;
1331
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1332

            
1333
eval {
1334
$where = $dbi->where
1335
             ->clause(['uuu']);
1336
$result = $dbi->select(
1337
    table => 'table1',
1338
    where => $where
1339
);
1340
};
1341
ok($@);
1342

            
1343
$where = $dbi->where;
1344
is("$where", '');
1345

            
1346
$where = $dbi->where
1347
             ->clause(['or', ('key1 = :key1') x 2])
1348
             ->param({key1 => [1, 3]});
1349
$result = $dbi->select(
1350
    table => 'table1',
1351
    where => $where,
1352
);
1353
$row = $result->all;
1354
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1355

            
1356
$where = $dbi->where
1357
             ->clause(['or', ('key1 = :key1') x 2])
1358
             ->param({key1 => [1]});
1359
$result = $dbi->select(
1360
    table => 'table1',
1361
    where => $where,
1362
);
1363
$row = $result->all;
1364
is_deeply($row, [{key1 => 1, key2 => 2}]);
1365

            
1366
$where = $dbi->where
1367
             ->clause(['or', ('key1 = :key1') x 2])
1368
             ->param({key1 => 1});
1369
$result = $dbi->select(
1370
    table => 'table1',
1371
    where => $where,
1372
);
1373
$row = $result->all;
1374
is_deeply($row, [{key1 => 1, key2 => 2}]);
1375

            
1376
$where = $dbi->where
1377
             ->clause('key1 = :key1')
1378
             ->param({key1 => 1});
1379
$result = $dbi->select(
1380
    table => 'table1',
1381
    where => $where,
1382
);
1383
$row = $result->all;
1384
is_deeply($row, [{key1 => 1, key2 => 2}]);
1385

            
1386
$where = $dbi->where
1387
             ->clause('key1 = :key1 key2 = :key2')
1388
             ->param({key1 => 1});
1389
eval{$where->to_string};
1390
like($@, qr/one column/);
1391

            
1392
$where = $dbi->where
1393
             ->clause(['or', ('key1 = :key1') x 3])
1394
             ->param({key1 => [$dbi->not_exists, 1, 3]});
1395
$result = $dbi->select(
1396
    table => 'table1',
1397
    where => $where,
1398
);
1399
$row = $result->all;
1400
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1401

            
1402
$where = $dbi->where
1403
             ->clause(['or', ('key1 = :key1') x 3])
1404
             ->param({key1 => [1, $dbi->not_exists, 3]});
1405
$result = $dbi->select(
1406
    table => 'table1',
1407
    where => $where,
1408
);
1409
$row = $result->all;
1410
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1411

            
1412
$where = $dbi->where
1413
             ->clause(['or', ('key1 = :key1') x 3])
1414
             ->param({key1 => [1, 3, $dbi->not_exists]});
1415
$result = $dbi->select(
1416
    table => 'table1',
1417
    where => $where,
1418
);
1419
$row = $result->all;
1420
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1421

            
1422
$where = $dbi->where
1423
             ->clause(['or', ('key1 = :key1') x 3])
1424
             ->param({key1 => [1, $dbi->not_exists, $dbi->not_exists]});
1425
$result = $dbi->select(
1426
    table => 'table1',
1427
    where => $where,
1428
);
1429
$row = $result->all;
1430
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1431

            
1432
$where = $dbi->where
1433
             ->clause(['or', ('key1 = :key1') x 3])
1434
             ->param({key1 => [$dbi->not_exists, 1, $dbi->not_exists]});
1435
$result = $dbi->select(
1436
    table => 'table1',
1437
    where => $where,
1438
);
1439
$row = $result->all;
1440
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1441

            
1442
$where = $dbi->where
1443
             ->clause(['or', ('key1 = :key1') x 3])
1444
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists, 1]});
1445
$result = $dbi->select(
1446
    table => 'table1',
1447
    where => $where,
1448
);
1449
$row = $result->all;
1450
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1451

            
1452
$where = $dbi->where
1453
             ->clause(['or', ('key1 = :key1') x 3])
1454
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists, $dbi->not_exists]});
1455
$result = $dbi->select(
1456
    table => 'table1',
1457
    where => $where,
1458
);
1459
$row = $result->all;
1460
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1461

            
1462
$where = $dbi->where
1463
             ->clause(['or', ('key1 = :key1') x 3])
1464
             ->param({key1 => []});
1465
$result = $dbi->select(
1466
    table => 'table1',
1467
    where => $where,
1468
);
1469
$row = $result->all;
1470
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1471

            
1472
$where = $dbi->where
1473
             ->clause(['and', '{> key1}', '{< key1}' ])
1474
             ->param({key1 => [2, $dbi->not_exists]});
1475
$result = $dbi->select(
1476
    table => 'table1',
1477
    where => $where,
1478
);
1479
$row = $result->all;
1480
is_deeply($row, [{key1 => 3, key2 => 4}], 'not_exists');
1481

            
1482
$where = $dbi->where
1483
             ->clause(['and', '{> key1}', '{< key1}' ])
1484
             ->param({key1 => [$dbi->not_exists, 2]});
1485
$result = $dbi->select(
1486
    table => 'table1',
1487
    where => $where,
1488
);
1489
$row = $result->all;
1490
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1491

            
1492
$where = $dbi->where
1493
             ->clause(['and', '{> key1}', '{< key1}' ])
1494
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists]});
1495
$result = $dbi->select(
1496
    table => 'table1',
1497
    where => $where,
1498
);
1499
$row = $result->all;
1500
is_deeply($row, [{key1 => 1, key2 => 2},{key1 => 3, key2 => 4}], 'not_exists');
1501

            
1502
$where = $dbi->where
1503
             ->clause(['and', '{> key1}', '{< key1}' ])
1504
             ->param({key1 => [0, 2]});
1505
$result = $dbi->select(
1506
    table => 'table1',
1507
    where => $where,
1508
);
1509
$row = $result->all;
1510
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1511

            
1512
$where = $dbi->where
1513
             ->clause(['and', 'key1 is not null', 'key2 is not null' ]);
1514
$result = $dbi->select(
1515
    table => 'table1',
1516
    where => $where,
1517
);
1518
$row = $result->all;
1519
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1520

            
1521
eval {$dbi->where(ppp => 1) };
1522
like($@, qr/invalid/);
1523

            
1524
$where = $dbi->where(
1525
    clause => ['and', ['or'], ['and', 'key1 = :key1', 'key2 = :key2']],
1526
    param => {key1 => 1, key2 => 2}
1527
);
1528
$result = $dbi->select(
1529
    table => 'table1',
1530
    where => $where,
1531
);
1532
$row = $result->all;
1533
is_deeply($row, [{key1 => 1, key2 => 2}]);
1534

            
1535

            
1536
$where = $dbi->where(
1537
    clause => ['and', ['or'], ['or', ':key1', ':key2']],
1538
    param => {}
1539
);
1540
$result = $dbi->select(
1541
    table => 'table1',
1542
    where => $where,
1543
);
1544
$row = $result->all;
1545
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1546

            
1547
$where = $dbi->where;
1548
$where->clause(['and', ':key1{=}']);
1549
$where->param({key1 => undef});
1550
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1551
$row = $result->all;
1552
is_deeply($row, [{key1 => 1, key2 => 2}]);
1553

            
1554
$where = $dbi->where;
1555
$where->clause(['and', ':key1{=}']);
1556
$where->param({key1 => undef});
1557
$where->if('defined');
1558
$where->map;
1559
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1560
$row = $result->all;
1561
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1562

            
1563
$where = $dbi->where;
1564
$where->clause(['or', ':key1{=}', ':key1{=}']);
1565
$where->param({key1 => [undef, undef]});
1566
$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]});
1567
$row = $result->all;
1568
is_deeply($row, [{key1 => 1, key2 => 2}]);
1569
$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]});
1570
$row = $result->all;
1571
is_deeply($row, [{key1 => 1, key2 => 2}]);
1572

            
1573
$where = $dbi->where;
1574
$where->clause(['and', ':key1{=}']);
1575
$where->param({key1 => [undef, undef]});
1576
$where->if('defined');
1577
$where->map;
1578
$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]});
1579
$row = $result->all;
1580
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1581
$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]});
1582
$row = $result->all;
1583
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1584

            
1585
$where = $dbi->where;
1586
$where->clause(['and', ':key1{=}']);
1587
$where->param({key1 => 0});
1588
$where->if('length');
1589
$where->map;
1590
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1591
$row = $result->all;
1592
is_deeply($row, [{key1 => 1, key2 => 2}]);
1593

            
1594
$where = $dbi->where;
1595
$where->clause(['and', ':key1{=}']);
1596
$where->param({key1 => ''});
1597
$where->if('length');
1598
$where->map;
1599
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1600
$row = $result->all;
1601
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1602

            
1603
$where = $dbi->where;
1604
$where->clause(['and', ':key1{=}']);
1605
$where->param({key1 => 5});
1606
$where->if(sub { ($_[0] || '') eq 5 });
1607
$where->map;
1608
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1609
$row = $result->all;
1610
is_deeply($row, [{key1 => 1, key2 => 2}]);
1611

            
1612
$where = $dbi->where;
1613
$where->clause(['and', ':key1{=}']);
1614
$where->param({key1 => 7});
1615
$where->if(sub { ($_[0] || '') eq 5 });
1616
$where->map;
1617
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1618
$row = $result->all;
1619
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1620

            
1621
$where = $dbi->where;
1622
$where->param({id => 1, author => 'Ken', price => 1900});
1623
$where->map(id => 'book.id',
1624
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1625
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1626
);
1627
is_deeply($where->param, {'book.id' => 1, 'book.author' => '%Ken%',
1628
  'book.price' => 1900});
1629

            
1630
$where = $dbi->where;
1631
$where->param({id => 0, author => 0, price => 0});
1632
$where->map(
1633
    id => 'book.id',
1634
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1635
    price => ['book.price', sub { '%' . $_[0] . '%' },
1636
      {if => sub { $_[0] eq 0 }}]
1637
);
1638
is_deeply($where->param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
1639

            
1640
$where = $dbi->where;
1641
$where->param({id => '', author => '', price => ''});
1642
$where->if('length');
1643
$where->map(
1644
    id => 'book.id',
1645
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1646
    price => ['book.price', sub { '%' . $_[0] . '%' },
1647
      {if => sub { $_[0] eq 1 }}]
1648
);
1649
is_deeply($where->param, {});
1650

            
1651
$where = $dbi->where;
1652
$where->param({id => undef, author => undef, price => undef});
1653
$where->if('length');
1654
$where->map(
1655
    id => 'book.id',
1656
    price => ['book.price', {if => 'exists'}]
1657
);
1658
is_deeply($where->param, {'book.price' => undef});
1659

            
1660
$where = $dbi->where;
1661
$where->param({price => 'a'});
1662
$where->if('length');
1663
$where->map(
1664
    id => ['book.id', {if => 'exists'}],
1665
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
1666
);
1667
is_deeply($where->param, {'book.price' => '%a'});
1668

            
1669
$where = $dbi->where;
1670
$where->param({id => [1, 2], author => 'Ken', price => 1900});
1671
$where->map(
1672
    id => 'book.id',
1673
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1674
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1675
);
1676
is_deeply($where->param, {'book.id' => [1, 2], 'book.author' => '%Ken%',
1677
  'book.price' => 1900});
1678

            
1679
$where = $dbi->where;
1680
$where->if('length');
1681
$where->param({id => ['', ''], author => 'Ken', price => 1900});
1682
$where->map(
1683
    id => 'book.id',
1684
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1685
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1686
);
1687
is_deeply($where->param, {'book.id' => [$dbi->not_exists, $dbi->not_exists], 'book.author' => '%Ken%',
1688
  'book.price' => 1900});
1689

            
1690
$where = $dbi->where;
1691
$where->param({id => ['', ''], author => 'Ken', price => 1900});
1692
$where->map(
1693
    id => ['book.id', {if => 'length'}],
1694
    author => ['book.author', sub { '%' . $_[0] . '%' }, {if => 'defined'}],
1695
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1696
);
1697
is_deeply($where->param, {'book.id' => [$dbi->not_exists, $dbi->not_exists], 'book.author' => '%Ken%',
1698
  'book.price' => 1900});
1699

            
1700
test 'dbi_option default';
1701
$dbi = DBIx::Custom->new;
1702
is_deeply($dbi->dbi_option, {});
1703

            
1704
test 'register_tag_processor';
1705
$dbi = DBIx::Custom->connect;
1706
$dbi->register_tag_processor(
1707
    a => sub { 1 }
1708
);
1709
is($dbi->query_builder->tag_processors->{a}->(), 1);
1710

            
1711
test 'register_tag';
1712
$dbi = DBIx::Custom->connect;
1713
$dbi->register_tag(
1714
    b => sub { 2 }
1715
);
1716
is($dbi->query_builder->tags->{b}->(), 2);
1717

            
1718
test 'table not specify exception';
1719
$dbi = DBIx::Custom->connect;
1720
eval {$dbi->insert};
1721
like($@, qr/table/);
1722
eval {$dbi->update};
1723
like($@, qr/table/);
1724
eval {$dbi->delete};
1725
like($@, qr/table/);
1726
eval {$dbi->select};
1727
like($@, qr/table/);
test cleanup
Yuki Kimoto authored on 2011-08-10
1728

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1729
test 'more tests';
1730
$dbi = DBIx::Custom->connect;
1731
eval{$dbi->apply_filter('table', 'column', [])};
1732
like($@, qr/apply_filter/);
1733

            
1734
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
1735
like($@, qr/apply_filter/);
1736

            
1737
$dbi->apply_filter(
1738

            
1739
);
1740
$dbi = DBIx::Custom->connect;
1741
eval { $dbi->execute('drop table table1') };
1742
$dbi->execute($create_table1);
1743
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1744
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1745
$dbi->apply_filter('table1', 'key2', 
1746
                   {in => sub { $_[0] * 3 }, out => sub { $_[0] * 2 }});
1747
$rows = $dbi->select(table => 'table1', where => {key2 => 1})->all;
1748
is_deeply($rows, [{key1 => 1, key2 => 6}]);
1749

            
1750
$dbi = DBIx::Custom->connect;
1751
eval { $dbi->execute('drop table table1') };
1752
$dbi->execute($create_table1);
1753
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1754
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1755
$dbi->apply_filter('table1', 'key2', {});
1756
$rows = $dbi->select(table => 'table1', where => {key2 => 2})->all;
1757
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1758

            
1759
$dbi = DBIx::Custom->connect;
1760
eval {$dbi->apply_filter('table1', 'key2', {out => 'no'})};
1761
like($@, qr/not registered/);
1762
eval {$dbi->apply_filter('table1', 'key2', {in => 'no'})};
1763
like($@, qr/not registered/);
1764
$dbi->method({one => sub { 1 }});
1765
is($dbi->one, 1);
1766

            
1767
eval{DBIx::Custom->connect(dsn => undef)};
1768
like($@, qr/_connect/);
1769

            
1770
$dbi = DBIx::Custom->connect;
1771
eval { $dbi->execute('drop table table1') };
1772
$dbi->execute($create_table1);
1773
$dbi->register_filter(twice => sub { $_[0] * 2 });
1774
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
1775
             filter => {key1 => 'twice'});
1776
$row = $dbi->select(table => 'table1')->one;
1777
is_deeply($row, {key1 => 2, key2 => 2});
1778
eval {$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
1779
             filter => {key1 => 'no'}) };
1780
like($@, qr//);
1781

            
1782
$dbi->register_filter(one => sub { });
1783
$dbi->default_fetch_filter('one');
1784
ok($dbi->default_fetch_filter);
1785
$dbi->default_bind_filter('one');
1786
ok($dbi->default_bind_filter);
1787
eval{$dbi->default_fetch_filter('no')};
1788
like($@, qr/not registered/);
1789
eval{$dbi->default_bind_filter('no')};
1790
like($@, qr/not registered/);
1791
$dbi->default_bind_filter(undef);
1792
ok(!defined $dbi->default_bind_filter);
1793
$dbi->default_fetch_filter(undef);
1794
ok(!defined $dbi->default_fetch_filter);
1795
eval {$dbi->execute('select * from table1 {} {= author') };
1796
like($@, qr/Tag not finished/);
1797

            
1798
$dbi = DBIx::Custom->connect;
1799
eval { $dbi->execute('drop table table1') };
1800
$dbi->execute($create_table1);
1801
$dbi->register_filter(one => sub { 1 });
1802
$result = $dbi->select(table => 'table1');
1803
eval {$result->filter(key1 => 'no')};
1804
like($@, qr/not registered/);
1805
eval {$result->end_filter(key1 => 'no')};
1806
like($@, qr/not registered/);
1807
$result->default_filter(undef);
1808
ok(!defined $result->default_filter);
1809
$result->default_filter('one');
1810
is($result->default_filter->(), 1);
1811

            
1812
test 'dbi_option';
1813
$dbi = DBIx::Custom->connect(dbi_option => {PrintError => 1});
1814
ok($dbi->dbh->{PrintError});
1815
$dbi = DBIx::Custom->connect(dbi_options => {PrintError => 1});
1816
ok($dbi->dbh->{PrintError});
1817

            
1818
test 'DBIx::Custom::Result stash()';
1819
$result = DBIx::Custom::Result->new;
1820
is_deeply($result->stash, {}, 'default');
1821
$result->stash->{foo} = 1;
1822
is($result->stash->{foo}, 1, 'get and set');
test cleanup
Yuki Kimoto authored on 2011-08-10
1823

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1824
test 'delete_at';
1825
$dbi = DBIx::Custom->connect;
1826
eval { $dbi->execute('drop table table1') };
1827
$dbi->execute($create_table1_2);
1828
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1829
$dbi->delete_at(
1830
    table => 'table1',
1831
    primary_key => ['key1', 'key2'],
1832
    where => [1, 2],
1833
);
1834
is_deeply($dbi->select(table => 'table1')->all, []);
1835

            
1836
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1837
$dbi->delete_at(
1838
    table => 'table1',
1839
    primary_key => 'key1',
1840
    where => 1,
1841
);
1842
is_deeply($dbi->select(table => 'table1')->all, []);
1843

            
1844
test 'insert_at';
1845
$dbi = DBIx::Custom->connect;
1846
eval { $dbi->execute('drop table table1') };
1847
$dbi->execute($create_table1_2);
1848
$dbi->insert_at(
1849
    primary_key => ['key1', 'key2'], 
1850
    table => 'table1',
1851
    where => [1, 2],
1852
    param => {key3 => 3}
1853
);
1854
is($dbi->select(table => 'table1')->one->{key1}, 1);
1855
is($dbi->select(table => 'table1')->one->{key2}, 2);
1856
is($dbi->select(table => 'table1')->one->{key3}, 3);
1857

            
1858
$dbi->delete_all(table => 'table1');
1859
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1860
$dbi->insert_at(
1861
    primary_key => 'key1', 
1862
    table => 'table1',
1863
    where => 1,
1864
    param => {key2 => 2, key3 => 3}
1865
);
1866

            
1867
is($dbi->select(table => 'table1')->one->{key1}, 1);
1868
is($dbi->select(table => 'table1')->one->{key2}, 2);
1869
is($dbi->select(table => 'table1')->one->{key3}, 3);
1870

            
1871
eval {
1872
    $dbi->insert_at(
1873
        table => 'table1',
1874
        primary_key => ['key1', 'key2'],
1875
        where => {},
1876
        param => {key1 => 1, key2 => 2, key3 => 3},
1877
    );
1878
};
1879
like($@, qr/must be/);
1880

            
1881
$dbi = DBIx::Custom->connect;
1882
eval { $dbi->execute('drop table table1') };
1883
$dbi->execute($create_table1_2);
1884
$dbi->insert_at(
1885
    {key3 => 3},
1886
    primary_key => ['key1', 'key2'], 
1887
    table => 'table1',
1888
    where => [1, 2],
1889
);
1890
is($dbi->select(table => 'table1')->one->{key1}, 1);
1891
is($dbi->select(table => 'table1')->one->{key2}, 2);
1892
is($dbi->select(table => 'table1')->one->{key3}, 3);
1893

            
1894
test 'update_at';
1895
$dbi = DBIx::Custom->connect;
1896
eval { $dbi->execute('drop table table1') };
1897
$dbi->execute($create_table1_2);
1898
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1899
$dbi->update_at(
1900
    table => 'table1',
1901
    primary_key => ['key1', 'key2'],
1902
    where => [1, 2],
1903
    param => {key3 => 4}
1904
);
1905
is($dbi->select(table => 'table1')->one->{key1}, 1);
1906
is($dbi->select(table => 'table1')->one->{key2}, 2);
1907
is($dbi->select(table => 'table1')->one->{key3}, 4);
1908

            
1909
$dbi->delete_all(table => 'table1');
1910
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1911
$dbi->update_at(
1912
    table => 'table1',
1913
    primary_key => 'key1',
1914
    where => 1,
1915
    param => {key3 => 4}
1916
);
1917
is($dbi->select(table => 'table1')->one->{key1}, 1);
1918
is($dbi->select(table => 'table1')->one->{key2}, 2);
1919
is($dbi->select(table => 'table1')->one->{key3}, 4);
1920

            
1921
$dbi = DBIx::Custom->connect;
1922
eval { $dbi->execute('drop table table1') };
1923
$dbi->execute($create_table1_2);
1924
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1925
$dbi->update_at(
1926
    {key3 => 4},
1927
    table => 'table1',
1928
    primary_key => ['key1', 'key2'],
1929
    where => [1, 2]
1930
);
1931
is($dbi->select(table => 'table1')->one->{key1}, 1);
1932
is($dbi->select(table => 'table1')->one->{key2}, 2);
1933
is($dbi->select(table => 'table1')->one->{key3}, 4);
1934

            
1935
test 'select_at';
1936
$dbi = DBIx::Custom->connect;
1937
eval { $dbi->execute('drop table table1') };
1938
$dbi->execute($create_table1_2);
1939
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1940
$result = $dbi->select_at(
1941
    table => 'table1',
1942
    primary_key => ['key1', 'key2'],
1943
    where => [1, 2]
1944
);
1945
$row = $result->one;
1946
is($row->{key1}, 1);
1947
is($row->{key2}, 2);
1948
is($row->{key3}, 3);
1949

            
1950
$dbi->delete_all(table => 'table1');
1951
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1952
$result = $dbi->select_at(
1953
    table => 'table1',
1954
    primary_key => 'key1',
1955
    where => 1,
1956
);
1957
$row = $result->one;
1958
is($row->{key1}, 1);
1959
is($row->{key2}, 2);
1960
is($row->{key3}, 3);
1961

            
1962
$dbi->delete_all(table => 'table1');
1963
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1964
$result = $dbi->select_at(
1965
    table => 'table1',
1966
    primary_key => ['key1', 'key2'],
1967
    where => [1, 2]
1968
);
1969
$row = $result->one;
1970
is($row->{key1}, 1);
1971
is($row->{key2}, 2);
1972
is($row->{key3}, 3);
1973

            
1974
eval {
1975
    $result = $dbi->select_at(
1976
        table => 'table1',
1977
        primary_key => ['key1', 'key2'],
1978
        where => {},
1979
    );
1980
};
1981
like($@, qr/must be/);
1982

            
1983
eval {
1984
    $result = $dbi->select_at(
1985
        table => 'table1',
1986
        primary_key => ['key1', 'key2'],
1987
        where => [1],
1988
    );
1989
};
1990
like($@, qr/same/);
1991

            
1992
eval {
1993
    $result = $dbi->update_at(
1994
        table => 'table1',
1995
        primary_key => ['key1', 'key2'],
1996
        where => {},
1997
        param => {key1 => 1, key2 => 2},
1998
    );
1999
};
2000
like($@, qr/must be/);
2001

            
2002
eval {
2003
    $result = $dbi->delete_at(
2004
        table => 'table1',
2005
        primary_key => ['key1', 'key2'],
2006
        where => {},
2007
    );
2008
};
2009
like($@, qr/must be/);
2010

            
2011
test 'columns';
2012
use MyDBI1;
2013
$dbi = MyDBI1->connect;
2014
$model = $dbi->model('book');
2015

            
2016

            
2017
test 'model delete_at';
2018
$dbi = MyDBI6->connect;
2019
eval { $dbi->execute('drop table table1') };
2020
eval { $dbi->execute('drop table table2') };
2021
eval { $dbi->execute('drop table table3') };
2022
$dbi->execute($create_table1_2);
2023
$dbi->execute($create_table2_2);
2024
$dbi->execute($create_table3);
2025
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2026
$dbi->model('table1')->delete_at(where => [1, 2]);
2027
is_deeply($dbi->select(table => 'table1')->all, []);
2028
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
2029
$dbi->model('table1_1')->delete_at(where => [1, 2]);
2030
is_deeply($dbi->select(table => 'table1')->all, []);
2031
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
2032
$dbi->model('table1_3')->delete_at(where => [1, 2]);
2033
is_deeply($dbi->select(table => 'table1')->all, []);
2034

            
2035
test 'model insert_at';
2036
$dbi = MyDBI6->connect;
2037
eval { $dbi->execute('drop table table1') };
2038
$dbi->execute($create_table1_2);
2039
$dbi->model('table1')->insert_at(
2040
    where => [1, 2],
2041
    param => {key3 => 3}
2042
);
2043
$result = $dbi->model('table1')->select;
2044
$row = $result->one;
2045
is($row->{key1}, 1);
2046
is($row->{key2}, 2);
2047
is($row->{key3}, 3);
2048

            
2049
test 'model update_at';
2050
$dbi = MyDBI6->connect;
2051
eval { $dbi->execute('drop table table1') };
2052
$dbi->execute($create_table1_2);
2053
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2054
$dbi->model('table1')->update_at(
2055
    where => [1, 2],
2056
    param => {key3 => 4}
2057
);
2058
$result = $dbi->model('table1')->select;
2059
$row = $result->one;
2060
is($row->{key1}, 1);
2061
is($row->{key2}, 2);
2062
is($row->{key3}, 4);
2063

            
2064
test 'model select_at';
2065
$dbi = MyDBI6->connect;
2066
eval { $dbi->execute('drop table table1') };
2067
$dbi->execute($create_table1_2);
2068
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2069
$result = $dbi->model('table1')->select_at(where => [1, 2]);
2070
$row = $result->one;
2071
is($row->{key1}, 1);
2072
is($row->{key2}, 2);
2073
is($row->{key3}, 3);
2074

            
2075

            
2076
test 'mycolumn and column';
2077
$dbi = MyDBI7->connect;
2078
eval { $dbi->execute('drop table table1') };
2079
eval { $dbi->execute('drop table table2') };
2080
$dbi->execute($create_table1);
2081
$dbi->execute($create_table2);
2082
$dbi->separator('__');
2083
$dbi->setup_model;
2084
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2085
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2086
$model = $dbi->model('table1');
2087
$result = $model->select(
2088
    column => [$model->mycolumn, $model->column('table2')],
2089
    where => {'table1.key1' => 1}
2090
);
2091
is_deeply($result->one,
2092
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2093

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2094
test 'insert_param';
2095
$dbi = DBIx::Custom->connect;
2096
eval { $dbi->execute('drop table table1') };
2097
$dbi->execute($create_table1_2);
2098
$param = {key1 => 1, key2 => 2};
2099
$insert_param = $dbi->insert_param($param);
2100
$sql = <<"EOS";
2101
insert into table1 $insert_param
2102
EOS
2103
$dbi->execute($sql, param => $param, table => 'table1');
2104
is($dbi->select(table => 'table1')->one->{key1}, 1);
2105
is($dbi->select(table => 'table1')->one->{key2}, 2);
2106

            
2107
$dbi = DBIx::Custom->connect;
2108
eval { $dbi->execute('drop table table1') };
2109
$dbi->execute($create_table1_2);
2110
$param = {key1 => 1, key2 => 2};
2111
$insert_param = $dbi->insert_param($param);
2112
$sql = <<"EOS";
2113
insert into table1 $insert_param
2114
EOS
2115
$dbi->execute($sql, param => $param, table => 'table1');
2116
is($dbi->select(table => 'table1')->one->{key1}, 1);
2117
is($dbi->select(table => 'table1')->one->{key2}, 2);
2118

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2122
test 'mycolumn';
2123
$dbi = MyDBI8->connect;
2124
eval { $dbi->execute('drop table table1') };
2125
eval { $dbi->execute('drop table table2') };
2126
$dbi->execute($create_table1);
2127
$dbi->execute($create_table2);
2128
$dbi->setup_model;
2129
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2130
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2131
$model = $dbi->model('table1');
2132
$result = $model->select_at(
2133
    column => [
2134
        $model->mycolumn,
2135
        $model->column('table2')
2136
    ]
2137
);
2138
is_deeply($result->one,
2139
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
2140

            
2141
$result = $model->select_at(
2142
    column => [
2143
        $model->mycolumn(['key1']),
2144
        $model->column(table2 => ['key1'])
2145
    ]
2146
);
2147
is_deeply($result->one,
2148
          {key1 => 1, 'table2.key1' => 1});
2149
$result = $model->select_at(
2150
    column => [
2151
        $model->mycolumn(['key1']),
2152
        {table2 => ['key1']}
2153
    ]
2154
);
2155
is_deeply($result->one,
2156
          {key1 => 1, 'table2.key1' => 1});
2157

            
2158
$result = $model->select_at(
2159
    column => [
2160
        $model->mycolumn(['key1']),
2161
        ['table2.key1', as => 'table2.key1']
2162
    ]
2163
);
2164
is_deeply($result->one,
2165
          {key1 => 1, 'table2.key1' => 1});
2166

            
2167
$result = $model->select_at(
2168
    column => [
2169
        $model->mycolumn(['key1']),
2170
        ['table2.key1' => 'table2.key1']
2171
    ]
2172
);
2173
is_deeply($result->one,
2174
          {key1 => 1, 'table2.key1' => 1});
2175

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2176
test 'merge_param';
2177
$dbi = DBIx::Custom->new;
2178
$params = [
2179
    {key1 => 1, key2 => 2, key3 => 3},
2180
    {key1 => 1, key2 => 2},
2181
    {key1 => 1}
2182
];
2183
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]);
2184
is_deeply($param, {key1 => [1, 1, 1], key2 => [2, 2], key3 => 3});
2185

            
2186
$params = [
2187
    {key1 => [1, 2], key2 => 1, key3 => [1, 2]},
2188
    {key1 => [3, 4], key2 => [2, 3], key3 => 3}
2189
];
2190
$param = $dbi->merge_param($params->[0], $params->[1]);
2191
is_deeply($param, {key1 => [1, 2, 3, 4], key2 => [1, 2, 3], key3 => [1, 2, 3]});
2192

            
2193
test 'select() param option';
2194
$dbi = DBIx::Custom->connect;
2195
eval { $dbi->execute('drop table table1') };
2196
$dbi->execute($create_table1);
2197
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2198
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2199
eval { $dbi->execute('drop table table2') };
2200
$dbi->execute($create_table2);
2201
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2202
$dbi->insert(table => 'table2', param => {key1 => 2, key3 => 5});
2203
$rows = $dbi->select(
2204
    table => 'table1',
2205
    column => 'table1.key1 as table1_key1, key2, key3',
2206
    where   => {'table1.key2' => 3},
2207
    join  => ['inner join (select * from table2 where {= table2.key3})' . 
2208
              ' as table2 on table1.key1 = table2.key1'],
2209
    param => {'table2.key3' => 5}
2210
)->all;
2211
is_deeply($rows, [{table1_key1 => 2, key2 => 3, key3 => 5}]);
2212

            
2213
test 'select() string where';
2214
$dbi = DBIx::Custom->connect;
2215
eval { $dbi->execute('drop table table1') };
2216
$dbi->execute($create_table1);
2217
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2218
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2219
$rows = $dbi->select(
2220
    table => 'table1',
2221
    where => 'key1 = :key1 and key2 = :key2',
2222
    where_param => {key1 => 1, key2 => 2}
2223
)->all;
2224
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2225

            
2226
$dbi = DBIx::Custom->connect;
2227
eval { $dbi->execute('drop table table1') };
2228
$dbi->execute($create_table1);
2229
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2230
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2231
$rows = $dbi->select(
2232
    table => 'table1',
2233
    where => [
2234
        'key1 = :key1 and key2 = :key2',
2235
        {key1 => 1, key2 => 2}
2236
    ]
2237
)->all;
2238
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2239

            
2240
test 'delete() string where';
2241
$dbi = DBIx::Custom->connect;
2242
eval { $dbi->execute('drop table table1') };
2243
$dbi->execute($create_table1);
2244
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2245
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2246
$dbi->delete(
2247
    table => 'table1',
2248
    where => 'key1 = :key1 and key2 = :key2',
2249
    where_param => {key1 => 1, key2 => 2}
2250
);
2251
$rows = $dbi->select(table => 'table1')->all;
2252
is_deeply($rows, [{key1 => 2, key2 => 3}]);
2253

            
2254
$dbi = DBIx::Custom->connect;
2255
eval { $dbi->execute('drop table table1') };
2256
$dbi->execute($create_table1);
2257
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2258
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2259
$dbi->delete(
2260
    table => 'table1',
2261
    where => [
2262
        'key1 = :key1 and key2 = :key2',
2263
         {key1 => 1, key2 => 2}
2264
    ]
2265
);
2266
$rows = $dbi->select(table => 'table1')->all;
2267
is_deeply($rows, [{key1 => 2, key2 => 3}]);
2268

            
2269

            
2270
test 'update() string where';
2271
$dbi = DBIx::Custom->connect;
2272
eval { $dbi->execute('drop table table1') };
2273
$dbi->execute($create_table1);
2274
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2275
$dbi->update(
2276
    table => 'table1',
2277
    param => {key1 => 5},
2278
    where => 'key1 = :key1 and key2 = :key2',
2279
    where_param => {key1 => 1, key2 => 2}
2280
);
2281
$rows = $dbi->select(table => 'table1')->all;
2282
is_deeply($rows, [{key1 => 5, key2 => 2}]);
2283

            
2284
$dbi = DBIx::Custom->connect;
2285
eval { $dbi->execute('drop table table1') };
2286
$dbi->execute($create_table1);
2287
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2288
$dbi->update(
2289
    table => 'table1',
2290
    param => {key1 => 5},
2291
    where => [
2292
        'key1 = :key1 and key2 = :key2',
2293
        {key1 => 1, key2 => 2}
2294
    ]
2295
);
2296
$rows = $dbi->select(table => 'table1')->all;
2297
is_deeply($rows, [{key1 => 5, key2 => 2}]);
2298

            
2299
test 'insert id and primary_key option';
2300
$dbi = DBIx::Custom->connect;
2301
eval { $dbi->execute('drop table table1') };
2302
$dbi->execute($create_table1_2);
2303
$dbi->insert(
2304
    primary_key => ['key1', 'key2'], 
2305
    table => 'table1',
2306
    id => [1, 2],
2307
    param => {key3 => 3}
2308
);
2309
is($dbi->select(table => 'table1')->one->{key1}, 1);
2310
is($dbi->select(table => 'table1')->one->{key2}, 2);
2311
is($dbi->select(table => 'table1')->one->{key3}, 3);
2312

            
2313
$dbi->delete_all(table => 'table1');
2314
$dbi->insert(
2315
    primary_key => 'key1', 
2316
    table => 'table1',
2317
    id => 0,
2318
    param => {key2 => 2, key3 => 3}
2319
);
2320

            
2321
is($dbi->select(table => 'table1')->one->{key1}, 0);
2322
is($dbi->select(table => 'table1')->one->{key2}, 2);
2323
is($dbi->select(table => 'table1')->one->{key3}, 3);
2324

            
2325
$dbi = DBIx::Custom->connect;
2326
eval { $dbi->execute('drop table table1') };
2327
$dbi->execute($create_table1_2);
2328
$dbi->insert(
2329
    {key3 => 3},
2330
    primary_key => ['key1', 'key2'], 
2331
    table => 'table1',
2332
    id => [1, 2],
2333
);
2334
is($dbi->select(table => 'table1')->one->{key1}, 1);
2335
is($dbi->select(table => 'table1')->one->{key2}, 2);
2336
is($dbi->select(table => 'table1')->one->{key3}, 3);
test cleanup
Yuki Kimoto authored on 2011-08-10
2337

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2338
test 'model insert id and primary_key option';
2339
$dbi = MyDBI6->connect;
2340
eval { $dbi->execute('drop table table1') };
2341
$dbi->execute($create_table1_2);
2342
$dbi->model('table1')->insert(
2343
    id => [1, 2],
2344
    param => {key3 => 3}
2345
);
2346
$result = $dbi->model('table1')->select;
2347
$row = $result->one;
2348
is($row->{key1}, 1);
2349
is($row->{key2}, 2);
2350
is($row->{key3}, 3);
2351

            
2352
$dbi = MyDBI6->connect;
2353
eval { $dbi->execute('drop table table1') };
2354
$dbi->execute($create_table1_2);
2355
$dbi->model('table1')->insert(
2356
    {key3 => 3},
2357
    id => [1, 2]
2358
);
2359
$result = $dbi->model('table1')->select;
2360
$row = $result->one;
2361
is($row->{key1}, 1);
2362
is($row->{key2}, 2);
2363
is($row->{key3}, 3);
2364

            
2365
test 'update and id option';
2366
$dbi = DBIx::Custom->connect;
2367
eval { $dbi->execute('drop table table1') };
2368
$dbi->execute($create_table1_2);
2369
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2370
$dbi->update(
2371
    table => 'table1',
2372
    primary_key => ['key1', 'key2'],
2373
    id => [1, 2],
2374
    param => {key3 => 4}
2375
);
2376
is($dbi->select(table => 'table1')->one->{key1}, 1);
2377
is($dbi->select(table => 'table1')->one->{key2}, 2);
2378
is($dbi->select(table => 'table1')->one->{key3}, 4);
2379

            
2380
$dbi->delete_all(table => 'table1');
2381
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2382
$dbi->update(
2383
    table => 'table1',
2384
    primary_key => 'key1',
2385
    id => 0,
2386
    param => {key3 => 4}
2387
);
2388
is($dbi->select(table => 'table1')->one->{key1}, 0);
2389
is($dbi->select(table => 'table1')->one->{key2}, 2);
2390
is($dbi->select(table => 'table1')->one->{key3}, 4);
2391

            
2392
$dbi = DBIx::Custom->connect;
2393
eval { $dbi->execute('drop table table1') };
2394
$dbi->execute($create_table1_2);
2395
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2396
$dbi->update(
2397
    {key3 => 4},
2398
    table => 'table1',
2399
    primary_key => ['key1', 'key2'],
2400
    id => [1, 2]
2401
);
2402
is($dbi->select(table => 'table1')->one->{key1}, 1);
2403
is($dbi->select(table => 'table1')->one->{key2}, 2);
2404
is($dbi->select(table => 'table1')->one->{key3}, 4);
2405

            
2406

            
2407
test 'model update and id option';
2408
$dbi = MyDBI6->connect;
2409
eval { $dbi->execute('drop table table1') };
2410
$dbi->execute($create_table1_2);
2411
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2412
$dbi->model('table1')->update(
2413
    id => [1, 2],
2414
    param => {key3 => 4}
2415
);
2416
$result = $dbi->model('table1')->select;
2417
$row = $result->one;
2418
is($row->{key1}, 1);
2419
is($row->{key2}, 2);
2420
is($row->{key3}, 4);
2421

            
2422

            
2423
test 'delete and id option';
2424
$dbi = DBIx::Custom->connect;
2425
eval { $dbi->execute('drop table table1') };
2426
$dbi->execute($create_table1_2);
2427
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2428
$dbi->delete(
2429
    table => 'table1',
2430
    primary_key => ['key1', 'key2'],
2431
    id => [1, 2],
2432
);
2433
is_deeply($dbi->select(table => 'table1')->all, []);
2434

            
2435
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2436
$dbi->delete(
2437
    table => 'table1',
2438
    primary_key => 'key1',
2439
    id => 0,
2440
);
2441
is_deeply($dbi->select(table => 'table1')->all, []);
2442

            
2443

            
2444
test 'model delete and id option';
2445
$dbi = MyDBI6->connect;
2446
eval { $dbi->execute('drop table table1') };
2447
eval { $dbi->execute('drop table table2') };
2448
eval { $dbi->execute('drop table table3') };
2449
$dbi->execute($create_table1_2);
2450
$dbi->execute($create_table2_2);
2451
$dbi->execute($create_table3);
2452
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2453
$dbi->model('table1')->delete(id => [1, 2]);
2454
is_deeply($dbi->select(table => 'table1')->all, []);
2455
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
2456
$dbi->model('table1_1')->delete(id => [1, 2]);
2457
is_deeply($dbi->select(table => 'table1')->all, []);
2458
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
2459
$dbi->model('table1_3')->delete(id => [1, 2]);
2460
is_deeply($dbi->select(table => 'table1')->all, []);
2461

            
2462

            
2463
test 'select and id option';
2464
$dbi = DBIx::Custom->connect;
2465
eval { $dbi->execute('drop table table1') };
2466
$dbi->execute($create_table1_2);
2467
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2468
$result = $dbi->select(
2469
    table => 'table1',
2470
    primary_key => ['key1', 'key2'],
2471
    id => [1, 2]
2472
);
2473
$row = $result->one;
2474
is($row->{key1}, 1);
2475
is($row->{key2}, 2);
2476
is($row->{key3}, 3);
2477

            
2478
$dbi->delete_all(table => 'table1');
2479
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2480
$result = $dbi->select(
2481
    table => 'table1',
2482
    primary_key => 'key1',
2483
    id => 0,
2484
);
2485
$row = $result->one;
2486
is($row->{key1}, 0);
2487
is($row->{key2}, 2);
2488
is($row->{key3}, 3);
2489

            
2490
$dbi->delete_all(table => 'table1');
2491
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2492
$result = $dbi->select(
2493
    table => 'table1',
2494
    primary_key => ['key1', 'key2'],
2495
    id => [1, 2]
2496
);
2497
$row = $result->one;
2498
is($row->{key1}, 1);
2499
is($row->{key2}, 2);
2500
is($row->{key3}, 3);
2501

            
2502

            
2503
test 'model select_at';
2504
$dbi = MyDBI6->connect;
2505
eval { $dbi->execute('drop table table1') };
2506
$dbi->execute($create_table1_2);
2507
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2508
$result = $dbi->model('table1')->select(id => [1, 2]);
2509
$row = $result->one;
2510
is($row->{key1}, 1);
2511
is($row->{key2}, 2);
2512
is($row->{key3}, 3);
2513

            
2514
test 'column separator is default .';
2515
$dbi = MyDBI7->connect;
2516
eval { $dbi->execute('drop table table1') };
2517
eval { $dbi->execute('drop table table2') };
2518
$dbi->execute($create_table1);
2519
$dbi->execute($create_table2);
2520
$dbi->setup_model;
2521
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2522
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2523
$model = $dbi->model('table1');
2524
$result = $model->select(
2525
    column => [$model->column('table2')],
2526
    where => {'table1.key1' => 1}
2527
);
2528
is_deeply($result->one,
2529
          {'table2.key1' => 1, 'table2.key3' => 3});
2530

            
2531
$result = $model->select(
2532
    column => [$model->column('table2' => [qw/key1 key3/])],
2533
    where => {'table1.key1' => 1}
2534
);
2535
is_deeply($result->one,
2536
          {'table2.key1' => 1, 'table2.key3' => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2537

            
2538

            
2539

            
2540

            
2541

            
2542

            
2543

            
2544

            
2545

            
2546

            
2547

            
2548

            
2549

            
2550

            
2551

            
2552

            
2553

            
2554

            
2555

            
2556

            
2557

            
2558

            
2559

            
2560

            
2561

            
2562

            
2563

            
2564

            
2565

            
2566

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