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

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

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

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

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

            
31
    use strict;
32
    use warnings;
33

            
34
    use base 'DBIx::Custom';
35

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

            
47
    package MyModel2::Base1;
48

            
49
    use strict;
50
    use warnings;
51

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

            
54
    package MyModel2::book;
55

            
56
    use strict;
57
    use warnings;
58

            
59
    use base 'MyModel2::Base1';
60

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

            
67
    sub list { shift->select; }
68

            
69
    package MyModel2::Company;
70

            
71
    use strict;
72
    use warnings;
73

            
74
    use base 'MyModel2::Base1';
75

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

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

            
87
    use strict;
88
    use warnings;
89

            
90
    use base 'DBIx::Custom';
91

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

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

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

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

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

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

            
199

            
200

            
201

            
202

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

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

            
205

            
cleanup test
Yuki Kimoto authored on 2011-08-10
206
test 'order';
test cleanup
Yuki Kimoto authored on 2011-08-10
207
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
208
eval { $dbi->execute('drop table table1') };
209
$dbi->execute("create table table1 (key1, key2)");
210
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
211
$dbi->insert({key1 => 1, key2 => 3}, table => 'table1');
212
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
213
$dbi->insert({key1 => 2, key2 => 4}, table => 'table1');
214
my $order = $dbi->order;
215
$order->prepend('key1', 'key2 desc');
216
$result = $dbi->select(table => 'table1', append => "$order");
217
is_deeply($result->all, [{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1},
218
  {key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}]);
