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

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

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

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

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

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

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

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

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

            
73

            
74
test 'delete_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
75
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
76
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
77
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
78
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
79
$dbi->delete_at(
80
    table => 'table1',
81
    primary_key => ['key1', 'key2'],
82
    where => [1, 2],
83
);
84
is_deeply($dbi->select(table => 'table1')->all, []);
85

            
86
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
87
$dbi->delete_at(
88
    table => 'table1',
89
    primary_key => 'key1',
90
    where => 1,
91
);
92
is_deeply($dbi->select(table => 'table1')->all, []);
93

            
94
test 'insert_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
95
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
96
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
97
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
98
$dbi->insert_at(
99
    primary_key => ['key1', 'key2'], 
100
    table => 'table1',
101
    where => [1, 2],
102
    param => {key3 => 3}
103
);
104
is($dbi->select(table => 'table1')->one->{key1}, 1);
105
is($dbi->select(table => 'table1')->one->{key2}, 2);
106
is($dbi->select(table => 'table1')->one->{key3}, 3);
107

            
108
$dbi->delete_all(table => 'table1');
109
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
110
$dbi->insert_at(
111
    primary_key => 'key1', 
112
    table => 'table1',
113
    where => 1,
114
    param => {key2 => 2, key3 => 3}
115
);
116

            
117
is($dbi->select(table => 'table1')->one->{key1}, 1);
118
is($dbi->select(table => 'table1')->one->{key2}, 2);
119
is($dbi->select(table => 'table1')->one->{key3}, 3);
120

            
121
eval {
122
    $dbi->insert_at(
123
        table => 'table1',
124
        primary_key => ['key1', 'key2'],
125
        where => {},
126
        param => {key1 => 1, key2 => 2, key3 => 3},
127
    );
128
};
129
like($@, qr/must be/);
130

            
test cleanup
Yuki Kimoto authored on 2011-08-10
131
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
132
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
133
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
134
$dbi->insert_at(
135
    {key3 => 3},
136
    primary_key => ['key1', 'key2'], 
137
    table => 'table1',
138
    where => [1, 2],
139
);
140
is($dbi->select(table => 'table1')->one->{key1}, 1);
141
is($dbi->select(table => 'table1')->one->{key2}, 2);
142
is($dbi->select(table => 'table1')->one->{key3}, 3);
143

            
144
test 'update_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
145
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
146
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
147
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
148
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
149
$dbi->update_at(
150
    table => 'table1',
151
    primary_key => ['key1', 'key2'],
152
    where => [1, 2],
153
    param => {key3 => 4}
154
);
155
is($dbi->select(table => 'table1')->one->{key1}, 1);
156
is($dbi->select(table => 'table1')->one->{key2}, 2);
157
is($dbi->select(table => 'table1')->one->{key3}, 4);
158

            
159
$dbi->delete_all(table => 'table1');
160
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
161
$dbi->update_at(
162
    table => 'table1',
163
    primary_key => 'key1',
164
    where => 1,
165
    param => {key3 => 4}
166
);
167
is($dbi->select(table => 'table1')->one->{key1}, 1);
168
is($dbi->select(table => 'table1')->one->{key2}, 2);
169
is($dbi->select(table => 'table1')->one->{key3}, 4);
170

            
test cleanup
Yuki Kimoto authored on 2011-08-10
171
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
172
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
173
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
174
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
175
$dbi->update_at(
176
    {key3 => 4},
177
    table => 'table1',
178
    primary_key => ['key1', 'key2'],
179
    where => [1, 2]
180
);
181
is($dbi->select(table => 'table1')->one->{key1}, 1);
182
is($dbi->select(table => 'table1')->one->{key2}, 2);
183
is($dbi->select(table => 'table1')->one->{key3}, 4);
184

            
185
test 'select_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
186
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
187
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
188
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
189
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
190
$result = $dbi->select_at(
191
    table => 'table1',
192
    primary_key => ['key1', 'key2'],
193
    where => [1, 2]
194
);
195
$row = $result->one;
196
is($row->{key1}, 1);
197
is($row->{key2}, 2);
198
is($row->{key3}, 3);
199

            
200
$dbi->delete_all(table => 'table1');
201
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
202
$result = $dbi->select_at(
203
    table => 'table1',
204
    primary_key => 'key1',
205
    where => 1,
206
);
207
$row = $result->one;
208
is($row->{key1}, 1);
209
is($row->{key2}, 2);
210
is($row->{key3}, 3);
211

            
212
$dbi->delete_all(table => 'table1');
213
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
214
$result = $dbi->select_at(
215
    table => 'table1',
216
    primary_key => ['key1', 'key2'],
217
    where => [1, 2]
218
);
219
$row = $result->one;
220
is($row->{key1}, 1);
221
is($row->{key2}, 2);
222
is($row->{key3}, 3);
223

            
224
eval {
225
    $result = $dbi->select_at(
226
        table => 'table1',
227
        primary_key => ['key1', 'key2'],
228
        where => {},
229
    );
230
};
231
like($@, qr/must be/);
232

            
233
eval {
234
    $result = $dbi->select_at(
235
        table => 'table1',
236
        primary_key => ['key1', 'key2'],
237
        where => [1],
238
    );
239
};
240
like($@, qr/same/);
241

            
242
eval {
243
    $result = $dbi->update_at(
244
        table => 'table1',
245
        primary_key => ['key1', 'key2'],
246
        where => {},
247
        param => {key1 => 1, key2 => 2},
248
    );
249
};
250
like($@, qr/must be/);
251

            
252
eval {
253
    $result = $dbi->delete_at(
254
        table => 'table1',
255
        primary_key => ['key1', 'key2'],
256
        where => {},
257
    );
258
};
259
like($@, qr/must be/);
260

            
261
test 'columns';
262
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
263
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
264
$model = $dbi->model('book');
265

            
266

            
267
test 'model delete_at';
268
{
269
    package MyDBI6;
270
    
271
    use base 'DBIx::Custom';
272
    
273
    sub connect {
274
        my $self = shift->SUPER::connect(@_);
275
        
276
        $self->include_model('MyModel5');
277
        
278
        return $self;
279
    }
280
}
test cleanup
Yuki Kimoto authored on 2011-08-10
281
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
282
eval { $dbi->execute('drop table table1') };
283
eval { $dbi->execute('drop table table2') };
284
eval { $dbi->execute('drop table table3') };
cleanup test
Yuki Kimoto authored on 2011-08-10
285
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-10
286
$dbi->execute($create_table2_2);
287
$dbi->execute($create_table3);
cleanup test
Yuki Kimoto authored on 2011-08-06
288
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
289
$dbi->model('table1')->delete_at(where => [1, 2]);
290
is_deeply($dbi->select(table => 'table1')->all, []);
291
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
292
$dbi->model('table1_1')->delete_at(where => [1, 2]);
293
is_deeply($dbi->select(table => 'table1')->all, []);
294
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
295
$dbi->model('table1_3')->delete_at(where => [1, 2]);
296
is_deeply($dbi->select(table => 'table1')->all, []);
297

            
298
test 'model insert_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
299
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
300
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
301
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
302
$dbi->model('table1')->insert_at(
303
    where => [1, 2],
304
    param => {key3 => 3}
305
);
306
$result = $dbi->model('table1')->select;
307
$row = $result->one;
308
is($row->{key1}, 1);
309
is($row->{key2}, 2);
310
is($row->{key3}, 3);
311

            
312
test 'model update_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
313
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
314
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
315
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
316
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
317
$dbi->model('table1')->update_at(
318
    where => [1, 2],
319
    param => {key3 => 4}
320
);
321
$result = $dbi->model('table1')->select;
322
$row = $result->one;
323
is($row->{key1}, 1);
324
is($row->{key2}, 2);
325
is($row->{key3}, 4);
326

            
327
test 'model select_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
328
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
329
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
330
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
331
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
332
$result = $dbi->model('table1')->select_at(where => [1, 2]);
333
$row = $result->one;
334
is($row->{key1}, 1);
335
is($row->{key2}, 2);
336
is($row->{key3}, 3);
337

            
338

            
339
test 'mycolumn and column';
340
{
341
    package MyDBI7;
342
    
343
    use base 'DBIx::Custom';
344
    
345
    sub connect {
346
        my $self = shift->SUPER::connect(@_);
347
        
348
        $self->include_model('MyModel6');
349
        
350
        
351
        return $self;
352
    }
353
}
test cleanup
Yuki Kimoto authored on 2011-08-10
354
$dbi = MyDBI7->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
355
eval { $dbi->execute('drop table table1') };
356
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
357
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
358
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
359
$dbi->separator('__');
360
$dbi->setup_model;
361
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
362
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
363
$model = $dbi->model('table1');
364
$result = $model->select(
365
    column => [$model->mycolumn, $model->column('table2')],
366
    where => {'table1.key1' => 1}
367
);
368
is_deeply($result->one,
369
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
370

            
371
test 'update_param';
test cleanup
Yuki Kimoto authored on 2011-08-10
372
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
373
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
374
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
375
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
376
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
377

            
378
$param = {key2 => 11};
379
$update_param = $dbi->update_param($param);
380
$sql = <<"EOS";
381
update table1 $update_param
382
where key1 = 1
383
EOS
384
$dbi->execute($sql, param => $param);
test cleanup
Yuki Kimoto authored on 2011-08-06
385
$result = $dbi->execute('select * from table1;', table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
386
$rows   = $result->all;
387
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
388
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
389
                  "basic");
390

            
391

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

            
398
$param = {key2 => 11, key3 => 33};
399
$update_param = $dbi->update_param($param);
400
$sql = <<"EOS";
401
update table1 $update_param
402
where key1 = 1
403
EOS
404
$dbi->execute($sql, param => $param);
test cleanup
Yuki Kimoto authored on 2011-08-06
405
$result = $dbi->execute('select * from table1;', table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
406
$rows   = $result->all;
407
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
408
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
409
                  "basic");
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_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
414
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
415
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
416

            
417
$param = {key2 => 11, key3 => 33};
418
$update_param = $dbi->update_param($param, {no_set => 1});
419
$sql = <<"EOS";
420
update table1 set $update_param
421
where key1 = 1
422
EOS
423
$dbi->execute($sql, param => $param);
test cleanup
Yuki Kimoto authored on 2011-08-06
424
$result = $dbi->execute('select * from table1;', table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
425
$rows   = $result->all;
426
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
427
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
428
                  "update param no_set");
429

            
430
            
431
eval { $dbi->update_param({";" => 1}) };
432
like($@, qr/not safety/);
433

            
434

            
435
test 'update_param';
test cleanup
Yuki Kimoto authored on 2011-08-10
436
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
437
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
438
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
439
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
440
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
441

            
442
$param = {key2 => 11};
443
$update_param = $dbi->assign_param($param);
444
$sql = <<"EOS";
445
update table1 set $update_param
446
where key1 = 1
447
EOS
448
$dbi->execute($sql, param => $param, table => 'table1');
test cleanup
Yuki Kimoto authored on 2011-08-06
449
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
450
$rows   = $result->all;
451
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
452
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
453
                  "basic");
