DBIx-Custom / t / common.t /
Newer Older
4203 lines | 140.636kb
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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2876
test 'map_param';
2877
$dbi = DBIx::Custom->connect;
2878
$param = $dbi->map_param(
2879
    {id => 1, author => 'Ken', price => 1900},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2880
    id => "$table1.id",
2881
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2882
    price => ["$table1.price", {if => sub { $_[0] eq 1900 }}]
cleanup test
Yuki Kimoto authored on 2011-08-10
2883
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2884
is_deeply($param, {"$table1.id" => 1, "$table1.author" => '%Ken%',
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2885
  "$table1.price" => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
2886

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2887
$param = $dbi->map_param(
2888
    {id => 0, author => 0, price => 0},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2889
    id => "$table1.id",
2890
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2891
    price => ["$table1.price", sub { '%' . $_[0] . '%' },
cleanup test
Yuki Kimoto authored on 2011-08-10
2892
      {if => sub { $_[0] eq 0 }}]
2893
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2894
is_deeply($param, {"$table1.id" => 0, "$table1.author" => '%0%', "$table1.price" => '%0%'});
test cleanup
Yuki Kimoto authored on 2011-08-10
2895

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2896
$param = $dbi->map_param(
2897
    {id => '', author => '', price => ''},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2898
    id => "$table1.id",
2899
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2900
    price => ["$table1.price", sub { '%' . $_[0] . '%' },
cleanup test
Yuki Kimoto authored on 2011-08-10
2901
      {if => sub { $_[0] eq 1 }}]
2902
);
2903
is_deeply($param, {});
test cleanup
Yuki Kimoto authored on 2011-08-10
2904

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2905
$param = $dbi->map_param(
2906
    {id => undef, author => undef, price => undef},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2907
    id => "$table1.id",
2908
    price => ["$table1.price", {if => 'exists'}]
cleanup test
Yuki Kimoto authored on 2011-08-10
2909
);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2910
is_deeply($param, {"$table1.price" => undef});
test cleanup
Yuki Kimoto authored on 2011-08-10
2911

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2912
$param = $dbi->map_param(
2913
    {price => 'a'},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2914
    id => ["$table1.id", {if => 'exists'}],
2915
    price => ["$table1.price", sub { '%' . $_[0] }, {if => 'exists'}]
cleanup test
Yuki Kimoto authored on 2011-08-10
2916
);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2917
is_deeply($param, {"$table1.price" => '%a'});
test cleanup
Yuki Kimoto authored on 2011-08-10
2918

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2919
test 'order';
2920
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2921
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2922
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2923
$dbi->insert({$key1 => 1, $key2 => 1}, table => $table1);
2924
$dbi->insert({$key1 => 1, $key2 => 3}, table => $table1);
2925
$dbi->insert({$key1 => 2, $key2 => 2}, table => $table1);
2926
$dbi->insert({$key1 => 2, $key2 => 4}, table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2927
my $order = $dbi->order;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2928
$order->prepend($key1, "$key2 desc");
2929
$result = $dbi->select(table => $table1, append => $order);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2930
is_deeply($result->all, [{$key1 => 1, $key2 => 3}, {$key1 => 1, $key2 => 1},
2931
  {$key1 => 2, $key2 => 4}, {$key1 => 2, $key2 => 2}]);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2932
$order->prepend("$key1 desc");
2933
$result = $dbi->select(table => $table1, append => $order);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2934
is_deeply($result->all, [{$key1 => 2, $key2 => 4}, {$key1 => 2, $key2 => 2},
2935
  {$key1 => 1, $key2 => 3}, {$key1 => 1, $key2 => 1}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2936

            
2937
$order = $dbi->order;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2938
$order->prepend(["$table1-$key1"], ["$table1-$key2", 'desc']);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2939
$result = $dbi->select(table => $table1,
2940
  column => [[$key1 => "$table1-$key1"], [$key2 => "$table1-$key2"]],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2941
  append => $order);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2942
is_deeply($result->all, [{"$table1-$key1" => 1, "$table1-$key2" => 3},
2943
  {"$table1-$key1" => 1, "$table1-$key2" => 1},
2944
  {"$table1-$key1" => 2, "$table1-$key2" => 4},
2945
  {"$table1-$key1" => 2, "$table1-$key2" => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2946

            
2947
test 'tag_parse';
2948
$dbi = DBIx::Custom->connect;
2949
$dbi->tag_parse(0);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2950
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2951
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2952
$dbi->insert({$key1 => 1, $key2 => 1}, table => $table1);
2953
eval {$dbi->execute("select * from $table1 where {= $key1}", {$key1 => 1})};
test cleanup
Yuki Kimoto authored on 2011-08-10
2954
ok($@);
2955

            
2956
test 'last_sql';
2957
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2958
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2959
$dbi->execute($create_table1);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2960
$dbi->execute("select * from $table1");
cleanup test
Yuki Kimoto authored on 2011-08-15
2961
is($dbi->last_sql, "select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
2962

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

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

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

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

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

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2994
$source = "select * from $table1 where :$table1.${key1}{=} and :$table1.${key2}{=}";
test cleanup
Yuki Kimoto authored on 2011-08-10
2995
$result = $dbi->execute(
2996
    $source,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2997
    param => {"$table1.$key1" => 1, "$table1.$key2" => 1},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2998
    filter => {"$table1.$key2" => sub { $_[0] * 2 }}
test cleanup
Yuki Kimoto authored on 2011-08-10
2999
);
3000
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3001
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
3002

            
3003
test 'high perfomance way';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3004
$dbi->execute("drop table $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
3005
$dbi->execute($create_table1_highperformance);
3006
$rows = [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3007
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
3008
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
3009
];
3010
{
3011
    my $query;
3012
    foreach my $row (@$rows) {
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3013
      $query ||= $dbi->insert($row, table => $table1, query => 1);
3014
      $dbi->execute($query, $row, filter => {$key7 => sub { $_[0] * 2 }});
test cleanup
Yuki Kimoto authored on 2011-08-10
3015
    }
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3016
    is_deeply($dbi->select(table => $table1)->all,
test cleanup
Yuki Kimoto authored on 2011-08-10
3017
      [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3018
          {$key7 => 2, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
3019
          {$key7 => 2, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
3020
      ]
3021
    );
3022
}
3023

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3024
$dbi->execute("drop table $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
3025
$dbi->execute($create_table1_highperformance);
3026
$rows = [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3027
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
3028
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
3029
];
3030
{
3031
    my $query;
3032
    my $sth;
3033
    foreach my $row (@$rows) {
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3034
      $query ||= $dbi->insert($row, table => $table1, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
3035
      $sth ||= $query->sth;
3036
      $sth->execute(map { $row->{$_} } sort keys %$row);
3037
    }
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3038
    is_deeply($dbi->select(table => $table1)->all,
test cleanup
Yuki Kimoto authored on 2011-08-10
3039
      [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3040
          {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
3041
          {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
3042
      ]
3043
    );
3044
}
3045

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
3046
$dbi->execute("drop table $table1");
3047
$dbi->execute($create_table1_highperformance);
3048
$rows = [
3049
    {$key7 => 10, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 5},
3050
    {$key7 => 11, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6},
3051
];
3052
{
3053
    $model = $dbi->create_model(table => $table1, primary_key => $key1);
3054
    my $query;
3055
    foreach my $row (@$rows) {
3056
      $query ||= $model->insert($row, id => 1, query => 1);
3057
      $model->execute($query, $row, id => 1, filter => {$key7 => sub { $_[0] * 2 }});
3058
    }
3059
    is_deeply($dbi->select(table => $table1, append => 'order by key2')->all,
3060
      [
3061
          {$key7 => 20, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 5, $key1 => 1},
3062
          {$key7 => 22, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 1},
3063
      ]
3064
    );
3065
}
3066

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3067
test 'result';
3068
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3069
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
3070
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3071
$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1);
3072
$dbi->insert({$key1 => 3, $key2 => 4}, table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
3073

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3074
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
3075
@rows = ();
3076
while (my $row = $result->fetch) {
3077
    push @rows, [@$row];
3078
}
3079
is_deeply(\@rows, [[1, 2], [3, 4]]);
3080

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3081
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
3082
@rows = ();
3083
while (my $row = $result->fetch_hash) {
3084
    push @rows, {%$row};
3085
}
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3086
is_deeply(\@rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
3087

            
3088
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3089
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
3090
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3091
$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1);
3092
$dbi->insert({$key1 => 3, $key2 => 4}, table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
3093

            
3094
test 'fetch_all';
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3095
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
3096
$rows = $result->fetch_all;
3097
is_deeply($rows, [[1, 2], [3, 4]]);
3098

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

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

            
3107
$rows = $result->fetch_all;
3108
is_deeply($rows, [[3, 2], [9, 4]], "array");
3109

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

            
3116
test "query_builder";
3117
$datas = [
3118
    # Basic tests
3119
    {   name            => 'placeholder basic',
3120
        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
cleanup test
Yuki Kimoto authored on 2011-08-15
3121
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?",
test cleanup
Yuki Kimoto authored on 2011-08-10
3122
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
3123
    },
3124
    {
3125
        name            => 'placeholder in',
cleanup test
Yuki Kimoto authored on 2011-08-15
3126
        source            => "{in k1 3}",
3127
        sql_expected    => "k1 in (?, ?, ?)",
test cleanup
Yuki Kimoto authored on 2011-08-10
3128
        columns_expected   => [qw/k1 k1 k1/]
3129
    },
3130
    
3131
    # Table name
3132
    {
3133
        name            => 'placeholder with table name',
3134
        source            => "{= a.k1} {= a.k2}",
cleanup test
Yuki Kimoto authored on 2011-08-15
3135
        sql_expected    => "a.k1 = ? a.k2 = ?",
test cleanup
Yuki Kimoto authored on 2011-08-10
3136
        columns_expected  => [qw/a.k1 a.k2/]
3137
    },
3138
    {   
3139
        name            => 'placeholder in with table name',
3140
        source            => "{in a.k1 2} {in b.k2 2}",
cleanup test
Yuki Kimoto authored on 2011-08-15
3141
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?)",
test cleanup
Yuki Kimoto authored on 2011-08-10
3142
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
3143
    },
3144
    {
3145
        name            => 'not contain tag',
3146
        source            => "aaa",
cleanup test
Yuki Kimoto authored on 2011-08-15
3147
        sql_expected    => "aaa",
test cleanup
Yuki Kimoto authored on 2011-08-10
3148
        columns_expected  => [],
3149
    }
3150
];
3151

            
3152
for (my $i = 0; $i < @$datas; $i++) {
3153
    my $data = $datas->[$i];
cleanup
Yuki Kimoto authored on 2011-08-13
3154
    my $dbi = DBIx::Custom->new;
3155
    my $builder = $dbi->query_builder;
test cleanup
Yuki Kimoto authored on 2011-08-10
3156
    my $query = $builder->build_query($data->{source});
3157
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
3158
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
3159
}
3160

            
cleanup
Yuki Kimoto authored on 2011-08-13
3161
$dbi = DBIx::Custom->new;
3162
$builder = $dbi->query_builder;
3163
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
3164
    p => sub {
3165
        my @args = @_;
3166
        
3167
        my $expand    = "? $args[0] $args[1]";
3168
        my $columns = [2];
3169
        return [$expand, $columns];
3170
    }
3171
);
3172

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
3183
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
3184
    q => 'string'
3185
});
3186

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
3190
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
3191
   r => sub {} 
3192
});
3193

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
3197
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
3198
   s => sub { return ["a", ""]} 
3199
});
3200

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
3204
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
3205
    t => sub {return ["a", []]}
3206
);
3207

            
3208

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

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

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

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

            
3224
test 'variouse source';
3225
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d";
3226
$query = $builder->build_query($source);
3227
is($query->sql, 'a b = ? c { } { = ? } = ? d', "basic : 1");
3228

            
3229
$source = "abc";
3230
$query = $builder->build_query($source);
3231
is($query->sql, 'abc', "basic : 2");
3232

            
3233
$source = "{= a}";
3234
$query = $builder->build_query($source);
3235
is($query->sql, 'a = ?', "only tag");
3236

            
3237
$source = "000";
3238
$query = $builder->build_query($source);
3239
is($query->sql, '000', "contain 0 value");
3240

            
3241
$source = "a {= b} }";
3242
eval{$builder->build_query($source)};
3243
like($@, qr/unexpected "}"/, "error : 1");
3244

            
3245
$source = "a {= {}";
3246
eval{$builder->build_query($source)};
3247
like($@, qr/unexpected "{"/, "error : 2");
3248

            
3249
test 'select() sqlfilter option';
3250
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3251
$dbi->user_table_info($user_table_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3252
eval { $dbi->execute("drop table $table1") };
3253
$dbi->execute($create_table1);
3254
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3255
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
3256
$rows = $dbi->select(
3257
    table => $table1,
3258
    column => $key1,
3259
    sqlfilter => sub {
3260
        my $sql = shift;
test cleanup
Yuki Kimoto authored on 2011-08-15
3261
        $sql = "select * from ( $sql ) t where $key1 = 1";
cleanup test
Yuki Kimoto authored on 2011-08-15
3262
        return $sql;
3263
    }
3264
)->all;
3265
is_deeply($rows, [{$key1 => 1}]);
3266

            
3267
test 'dbi method from model';
3268
$dbi = MyDBI9->connect;
3269
eval { $dbi->execute("drop table $table1") };
3270
$dbi->execute($create_table1);
3271
$dbi->setup_model;
3272
$model = $dbi->model($table1);
3273
eval{$model->execute("select * from $table1")};
3274
ok(!$@);
3275

            
3276
test 'column table option';
3277
$dbi = MyDBI9->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3278
$dbi->user_table_info($user_table_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3279
eval { $dbi->execute("drop table $table1") };
3280
$dbi->execute($create_table1);
3281
eval { $dbi->execute("drop table $table2") };
3282
$dbi->execute($create_table2);
3283
$dbi->setup_model;
3284
$dbi->execute("insert into $table1 ($key1, $key2) values (1, 2)");
3285
$dbi->execute("insert into $table2 ($key1, $key3) values (1, 4)");
3286
$model = $dbi->model($table1);
3287
$result = $model->select(
3288
    column => [
3289
        $model->column($table2, {alias => $table2_alias})
3290
    ],
3291
    where => {"$table2_alias.$key3" => 4}
3292
);
3293
is_deeply($result->one, 
3294
          {"$table2_alias.$key1" => 1, "$table2_alias.$key3" => 4});
3295

            
3296
$dbi->separator('__');
3297
$result = $model->select(
3298
    column => [
3299
        $model->column($table2, {alias => $table2_alias})
3300
    ],
3301
    where => {"$table2_alias.$key3" => 4}
3302
);
3303
is_deeply($result->one, 
3304
          {"${table2_alias}__$key1" => 1, "${table2_alias}__$key3" => 4});
3305

            
3306
$dbi->separator('-');
3307
$result = $model->select(
3308
    column => [
3309
        $model->column($table2, {alias => $table2_alias})
3310
    ],
3311
    where => {"$table2_alias.$key3" => 4}
3312
);
3313
is_deeply($result->one, 
3314
          {"$table2_alias-$key1" => 1, "$table2_alias-$key3" => 4});
3315

            
3316
test 'create_model';
3317
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3318
$dbi->user_table_info($user_table_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3319
eval { $dbi->execute("drop table $table1") };
3320
eval { $dbi->execute("drop table $table2") };
3321
$dbi->execute($create_table1);
3322
$dbi->execute($create_table2);
3323

            
3324
$dbi->create_model(
3325
    table => $table1,
3326
    join => [
3327
       "left outer join $table2 on $table1.$key1 = $table2.$key1"
3328
    ],
3329
    primary_key => [$key1]
3330
);
3331
$model2 = $dbi->create_model(
3332
    table => $table2
3333
);
3334
$dbi->create_model(
3335
    table => $table3,
3336
    filter => [
3337
        $key1 => {in => sub { uc $_[0] }}
3338
    ]
3339
);
3340
$dbi->setup_model;
3341
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3342
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
3343
$model = $dbi->model($table1);
3344
$result = $model->select(
3345
    column => [$model->mycolumn, $model->column($table2)],
3346
    where => {"$table1.$key1" => 1}
3347
);
3348
is_deeply($result->one,
3349
          {$key1 => 1, $key2 => 2, "$table2.$key1" => 1, "$table2.$key3" => 3});
3350
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3});
3351

            
3352
test 'model method';
3353
$dbi = DBIx::Custom->connect;
3354
eval { $dbi->execute("drop table $table2") };
3355
$dbi->execute($create_table2);
3356
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
3357
$model = $dbi->create_model(
3358
    table => $table2
3359
);
3360
$model->method(foo => sub { shift->select(@_) });
3361
is_deeply($model->foo->one, {$key1 => 1, $key3 => 3});
3362

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

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

            
3383

            
3384
$dbi = DBIx::Custom->connect;
3385
eval { $dbi->execute("drop table $table1") };
3386
$dbi->execute($create_table1_2);
3387
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3388
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3389

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

            
3403
$dbi = DBIx::Custom->connect;
3404
eval { $dbi->execute("drop table $table1") };
3405
$dbi->execute($create_table1_2);
3406
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3407
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3408

            
3409
$param = {$key2 => 11, $key3 => 33};
3410
$update_param = $dbi->update_param($param, {no_set => 1});
3411
$sql = <<"EOS";
3412
update $table1 set $update_param
3413
where $key1 = 1
3414
EOS
3415
$dbi->execute($sql, param => $param);
3416
$result = $dbi->execute("select * from $table1 order by $key1", table => $table1);
3417
$rows   = $result->all;
3418
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 33, $key4 => 4, $key5 => 5},
3419
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3420
                  "update param no_set");
3421

            
3422
            
3423
eval { $dbi->update_param({";" => 1}) };
3424
like($@, qr/not safety/);
3425

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3427
test 'update_param';
3428
$dbi = DBIx::Custom->connect;
3429
eval { $dbi->execute("drop table $table1") };
3430
$dbi->execute($create_table1_2);
3431
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3432
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
test cleanup
Yuki Kimoto authored on 2011-08-10
3433

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3434
$param = {$key2 => 11};
3435
$update_param = $dbi->assign_param($param);
3436
$sql = <<"EOS";
3437
update $table1 set $update_param
3438
where $key1 = 1
3439
EOS
3440
$dbi->execute($sql, param => $param, table => $table1);
3441
$result = $dbi->execute("select * from $table1 order by $key1");
3442
$rows   = $result->all;
3443
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
3444
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3445
                  "basic");
test cleanup
Yuki Kimoto authored on 2011-08-10
3446

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3447
test 'Model class';
3448
$dbi = MyDBI1->connect;
3449
eval { $dbi->execute("drop table $table1") };
3450
$dbi->execute($create_table1);
3451
$model = $dbi->model($table1);
3452
$model->insert({$key1 => 'a', $key2 => 'b'});
3453
is_deeply($model->list->all, [{$key1 => 'a', $key2 => 'b'}], 'basic');
3454
eval { $dbi->execute("drop table $table2") };
3455
$dbi->execute($create_table2);
3456
$model = $dbi->model($table2);
3457
$model->insert({$key1 => 'a'});
3458
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'basic');
3459
is($dbi->models->{$table1}, $dbi->model($table1));
3460
is($dbi->models->{$table2}, $dbi->model($table2));
3461

            
3462
$dbi = MyDBI4->connect;
3463
eval { $dbi->execute("drop table $table1") };
3464
$dbi->execute($create_table1);
3465
$model = $dbi->model($table1);
3466
$model->insert({$key1 => 'a', $key2 => 'b'});
3467
is_deeply($model->list->all, [{$key1 => 'a', $key2 => 'b'}], 'basic');
3468
eval { $dbi->execute("drop table $table2") };
3469
$dbi->execute($create_table2);
3470
$model = $dbi->model($table2);
3471
$model->insert({$key1 => 'a'});
3472
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'basic');
3473

            
3474
$dbi = MyDBI5->connect;
3475
eval { $dbi->execute("drop table $table1") };
3476
eval { $dbi->execute("drop table $table2") };
3477
$dbi->execute($create_table1);
3478
$dbi->execute($create_table2);
3479
$model = $dbi->model($table2);
3480
$model->insert({$key1 => 'a'});
3481
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'include all model');
3482
$dbi->insert(table => $table1, param => {$key1 => 1});
3483
$model = $dbi->model($table1);
3484
is_deeply($model->list->all, [{$key1 => 1, $key2 => undef}], 'include all model');
3485

            
3486
test 'primary_key';
3487
$dbi = MyDBI1->connect;
3488
$model = $dbi->model($table1);
3489
$model->primary_key([$key1, $key2]);
3490
is_deeply($model->primary_key, [$key1, $key2]);
3491

            
3492
test 'columns';
3493
$dbi = MyDBI1->connect;
3494
$model = $dbi->model($table1);
3495
$model->columns([$key1, $key2]);
3496
is_deeply($model->columns, [$key1, $key2]);
cleanup test
Yuki Kimoto authored on 2011-08-10
3497

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3498
test 'setup_model';
3499
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3500
$dbi->user_table_info($user_table_info);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3501
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3502
eval { $dbi->execute("drop table $table2") };
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
3503

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3504
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-15
3505
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-10
3506
$dbi->setup_model;
cleanup test
Yuki Kimoto authored on 2011-08-15
3507
is_deeply([sort @{$dbi->model($table1)->columns}], [$key1, $key2]);
3508
is_deeply([sort @{$dbi->model($table2)->columns}], [$key1, $key3]);
cleanup test
Yuki Kimoto authored on 2011-08-10
3509

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3510
test 'each_column';
3511
$dbi = DBIx::Custom->connect;
3512
eval { $dbi->execute("drop table ${q}table$p") };
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3513
eval { $dbi->execute("drop table $table1") };
3514
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3515
eval { $dbi->execute("drop table $table3") };
3516
$dbi->execute($create_table1_type);
cleanup test
Yuki Kimoto authored on 2011-08-10
3517
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-15
3518

            
3519
$infos = [];
3520
$dbi->each_column(sub {
3521
    my ($self, $table, $column, $cinfo) = @_;
3522
    
3523
    if ($table =~ /^table\d/i) {
3524
         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
3525
         push @$infos, $info;
3526
    }
3527
});
3528
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3529
is_deeply($infos, 
3530
    [
3531
        [$table1, $key1, $key1],
3532
        [$table1, $key2, $key2],
3533
        [$table2, $key1, $key1],
3534
        [$table2, $key3, $key3]
3535
    ]
3536
    
cleanup test
Yuki Kimoto authored on 2011-08-10
3537
);
cleanup test
Yuki Kimoto authored on 2011-08-16
3538

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3539
test 'each_table';
3540
$dbi = DBIx::Custom->connect;
3541
eval { $dbi->execute("drop table $table1") };
3542
eval { $dbi->execute("drop table $table2") };
3543
$dbi->execute($create_table2);
3544
$dbi->execute($create_table1_type);
cleanup test
Yuki Kimoto authored on 2011-08-10
3545

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3546
$infos = [];
3547
$dbi->each_table(sub {
3548
    my ($self, $table, $table_info) = @_;
3549
    
3550
    if ($table =~ /^table\d/i) {
3551
         my $info = [$table, $table_info->{TABLE_NAME}];
3552
         push @$infos, $info;
3553
    }
3554
});
3555
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3556
is_deeply($infos, 
3557
    [
3558
        [$table1, $table1],
3559
        [$table2, $table2],
3560
    ]
cleanup test
Yuki Kimoto authored on 2011-08-10
3561
);
3562

            
cleanup test
Yuki Kimoto authored on 2011-08-16
3563
$dbi = DBIx::Custom->connect;
3564
eval { $dbi->execute("drop table $table1") };
3565
eval { $dbi->execute("drop table $table2") };
3566
$dbi->execute($create_table2);
3567
$dbi->execute($create_table1_type);
3568

            
3569
$infos = [];
3570
$dbi->user_table_info($user_table_info);
3571
$dbi->each_table(sub {
3572
    my ($self, $table, $table_info) = @_;
3573
    
3574
    if ($table =~ /^table\d/i) {
3575
         my $info = [$table, $table_info->{TABLE_NAME}];
3576
         push @$infos, $info;
3577
    }
3578
});
3579
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3580
is_deeply($infos, 
3581
    [
3582
        [$table1, $table1],
3583
        [$table2, $table2],
3584
        [$table3, $table3],
3585
    ]
3586
);
3587

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3588
test 'type_rule into';
3589
eval { $dbi->execute("drop table $table1") };
3590
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3591
$user_column_info = $dbi->get_column_info(exclude_table => $dbi->exclude_table);
3592

            
3593

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3594
$dbi = DBIx::Custom->connect;
3595
eval { $dbi->execute("drop table $table1") };
3596
$dbi->execute($create_table1_type);
3597

            
cleanup
Yuki Kimoto authored on 2011-08-16
3598
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3599
$dbi->type_rule(
3600
    into1 => {
3601
        $date_typename => sub { '2010-' . $_[0] }
3602
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3603
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3604
$dbi->insert({$key1 => '01-01'}, table => $table1);
3605
$result = $dbi->select(table => $table1);
3606
like($result->one->{$key1}, qr/^2010-01-01/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3607

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3627
$dbi = DBIx::Custom->connect;
3628
eval { $dbi->execute("drop table $table1") };
3629
$dbi->execute($create_table1_type);
3630
$dbi->insert({$key1 => '2010-01-03', $key2 => '2010-01-01 01:01:03'}, table => $table1);
cleanup
Yuki Kimoto authored on 2011-08-16
3631
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3632
$dbi->type_rule(
3633
    into1 => [
3634
        [$date_typename, $datetime_typename] => sub {
3635
            my $value = shift;
3636
            $value =~ s/02/03/g;
3637
            return $value;
3638
        }
3639
    ]
cleanup test
Yuki Kimoto authored on 2011-08-10
3640
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3641
$result = $dbi->execute(
3642
    "select * from $table1 where $key1 = :$key1 and $key2 = :$table1.$key2",
3643
    param => {$key1 => '2010-01-03', "$table1.$key2" => '2010-01-01 01:01:02'}
cleanup test
Yuki Kimoto authored on 2011-08-10
3644
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3645
$row = $result->one;
3646
like($row->{$key1}, qr/^2010-01-03/);
3647
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
3648

            
3649
$dbi = DBIx::Custom->connect;
3650
eval { $dbi->execute("drop table $table1") };
3651
$dbi->execute($create_table1_type);
3652
$dbi->insert({$key1 => '2010-01-03', $key2 => '2010-01-01 01:01:03'}, table => $table1);
cleanup
Yuki Kimoto authored on 2011-08-16
3653
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3654
$dbi->type_rule(
3655
    into1 => [
3656
        [$date_typename, $datetime_typename] => sub {
3657
            my $value = shift;
3658
            $value =~ s/02/03/g;
3659
            return $value;
3660
        }
cleanup test
Yuki Kimoto authored on 2011-08-10
3661
    ]
3662
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3663
$result = $dbi->execute(
3664
    "select * from $table1 where $key1 = :$key1 and $key2 = :$table1.$key2",
3665
    param => {$key1 => '2010-01-02', "$table1.$key2" => '2010-01-01 01:01:02'},
3666
    table => $table1
3667
);
3668
$row = $result->one;
3669
like($row->{$key1}, qr/^2010-01-03/);
3670
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3671

            
3672
$dbi = DBIx::Custom->connect;
3673
eval { $dbi->execute("drop table $table1") };
3674
$dbi->execute($create_table1_type);
3675
$dbi->register_filter(convert => sub {
3676
    my $value = shift || '';
3677
    $value =~ s/02/03/;
3678
    return $value;
3679
});
cleanup
Yuki Kimoto authored on 2011-08-16
3680
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3681
$dbi->type_rule(
3682
    from1 => {
3683
        $date_datatype => 'convert',
3684
    },
3685
    into1 => {
3686
        $date_typename => 'convert',
3687
    }
3688
);
3689
$dbi->insert({$key1 => '2010-02-02'}, table => $table1);
3690
$result = $dbi->select(table => $table1);
3691
like($result->fetch->[0], qr/^2010-03-03/);
3692

            
3693
test 'type_rule and filter order';
3694
$dbi = DBIx::Custom->connect;
3695
eval { $dbi->execute("drop table $table1") };
3696
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3697
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3698
$dbi->type_rule(
3699
    into1 => {
3700
        $date_typename => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3701
    },
3702
    into2 => {
3703
        $date_typename => sub { my $v = shift || ''; $v =~ s/5/6/; return $v }
3704
    },
3705
    from1 => {
3706
        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/7/; return $v }
3707
    },
3708
    from2 => {
3709
        $date_datatype => sub { my $v = shift || ''; $v =~ s/7/8/; return $v }
3710
    }
3711
);
3712
$dbi->insert({$key1 => '2010-01-03'}, 
3713
  table => $table1, filter => {$key1 => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }});
3714
$result = $dbi->select(table => $table1);
3715
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/8/9/; return $v });
3716
like($result->fetch_first->[0], qr/^2010-01-09/);
3717

            
3718

            
3719
$dbi = DBIx::Custom->connect;
3720
eval { $dbi->execute("drop table $table1") };
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
    from2 => {
3728
        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3729
    },
3730
);
3731
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3732
$result = $dbi->select(table => $table1);
cleanup
Yuki Kimoto authored on 2011-08-16
3733
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3734
$result->type_rule(
3735
    from1 => {
3736
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/6/; return $v }
3737
    },
3738
    from2 => {
3739
        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/8/; return $v }
3740
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3741
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3742
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/8/9/; return $v });
3743
like($result->fetch_first->[0], qr/^2010-01-09/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3744

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

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

            
3778
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3779
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3780
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3781
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3782
$dbi->type_rule(
3783
    from1 => {
3784
        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3785
    },
3786
    into1 => {
3787
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3788
    }
3789
);
3790
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3791
$result = $dbi->select(table => $table1);
3792
like($result->one->{$key1}, qr/^2010-01-05/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3793

            
3794
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3795
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3796
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3797
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3798
$dbi->type_rule(
3799
    from1 => {
3800
        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3801
    },
3802
    into1 => {
3803
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3804
    }
3805
);
3806
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3807
$result = $dbi->select(table => $table1);
3808
like($result->fetch->[0], qr/2010-01-05/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3809

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3824
eval{$dbi->type_rule(
3825
    into1 => {
3826
        $date_typename => 'pp'
3827
    }
3828
)};
3829
like($@, qr/not registered/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3830

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3831
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3832
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3833
$dbi->execute($create_table1_type);
cleanup test
Yuki Kimoto authored on 2011-08-10
3834
eval {
cleanup test
Yuki Kimoto authored on 2011-08-15
3835
    $dbi->type_rule(
3836
        from1 => {
3837
            Date => sub { $_[0] * 2 },
3838
        }
cleanup test
Yuki Kimoto authored on 2011-08-10
3839
    );
3840
};
cleanup test
Yuki Kimoto authored on 2011-08-15
3841
like($@, qr/lower/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3842

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3843
eval {
3844
    $dbi->type_rule(
3845
        into1 => {
3846
            Date => sub { $_[0] * 2 },
3847
        }
3848
    );
3849
};
3850
like($@, qr/lower/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3851

            
3852
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3853
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3854
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3855
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3856
$dbi->type_rule(
3857
    from1 => {
3858
        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3859
    },
3860
    into1 => {
3861
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3862
    }
3863
);
3864
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3865
$result = $dbi->select(table => $table1);
3866
$result->type_rule_off;
3867
like($result->one->{$key1}, qr/^2010-01-04/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3868

            
3869
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3870
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3871
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3872
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3873
$dbi->type_rule(
3874
    from1 => {
3875
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v },
3876
        $datetime_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3877
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
3878
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3879
$dbi->insert({$key1 => '2010-01-03', $key2 => '2010-01-01 01:01:03'}, table => $table1);
3880
$result = $dbi->select(table => $table1);
3881
$result->type_rule(
3882
    from1 => {
3883
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3884
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3885
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3886
$row = $result->one;
3887
like($row->{$key1}, qr/^2010-01-05/);
3888
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
3889

            
3890
$result = $dbi->select(table => $table1);
3891
$result->type_rule(
3892
    from1 => {
3893
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3894
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3895
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3896
$row = $result->one;
3897
like($row->{$key1}, qr/2010-01-05/);
3898
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3899

            
3900
$result = $dbi->select(table => $table1);
3901
$result->type_rule(
3902
    from1 => {
3903
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3904
    }
3905
);
3906
$row = $result->one;
3907
like($row->{$key1}, qr/2010-01-05/);
3908
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3909

            
3910
$result = $dbi->select(table => $table1);
3911
$result->type_rule(
3912
    from1 => [$date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }]
3913
);
3914
$row = $result->one;
3915
like($row->{$key1}, qr/2010-01-05/);
3916
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3917

            
3918
$dbi->register_filter(five => sub { my $v = shift || ''; $v =~ s/3/5/; return $v });
3919
$result = $dbi->select(table => $table1);
3920
$result->type_rule(
3921
    from1 => [$date_datatype => 'five']
3922
);
3923
$row = $result->one;
3924
like($row->{$key1}, qr/^2010-01-05/);
3925
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
3926

            
3927
$result = $dbi->select(table => $table1);
3928
$result->type_rule(
3929
    from1 => [$date_datatype => undef]
3930
);
3931
$row = $result->one;
3932
like($row->{$key1}, qr/^2010-01-03/);
3933
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3934

            
3935
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3936
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3937
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3938
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3939
$dbi->type_rule(
3940
    from1 => {
3941
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v },
3942
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
3943
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3944
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3945
$result = $dbi->select(table => $table1);
3946
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/4/5/; return $v });
3947
like($result->one->{$key1}, qr/^2010-01-05/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3948

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3949
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3950
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3951
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3952
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3953
$dbi->type_rule(
3954
    from1 => {
3955
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3956
    },
3957
);
3958
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3959
$result = $dbi->select(table => $table1);
3960
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/4/5/; return $v });
3961
like($result->fetch->[0], qr/^2010-01-05/);
test cleanup
Yuki Kimoto authored on 2011-08-10
3962

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3987
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3988
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3989
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3990
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3991
$dbi->type_rule(
3992
    into1 => {
3993
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3994
    },
3995
    into2 => {
3996
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3997
    },
3998
    from1 => {
3999
        $date_datatype => sub { my $v = shift || ''; $v =~ s/(3|5)/6/; return $v }
4000
    },
4001
    from2 => {
4002
        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/7/; return $v }
4003
    }
4004
);
4005
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule1_off => 1);
4006
$result = $dbi->select(table => $table1);
4007
like($result->type_rule1_off->fetch_first->[0], qr/^2010-01-05/);
4008
$result = $dbi->select(table => $table1);
4009
like($result->type_rule1_on->fetch_first->[0], qr/^2010-01-07/);
test cleanup
Yuki Kimoto authored on 2011-08-10
4010

            
cleanup test
Yuki Kimoto authored on 2011-08-15
4011
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
4012
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
4013
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
4014
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
4015
$dbi->type_rule(
4016
    into1 => {
4017
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
4018
    },
4019
    into2 => {
4020
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
4021
    },
4022
    from1 => {
4023
        $date_datatype => sub { my $v = shift || ''; $v =~ s/5/6/; return $v }
4024
    },
4025
    from2 => {
4026
        $date_datatype => sub { my $v = shift || ''; $v =~ s/(3|6)/7/; return $v }
4027
    }
4028
);
4029
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule2_off => 1);
4030
$result = $dbi->select(table => $table1);
4031
like($result->type_rule2_off->fetch_first->[0], qr/^2010-01-06/);
4032
$result = $dbi->select(table => $table1);
4033
like($result->type_rule2_on->fetch_first->[0], qr/^2010-01-07/);
test cleanup
Yuki Kimoto authored on 2011-08-10
4034

            
added memory leak check test
Yuki Kimoto authored on 2011-08-15
4035
test 'join';
4036
$dbi = DBIx::Custom->connect;
4037
eval { $dbi->execute("drop table $table1") };
4038
$dbi->execute($create_table1);
4039
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
4040
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
4041
eval { $dbi->execute("drop table $table2") };
4042
$dbi->execute($create_table2);
4043
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
4044
eval { $dbi->execute("drop table $table3") };
4045
$dbi->execute("create table $table3 ($key3 int, $key4 int)");
4046
$dbi->insert(table => $table3, param => {$key3 => 5, $key4 => 4});
4047
$rows = $dbi->select(
4048
    table => $table1,
4049
    column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
4050
    where   => {"$table1.$key2" => 2},
4051
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1"]
4052
)->all;
4053
is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}]);
4054

            
4055
$rows = $dbi->select(
4056
    table => $table1,
4057
    where   => {$key1 => 1},
4058
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1"]
4059
)->all;
4060
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
4061

            
4062
eval {
4063
    $rows = $dbi->select(
4064
        table => $table1,
4065
        column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
4066
        where   => {"$table1.$key2" => 2},
4067
        join  => {"$table1.$key1" => "$table2.$key1"}
4068
    );
4069
};
4070
like ($@, qr/array/);
4071

            
4072
$rows = $dbi->select(
4073
    table => $table1,
4074
    where   => {$key1 => 1},
4075
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
4076
              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
4077
)->all;
4078
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
4079

            
4080
$rows = $dbi->select(
4081
    column => "$table3.$key4 as ${table3}__$key4",
4082
    table => $table1,
4083
    where   => {"$table1.$key1" => 1},
4084
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
4085
              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
4086
)->all;
4087
is_deeply($rows, [{"${table3}__$key4" => 4}]);
4088

            
4089
$rows = $dbi->select(
4090
    column => "$table1.$key1 as ${table1}__$key1",
4091
    table => $table1,
4092
    where   => {"$table3.$key4" => 4},
4093
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
4094
              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
4095
)->all;
4096
is_deeply($rows, [{"${table1}__$key1" => 1}]);
4097

            
4098
$dbi = DBIx::Custom->connect;
4099
eval { $dbi->execute("drop table $table1") };
4100
$dbi->execute($create_table1);
4101
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
4102
eval { $dbi->execute("drop table $table2") };
4103
$dbi->execute($create_table2);
4104
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
4105
$rows = $dbi->select(
4106
    table => $table1,
4107
    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",
4108
    where   => {"$table1.$key2" => 2},
4109
    join  => ["left outer join ${q}$table2$p on ${q}$table1$p.${q}$key1$p = ${q}$table2$p.${q}$key1$p"],
4110
)->all;
4111
is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}],
4112
          'quote');
4113

            
4114

            
4115
$dbi = DBIx::Custom->connect;
4116
eval { $dbi->execute("drop table $table1") };
4117
$dbi->execute($create_table1);
4118
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
4119
$sql = <<"EOS";
4120
left outer join (
4121
  select * from $table1 t1
4122
  where t1.$key2 = (
4123
    select max(t2.$key2) from $table1 t2
4124
    where t1.$key1 = t2.$key1
4125
  )
4126
) $table3 on $table1.$key1 = $table3.$key1
4127
EOS
4128
$join = [$sql];
4129
$rows = $dbi->select(
4130
    table => $table1,
4131
    column => "$table3.$key1 as ${table3}__$key1",
4132
    join  => $join
4133
)->all;
4134
is_deeply($rows, [{"${table3}__$key1" => 1}]);
4135

            
4136
$dbi = DBIx::Custom->connect;
4137
eval { $dbi->execute("drop table $table1") };
4138
eval { $dbi->execute("drop table $table2") };
4139
$dbi->execute($create_table1);
4140
$dbi->execute($create_table2);
4141
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
4142
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4});
4143
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
4144
$result = $dbi->select(
4145
    table => $table1,
4146
    join => [
4147
        "left outer join $table2 on $table2.$key2 = '4' and $table1.$key1 = $table2.$key1"
4148
    ]
4149
);
4150
is_deeply($result->all, [{$key1 => 1, $key2 => 2}]);
4151
$result = $dbi->select(
4152
    table => $table1,
4153
    column => [{$table2 => [$key3]}],
4154
    join => [
4155
        "left outer join $table2 on $table2.$key3 = '4' and $table1.$key1 = $table2.$key1"
4156
    ]
4157
);
4158
is_deeply($result->all, [{"$table2.$key3" => 4}]);
4159
$result = $dbi->select(
4160
    table => $table1,
4161
    column => [{$table2 => [$key3]}],
4162
    join => [
4163
        "left outer join $table2 on $table1.$key1 = $table2.$key1 and $table2.$key3 = '4'"
4164
    ]
4165
);
4166
is_deeply($result->all, [{"$table2.$key3" => 4}]);
4167

            
4168
$dbi = DBIx::Custom->connect;
4169
eval { $dbi->execute("drop table $table1") };
4170
eval { $dbi->execute("drop table $table2") };
4171
$dbi->execute($create_table1);
4172
$dbi->execute($create_table2);
4173
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
4174
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4});
4175
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
4176
$result = $dbi->select(
4177
    table => $table1,
4178
    column => [{$table2 => [$key3]}],
4179
    join => [
4180
        {
4181
            clause => "left outer join $table2 on $table2.$key3 = '4' and $table1.$key1 = $table2.$key1",
4182
            table => [$table1, $table2]
4183
        }
4184
    ]
4185
);
4186
is_deeply($result->all, [{"$table2.$key3" => 4}]);
4187

            
4188
test 'columns';
4189
$dbi = MyDBI1->connect;
4190
$model = $dbi->model($table1);
4191

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
4192
test 'count';
4193
$dbi = DBIx::Custom->connect;
4194
eval { $dbi->execute("drop table $table1") };
4195
$dbi->execute($create_table1);
4196
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
4197
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 3});
4198
is($dbi->count(table => $table1), 2);
4199
is($dbi->count(table => $table1, where => {$key2 => 2}), 1);
4200
$model = $dbi->create_model(table => $table1);
4201
is($model->count, 2);
cleanup test
Yuki Kimoto authored on 2011-08-10
4202

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