219
$order->prepend('key1 desc');
220
$result = $dbi->select(table => 'table1', append => "$order");
221
is_deeply($result->all, [{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2},
222
  {key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
223

            
cleanup test
Yuki Kimoto authored on 2011-08-10
224
$order = $dbi->order;
225
$order->prepend(['table1-key1'], [qw/table1-key2 desc/]);
226
$result = $dbi->select(table => 'table1',
227
  column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']],
228
  append => "$order");
229
is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3},
230
  {'table1-key1' => 1, 'table1-key2' => 1},
231
  {'table1-key1' => 2, 'table1-key2' => 4},
232
  {'table1-key1' => 2, 'table1-key2' => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
233

            
cleanup test
Yuki Kimoto authored on 2011-08-10
234
test 'tag_parse';
test cleanup
Yuki Kimoto authored on 2011-08-10
235
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
236
$dbi->tag_parse(0);
237
eval { $dbi->execute('drop table table1') };
238
$dbi->execute("create table table1 (key1, key2)");
239
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
240
eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})};
241
ok($@);
cleanup test
Yuki Kimoto authored on 2011-08-06
242

            
cleanup test
Yuki Kimoto authored on 2011-08-10
243
test 'last_sql';
test cleanup
Yuki Kimoto authored on 2011-08-10
244
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
245
eval { $dbi->execute('drop table table1') };
246
$dbi->execute("create table table1 (key1, key2)");
247
$dbi->execute('select * from table1');
248
is($dbi->last_sql, 'select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
249

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
253
test 'DBIx::Custom header';
test cleanup
Yuki Kimoto authored on 2011-08-10
254
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
255
eval { $dbi->execute('drop table table1') };
256
$dbi->execute("create table table1 (key1, key2)");
257
$result = $dbi->execute('select key1 as h1, key2 as h2 from table1');
258
is_deeply($result->header, [qw/h1 h2/]);
cleanup test
Yuki Kimoto authored on 2011-08-06
259

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

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
281
$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}";
282
$result = $dbi->execute(
283
    $source,
284
    param => {'table1.key1' => 1, 'table1.key2' => 1},
285
    filter => {'table1.key2' => sub { $_[0] * 2 }}
cleanup test
Yuki Kimoto authored on 2011-08-06
286
);
cleanup test
Yuki Kimoto authored on 2011-08-10
287
$rows = $result->all;
288
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
289

            
cleanup test
Yuki Kimoto authored on 2011-08-10
290
test 'high perfomance way';
291
$dbi->execute('drop table table1');
292
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
293
$rows = [
294
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
295
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
296
];
297
{
298
    my $query;
299
    foreach my $row (@$rows) {
300
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
301
      $dbi->execute($query, $row, filter => {ab => sub { $_[0] * 2 }});
cleanup test
Yuki Kimoto authored on 2011-08-06
302
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
303
    is_deeply($dbi->select(table => 'table1')->all,
304
      [
305
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
306
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
307
      ]
308
    );
309
}
310

            
311
$dbi->execute('drop table table1');
312
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
313
$rows = [
314
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
315
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
316
];
317
{
318
    my $query;
319
    my $sth;
320
    foreach my $row (@$rows) {
321
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
322
      $sth ||= $query->sth;
323
      $sth->execute(map { $row->{$_} } sort keys %$row);
cleanup test
Yuki Kimoto authored on 2011-08-06
324
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
325
    is_deeply($dbi->select(table => 'table1')->all,
326
      [
327
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
328
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
329
      ]
330
    );
331
}
cleanup test
Yuki Kimoto authored on 2011-08-06
332

            
cleanup test
Yuki Kimoto authored on 2011-08-10
333
test 'result';
test cleanup
Yuki Kimoto authored on 2011-08-10
334
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
335
eval { $dbi->execute('drop table table1') };
336
$dbi->execute($create_table1);
337
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
338
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
339

            
cleanup test
Yuki Kimoto authored on 2011-08-06
340
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
341
@rows = ();
342
while (my $row = $result->fetch) {
343
    push @rows, [@$row];
344
}
345
is_deeply(\@rows, [[1, 2], [3, 4]]);
cleanup test
Yuki Kimoto authored on 2011-08-06
346

            
347
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
348
@rows = ();
349
while (my $row = $result->fetch_hash) {
350
    push @rows, {%$row};
351
}
352
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
353

            
354
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
355
$row = $result->fetch_first;
356
is_deeply($row, [1, 2], "row");
357
$row = $result->fetch;
358
ok(!$row, "finished");
359

            
cleanup test
Yuki Kimoto authored on 2011-08-06
360
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
361
$row = $result->fetch_hash_first;
362
is_deeply($row, {key1 => 1, key2 => 2}, "row");
363
$row = $result->fetch_hash;
364
ok(!$row, "finished");
365

            
366
$dbi->execute('create table table2 (key1, key2);');
367
$result = $dbi->select(table => 'table2');
368
$row = $result->fetch_hash_first;
369
ok(!$row, "no row fetch");
cleanup test
Yuki Kimoto authored on 2011-08-06
370

            
test cleanup
Yuki Kimoto authored on 2011-08-10
371
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
372
eval { $dbi->execute('drop table table1') };
373
$dbi->execute($create_table1);
374
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
375
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
376
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1');
377
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1');
378
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
379
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
380
$rows = $result->fetch_multi(2);
381
is_deeply($rows, [[1, 2],
382
                  [3, 4]], "fetch_multi first");
383
$rows = $result->fetch_multi(2);
384
is_deeply($rows, [[5, 6],
385
                  [7, 8]], "fetch_multi secound");
386
$rows = $result->fetch_multi(2);
387
is_deeply($rows, [[9, 10]], "fetch_multi third");
388
$rows = $result->fetch_multi(2);
389
ok(!$rows);
390

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

            
395
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
396
$rows = $result->fetch_hash_multi(2);
397
is_deeply($rows, [{key1 => 1, key2 => 2},
398
                  {key1 => 3, key2 => 4}], "fetch_multi first");
399
$rows = $result->fetch_hash_multi(2);
400
is_deeply($rows, [{key1 => 5, key2 => 6},
401
                  {key1 => 7, key2 => 8}], "fetch_multi secound");
402
$rows = $result->fetch_hash_multi(2);
403
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third");
404
$rows = $result->fetch_hash_multi(2);
405
ok(!$rows);
406

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
411
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
412
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
413
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
414
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
415
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
416

            
cleanup test
Yuki Kimoto authored on 2011-08-10
417
test 'fetch_all';
418
$result = $dbi->select(table => 'table1');
419
$rows = $result->fetch_all;
420
is_deeply($rows, [[1, 2], [3, 4]]);
cleanup test
Yuki Kimoto authored on 2011-08-06
421

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
433
$result = $dbi->select(table => 'table1');
434
$result->dbi->filters({three_times => sub { $_[0] * 3}});
435
$result->filter({key1 => 'three_times'});
436
$rows = $result->fetch_hash_all;
437
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash");
438

            
439
test "query_builder";
440
$datas = [
441
    # Basic tests
442
    {   name            => 'placeholder basic',
443
        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
444
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;",
445
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
446
    },
447
    {
448
        name            => 'placeholder in',
449
        source            => "{in k1 3};",
450
        sql_expected    => "k1 in (?, ?, ?);",
451
        columns_expected   => [qw/k1 k1 k1/]
452
    },
453
    
454
    # Table name
455
    {
456
        name            => 'placeholder with table name',
457
        source            => "{= a.k1} {= a.k2}",
458
        sql_expected    => "a.k1 = ? a.k2 = ?;",
459
        columns_expected  => [qw/a.k1 a.k2/]
460
    },
461
    {   
462
        name            => 'placeholder in with table name',
463
        source            => "{in a.k1 2} {in b.k2 2}",
464
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?);",
465
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
466
    },
467
    {
468
        name            => 'not contain tag',
469
        source            => "aaa",
470
        sql_expected    => "aaa;",
471
        columns_expected  => [],
472
    }
473
];
474

            
475
for (my $i = 0; $i < @$datas; $i++) {
476
    my $data = $datas->[$i];
477
    my $builder = DBIx::Custom->new->query_builder;
478
    my $query = $builder->build_query($data->{source});
479
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
480
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
481
}
482

            
483
$builder = DBIx::Custom->new->query_builder;
484
$ret_val = $builder->register_tag(
485
    p => sub {
486
        my @args = @_;
487
        
488
        my $expand    = "? $args[0] $args[1]";
489
        my $columns = [2];
490
        return [$expand, $columns];
491
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
492
);
493

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

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
507
$builder->register_tag({
508
    q => 'string'
509
});
cleanup test
Yuki Kimoto authored on 2011-08-06
510

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
514
$builder->register_tag({
515
   r => sub {} 
516
});
cleanup test
Yuki Kimoto authored on 2011-08-06
517

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

            
521
$builder->register_tag({
522
   s => sub { return ["a", ""]} 
523
});
524

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

            
528
$builder->register_tag(
529
    t => sub {return ["a", []]}
cleanup test
Yuki Kimoto authored on 2011-08-06
530
);
531

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

            
533
test 'General error case';
534
$builder = DBIx::Custom->new->query_builder;
535
$builder->register_tag(
536
    a => sub {
537
        return ["? ? ?", ['']];
538
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
539
);
cleanup test
Yuki Kimoto authored on 2011-08-10
540
eval{$builder->build_query("{a}")};
541
like($@, qr/\QPlaceholder count/, "placeholder count is invalid");
cleanup test
Yuki Kimoto authored on 2011-08-06
542

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

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

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

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

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

            
559
test 'variouse source';
560
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;";
561
$query = $builder->build_query($source);
562
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1");
563

            
564
$source = "abc;";
565
$query = $builder->build_query($source);
566
is($query->sql, 'abc;', "basic : 2");
567

            
568
$source = "{= a}";
569
$query = $builder->build_query($source);
570
is($query->sql, 'a = ?;', "only tag");
571

            
572
$source = "000;";
573
$query = $builder->build_query($source);
574
is($query->sql, '000;', "contain 0 value");
575

            
576
$source = "a {= b} }";
577
eval{$builder->build_query($source)};
578
like($@, qr/unexpected "}"/, "error : 1");
579

            
580
$source = "a {= {}";
581
eval{$builder->build_query($source)};
582
like($@, qr/unexpected "{"/, "error : 2");
583

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

            
585

            
586

            
587

            
588

            
589

            
590

            
591

            
592

            
593

            
594

            
595

            
596

            
597

            
598

            
599

            
600

            
601

            
602

            
603

            
604

            
605

            
606

            
607

            
608

            
609

            
610
### a little complex test
cleanup test
Yuki Kimoto authored on 2011-08-10
611
test 'table_alias';
612
$dbi = DBIx::Custom->connect;
613
eval { $dbi->execute('drop table table1') };
614
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
615
$dbi->type_rule(
616
    into1 => {
617
        date => sub { uc $_[0] }
618
    }
619
);
620
$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'},
621
  table_alias => {table2 => 'table1'});
622
$result = $dbi->select(table => 'table1');
623
is($result->one->{key1}, 'A');
624

            
cleanup test
Yuki Kimoto authored on 2011-08-10
625
test 'select() wrap option';
626
$dbi = DBIx::Custom->connect;
627
eval { $dbi->execute('drop table table1') };
628
$dbi->execute($create_table1);
629
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
630
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
631
$rows = $dbi->select(
632
    table => 'table1',
633
    column => 'key1',
634
    wrap => ['select * from (', ') as t where key1 = 1']
635
)->all;
636
is_deeply($rows, [{key1 => 1}]);
637

            
638
eval {
639
$dbi->select(
640
    table => 'table1',
641
    column => 'key1',
642
    wrap => 'select * from ('
643
)
644
};
645
like($@, qr/array/);
646

            
cleanup test
Yuki Kimoto authored on 2011-08-10
647
test 'dbi method from model';
648
$dbi = MyDBI9->connect;
649
eval { $dbi->execute('drop table table1') };
650
$dbi->execute($create_table1);
651
$dbi->setup_model;
652
$model = $dbi->model('table1');
653
eval{$model->execute('select * from table1')};
654
ok(!$@);
655

            
656
test 'column table option';
657
$dbi = MyDBI9->connect;
658
eval { $dbi->execute('drop table table1') };
659
$dbi->execute($create_table1);
660
eval { $dbi->execute('drop table table2') };
661
$dbi->execute($create_table2);
662
$dbi->setup_model;
663
$dbi->execute('insert into table1 (key1, key2) values (1, 2);');
664
$dbi->execute('insert into table2 (key1, key3) values (1, 4);');
665
$model = $dbi->model('table1');
666
$result = $model->select(
667
    column => [
668
        $model->column('table2', {alias => 'table2_alias'})
669
    ],
670
    where => {'table2_alias.key3' => 4}
671
);
672
is_deeply($result->one, 
673
          {'table2_alias.key1' => 1, 'table2_alias.key3' => 4});
674

            
675
$dbi->separator('__');
676
$result = $model->select(
677
    column => [
678
        $model->column('table2', {alias => 'table2_alias'})
679
    ],
680
    where => {'table2_alias.key3' => 4}
681
);
682
is_deeply($result->one, 
683
          {'table2_alias__key1' => 1, 'table2_alias__key3' => 4});
684

            
685
$dbi->separator('-');
686
$result = $model->select(
687
    column => [
688
        $model->column('table2', {alias => 'table2_alias'})
689
    ],
690
    where => {'table2_alias.key3' => 4}
691
);
692
is_deeply($result->one, 
693
          {'table2_alias-key1' => 1, 'table2_alias-key3' => 4});
694

            
695
test 'create_model';
696
$dbi = DBIx::Custom->connect;
697
eval { $dbi->execute('drop table table1') };
698
eval { $dbi->execute('drop table table2') };
699
$dbi->execute($create_table1);
700
$dbi->execute($create_table2);
701

            
702
$dbi->create_model(
703
    table => 'table1',
704
    join => [
705
       'left outer join table2 on table1.key1 = table2.key1'
706
    ],
707
    primary_key => ['key1']
708
);
709
$model2 = $dbi->create_model(
710
    table => 'table2'
711
);
712
$dbi->create_model(
713
    table => 'table3',
714
    filter => [
715
        key1 => {in => sub { uc $_[0] }}
716
    ]
717
);
718
$dbi->setup_model;
719
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
720
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
721
$model = $dbi->model('table1');
722
$result = $model->select(
723
    column => [$model->mycolumn, $model->column('table2')],
724
    where => {'table1.key1' => 1}
725
);
726
is_deeply($result->one,
727
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
728
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
729

            
730
test 'model method';
731
$dbi = DBIx::Custom->connect;
732
eval { $dbi->execute('drop table table2') };
733
$dbi->execute($create_table2);
734
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
735
$model = $dbi->create_model(
736
    table => 'table2'
737
);
738
$model->method(foo => sub { shift->select(@_) });
739
is_deeply($model->foo->one, {key1 => 1, key3 => 3});
740

            
cleanup test
Yuki Kimoto authored on 2011-08-10
741
test 'join';
742
$dbi = DBIx::Custom->connect;
743
eval { $dbi->execute('drop table table1') };
744
$dbi->execute($create_table1);
745
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
746
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
747
$dbi->execute($create_table2);
748
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
749
$dbi->execute('create table table3 (key3 int, key4 int);');
750
$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4});
751
$rows = $dbi->select(
752
    table => 'table1',
753
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
754
    where   => {'table1.key2' => 2},
755
    join  => ['left outer join table2 on table1.key1 = table2.key1']
756
)->all;
757
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]);
758

            
759
$rows = $dbi->select(
760
    table => 'table1',
761
    where   => {'key1' => 1},
762
    join  => ['left outer join table2 on table1.key1 = table2.key1']
763
)->all;
764
is_deeply($rows, [{key1 => 1, key2 => 2}]);
765

            
766
eval {
767
    $rows = $dbi->select(
768
        table => 'table1',
769
        column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
770
        where   => {'table1.key2' => 2},
771
        join  => {'table1.key1' => 'table2.key1'}
772
    );
773
};
774
like ($@, qr/array/);
775

            
776
$rows = $dbi->select(
777
    table => 'table1',
778
    where   => {'key1' => 1},
779
    join  => ['left outer join table2 on table1.key1 = table2.key1',
780
              'left outer join table3 on table2.key3 = table3.key3']
781
)->all;
782
is_deeply($rows, [{key1 => 1, key2 => 2}]);
783

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

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

            
802
$dbi = DBIx::Custom->connect;
803
$dbi->quote('"');
804
eval { $dbi->execute('drop table table1') };
805
$dbi->execute($create_table1);
806
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
807
$dbi->execute($create_table2);
808
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
809
$rows = $dbi->select(
810
    table => 'table1',
811
    column => '"table1"."key1" as "table1_key1", "table2"."key1" as "table2_key1", "key2", "key3"',
812
    where   => {'table1.key2' => 2},
813
    join  => ['left outer join "table2" on "table1"."key1" = "table2"."key1"'],
814
)->all;
815
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}],
816
          'quote');