454

            
455

            
456
test 'insert_param';
test cleanup
Yuki Kimoto authored on 2011-08-10
457
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
458
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
459
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
460
$param = {key1 => 1, key2 => 2};
461
$insert_param = $dbi->insert_param($param);
462
$sql = <<"EOS";
463
insert into table1 $insert_param
464
EOS
465
$dbi->execute($sql, param => $param, table => 'table1');
466
is($dbi->select(table => 'table1')->one->{key1}, 1);
467
is($dbi->select(table => 'table1')->one->{key2}, 2);
468

            
test cleanup
Yuki Kimoto authored on 2011-08-10
469
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
470
$dbi->quote('"');
cleanup test
Yuki Kimoto authored on 2011-08-10
471
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
472
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
473
$param = {key1 => 1, key2 => 2};
474
$insert_param = $dbi->insert_param($param);
475
$sql = <<"EOS";
476
insert into table1 $insert_param
477
EOS
478
$dbi->execute($sql, param => $param, table => 'table1');
479
is($dbi->select(table => 'table1')->one->{key1}, 1);
480
is($dbi->select(table => 'table1')->one->{key2}, 2);
481

            
482
eval { $dbi->insert_param({";" => 1}) };
483
like($@, qr/not safety/);
484

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

            
486
test 'join';
test cleanup
Yuki Kimoto authored on 2011-08-10
487
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
488
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
489
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
490
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
491
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-10
492
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
493
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
cleanup test
Yuki Kimoto authored on 2011-08-06
494
$dbi->execute('create table table3 (key3 int, key4 int);');
cleanup test
Yuki Kimoto authored on 2011-08-06
495
$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4});
496
$rows = $dbi->select(
497
    table => 'table1',
498
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
499
    where   => {'table1.key2' => 2},
500
    join  => ['left outer join table2 on table1.key1 = table2.key1']
501
)->all;
502
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]);
503

            
504
$rows = $dbi->select(
505
    table => 'table1',
506
    where   => {'key1' => 1},
507
    join  => ['left outer join table2 on table1.key1 = table2.key1']
508
)->all;
509
is_deeply($rows, [{key1 => 1, key2 => 2}]);
510

            
511
eval {
512
    $rows = $dbi->select(
513
        table => 'table1',
514
        column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
515
        where   => {'table1.key2' => 2},
516
        join  => {'table1.key1' => 'table2.key1'}
517
    );
518
};
519
like ($@, qr/array/);
520

            
521
$rows = $dbi->select(
522
    table => 'table1',
523
    where   => {'key1' => 1},
524
    join  => ['left outer join table2 on table1.key1 = table2.key1',
525
              'left outer join table3 on table2.key3 = table3.key3']
526
)->all;
527
is_deeply($rows, [{key1 => 1, key2 => 2}]);
528

            
529
$rows = $dbi->select(
530
    column => 'table3.key4 as table3__key4',
531
    table => 'table1',
532
    where   => {'table1.key1' => 1},
533
    join  => ['left outer join table2 on table1.key1 = table2.key1',
534
              'left outer join table3 on table2.key3 = table3.key3']
535
)->all;
536
is_deeply($rows, [{table3__key4 => 4}]);
537

            
538
$rows = $dbi->select(
539
    column => 'table1.key1 as table1__key1',
540
    table => 'table1',
541
    where   => {'table3.key4' => 4},
542
    join  => ['left outer join table2 on table1.key1 = table2.key1',
543
              'left outer join table3 on table2.key3 = table3.key3']
544
)->all;
545
is_deeply($rows, [{table1__key1 => 1}]);
546

            
test cleanup
Yuki Kimoto authored on 2011-08-10
547
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
548
$dbi->quote('"');
cleanup test
Yuki Kimoto authored on 2011-08-10
549
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
550
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
551
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
552
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
553
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
554
$rows = $dbi->select(
555
    table => 'table1',
556
    column => '"table1"."key1" as "table1_key1", "table2"."key1" as "table2_key1", "key2", "key3"',
557
    where   => {'table1.key2' => 2},
558
    join  => ['left outer join "table2" on "table1"."key1" = "table2"."key1"'],
559
)->all;
560
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}],
561
          'quote');
