DBIx-Custom / t / common.t /
Newer Older
4160 lines | 139.243kb
added common test executing ...
Yuki Kimoto authored on 2011-08-07
1
use Test::More;
2
use strict;
3
use warnings;
test cleanup
Yuki Kimoto authored on 2011-08-10
4
use Encode qw/encode_utf8/;
cleanup test
Yuki Kimoto authored on 2011-08-10
5
use FindBin;
cleanup
Yuki Kimoto authored on 2011-08-13
6
use Scalar::Util 'isweak';
cleanup test
Yuki Kimoto authored on 2011-08-10
7

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

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

            
14
plan 'no_plan';
15

            
finished oracle test
Yuki Kimoto authored on 2011-08-15
16
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/};
17
sub test { print "# $_[0]\n" }
18

            
cleanup test
Yuki Kimoto authored on 2011-08-15
19
# Constant
20
my $table1 = $dbi->table1;
21
my $table2 = $dbi->table2;
22
my $table2_alias = $dbi->table2_alias;
23
my $table3 = $dbi->table3;
24
my $key1 = $dbi->key1;
25
my $key2 = $dbi->key2;
26
my $key3 = $dbi->key3;
27
my $key4 = $dbi->key4;
28
my $key5 = $dbi->key5;
29
my $key6 = $dbi->key6;
30
my $key7 = $dbi->key7;
31
my $key8 = $dbi->key8;
32
my $key9 = $dbi->key9;
33
my $key10 = $dbi->key10;
34
my $create_table1 = $dbi->create_table1;
35
my $create_table1_2 = $dbi->create_table1_2;
36
my $create_table1_type = $dbi->create_table1_type;
37
my $create_table1_highperformance = $dbi->create_table1_highperformance;
38
my $create_table2 = $dbi->create_table2;
39
my $create_table2_2 = $dbi->create_table2_2;
40
my $create_table3 = $dbi->create_table3;
41
my $create_table_reserved = $dbi->create_table_reserved;
42
my $q = substr($dbi->quote, 0, 1);
43
my $p = substr($dbi->quote, 1, 1) || $q;
44
my $date_typename = $dbi->date_typename;
45
my $datetime_typename = $dbi->datetime_typename;
46
my $date_datatype = $dbi->date_datatype;
47
my $datetime_datatype = $dbi->datetime_datatype;
48

            
49
# Variables
50
my $builder;
51
my $datas;
52
my $sth;
53
my $source;
54
my @sources;
55
my $select_source;
56
my $insert_source;
57
my $update_source;
58
my $param;
59
my $params;
60
my $sql;
61
my $result;
62
my $row;
63
my @rows;
64
my $rows;
65
my $query;
66
my @queries;
67
my $select_query;
68
my $insert_query;
69
my $update_query;
70
my $ret_val;
71
my $infos;
72
my $model;
73
my $model2;
74
my $where;
75
my $update_param;
76
my $insert_param;
77
my $join;
78
my $binary;
added test
Yuki Kimoto authored on 2011-08-16
79
my $user_table_info;
cleanup
Yuki Kimoto authored on 2011-08-16
80
my $user_column_info;
cleanup test
Yuki Kimoto authored on 2011-08-15
81

            
test cleanup
Yuki Kimoto authored on 2011-08-15
82
require MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
83
{
84
    package MyDBI4;
85

            
86
    use strict;
87
    use warnings;
88

            
89
    use base 'DBIx::Custom';
90

            
91
    sub connect {
92
        my $self = shift->SUPER::connect(@_);
93
        
94
        $self->include_model(
95
            MyModel2 => [
cleanup test
Yuki Kimoto authored on 2011-08-15
96
                $table1,
97
                {class => $table2, name => $table2}
test cleanup
Yuki Kimoto authored on 2011-08-10
98
            ]
99
        );
100
    }
101

            
102
    package MyModel2::Base1;
103

            
104
    use strict;
105
    use warnings;
106

            
107
    use base 'DBIx::Custom::Model';
108

            
test cleanup
Yuki Kimoto authored on 2011-08-10
109
    package MyModel2::table1;
test cleanup
Yuki Kimoto authored on 2011-08-10
110

            
111
    use strict;
112
    use warnings;
113

            
114
    use base 'MyModel2::Base1';
115

            
116
    sub insert {
117
        my ($self, $param) = @_;
118
        
119
        return $self->SUPER::insert(param => $param);
120
    }
121

            
122
    sub list { shift->select; }
123

            
test cleanup
Yuki Kimoto authored on 2011-08-10
124
    package MyModel2::table2;
test cleanup
Yuki Kimoto authored on 2011-08-10
125

            
126
    use strict;
127
    use warnings;
128

            
129
    use base 'MyModel2::Base1';
130

            
131
    sub insert {
132
        my ($self, $param) = @_;
133
        
134
        return $self->SUPER::insert(param => $param);
135
    }
136

            
137
    sub list { shift->select; }
test cleanup
Yuki Kimoto authored on 2011-08-15
138

            
139
    package MyModel2::TABLE1;
140

            
141
    use strict;
142
    use warnings;
143

            
144
    use base 'MyModel2::Base1';
145

            
146
    sub insert {
147
        my ($self, $param) = @_;
148
        
149
        return $self->SUPER::insert(param => $param);
150
    }
151

            
152
    sub list { shift->select; }
153

            
154
    package MyModel2::TABLE2;
155

            
156
    use strict;
157
    use warnings;
158

            
159
    use base 'MyModel2::Base1';
160

            
161
    sub insert {
162
        my ($self, $param) = @_;
163
        
164
        return $self->SUPER::insert(param => $param);
165
    }
166

            
167
    sub list { shift->select; }
test cleanup
Yuki Kimoto authored on 2011-08-10
168
}
169
{
170
     package MyDBI5;
171

            
172
    use strict;
173
    use warnings;
174

            
175
    use base 'DBIx::Custom';
176

            
177
    sub connect {
178
        my $self = shift->SUPER::connect(@_);
179
        
180
        $self->include_model('MyModel4');
181
    }
182
}
183
{
184
    package MyDBI6;
185
    
186
    use base 'DBIx::Custom';
187
    
188
    sub connect {
189
        my $self = shift->SUPER::connect(@_);
190
        
191
        $self->include_model('MyModel5');
192
        
193
        return $self;
194
    }
195
}
196
{
197
    package MyDBI7;
198
    
199
    use base 'DBIx::Custom';
200
    
201
    sub connect {
202
        my $self = shift->SUPER::connect(@_);
203
        
204
        $self->include_model('MyModel6');
205
        
206
        
207
        return $self;
208
    }
209
}
210
{
211
    package MyDBI8;
212
    
213
    use base 'DBIx::Custom';
214
    
215
    sub connect {
216
        my $self = shift->SUPER::connect(@_);
217
        
218
        $self->include_model('MyModel7');
219
        
220
        return $self;
221
    }
222
}
223

            
224
{
225
    package MyDBI9;
226
    
227
    use base 'DBIx::Custom';
228
    
229
    sub connect {
230
        my $self = shift->SUPER::connect(@_);
231
        
cleanup test
Yuki Kimoto authored on 2011-08-10
232
        $self->include_model('MyModel8');
test cleanup
Yuki Kimoto authored on 2011-08-10
233
        
234
        return $self;
235
    }
236
}
237

            
added test
Yuki Kimoto authored on 2011-08-16
238
# Get user table info
239
$dbi = DBIx::Custom->connect;
240
eval { $dbi->execute("drop table $table1") };
241
eval { $dbi->execute("drop table $table2") };
242
eval { $dbi->execute("drop table $table3") };
243
$dbi->execute($create_table1);
244
$dbi->execute($create_table2);
245
$dbi->execute($create_table3);
246
$user_table_info = $dbi->get_table_info(exclude => $dbi->exclude_table);
247

            
cleanup test
Yuki Kimoto authored on 2011-08-15
248
# Create table
test cleanup
Yuki Kimoto authored on 2011-08-10
249
$dbi = DBIx::Custom->connect;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
250
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
251
$dbi->execute($create_table1);
252
$model = $dbi->create_model(table => $table1);
253
$model->insert({$key1 => 1, $key2 => 2});
254
is_deeply($model->select->all, [{$key1 => 1, $key2 => 2}]);
cleanup
Yuki Kimoto authored on 2011-08-15
255

            
cleanup test
Yuki Kimoto authored on 2011-08-15
256
test 'DBIx::Custom::Result test';
257
$dbi->delete_all(table => $table1);
258
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
259
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
260
$source = "select $key1, $key2 from $table1";
261
$query = $dbi->create_query($source);
262
$result = $dbi->execute($query);
cleanup
Yuki Kimoto authored on 2011-08-15
263

            
cleanup test
Yuki Kimoto authored on 2011-08-15
264
@rows = ();
265
while (my $row = $result->fetch) {
266
    push @rows, [@$row];
267
}
268
is_deeply(\@rows, [[1, 2], [3, 4]], "fetch");
cleanup
Yuki Kimoto authored on 2011-08-15
269

            
cleanup test
Yuki Kimoto authored on 2011-08-15
270
$result = $dbi->execute($query);
271
@rows = ();
272
while (my $row = $result->fetch_hash) {
273
    push @rows, {%$row};
274
}
275
is_deeply(\@rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], "fetch_hash");
test cleanup
Yuki Kimoto authored on 2011-08-10
276

            
cleanup test
Yuki Kimoto authored on 2011-08-15
277
$result = $dbi->execute($query);
278
$rows = $result->fetch_all;
279
is_deeply($rows, [[1, 2], [3, 4]], "fetch_all");
test cleanup
Yuki Kimoto authored on 2011-08-10
280

            
cleanup test
Yuki Kimoto authored on 2011-08-15
281
$result = $dbi->execute($query);
282
$rows = $result->fetch_hash_all;
283
is_deeply($rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], "all");
test cleanup
Yuki Kimoto authored on 2011-08-10
284

            
cleanup test
Yuki Kimoto authored on 2011-08-15
285
test 'Insert query return value';
286
$source = "insert into $table1 {insert_param $key1 $key2}";
287
$query = $dbi->execute($source, {}, query => 1);
288
$ret_val = $dbi->execute($query, param => {$key1 => 1, $key2 => 2});
289
ok($ret_val);
test cleanup
Yuki Kimoto authored on 2011-08-10
290

            
cleanup test
Yuki Kimoto authored on 2011-08-15
291
test 'Direct query';
292
$dbi->delete_all(table => $table1);
293
$insert_source = "insert into $table1 {insert_param $key1 $key2}";
294
$dbi->execute($insert_source, param => {$key1 => 1, $key2 => 2});
295
$result = $dbi->execute("select * from $table1");
296
$rows = $result->all;
297
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-08
298

            
299
test 'Filter basic';
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
300
$dbi->delete_all(table => $table1);
cleanup test
Yuki Kimoto authored on 2011-08-08
301
$dbi->register_filter(twice       => sub { $_[0] * 2}, 
302
                    three_times => sub { $_[0] * 3});
303

            
cleanup test
Yuki Kimoto authored on 2011-08-15
304
$insert_source  = "insert into $table1 {insert_param $key1 $key2}";
cleanup test
Yuki Kimoto authored on 2011-08-08
305
$insert_query = $dbi->execute($insert_source, {}, query => 1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
306
$insert_query->filter({$key1 => 'twice'});
307
$dbi->execute($insert_query, param => {$key1 => 1, $key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-15
308
$result = $dbi->execute("select * from $table1");
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
309
$rows = $result->filter({$key2 => 'three_times'})->all;
310
is_deeply($rows, [{$key1 => 2, $key2 => 6}], "filter fetch_filter");
cleanup test
Yuki Kimoto authored on 2011-08-08
311

            
312
test 'Filter in';
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
313
$dbi->delete_all(table => $table1);
cleanup test
Yuki Kimoto authored on 2011-08-15
314
$insert_source  = "insert into $table1 {insert_param $key1 $key2}";
cleanup test
Yuki Kimoto authored on 2011-08-08
315
$insert_query = $dbi->execute($insert_source, {}, query => 1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
316
$dbi->execute($insert_query, param => {$key1 => 2, $key2 => 4});
317
$select_source = "select * from $table1 where {in $table1.$key1 2} and {in $table1.$key2 2}";
cleanup test
Yuki Kimoto authored on 2011-08-08
318
$select_query = $dbi->execute($select_source,{}, query => 1);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
319
$select_query->filter({"$table1.$key1" => 'twice'});
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
320
$result = $dbi->execute($select_query, param => {"$table1.$key1" => [1,5], "$table1.$key2" => [2,4]});
cleanup test
Yuki Kimoto authored on 2011-08-08
321
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
322
is_deeply($rows, [{$key1 => 2, $key2 => 4}], "filter");
cleanup test
Yuki Kimoto authored on 2011-08-08
323

            
cleanup test
Yuki Kimoto authored on 2011-08-08
324
test 'DBIx::Custom::SQLTemplate basic tag';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
325
$dbi->execute("drop table $table1");
cleanup test
Yuki Kimoto authored on 2011-08-08
326
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
327
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
328
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
cleanup test
Yuki Kimoto authored on 2011-08-08
329

            
cleanup test
Yuki Kimoto authored on 2011-08-15
330
$source = "select * from $table1 where $key1 = :$key1 and {<> $key2} and {< $key3} and {> $key4} and {>= $key5}";
cleanup test
Yuki Kimoto authored on 2011-08-08
331
$query = $dbi->execute($source, {}, query => 1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
332
$result = $dbi->execute($query, param => {$key1 => 1, $key2 => 3, $key3 => 4, $key4 => 3, $key5 => 5});
cleanup test
Yuki Kimoto authored on 2011-08-08
333
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
334
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}], "basic tag1");
cleanup test
Yuki Kimoto authored on 2011-08-08
335

            
cleanup test
Yuki Kimoto authored on 2011-08-15
336
$source = "select * from $table1 where $key1 = :$key1 and {<> $key2} and {< $key3} and {> $key4} and {>= $key5}";
cleanup test
Yuki Kimoto authored on 2011-08-08
337
$query = $dbi->execute($source, {}, query => 1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
338
$result = $dbi->execute($query, {$key1 => 1, $key2 => 3, $key3 => 4, $key4 => 3, $key5 => 5});
cleanup test
Yuki Kimoto authored on 2011-08-08
339
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
340
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}], "basic tag1");
cleanup test
Yuki Kimoto authored on 2011-08-08
341

            
cleanup test
Yuki Kimoto authored on 2011-08-15
342
$source = "select * from $table1 where {<= $key1} and {like $key2}";
cleanup test
Yuki Kimoto authored on 2011-08-08
343
$query = $dbi->execute($source, {}, query => 1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
344
$result = $dbi->execute($query, param => {$key1 => 1, $key2 => '%2%'});
cleanup test
Yuki Kimoto authored on 2011-08-08
345
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
346
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}], "basic tag2");
cleanup test
Yuki Kimoto authored on 2011-08-08
347

            
348
test 'DIB::Custom::SQLTemplate in tag';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
349
$dbi->execute("drop table $table1");
cleanup test
Yuki Kimoto authored on 2011-08-08
350
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
351
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
352
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
cleanup test
Yuki Kimoto authored on 2011-08-08
353

            
cleanup test
Yuki Kimoto authored on 2011-08-15
354
$source = "select * from $table1 where {in $key1 2}";
cleanup test
Yuki Kimoto authored on 2011-08-08
355
$query = $dbi->execute($source, {}, query => 1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
356
$result = $dbi->execute($query, param => {$key1 => [9, 1]});
cleanup test
Yuki Kimoto authored on 2011-08-08
357
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
358
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}], "basic");
cleanup test
Yuki Kimoto authored on 2011-08-08
359

            
360
test 'DBIx::Custom::SQLTemplate insert tag';
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
361
$dbi->delete_all(table => $table1);
362
$insert_source = "insert into $table1 {insert_param $key1 $key2 $key3 $key4 $key5}";
363
$dbi->execute($insert_source, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
cleanup test
Yuki Kimoto authored on 2011-08-08
364

            
cleanup test
Yuki Kimoto authored on 2011-08-15
365
$result = $dbi->execute("select * from $table1");
cleanup test
Yuki Kimoto authored on 2011-08-08
366
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
367
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}], "basic");
cleanup test
Yuki Kimoto authored on 2011-08-08
368

            
369
test 'DBIx::Custom::SQLTemplate update tag';
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
370
$dbi->delete_all(table => $table1);
371
$insert_source = "insert into $table1 {insert_param $key1 $key2 $key3 $key4 $key5}";
372
$dbi->execute($insert_source, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
373
$dbi->execute($insert_source, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
cleanup test
Yuki Kimoto authored on 2011-08-08
374

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
375
$update_source = "update $table1 {update_param $key1 $key2 $key3 $key4} where {= $key5}";
376
$dbi->execute($update_source, param => {$key1 => 1, $key2 => 1, $key3 => 1, $key4 => 1, $key5 => 5});
cleanup test
Yuki Kimoto authored on 2011-08-08
377

            
cleanup test
Yuki Kimoto authored on 2011-08-15
378
$result = $dbi->execute("select * from $table1 order by $key1");
cleanup test
Yuki Kimoto authored on 2011-08-08
379
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
380
is_deeply($rows, [{$key1 => 1, $key2 => 1, $key3 => 1, $key4 => 1, $key5 => 5},
381
                  {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}], "basic");
cleanup test
Yuki Kimoto authored on 2011-08-08
382

            
cleanup test
Yuki Kimoto authored on 2011-08-08
383
test 'Named placeholder';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
384
$dbi->execute("drop table $table1");
cleanup test
Yuki Kimoto authored on 2011-08-08
385
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
386
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
387
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
cleanup test
Yuki Kimoto authored on 2011-08-08
388

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
389
$source = "select * from $table1 where $key1 = :$key1 and $key2 = :$key2";
390
$result = $dbi->execute($source, param => {$key1 => 1, $key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-08
391
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
392
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-08
393

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
394
$source = "select * from $table1 where $key1 = \n:$key1\n and $key2 = :$key2";
395
$result = $dbi->execute($source, param => {$key1 => 1, $key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-08
396
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
397
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-08
398

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
399
$source = "select * from $table1 where $key1 = :$key1 or $key1 = :$key1";
400
$result = $dbi->execute($source, param => {$key1 => [1, 2]});
cleanup test
Yuki Kimoto authored on 2011-08-08
401
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
402
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-08
403

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
404
$source = "select * from $table1 where $key1 = :$table1.$key1 and $key2 = :$table1.$key2";
cleanup test
Yuki Kimoto authored on 2011-08-08
405
$result = $dbi->execute(
406
    $source,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
407
    param => {"$table1.$key1" => 1, "$table1.$key2" => 1},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
408
    filter => {"$table1.$key2" => sub { $_[0] * 2 }}
cleanup test
Yuki Kimoto authored on 2011-08-08
409
);
410
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
411
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-08
412

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
413
$dbi->execute("drop table $table1");
cleanup test
Yuki Kimoto authored on 2011-08-08
414
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
415
$dbi->insert(table => $table1, param => {$key1 => '2011-10-14 12:19:18', $key2 => 2});
416
$source = "select * from $table1 where $key1 = '2011-10-14 12:19:18' and $key2 = :$key2";
cleanup test
Yuki Kimoto authored on 2011-08-08
417
$result = $dbi->execute(
418
    $source,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
419
    param => {$key2 => 2},
cleanup test
Yuki Kimoto authored on 2011-08-08
420
);
421

            
422
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
423
like($rows->[0]->{$key1}, qr/2011-10-14 12:19:18/);
424
is($rows->[0]->{$key2}, 2);
cleanup test
Yuki Kimoto authored on 2011-08-08
425

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
426
$dbi->delete_all(table => $table1);
427
$dbi->insert(table => $table1, param => {$key1 => 'a:b c:d', $key2 => 2});
428
$source = "select * from $table1 where $key1 = 'a\\:b c\\:d' and $key2 = :$key2";
cleanup test
Yuki Kimoto authored on 2011-08-08
429
$result = $dbi->execute(
430
    $source,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
431
    param => {$key2 => 2},
cleanup test
Yuki Kimoto authored on 2011-08-08
432
);
433
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
434
is_deeply($rows, [{$key1 => 'a:b c:d', $key2 => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-08
435

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
443
test 'insert';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
444
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
445
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
446
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
447
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
cleanup test
Yuki Kimoto authored on 2011-08-15
448
$result = $dbi->execute("select * from $table1");
cleanup test
Yuki Kimoto authored on 2011-08-10
449
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
450
is_deeply($rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], "basic");
cleanup test
Yuki Kimoto authored on 2011-08-10
451

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
452
$dbi->execute("delete from $table1");
cleanup test
Yuki Kimoto authored on 2011-08-10
453
$dbi->register_filter(
454
    twice       => sub { $_[0] * 2 },
455
    three_times => sub { $_[0] * 3 }
456
);
457
$dbi->default_bind_filter('twice');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
458
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}, filter => {$key1 => 'three_times'});
cleanup test
Yuki Kimoto authored on 2011-08-15
459
$result = $dbi->execute("select * from $table1");
cleanup test
Yuki Kimoto authored on 2011-08-10
460
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
461
is_deeply($rows, [{$key1 => 3, $key2 => 4}], "filter");
cleanup test
Yuki Kimoto authored on 2011-08-10
462
$dbi->default_bind_filter(undef);
463

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
464
$dbi->execute("drop table $table1");
cleanup test
Yuki Kimoto authored on 2011-08-10
465
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
466
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}, append => '   ');
467
$rows = $dbi->select(table => $table1)->all;
468
is_deeply($rows, [{$key1 => 1, $key2 => 2}], 'insert append');
cleanup test
Yuki Kimoto authored on 2011-08-10
469

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
470
eval{$dbi->insert(table => $table1, noexist => 1)};
cleanup test
Yuki Kimoto authored on 2011-08-10
471
like($@, qr/noexist/, "invalid");
472

            
473
eval{$dbi->insert(table => 'table', param => {';' => 1})};
474
like($@, qr/safety/);
475

            
cleanup test
Yuki Kimoto authored on 2011-08-10
476
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
477
$dbi->execute($create_table_reserved);
cleanup test
Yuki Kimoto authored on 2011-08-10
478
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
479
$dbi->insert(table => 'table', param => {select => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
480
$result = $dbi->execute("select * from ${q}table$p");
cleanup test
Yuki Kimoto authored on 2011-08-10
481
$rows   = $result->all;
test cleanup
Yuki Kimoto authored on 2011-08-10
482
is_deeply($rows, [{select => 2, update => undef}], "reserved word");
cleanup test
Yuki Kimoto authored on 2011-08-10
483

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
484
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
485
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
486
$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1);
487
$dbi->insert({$key1 => 3, $key2 => 4}, table => $table1);
cleanup test
Yuki Kimoto authored on 2011-08-15
488
$result = $dbi->execute("select * from $table1");
cleanup test
Yuki Kimoto authored on 2011-08-10
489
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
490
is_deeply($rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], "basic");
cleanup test
Yuki Kimoto authored on 2011-08-10
491

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
492
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
493
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
494
$dbi->insert(table => $table1, param => {$key1 => \"'1'", $key2 => 2});
495
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
cleanup test
Yuki Kimoto authored on 2011-08-15
496
$result = $dbi->execute("select * from $table1");
cleanup test
Yuki Kimoto authored on 2011-08-10
497
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
498
is_deeply($rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], "basic");
cleanup test
Yuki Kimoto authored on 2011-08-10
499

            
updated pod
Yuki Kimoto authored on 2011-09-02
500
eval { $dbi->execute("drop table $table1") };
501
$dbi->execute($create_table1);
502
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2},
503
  wrap => {$key1 => sub { "$_[0] - 1" }});
504
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
505
$result = $dbi->execute("select * from $table1");
506
$rows   = $result->all;
507
is_deeply($rows, [{$key1 => 0, $key2 => 2}, {$key1 => 3, $key2 => 4}], "basic");
508

            
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
509
eval { $dbi->execute("drop table $table1") };
510
$dbi->execute($create_table1);
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
511
$dbi->insert_timestamp(
512
    $key1 => '5'
513
);
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
514
$dbi->insert(table => $table1, param => {$key2 => 2}, timestamp => 1);
515
$result = $dbi->execute("select * from $table1");
516
$rows   = $result->all;
517
is_deeply($rows, [{$key1 => 5, $key2 => 2}], "basic");
518

            
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
519
eval { $dbi->execute("drop table $table1") };
520
$dbi->execute($create_table1);
521
$dbi->insert_timestamp(
522
    [$key1, $key2] => sub { 5 }
523
);
524
$dbi->insert(table => $table1, timestamp => 1);
525
$result = $dbi->execute("select * from $table1");
526
$rows   = $result->all;
527
is_deeply($rows, [{$key1 => 5, $key2 => 5}], "basic");
528

            
529
eval { $dbi->execute("drop table $table1") };
530
$dbi->execute($create_table1);
531
$dbi->insert_timestamp(
532
    [$key1, $key2] => sub { "" . DBIx::Custom->new }
533
);
534
$dbi->insert(table => $table1, timestamp => 1);
535
$result = $dbi->execute("select * from $table1");
536
$rows   = $result->all;
537
is($rows->[0]->{$key1}, $rows->[0]->{$key2});
538

            
test cleanup
Yuki Kimoto authored on 2011-08-10
539
test 'update';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
540
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
541
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
542
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
543
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
544
$dbi->update(table => $table1, param => {$key2 => 11}, where => {$key1 => 1});
cleanup test
Yuki Kimoto authored on 2011-08-15
545
$result = $dbi->execute("select * from $table1 order by $key1");
test cleanup
Yuki Kimoto authored on 2011-08-10
546
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
547
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
548
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
549
                  "basic");