817

            
818

            
819
$dbi = DBIx::Custom->connect;
820
eval { $dbi->execute('drop table table1') };
821
$dbi->execute($create_table1);
822
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
823
$sql = <<"EOS";
824
left outer join (
825
  select * from table1 as t1
826
  where t1.key2 = (
827
    select max(t2.key2) from table1 as t2
828
    where t1.key1 = t2.key1
829
  )
830
) as latest_table1 on table1.key1 = latest_table1.key1
831
EOS
832
$join = [$sql];
833
$rows = $dbi->select(
834
    table => 'table1',
835
    column => 'latest_table1.key1 as latest_table1__key1',
836
    join  => $join
837
)->all;
838
is_deeply($rows, [{latest_table1__key1 => 1}]);
839

            
840
$dbi = DBIx::Custom->connect;
841
eval { $dbi->execute('drop table table1') };
842
eval { $dbi->execute('drop table table2') };
843
$dbi->execute($create_table1);
844
$dbi->execute($create_table2);
845
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
846
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
847
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
848
$result = $dbi->select(
849
    table => 'table1',
850
    join => [
851
        "left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1"
852
    ]
853
);
854
is_deeply($result->all, [{key1 => 1, key2 => 2}]);
855
$result = $dbi->select(
856
    table => 'table1',
857
    column => [{table2 => ['key3']}],
858
    join => [
859
        "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1"
860
    ]
861
);
862
is_deeply($result->all, [{'table2.key3' => 4}]);
863
$result = $dbi->select(
864
    table => 'table1',
865
    column => [{table2 => ['key3']}],
866
    join => [
867
        "left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'"
868
    ]
869
);
870
is_deeply($result->all, [{'table2.key3' => 4}]);
871

            
872
$dbi = DBIx::Custom->connect;
873
eval { $dbi->execute('drop table table1') };
874
eval { $dbi->execute('drop table table2') };
875
$dbi->execute($create_table1);
876
$dbi->execute($create_table2);
877
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
878
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
879
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
880
$result = $dbi->select(
881
    table => 'table1',
882
    column => [{table2 => ['key3']}],
883
    join => [
884
        {
885
            clause => "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1",
886
            table => ['table1', 'table2']
887
        }
888
    ]
889
);
890
is_deeply($result->all, [{'table2.key3' => 4}]);
891

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

            
893
test 'update_param';
894
$dbi = DBIx::Custom->connect;
895
eval { $dbi->execute('drop table table1') };
896
$dbi->execute($create_table1_2);
897
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
898
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
899

            
900
$param = {key2 => 11};
901
$update_param = $dbi->update_param($param);
902
$sql = <<"EOS";
903
update table1 $update_param
904
where key1 = 1
905
EOS
906
$dbi->execute($sql, param => $param);
907
$result = $dbi->execute('select * from table1;', table => 'table1');
908
$rows   = $result->all;
909
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
910
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
911
                  "basic");