562

            
563
{
564
    package MyDBI8;
565
    
566
    use base 'DBIx::Custom';
567
    
568
    sub connect {
569
        my $self = shift->SUPER::connect(@_);
570
        
571
        $self->include_model('MyModel7');
572
        
573
        return $self;
574
    }
575
}
cleanup test
Yuki Kimoto authored on 2011-08-06
576

            
test cleanup
Yuki Kimoto authored on 2011-08-10
577
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
578
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
579
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
580
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
581
$sql = <<"EOS";
cleanup test
Yuki Kimoto authored on 2011-08-06
582
left outer join (
583
  select * from table1 as t1
584
  where t1.key2 = (
585
    select max(t2.key2) from table1 as t2
586
    where t1.key1 = t2.key1
587
  )
588
) as latest_table1 on table1.key1 = latest_table1.key1
589
EOS
cleanup test
Yuki Kimoto authored on 2011-08-06
590
$join = [$sql];
591
$rows = $dbi->select(
592
    table => 'table1',
593
    column => 'latest_table1.key1 as latest_table1__key1',
594
    join  => $join
595
)->all;
596
is_deeply($rows, [{latest_table1__key1 => 1}]);
597

            
test cleanup
Yuki Kimoto authored on 2011-08-10
598
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
599
eval { $dbi->execute('drop table table1') };
600
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
601
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
602
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
603
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
604
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
605
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
606
$result = $dbi->select(
607
    table => 'table1',
608
    join => [
609
        "left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1"
610
    ]
611
);
612
is_deeply($result->all, [{key1 => 1, key2 => 2}]);
613
$result = $dbi->select(
614
    table => 'table1',
615
    column => [{table2 => ['key3']}],
616
    join => [
617
        "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1"
618
    ]
619
);
620
is_deeply($result->all, [{'table2.key3' => 4}]);
621
$result = $dbi->select(
622
    table => 'table1',
623
    column => [{table2 => ['key3']}],
624
    join => [
625
        "left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'"
626
    ]
627
);
628
is_deeply($result->all, [{'table2.key3' => 4}]);
629

            
test cleanup
Yuki Kimoto authored on 2011-08-10
630
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
631
eval { $dbi->execute('drop table table1') };
632
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
633
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
634
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
635
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
636
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
637
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
638
$result = $dbi->select(
639
    table => 'table1',
640
    column => [{table2 => ['key3']}],
641
    join => [
642
        {
643
            clause => "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1",
644
            table => ['table1', 'table2']
645
        }
646
    ]
647
);
648
is_deeply($result->all, [{'table2.key3' => 4}]);
649

            
650
test 'mycolumn';
test cleanup
Yuki Kimoto authored on 2011-08-10
651
$dbi = MyDBI8->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
652
eval { $dbi->execute('drop table table1') };
653
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
654
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
655
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
656
$dbi->setup_model;
657
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
658
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
659
$model = $dbi->model('table1');
660
$result = $model->select_at(
661
    column => [
662
        $model->mycolumn,
663
        $model->column('table2')
664
    ]
665
);
666
is_deeply($result->one,
667
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
668

            
669
$result = $model->select_at(
670
    column => [
671
        $model->mycolumn(['key1']),
672
        $model->column(table2 => ['key1'])
673
    ]
674
);
675
is_deeply($result->one,
676
          {key1 => 1, 'table2.key1' => 1});
677
$result = $model->select_at(
678
    column => [
679
        $model->mycolumn(['key1']),
680
        {table2 => ['key1']}
681
    ]
682
);
683
is_deeply($result->one,
684
          {key1 => 1, 'table2.key1' => 1});
685

            
686
$result = $model->select_at(
687
    column => [
688
        $model->mycolumn(['key1']),
689
        ['table2.key1', as => 'table2.key1']
690
    ]
691
);
692
is_deeply($result->one,
693
          {key1 => 1, 'table2.key1' => 1});
694

            
695
$result = $model->select_at(
696
    column => [
697
        $model->mycolumn(['key1']),
698
        ['table2.key1' => 'table2.key1']
699
    ]
700
);
701
is_deeply($result->one,
702
          {key1 => 1, 'table2.key1' => 1});
703

            
704
test 'dbi method from model';
705
{
706
    package MyDBI9;
707
    
708
    use base 'DBIx::Custom';
709
    
710
    sub connect {
711
        my $self = shift->SUPER::connect(@_);
712
        
713
        $self->include_model('MyModel8')->setup_model;
714
        
715
        return $self;
716
    }
717
}
test cleanup
Yuki Kimoto authored on 2011-08-10
718
$dbi = MyDBI9->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
719
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
720
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
721
$model = $dbi->model('table1');
722
eval{$model->execute('select * from table1')};
723
ok(!$@);
724

            
725
test 'column table option';
test cleanup
Yuki Kimoto authored on 2011-08-10
726
$dbi = MyDBI9->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
727
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
728
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
729
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
730
$dbi->setup_model;
731
$dbi->execute('insert into table1 (key1, key2) values (1, 2);');
732
$dbi->execute('insert into table2 (key1, key3) values (1, 4);');
733
$model = $dbi->model('table1');
734
$result = $model->select(
735
    column => [
736
        $model->column('table2', {alias => 'table2_alias'})
737
    ],
738
    where => {'table2_alias.key3' => 4}
739
);
740
is_deeply($result->one, 
741
          {'table2_alias.key1' => 1, 'table2_alias.key3' => 4});
742

            
743
$dbi->separator('__');
744
$result = $model->select(
745
    column => [
746
        $model->column('table2', {alias => 'table2_alias'})
747
    ],
748
    where => {'table2_alias.key3' => 4}
749
);
750
is_deeply($result->one, 
751
          {'table2_alias__key1' => 1, 'table2_alias__key3' => 4});
752

            
753
$dbi->separator('-');
754
$result = $model->select(
755
    column => [
756
        $model->column('table2', {alias => 'table2_alias'})
757
    ],
758
    where => {'table2_alias.key3' => 4}
759
);
760
is_deeply($result->one, 
761
          {'table2_alias-key1' => 1, 'table2_alias-key3' => 4});
762

            
763
test 'type option'; # DEPRECATED!
764
$dbi = DBIx::Custom->connect(
765
    dbi_option => {
766
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
767
    }
768
);
cleanup test
Yuki Kimoto authored on 2011-08-10
769
$binary = pack("I3", 1, 2, 3);
770
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
771
$dbi->execute('create table table1(key1, key2)');
772
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
773
$result = $dbi->select(table => 'table1');
774
$row   = $result->one;
775
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
776
$result = $dbi->execute('select length(key1) as key1_length from table1');
777
$row = $result->one;
778
is($row->{key1_length}, length $binary);
779

            
780
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [['key1'] => DBI::SQL_BLOB]);
781
$result = $dbi->select(table => 'table1');
782
$row   = $result->one;
783
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
784
$result = $dbi->execute('select length(key1) as key1_length from table1');
785
$row = $result->one;
786
is($row->{key1_length}, length $binary);
787

            
788

            
789
test 'bind_type option';
790
$dbi = DBIx::Custom->connect(
791
    dbi_option => {
792
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
793
    }
794
);
795
$binary = pack("I3", 1, 2, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
796
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
797
$dbi->execute('create table table1(key1, key2)');
798
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, bind_type => [key1 => DBI::SQL_BLOB]);
799
$result = $dbi->select(table => 'table1');
800
$row   = $result->one;
801
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
802
$result = $dbi->execute('select length(key1) as key1_length from table1');
803
$row = $result->one;
804
is($row->{key1_length}, length $binary);
805

            
806
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, bind_type => [['key1'] => DBI::SQL_BLOB]);
807
$result = $dbi->select(table => 'table1');
808
$row   = $result->one;
809
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
810
$result = $dbi->execute('select length(key1) as key1_length from table1');
811
$row = $result->one;
812
is($row->{key1_length}, length $binary);
813

            
814
test 'model type attribute';
815
$dbi = DBIx::Custom->connect(
816
    dbi_option => {
817
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
818
    }
819
);
820
$binary = pack("I3", 1, 2, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
821
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
822
$dbi->execute('create table table1(key1, key2)');
823
$model = $dbi->create_model(table => 'table1', bind_type => [key1 => DBI::SQL_BLOB]);
824
$model->insert(param => {key1 => $binary, key2 => 'あ'});
825
$result = $dbi->select(table => 'table1');
826
$row   = $result->one;
827
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
828
$result = $dbi->execute('select length(key1) as key1_length from table1');
829
$row = $result->one;
830
is($row->{key1_length}, length $binary);
831

            
832
test 'create_model';
test cleanup
Yuki Kimoto authored on 2011-08-10
833
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
834
eval { $dbi->execute('drop table table1') };
835
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
836
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
837
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
838

            
839
$dbi->create_model(
840
    table => 'table1',
841
    join => [
842
       'left outer join table2 on table1.key1 = table2.key1'
843
    ],
844
    primary_key => ['key1']
845
);
846
$model2 = $dbi->create_model(
847
    table => 'table2'
848
);
849
$dbi->create_model(
850
    table => 'table3',
851
    filter => [
852
        key1 => {in => sub { uc $_[0] }}
853
    ]
854
);
855
$dbi->setup_model;
856
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
857
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
858
$model = $dbi->model('table1');
859
$result = $model->select(
860
    column => [$model->mycolumn, $model->column('table2')],
861
    where => {'table1.key1' => 1}
862
);
863
is_deeply($result->one,
864
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
865
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
866

            
867
test 'model method';
868
test 'create_model';
test cleanup
Yuki Kimoto authored on 2011-08-10
869
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
870
eval { $dbi->execute('drop table table2') };
test cleanup
Yuki Kimoto authored on 2011-08-10
871
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
872
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
873
$model = $dbi->create_model(
874
    table => 'table2'
875
);
876
$model->method(foo => sub { shift->select(@_) });
877
is_deeply($model->foo->one, {key1 => 1, key3 => 3});
878

            
879
test 'merge_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
880
$dbi = DBIx::Custom->new;
881
$params = [
882
    {key1 => 1, key2 => 2, key3 => 3},
883
    {key1 => 1, key2 => 2},
884
    {key1 => 1}
885
];
886
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]);
887
is_deeply($param, {key1 => [1, 1, 1], key2 => [2, 2], key3 => 3});
888

            
889
$params = [
890
    {key1 => [1, 2], key2 => 1, key3 => [1, 2]},
891
    {key1 => [3, 4], key2 => [2, 3], key3 => 3}
892
];
893
$param = $dbi->merge_param($params->[0], $params->[1]);
894
is_deeply($param, {key1 => [1, 2, 3, 4], key2 => [1, 2, 3], key3 => [1, 2, 3]});
cleanup test
Yuki Kimoto authored on 2011-08-06
895

            
896
test 'select() param option';
test cleanup
Yuki Kimoto authored on 2011-08-10
897
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
898
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
899
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
900
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
901
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
902
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
903
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
904
$dbi->insert(table => 'table2', param => {key1 => 2, key3 => 5});
905
$rows = $dbi->select(
906
    table => 'table1',
907
    column => 'table1.key1 as table1_key1, key2, key3',
908
    where   => {'table1.key2' => 3},
909
    join  => ['inner join (select * from table2 where {= table2.key3})' . 
910
              ' as table2 on table1.key1 = table2.key1'],
911
    param => {'table2.key3' => 5}
912
)->all;
913
is_deeply($rows, [{table1_key1 => 2, key2 => 3, key3 => 5}]);
914

            
915

            
916
test 'select() wrap option';
test cleanup
Yuki Kimoto authored on 2011-08-10
917
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
918
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
919
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
920
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
921
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
922
$rows = $dbi->select(
923
    table => 'table1',
924
    column => 'key1',
925
    wrap => ['select * from (', ') as t where key1 = 1']
926
)->all;
927
is_deeply($rows, [{key1 => 1}]);
928

            
929
eval {
930
$dbi->select(
931
    table => 'table1',
932
    column => 'key1',
933
    wrap => 'select * from ('
934
)
935
};
936
like($@, qr/array/);
937

            
938
test 'select() string where';
test cleanup
Yuki Kimoto authored on 2011-08-10
939
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
940
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
941
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
942
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
943
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
944
$rows = $dbi->select(
945
    table => 'table1',
946
    where => 'key1 = :key1 and key2 = :key2',
947
    where_param => {key1 => 1, key2 => 2}
948
)->all;
949
is_deeply($rows, [{key1 => 1, key2 => 2}]);
950

            
test cleanup
Yuki Kimoto authored on 2011-08-10
951
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
952
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
953
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
954
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
955
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
956
$rows = $dbi->select(
957
    table => 'table1',
958
    where => [
959
        'key1 = :key1 and key2 = :key2',
960
        {key1 => 1, key2 => 2}
961
    ]
962
)->all;
963
is_deeply($rows, [{key1 => 1, key2 => 2}]);
964

            
965
test 'delete() string where';
test cleanup
Yuki Kimoto authored on 2011-08-10
966
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
967
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
968
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
969
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
970
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
971
$dbi->delete(
972
    table => 'table1',
973
    where => 'key1 = :key1 and key2 = :key2',
974
    where_param => {key1 => 1, key2 => 2}
975
);
976
$rows = $dbi->select(table => 'table1')->all;
977
is_deeply($rows, [{key1 => 2, key2 => 3}]);
978

            
test cleanup
Yuki Kimoto authored on 2011-08-10
979
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
980
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
981
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
982
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
983
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
984
$dbi->delete(
985
    table => 'table1',
986
    where => [
987
        'key1 = :key1 and key2 = :key2',
988
         {key1 => 1, key2 => 2}
989
    ]
990
);
991
$rows = $dbi->select(table => 'table1')->all;
992
is_deeply($rows, [{key1 => 2, key2 => 3}]);
993

            
994

            
995
test 'update() string where';
test cleanup
Yuki Kimoto authored on 2011-08-10
996
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
997
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
998
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
999
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1000
$dbi->update(
1001
    table => 'table1',
1002
    param => {key1 => 5},
1003
    where => 'key1 = :key1 and key2 = :key2',
1004
    where_param => {key1 => 1, key2 => 2}
1005
);
1006
$rows = $dbi->select(table => 'table1')->all;
1007
is_deeply($rows, [{key1 => 5, key2 => 2}]);
1008

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1009
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1010
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1011
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1012
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1013
$dbi->update(
1014
    table => 'table1',
1015
    param => {key1 => 5},
1016
    where => [
1017
        'key1 = :key1 and key2 = :key2',
1018
        {key1 => 1, key2 => 2}
1019
    ]
1020
);
1021
$rows = $dbi->select(table => 'table1')->all;
1022
is_deeply($rows, [{key1 => 5, key2 => 2}]);
1023

            
1024
test 'insert id and primary_key option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1025
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1026
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1027
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1028
$dbi->insert(
1029
    primary_key => ['key1', 'key2'], 
1030
    table => 'table1',
1031
    id => [1, 2],
1032
    param => {key3 => 3}
1033
);
1034
is($dbi->select(table => 'table1')->one->{key1}, 1);
1035
is($dbi->select(table => 'table1')->one->{key2}, 2);
1036
is($dbi->select(table => 'table1')->one->{key3}, 3);
1037

            
1038
$dbi->delete_all(table => 'table1');
1039
$dbi->insert(
1040
    primary_key => 'key1', 
1041
    table => 'table1',
1042
    id => 0,
1043
    param => {key2 => 2, key3 => 3}
1044
);
1045

            
1046
is($dbi->select(table => 'table1')->one->{key1}, 0);
1047
is($dbi->select(table => 'table1')->one->{key2}, 2);
1048
is($dbi->select(table => 'table1')->one->{key3}, 3);
1049

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1050
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1051
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1052
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1053
$dbi->insert(
1054
    {key3 => 3},
1055
    primary_key => ['key1', 'key2'], 
1056
    table => 'table1',
1057
    id => [1, 2],
1058
);
1059
is($dbi->select(table => 'table1')->one->{key1}, 1);
1060
is($dbi->select(table => 'table1')->one->{key2}, 2);
1061
is($dbi->select(table => 'table1')->one->{key3}, 3);
1062

            
1063

            
1064
test 'model insert id and primary_key option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1065
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1066
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1067
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1068
$dbi->model('table1')->insert(
1069
    id => [1, 2],
1070
    param => {key3 => 3}
1071
);
1072
$result = $dbi->model('table1')->select;
1073
$row = $result->one;
1074
is($row->{key1}, 1);
1075
is($row->{key2}, 2);
1076
is($row->{key3}, 3);
1077

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1078
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1079
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1080
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1081
$dbi->model('table1')->insert(
1082
    {key3 => 3},
1083
    id => [1, 2]
1084
);
1085
$result = $dbi->model('table1')->select;
1086
$row = $result->one;
1087
is($row->{key1}, 1);
1088
is($row->{key2}, 2);
1089
is($row->{key3}, 3);
1090

            
1091
test 'update and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1092
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1093
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1094
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1095
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1096
$dbi->update(
1097
    table => 'table1',
1098
    primary_key => ['key1', 'key2'],
1099
    id => [1, 2],
1100
    param => {key3 => 4}
1101
);
1102
is($dbi->select(table => 'table1')->one->{key1}, 1);
1103
is($dbi->select(table => 'table1')->one->{key2}, 2);
1104
is($dbi->select(table => 'table1')->one->{key3}, 4);
1105

            
1106
$dbi->delete_all(table => 'table1');
1107
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
1108
$dbi->update(
1109
    table => 'table1',
1110
    primary_key => 'key1',
1111
    id => 0,
1112
    param => {key3 => 4}
1113
);
1114
is($dbi->select(table => 'table1')->one->{key1}, 0);
1115
is($dbi->select(table => 'table1')->one->{key2}, 2);
1116
is($dbi->select(table => 'table1')->one->{key3}, 4);
1117

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1118
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1119
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1120
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1121
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1122
$dbi->update(
1123
    {key3 => 4},
1124
    table => 'table1',
1125
    primary_key => ['key1', 'key2'],
1126
    id => [1, 2]
1127
);
1128
is($dbi->select(table => 'table1')->one->{key1}, 1);
1129
is($dbi->select(table => 'table1')->one->{key2}, 2);
1130
is($dbi->select(table => 'table1')->one->{key3}, 4);
1131

            
1132

            
1133
test 'model update and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1134
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1135
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1136
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1137
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1138
$dbi->model('table1')->update(
1139
    id => [1, 2],
1140
    param => {key3 => 4}
1141
);
1142
$result = $dbi->model('table1')->select;
1143
$row = $result->one;
1144
is($row->{key1}, 1);
1145
is($row->{key2}, 2);
1146
is($row->{key3}, 4);
1147

            
1148

            
1149
test 'delete and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1150
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1151
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1152
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1153
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1154
$dbi->delete(
1155
    table => 'table1',
1156
    primary_key => ['key1', 'key2'],
1157
    id => [1, 2],
1158
);
1159
is_deeply($dbi->select(table => 'table1')->all, []);
1160

            
1161
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
1162
$dbi->delete(
1163
    table => 'table1',
1164
    primary_key => 'key1',
1165
    id => 0,
1166
);
1167
is_deeply($dbi->select(table => 'table1')->all, []);
1168

            
1169

            
1170
test 'model delete and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1171
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1172
eval { $dbi->execute('drop table table1') };
1173
eval { $dbi->execute('drop table table2') };
1174
eval { $dbi->execute('drop table table3') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1175
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-10
1176
$dbi->execute($create_table2_2);
1177
$dbi->execute($create_table3);
cleanup test
Yuki Kimoto authored on 2011-08-06
1178
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1179
$dbi->model('table1')->delete(id => [1, 2]);
1180
is_deeply($dbi->select(table => 'table1')->all, []);
1181
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
1182
$dbi->model('table1_1')->delete(id => [1, 2]);
1183
is_deeply($dbi->select(table => 'table1')->all, []);
1184
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
1185
$dbi->model('table1_3')->delete(id => [1, 2]);
1186
is_deeply($dbi->select(table => 'table1')->all, []);
1187

            
1188

            
1189
test 'select and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1190
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1191
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1192
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1193
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1194
$result = $dbi->select(
1195
    table => 'table1',
1196
    primary_key => ['key1', 'key2'],
1197
    id => [1, 2]
1198
);
1199
$row = $result->one;
1200
is($row->{key1}, 1);
1201
is($row->{key2}, 2);
1202
is($row->{key3}, 3);
1203

            
1204
$dbi->delete_all(table => 'table1');
1205
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
1206
$result = $dbi->select(
1207
    table => 'table1',
1208
    primary_key => 'key1',
1209
    id => 0,
1210
);
1211
$row = $result->one;
1212
is($row->{key1}, 0);
1213
is($row->{key2}, 2);
1214
is($row->{key3}, 3);
1215

            
1216
$dbi->delete_all(table => 'table1');
1217
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1218
$result = $dbi->select(
1219
    table => 'table1',
1220
    primary_key => ['key1', 'key2'],
1221
    id => [1, 2]
1222
);
1223
$row = $result->one;
1224
is($row->{key1}, 1);
1225
is($row->{key2}, 2);
1226
is($row->{key3}, 3);
1227

            
1228

            
1229
test 'model select_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
1230
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1231
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1232
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1233
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1234
$result = $dbi->model('table1')->select(id => [1, 2]);
1235
$row = $result->one;
1236
is($row->{key1}, 1);
1237
is($row->{key2}, 2);
1238
is($row->{key3}, 3);
1239

            
1240
test 'column separator is default .';
test cleanup
Yuki Kimoto authored on 2011-08-10
1241
$dbi = MyDBI7->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1242
eval { $dbi->execute('drop table table1') };
1243
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1244
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1245
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1246
$dbi->setup_model;
1247
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1248
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1249
$model = $dbi->model('table1');
1250
$result = $model->select(
1251
    column => [$model->column('table2')],
1252
    where => {'table1.key1' => 1}
1253
);
1254
is_deeply($result->one,
1255
          {'table2.key1' => 1, 'table2.key3' => 3});
1256

            
1257
$result = $model->select(
1258
    column => [$model->column('table2' => [qw/key1 key3/])],
1259
    where => {'table1.key1' => 1}
1260
);
1261
is_deeply($result->one,
1262
          {'table2.key1' => 1, 'table2.key3' => 3});
1263

            
1264

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

            
1266
test 'separator';
test cleanup
Yuki Kimoto authored on 2011-08-10
1267
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1268
eval { $dbi->execute('drop table table1') };
1269
eval { $dbi->execute('drop table table2') };
1270
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1271
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-10
1272

            
1273
$dbi->create_model(
1274
    table => 'table1',
1275
    join => [
1276
       'left outer join table2 on table1.key1 = table2.key1'
1277
    ],
1278
    primary_key => ['key1'],
cleanup test
Yuki Kimoto authored on 2011-08-06
1279
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1280
$model2 = $dbi->create_model(
1281
    table => 'table2',
1282
);
1283
$dbi->setup_model;
1284
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1285
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1286
$model = $dbi->model('table1');
1287
$result = $model->select(
1288
    column => [
1289
        $model->mycolumn,
1290
        {table2 => [qw/key1 key3/]}
1291
    ],
1292
    where => {'table1.key1' => 1}
1293
);
1294
is_deeply($result->one,
1295
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
1296
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
1297

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1298
$dbi->separator('__');
1299
$model = $dbi->model('table1');
1300
$result = $model->select(
1301
    column => [
1302
        $model->mycolumn,
1303
        {table2 => [qw/key1 key3/]}
1304
    ],
1305
    where => {'table1.key1' => 1}
1306
);
1307
is_deeply($result->one,
1308
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
1309
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
1310

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1311
$dbi->separator('-');
1312
$model = $dbi->model('table1');
1313
$result = $model->select(
1314
    column => [
1315
        $model->mycolumn,
1316
        {table2 => [qw/key1 key3/]}
1317
    ],
1318
    where => {'table1.key1' => 1}
1319
);
1320
is_deeply($result->one,
1321
          {key1 => 1, key2 => 2, 'table2-key1' => 1, 'table2-key3' => 3});
1322
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
1323

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

            
1325
test 'filter_off';
test cleanup
Yuki Kimoto authored on 2011-08-10
1326
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1327
eval { $dbi->execute('drop table table1') };
1328
eval { $dbi->execute('drop table table2') };
1329
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1330
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-10
1331

            
1332
$dbi->create_model(
1333
    table => 'table1',
1334
    join => [
1335
       'left outer join table2 on table1.key1 = table2.key1'
1336
    ],
1337
    primary_key => ['key1'],
cleanup test
Yuki Kimoto authored on 2011-08-06
1338
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1339
$dbi->setup_model;
1340
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1341
$model = $dbi->model('table1');
1342
$result = $model->select(column => 'key1');
1343
$result->filter(key1 => sub { $_[0] * 2 });
1344
is_deeply($result->one, {key1 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-06
1345

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1346
test 'available_datetype';
test cleanup
Yuki Kimoto authored on 2011-08-10
1347
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1348
ok($dbi->can('available_datatype'));
1349

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1351
test 'select prefix option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1352
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1353
eval { $dbi->execute('drop table table1') };
1354
$dbi->execute($create_table1);
1355
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1356
$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all;
1357
is_deeply($rows, [{key1 => 1, key2 => 2}], "table");
1358

            
1359

            
1360
test 'separator';
1361
$dbi = DBIx::Custom->connect;
1362
is($dbi->separator, '.');
1363
$dbi->separator('-');
1364
is($dbi->separator, '-');
1365
$dbi->separator('__');
1366
is($dbi->separator, '__');
1367
eval { $dbi->separator('?') };
1368
like($@, qr/Separator/);
1369

            
1370

            
1371
test 'map_param';
1372
$dbi = DBIx::Custom->connect;
1373
$param = $dbi->map_param(
1374
    {id => 1, author => 'Ken', price => 1900},
1375
    id => 'book.id',
1376
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1377
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
cleanup test
Yuki Kimoto authored on 2011-08-06
1378
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1379
is_deeply($param, {'book.id' => 1, 'book.author' => '%Ken%',
1380
  'book.price' => 1900});
1381

            
1382
$param = $dbi->map_param(
1383
    {id => 0, author => 0, price => 0},
1384
    id => 'book.id',
1385
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1386
    price => ['book.price', sub { '%' . $_[0] . '%' },
1387
      {if => sub { $_[0] eq 0 }}]
cleanup test
Yuki Kimoto authored on 2011-08-06
1388
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1389
is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
cleanup test
Yuki Kimoto authored on 2011-08-06
1390

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1391
$param = $dbi->map_param(
1392
    {id => '', author => '', price => ''},
1393
    id => 'book.id',
1394
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1395
    price => ['book.price', sub { '%' . $_[0] . '%' },
1396
      {if => sub { $_[0] eq 1 }}]
cleanup test
Yuki Kimoto authored on 2011-08-06
1397
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1398
is_deeply($param, {});
1399

            
1400
$param = $dbi->map_param(
1401
    {id => undef, author => undef, price => undef},
1402
    id => 'book.id',
1403
    price => ['book.price', {if => 'exists'}]
cleanup test
Yuki Kimoto authored on 2011-08-06
1404
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1405
is_deeply($param, {'book.price' => undef});
1406

            
1407
$param = $dbi->map_param(
1408
    {price => 'a'},
1409
    id => ['book.id', {if => 'exists'}],
1410
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
1411
);
1412
is_deeply($param, {'book.price' => '%a'});
cleanup test
Yuki Kimoto authored on 2011-08-06
1413

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

            
1415
test 'table_alias';
test cleanup
Yuki Kimoto authored on 2011-08-10
1416
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1417
eval { $dbi->execute('drop table table1') };
1418
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
cleanup test
Yuki Kimoto authored on 2011-08-06
1419
$dbi->type_rule(
1420
    into1 => {
cleanup test
Yuki Kimoto authored on 2011-08-10
1421
        date => sub { uc $_[0] }
cleanup test
Yuki Kimoto authored on 2011-08-06
1422
    }
1423
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1424
$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'},
1425
  table_alias => {table2 => 'table1'});
cleanup test
Yuki Kimoto authored on 2011-08-06
1426
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1427
is($result->one->{key1}, 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
1428

            
1429

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1430
test 'order';
test cleanup
Yuki Kimoto authored on 2011-08-10
1431
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1432
eval { $dbi->execute('drop table table1') };
1433
$dbi->execute("create table table1 (key1, key2)");
1434
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
1435
$dbi->insert({key1 => 1, key2 => 3}, table => 'table1');
1436
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
1437
$dbi->insert({key1 => 2, key2 => 4}, table => 'table1');
1438
my $order = $dbi->order;
1439
$order->prepend('key1', 'key2 desc');
1440
$result = $dbi->select(table => 'table1', append => "$order");
1441
is_deeply($result->all, [{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1},
1442
  {key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}]);
1443
$order->prepend('key1 desc');
1444
$result = $dbi->select(table => 'table1', append => "$order");
1445
is_deeply($result->all, [{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2},
1446
  {key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
1447

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1448
$order = $dbi->order;
1449
$order->prepend(['table1-key1'], [qw/table1-key2 desc/]);
1450
$result = $dbi->select(table => 'table1',
1451
  column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']],
1452
  append => "$order");
1453
is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3},
1454
  {'table1-key1' => 1, 'table1-key2' => 1},
1455
  {'table1-key1' => 2, 'table1-key2' => 4},
1456
  {'table1-key1' => 2, 'table1-key2' => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
1457

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1458
test 'tag_parse';
test cleanup
Yuki Kimoto authored on 2011-08-10
1459
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1460
$dbi->tag_parse(0);
1461
eval { $dbi->execute('drop table table1') };
1462
$dbi->execute("create table table1 (key1, key2)");
1463
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
1464
eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})};
1465
ok($@);
cleanup test
Yuki Kimoto authored on 2011-08-06
1466

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1467
test 'last_sql';
test cleanup
Yuki Kimoto authored on 2011-08-10
1468
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1469
eval { $dbi->execute('drop table table1') };
1470
$dbi->execute("create table table1 (key1, key2)");
1471
$dbi->execute('select * from table1');
1472
is($dbi->last_sql, 'select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
1473

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1477
test 'DBIx::Custom header';
test cleanup
Yuki Kimoto authored on 2011-08-10
1478
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1479
eval { $dbi->execute('drop table table1') };
1480
$dbi->execute("create table table1 (key1, key2)");
1481
$result = $dbi->execute('select key1 as h1, key2 as h2 from table1');
1482
is_deeply($result->header, [qw/h1 h2/]);
cleanup test
Yuki Kimoto authored on 2011-08-06
1483

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

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1505
$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}";
1506
$result = $dbi->execute(
1507
    $source,
1508
    param => {'table1.key1' => 1, 'table1.key2' => 1},
1509
    filter => {'table1.key2' => sub { $_[0] * 2 }}
cleanup test
Yuki Kimoto authored on 2011-08-06
1510
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1511
$rows = $result->all;
1512
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
1513

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1514
test 'high perfomance way';
1515
$dbi->execute('drop table table1');
1516
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
1517
$rows = [
1518
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
1519
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
1520
];
1521
{
1522
    my $query;
1523
    foreach my $row (@$rows) {
1524
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
1525
      $dbi->execute($query, $row, filter => {ab => sub { $_[0] * 2 }});
cleanup test
Yuki Kimoto authored on 2011-08-06
1526
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
1527
    is_deeply($dbi->select(table => 'table1')->all,
1528
      [
1529
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
1530
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
1531
      ]
1532
    );
1533
}
1534

            
1535
$dbi->execute('drop table table1');
1536
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
1537
$rows = [
1538
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
1539
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
1540
];
1541
{
1542
    my $query;
1543
    my $sth;
1544
    foreach my $row (@$rows) {
1545
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
1546
      $sth ||= $query->sth;
1547
      $sth->execute(map { $row->{$_} } sort keys %$row);
cleanup test
Yuki Kimoto authored on 2011-08-06
1548
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
1549
    is_deeply($dbi->select(table => 'table1')->all,
1550
      [
1551
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
1552
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
1553
      ]
1554
    );
1555
}
cleanup test
Yuki Kimoto authored on 2011-08-06
1556

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1557
test 'result';
test cleanup
Yuki Kimoto authored on 2011-08-10
1558
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1559
eval { $dbi->execute('drop table table1') };
1560
$dbi->execute($create_table1);
1561
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
1562
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
1563

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1564
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1565
@rows = ();
1566
while (my $row = $result->fetch) {
1567
    push @rows, [@$row];
1568
}
1569
is_deeply(\@rows, [[1, 2], [3, 4]]);
cleanup test
Yuki Kimoto authored on 2011-08-06
1570

            
1571
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1572
@rows = ();
1573
while (my $row = $result->fetch_hash) {
1574
    push @rows, {%$row};
1575
}
1576
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
1577

            
1578
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1579
$row = $result->fetch_first;
1580
is_deeply($row, [1, 2], "row");
1581
$row = $result->fetch;
1582
ok(!$row, "finished");
1583

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1584
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1585
$row = $result->fetch_hash_first;
1586
is_deeply($row, {key1 => 1, key2 => 2}, "row");
1587
$row = $result->fetch_hash;
1588
ok(!$row, "finished");
1589

            
1590
$dbi->execute('create table table2 (key1, key2);');
1591
$result = $dbi->select(table => 'table2');
1592
$row = $result->fetch_hash_first;
1593
ok(!$row, "no row fetch");
cleanup test
Yuki Kimoto authored on 2011-08-06
1594

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1595
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1596
eval { $dbi->execute('drop table table1') };
1597
$dbi->execute($create_table1);
1598
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
1599
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
1600
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1');
1601
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1');
1602
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
1603
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1604
$rows = $result->fetch_multi(2);
1605
is_deeply($rows, [[1, 2],
1606
                  [3, 4]], "fetch_multi first");
1607
$rows = $result->fetch_multi(2);
1608
is_deeply($rows, [[5, 6],
1609
                  [7, 8]], "fetch_multi secound");
1610
$rows = $result->fetch_multi(2);
1611
is_deeply($rows, [[9, 10]], "fetch_multi third");
1612
$rows = $result->fetch_multi(2);
1613
ok(!$rows);
1614

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

            
1619
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
1620
$rows = $result->fetch_hash_multi(2);
1621
is_deeply($rows, [{key1 => 1, key2 => 2},
1622
                  {key1 => 3, key2 => 4}], "fetch_multi first");
1623
$rows = $result->fetch_hash_multi(2);
1624
is_deeply($rows, [{key1 => 5, key2 => 6},
1625
                  {key1 => 7, key2 => 8}], "fetch_multi secound");
1626
$rows = $result->fetch_hash_multi(2);
1627
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third");
1628
$rows = $result->fetch_hash_multi(2);
1629
ok(!$rows);
1630

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1635
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1636
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1637
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
1638
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
1639
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
1640

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1641
test 'fetch_all';
1642
$result = $dbi->select(table => 'table1');
1643
$rows = $result->fetch_all;
1644
is_deeply($rows, [[1, 2], [3, 4]]);
cleanup test
Yuki Kimoto authored on 2011-08-06
1645

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1657
$result = $dbi->select(table => 'table1');
1658
$result->dbi->filters({three_times => sub { $_[0] * 3}});
1659
$result->filter({key1 => 'three_times'});
1660
$rows = $result->fetch_hash_all;
1661
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash");
1662

            
1663
test "query_builder";
1664
$datas = [
1665
    # Basic tests
1666
    {   name            => 'placeholder basic',
1667
        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
1668
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;",
1669
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
1670
    },
1671
    {
1672
        name            => 'placeholder in',
1673
        source            => "{in k1 3};",
1674
        sql_expected    => "k1 in (?, ?, ?);",
1675
        columns_expected   => [qw/k1 k1 k1/]
1676
    },
1677
    
1678
    # Table name
1679
    {
1680
        name            => 'placeholder with table name',
1681
        source            => "{= a.k1} {= a.k2}",
1682
        sql_expected    => "a.k1 = ? a.k2 = ?;",
1683
        columns_expected  => [qw/a.k1 a.k2/]
1684
    },
1685
    {   
1686
        name            => 'placeholder in with table name',
1687
        source            => "{in a.k1 2} {in b.k2 2}",
1688
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?);",
1689
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
1690
    },
1691
    {
1692
        name            => 'not contain tag',
1693
        source            => "aaa",
1694
        sql_expected    => "aaa;",
1695
        columns_expected  => [],
1696
    }
1697
];
1698

            
1699
for (my $i = 0; $i < @$datas; $i++) {
1700
    my $data = $datas->[$i];
1701
    my $builder = DBIx::Custom->new->query_builder;
1702
    my $query = $builder->build_query($data->{source});
1703
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
1704
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
1705
}
1706

            
1707
$builder = DBIx::Custom->new->query_builder;
1708
$ret_val = $builder->register_tag(
1709
    p => sub {
1710
        my @args = @_;
1711
        
1712
        my $expand    = "? $args[0] $args[1]";
1713
        my $columns = [2];
1714
        return [$expand, $columns];
1715
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
1716
);
1717

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

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1731
$builder->register_tag({
1732
    q => 'string'
1733
});
cleanup test
Yuki Kimoto authored on 2011-08-06
1734

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1738
$builder->register_tag({
1739
   r => sub {} 
1740
});
cleanup test
Yuki Kimoto authored on 2011-08-06
1741

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

            
1745
$builder->register_tag({
1746
   s => sub { return ["a", ""]} 
1747
});
1748

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

            
1752
$builder->register_tag(
1753
    t => sub {return ["a", []]}
cleanup test
Yuki Kimoto authored on 2011-08-06
1754
);
1755

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

            
1757
test 'General error case';
1758
$builder = DBIx::Custom->new->query_builder;
1759
$builder->register_tag(
1760
    a => sub {
1761
        return ["? ? ?", ['']];
1762
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
1763
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1764
eval{$builder->build_query("{a}")};
1765
like($@, qr/\QPlaceholder count/, "placeholder count is invalid");
cleanup test
Yuki Kimoto authored on 2011-08-06
1766

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

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

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

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

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

            
1783
test 'variouse source';
1784
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;";
1785
$query = $builder->build_query($source);
1786
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1");
1787

            
1788
$source = "abc;";
1789
$query = $builder->build_query($source);
1790
is($query->sql, 'abc;', "basic : 2");
1791

            
1792
$source = "{= a}";
1793
$query = $builder->build_query($source);
1794
is($query->sql, 'a = ?;', "only tag");
1795

            
1796
$source = "000;";
1797
$query = $builder->build_query($source);
1798
is($query->sql, '000;', "contain 0 value");
1799

            
1800
$source = "a {= b} }";
1801
eval{$builder->build_query($source)};
1802
like($@, qr/unexpected "}"/, "error : 1");
1803

            
1804
$source = "a {= {}";
1805
eval{$builder->build_query($source)};
1806
like($@, qr/unexpected "{"/, "error : 2");
1807

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

            
1809

            
1810

            
1811

            
1812

            
1813

            
1814

            
1815

            
1816

            
1817

            
1818

            
1819

            
1820

            
1821

            
1822

            
1823

            
1824

            
1825

            
1826

            
1827

            
1828

            
1829

            
1830

            
1831

            
1832

            
1833

            
1834
### a little complex test
cleanup test
Yuki Kimoto authored on 2011-08-10
1835
test 'type option'; # DEPRECATED!
1836
$dbi = DBIx::Custom->connect(
1837
    data_source => 'dbi:SQLite:dbname=:memory:',
1838
    dbi_option => {
1839
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
1840
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
1841
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1842
$binary = pack("I3", 1, 2, 3);
1843
eval { $dbi->execute('drop table table1') };
1844
$dbi->execute('create table table1(key1, key2)');
1845
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
1846
$result = $dbi->select(table => 'table1');
1847
$row   = $result->one;
1848
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
1849
$result = $dbi->execute('select length(key1) as key1_length from table1');
1850
$row = $result->one;
1851
is($row->{key1_length}, length $binary);
cleanup test
Yuki Kimoto authored on 2011-08-06
1852

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1853
test 'type_rule from';
1854
$dbi = DBIx::Custom->connect;
1855
$dbi->type_rule(
1856
    from1 => {
1857
        date => sub { uc $_[0] }
1858
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
1859
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1860
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1861
$dbi->insert({key1 => 'a'}, table => 'table1');
1862
$result = $dbi->select(table => 'table1');
1863
is($result->fetch_first->[0], 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
1864

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

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

            
1869
test 'type_rule into';
test cleanup
Yuki Kimoto authored on 2011-08-10
1870
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1871
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1872
$dbi->type_rule(
1873
    into1 => {
1874
        date => sub { uc $_[0] }
1875
    }
1876
);
cleanup test
Yuki Kimoto authored on 2011-08-10
1877
$dbi->insert({key1 => 'a'}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
1878
$result = $dbi->select(table => 'table1');
1879
is($result->one->{key1}, 'A');
1880

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1881
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1882
$dbi->execute("create table table1 (key1 date, key2 datetime)");
1883
$dbi->type_rule(
1884
    into1 => [
1885
         [qw/date datetime/] => sub { uc $_[0] }
1886
    ]
1887
);
1888
$dbi->insert({key1 => 'a', key2 => 'b'}, table => 'table1');
1889
$result = $dbi->select(table => 'table1');
1890
$row = $result->one;
1891
is($row->{key1}, 'A');
1892
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
1893

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1894
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1895
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1896
$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1');
1897
$dbi->type_rule(
1898
    into1 => [
1899
        [qw/date datetime/] => sub { uc $_[0] }
1900
    ]
1901
);
1902
$result = $dbi->execute(
1903
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
1904
    param => {key1 => 'a', 'table1.key2' => 'b'}
1905
);
1906
$row = $result->one;
1907
is($row->{key1}, 'a');
1908
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
1909

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1910
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1911
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1912
$dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1');
1913
$dbi->type_rule(
1914
    into1 => [
1915
        [qw/date datetime/] => sub { uc $_[0] }
1916
    ]
1917
);
1918
$result = $dbi->execute(
1919
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
1920
    param => {key1 => 'a', 'table1.key2' => 'b'},
1921
    table => 'table1'
1922
);
1923
$row = $result->one;
1924
is($row->{key1}, 'A');
1925
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
1926

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1927
$dbi = DBIx::Custom->connect;
1928
$dbi->execute("create table table1 (key1 date, key2 datetime)");
1929
$dbi->register_filter(twice => sub { $_[0] * 2 });
1930
$dbi->type_rule(
1931
    from1 => {
1932
        date => 'twice',
1933
    },
1934
    into1 => {
1935
        date => 'twice',
1936
    }
1937
);
1938
$dbi->insert({key1 => 2}, table => 'table1');
1939
$result = $dbi->select(table => 'table1');
1940
is($result->fetch->[0], 8);
cleanup test
Yuki Kimoto authored on 2011-08-06
1941

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1942
test 'type_rule and filter order';
test cleanup
Yuki Kimoto authored on 2011-08-10
1943
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1944
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1945
$dbi->type_rule(
1946
    into1 => {
1947
        date => sub { $_[0] . 'b' }
1948
    },
1949
    into2 => {
1950
        date => sub { $_[0] . 'c' }
1951
    },
1952
    from1 => {
1953
        date => sub { $_[0] . 'd' }
1954
    },
1955
    from2 => {
1956
        date => sub { $_[0] . 'e' }
1957
    }
1958
);
1959
$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }});
1960
$result = $dbi->select(table => 'table1');
1961
$result->filter(key1 => sub { $_[0] . 'f' });
1962
is($result->fetch_first->[0], '1abcdef');
cleanup test
Yuki Kimoto authored on 2011-08-06
1963

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1964
$dbi = DBIx::Custom->connect;
1965
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1966
$dbi->type_rule(
1967
    from1 => {
1968
        date => sub { $_[0] . 'p' }
1969
    },
1970
    from2 => {
1971
        date => sub { $_[0] . 'q' }
1972
    },
1973
);
1974
$dbi->insert({key1 => '1'}, table => 'table1');
1975
$result = $dbi->select(table => 'table1');
1976
$result->type_rule(
1977
    from1 => {
1978
        date => sub { $_[0] . 'd' }
1979
    },
1980
    from2 => {
1981
        date => sub { $_[0] . 'e' }
1982
    }
1983
);
1984
$result->filter(key1 => sub { $_[0] . 'f' });
1985
is($result->fetch_first->[0], '1def');
cleanup test
Yuki Kimoto authored on 2011-08-06
1986

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1987
test 'type_rule_off';
1988
$dbi = DBIx::Custom->connect;
1989
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
1990
$dbi->type_rule(
1991
    from1 => {
1992
        date => sub { $_[0] * 2 },
1993
    },
1994
    into1 => {
1995
        date => sub { $_[0] * 2 },
1996
    }
1997
);
1998
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
1999
$result = $dbi->select(table => 'table1', type_rule_off => 1);
2000
is($result->type_rule_off->fetch->[0], 2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2001

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2002
$dbi = DBIx::Custom->connect;
2003
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2004
$dbi->type_rule(
2005
    from1 => {
2006
        date => sub { $_[0] * 2 },
2007
    },
2008
    into1 => {
2009
        date => sub { $_[0] * 3 },
2010
    }
2011
);
2012
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
2013
$result = $dbi->select(table => 'table1', type_rule_off => 1);
2014
is($result->one->{key1}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-06
2015

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2016
$dbi = DBIx::Custom->connect;
2017
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2018
$dbi->type_rule(
2019
    from1 => {
2020
        date => sub { $_[0] * 2 },
2021
    },
2022
    into1 => {
2023
        date => sub { $_[0] * 3 },
2024
    }
2025
);
2026
$dbi->insert({key1 => 2}, table => 'table1');
2027
$result = $dbi->select(table => 'table1');
2028
is($result->one->{key1}, 12);
cleanup test
Yuki Kimoto authored on 2011-08-06
2029

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2030
$dbi = DBIx::Custom->connect;
2031
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2032
$dbi->type_rule(
2033
    from1 => {
2034
        date => sub { $_[0] * 2 },
2035
    },
2036
    into1 => {
2037
        date => sub { $_[0] * 3 },
2038
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
2039
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2040
$dbi->insert({key1 => 2}, table => 'table1');
2041
$result = $dbi->select(table => 'table1');
2042
is($result->fetch->[0], 12);
cleanup test
Yuki Kimoto authored on 2011-08-06
2043

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2044
$dbi = DBIx::Custom->connect;
2045
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2046
$dbi->register_filter(ppp => sub { uc $_[0] });
2047
$dbi->type_rule(
2048
    into1 => {
2049
        date => 'ppp'
cleanup test
Yuki Kimoto authored on 2011-08-06
2050
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
2051
);
2052
$dbi->insert({key1 => 'a'}, table => 'table1');
2053
$result = $dbi->select(table => 'table1');
2054
is($result->one->{key1}, 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
2055

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2056
eval{$dbi->type_rule(
2057
    into1 => {
2058
        date => 'pp'
cleanup test
Yuki Kimoto authored on 2011-08-06
2059
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
2060
)};
2061
like($@, qr/not registered/);
cleanup test
Yuki Kimoto authored on 2011-08-06
2062

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2063
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2064
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2065
eval {
2066
    $dbi->type_rule(
2067
        from1 => {
2068
            Date => sub { $_[0] * 2 },
2069
        }
2070
    );
2071
};
2072
like($@, qr/lower/);
clenup test
Yuki Kimoto authored on 2011-08-06
2073

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2074
eval {
2075
    $dbi->type_rule(
2076
        into1 => {
2077
            Date => sub { $_[0] * 2 },
2078
        }
2079
    );
2080
};
2081
like($@, qr/lower/);
clenup test
Yuki Kimoto authored on 2011-08-06
2082

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2083
$dbi = DBIx::Custom->connect;
2084
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2085
$dbi->type_rule(
2086
    from1 => {
2087
        date => sub { $_[0] * 2 },
2088
    },
2089
    into1 => {
2090
        date => sub { $_[0] * 3 },
2091
    }
2092
);
2093
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
2094
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2095
$result->type_rule_off;
2096
is($result->one->{key1}, 6);
clenup test
Yuki Kimoto authored on 2011-08-06
2097

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2098
$dbi = DBIx::Custom->connect;
2099
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2100
$dbi->type_rule(
2101
    from1 => {
2102
        date => sub { $_[0] * 2 },
2103
        datetime => sub { $_[0] * 4 },
2104
    },
2105
);
2106
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
2107
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2108
$result->type_rule(
2109
    from1 => {
2110
        date => sub { $_[0] * 3 }
2111
    }
2112
);
2113
$row = $result->one;
2114
is($row->{key1}, 6);
2115
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
2116

            
2117
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2118
$result->type_rule(
2119
    from1 => {
2120
        date => sub { $_[0] * 3 }
2121
    }
2122
);
2123
$row = $result->one;
2124
is($row->{key1}, 6);
2125
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
2126

            
2127
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2128
$result->type_rule(
2129
    from1 => {
2130
        date => sub { $_[0] * 3 }
2131
    }
2132
);
2133
$row = $result->one;
2134
is($row->{key1}, 6);
2135
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
2136
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2137
$result->type_rule(
2138
    from1 => [date => sub { $_[0] * 3 }]
2139
);
2140
$row = $result->one;
2141
is($row->{key1}, 6);
2142
is($row->{key2}, 2);
2143
$dbi->register_filter(fivetimes => sub { $_[0] * 5});
clenup test
Yuki Kimoto authored on 2011-08-06
2144
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2145
$result->type_rule(
2146
    from1 => [date => 'fivetimes']
2147
);
2148
$row = $result->one;
2149
is($row->{key1}, 10);
2150
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
2151
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2152
$result->type_rule(
2153
    from1 => [date => undef]
2154
);
2155
$row = $result->one;
2156
is($row->{key1}, 2);
2157
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
2158

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2159
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2160
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2161
$dbi->type_rule(
2162
    from1 => {
2163
        date => sub { $_[0] * 2 },
2164
    },
2165
);
2166
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
2167
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2168
$result->filter(key1 => sub { $_[0] * 3 });
2169
is($result->one->{key1}, 12);
clenup test
Yuki Kimoto authored on 2011-08-06
2170

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2171
$dbi = DBIx::Custom->connect;
2172
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2173
$dbi->type_rule(
2174
    from1 => {
2175
        date => sub { $_[0] * 2 },
2176
    },
2177
);
2178
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
2179
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2180
$result->filter(key1 => sub { $_[0] * 3 });
2181
is($result->fetch->[0], 12);
clenup test
Yuki Kimoto authored on 2011-08-06
2182

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2227
$dbi = DBIx::Custom->connect;
2228
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2229
$dbi->type_rule(
2230
    into1 => {
2231
        date => sub { $_[0] . 'b' }
2232
    },
2233
    into2 => {
2234
        date => sub { $_[0] . 'c' }
2235
    },
2236
    from1 => {
2237
        date => sub { $_[0] . 'd' }
2238
    },
2239
    from2 => {
2240
        date => sub { $_[0] . 'e' }
cleanup test
Yuki Kimoto authored on 2011-08-06
2241
    }
2242
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2243
$dbi->insert({key1 => '1'}, table => 'table1', type_rule2_off => 1);
2244
$result = $dbi->select(table => 'table1');
2245
is($result->type_rule2_off->fetch_first->[0], '1bd');
2246
$result = $dbi->select(table => 'table1');
2247
is($result->type_rule2_on->fetch_first->[0], '1bde');
test cleanup
Yuki Kimoto authored on 2011-08-10
2248

            
2249
test 'prefix';
2250
$dbi = DBIx::Custom->connect;
2251
eval { $dbi->execute('drop table table1') };
2252
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
2253
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2254
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace');
2255
$result = $dbi->execute('select * from table1;');
2256
$rows   = $result->all;
2257
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
2258

            
2259
$dbi = DBIx::Custom->connect;
2260
eval { $dbi->execute('drop table table1') };
2261
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
2262
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2263
$dbi->update(table => 'table1', param => {key2 => 4},
2264
  where => {key1 => 1}, prefix => 'or replace');
2265
$result = $dbi->execute('select * from table1;');
2266
$rows   = $result->all;
2267
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
2268

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2269
test 'Model class';
2270
use MyDBI1;
2271
$dbi = MyDBI1->connect;
2272
eval { $dbi->execute('drop table book') };
2273
$dbi->execute("create table book (title, author)");
2274
$model = $dbi->model('book');
2275
$model->insert({title => 'a', author => 'b'});
2276
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
2277
$dbi->execute("create table company (name)");
2278
$model = $dbi->model('company');
2279
$model->insert({name => 'a'});
2280
is_deeply($model->list->all, [{name => 'a'}], 'basic');
2281
is($dbi->models->{'book'}, $dbi->model('book'));
2282
is($dbi->models->{'company'}, $dbi->model('company'));
2283

            
2284
{
2285
    package MyDBI4;
test cleanup
Yuki Kimoto authored on 2011-08-10
2286

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2287
    use strict;
2288
    use warnings;
test cleanup
Yuki Kimoto authored on 2011-08-10
2289

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2290
    use base 'DBIx::Custom';
2291

            
2292
    sub connect {
2293
        my $self = shift->SUPER::connect(@_);
2294
        
2295
        $self->include_model(
2296
            MyModel2 => [
2297
                'book',
2298
                {class => 'Company', name => 'company'}
2299
            ]
2300
        );
2301
    }
2302

            
2303
    package MyModel2::Base1;
2304

            
2305
    use strict;
2306
    use warnings;
2307

            
2308
    use base 'DBIx::Custom::Model';
2309

            
2310
    package MyModel2::book;
2311

            
2312
    use strict;
2313
    use warnings;
2314

            
2315
    use base 'MyModel2::Base1';
2316

            
2317
    sub insert {
2318
        my ($self, $param) = @_;
2319
        
2320
        return $self->SUPER::insert(param => $param);
2321
    }
2322

            
2323
    sub list { shift->select; }
2324

            
2325
    package MyModel2::Company;
2326

            
2327
    use strict;
2328
    use warnings;
2329

            
2330
    use base 'MyModel2::Base1';
2331

            
2332
    sub insert {
2333
        my ($self, $param) = @_;
2334
        
2335
        return $self->SUPER::insert(param => $param);
2336
    }
2337

            
2338
    sub list { shift->select; }
2339
}
2340
$dbi = MyDBI4->connect;
2341
eval { $dbi->execute('drop table book') };
2342
$dbi->execute("create table book (title, author)");
2343
$model = $dbi->model('book');
2344
$model->insert({title => 'a', author => 'b'});
2345
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
2346
$dbi->execute("create table company (name)");
2347
$model = $dbi->model('company');
2348
$model->insert({name => 'a'});
2349
is_deeply($model->list->all, [{name => 'a'}], 'basic');
test cleanup
Yuki Kimoto authored on 2011-08-10
2350

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2351
{
2352
     package MyDBI5;
test cleanup
Yuki Kimoto authored on 2011-08-10
2353

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2354
    use strict;
2355
    use warnings;
test cleanup
Yuki Kimoto authored on 2011-08-10
2356

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2357
    use base 'DBIx::Custom';
test cleanup
Yuki Kimoto authored on 2011-08-10
2358

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2359
    sub connect {
2360
        my $self = shift->SUPER::connect(@_);
2361
        
2362
        $self->include_model('MyModel4');
2363
    }
2364
}
2365
$dbi = MyDBI5->connect;
2366
eval { $dbi->execute('drop table company') };
2367
eval { $dbi->execute('drop table table1') };
2368
$dbi->execute("create table company (name)");
2369
$dbi->execute("create table table1 (key1)");
2370
$model = $dbi->model('company');
2371
$model->insert({name => 'a'});
2372
is_deeply($model->list->all, [{name => 'a'}], 'include all model');
2373
$dbi->insert(table => 'table1', param => {key1 => 1});
2374
$model = $dbi->model('book');
2375
is_deeply($model->list->all, [{key1 => 1}], 'include all model');
test cleanup
Yuki Kimoto authored on 2011-08-10
2376

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2377
test 'primary_key';
2378
use MyDBI1;
2379
$dbi = MyDBI1->connect;
2380
$model = $dbi->model('book');
2381
$model->primary_key(['id', 'number']);
2382
is_deeply($model->primary_key, ['id', 'number']);
test cleanup
Yuki Kimoto authored on 2011-08-10
2383

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2384
test 'columns';
2385
use MyDBI1;
2386
$dbi = MyDBI1->connect;
2387
$model = $dbi->model('book');
2388
$model->columns(['id', 'number']);
2389
is_deeply($model->columns, ['id', 'number']);
test cleanup
Yuki Kimoto authored on 2011-08-10
2390

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2391
test 'setup_model';
2392
use MyDBI1;
2393
$dbi = MyDBI1->connect;
2394
eval { $dbi->execute('drop table book') };
2395
eval { $dbi->execute('drop table company') };
2396
eval { $dbi->execute('drop table test') };
test cleanup
Yuki Kimoto authored on 2011-08-10
2397

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2398
$dbi->execute('create table book (id)');
2399
$dbi->execute('create table company (id, name);');
2400
$dbi->execute('create table test (id, name, primary key (id, name));');
2401
$dbi->setup_model;
2402
is_deeply($dbi->model('book')->columns, ['id']);
2403
is_deeply($dbi->model('company')->columns, ['id', 'name']);
test cleanup
Yuki Kimoto authored on 2011-08-10
2404

            
2405

            
2406

            
2407

            
2408

            
2409

            
2410

            
2411

            
2412

            
2413

            
2414

            
2415

            
2416

            
2417

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2418
### SQLite only test
2419
test 'quote';
2420
$dbi = DBIx::Custom->connect;
2421
$dbi->quote('"');
2422
eval { $dbi->execute("drop table ${q}table$p") };
2423
$dbi->execute($create_table_reserved);
2424
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
2425
$dbi->insert(table => 'table', param => {select => 1});
2426
$dbi->delete(table => 'table', where => {select => 1});
2427
$result = $dbi->execute("select * from ${q}table$p");
2428
$rows   = $result->all;
2429
is_deeply($rows, [], "reserved word");
2430

            
2431

            
2432

            
2433

            
2434

            
2435

            
2436

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

            
2438

            
2439

            
2440

            
2441

            
2442

            
2443

            
2444

            
2445

            
2446

            
2447
# DEPRECATED! test
2448
test 'filter __ expression';
2449
$dbi = DBIx::Custom->connect;
2450
eval { $dbi->execute('drop table company') };
2451
eval { $dbi->execute('drop table location') };
2452
$dbi->execute('create table company (id, name, location_id)');
2453
$dbi->execute('create table location (id, name)');
2454
$dbi->apply_filter('location',
2455
  name => {in => sub { uc $_[0] } }
2456
);
2457

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

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

            
2467
$result = $dbi->select(
2468
    table => 'company', relation => {'company.location_id' => 'location.id'},
2469
    column => ['location.name as location__name']
2470
);
2471
is($result->fetch_first->[0], 'B');
2472

            
2473
$result = $dbi->select(
2474
    table => 'company', relation => {'company.location_id' => 'location.id'},
2475
    column => ['location.name as "location.name"']
2476
);
2477
is($result->fetch_first->[0], 'B');
test cleanup
Yuki Kimoto authored on 2011-08-10
2478

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