550
                  
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
551
$dbi->execute("delete from $table1");
552
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
553
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
554
$dbi->update(table => $table1, param => {$key2 => 12}, where => {$key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-15
555
$result = $dbi->execute("select * from $table1 order by $key1");
test cleanup
Yuki Kimoto authored on 2011-08-10
556
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
557
is_deeply($rows, [{$key1 => 1, $key2 => 12, $key3 => 3, $key4 => 4, $key5 => 5},
558
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
559
                  "update key same as search key");
560

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
561
$dbi->update(table => $table1, param => {$key2 => [12]}, where => {$key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-15
562
$result = $dbi->execute("select * from $table1 order by $key1");
test cleanup
Yuki Kimoto authored on 2011-08-10
563
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
564
is_deeply($rows, [{$key1 => 1, $key2 => 12, $key3 => 3, $key4 => 4, $key5 => 5},
565
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
566
                  "update key same as search key : param is array ref");
567

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
568
$dbi->execute("delete from $table1");
569
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
570
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
test cleanup
Yuki Kimoto authored on 2011-08-10
571
$dbi->register_filter(twice => sub { $_[0] * 2 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
572
$dbi->update(table => $table1, param => {$key2 => 11}, where => {$key1 => 1},
573
              filter => {$key2 => sub { $_[0] * 2 }});
cleanup test
Yuki Kimoto authored on 2011-08-15
574
$result = $dbi->execute("select * from $table1 order by $key1");
test cleanup
Yuki Kimoto authored on 2011-08-10
575
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
576
is_deeply($rows, [{$key1 => 1, $key2 => 22, $key3 => 3, $key4 => 4, $key5 => 5},
577
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
578
                  "filter");
579

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
580
$result = $dbi->update(table => $table1, param => {$key2 => 11}, where => {$key1 => 1}, append => '   ');
test cleanup
Yuki Kimoto authored on 2011-08-10
581

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
582
eval{$dbi->update(table => $table1, where => {$key1 => 1}, noexist => 1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
583
like($@, qr/noexist/, "invalid");
584

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
585
eval{$dbi->update(table => $table1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
586
like($@, qr/where/, "not contain where");
587

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
588
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
589
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
590
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
591
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
592
$where->clause(['and', "$key1 = :$key1", "$key2 = :$key2"]);
593
$where->param({$key1 => 1, $key2 => 2});
594
$dbi->update(table => $table1, param => {$key1 => 3}, where => $where);
595
$result = $dbi->select(table => $table1);
596
is_deeply($result->all, [{$key1 => 3, $key2 => 2}], 'update() where');
test cleanup
Yuki Kimoto authored on 2011-08-10
597

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
598
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
599
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
600
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
601
$dbi->update(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
602
    table => $table1,
603
    param => {$key1 => 3},
test cleanup
Yuki Kimoto authored on 2011-08-10
604
    where => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
605
        ['and', "$key1 = :$key1", "$key2 = :$key2"],
606
        {$key1 => 1, $key2 => 2}
test cleanup
Yuki Kimoto authored on 2011-08-10
607
    ]
608
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
609
$result = $dbi->select(table => $table1);
610
is_deeply($result->all, [{$key1 => 3, $key2 => 2}], 'update() where');
test cleanup
Yuki Kimoto authored on 2011-08-10
611

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
612
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
613
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
614
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
615
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
616
$where->clause(['and', "$key2 = :$key2"]);
617
$where->param({$key2 => 2});
618
$dbi->update(table => $table1, param => {$key1 => 3}, where => $where);
619
$result = $dbi->select(table => $table1);
620
is_deeply($result->all, [{$key1 => 3, $key2 => 2}], 'update() where');
test cleanup
Yuki Kimoto authored on 2011-08-10
621

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
622
eval{$dbi->update(table => $table1, param => {';' => 1})};
test cleanup
Yuki Kimoto authored on 2011-08-10
623
like($@, qr/safety/);
624

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
625
eval{$dbi->update(table => $table1, param => {$key1 => 1}, where => {';' => 1})};
test cleanup
Yuki Kimoto authored on 2011-08-10
626
like($@, qr/safety/);
627

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
628
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
629
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
630
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
631
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
632
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
633
$dbi->insert(table => 'table', param => {select => 1});
634
$dbi->update(table => 'table', where => {select => 1}, param => {update => 2});
635
$result = $dbi->execute("select * from ${q}table$p");
636
$rows   = $result->all;
637
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
638

            
639
eval {$dbi->update_all(table => 'table', param => {';' => 2}) };
640
like($@, qr/safety/);
641

            
642
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
643
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
644
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
645
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
646
$dbi->insert(table => 'table', param => {select => 1});
647
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
648
$result = $dbi->execute("select * from ${q}table$p");
649
$rows   = $result->all;
650
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
651

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
652
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
653
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
654
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
655
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
656
$dbi->update({$key2 => 11}, table => $table1, where => {$key1 => 1});
cleanup test
Yuki Kimoto authored on 2011-08-15
657
$result = $dbi->execute("select * from $table1 order by $key1");
test cleanup
Yuki Kimoto authored on 2011-08-10
658
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
659
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
660
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
661
                  "basic");
662

            
updated pod
Yuki Kimoto authored on 2011-09-02
663
eval { $dbi->execute("drop table $table1") };
664
$dbi->execute($create_table1_2);
665
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
666
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
667
$dbi->update(table => $table1, param => {$key2 => 11}, where => {$key1 => 1},
668
wrap => {$key2 => sub { "$_[0] - 1" }});
669
$result = $dbi->execute("select * from $table1 order by $key1");
670
$rows   = $result->all;
671
is_deeply($rows, [{$key1 => 1, $key2 => 10, $key3 => 3, $key4 => 4, $key5 => 5},
672
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
673
                  "basic");
674

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
675
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
676
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
677
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
678
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
679
$dbi->update(table => $table1, param => {$key2 => \"'11'"}, where => {$key1 => 1});
cleanup test
Yuki Kimoto authored on 2011-08-15
680
$result = $dbi->execute("select * from $table1 order by $key1");
test cleanup
Yuki Kimoto authored on 2011-08-10
681
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
682
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
683
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
684
                  "basic");
685

            
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
686
eval { $dbi->execute("drop table $table1") };
687
$dbi->execute($create_table1);
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
688
$dbi->update_timestamp(
689
    $key1 => '5'
690
);
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
691
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
692
$dbi->update(table => $table1, timestamp => 1, where => {$key2 => 2});
693
$result = $dbi->execute("select * from $table1");
694
$rows   = $result->all;
695
is_deeply($rows, [{$key1 => 5, $key2 => 2}], "basic");
696

            
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
697
eval { $dbi->execute("drop table $table1") };
698
$dbi->execute($create_table1);
699
$dbi->update_timestamp(
700
    [$key1, $key2] => sub { '5' }
701
);
702
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
703
$dbi->update_all(table => $table1, timestamp => 1);
704
$result = $dbi->execute("select * from $table1");
705
$rows   = $result->all;
706
is_deeply($rows, [{$key1 => 5, $key2 => 5}], "basic");
707

            
708
eval { $dbi->execute("drop table $table1") };
709
$dbi->execute($create_table1);
710
$dbi->update_timestamp(
711
    [$key1, $key2] => sub { "" . DBIx::Custom->new }
712
);
713
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
714
$dbi->update_all(table => $table1, timestamp => 1);
715
$result = $dbi->execute("select * from $table1");
716
$rows   = $result->all;
717
is($rows->[0]->{$key1}, $rows->[0]->{$key2});
718

            
test cleanup
Yuki Kimoto authored on 2011-08-10
719
test 'update_all';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
720
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
721
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
722
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
723
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
test cleanup
Yuki Kimoto authored on 2011-08-10
724
$dbi->register_filter(twice => sub { $_[0] * 2 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
725
$dbi->update_all(table => $table1, param => {$key2 => 10}, filter => {$key2 => 'twice'});
cleanup test
Yuki Kimoto authored on 2011-08-15
726
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
727
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
728
is_deeply($rows, [{$key1 => 1, $key2 => 20, $key3 => 3, $key4 => 4, $key5 => 5},
729
                  {$key1 => 6, $key2 => 20, $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
730
                  "filter");
731

            
732

            
733
test 'delete';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
734
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
735
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
736
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
737
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
738
$dbi->delete(table => $table1, where => {$key1 => 1});
cleanup test
Yuki Kimoto authored on 2011-08-15
739
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
740
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
741
is_deeply($rows, [{$key1 => 3, $key2 => 4}], "basic");
test cleanup
Yuki Kimoto authored on 2011-08-10
742

            
cleanup test
Yuki Kimoto authored on 2011-08-15
743
$dbi->execute("delete from $table1");
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
744
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
745
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-10
746
$dbi->register_filter(twice => sub { $_[0] * 2 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
747
$dbi->delete(table => $table1, where => {$key2 => 1}, filter => {$key2 => 'twice'});
cleanup test
Yuki Kimoto authored on 2011-08-15
748
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
749
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
750
is_deeply($rows, [{$key1 => 3, $key2 => 4}], "filter");
test cleanup
Yuki Kimoto authored on 2011-08-10
751

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
752
$dbi->delete(table => $table1, where => {$key1 => 1}, append => '   ');
test cleanup
Yuki Kimoto authored on 2011-08-10
753

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
754
$dbi->delete_all(table => $table1);
755
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
756
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
757
$dbi->delete(table => $table1, where => {$key1 => 1, $key2 => 2});
758
$rows = $dbi->select(table => $table1)->all;
759
is_deeply($rows, [{$key1 => 3, $key2 => 4}], "delete multi key");
test cleanup
Yuki Kimoto authored on 2011-08-10
760

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
761
eval{$dbi->delete(table => $table1, where => {$key1 => 1}, noexist => 1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
762
like($@, qr/noexist/, "invalid");
763

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
764
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
765
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
766
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
767
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-10
768
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
769
$where->clause(['and', "$key1 = :$key1", "$key2 = :$key2"]);
770
$where->param({ke1 => 1, $key2 => 2});
771
$dbi->delete(table => $table1, where => $where);
772
$result = $dbi->select(table => $table1);
773
is_deeply($result->all, [{$key1 => 3, $key2 => 4}], 'delete() where');
test cleanup
Yuki Kimoto authored on 2011-08-10
774

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
775
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
776
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
777
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
778
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-10
779
$dbi->delete(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
780
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
781
    where => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
782
        ['and', "$key1 = :$key1", "$key2 = :$key2"],
783
        {ke1 => 1, $key2 => 2}
test cleanup
Yuki Kimoto authored on 2011-08-10
784
    ]
785
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
786
$result = $dbi->select(table => $table1);
787
is_deeply($result->all, [{$key1 => 3, $key2 => 4}], 'delete() where');
test cleanup
Yuki Kimoto authored on 2011-08-10
788

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
789
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
790
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
791
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
792
$dbi->delete(table => $table1, where => {$key1 => 1}, prefix => '    ');
cleanup test
Yuki Kimoto authored on 2011-08-15
793
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
794
$rows   = $result->all;
795
is_deeply($rows, [], "basic");
796

            
797
test 'delete error';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
798
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
799
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
800
eval{$dbi->delete(table => $table1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
801
like($@, qr/"where" must be specified/,
802
         "where key-value pairs not specified");
803

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
804
eval{$dbi->delete(table => $table1, where => {';' => 1})};
test cleanup
Yuki Kimoto authored on 2011-08-10
805
like($@, qr/safety/);
806

            
test cleanup
Yuki Kimoto authored on 2011-08-10
807
$dbi = undef;
test cleanup
Yuki Kimoto authored on 2011-08-10
808
$dbi = DBIx::Custom->connect;
809
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
810
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
811
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
812
$dbi->insert(table => 'table', param => {select => 1});
813
$dbi->delete(table => 'table', where => {select => 1});
814
$result = $dbi->execute("select * from ${q}table$p");
815
$rows   = $result->all;
816
is_deeply($rows, [], "reserved word");
817

            
818
test 'delete_all';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
819
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
820
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
821
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
822
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
823
$dbi->delete_all(table => $table1);
cleanup test
Yuki Kimoto authored on 2011-08-15
824
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
825
$rows   = $result->all;
826
is_deeply($rows, [], "basic");
827

            
828

            
829
test 'select';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
830
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
831
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
832
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
833
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
834
$rows = $dbi->select(table => $table1)->all;
835
is_deeply($rows, [{$key1 => 1, $key2 => 2},
836
                  {$key1 => 3, $key2 => 4}], "table");
test cleanup
Yuki Kimoto authored on 2011-08-10
837

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
838
$rows = $dbi->select(table => $table1, column => [$key1])->all;
839
is_deeply($rows, [{$key1 => 1}, {$key1 => 3}], "table and columns and where key");
test cleanup
Yuki Kimoto authored on 2011-08-10
840

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
841
$rows = $dbi->select(table => $table1, where => {$key1 => 1})->all;
842
is_deeply($rows, [{$key1 => 1, $key2 => 2}], "table and columns and where key");
test cleanup
Yuki Kimoto authored on 2011-08-10
843

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
844
$rows = $dbi->select(table => $table1, column => [$key1], where => {$key1 => 3})->all;
845
is_deeply($rows, [{$key1 => 3}], "table and columns and where key");
test cleanup
Yuki Kimoto authored on 2011-08-10
846

            
847
$dbi->register_filter(decrement => sub { $_[0] - 1 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
848
$rows = $dbi->select(table => $table1, where => {$key1 => 2}, filter => {$key1 => 'decrement'})
test cleanup
Yuki Kimoto authored on 2011-08-10
849
            ->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
850
is_deeply($rows, [{$key1 => 1, $key2 => 2}], "filter");
test cleanup
Yuki Kimoto authored on 2011-08-10
851

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
852
eval { $dbi->execute("drop table $table2") };
test cleanup
Yuki Kimoto authored on 2011-08-10
853
$dbi->execute($create_table2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
854
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
test cleanup
Yuki Kimoto authored on 2011-08-10
855
$rows = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
856
    table => [$table1, $table2],
857
    column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
858
    where   => {"$table1.$key2" => 2},
859
    relation  => {"$table1.$key1" => "$table2.$key1"}
test cleanup
Yuki Kimoto authored on 2011-08-10
860
)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
861
is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}], "relation : exists where");
test cleanup
Yuki Kimoto authored on 2011-08-10
862

            
863
$rows = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
864
    table => [$table1, $table2],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
865
    column => ["$table1.$key1 as ${table1}_$key1", "${table2}.$key1 as ${table2}_$key1", $key2, $key3],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
866
    relation  => {"$table1.$key1" => "$table2.$key1"}
test cleanup
Yuki Kimoto authored on 2011-08-10
867
)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
868
is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}], "relation : no exists where");
test cleanup
Yuki Kimoto authored on 2011-08-10
869

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
870
eval{$dbi->select(table => $table1, noexist => 1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
871
like($@, qr/noexist/, "invalid");
872

            
873
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
874
eval { $dbi->execute("drop table ${q}table$p") };
875
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
876
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
877
$dbi->insert(table => 'table', param => {select => 1, update => 2});
878
$result = $dbi->select(table => 'table', where => {select => 1});
879
$rows   = $result->all;
880
is_deeply($rows, [{select => 2, update => 2}], "reserved word");
881

            
882
test 'fetch filter';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
883
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
884
$dbi->register_filter(
885
    twice       => sub { $_[0] * 2 },
886
    three_times => sub { $_[0] * 3 }
887
);
888
$dbi->default_fetch_filter('twice');
889
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
890
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
891
$result = $dbi->select(table => $table1);
892
$result->filter({$key1 => 'three_times'});
test cleanup
Yuki Kimoto authored on 2011-08-10
893
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
894
is_deeply($row, {$key1 => 3, $key2 => 4}, "default_fetch_filter and filter");
test cleanup
Yuki Kimoto authored on 2011-08-10
895

            
896
test 'filters';
897
$dbi = DBIx::Custom->new;
898

            
899
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')),
900
   'あ', "decode_utf8");
901

            
902
is($dbi->filters->{encode_utf8}->('あ'),
903
   encode_utf8('あ'), "encode_utf8");
904

            
cleanup test
Yuki Kimoto authored on 2011-08-10
905
test 'transaction1';
test cleanup
Yuki Kimoto authored on 2011-08-10
906
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
907
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
908
$dbi->execute($create_table1);
fixed transaction test
Yuki Kimoto authored on 2011-08-14
909
$dbi->begin_work;
910
$dbi->dbh->{AutoCommit} = 0;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
911
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
fixed transaction test
Yuki Kimoto authored on 2011-08-14
912
$dbi->rollback;
913
$dbi->dbh->{AutoCommit} = 1;
914

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
915
$result = $dbi->select(table => $table1);
fixed transaction test
Yuki Kimoto authored on 2011-08-14
916
ok(! $result->fetch_first, "rollback");
917

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

            
919
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
920
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
921
$dbi->execute($create_table1);
fixed transaction test
Yuki Kimoto authored on 2011-08-14
922
$dbi->begin_work;
923
$dbi->dbh->{AutoCommit} = 0;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
924
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
925
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
fixed transaction test
Yuki Kimoto authored on 2011-08-14
926
$dbi->commit;
927
$dbi->dbh->{AutoCommit} = 1;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
928
$result = $dbi->select(table => $table1);
929
is_deeply(scalar $result->all, [{$key1 => 1, $key2 => 2}, {$key1 => 2, $key2 => 3}],
fixed transaction test
Yuki Kimoto authored on 2011-08-14
930
          "commit");
test cleanup
Yuki Kimoto authored on 2011-08-10
931

            
932
test 'execute';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
933
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
934
$dbi->execute($create_table1);
935
{
936
    local $Carp::Verbose = 0;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
937
    eval{$dbi->execute("select * frm $table1")};
cleanup test
Yuki Kimoto authored on 2011-08-15
938
    like($@, qr/\Qselect * frm $table1/, "fail prepare");
test cleanup
Yuki Kimoto authored on 2011-08-10
939
    like($@, qr/\.t /, "fail : not verbose");
940
}
941
{
942
    local $Carp::Verbose = 1;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
943
    eval{$dbi->execute("select * frm $table1")};
test cleanup
Yuki Kimoto authored on 2011-08-10
944
    like($@, qr/Custom.*\.t /s, "fail : verbose");
945
}
946

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
947
eval{$dbi->execute('select * from $table1', no_exists => 1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
948
like($@, qr/wrong/, "invald SQL");
949

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
950
$query = $dbi->execute("select * from $table1 where $key1 = :$key1", {}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
951
$dbi->dbh->disconnect;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
952
eval{$dbi->execute($query, param => {$key1 => {a => 1}})};
test cleanup
Yuki Kimoto authored on 2011-08-10
953
ok($@, "execute fail");
954

            
955
{
956
    local $Carp::Verbose = 0;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
957
    eval{$dbi->execute("select * from $table1 where {0 $key1}", {}, query => 1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
958
    like($@, qr/\Q.t /, "caller spec : not vebose");
959
}
960
{
961
    local $Carp::Verbose = 1;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
962
    eval{$dbi->execute("select * from $table1 where {0 $key1}", {}, query => 1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
963
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
964
}
965

            
966

            
cleanup test
Yuki Kimoto authored on 2011-08-10
967
test 'transaction2';
test cleanup
Yuki Kimoto authored on 2011-08-10
968
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
969
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
970
$dbi->execute($create_table1);
971

            
972
$dbi->begin_work;
973

            
974
eval {
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
975
    $dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
976
    die "Error";
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
977
    $dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-10
978
};
979

            
980
$dbi->rollback if $@;
981

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
982
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
983
$rows = $result->all;
984
is_deeply($rows, [], "rollback");
985

            
986
$dbi->begin_work;
987

            
988
eval {
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
989
    $dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
990
    $dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-10
991
};
992

            
993
$dbi->commit unless $@;
994

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
995
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
996
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
997
is_deeply($rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], "commit");
test cleanup
Yuki Kimoto authored on 2011-08-10
998

            
999
$dbi->dbh->{AutoCommit} = 0;
1000
eval{ $dbi->begin_work };
1001
ok($@, "exception");
1002
$dbi->dbh->{AutoCommit} = 1;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
1003

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1004
test 'cache';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1005
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1006
$dbi->cache(1);
1007
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-15
1008
$source = "select * from $table1 where $key1 = :$key1 and $key2 = :$key2";
test cleanup
Yuki Kimoto authored on 2011-08-10
1009
$dbi->execute($source, {}, query => 1);
1010
is_deeply($dbi->{_cached}->{$source}, 
cleanup test
Yuki Kimoto authored on 2011-08-15
1011
          {sql => "select * from $table1 where $key1 = ? and $key2 = ?", columns => [$key1, $key2], tables => []}, "cache");
test cleanup
Yuki Kimoto authored on 2011-08-10
1012

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1013
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1014
$dbi->execute($create_table1);
1015
$dbi->{_cached} = {};
1016
$dbi->cache(0);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1017
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
1018
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
1019

            
1020
test 'execute';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1021
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1022
$dbi->execute($create_table1);
1023
{
1024
    local $Carp::Verbose = 0;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1025
    eval{$dbi->execute("select * frm $table1")};
cleanup test
Yuki Kimoto authored on 2011-08-15
1026
    like($@, qr/\Qselect * frm $table1/, "fail prepare");
test cleanup
Yuki Kimoto authored on 2011-08-10
1027
    like($@, qr/\.t /, "fail : not verbose");
1028
}
1029
{
1030
    local $Carp::Verbose = 1;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1031
    eval{$dbi->execute('select * frm $table1')};
test cleanup
Yuki Kimoto authored on 2011-08-10
1032
    like($@, qr/Custom.*\.t /s, "fail : verbose");
1033
}
1034

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1035
eval{$dbi->execute('select * from $table1', no_exists => 1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
1036
like($@, qr/wrong/, "invald SQL");
1037

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1038
$query = $dbi->execute("select * from $table1 where $key1 = :$key1", {}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1039
$dbi->dbh->disconnect;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1040
eval{$dbi->execute($query, param => {$key1 => {a => 1}})};
test cleanup
Yuki Kimoto authored on 2011-08-10
1041
ok($@, "execute fail");
1042

            
1043
{
1044
    local $Carp::Verbose = 0;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1045
    eval{$dbi->execute("select * from $table1 where {0 $key1}", {}, query => 1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
1046
    like($@, qr/\Q.t /, "caller spec : not vebose");
1047
}
1048
{
1049
    local $Carp::Verbose = 1;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1050
    eval{$dbi->execute("select * from $table1 where {0 $key1}", {}, query => 1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
1051
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
1052
}
1053

            
1054
test 'method';
1055
$dbi->method(
1056
    one => sub { 1 }
1057
);
1058
$dbi->method(
1059
    two => sub { 2 }
1060
);
1061
$dbi->method({
1062
    twice => sub {
1063
        my $self = shift;
1064
        return $_[0] * 2;
1065
    }
1066
});
1067

            
1068
is($dbi->one, 1, "first");
1069
is($dbi->two, 2, "second");
1070
is($dbi->twice(5), 10 , "second");
1071

            
1072
eval {$dbi->XXXXXX};
1073
ok($@, "not exists");
1074

            
1075
test 'out filter';
1076
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1077
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1078
$dbi->execute($create_table1);
1079
$dbi->register_filter(twice => sub { $_[0] * 2 });
1080
$dbi->register_filter(three_times => sub { $_[0] * 3});
1081
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1082
    $table1, $key1 => {out => 'twice', in => 'three_times'}, 
1083
              $key2 => {out => 'three_times', in => 'twice'});
1084
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-15
1085
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
1086
$row   = $result->fetch_hash_first;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1087
is_deeply($row, {$key1 => 2, $key2 => 6}, "insert");
1088
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1089
$row   = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1090
is_deeply($row, {$key1 => 6, $key2 => 12}, "insert");
test cleanup
Yuki Kimoto authored on 2011-08-10
1091

            
1092
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1093
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1094
$dbi->execute($create_table1);
1095
$dbi->register_filter(twice => sub { $_[0] * 2 });
1096
$dbi->register_filter(three_times => sub { $_[0] * 3});
1097
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1098
    $table1, $key1 => {out => 'twice', in => 'three_times'}, 
1099
              $key2 => {out => 'three_times', in => 'twice'});
test cleanup
Yuki Kimoto authored on 2011-08-10
1100
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1101
    $table1, $key1 => {out => undef}
test cleanup
Yuki Kimoto authored on 2011-08-10
1102
); 
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1103
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-15
1104
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
1105
$row   = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1106
is_deeply($row, {$key1 => 1, $key2 => 6}, "insert");
test cleanup
Yuki Kimoto authored on 2011-08-10
1107

            
1108
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1109
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1110
$dbi->execute($create_table1);
1111
$dbi->register_filter(twice => sub { $_[0] * 2 });
1112
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1113
    $table1, $key1 => {out => 'twice', in => 'twice'}
test cleanup
Yuki Kimoto authored on 2011-08-10
1114
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1115
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}, filter => {$key1 => undef});
1116
$dbi->update(table => $table1, param => {$key1 => 2}, where => {$key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-15
1117
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
1118
$row   = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1119
is_deeply($row, {$key1 => 4, $key2 => 2}, "update");
test cleanup
Yuki Kimoto authored on 2011-08-10
1120

            
1121
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1122
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1123
$dbi->execute($create_table1);
1124
$dbi->register_filter(twice => sub { $_[0] * 2 });
1125
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1126
    $table1, $key1 => {out => 'twice', in => 'twice'}
test cleanup
Yuki Kimoto authored on 2011-08-10
1127
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1128
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 2}, filter => {$key1=> undef});
1129
$dbi->delete(table => $table1, where => {$key1 => 1});
cleanup test
Yuki Kimoto authored on 2011-08-15
1130
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
1131
$rows   = $result->all;
1132
is_deeply($rows, [], "delete");
1133

            
1134
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1135
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1136
$dbi->execute($create_table1);
1137
$dbi->register_filter(twice => sub { $_[0] * 2 });
1138
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1139
    $table1, $key1 => {out => 'twice', in => 'twice'}
test cleanup
Yuki Kimoto authored on 2011-08-10
1140
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1141
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 2}, filter => {$key1 => undef});
1142
$result = $dbi->select(table => $table1, where => {$key1 => 1});
1143
$result->filter({$key2 => 'twice'});
test cleanup
Yuki Kimoto authored on 2011-08-10
1144
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1145
is_deeply($rows, [{$key1 => 4, $key2 => 4}], "select");
test cleanup
Yuki Kimoto authored on 2011-08-10
1146

            
1147
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1148
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1149
$dbi->execute($create_table1);
1150
$dbi->register_filter(twice => sub { $_[0] * 2 });
1151
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1152
    $table1, $key1 => {out => 'twice', in => 'twice'}
test cleanup
Yuki Kimoto authored on 2011-08-10
1153
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1154
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 2}, filter => {$key1 => undef});
cleanup test
Yuki Kimoto authored on 2011-08-15
1155
$result = $dbi->execute("select * from $table1 where $key1 = :$key1 and $key2 = :$key2",
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1156
                        param => {$key1 => 1, $key2 => 2},
1157
                        table => [$table1]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1158
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1159
is_deeply($rows, [{$key1 => 4, $key2 => 2}], "execute");
test cleanup
Yuki Kimoto authored on 2011-08-10
1160

            
1161
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1162
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1163
$dbi->execute($create_table1);
1164
$dbi->register_filter(twice => sub { $_[0] * 2 });
1165
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1166
    $table1, $key1 => {out => 'twice', in => 'twice'}
test cleanup
Yuki Kimoto authored on 2011-08-10
1167
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1168
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 2}, filter => {$key1 => undef});
cleanup test
Yuki Kimoto authored on 2011-08-15
1169
$result = $dbi->execute("select * from {table $table1} where $key1 = :$key1 and $key2 = :$key2",
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1170
                        param => {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
1171
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1172
is_deeply($rows, [{$key1 => 4, $key2 => 2}], "execute table tag");
test cleanup
Yuki Kimoto authored on 2011-08-10
1173

            
1174
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1175
eval { $dbi->execute("drop table $table1") };
1176
eval { $dbi->execute("drop table $table2") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1177
$dbi->execute($create_table1);
1178
$dbi->execute($create_table2);
1179
$dbi->register_filter(twice => sub { $_[0] * 2 });
1180
$dbi->register_filter(three_times => sub { $_[0] * 3 });
1181
$dbi->apply_filter(
cleanup test
Yuki Kimoto authored on 2011-08-15
1182
    $table1, $key2 => {out => 'twice', in => 'twice'}
1183
);
1184
$dbi->apply_filter(
1185
    $table2, $key3 => {out => 'three_times', in => 'three_times'}
test cleanup
Yuki Kimoto authored on 2011-08-10
1186
);
cleanup test
Yuki Kimoto authored on 2011-08-15
1187
$dbi->insert(table => $table1, param => {$key1 => 5, $key2 => 2}, filter => {$key2 => undef});
1188
$dbi->insert(table => $table2, param => {$key1 => 5, $key3 => 6}, filter => {$key3 => undef});
1189
$result = $dbi->select(
1190
     table => [$table1, $table2],
1191
     column => [$key2, $key3],
1192
     where => {"$table1.$key2" => 1, "$table2.$key3" => 2}, relation => {"$table1.$key1" => "$table2.$key1"});
1193

            
1194
$result->filter({$key2 => 'twice'});
1195
$rows   = $result->all;
1196
is_deeply($rows, [{$key2 => 4, $key3 => 18}], "select : join");
1197

            
1198
$result = $dbi->select(
1199
     table => [$table1, $table2],
1200
     column => [$key2, $key3],
1201
     where => {$key2 => 1, $key3 => 2}, relation => {"$table1.$key1" => "$table2.$key1"});
1202

            
1203
$result->filter({$key2 => 'twice'});
1204
$rows   = $result->all;
1205
is_deeply($rows, [{$key2 => 4, $key3 => 18}], "select : join : omit");
test cleanup
Yuki Kimoto authored on 2011-08-10
1206

            
1207
test 'connect super';
test cleanup
Yuki Kimoto authored on 2011-08-10
1208
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1209
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1210
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1211
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1212
is($dbi->select(table => $table1)->one->{$key1}, 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1213

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1214
$dbi = DBIx::Custom->new;
test cleanup
Yuki Kimoto authored on 2011-08-10
1215
$dbi->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1216
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1217
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1218
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1219
is($dbi->select(table => $table1)->one->{$key1}, 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1220

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1221
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1222
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1223
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1224
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1225
is($dbi->select(table => $table1)->one->{$key1}, 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1226

            
1227
test 'end_filter';
1228
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1229
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1230
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1231
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1232
$result = $dbi->select(table => $table1);
1233
$result->filter($key1 => sub { $_[0] * 2 }, $key2 => sub { $_[0] * 4 });
1234
$result->end_filter($key1 => sub { $_[0] * 3 }, $key2 => sub { $_[0] * 5 });
test cleanup
Yuki Kimoto authored on 2011-08-10
1235
$row = $result->fetch_first;
1236
is_deeply($row, [6, 40]);
1237

            
1238
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1239
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1240
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1241
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1242
$result = $dbi->select(table => $table1);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1243
$result->filter([$key1, $key2] => sub { $_[0] * 2 });
1244
$result->end_filter([[$key1, $key2] => sub { $_[0] * 3 }]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1245
$row = $result->fetch_first;
1246
is_deeply($row, [6, 12]);
1247

            
1248
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1249
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1250
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1251
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1252
$result = $dbi->select(table => $table1);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1253
$result->filter([[$key1, $key2] => sub { $_[0] * 2 }]);
1254
$result->end_filter([$key1, $key2] => sub { $_[0] * 3 });
test cleanup
Yuki Kimoto authored on 2011-08-10
1255
$row = $result->fetch_first;
1256
is_deeply($row, [6, 12]);
1257

            
1258
$dbi->register_filter(five_times => sub { $_[0] * 5 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1259
$result = $dbi->select(table => $table1);
1260
$result->filter($key1 => sub { $_[0] * 2 }, $key2 => sub { $_[0] * 4 });
1261
$result->end_filter({$key1 => sub { $_[0] * 3 }, $key2 => 'five_times' });
test cleanup
Yuki Kimoto authored on 2011-08-10
1262
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1263
is_deeply($row, {$key1 => 6, $key2 => 40});
test cleanup
Yuki Kimoto authored on 2011-08-10
1264

            
1265
$dbi->register_filter(five_times => sub { $_[0] * 5 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1266
$dbi->apply_filter($table1,
1267
    $key1 => {end => sub { $_[0] * 3 } },
1268
    $key2 => {end => 'five_times'}
test cleanup
Yuki Kimoto authored on 2011-08-10
1269
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1270
$result = $dbi->select(table => $table1);
1271
$result->filter($key1 => sub { $_[0] * 2 }, $key2 => sub { $_[0] * 4 });
test cleanup
Yuki Kimoto authored on 2011-08-10
1272
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1273
is_deeply($row, {$key1 => 6, $key2 => 40}, 'apply_filter');
test cleanup
Yuki Kimoto authored on 2011-08-10
1274

            
1275
$dbi->register_filter(five_times => sub { $_[0] * 5 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1276
$dbi->apply_filter($table1,
1277
    $key1 => {end => sub { $_[0] * 3 } },
1278
    $key2 => {end => 'five_times'}
1279
);
1280
$result = $dbi->select(table => $table1);
1281
$result->filter($key1 => sub { $_[0] * 2 }, $key2 => sub { $_[0] * 4 });
1282
$result->filter($key1 => undef);
1283
$result->end_filter($key1 => undef);
test cleanup
Yuki Kimoto authored on 2011-08-10
1284
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1285
is_deeply($row, {$key1 => 1, $key2 => 40}, 'apply_filter overwrite');
test cleanup
Yuki Kimoto authored on 2011-08-10
1286

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1287
test 'remove_end_filter and remove_filter';
1288
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1289
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1290
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1291
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1292
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1293
$row = $result
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1294
       ->filter($key1 => sub { $_[0] * 2 }, $key2 => sub { $_[0] * 4 })
test cleanup
Yuki Kimoto authored on 2011-08-10
1295
       ->remove_filter
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1296
       ->end_filter($key1 => sub { $_[0] * 3 }, $key2 => sub { $_[0] * 5 })
test cleanup
Yuki Kimoto authored on 2011-08-10
1297
       ->remove_end_filter
1298
       ->fetch_first;
1299
is_deeply($row, [1, 2]);
1300

            
1301
test 'empty where select';
1302
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1303
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1304
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1305
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1306
$result = $dbi->select(table => $table1, where => {});
test cleanup
Yuki Kimoto authored on 2011-08-10
1307
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1308
is_deeply($row, {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
1309

            
1310
test 'select query option';
1311
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1312
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1313
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1314
$query = $dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1315
is(ref $query, 'DBIx::Custom::Query');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1316
$query = $dbi->update(table => $table1, where => {$key1 => 1}, param => {$key2 => 2}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1317
is(ref $query, 'DBIx::Custom::Query');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1318
$query = $dbi->delete(table => $table1, where => {$key1 => 1}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1319
is(ref $query, 'DBIx::Custom::Query');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1320
$query = $dbi->select(table => $table1, where => {$key1 => 1, $key2 => 2}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1321
is(ref $query, 'DBIx::Custom::Query');
1322

            
1323
test 'where';
1324
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1325
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1326
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1327
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1328
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
1329
$where = $dbi->where->clause(['and', "$key1 = :$key1", "$key2 = :$key2"]);
1330
is("$where", "where ( $key1 = :$key1 and $key2 = :$key2 )", 'no param');
test cleanup
Yuki Kimoto authored on 2011-08-10
1331

            
1332
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1333
             ->clause(['and', "$key1 = :$key1", "$key2 = :$key2"])
1334
             ->param({$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1335

            
1336
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1337
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1338
    where => $where
1339
);
1340
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1341
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1342

            
1343
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1344
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1345
    where => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1346
        ['and', "$key1 = :$key1", "$key2 = :$key2"],
1347
        {$key1 => 1}
test cleanup
Yuki Kimoto authored on 2011-08-10
1348
    ]
1349
);
1350
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1351
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1352

            
1353
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1354
             ->clause(['and', "$key1 = :$key1", "$key2 = :$key2"])
1355
             ->param({$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
1356
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1357
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1358
    where => $where
1359
);
1360
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1361
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1362

            
1363
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1364
             ->clause(['and', "$key1 = :$key1", "$key2 = :$key2"])
test cleanup
Yuki Kimoto authored on 2011-08-10
1365
             ->param({});
1366
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1367
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1368
    where => $where,
1369
);
1370
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1371
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1372

            
1373
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1374
             ->clause(['and', ['or', "$key1 > :$key1", "$key1 < :$key1"], "$key2 = :$key2"])
1375
             ->param({$key1 => [0, 3], $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
1376
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1377
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1378
    where => $where,
1379
); 
1380
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1381
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1382

            
1383
$where = $dbi->where;
1384
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1385
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1386
    where => $where
1387
);
1388
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1389
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1390

            
1391
eval {
1392
$where = $dbi->where
1393
             ->clause(['uuu']);
1394
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1395
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1396
    where => $where
1397
);
1398
};
1399
ok($@);
1400

            
1401
$where = $dbi->where;
1402
is("$where", '');
1403

            
1404
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1405
             ->clause(['or', ("$key1 = :$key1") x 2])
1406
             ->param({$key1 => [1, 3]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1407
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1408
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1409
    where => $where,
1410
);
1411
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1412
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1413

            
1414
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1415
             ->clause(['or', ("$key1 = :$key1") x 2])
1416
             ->param({$key1 => [1]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1417
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1418
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1419
    where => $where,
1420
);
1421
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1422
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1423

            
1424
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1425
             ->clause(['or', ("$key1 = :$key1") x 2])
1426
             ->param({$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1427
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1428
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1429
    where => $where,
1430
);
1431
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1432
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1433

            
1434
$where = $dbi->where
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1435
             ->clause("$key1 = :$key1")
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1436
             ->param({$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1437
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1438
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1439
    where => $where,
1440
);
1441
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1442
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1443

            
1444
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1445
             ->clause("$key1 = :$key1 $key2 = :$key2")
1446
             ->param({$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1447
eval{$where->to_string};
1448
like($@, qr/one column/);
1449

            
1450
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1451
             ->clause(['or', ("$key1 = :$key1") x 3])
1452
             ->param({$key1 => [$dbi->not_exists, 1, 3]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1453
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1454
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1455
    where => $where,
1456
);
1457
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1458
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], 'not_exists');
test cleanup
Yuki Kimoto authored on 2011-08-10
1459

            
1460
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1461
             ->clause(['or', ("$key1 = :$key1") x 3])
1462
             ->param({$key1 => [1, $dbi->not_exists, 3]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1463
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1464
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1465
    where => $where,
1466
);
1467
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1468
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], 'not_exists');
test cleanup
Yuki Kimoto authored on 2011-08-10
1469

            
1470
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1471
             ->clause(['or', ("$key1 = :$key1") x 3])
1472
             ->param({$key1 => [1, 3, $dbi->not_exists]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1473
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1474
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1475
    where => $where,
1476
);
1477
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1478
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], 'not_exists');
test cleanup
Yuki Kimoto authored on 2011-08-10
1479

            
1480
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1481
             ->clause(['or', ("$key1 = :$key1") x 3])
1482
             ->param({$key1 => [1, $dbi->not_exists, $dbi->not_exists]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1483
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1484
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1485
    where => $where,
1486
);
1487
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1488
is_deeply($row, [{$key1 => 1, $key2 => 2}], 'not_exists');
test cleanup
Yuki Kimoto authored on 2011-08-10
1489

            
1490
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1491
             ->clause(['or', ("$key1 = :$key1") x 3])
1492
             ->param({$key1 => [$dbi->not_exists, 1, $dbi->not_exists]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1493
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1494
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1495
    where => $where,
1496
);
1497
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1498
is_deeply($row, [{$key1 => 1, $key2 => 2}], 'not_exists');
test cleanup
Yuki Kimoto authored on 2011-08-10
1499

            
1500
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1501
             ->clause(['or', ("$key1 = :$key1") x 3])
1502
             ->param({$key1 => [$dbi->not_exists, $dbi->not_exists, 1]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1503
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1504
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1505
    where => $where,
1506
);
1507
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1508
is_deeply($row, [{$key1 => 1, $key2 => 2}], 'not_exists');
test cleanup
Yuki Kimoto authored on 2011-08-10
1509

            
1510
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1511
             ->clause(['or', ("$key1 = :$key1") x 3])
1512
             ->param({$key1 => [$dbi->not_exists, $dbi->not_exists, $dbi->not_exists]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1513
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1514
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1515
    where => $where,
1516
);
1517
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1518
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], 'not_exists');
test cleanup
Yuki Kimoto authored on 2011-08-10
1519

            
1520
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1521
             ->clause(['or', ("$key1 = :$key1") x 3])
1522
             ->param({$key1 => []});
test cleanup
Yuki Kimoto authored on 2011-08-10
1523
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1524
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1525
    where => $where,
1526
);
1527
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1528
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], 'not_exists');
test cleanup
Yuki Kimoto authored on 2011-08-10
1529

            
1530
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1531
             ->clause(['and', "{> $key1}", "{< $key1}" ])
1532
             ->param({$key1 => [2, $dbi->not_exists]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1533
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1534
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1535
    where => $where,
1536
);
1537
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1538
is_deeply($row, [{$key1 => 3, $key2 => 4}], 'not_exists');
test cleanup
Yuki Kimoto authored on 2011-08-10
1539

            
1540
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1541
             ->clause(['and', "{> $key1}", "{< $key1}" ])
1542
             ->param({$key1 => [$dbi->not_exists, 2]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1543
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1544
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1545
    where => $where,
1546
);
1547
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1548
is_deeply($row, [{$key1 => 1, $key2 => 2}], 'not_exists');
test cleanup
Yuki Kimoto authored on 2011-08-10
1549

            
1550
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1551
             ->clause(['and', "{> $key1}", "{< $key1}" ])
1552
             ->param({$key1 => [$dbi->not_exists, $dbi->not_exists]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1553
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1554
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1555
    where => $where,
1556
);
1557
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1558
is_deeply($row, [{$key1 => 1, $key2 => 2},{$key1 => 3, $key2 => 4}], 'not_exists');
test cleanup
Yuki Kimoto authored on 2011-08-10
1559

            
1560
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1561
             ->clause(['and', "{> $key1}", "{< $key1}" ])
1562
             ->param({$key1 => [0, 2]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1563
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1564
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1565
    where => $where,
1566
);
1567
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1568
is_deeply($row, [{$key1 => 1, $key2 => 2}], 'not_exists');
test cleanup
Yuki Kimoto authored on 2011-08-10
1569

            
1570
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1571
             ->clause(['and',"$key1 is not null", "$key2 is not null" ]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1572
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1573
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1574
    where => $where,
1575
);
1576
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1577
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], 'not_exists');
test cleanup
Yuki Kimoto authored on 2011-08-10
1578

            
1579
eval {$dbi->where(ppp => 1) };
1580
like($@, qr/invalid/);
1581

            
1582
$where = $dbi->where(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1583
    clause => ['and', ['or'], ['and', "$key1 = :$key1", "$key2 = :$key2"]],
1584
    param => {$key1 => 1, $key2 => 2}
test cleanup
Yuki Kimoto authored on 2011-08-10
1585
);
1586
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1587
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1588
    where => $where,
1589
);
1590
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1591
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1592

            
1593

            
1594
$where = $dbi->where(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1595
    clause => ['and', ['or'], ['or', ":$key1", ":$key2"]],
test cleanup
Yuki Kimoto authored on 2011-08-10
1596
    param => {}
1597
);
1598
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1599
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1600
    where => $where,
1601
);
1602
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1603
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1604

            
1605
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1606
$where->clause(['and', ":${key1}{=}"]);
1607
$where->param({$key1 => undef});
1608
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1609
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1610
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1611

            
1612
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1613
$where->clause(['and', ":${key1}{=}"]);
1614
$where->param({$key1 => undef});
test cleanup
Yuki Kimoto authored on 2011-08-10
1615
$where->if('defined');
1616
$where->map;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1617
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1618
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1619
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1620

            
1621
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1622
$where->clause(['or', ":${key1}{=}", ":${key1}{=}"]);
1623
$where->param({$key1 => [undef, undef]});
1624
$result = $dbi->execute("select * from $table1 $where", {$key1 => [1, 0]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1625
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1626
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
1627
$result = $dbi->execute("select * from $table1 $where", {$key1 => [0, 1]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1628
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1629
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1630

            
1631
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1632
$where->clause(['and', ":${key1}{=}"]);
1633
$where->param({$key1 => [undef, undef]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1634
$where->if('defined');
1635
$where->map;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1636
$result = $dbi->execute("select * from $table1 $where", {$key1 => [1, 0]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1637
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1638
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
1639
$result = $dbi->execute("select * from $table1 $where", {$key1 => [0, 1]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1640
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1641
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1642

            
1643
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1644
$where->clause(['and', ":${key1}{=}"]);
1645
$where->param({$key1 => 0});
test cleanup
Yuki Kimoto authored on 2011-08-10
1646
$where->if('length');
1647
$where->map;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1648
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1649
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1650
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1651

            
1652
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1653
$where->clause(['and', ":${key1}{=}"]);
1654
$where->param({$key1 => ''});
test cleanup
Yuki Kimoto authored on 2011-08-10
1655
$where->if('length');
1656
$where->map;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1657
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1658
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1659
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1660

            
1661
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1662
$where->clause(['and', ":${key1}{=}"]);
1663
$where->param({$key1 => 5});
test cleanup
Yuki Kimoto authored on 2011-08-10
1664
$where->if(sub { ($_[0] || '') eq 5 });
1665
$where->map;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1666
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1667
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1668
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1669

            
1670
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1671
$where->clause(['and', ":${key1}{=}"]);
1672
$where->param({$key1 => 7});
test cleanup
Yuki Kimoto authored on 2011-08-10
1673
$where->if(sub { ($_[0] || '') eq 5 });
1674
$where->map;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1675
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1676
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1677
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1678

            
1679
$where = $dbi->where;
1680
$where->param({id => 1, author => 'Ken', price => 1900});
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1681
$where->map(id => "$table1.id",
1682
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
1683
    price => ["$table1.price", {if => sub { $_[0] eq 1900 }}]
test cleanup
Yuki Kimoto authored on 2011-08-10
1684
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1685
is_deeply($where->param, {"$table1.id" => 1, "$table1.author" => '%Ken%',
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1686
  "$table1.price" => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
1687

            
1688
$where = $dbi->where;
1689
$where->param({id => 0, author => 0, price => 0});
1690
$where->map(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1691
    id => "$table1.id",
1692
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
1693
    price => ["$table1.price", sub { '%' . $_[0] . '%' },
test cleanup
Yuki Kimoto authored on 2011-08-10
1694
      {if => sub { $_[0] eq 0 }}]
1695
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1696
is_deeply($where->param, {"$table1.id" => 0, "$table1.author" => '%0%', "$table1.price" => '%0%'});
test cleanup
Yuki Kimoto authored on 2011-08-10
1697

            
1698
$where = $dbi->where;
1699
$where->param({id => '', author => '', price => ''});
1700
$where->if('length');
1701
$where->map(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1702
    id => "$table1.id",
1703
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
1704
    price => ["$table1.price", sub { '%' . $_[0] . '%' },
test cleanup
Yuki Kimoto authored on 2011-08-10
1705
      {if => sub { $_[0] eq 1 }}]
1706
);
1707
is_deeply($where->param, {});
1708

            
1709
$where = $dbi->where;
1710
$where->param({id => undef, author => undef, price => undef});
1711
$where->if('length');
1712
$where->map(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1713
    id => "$table1.id",
1714
    price => ["$table1.price", {if => 'exists'}]
test cleanup
Yuki Kimoto authored on 2011-08-10
1715
);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1716
is_deeply($where->param, {"$table1.price" => undef});
test cleanup
Yuki Kimoto authored on 2011-08-10
1717

            
1718
$where = $dbi->where;
1719
$where->param({price => 'a'});
1720
$where->if('length');
1721
$where->map(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1722
    id => ["$table1.id", {if => 'exists'}],
1723
    price => ["$table1.price", sub { '%' . $_[0] }, {if => 'exists'}]
test cleanup
Yuki Kimoto authored on 2011-08-10
1724
);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1725
is_deeply($where->param, {"$table1.price" => '%a'});
test cleanup
Yuki Kimoto authored on 2011-08-10
1726

            
1727
$where = $dbi->where;
1728
$where->param({id => [1, 2], author => 'Ken', price => 1900});
1729
$where->map(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1730
    id => "$table1.id",
1731
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
1732
    price => ["$table1.price", {if => sub { $_[0] eq 1900 }}]
test cleanup
Yuki Kimoto authored on 2011-08-10
1733
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1734
is_deeply($where->param, {"$table1.id" => [1, 2], "$table1.author" => '%Ken%',
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1735
  "$table1.price" => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
1736

            
1737
$where = $dbi->where;
1738
$where->if('length');
1739
$where->param({id => ['', ''], author => 'Ken', price => 1900});
1740
$where->map(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1741
    id => "$table1.id",
1742
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
1743
    price => ["$table1.price", {if => sub { $_[0] eq 1900 }}]
test cleanup
Yuki Kimoto authored on 2011-08-10
1744
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1745
is_deeply($where->param, {"$table1.id" => [$dbi->not_exists, $dbi->not_exists], "$table1.author" => '%Ken%',
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1746
  "$table1.price" => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
1747

            
1748
$where = $dbi->where;
1749
$where->param({id => ['', ''], author => 'Ken', price => 1900});
1750
$where->map(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1751
    id => ["$table1.id", {if => 'length'}],
1752
    author => ["$table1.author", sub { '%' . $_[0] . '%' }, {if => 'defined'}],
1753
    price => ["$table1.price", {if => sub { $_[0] eq 1900 }}]
test cleanup
Yuki Kimoto authored on 2011-08-10
1754
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1755
is_deeply($where->param, {"$table1.id" => [$dbi->not_exists, $dbi->not_exists], "$table1.author" => '%Ken%',
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1756
  "$table1.price" => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
1757

            
1758
test 'register_tag_processor';
1759
$dbi = DBIx::Custom->connect;
1760
$dbi->register_tag_processor(
1761
    a => sub { 1 }
1762
);
test cleanup
Yuki Kimoto authored on 2011-08-10
1763
is($dbi->{_tags}->{a}->(), 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1764

            
1765
test 'register_tag';
1766
$dbi = DBIx::Custom->connect;
1767
$dbi->register_tag(
1768
    b => sub { 2 }
1769
);
test cleanup
Yuki Kimoto authored on 2011-08-10
1770
is($dbi->{_tags}->{b}->(), 2);
test cleanup
Yuki Kimoto authored on 2011-08-10
1771

            
1772
test 'table not specify exception';
1773
$dbi = DBIx::Custom->connect;
1774
eval {$dbi->insert};
1775
like($@, qr/table/);
1776
eval {$dbi->update};
1777
like($@, qr/table/);
1778
eval {$dbi->delete};
1779
like($@, qr/table/);
1780
eval {$dbi->select};
1781
like($@, qr/table/);
test cleanup
Yuki Kimoto authored on 2011-08-10
1782

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1783
test 'more tests';
1784
$dbi = DBIx::Custom->connect;
1785
eval{$dbi->apply_filter('table', 'column', [])};
1786
like($@, qr/apply_filter/);
1787

            
1788
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
1789
like($@, qr/apply_filter/);
1790

            
1791
$dbi->apply_filter(
1792

            
1793
);
1794
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1795
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1796
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1797
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1798
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
1799
$dbi->apply_filter($table1, $key2, 
test cleanup
Yuki Kimoto authored on 2011-08-10
1800
                   {in => sub { $_[0] * 3 }, out => sub { $_[0] * 2 }});
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1801
$rows = $dbi->select(table => $table1, where => {$key2 => 1})->all;
1802
is_deeply($rows, [{$key1 => 1, $key2 => 6}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1803

            
1804
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1805
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1806
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1807
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1808
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
1809
$dbi->apply_filter($table1, $key2, {});
1810
$rows = $dbi->select(table => $table1, where => {$key2 => 2})->all;
1811
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1812

            
1813
$dbi = DBIx::Custom->connect;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1814
eval {$dbi->apply_filter($table1, $key2, {out => 'no'})};
test cleanup
Yuki Kimoto authored on 2011-08-10
1815
like($@, qr/not registered/);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1816
eval {$dbi->apply_filter($table1, $key2, {in => 'no'})};
test cleanup
Yuki Kimoto authored on 2011-08-10
1817
like($@, qr/not registered/);
1818
$dbi->method({one => sub { 1 }});
1819
is($dbi->one, 1);
1820

            
1821
eval{DBIx::Custom->connect(dsn => undef)};
1822
like($@, qr/_connect/);
1823

            
1824
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1825
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1826
$dbi->execute($create_table1);
1827
$dbi->register_filter(twice => sub { $_[0] * 2 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1828
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2},
1829
             filter => {$key1 => 'twice'});
1830
$row = $dbi->select(table => $table1)->one;
1831
is_deeply($row, {$key1 => 2, $key2 => 2});
1832
eval {$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2},
1833
             filter => {$key1 => 'no'}) };
test cleanup
Yuki Kimoto authored on 2011-08-10
1834
like($@, qr//);
1835

            
1836
$dbi->register_filter(one => sub { });
1837
$dbi->default_fetch_filter('one');
1838
ok($dbi->default_fetch_filter);
1839
$dbi->default_bind_filter('one');
1840
ok($dbi->default_bind_filter);
1841
eval{$dbi->default_fetch_filter('no')};
1842
like($@, qr/not registered/);
1843
eval{$dbi->default_bind_filter('no')};
1844
like($@, qr/not registered/);
1845
$dbi->default_bind_filter(undef);
1846
ok(!defined $dbi->default_bind_filter);
1847
$dbi->default_fetch_filter(undef);
1848
ok(!defined $dbi->default_fetch_filter);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1849
eval {$dbi->execute("select * from $table1 {} {= author") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1850
like($@, qr/Tag not finished/);
1851

            
1852
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1853
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1854
$dbi->execute($create_table1);
1855
$dbi->register_filter(one => sub { 1 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1856
$result = $dbi->select(table => $table1);
1857
eval {$result->filter($key1 => 'no')};
test cleanup
Yuki Kimoto authored on 2011-08-10
1858
like($@, qr/not registered/);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1859
eval {$result->end_filter($key1 => 'no')};
test cleanup
Yuki Kimoto authored on 2011-08-10
1860
like($@, qr/not registered/);
1861
$result->default_filter(undef);
1862
ok(!defined $result->default_filter);
1863
$result->default_filter('one');
1864
is($result->default_filter->(), 1);
1865

            
1866
test 'dbi_option';
1867
$dbi = DBIx::Custom->connect(dbi_option => {PrintError => 1});
1868
ok($dbi->dbh->{PrintError});
1869
$dbi = DBIx::Custom->connect(dbi_options => {PrintError => 1});
1870
ok($dbi->dbh->{PrintError});
1871

            
1872
test 'DBIx::Custom::Result stash()';
1873
$result = DBIx::Custom::Result->new;
1874
is_deeply($result->stash, {}, 'default');
1875
$result->stash->{foo} = 1;
1876
is($result->stash->{foo}, 1, 'get and set');
test cleanup
Yuki Kimoto authored on 2011-08-10
1877

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1878
test 'delete_at';
1879
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1880
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
1881
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1882
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1883
$dbi->delete_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1884
    table => $table1,
1885
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1886
    where => [1, 2],
1887
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1888
is_deeply($dbi->select(table => $table1)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
1889

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1890
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1891
$dbi->delete_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1892
    table => $table1,
1893
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
1894
    where => 1,
1895
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1896
is_deeply($dbi->select(table => $table1)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
1897

            
1898
test 'insert_at';
1899
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1900
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
1901
$dbi->execute($create_table1_2);
1902
$dbi->insert_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1903
    primary_key => [$key1, $key2], 
1904
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
1905
    where => [1, 2],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1906
    param => {$key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
1907
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1908
is($dbi->select(table => $table1)->one->{$key1}, 1);
1909
is($dbi->select(table => $table1)->one->{$key2}, 2);
1910
is($dbi->select(table => $table1)->one->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
1911

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1912
$dbi->delete_all(table => $table1);
1913
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1914
$dbi->insert_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1915
    primary_key => $key1, 
1916
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
1917
    where => 1,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1918
    param => {$key2 => 2, $key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
1919
);
1920

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1921
is($dbi->select(table => $table1)->one->{$key1}, 1);
1922
is($dbi->select(table => $table1)->one->{$key2}, 2);
1923
is($dbi->select(table => $table1)->one->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
1924

            
1925
eval {
1926
    $dbi->insert_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1927
        table => $table1,
1928
        primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1929
        where => {},
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1930
        param => {$key1 => 1, $key2 => 2, $key3 => 3},
cleanup test
Yuki Kimoto authored on 2011-08-10
1931
    );
1932
};
1933
like($@, qr/must be/);
1934

            
1935
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1936
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
1937
$dbi->execute($create_table1_2);
1938
$dbi->insert_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1939
    {$key3 => 3},
1940
    primary_key => [$key1, $key2], 
1941
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
1942
    where => [1, 2],
1943
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1944
is($dbi->select(table => $table1)->one->{$key1}, 1);
1945
is($dbi->select(table => $table1)->one->{$key2}, 2);
1946
is($dbi->select(table => $table1)->one->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
1947

            
1948
test 'update_at';
1949
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1950
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
1951
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1952
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1953
$dbi->update_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1954
    table => $table1,
1955
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1956
    where => [1, 2],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1957
    param => {$key3 => 4}
cleanup test
Yuki Kimoto authored on 2011-08-10
1958
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1959
is($dbi->select(table => $table1)->one->{$key1}, 1);
1960
is($dbi->select(table => $table1)->one->{$key2}, 2);
1961
is($dbi->select(table => $table1)->one->{$key3}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-10
1962

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1963
$dbi->delete_all(table => $table1);
1964
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1965
$dbi->update_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1966
    table => $table1,
1967
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
1968
    where => 1,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1969
    param => {$key3 => 4}
cleanup test
Yuki Kimoto authored on 2011-08-10
1970
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1971
is($dbi->select(table => $table1)->one->{$key1}, 1);
1972
is($dbi->select(table => $table1)->one->{$key2}, 2);
1973
is($dbi->select(table => $table1)->one->{$key3}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-10
1974

            
1975
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1976
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
1977
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1978
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1979
$dbi->update_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1980
    {$key3 => 4},
1981
    table => $table1,
1982
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1983
    where => [1, 2]
1984
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1985
is($dbi->select(table => $table1)->one->{$key1}, 1);
1986
is($dbi->select(table => $table1)->one->{$key2}, 2);
1987
is($dbi->select(table => $table1)->one->{$key3}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-10
1988

            
1989
test 'select_at';
1990
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1991
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
1992
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1993
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1994
$result = $dbi->select_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1995
    table => $table1,
1996
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1997
    where => [1, 2]
1998
);
1999
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2000
is($row->{$key1}, 1);
2001
is($row->{$key2}, 2);
2002
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2003

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2004
$dbi->delete_all(table => $table1);
2005
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2006
$result = $dbi->select_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2007
    table => $table1,
2008
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2009
    where => 1,
2010
);
2011
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2012
is($row->{$key1}, 1);
2013
is($row->{$key2}, 2);
2014
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2015

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2016
$dbi->delete_all(table => $table1);
2017
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2018
$result = $dbi->select_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2019
    table => $table1,
2020
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
2021
    where => [1, 2]
2022
);
2023
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2024
is($row->{$key1}, 1);
2025
is($row->{$key2}, 2);
2026
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2027

            
2028
eval {
2029
    $result = $dbi->select_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2030
        table => $table1,
2031
        primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
2032
        where => {},
2033
    );
2034
};
2035
like($@, qr/must be/);
2036

            
2037
eval {
2038
    $result = $dbi->select_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2039
        table => $table1,
2040
        primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
2041
        where => [1],
2042
    );
2043
};
2044
like($@, qr/same/);
2045

            
2046
eval {
2047
    $result = $dbi->update_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2048
        table => $table1,
2049
        primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
2050
        where => {},
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2051
        param => {$key1 => 1, $key2 => 2},
cleanup test
Yuki Kimoto authored on 2011-08-10
2052
    );
2053
};
2054
like($@, qr/must be/);
2055

            
2056
eval {
2057
    $result = $dbi->delete_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2058
        table => $table1,
2059
        primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
2060
        where => {},
2061
    );
2062
};
2063
like($@, qr/must be/);
2064

            
2065
test 'model delete_at';
2066
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2067
eval { $dbi->execute("drop table $table1") };
2068
eval { $dbi->execute("drop table $table2") };
2069
eval { $dbi->execute("drop table $table3") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2070
$dbi->execute($create_table1_2);
2071
$dbi->execute($create_table2_2);
2072
$dbi->execute($create_table3);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2073
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2074
$dbi->model($table1)->delete_at(where => [1, 2]);
2075
is_deeply($dbi->select(table => $table1)->all, []);
2076
$dbi->insert(table => $table2, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2077
$dbi->model($table1)->delete_at(where => [1, 2]);
2078
is_deeply($dbi->select(table => $table1)->all, []);
2079
$dbi->insert(table => $table3, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2080
$dbi->model($table3)->delete_at(where => [1, 2]);
2081
is_deeply($dbi->select(table => $table3)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
2082

            
2083
test 'model insert_at';
2084
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2085
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2086
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2087
$dbi->model($table1)->insert_at(
cleanup test
Yuki Kimoto authored on 2011-08-10
2088
    where => [1, 2],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2089
    param => {$key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
2090
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2091
$result = $dbi->model($table1)->select;
cleanup test
Yuki Kimoto authored on 2011-08-10
2092
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2093
is($row->{$key1}, 1);
2094
is($row->{$key2}, 2);
2095
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2096

            
2097
test 'model update_at';
2098
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2099
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2100
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2101
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2102
$dbi->model($table1)->update_at(
cleanup test
Yuki Kimoto authored on 2011-08-10
2103
    where => [1, 2],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2104
    param => {$key3 => 4}
cleanup test
Yuki Kimoto authored on 2011-08-10
2105
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2106
$result = $dbi->model($table1)->select;
cleanup test
Yuki Kimoto authored on 2011-08-10
2107
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2108
is($row->{$key1}, 1);
2109
is($row->{$key2}, 2);
2110
is($row->{$key3}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-10
2111

            
2112
test 'model select_at';
2113
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2114
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2115
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2116
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2117
$result = $dbi->model($table1)->select_at(where => [1, 2]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2118
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2119
is($row->{$key1}, 1);
2120
is($row->{$key2}, 2);
2121
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2122

            
2123

            
2124
test 'mycolumn and column';
2125
$dbi = MyDBI7->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
2126
$dbi->user_table_info($user_table_info);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2127
eval { $dbi->execute("drop table $table1") };
2128
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2129
$dbi->execute($create_table1);
2130
$dbi->execute($create_table2);
2131
$dbi->separator('__');
2132
$dbi->setup_model;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2133
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2134
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
2135
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2136
$result = $model->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2137
    column => [$model->mycolumn, $model->column($table2)],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2138
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2139
);
2140
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2141
          {$key1 => 1, $key2 => 2, "${table2}__$key1" => 1, "${table2}__$key3" => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2142

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2143
test 'insert_param';
2144
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2145
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2146
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2147
$param = {$key1 => 1, $key2 => 2};
test cleanup
Yuki Kimoto authored on 2011-08-10
2148
$insert_param = $dbi->insert_param($param);
2149
$sql = <<"EOS";
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2150
insert into $table1 $insert_param
test cleanup
Yuki Kimoto authored on 2011-08-10
2151
EOS
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2152
$dbi->execute($sql, param => $param, table => $table1);
2153
is($dbi->select(table => $table1)->one->{$key1}, 1);
2154
is($dbi->select(table => $table1)->one->{$key2}, 2);
test cleanup
Yuki Kimoto authored on 2011-08-10
2155

            
2156
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2157
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2158
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2159
$param = {$key1 => 1, $key2 => 2};
test cleanup
Yuki Kimoto authored on 2011-08-10
2160
$insert_param = $dbi->insert_param($param);
2161
$sql = <<"EOS";
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2162
insert into $table1 $insert_param
test cleanup
Yuki Kimoto authored on 2011-08-10
2163
EOS
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2164
$dbi->execute($sql, param => $param, table => $table1);
2165
is($dbi->select(table => $table1)->one->{$key1}, 1);
2166
is($dbi->select(table => $table1)->one->{$key2}, 2);
test cleanup
Yuki Kimoto authored on 2011-08-10
2167

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2171
test 'mycolumn';
2172
$dbi = MyDBI8->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
2173
$dbi->user_table_info($user_table_info);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2174
eval { $dbi->execute("drop table $table1") };
2175
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2176
$dbi->execute($create_table1);
2177
$dbi->execute($create_table2);
2178
$dbi->setup_model;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2179
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2180
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
2181
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2182
$result = $model->select_at(
2183
    column => [
2184
        $model->mycolumn,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2185
        $model->column($table2)
cleanup test
Yuki Kimoto authored on 2011-08-10
2186
    ]
2187
);
2188
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2189
          {$key1 => 1, $key2 => 2, "$table2.$key1" => 1, "$table2.$key3" => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2190

            
2191
$result = $model->select_at(
2192
    column => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2193
        $model->mycolumn([$key1]),
2194
        $model->column($table2 => [$key1])
cleanup test
Yuki Kimoto authored on 2011-08-10
2195
    ]
2196
);
2197
is_deeply($result->one,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2198
          {$key1 => 1, "$table2.$key1" => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
2199
$result = $model->select_at(
2200
    column => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2201
        $model->mycolumn([$key1]),
2202
        {$table2 => [$key1]}
cleanup test
Yuki Kimoto authored on 2011-08-10
2203
    ]
2204
);
2205
is_deeply($result->one,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2206
          {$key1 => 1, "$table2.$key1" => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
2207

            
2208
$result = $model->select_at(
2209
    column => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2210
        $model->mycolumn([$key1]),
2211
        ["$table2.$key1", as => "$table2.$key1"]
cleanup test
Yuki Kimoto authored on 2011-08-10
2212
    ]
2213
);
2214
is_deeply($result->one,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2215
          {$key1 => 1, "$table2.$key1" => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
2216

            
2217
$result = $model->select_at(
2218
    column => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2219
        $model->mycolumn([$key1]),
2220
        ["$table2.$key1" => "$table2.$key1"]
cleanup test
Yuki Kimoto authored on 2011-08-10
2221
    ]
2222
);
2223
is_deeply($result->one,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2224
          {$key1 => 1, "$table2.$key1" => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
2225

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2226
test 'merge_param';
2227
$dbi = DBIx::Custom->new;
2228
$params = [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2229
    {$key1 => 1, $key2 => 2, $key3 => 3},
2230
    {$key1 => 1, $key2 => 2},
2231
    {$key1 => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2232
];
2233
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2234
is_deeply($param, {$key1 => [1, 1, 1], $key2 => [2, 2], $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2235

            
2236
$params = [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2237
    {$key1 => [1, 2], $key2 => 1, $key3 => [1, 2]},
2238
    {$key1 => [3, 4], $key2 => [2, 3], $key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
2239
];
2240
$param = $dbi->merge_param($params->[0], $params->[1]);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2241
is_deeply($param, {$key1 => [1, 2, 3, 4], $key2 => [1, 2, 3], $key3 => [1, 2, 3]});
cleanup test
Yuki Kimoto authored on 2011-08-10
2242

            
2243
test 'select() param option';
2244
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2245
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2246
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2247
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2248
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2249
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2250
$dbi->execute($create_table2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2251
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4});
2252
$dbi->insert(table => $table2, param => {$key1 => 2, $key3 => 5});
cleanup test
Yuki Kimoto authored on 2011-08-10
2253
$rows = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2254
    table => $table1,
2255
    column => "$table1.$key1 as ${table1}_$key1, $key2, $key3",
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2256
    where   => {"$table1.$key2" => 3},
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2257
    join  => ["inner join (select * from $table2 where {= $table2.$key3})" . 
test cleanup
Yuki Kimoto authored on 2011-08-15
2258
              " $table2 on $table1.$key1 = $table2.$key1"],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2259
    param => {"$table2.$key3" => 5}
cleanup test
Yuki Kimoto authored on 2011-08-10
2260
)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2261
is_deeply($rows, [{"${table1}_$key1" => 2, $key2 => 3, $key3 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2262

            
2263
test 'select() string where';
2264
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2265
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2266
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2267
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2268
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2269
$rows = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2270
    table => $table1,
2271
    where => "$key1 = :$key1 and $key2 = :$key2",
2272
    where_param => {$key1 => 1, $key2 => 2}
cleanup test
Yuki Kimoto authored on 2011-08-10
2273
)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2274
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2275

            
2276
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2277
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2278
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2279
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2280
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2281
$rows = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2282
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2283
    where => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2284
        "$key1 = :$key1 and $key2 = :$key2",
2285
        {$key1 => 1, $key2 => 2}
cleanup test
Yuki Kimoto authored on 2011-08-10
2286
    ]
2287
)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2288
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2289

            
2290
test 'delete() string where';
2291
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2292
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2293
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2294
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2295
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2296
$dbi->delete(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2297
    table => $table1,
2298
    where => "$key1 = :$key1 and $key2 = :$key2",
2299
    where_param => {$key1 => 1, $key2 => 2}
cleanup test
Yuki Kimoto authored on 2011-08-10
2300
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2301
$rows = $dbi->select(table => $table1)->all;
2302
is_deeply($rows, [{$key1 => 2, $key2 => 3}]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2303

            
2304
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2305
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2306
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2307
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2308
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2309
$dbi->delete(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2310
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2311
    where => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2312
        "$key1 = :$key1 and $key2 = :$key2",
2313
         {$key1 => 1, $key2 => 2}
cleanup test
Yuki Kimoto authored on 2011-08-10
2314
    ]
2315
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2316
$rows = $dbi->select(table => $table1)->all;
2317
is_deeply($rows, [{$key1 => 2, $key2 => 3}]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2318

            
2319

            
2320
test 'update() string where';
2321
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2322
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2323
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2324
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-10
2325
$dbi->update(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2326
    table => $table1,
2327
    param => {$key1 => 5},
2328
    where => "$key1 = :$key1 and $key2 = :$key2",
2329
    where_param => {$key1 => 1, $key2 => 2}
cleanup test
Yuki Kimoto authored on 2011-08-10
2330
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2331
$rows = $dbi->select(table => $table1)->all;
2332
is_deeply($rows, [{$key1 => 5, $key2 => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2333

            
2334
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2335
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2336
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2337
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-10
2338
$dbi->update(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2339
    table => $table1,
2340
    param => {$key1 => 5},
cleanup test
Yuki Kimoto authored on 2011-08-10
2341
    where => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2342
        "$key1 = :$key1 and $key2 = :$key2",
2343
        {$key1 => 1, $key2 => 2}
cleanup test
Yuki Kimoto authored on 2011-08-10
2344
    ]
2345
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2346
$rows = $dbi->select(table => $table1)->all;
2347
is_deeply($rows, [{$key1 => 5, $key2 => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2348

            
2349
test 'insert id and primary_key option';
2350
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2351
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2352
$dbi->execute($create_table1_2);
2353
$dbi->insert(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2354
    primary_key => [$key1, $key2], 
2355
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2356
    id => [1, 2],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2357
    param => {$key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
2358
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2359
is($dbi->select(table => $table1)->one->{$key1}, 1);
2360
is($dbi->select(table => $table1)->one->{$key2}, 2);
2361
is($dbi->select(table => $table1)->one->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2362

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2363
$dbi->delete_all(table => $table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2364
$dbi->insert(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2365
    primary_key => $key1, 
2366
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2367
    id => 0,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2368
    param => {$key2 => 2, $key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
2369
);
2370

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2371
is($dbi->select(table => $table1)->one->{$key1}, 0);
2372
is($dbi->select(table => $table1)->one->{$key2}, 2);
2373
is($dbi->select(table => $table1)->one->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2374

            
2375
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2376
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2377
$dbi->execute($create_table1_2);
2378
$dbi->insert(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2379
    {$key3 => 3},
2380
    primary_key => [$key1, $key2], 
2381
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2382
    id => [1, 2],
2383
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2384
is($dbi->select(table => $table1)->one->{$key1}, 1);
2385
is($dbi->select(table => $table1)->one->{$key2}, 2);
2386
is($dbi->select(table => $table1)->one->{$key3}, 3);
test cleanup
Yuki Kimoto authored on 2011-08-10
2387

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2388
test 'model insert id and primary_key option';
2389
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2390
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2391
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2392
$dbi->model($table1)->insert(
cleanup test
Yuki Kimoto authored on 2011-08-10
2393
    id => [1, 2],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2394
    param => {$key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
2395
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2396
$result = $dbi->model($table1)->select;
cleanup test
Yuki Kimoto authored on 2011-08-10
2397
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2398
is($row->{$key1}, 1);
2399
is($row->{$key2}, 2);
2400
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2401

            
2402
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2403
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2404
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2405
$dbi->model($table1)->insert(
2406
    {$key3 => 3},
cleanup test
Yuki Kimoto authored on 2011-08-10
2407
    id => [1, 2]
2408
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2409
$result = $dbi->model($table1)->select;
cleanup test
Yuki Kimoto authored on 2011-08-10
2410
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2411
is($row->{$key1}, 1);
2412
is($row->{$key2}, 2);
2413
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2414

            
2415
test 'update and id option';
2416
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2417
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2418
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2419
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2420
$dbi->update(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2421
    table => $table1,
2422
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
2423
    id => [1, 2],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2424
    param => {$key3 => 4}
cleanup test
Yuki Kimoto authored on 2011-08-10
2425
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2426
is($dbi->select(table => $table1)->one->{$key1}, 1);
2427
is($dbi->select(table => $table1)->one->{$key2}, 2);
2428
is($dbi->select(table => $table1)->one->{$key3}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-10
2429

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2430
$dbi->delete_all(table => $table1);
2431
$dbi->insert(table => $table1, param => {$key1 => 0, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2432
$dbi->update(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2433
    table => $table1,
2434
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2435
    id => 0,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2436
    param => {$key3 => 4}
cleanup test
Yuki Kimoto authored on 2011-08-10
2437
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2438
is($dbi->select(table => $table1)->one->{$key1}, 0);
2439
is($dbi->select(table => $table1)->one->{$key2}, 2);
2440
is($dbi->select(table => $table1)->one->{$key3}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-10
2441

            
2442
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2443
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2444
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2445
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2446
$dbi->update(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2447
    {$key3 => 4},
2448
    table => $table1,
2449
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
2450
    id => [1, 2]
2451
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2452
is($dbi->select(table => $table1)->one->{$key1}, 1);
2453
is($dbi->select(table => $table1)->one->{$key2}, 2);
2454
is($dbi->select(table => $table1)->one->{$key3}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-10
2455

            
2456

            
2457
test 'model update and id option';
2458
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2459
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2460
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2461
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2462
$dbi->model($table1)->update(
cleanup test
Yuki Kimoto authored on 2011-08-10
2463
    id => [1, 2],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2464
    param => {$key3 => 4}
cleanup test
Yuki Kimoto authored on 2011-08-10
2465
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2466
$result = $dbi->model($table1)->select;
cleanup test
Yuki Kimoto authored on 2011-08-10
2467
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2468
is($row->{$key1}, 1);
2469
is($row->{$key2}, 2);
2470
is($row->{$key3}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-10
2471

            
2472

            
2473
test 'delete and id option';
2474
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2475
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2476
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2477
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2478
$dbi->delete(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2479
    table => $table1,
2480
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
2481
    id => [1, 2],
2482
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2483
is_deeply($dbi->select(table => $table1)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
2484

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2485
$dbi->insert(table => $table1, param => {$key1 => 0, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2486
$dbi->delete(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2487
    table => $table1,
2488
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2489
    id => 0,
2490
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2491
is_deeply($dbi->select(table => $table1)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
2492

            
2493

            
2494
test 'model delete and id option';
2495
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2496
eval { $dbi->execute("drop table $table1") };
2497
eval { $dbi->execute("drop table $table2") };
2498
eval { $dbi->execute("drop table $table3") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2499
$dbi->execute($create_table1_2);
2500
$dbi->execute($create_table2_2);
2501
$dbi->execute($create_table3);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2502
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2503
$dbi->model($table1)->delete(id => [1, 2]);
2504
is_deeply($dbi->select(table => $table1)->all, []);
2505
$dbi->insert(table => $table2, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2506
$dbi->model($table1)->delete(id => [1, 2]);
2507
is_deeply($dbi->select(table => $table1)->all, []);
2508
$dbi->insert(table => $table3, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2509
$dbi->model($table3)->delete(id => [1, 2]);
2510
is_deeply($dbi->select(table => $table3)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
2511

            
2512

            
2513
test 'select and id option';
2514
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2515
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2516
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2517
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2518
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2519
    table => $table1,
2520
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
2521
    id => [1, 2]
2522
);
2523
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2524
is($row->{$key1}, 1);
2525
is($row->{$key2}, 2);
2526
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2527

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2528
$dbi->delete_all(table => $table1);
2529
$dbi->insert(table => $table1, param => {$key1 => 0, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2530
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2531
    table => $table1,
2532
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2533
    id => 0,
2534
);
2535
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2536
is($row->{$key1}, 0);
2537
is($row->{$key2}, 2);
2538
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2539

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2540
$dbi->delete_all(table => $table1);
2541
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2542
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2543
    table => $table1,
2544
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
2545
    id => [1, 2]
2546
);
2547
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2548
is($row->{$key1}, 1);
2549
is($row->{$key2}, 2);
2550
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2551

            
2552

            
2553
test 'model select_at';
2554
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2555
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2556
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2557
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2558
$result = $dbi->model($table1)->select(id => [1, 2]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2559
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2560
is($row->{$key1}, 1);
2561
is($row->{$key2}, 2);
2562
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2563

            
2564
test 'column separator is default .';
2565
$dbi = MyDBI7->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
2566
$dbi->user_table_info($user_table_info);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2567
eval { $dbi->execute("drop table $table1") };
2568
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2569
$dbi->execute($create_table1);
2570
$dbi->execute($create_table2);
2571
$dbi->setup_model;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2572
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2573
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
2574
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2575
$result = $model->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2576
    column => [$model->column($table2)],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2577
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2578
);
2579
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2580
          {"$table2.$key1" => 1, "$table2.$key3" => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2581

            
2582
$result = $model->select(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2583
    column => [$model->column($table2 => [$key1, $key3])],
2584
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2585
);
2586
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2587
          {"$table2.$key1" => 1, "$table2.$key3" => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2588

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2589
test 'separator';
2590
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
2591
$dbi->user_table_info($user_table_info);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2592
eval { $dbi->execute("drop table $table1") };
2593
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2594
$dbi->execute($create_table1);
2595
$dbi->execute($create_table2);
test cleanup
Yuki Kimoto authored on 2011-08-10
2596

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2597
$dbi->create_model(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2598
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2599
    join => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2600
       "left outer join $table2 on $table1.$key1 = $table2.$key1"
cleanup test
Yuki Kimoto authored on 2011-08-10
2601
    ],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2602
    primary_key => [$key1],
cleanup test
Yuki Kimoto authored on 2011-08-10
2603
);
2604
$model2 = $dbi->create_model(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2605
    table => $table2,
cleanup test
Yuki Kimoto authored on 2011-08-10
2606
);
2607
$dbi->setup_model;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2608
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2609
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
2610
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2611
$result = $model->select(
2612
    column => [
2613
        $model->mycolumn,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2614
        {$table2 => [$key1, $key3]}
cleanup test
Yuki Kimoto authored on 2011-08-10
2615
    ],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2616
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2617
);
2618
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2619
          {$key1 => 1, $key2 => 2, "$table2.$key1" => 1, "$table2.$key3" => 3});
2620
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2621

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2622
$dbi->separator('__');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2623
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2624
$result = $model->select(
2625
    column => [
2626
        $model->mycolumn,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2627
        {$table2 => [$key1, $key3]}
cleanup test
Yuki Kimoto authored on 2011-08-10
2628
    ],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2629
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2630
);
2631
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2632
          {$key1 => 1, $key2 => 2, "${table2}__$key1" => 1, "${table2}__$key3" => 3});
2633
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2634

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2635
$dbi->separator('-');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2636
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2637
$result = $model->select(
2638
    column => [
2639
        $model->mycolumn,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2640
        {$table2 => [$key1, $key3]}
cleanup test
Yuki Kimoto authored on 2011-08-10
2641
    ],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2642
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2643
);
2644
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2645
          {$key1 => 1, $key2 => 2, "$table2-$key1" => 1, "$table2-$key3" => 3});
2646
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2647

            
2648

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2649
test 'filter_off';
2650
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
2651
$dbi->user_table_info($user_table_info);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2652
eval { $dbi->execute("drop table $table1") };
2653
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2654
$dbi->execute($create_table1);
2655
$dbi->execute($create_table2);
test cleanup
Yuki Kimoto authored on 2011-08-10
2656

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2657
$dbi->create_model(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2658
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2659
    join => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2660
       "left outer join $table2 on $table1.$key1 = $table2.$key1"
cleanup test
Yuki Kimoto authored on 2011-08-10
2661
    ],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2662
    primary_key => [$key1],
cleanup test
Yuki Kimoto authored on 2011-08-10
2663
);
2664
$dbi->setup_model;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2665
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2666
$model = $dbi->model($table1);
2667
$result = $model->select(column => $key1);
2668
$result->filter($key1 => sub { $_[0] * 2 });
2669
is_deeply($result->one, {$key1 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
2670

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2671
test 'available_datetype';
2672
$dbi = DBIx::Custom->connect;
2673
ok($dbi->can('available_datatype'));
test cleanup
Yuki Kimoto authored on 2011-08-10
2674

            
2675

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2676
test 'select prefix option';
2677
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2678
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2679
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2680
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2681
$rows = $dbi->select(prefix => "$key1,", column => $key2, table => $table1)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2682
is_deeply($rows, [{$key1 => 1, $key2 => 2}], "table");
test cleanup
Yuki Kimoto authored on 2011-08-10
2683

            
- added EXPERIMENTAL pass at...
Yuki Kimoto authored on 2011-09-02
2684

            
added tests
Yuki Kimoto authored on 2011-08-26
2685
test 'mapper';
2686
$dbi = DBIx::Custom->connect;
2687
$param = $dbi->mapper(param => {id => 1, author => 'Ken', price => 1900})->map(
2688
    id => "$table1.id",
2689
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2690
    price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
2691
);
2692
is_deeply($param, {"$table1.id" => 1, "$table1.author" => '%Ken%',
2693
  "$table1.price" => 1900});
2694

            
2695
$param = $dbi->mapper(param => {id => 0, author => 0, price => 0})->map(
2696
    id => "$table1.id",
2697
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2698
    price => ["$table1.price", sub { '%' . $_[0] . '%' },
2699
      {condition => sub { $_[0] eq 0 }}]
2700
);
2701
is_deeply($param, {"$table1.id" => 0, "$table1.author" => '%0%', "$table1.price" => '%0%'});
2702

            
2703
$param = $dbi->mapper(param => {id => '', author => '', price => ''})->map(
2704
    id => "$table1.id",
2705
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2706
    price => ["$table1.price", sub { '%' . $_[0] . '%' },
2707
      {condition => sub { $_[0] eq 1 }}]
2708
);
2709
is_deeply($param, {});
2710

            
2711
$param = $dbi->mapper(param => {id => undef, author => undef, price => undef})->map(
2712
    id => "$table1.id",
2713
    price => ["$table1.price", {condition => 'exists'}]
2714
);
2715
is_deeply($param, {"$table1.price" => undef});
2716

            
2717
$param = $dbi->mapper(param => {price => 'a'})->map(
2718
    id => ["$table1.id", {condition => 'exists'}],
2719
    price => ["$table1.price", sub { '%' . $_[0] }, {condition => 'exists'}]
2720
);
2721
is_deeply($param, {"$table1.price" => '%a'});
2722

            
2723
$param = $dbi->mapper(param => {price => 'a'}, condition => 'exists')->map(
2724
    id => ["$table1.id"],
2725
    price => ["$table1.price", sub { '%' . $_[0] }]
2726
);
2727
is_deeply($param, {"$table1.price" => '%a'});
2728

            
- added {KEY => sub { VALUE ...
Yuki Kimoto authored on 2011-09-02
2729
$param = $dbi->mapper(param => {price => 'a'})->map(
2730
    price => sub { '%' . $_[0] }
2731
);
2732
is_deeply($param, {price => '%a'});
2733

            
- added EXPERIMENTAL pass at...
Yuki Kimoto authored on 2011-09-02
2734
eval { $dbi->execute("drop table $table1") };
2735
$dbi->execute($create_table1);
2736
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2737
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
2738

            
2739
$where = $dbi->where;
2740
$where->clause(['and', ":${key1}{=}"]);
2741
$param = $dbi->mapper(param => {$key1 => undef}, condition => 'defined')->map;
2742
$where->param($param);
2743
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
2744
$row = $result->all;
2745
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
2746

            
2747
$where = $dbi->where;
2748
$where->clause(['or', ":${key1}{=}", ":${key1}{=}"]);
2749
$param = $dbi->mapper(param => {$key1 => [undef, undef]}, condition => 'exists')->map;
2750
$result = $dbi->execute("select * from $table1 $where", {$key1 => [1, 0]});
2751
$row = $result->all;
2752
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
2753
$result = $dbi->execute("select * from $table1 $where", {$key1 => [0, 1]});
2754
$row = $result->all;
2755
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
2756

            
2757
$where = $dbi->where;
2758
$where->clause(['and', ":${key1}{=}"]);
2759
$param = $dbi->mapper(param => {$key1 => [undef, undef]}, condition => 'defined')->map;
2760
$where->param($param);
2761
$result = $dbi->execute("select * from $table1 $where", {$key1 => [1, 0]});
2762
$row = $result->all;
2763
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
2764
$result = $dbi->execute("select * from $table1 $where", {$key1 => [0, 1]});
2765
$row = $result->all;
2766
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
2767

            
2768
$where = $dbi->where;
2769
$where->clause(['and', ":${key1}{=}"]);
2770
$param = $dbi->mapper(param => {$key1 => 0}, condition => 'length')
2771
  ->pass([$key1, $key2])->map;
2772
$where->param($param);
2773
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
2774
$row = $result->all;
2775
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
2776

            
2777
$where = $dbi->where;
2778
$where->clause(['and', ":${key1}{=}"]);
2779
$param = $dbi->mapper(param => {$key1 => ''}, condition => 'length')->map;
2780
$where->param($param);
2781
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
2782
$row = $result->all;
2783
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
2784

            
2785
$where = $dbi->where;
2786
$where->clause(['and', ":${key1}{=}"]);
2787
$param = $dbi->mapper(param => {$key1 => 5}, condition => sub { ($_[0] || '') eq 5 })
2788
  ->pass([$key1, $key2])->map;
2789
$where->param($param);
2790
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
2791
$row = $result->all;
2792
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
2793

            
2794
$where = $dbi->where;
2795
$where->clause(['and', ":${key1}{=}"]);
2796
$param = $dbi->mapper(param => {$key1 => 7}, condition => sub { ($_[0] || '') eq 5 })->map;
2797
$where->param($param);
2798
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
2799
$row = $result->all;
2800
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
2801

            
2802
$where = $dbi->where;
2803
$param = $dbi->mapper(param => {id => 1, author => 'Ken', price => 1900})->map(
2804
    id => "$table1.id",
2805
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2806
    price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
2807
);
2808
$where->param($param);
2809
is_deeply($where->param, {"$table1.id" => 1, "$table1.author" => '%Ken%',
2810
  "$table1.price" => 1900});
2811

            
2812
$where = $dbi->where;
2813
$param = $dbi->mapper(param => {id => 0, author => 0, price => 0})->map(
2814
    id => "$table1.id",
2815
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2816
    price => ["$table1.price", sub { '%' . $_[0] . '%' }, {condition => sub { $_[0] eq 0 }}]
2817
);
2818
$where->param($param);
2819
is_deeply($where->param, {"$table1.id" => 0, "$table1.author" => '%0%', "$table1.price" => '%0%'});
2820

            
2821
$where = $dbi->where;
2822
$param = $dbi->mapper(param => {id => '', author => '', price => ''})->map(
2823
    id => "$table1.id",
2824
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2825
    price => ["$table1.price", sub { '%' . $_[0] . '%' }, {condition => sub { $_[0] eq 1 }}]
2826
);
2827
$where->param($param);
2828
is_deeply($where->param, {});
2829

            
2830
$where = $dbi->where;
2831
$param = $dbi->mapper(param => {id => undef, author => undef, price => undef}, condition => 'exists')->map(
2832
    id => "$table1.id",
2833
    price => ["$table1.price", {condition => 'exists'}]
2834
);
2835
is_deeply($param, {"$table1.id"  => undef,"$table1.price" => undef});
2836

            
2837
$where = $dbi->where;
2838
$param = $dbi->mapper(param => {price => 'a'})->map(
2839
    id => ["$table1.id", {condition => 'exists'}],
2840
    price => ["$table1.price", sub { '%' . $_[0] }, {condition => 'exists'}]
2841
);
2842
is_deeply($param, {"$table1.price" => '%a'});
2843

            
2844
$where = $dbi->where;
2845
$param = $dbi->mapper(param => {id => [1, 2], author => 'Ken', price => 1900})->map(
2846
    id => "$table1.id",
2847
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2848
    price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
2849
);
2850
is_deeply($param, {"$table1.id" => [1, 2], "$table1.author" => '%Ken%',
2851
  "$table1.price" => 1900});
2852

            
2853
$where = $dbi->where;
2854
$param = $dbi->mapper(param => {id => ['', ''], author => 'Ken', price => 1900}, condition => 'length')->map(
2855
    id => "$table1.id",
2856
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2857
    price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
2858
);
2859
is_deeply($param, {"$table1.id" => [$dbi->not_exists, $dbi->not_exists], "$table1.author" => '%Ken%',
2860
  "$table1.price" => 1900});
2861

            
2862
$where = $dbi->where;
2863
$param = $dbi->mapper(param => {id => ['', ''], author => 'Ken', price => 1900})->map(
2864
    id => ["$table1.id", {condition => 'length'}],
2865
    author => ["$table1.author", sub { '%' . $_[0] . '%' }, {condition => 'defined'}],
2866
    price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
2867
);
2868
is_deeply($param, {"$table1.id" => [$dbi->not_exists, $dbi->not_exists], "$table1.author" => '%Ken%',
2869
  "$table1.price" => 1900});
2870

            
2871
$where = $dbi->where;
2872
$param = $dbi->mapper(param => {id => 'a', author => 'b', price => 'c'}, pass => [qw/id author/])
2873
  ->map(price => 'book.price');
2874
is_deeply($param, {id => 'a', author => 'b', 'book.price' => 'c'});
2875

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2876
test 'order';
2877
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2878
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2879
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2880
$dbi->insert({$key1 => 1, $key2 => 1}, table => $table1);
2881
$dbi->insert({$key1 => 1, $key2 => 3}, table => $table1);
2882
$dbi->insert({$key1 => 2, $key2 => 2}, table => $table1);
2883
$dbi->insert({$key1 => 2, $key2 => 4}, table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2884
my $order = $dbi->order;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2885
$order->prepend($key1, "$key2 desc");
2886
$result = $dbi->select(table => $table1, append => $order);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2887
is_deeply($result->all, [{$key1 => 1, $key2 => 3}, {$key1 => 1, $key2 => 1},
2888
  {$key1 => 2, $key2 => 4}, {$key1 => 2, $key2 => 2}]);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2889
$order->prepend("$key1 desc");
2890
$result = $dbi->select(table => $table1, append => $order);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2891
is_deeply($result->all, [{$key1 => 2, $key2 => 4}, {$key1 => 2, $key2 => 2},
2892
  {$key1 => 1, $key2 => 3}, {$key1 => 1, $key2 => 1}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2893

            
2894
$order = $dbi->order;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2895
$order->prepend(["$table1-$key1"], ["$table1-$key2", 'desc']);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2896
$result = $dbi->select(table => $table1,
2897
  column => [[$key1 => "$table1-$key1"], [$key2 => "$table1-$key2"]],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2898
  append => $order);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2899
is_deeply($result->all, [{"$table1-$key1" => 1, "$table1-$key2" => 3},
2900
  {"$table1-$key1" => 1, "$table1-$key2" => 1},
2901
  {"$table1-$key1" => 2, "$table1-$key2" => 4},
2902
  {"$table1-$key1" => 2, "$table1-$key2" => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2903

            
2904
test 'tag_parse';
2905
$dbi = DBIx::Custom->connect;
2906
$dbi->tag_parse(0);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2907
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2908
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2909
$dbi->insert({$key1 => 1, $key2 => 1}, table => $table1);
2910
eval {$dbi->execute("select * from $table1 where {= $key1}", {$key1 => 1})};
test cleanup
Yuki Kimoto authored on 2011-08-10
2911
ok($@);
2912

            
2913
test 'last_sql';
2914
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2915
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2916
$dbi->execute($create_table1);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2917
$dbi->execute("select * from $table1");
cleanup test
Yuki Kimoto authored on 2011-08-15
2918
is($dbi->last_sql, "select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
2919

            
2920
eval{$dbi->execute("aaa")};
cleanup test
Yuki Kimoto authored on 2011-08-15
2921
is($dbi->last_sql, 'aaa');
test cleanup
Yuki Kimoto authored on 2011-08-10
2922

            
2923
test 'DBIx::Custom header';
2924
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2925
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2926
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2927
$result = $dbi->execute("select $key1 as h1, $key2 as h2 from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-15
2928
is_deeply([map { lc } @{$result->header}], [qw/h1 h2/]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2929

            
2930
test 'Named placeholder :name(operater) syntax';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2931
$dbi->execute("drop table $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
2932
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2933
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
2934
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
test cleanup
Yuki Kimoto authored on 2011-08-10
2935

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2936
$source = "select * from $table1 where :${key1}{=} and :${key2}{=}";
2937
$result = $dbi->execute($source, param => {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
2938
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2939
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2940

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2941
$source = "select * from $table1 where :${key1}{ = } and :${key2}{=}";
2942
$result = $dbi->execute($source, param => {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
2943
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2944
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2945

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2946
$source = "select * from $table1 where :${key1}{<} and :${key2}{=}";
2947
$result = $dbi->execute($source, param => {$key1 => 5, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
2948
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2949
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2950

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2951
$source = "select * from $table1 where :$table1.${key1}{=} and :$table1.${key2}{=}";
test cleanup
Yuki Kimoto authored on 2011-08-10
2952
$result = $dbi->execute(
2953
    $source,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2954
    param => {"$table1.$key1" => 1, "$table1.$key2" => 1},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2955
    filter => {"$table1.$key2" => sub { $_[0] * 2 }}
test cleanup
Yuki Kimoto authored on 2011-08-10
2956
);
2957
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2958
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2959

            
2960
test 'high perfomance way';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2961
$dbi->execute("drop table $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
2962
$dbi->execute($create_table1_highperformance);
2963
$rows = [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2964
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
2965
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
2966
];
2967
{
2968
    my $query;
2969
    foreach my $row (@$rows) {
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2970
      $query ||= $dbi->insert($row, table => $table1, query => 1);
2971
      $dbi->execute($query, $row, filter => {$key7 => sub { $_[0] * 2 }});
test cleanup
Yuki Kimoto authored on 2011-08-10
2972
    }
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2973
    is_deeply($dbi->select(table => $table1)->all,
test cleanup
Yuki Kimoto authored on 2011-08-10
2974
      [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2975
          {$key7 => 2, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
2976
          {$key7 => 2, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
2977
      ]
2978
    );
2979
}
2980

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2981
$dbi->execute("drop table $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
2982
$dbi->execute($create_table1_highperformance);
2983
$rows = [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2984
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
2985
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
2986
];
2987
{
2988
    my $query;
2989
    my $sth;
2990
    foreach my $row (@$rows) {
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2991
      $query ||= $dbi->insert($row, table => $table1, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2992
      $sth ||= $query->sth;
2993
      $sth->execute(map { $row->{$_} } sort keys %$row);
2994
    }
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2995
    is_deeply($dbi->select(table => $table1)->all,
test cleanup
Yuki Kimoto authored on 2011-08-10
2996
      [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2997
          {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
2998
          {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
2999
      ]
3000
    );
3001
}
3002

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
3003
$dbi->execute("drop table $table1");
3004
$dbi->execute($create_table1_highperformance);
3005
$rows = [
3006
    {$key7 => 10, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 5},
3007
    {$key7 => 11, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6},
3008
];
3009
{
3010
    $model = $dbi->create_model(table => $table1, primary_key => $key1);
3011
    my $query;
3012
    foreach my $row (@$rows) {
3013
      $query ||= $model->insert($row, id => 1, query => 1);
3014
      $model->execute($query, $row, id => 1, filter => {$key7 => sub { $_[0] * 2 }});
3015
    }
3016
    is_deeply($dbi->select(table => $table1, append => 'order by key2')->all,
3017
      [
3018
          {$key7 => 20, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 5, $key1 => 1},
3019
          {$key7 => 22, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 1},
3020
      ]
3021
    );
3022
}
3023

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3024
test 'result';
3025
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3026
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
3027
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3028
$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1);
3029
$dbi->insert({$key1 => 3, $key2 => 4}, table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
3030

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3031
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
3032
@rows = ();
3033
while (my $row = $result->fetch) {
3034
    push @rows, [@$row];
3035
}
3036
is_deeply(\@rows, [[1, 2], [3, 4]]);
3037

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3038
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
3039
@rows = ();
3040
while (my $row = $result->fetch_hash) {
3041
    push @rows, {%$row};
3042
}
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3043
is_deeply(\@rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
3044

            
3045
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3046
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
3047
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3048
$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1);
3049
$dbi->insert({$key1 => 3, $key2 => 4}, table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
3050

            
3051
test 'fetch_all';
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3052
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
3053
$rows = $result->fetch_all;
3054
is_deeply($rows, [[1, 2], [3, 4]]);
3055

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3056
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
3057
$rows = $result->fetch_hash_all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3058
is_deeply($rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
3059

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3060
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
3061
$result->dbi->filters({three_times => sub { $_[0] * 3}});
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3062
$result->filter({$key1 => 'three_times'});
test cleanup
Yuki Kimoto authored on 2011-08-10
3063

            
3064
$rows = $result->fetch_all;
3065
is_deeply($rows, [[3, 2], [9, 4]], "array");
3066

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3067
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
3068
$result->dbi->filters({three_times => sub { $_[0] * 3}});
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3069
$result->filter({$key1 => 'three_times'});
test cleanup
Yuki Kimoto authored on 2011-08-10
3070
$rows = $result->fetch_hash_all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3071
is_deeply($rows, [{$key1 => 3, $key2 => 2}, {$key1 => 9, $key2 => 4}], "hash");
test cleanup
Yuki Kimoto authored on 2011-08-10
3072

            
3073
test "query_builder";
3074
$datas = [
3075
    # Basic tests
3076
    {   name            => 'placeholder basic',
3077
        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
cleanup test
Yuki Kimoto authored on 2011-08-15
3078
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?",
test cleanup
Yuki Kimoto authored on 2011-08-10
3079
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
3080
    },
3081
    {
3082
        name            => 'placeholder in',
cleanup test
Yuki Kimoto authored on 2011-08-15
3083
        source            => "{in k1 3}",
3084
        sql_expected    => "k1 in (?, ?, ?)",
test cleanup
Yuki Kimoto authored on 2011-08-10
3085
        columns_expected   => [qw/k1 k1 k1/]
3086
    },
3087
    
3088
    # Table name
3089
    {
3090
        name            => 'placeholder with table name',
3091
        source            => "{= a.k1} {= a.k2}",
cleanup test
Yuki Kimoto authored on 2011-08-15
3092
        sql_expected    => "a.k1 = ? a.k2 = ?",
test cleanup
Yuki Kimoto authored on 2011-08-10
3093
        columns_expected  => [qw/a.k1 a.k2/]
3094
    },
3095
    {   
3096
        name            => 'placeholder in with table name',
3097
        source            => "{in a.k1 2} {in b.k2 2}",
cleanup test
Yuki Kimoto authored on 2011-08-15
3098
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?)",
test cleanup
Yuki Kimoto authored on 2011-08-10
3099
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
3100
    },
3101
    {
3102
        name            => 'not contain tag',
3103
        source            => "aaa",
cleanup test
Yuki Kimoto authored on 2011-08-15
3104
        sql_expected    => "aaa",
test cleanup
Yuki Kimoto authored on 2011-08-10
3105
        columns_expected  => [],
3106
    }
3107
];
3108

            
3109
for (my $i = 0; $i < @$datas; $i++) {
3110
    my $data = $datas->[$i];
cleanup
Yuki Kimoto authored on 2011-08-13
3111
    my $dbi = DBIx::Custom->new;
3112
    my $builder = $dbi->query_builder;
test cleanup
Yuki Kimoto authored on 2011-08-10
3113
    my $query = $builder->build_query($data->{source});
3114
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
3115
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
3116
}
3117

            
cleanup
Yuki Kimoto authored on 2011-08-13
3118
$dbi = DBIx::Custom->new;
3119
$builder = $dbi->query_builder;
3120
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
3121
    p => sub {
3122
        my @args = @_;
3123
        
3124
        my $expand    = "? $args[0] $args[1]";
3125
        my $columns = [2];
3126
        return [$expand, $columns];
3127
    }
3128
);
3129

            
3130
$query = $builder->build_query("{p a b}");
cleanup test
Yuki Kimoto authored on 2011-08-15
3131
is($query->{sql}, "? a b", "register_tag sql");
test cleanup
Yuki Kimoto authored on 2011-08-10
3132
is_deeply($query->{columns}, [2], "register_tag columns");
3133

            
3134
eval{$builder->build_query('{? }')};
3135
like($@, qr/\QColumn name must be specified in tag "{? }"/, "? not arguments");
3136

            
3137
eval{$builder->build_query("{a }")};
3138
like($@, qr/\QTag "a" is not registered/, "tag not exist");
3139

            
cleanup
Yuki Kimoto authored on 2011-08-13
3140
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
3141
    q => 'string'
3142
});
3143

            
3144
eval{$builder->build_query("{q}", {})};
3145
like($@, qr/Tag "q" must be sub reference/, "tag not code ref");
3146

            
cleanup
Yuki Kimoto authored on 2011-08-13
3147
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
3148
   r => sub {} 
3149
});
3150

            
3151
eval{$builder->build_query("{r}")};
3152
like($@, qr/\QTag "r" must return [STRING, ARRAY_REFERENCE]/, "tag return noting");
3153

            
cleanup
Yuki Kimoto authored on 2011-08-13
3154
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
3155
   s => sub { return ["a", ""]} 
3156
});
3157

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
3161
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
3162
    t => sub {return ["a", []]}
3163
);
3164

            
3165

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3166
test 'Default tag Error case';
3167
eval{$builder->build_query("{= }")};
3168
like($@, qr/Column name must be specified in tag "{= }"/, "basic '=' : key not exist");
3169

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

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

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

            
3181
test 'variouse source';
3182
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d";
3183
$query = $builder->build_query($source);
3184
is($query->sql, 'a b = ? c { } { = ? } = ? d', "basic : 1");
3185

            
3186
$source = "abc";
3187
$query = $builder->build_query($source);
3188
is($query->sql, 'abc', "basic : 2");
3189

            
3190
$source = "{= a}";
3191
$query = $builder->build_query($source);
3192
is($query->sql, 'a = ?', "only tag");
3193

            
3194
$source = "000";
3195
$query = $builder->build_query($source);
3196
is($query->sql, '000', "contain 0 value");
3197

            
3198
$source = "a {= b} }";
3199
eval{$builder->build_query($source)};
3200
like($@, qr/unexpected "}"/, "error : 1");
3201

            
3202
$source = "a {= {}";
3203
eval{$builder->build_query($source)};
3204
like($@, qr/unexpected "{"/, "error : 2");
3205

            
3206
test 'select() sqlfilter option';
3207
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3208
$dbi->user_table_info($user_table_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3209
eval { $dbi->execute("drop table $table1") };
3210
$dbi->execute($create_table1);
3211
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3212
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
3213
$rows = $dbi->select(
3214
    table => $table1,
3215
    column => $key1,
3216
    sqlfilter => sub {
3217
        my $sql = shift;
test cleanup
Yuki Kimoto authored on 2011-08-15
3218
        $sql = "select * from ( $sql ) t where $key1 = 1";
cleanup test
Yuki Kimoto authored on 2011-08-15
3219
        return $sql;
3220
    }
3221
)->all;
3222
is_deeply($rows, [{$key1 => 1}]);
3223

            
3224
test 'dbi method from model';
3225
$dbi = MyDBI9->connect;
3226
eval { $dbi->execute("drop table $table1") };
3227
$dbi->execute($create_table1);
3228
$dbi->setup_model;
3229
$model = $dbi->model($table1);
3230
eval{$model->execute("select * from $table1")};
3231
ok(!$@);
3232

            
3233
test 'column table option';
3234
$dbi = MyDBI9->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3235
$dbi->user_table_info($user_table_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3236
eval { $dbi->execute("drop table $table1") };
3237
$dbi->execute($create_table1);
3238
eval { $dbi->execute("drop table $table2") };
3239
$dbi->execute($create_table2);
3240
$dbi->setup_model;
3241
$dbi->execute("insert into $table1 ($key1, $key2) values (1, 2)");
3242
$dbi->execute("insert into $table2 ($key1, $key3) values (1, 4)");
3243
$model = $dbi->model($table1);
3244
$result = $model->select(
3245
    column => [
3246
        $model->column($table2, {alias => $table2_alias})
3247
    ],
3248
    where => {"$table2_alias.$key3" => 4}
3249
);
3250
is_deeply($result->one, 
3251
          {"$table2_alias.$key1" => 1, "$table2_alias.$key3" => 4});
3252

            
3253
$dbi->separator('__');
3254
$result = $model->select(
3255
    column => [
3256
        $model->column($table2, {alias => $table2_alias})
3257
    ],
3258
    where => {"$table2_alias.$key3" => 4}
3259
);
3260
is_deeply($result->one, 
3261
          {"${table2_alias}__$key1" => 1, "${table2_alias}__$key3" => 4});
3262

            
3263
$dbi->separator('-');
3264
$result = $model->select(
3265
    column => [
3266
        $model->column($table2, {alias => $table2_alias})
3267
    ],
3268
    where => {"$table2_alias.$key3" => 4}
3269
);
3270
is_deeply($result->one, 
3271
          {"$table2_alias-$key1" => 1, "$table2_alias-$key3" => 4});
3272

            
3273
test 'create_model';
3274
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3275
$dbi->user_table_info($user_table_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3276
eval { $dbi->execute("drop table $table1") };
3277
eval { $dbi->execute("drop table $table2") };
3278
$dbi->execute($create_table1);
3279
$dbi->execute($create_table2);
3280

            
3281
$dbi->create_model(
3282
    table => $table1,
3283
    join => [
3284
       "left outer join $table2 on $table1.$key1 = $table2.$key1"
3285
    ],
3286
    primary_key => [$key1]
3287
);
3288
$model2 = $dbi->create_model(
3289
    table => $table2
3290
);
3291
$dbi->create_model(
3292
    table => $table3,
3293
    filter => [
3294
        $key1 => {in => sub { uc $_[0] }}
3295
    ]
3296
);
3297
$dbi->setup_model;
3298
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3299
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
3300
$model = $dbi->model($table1);
3301
$result = $model->select(
3302
    column => [$model->mycolumn, $model->column($table2)],
3303
    where => {"$table1.$key1" => 1}
3304
);
3305
is_deeply($result->one,
3306
          {$key1 => 1, $key2 => 2, "$table2.$key1" => 1, "$table2.$key3" => 3});
3307
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3});
3308

            
3309
test 'model method';
3310
$dbi = DBIx::Custom->connect;
3311
eval { $dbi->execute("drop table $table2") };
3312
$dbi->execute($create_table2);
3313
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
3314
$model = $dbi->create_model(
3315
    table => $table2
3316
);
3317
$model->method(foo => sub { shift->select(@_) });
3318
is_deeply($model->foo->one, {$key1 => 1, $key3 => 3});
3319

            
3320
test 'update_param';
3321
$dbi = DBIx::Custom->connect;
3322
eval { $dbi->execute("drop table $table1") };
3323
$dbi->execute($create_table1_2);
3324
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3325
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3326

            
3327
$param = {$key2 => 11};
3328
$update_param = $dbi->update_param($param);
3329
$sql = <<"EOS";
3330
update $table1 $update_param
3331
where $key1 = 1
3332
EOS
3333
$dbi->execute($sql, param => $param);
3334
$result = $dbi->execute("select * from $table1 order by $key1", table => $table1);
3335
$rows   = $result->all;
3336
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
3337
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3338
                  "basic");
3339

            
3340

            
3341
$dbi = DBIx::Custom->connect;
3342
eval { $dbi->execute("drop table $table1") };
3343
$dbi->execute($create_table1_2);
3344
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3345
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3346

            
3347
$param = {$key2 => 11, $key3 => 33};
3348
$update_param = $dbi->update_param($param);
3349
$sql = <<"EOS";
3350
update $table1 $update_param
3351
where $key1 = 1
3352
EOS
3353
$dbi->execute($sql, param => $param);
3354
$result = $dbi->execute("select * from $table1 order by $key1", table => $table1);
3355
$rows   = $result->all;
3356
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 33, $key4 => 4, $key5 => 5},
3357
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3358
                  "basic");
3359

            
3360
$dbi = DBIx::Custom->connect;
3361
eval { $dbi->execute("drop table $table1") };
3362
$dbi->execute($create_table1_2);
3363
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3364
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3365

            
3366
$param = {$key2 => 11, $key3 => 33};
3367
$update_param = $dbi->update_param($param, {no_set => 1});
3368
$sql = <<"EOS";
3369
update $table1 set $update_param
3370
where $key1 = 1
3371
EOS
3372
$dbi->execute($sql, param => $param);
3373
$result = $dbi->execute("select * from $table1 order by $key1", table => $table1);
3374
$rows   = $result->all;
3375
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 33, $key4 => 4, $key5 => 5},
3376
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3377
                  "update param no_set");
3378

            
3379
            
3380
eval { $dbi->update_param({";" => 1}) };
3381
like($@, qr/not safety/);
3382

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3384
test 'update_param';
3385
$dbi = DBIx::Custom->connect;
3386
eval { $dbi->execute("drop table $table1") };
3387
$dbi->execute($create_table1_2);
3388
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3389
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
test cleanup
Yuki Kimoto authored on 2011-08-10
3390

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3391
$param = {$key2 => 11};
3392
$update_param = $dbi->assign_param($param);
3393
$sql = <<"EOS";
3394
update $table1 set $update_param
3395
where $key1 = 1
3396
EOS
3397
$dbi->execute($sql, param => $param, table => $table1);
3398
$result = $dbi->execute("select * from $table1 order by $key1");
3399
$rows   = $result->all;
3400
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
3401
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3402
                  "basic");
test cleanup
Yuki Kimoto authored on 2011-08-10
3403

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3404
test 'Model class';
3405
$dbi = MyDBI1->connect;
3406
eval { $dbi->execute("drop table $table1") };
3407
$dbi->execute($create_table1);
3408
$model = $dbi->model($table1);
3409
$model->insert({$key1 => 'a', $key2 => 'b'});
3410
is_deeply($model->list->all, [{$key1 => 'a', $key2 => 'b'}], 'basic');
3411
eval { $dbi->execute("drop table $table2") };
3412
$dbi->execute($create_table2);
3413
$model = $dbi->model($table2);
3414
$model->insert({$key1 => 'a'});
3415
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'basic');
3416
is($dbi->models->{$table1}, $dbi->model($table1));
3417
is($dbi->models->{$table2}, $dbi->model($table2));
3418

            
3419
$dbi = MyDBI4->connect;
3420
eval { $dbi->execute("drop table $table1") };
3421
$dbi->execute($create_table1);
3422
$model = $dbi->model($table1);
3423
$model->insert({$key1 => 'a', $key2 => 'b'});
3424
is_deeply($model->list->all, [{$key1 => 'a', $key2 => 'b'}], 'basic');
3425
eval { $dbi->execute("drop table $table2") };
3426
$dbi->execute($create_table2);
3427
$model = $dbi->model($table2);
3428
$model->insert({$key1 => 'a'});
3429
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'basic');
3430

            
3431
$dbi = MyDBI5->connect;
3432
eval { $dbi->execute("drop table $table1") };
3433
eval { $dbi->execute("drop table $table2") };
3434
$dbi->execute($create_table1);
3435
$dbi->execute($create_table2);
3436
$model = $dbi->model($table2);
3437
$model->insert({$key1 => 'a'});
3438
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'include all model');
3439
$dbi->insert(table => $table1, param => {$key1 => 1});
3440
$model = $dbi->model($table1);
3441
is_deeply($model->list->all, [{$key1 => 1, $key2 => undef}], 'include all model');
3442

            
3443
test 'primary_key';
3444
$dbi = MyDBI1->connect;
3445
$model = $dbi->model($table1);
3446
$model->primary_key([$key1, $key2]);
3447
is_deeply($model->primary_key, [$key1, $key2]);
3448

            
3449
test 'columns';
3450
$dbi = MyDBI1->connect;
3451
$model = $dbi->model($table1);
3452
$model->columns([$key1, $key2]);
3453
is_deeply($model->columns, [$key1, $key2]);
cleanup test
Yuki Kimoto authored on 2011-08-10
3454

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3455
test 'setup_model';
3456
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3457
$dbi->user_table_info($user_table_info);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3458
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3459
eval { $dbi->execute("drop table $table2") };
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
3460

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3461
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-15
3462
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-10
3463
$dbi->setup_model;
cleanup test
Yuki Kimoto authored on 2011-08-15
3464
is_deeply([sort @{$dbi->model($table1)->columns}], [$key1, $key2]);
3465
is_deeply([sort @{$dbi->model($table2)->columns}], [$key1, $key3]);
cleanup test
Yuki Kimoto authored on 2011-08-10
3466

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3467
test 'each_column';
3468
$dbi = DBIx::Custom->connect;
3469
eval { $dbi->execute("drop table ${q}table$p") };
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3470
eval { $dbi->execute("drop table $table1") };
3471
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3472
eval { $dbi->execute("drop table $table3") };
3473
$dbi->execute($create_table1_type);
cleanup test
Yuki Kimoto authored on 2011-08-10
3474
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-15
3475

            
3476
$infos = [];
3477
$dbi->each_column(sub {
3478
    my ($self, $table, $column, $cinfo) = @_;
3479
    
3480
    if ($table =~ /^table\d/i) {
3481
         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
3482
         push @$infos, $info;
3483
    }
3484
});
3485
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3486
is_deeply($infos, 
3487
    [
3488
        [$table1, $key1, $key1],
3489
        [$table1, $key2, $key2],
3490
        [$table2, $key1, $key1],
3491
        [$table2, $key3, $key3]
3492
    ]
3493
    
cleanup test
Yuki Kimoto authored on 2011-08-10
3494
);
cleanup test
Yuki Kimoto authored on 2011-08-16
3495

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3496
test 'each_table';
3497
$dbi = DBIx::Custom->connect;
3498
eval { $dbi->execute("drop table $table1") };
3499
eval { $dbi->execute("drop table $table2") };
3500
$dbi->execute($create_table2);
3501
$dbi->execute($create_table1_type);
cleanup test
Yuki Kimoto authored on 2011-08-10
3502

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3503
$infos = [];
3504
$dbi->each_table(sub {
3505
    my ($self, $table, $table_info) = @_;
3506
    
3507
    if ($table =~ /^table\d/i) {
3508
         my $info = [$table, $table_info->{TABLE_NAME}];
3509
         push @$infos, $info;
3510
    }
3511
});
3512
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3513
is_deeply($infos, 
3514
    [
3515
        [$table1, $table1],
3516
        [$table2, $table2],
3517
    ]
cleanup test
Yuki Kimoto authored on 2011-08-10
3518
);
3519

            
cleanup test
Yuki Kimoto authored on 2011-08-16
3520
$dbi = DBIx::Custom->connect;
3521
eval { $dbi->execute("drop table $table1") };
3522
eval { $dbi->execute("drop table $table2") };
3523
$dbi->execute($create_table2);
3524
$dbi->execute($create_table1_type);
3525

            
3526
$infos = [];
3527
$dbi->user_table_info($user_table_info);
3528
$dbi->each_table(sub {
3529
    my ($self, $table, $table_info) = @_;
3530
    
3531
    if ($table =~ /^table\d/i) {
3532
         my $info = [$table, $table_info->{TABLE_NAME}];
3533
         push @$infos, $info;
3534
    }
3535
});
3536
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3537
is_deeply($infos, 
3538
    [
3539
        [$table1, $table1],
3540
        [$table2, $table2],
3541
        [$table3, $table3],
3542
    ]
3543
);
3544

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3545
test 'type_rule into';
3546
eval { $dbi->execute("drop table $table1") };
3547
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3548
$user_column_info = $dbi->get_column_info(exclude_table => $dbi->exclude_table);
3549

            
3550

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3551
$dbi = DBIx::Custom->connect;
3552
eval { $dbi->execute("drop table $table1") };
3553
$dbi->execute($create_table1_type);
3554

            
cleanup
Yuki Kimoto authored on 2011-08-16
3555
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3556
$dbi->type_rule(
3557
    into1 => {
3558
        $date_typename => sub { '2010-' . $_[0] }
3559
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3560
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3561
$dbi->insert({$key1 => '01-01'}, table => $table1);
3562
$result = $dbi->select(table => $table1);
3563
like($result->one->{$key1}, qr/^2010-01-01/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3564

            
3565
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3566
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3567
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3568
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3569
$dbi->type_rule(
3570
    into1 => [
3571
         [$date_typename, $datetime_typename] => sub {
3572
            my $value = shift;
3573
            $value =~ s/02/03/g;
3574
            return $value;
3575
         }
3576
    ]
3577
);
3578
$dbi->insert({$key1 => '2010-01-02', $key2 => '2010-01-01 01:01:02'}, table => $table1);
3579
$result = $dbi->select(table => $table1);
3580
$row = $result->one;
3581
like($row->{$key1}, qr/^2010-01-03/);
3582
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3583

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3584
$dbi = DBIx::Custom->connect;
3585
eval { $dbi->execute("drop table $table1") };
3586
$dbi->execute($create_table1_type);
3587
$dbi->insert({$key1 => '2010-01-03', $key2 => '2010-01-01 01:01:03'}, table => $table1);
cleanup
Yuki Kimoto authored on 2011-08-16
3588
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3589
$dbi->type_rule(
3590
    into1 => [
3591
        [$date_typename, $datetime_typename] => sub {
3592
            my $value = shift;
3593
            $value =~ s/02/03/g;
3594
            return $value;
3595
        }
3596
    ]
cleanup test
Yuki Kimoto authored on 2011-08-10
3597
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3598
$result = $dbi->execute(
3599
    "select * from $table1 where $key1 = :$key1 and $key2 = :$table1.$key2",
3600
    param => {$key1 => '2010-01-03', "$table1.$key2" => '2010-01-01 01:01:02'}
cleanup test
Yuki Kimoto authored on 2011-08-10
3601
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3602
$row = $result->one;
3603
like($row->{$key1}, qr/^2010-01-03/);
3604
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
3605

            
3606
$dbi = DBIx::Custom->connect;
3607
eval { $dbi->execute("drop table $table1") };
3608
$dbi->execute($create_table1_type);
3609
$dbi->insert({$key1 => '2010-01-03', $key2 => '2010-01-01 01:01:03'}, table => $table1);
cleanup
Yuki Kimoto authored on 2011-08-16
3610
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3611
$dbi->type_rule(
3612
    into1 => [
3613
        [$date_typename, $datetime_typename] => sub {
3614
            my $value = shift;
3615
            $value =~ s/02/03/g;
3616
            return $value;
3617
        }
cleanup test
Yuki Kimoto authored on 2011-08-10
3618
    ]
3619
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3620
$result = $dbi->execute(
3621
    "select * from $table1 where $key1 = :$key1 and $key2 = :$table1.$key2",
3622
    param => {$key1 => '2010-01-02', "$table1.$key2" => '2010-01-01 01:01:02'},
3623
    table => $table1
3624
);
3625
$row = $result->one;
3626
like($row->{$key1}, qr/^2010-01-03/);
3627
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3628

            
3629
$dbi = DBIx::Custom->connect;
3630
eval { $dbi->execute("drop table $table1") };
3631
$dbi->execute($create_table1_type);
3632
$dbi->register_filter(convert => sub {
3633
    my $value = shift || '';
3634
    $value =~ s/02/03/;
3635
    return $value;
3636
});
cleanup
Yuki Kimoto authored on 2011-08-16
3637
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3638
$dbi->type_rule(
3639
    from1 => {
3640
        $date_datatype => 'convert',
3641
    },
3642
    into1 => {
3643
        $date_typename => 'convert',
3644
    }
3645
);
3646
$dbi->insert({$key1 => '2010-02-02'}, table => $table1);
3647
$result = $dbi->select(table => $table1);
3648
like($result->fetch->[0], qr/^2010-03-03/);
3649

            
3650
test 'type_rule and filter order';
3651
$dbi = DBIx::Custom->connect;
3652
eval { $dbi->execute("drop table $table1") };
3653
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3654
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3655
$dbi->type_rule(
3656
    into1 => {
3657
        $date_typename => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3658
    },
3659
    into2 => {
3660
        $date_typename => sub { my $v = shift || ''; $v =~ s/5/6/; return $v }
3661
    },
3662
    from1 => {
3663
        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/7/; return $v }
3664
    },
3665
    from2 => {
3666
        $date_datatype => sub { my $v = shift || ''; $v =~ s/7/8/; return $v }
3667
    }
3668
);
3669
$dbi->insert({$key1 => '2010-01-03'}, 
3670
  table => $table1, filter => {$key1 => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }});
3671
$result = $dbi->select(table => $table1);
3672
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/8/9/; return $v });
3673
like($result->fetch_first->[0], qr/^2010-01-09/);
3674

            
3675

            
3676
$dbi = DBIx::Custom->connect;
3677
eval { $dbi->execute("drop table $table1") };
3678
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3679
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3680
$dbi->type_rule(
3681
    from1 => {
3682
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3683
    },
3684
    from2 => {
3685
        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3686
    },
3687
);
3688
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3689
$result = $dbi->select(table => $table1);
cleanup
Yuki Kimoto authored on 2011-08-16
3690
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3691
$result->type_rule(
3692
    from1 => {
3693
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/6/; return $v }
3694
    },
3695
    from2 => {
3696
        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/8/; return $v }
3697
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3698
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3699
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/8/9/; return $v });
3700
like($result->fetch_first->[0], qr/^2010-01-09/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3701

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3702
test 'type_rule_off';
cleanup test
Yuki Kimoto authored on 2011-08-10
3703
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-15
3704
eval { $dbi->execute("drop table $table1") };
3705
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3706
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3707
$dbi->type_rule(
3708
    from1 => {
3709
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3710
    },
3711
    into1 => {
3712
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3713
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3714
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3715
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule_off => 1);
3716
$result = $dbi->select(table => $table1, type_rule_off => 1);
3717
like($result->type_rule_off->fetch->[0], qr/^2010-01-03/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3718

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3719
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3720
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3721
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3722
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3723
$dbi->type_rule(
3724
    from1 => {
3725
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3726
    },
3727
    into1 => {
3728
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3729
    }
3730
);
3731
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule_off => 1);
3732
$result = $dbi->select(table => $table1, type_rule_off => 1);
3733
like($result->one->{$key1}, qr/^2010-01-04/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3734

            
3735
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3736
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3737
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3738
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3739
$dbi->type_rule(
3740
    from1 => {
3741
        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3742
    },
3743
    into1 => {
3744
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3745
    }
3746
);
3747
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3748
$result = $dbi->select(table => $table1);
3749
like($result->one->{$key1}, qr/^2010-01-05/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3750

            
3751
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3752
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3753
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3754
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3755
$dbi->type_rule(
3756
    from1 => {
3757
        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3758
    },
3759
    into1 => {
3760
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3761
    }
3762
);
3763
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3764
$result = $dbi->select(table => $table1);
3765
like($result->fetch->[0], qr/2010-01-05/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3766

            
3767
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3768
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3769
$dbi->execute($create_table1_type);
3770
$dbi->register_filter(ppp => sub { my $v = shift || ''; $v =~ s/3/4/; return $v });
cleanup
Yuki Kimoto authored on 2011-08-16
3771
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3772
$dbi->type_rule(
3773
    into1 => {
3774
        $date_typename => 'ppp'
3775
    }
3776
);
3777
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3778
$result = $dbi->select(table => $table1);
3779
like($result->one->{$key1}, qr/^2010-01-04/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3780

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3781
eval{$dbi->type_rule(
3782
    into1 => {
3783
        $date_typename => 'pp'
3784
    }
3785
)};
3786
like($@, qr/not registered/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3787

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3788
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3789
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3790
$dbi->execute($create_table1_type);
cleanup test
Yuki Kimoto authored on 2011-08-10
3791
eval {
cleanup test
Yuki Kimoto authored on 2011-08-15
3792
    $dbi->type_rule(
3793
        from1 => {
3794
            Date => sub { $_[0] * 2 },
3795
        }
cleanup test
Yuki Kimoto authored on 2011-08-10
3796
    );
3797
};
cleanup test
Yuki Kimoto authored on 2011-08-15
3798
like($@, qr/lower/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3799

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3800
eval {
3801
    $dbi->type_rule(
3802
        into1 => {
3803
            Date => sub { $_[0] * 2 },
3804
        }
3805
    );
3806
};
3807
like($@, qr/lower/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3808

            
3809
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3810
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3811
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3812
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3813
$dbi->type_rule(
3814
    from1 => {
3815
        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3816
    },
3817
    into1 => {
3818
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3819
    }
3820
);
3821
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3822
$result = $dbi->select(table => $table1);
3823
$result->type_rule_off;
3824
like($result->one->{$key1}, qr/^2010-01-04/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3825

            
3826
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3827
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3828
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3829
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3830
$dbi->type_rule(
3831
    from1 => {
3832
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v },
3833
        $datetime_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3834
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
3835
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3836
$dbi->insert({$key1 => '2010-01-03', $key2 => '2010-01-01 01:01:03'}, table => $table1);
3837
$result = $dbi->select(table => $table1);
3838
$result->type_rule(
3839
    from1 => {
3840
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3841
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3842
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3843
$row = $result->one;
3844
like($row->{$key1}, qr/^2010-01-05/);
3845
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
3846

            
3847
$result = $dbi->select(table => $table1);
3848
$result->type_rule(
3849
    from1 => {
3850
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3851
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3852
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3853
$row = $result->one;
3854
like($row->{$key1}, qr/2010-01-05/);
3855
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3856

            
3857
$result = $dbi->select(table => $table1);
3858
$result->type_rule(
3859
    from1 => {
3860
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3861
    }
3862
);
3863
$row = $result->one;
3864
like($row->{$key1}, qr/2010-01-05/);
3865
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3866

            
3867
$result = $dbi->select(table => $table1);
3868
$result->type_rule(
3869
    from1 => [$date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }]
3870
);
3871
$row = $result->one;
3872
like($row->{$key1}, qr/2010-01-05/);
3873
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3874

            
3875
$dbi->register_filter(five => sub { my $v = shift || ''; $v =~ s/3/5/; return $v });
3876
$result = $dbi->select(table => $table1);
3877
$result->type_rule(
3878
    from1 => [$date_datatype => 'five']
3879
);
3880
$row = $result->one;
3881
like($row->{$key1}, qr/^2010-01-05/);
3882
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
3883

            
3884
$result = $dbi->select(table => $table1);
3885
$result->type_rule(
3886
    from1 => [$date_datatype => undef]
3887
);
3888
$row = $result->one;
3889
like($row->{$key1}, qr/^2010-01-03/);
3890
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3891

            
3892
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3893
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3894
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3895
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3896
$dbi->type_rule(
3897
    from1 => {
3898
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v },
3899
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
3900
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3901
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3902
$result = $dbi->select(table => $table1);
3903
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/4/5/; return $v });
3904
like($result->one->{$key1}, qr/^2010-01-05/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3905

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3906
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3907
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3908
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3909
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3910
$dbi->type_rule(
3911
    from1 => {
3912
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3913
    },
3914
);
3915
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3916
$result = $dbi->select(table => $table1);
3917
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/4/5/; return $v });
3918
like($result->fetch->[0], qr/^2010-01-05/);
test cleanup
Yuki Kimoto authored on 2011-08-10
3919

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3920
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3921
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3922
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3923
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3924
$dbi->type_rule(
3925
    into1 => {
3926
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3927
    },
3928
    into2 => {
3929
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3930
    },
3931
    from1 => {
3932
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/6/; return $v }
3933
    },
3934
    from2 => {
3935
        $date_datatype => sub { my $v = shift || ''; $v =~ s/(3|6)/7/; return $v }
3936
    }
3937
);
3938
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule_off => 1);
3939
$result = $dbi->select(table => $table1);
3940
like($result->type_rule_off->fetch_first->[0], qr/^2010-01-03/);
3941
$result = $dbi->select(table => $table1);
3942
like($result->type_rule_on->fetch_first->[0], qr/^2010-01-07/);
test cleanup
Yuki Kimoto authored on 2011-08-10
3943

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3944
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3945
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3946
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3947
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3948
$dbi->type_rule(
3949
    into1 => {
3950
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3951
    },
3952
    into2 => {
3953
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3954
    },
3955
    from1 => {
3956
        $date_datatype => sub { my $v = shift || ''; $v =~ s/(3|5)/6/; return $v }
3957
    },
3958
    from2 => {
3959
        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/7/; return $v }
3960
    }
3961
);
3962
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule1_off => 1);
3963
$result = $dbi->select(table => $table1);
3964
like($result->type_rule1_off->fetch_first->[0], qr/^2010-01-05/);
3965
$result = $dbi->select(table => $table1);
3966
like($result->type_rule1_on->fetch_first->[0], qr/^2010-01-07/);
test cleanup
Yuki Kimoto authored on 2011-08-10
3967

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3968
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3969
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3970
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3971
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3972
$dbi->type_rule(
3973
    into1 => {
3974
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3975
    },
3976
    into2 => {
3977
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3978
    },
3979
    from1 => {
3980
        $date_datatype => sub { my $v = shift || ''; $v =~ s/5/6/; return $v }
3981
    },
3982
    from2 => {
3983
        $date_datatype => sub { my $v = shift || ''; $v =~ s/(3|6)/7/; return $v }
3984
    }
3985
);
3986
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule2_off => 1);
3987
$result = $dbi->select(table => $table1);
3988
like($result->type_rule2_off->fetch_first->[0], qr/^2010-01-06/);
3989
$result = $dbi->select(table => $table1);
3990
like($result->type_rule2_on->fetch_first->[0], qr/^2010-01-07/);
test cleanup
Yuki Kimoto authored on 2011-08-10
3991

            
added memory leak check test
Yuki Kimoto authored on 2011-08-15
3992
test 'join';
3993
$dbi = DBIx::Custom->connect;
3994
eval { $dbi->execute("drop table $table1") };
3995
$dbi->execute($create_table1);
3996
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3997
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
3998
eval { $dbi->execute("drop table $table2") };
3999
$dbi->execute($create_table2);
4000
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
4001
eval { $dbi->execute("drop table $table3") };
4002
$dbi->execute("create table $table3 ($key3 int, $key4 int)");
4003
$dbi->insert(table => $table3, param => {$key3 => 5, $key4 => 4});
4004
$rows = $dbi->select(
4005
    table => $table1,
4006
    column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
4007
    where   => {"$table1.$key2" => 2},
4008
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1"]
4009
)->all;
4010
is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}]);
4011

            
4012
$rows = $dbi->select(
4013
    table => $table1,
4014
    where   => {$key1 => 1},
4015
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1"]
4016
)->all;
4017
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
4018

            
4019
eval {
4020
    $rows = $dbi->select(
4021
        table => $table1,
4022
        column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
4023
        where   => {"$table1.$key2" => 2},
4024
        join  => {"$table1.$key1" => "$table2.$key1"}
4025
    );
4026
};
4027
like ($@, qr/array/);
4028

            
4029
$rows = $dbi->select(
4030
    table => $table1,
4031
    where   => {$key1 => 1},
4032
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
4033
              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
4034
)->all;
4035
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
4036

            
4037
$rows = $dbi->select(
4038
    column => "$table3.$key4 as ${table3}__$key4",
4039
    table => $table1,
4040
    where   => {"$table1.$key1" => 1},
4041
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
4042
              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
4043
)->all;
4044
is_deeply($rows, [{"${table3}__$key4" => 4}]);
4045

            
4046
$rows = $dbi->select(
4047
    column => "$table1.$key1 as ${table1}__$key1",
4048
    table => $table1,
4049
    where   => {"$table3.$key4" => 4},
4050
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
4051
              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
4052
)->all;
4053
is_deeply($rows, [{"${table1}__$key1" => 1}]);
4054

            
4055
$dbi = DBIx::Custom->connect;
4056
eval { $dbi->execute("drop table $table1") };
4057
$dbi->execute($create_table1);
4058
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
4059
eval { $dbi->execute("drop table $table2") };
4060
$dbi->execute($create_table2);
4061
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
4062
$rows = $dbi->select(
4063
    table => $table1,
4064
    column => "${q}$table1$p.${q}$key1$p as ${q}${table1}_$key1$p, ${q}$table2$p.${q}$key1$p as ${q}${table2}_$key1$p, ${q}$key2$p, ${q}$key3$p",
4065
    where   => {"$table1.$key2" => 2},
4066
    join  => ["left outer join ${q}$table2$p on ${q}$table1$p.${q}$key1$p = ${q}$table2$p.${q}$key1$p"],
4067
)->all;
4068
is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}],
4069
          'quote');
4070

            
4071

            
4072
$dbi = DBIx::Custom->connect;
4073
eval { $dbi->execute("drop table $table1") };
4074
$dbi->execute($create_table1);
4075
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
4076
$sql = <<"EOS";
4077
left outer join (
4078
  select * from $table1 t1
4079
  where t1.$key2 = (
4080
    select max(t2.$key2) from $table1 t2
4081
    where t1.$key1 = t2.$key1
4082
  )
4083
) $table3 on $table1.$key1 = $table3.$key1
4084
EOS
4085
$join = [$sql];
4086
$rows = $dbi->select(
4087
    table => $table1,
4088
    column => "$table3.$key1 as ${table3}__$key1",
4089
    join  => $join
4090
)->all;
4091
is_deeply($rows, [{"${table3}__$key1" => 1}]);
4092

            
4093
$dbi = DBIx::Custom->connect;
4094
eval { $dbi->execute("drop table $table1") };
4095
eval { $dbi->execute("drop table $table2") };
4096
$dbi->execute($create_table1);
4097
$dbi->execute($create_table2);
4098
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
4099
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4});
4100
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
4101
$result = $dbi->select(
4102
    table => $table1,
4103
    join => [
4104
        "left outer join $table2 on $table2.$key2 = '4' and $table1.$key1 = $table2.$key1"
4105
    ]
4106
);
4107
is_deeply($result->all, [{$key1 => 1, $key2 => 2}]);
4108
$result = $dbi->select(
4109
    table => $table1,
4110
    column => [{$table2 => [$key3]}],
4111
    join => [
4112
        "left outer join $table2 on $table2.$key3 = '4' and $table1.$key1 = $table2.$key1"
4113
    ]
4114
);
4115
is_deeply($result->all, [{"$table2.$key3" => 4}]);
4116
$result = $dbi->select(
4117
    table => $table1,
4118
    column => [{$table2 => [$key3]}],
4119
    join => [
4120
        "left outer join $table2 on $table1.$key1 = $table2.$key1 and $table2.$key3 = '4'"
4121
    ]
4122
);
4123
is_deeply($result->all, [{"$table2.$key3" => 4}]);
4124

            
4125
$dbi = DBIx::Custom->connect;
4126
eval { $dbi->execute("drop table $table1") };
4127
eval { $dbi->execute("drop table $table2") };
4128
$dbi->execute($create_table1);
4129
$dbi->execute($create_table2);
4130
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
4131
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4});
4132
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
4133
$result = $dbi->select(
4134
    table => $table1,
4135
    column => [{$table2 => [$key3]}],
4136
    join => [
4137
        {
4138
            clause => "left outer join $table2 on $table2.$key3 = '4' and $table1.$key1 = $table2.$key1",
4139
            table => [$table1, $table2]
4140
        }
4141
    ]
4142
);
4143
is_deeply($result->all, [{"$table2.$key3" => 4}]);
4144

            
4145
test 'columns';
4146
$dbi = MyDBI1->connect;
4147
$model = $dbi->model($table1);
4148

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
4149
test 'count';
4150
$dbi = DBIx::Custom->connect;
4151
eval { $dbi->execute("drop table $table1") };
4152
$dbi->execute($create_table1);
4153
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
4154
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 3});
4155
is($dbi->count(table => $table1), 2);
4156
is($dbi->count(table => $table1, where => {$key2 => 2}), 1);
4157
$model = $dbi->create_model(table => $table1);
4158
is($model->count, 2);
cleanup test
Yuki Kimoto authored on 2011-08-10
4159

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