912

            
913

            
914
$dbi = DBIx::Custom->connect;
915
eval { $dbi->execute('drop table table1') };
916
$dbi->execute($create_table1_2);
917
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
918
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
919

            
920
$param = {key2 => 11, key3 => 33};
921
$update_param = $dbi->update_param($param);
922
$sql = <<"EOS";
923
update table1 $update_param
924
where key1 = 1
925
EOS
926
$dbi->execute($sql, param => $param);
927
$result = $dbi->execute('select * from table1;', table => 'table1');
928
$rows   = $result->all;
929
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
930
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
931
                  "basic");
932

            
933
$dbi = DBIx::Custom->connect;
934
eval { $dbi->execute('drop table table1') };
935
$dbi->execute($create_table1_2);
936
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
937
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
938

            
939
$param = {key2 => 11, key3 => 33};
940
$update_param = $dbi->update_param($param, {no_set => 1});
941
$sql = <<"EOS";
942
update table1 set $update_param
943
where key1 = 1
944
EOS
945
$dbi->execute($sql, param => $param);
946
$result = $dbi->execute('select * from table1;', table => 'table1');
947
$rows   = $result->all;
948
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
949
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
950
                  "update param no_set");
951

            
952
            
953
eval { $dbi->update_param({";" => 1}) };
954
like($@, qr/not safety/);
955

            
956

            
957
test 'update_param';
958
$dbi = DBIx::Custom->connect;
959
eval { $dbi->execute('drop table table1') };
960
$dbi->execute($create_table1_2);
961
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
962
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
963

            
964
$param = {key2 => 11};
965
$update_param = $dbi->assign_param($param);
966
$sql = <<"EOS";
967
update table1 set $update_param
968
where key1 = 1
969
EOS
970
$dbi->execute($sql, param => $param, table => 'table1');
971
$result = $dbi->execute('select * from table1;');
972
$rows   = $result->all;
973
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
974
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
975
                  "basic");
976

            
977

            
cleanup test
Yuki Kimoto authored on 2011-08-10
978
test 'type option'; # DEPRECATED!
979
$dbi = DBIx::Custom->connect(
980
    data_source => 'dbi:SQLite:dbname=:memory:',
981
    dbi_option => {
982
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
983
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
984
);
cleanup test
Yuki Kimoto authored on 2011-08-10
985
$binary = pack("I3", 1, 2, 3);
986
eval { $dbi->execute('drop table table1') };
987
$dbi->execute('create table table1(key1, key2)');
988
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
989
$result = $dbi->select(table => 'table1');
990
$row   = $result->one;
991
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
992
$result = $dbi->execute('select length(key1) as key1_length from table1');
993
$row = $result->one;
994
is($row->{key1_length}, length $binary);
cleanup test
Yuki Kimoto authored on 2011-08-06
995

            
cleanup test
Yuki Kimoto authored on 2011-08-10
996
test 'type_rule from';
997
$dbi = DBIx::Custom->connect;
998
$dbi->type_rule(
999
    from1 => {
1000
        date => sub { uc $_[0] }
1001
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
1002
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1003
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1004
$dbi->insert({key1 => 'a'}, table => 'table1');
1005
$result = $dbi->select(table => 'table1');
1006
is($result->fetch_first->[0], 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
1007

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

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

            
1012
test 'type_rule into';
test cleanup
Yuki Kimoto authored on 2011-08-10
1013
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1014
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1015
$dbi->type_rule(
1016
    into1 => {
1017
        date => sub { uc $_[0] }
1018
    }
1019
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1020
$dbi->insert({key1 => 'a'}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
1021
$result = $dbi->select(table => 'table1');
1022
is($result->one->{key1}, 'A');
1023

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1024
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1025
$dbi->execute("create table table1 (key1 date, key2 datetime)");
1026
$dbi->type_rule(
1027
    into1 => [
1028
         [qw/date datetime/] => sub { uc $_[0] }
1029
    ]
1030
);
1031
$dbi->insert({key1 => 'a', key2 => 'b'}, table => 'table1');
1032
$result = $dbi->select(table => 'table1');
1033
$row = $result->one;
1034
is($row->{key1}, 'A');
1035
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
1036

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1037
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1038
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1039
$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1');
1040
$dbi->type_rule(
1041
    into1 => [
1042
        [qw/date datetime/] => sub { uc $_[0] }
1043
    ]
1044
);
1045
$result = $dbi->execute(
1046
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
1047
    param => {key1 => 'a', 'table1.key2' => 'b'}
1048
);
1049
$row = $result->one;
1050
is($row->{key1}, 'a');
1051
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
1052

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1053
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1054
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1055
$dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1');
1056
$dbi->type_rule(
1057
    into1 => [
1058
        [qw/date datetime/] => sub { uc $_[0] }
1059
    ]
1060
);
1061
$result = $dbi->execute(
1062
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
1063
    param => {key1 => 'a', 'table1.key2' => 'b'},
1064
    table => 'table1'
1065
);
1066
$row = $result->one;
1067
is($row->{key1}, 'A');
1068
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
1069

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1070
$dbi = DBIx::Custom->connect;
1071
$dbi->execute("create table table1 (key1 date, key2 datetime)");
1072
$dbi->register_filter(twice => sub { $_[0] * 2 });
1073
$dbi->type_rule(
1074
    from1 => {
1075
        date => 'twice',
1076
    },
1077
    into1 => {
1078
        date => 'twice',
1079
    }
1080
);
1081
$dbi->insert({key1 => 2}, table => 'table1');
1082
$result = $dbi->select(table => 'table1');
1083
is($result->fetch->[0], 8);
cleanup test
Yuki Kimoto authored on 2011-08-06
1084

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1085
test 'type_rule and filter order';
test cleanup
Yuki Kimoto authored on 2011-08-10
1086
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1087
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1088
$dbi->type_rule(
1089
    into1 => {
1090
        date => sub { $_[0] . 'b' }
1091
    },
1092
    into2 => {
1093
        date => sub { $_[0] . 'c' }
1094
    },
1095
    from1 => {
1096
        date => sub { $_[0] . 'd' }
1097
    },
1098
    from2 => {
1099
        date => sub { $_[0] . 'e' }
1100
    }
1101
);
1102
$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }});
1103
$result = $dbi->select(table => 'table1');
1104
$result->filter(key1 => sub { $_[0] . 'f' });
1105
is($result->fetch_first->[0], '1abcdef');
cleanup test
Yuki Kimoto authored on 2011-08-06
1106

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1107
$dbi = DBIx::Custom->connect;
1108
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1109
$dbi->type_rule(
1110
    from1 => {
1111
        date => sub { $_[0] . 'p' }
1112
    },
1113
    from2 => {
1114
        date => sub { $_[0] . 'q' }
1115
    },
1116
);
1117
$dbi->insert({key1 => '1'}, table => 'table1');
1118
$result = $dbi->select(table => 'table1');
1119
$result->type_rule(
1120
    from1 => {
1121
        date => sub { $_[0] . 'd' }
1122
    },
1123
    from2 => {
1124
        date => sub { $_[0] . 'e' }
1125
    }
1126
);
1127
$result->filter(key1 => sub { $_[0] . 'f' });
1128
is($result->fetch_first->[0], '1def');
cleanup test
Yuki Kimoto authored on 2011-08-06
1129

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1130
test 'type_rule_off';
1131
$dbi = DBIx::Custom->connect;
1132
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1133
$dbi->type_rule(
1134
    from1 => {
1135
        date => sub { $_[0] * 2 },
1136
    },
1137
    into1 => {
1138
        date => sub { $_[0] * 2 },
1139
    }
1140
);
1141
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
1142
$result = $dbi->select(table => 'table1', type_rule_off => 1);
1143
is($result->type_rule_off->fetch->[0], 2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1144

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1145
$dbi = DBIx::Custom->connect;
1146
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1147
$dbi->type_rule(
1148
    from1 => {
1149
        date => sub { $_[0] * 2 },
1150
    },
1151
    into1 => {
1152
        date => sub { $_[0] * 3 },
1153
    }
1154
);
1155
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
1156
$result = $dbi->select(table => 'table1', type_rule_off => 1);
1157
is($result->one->{key1}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-06
1158

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1159
$dbi = DBIx::Custom->connect;
1160
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1161
$dbi->type_rule(
1162
    from1 => {
1163
        date => sub { $_[0] * 2 },
1164
    },
1165
    into1 => {
1166
        date => sub { $_[0] * 3 },
1167
    }
1168
);
1169
$dbi->insert({key1 => 2}, table => 'table1');
1170
$result = $dbi->select(table => 'table1');
1171
is($result->one->{key1}, 12);
cleanup test
Yuki Kimoto authored on 2011-08-06
1172

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1173
$dbi = DBIx::Custom->connect;
1174
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1175
$dbi->type_rule(
1176
    from1 => {
1177
        date => sub { $_[0] * 2 },
1178
    },
1179
    into1 => {
1180
        date => sub { $_[0] * 3 },
1181
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
1182
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1183
$dbi->insert({key1 => 2}, table => 'table1');
1184
$result = $dbi->select(table => 'table1');
1185
is($result->fetch->[0], 12);
cleanup test
Yuki Kimoto authored on 2011-08-06
1186

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1187
$dbi = DBIx::Custom->connect;
1188
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1189
$dbi->register_filter(ppp => sub { uc $_[0] });
1190
$dbi->type_rule(
1191
    into1 => {
1192
        date => 'ppp'
cleanup test
Yuki Kimoto authored on 2011-08-06
1193
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
1194
);
1195
$dbi->insert({key1 => 'a'}, table => 'table1');
1196
$result = $dbi->select(table => 'table1');
1197
is($result->one->{key1}, 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
1198

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1199
eval{$dbi->type_rule(
1200
    into1 => {
1201
        date => 'pp'
cleanup test
Yuki Kimoto authored on 2011-08-06
1202
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
1203
)};
1204
like($@, qr/not registered/);
cleanup test
Yuki Kimoto authored on 2011-08-06
1205

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1206
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1207
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1208
eval {
1209
    $dbi->type_rule(
1210
        from1 => {
1211
            Date => sub { $_[0] * 2 },
1212
        }
1213
    );
1214
};
1215
like($@, qr/lower/);
clenup test
Yuki Kimoto authored on 2011-08-06
1216

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1217
eval {
1218
    $dbi->type_rule(
1219
        into1 => {
1220
            Date => sub { $_[0] * 2 },
1221
        }
1222
    );
1223
};
1224
like($@, qr/lower/);
clenup test
Yuki Kimoto authored on 2011-08-06
1225

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1226
$dbi = DBIx::Custom->connect;
1227
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1228
$dbi->type_rule(
1229
    from1 => {
1230
        date => sub { $_[0] * 2 },
1231
    },
1232
    into1 => {
1233
        date => sub { $_[0] * 3 },
1234
    }
1235
);
1236
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
1237
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1238
$result->type_rule_off;
1239
is($result->one->{key1}, 6);
clenup test
Yuki Kimoto authored on 2011-08-06
1240

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1241
$dbi = DBIx::Custom->connect;
1242
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1243
$dbi->type_rule(
1244
    from1 => {
1245
        date => sub { $_[0] * 2 },
1246
        datetime => sub { $_[0] * 4 },
1247
    },
1248
);
1249
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
1250
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1251
$result->type_rule(
1252
    from1 => {
1253
        date => sub { $_[0] * 3 }
1254
    }
1255
);
1256
$row = $result->one;
1257
is($row->{key1}, 6);
1258
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
1259

            
1260
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1261
$result->type_rule(
1262
    from1 => {
1263
        date => sub { $_[0] * 3 }
1264
    }
1265
);
1266
$row = $result->one;
1267
is($row->{key1}, 6);
1268
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
1269

            
1270
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1271
$result->type_rule(
1272
    from1 => {
1273
        date => sub { $_[0] * 3 }
1274
    }
1275
);
1276
$row = $result->one;
1277
is($row->{key1}, 6);
1278
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
1279
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1280
$result->type_rule(
1281
    from1 => [date => sub { $_[0] * 3 }]
1282
);
1283
$row = $result->one;
1284
is($row->{key1}, 6);
1285
is($row->{key2}, 2);
1286
$dbi->register_filter(fivetimes => sub { $_[0] * 5});
clenup test
Yuki Kimoto authored on 2011-08-06
1287
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1288
$result->type_rule(
1289
    from1 => [date => 'fivetimes']
1290
);
1291
$row = $result->one;
1292
is($row->{key1}, 10);
1293
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
1294
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1295
$result->type_rule(
1296
    from1 => [date => undef]
1297
);
1298
$row = $result->one;
1299
is($row->{key1}, 2);
1300
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
1301

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1302
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1303
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1304
$dbi->type_rule(
1305
    from1 => {
1306
        date => sub { $_[0] * 2 },
1307
    },
1308
);
1309
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
1310
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1311
$result->filter(key1 => sub { $_[0] * 3 });
1312
is($result->one->{key1}, 12);
clenup test
Yuki Kimoto authored on 2011-08-06
1313

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1314
$dbi = DBIx::Custom->connect;
1315
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1316
$dbi->type_rule(
1317
    from1 => {
1318
        date => sub { $_[0] * 2 },
1319
    },
1320
);
1321
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
1322
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1323
$result->filter(key1 => sub { $_[0] * 3 });
1324
is($result->fetch->[0], 12);
clenup test
Yuki Kimoto authored on 2011-08-06
1325

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1326
$dbi = DBIx::Custom->connect;
1327
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1328
$dbi->type_rule(
1329
    into1 => {
1330
        date => sub { $_[0] . 'b' }
1331
    },
1332
    into2 => {
1333
        date => sub { $_[0] . 'c' }
1334
    },
1335
    from1 => {
1336
        date => sub { $_[0] . 'd' }
1337
    },
1338
    from2 => {
1339
        date => sub { $_[0] . 'e' }
1340
    }
1341
);
1342
$dbi->insert({key1 => '1'}, table => 'table1', type_rule_off => 1);
clenup test
Yuki Kimoto authored on 2011-08-06
1343
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1344
is($result->type_rule_off->fetch_first->[0], '1');
clenup test
Yuki Kimoto authored on 2011-08-06
1345
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1346
is($result->type_rule_on->fetch_first->[0], '1de');
clenup test
Yuki Kimoto authored on 2011-08-06
1347

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1348
$dbi = DBIx::Custom->connect;
1349
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1350
$dbi->type_rule(
1351
    into1 => {
1352
        date => sub { $_[0] . 'b' }
cleanup test
Yuki Kimoto authored on 2011-08-06
1353
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
1354
    into2 => {
1355
        date => sub { $_[0] . 'c' }
cleanup test
Yuki Kimoto authored on 2011-08-06
1356
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
1357
    from1 => {
1358
        date => sub { $_[0] . 'd' }
cleanup test
Yuki Kimoto authored on 2011-08-06
1359
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
1360
    from2 => {
1361
        date => sub { $_[0] . 'e' }
cleanup test
Yuki Kimoto authored on 2011-08-06
1362
    }
1363
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1364
$dbi->insert({key1 => '1'}, table => 'table1', type_rule1_off => 1);
1365
$result = $dbi->select(table => 'table1');
1366
is($result->type_rule1_off->fetch_first->[0], '1ce');
1367
$result = $dbi->select(table => 'table1');
1368
is($result->type_rule1_on->fetch_first->[0], '1cde');
cleanup test
Yuki Kimoto authored on 2011-08-06
1369

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1370
$dbi = DBIx::Custom->connect;
1371
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1372
$dbi->type_rule(
1373
    into1 => {
1374
        date => sub { $_[0] . 'b' }
1375
    },
1376
    into2 => {
1377
        date => sub { $_[0] . 'c' }
1378
    },
1379
    from1 => {
1380
        date => sub { $_[0] . 'd' }
1381
    },
1382
    from2 => {
1383
        date => sub { $_[0] . 'e' }
cleanup test
Yuki Kimoto authored on 2011-08-06
1384
    }
1385
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1386
$dbi->insert({key1 => '1'}, table => 'table1', type_rule2_off => 1);
1387
$result = $dbi->select(table => 'table1');
1388
is($result->type_rule2_off->fetch_first->[0], '1bd');
1389
$result = $dbi->select(table => 'table1');
1390
is($result->type_rule2_on->fetch_first->[0], '1bde');
test cleanup
Yuki Kimoto authored on 2011-08-10
1391

            
1392
test 'prefix';
1393
$dbi = DBIx::Custom->connect;
1394
eval { $dbi->execute('drop table table1') };
1395
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
1396
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1397
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace');
1398
$result = $dbi->execute('select * from table1;');
1399
$rows   = $result->all;
1400
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
1401

            
1402
$dbi = DBIx::Custom->connect;
1403
eval { $dbi->execute('drop table table1') };
1404
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
1405
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1406
$dbi->update(table => 'table1', param => {key2 => 4},
1407
  where => {key1 => 1}, prefix => 'or replace');
1408
$result = $dbi->execute('select * from table1;');
1409
$rows   = $result->all;
1410
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
1411

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1412
test 'Model class';
1413
use MyDBI1;
1414
$dbi = MyDBI1->connect;
1415
eval { $dbi->execute('drop table book') };
1416
$dbi->execute("create table book (title, author)");
1417
$model = $dbi->model('book');
1418
$model->insert({title => 'a', author => 'b'});
1419
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
1420
$dbi->execute("create table company (name)");
1421
$model = $dbi->model('company');
1422
$model->insert({name => 'a'});
1423
is_deeply($model->list->all, [{name => 'a'}], 'basic');
1424
is($dbi->models->{'book'}, $dbi->model('book'));
1425
is($dbi->models->{'company'}, $dbi->model('company'));
1426

            
1427
$dbi = MyDBI4->connect;
1428
eval { $dbi->execute('drop table book') };
1429
$dbi->execute("create table book (title, author)");
1430
$model = $dbi->model('book');
1431
$model->insert({title => 'a', author => 'b'});
1432
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
1433
$dbi->execute("create table company (name)");
1434
$model = $dbi->model('company');
1435
$model->insert({name => 'a'});
1436
is_deeply($model->list->all, [{name => 'a'}], 'basic');
test cleanup
Yuki Kimoto authored on 2011-08-10
1437

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1438
$dbi = MyDBI5->connect;
1439
eval { $dbi->execute('drop table company') };
1440
eval { $dbi->execute('drop table table1') };
1441
$dbi->execute("create table company (name)");
1442
$dbi->execute("create table table1 (key1)");
1443
$model = $dbi->model('company');
1444
$model->insert({name => 'a'});
1445
is_deeply($model->list->all, [{name => 'a'}], 'include all model');
1446
$dbi->insert(table => 'table1', param => {key1 => 1});
1447
$model = $dbi->model('book');
1448
is_deeply($model->list->all, [{key1 => 1}], 'include all model');
test cleanup
Yuki Kimoto authored on 2011-08-10
1449

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1450
test 'primary_key';
1451
use MyDBI1;
1452
$dbi = MyDBI1->connect;
1453
$model = $dbi->model('book');
1454
$model->primary_key(['id', 'number']);
1455
is_deeply($model->primary_key, ['id', 'number']);
test cleanup
Yuki Kimoto authored on 2011-08-10
1456

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1457
test 'columns';
1458
use MyDBI1;
1459
$dbi = MyDBI1->connect;
1460
$model = $dbi->model('book');
1461
$model->columns(['id', 'number']);
1462
is_deeply($model->columns, ['id', 'number']);
test cleanup
Yuki Kimoto authored on 2011-08-10
1463

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1464
test 'setup_model';
1465
use MyDBI1;
1466
$dbi = MyDBI1->connect;
1467
eval { $dbi->execute('drop table book') };
1468
eval { $dbi->execute('drop table company') };
1469
eval { $dbi->execute('drop table test') };
test cleanup
Yuki Kimoto authored on 2011-08-10
1470

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1471
$dbi->execute('create table book (id)');
1472
$dbi->execute('create table company (id, name);');
1473
$dbi->execute('create table test (id, name, primary key (id, name));');
1474
$dbi->setup_model;
1475
is_deeply($dbi->model('book')->columns, ['id']);
1476
is_deeply($dbi->model('company')->columns, ['id', 'name']);
test cleanup
Yuki Kimoto authored on 2011-08-10
1477

            
1478

            
1479

            
1480

            
1481

            
1482

            
1483

            
1484

            
1485

            
1486

            
1487

            
1488

            
1489

            
1490

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1491
### SQLite only test
1492
test 'quote';
1493
$dbi = DBIx::Custom->connect;
1494
$dbi->quote('"');
1495
eval { $dbi->execute("drop table ${q}table$p") };
1496
$dbi->execute($create_table_reserved);
1497
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
1498
$dbi->insert(table => 'table', param => {select => 1});
1499
$dbi->delete(table => 'table', where => {select => 1});
1500
$result = $dbi->execute("select * from ${q}table$p");
1501
$rows   = $result->all;
1502
is_deeply($rows, [], "reserved word");
1503

            
1504

            
1505

            
1506

            
1507

            
1508

            
1509

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

            
1511

            
1512

            
1513

            
1514

            
1515

            
1516

            
1517

            
1518

            
1519

            
1520
# DEPRECATED! test
1521
test 'filter __ expression';
1522
$dbi = DBIx::Custom->connect;
1523
eval { $dbi->execute('drop table company') };
1524
eval { $dbi->execute('drop table location') };
1525
$dbi->execute('create table company (id, name, location_id)');
1526
$dbi->execute('create table location (id, name)');
1527
$dbi->apply_filter('location',
1528
  name => {in => sub { uc $_[0] } }
1529
);
1530

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

            
1534
$result = $dbi->select(
1535
    table => ['company', 'location'], relation => {'company.location_id' => 'location.id'},
1536
    column => ['location.name as location__name']
1537
);
1538
is($result->fetch_first->[0], 'B');
1539

            
1540
$result = $dbi->select(
1541
    table => 'company', relation => {'company.location_id' => 'location.id'},
1542
    column => ['location.name as location__name']
1543
);
1544
is($result->fetch_first->[0], 'B');
1545

            
1546
$result = $dbi->select(
1547
    table => 'company', relation => {'company.location_id' => 'location.id'},
1548
    column => ['location.name as "location.name"']
1549
);
1550
is($result->fetch_first->[0], 'B');
test cleanup
Yuki Kimoto authored on 2011-08-10
1551

            
1552
test 'reserved_word_quote';
1553
$dbi = DBIx::Custom->connect;
1554
eval { $dbi->execute("drop table ${q}table$p") };
1555
$dbi->reserved_word_quote('"');
1556
$dbi->execute($create_table_reserved);
1557
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
1558
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
1559
$dbi->insert(table => 'table', param => {select => 1});
1560
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
1561
$result = $dbi->execute("select * from ${q}table$p");
1562
$rows   = $result->all;
1563
is_deeply($rows, [{select => 2, update => 6}], "reserved word");