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

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

            
46
# Variables
47
my $builder;
48
my $datas;
49
my $sth;
50
my $source;
51
my @sources;
52
my $select_source;
53
my $insert_source;
54
my $update_source;
55
my $param;
56
my $params;
57
my $sql;
58
my $result;
59
my $row;
60
my @rows;
61
my $rows;
62
my $query;
63
my @queries;
64
my $select_query;
65
my $insert_query;
66
my $update_query;
67
my $ret_val;
68
my $infos;
69
my $model;
70
my $model2;
71
my $where;
72
my $update_param;
73
my $insert_param;
74
my $join;
75
my $binary;
76

            
test cleanup
Yuki Kimoto authored on 2011-08-15
77
require MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
78
{
79
    package MyDBI4;
80

            
81
    use strict;
82
    use warnings;
83

            
84
    use base 'DBIx::Custom';
85

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

            
97
    package MyModel2::Base1;
98

            
99
    use strict;
100
    use warnings;
101

            
102
    use base 'DBIx::Custom::Model';
103

            
test cleanup
Yuki Kimoto authored on 2011-08-10
104
    package MyModel2::table1;
test cleanup
Yuki Kimoto authored on 2011-08-10
105

            
106
    use strict;
107
    use warnings;
108

            
109
    use base 'MyModel2::Base1';
110

            
111
    sub insert {
112
        my ($self, $param) = @_;
113
        
114
        return $self->SUPER::insert(param => $param);
115
    }
116

            
117
    sub list { shift->select; }
118

            
test cleanup
Yuki Kimoto authored on 2011-08-10
119
    package MyModel2::table2;
test cleanup
Yuki Kimoto authored on 2011-08-10
120

            
121
    use strict;
122
    use warnings;
123

            
124
    use base 'MyModel2::Base1';
125

            
126
    sub insert {
127
        my ($self, $param) = @_;
128
        
129
        return $self->SUPER::insert(param => $param);
130
    }
131

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

            
134
    package MyModel2::TABLE1;
135

            
136
    use strict;
137
    use warnings;
138

            
139
    use base 'MyModel2::Base1';
140

            
141
    sub insert {
142
        my ($self, $param) = @_;
143
        
144
        return $self->SUPER::insert(param => $param);
145
    }
146

            
147
    sub list { shift->select; }
148

            
149
    package MyModel2::TABLE2;
150

            
151
    use strict;
152
    use warnings;
153

            
154
    use base 'MyModel2::Base1';
155

            
156
    sub insert {
157
        my ($self, $param) = @_;
158
        
159
        return $self->SUPER::insert(param => $param);
160
    }
161

            
162
    sub list { shift->select; }
test cleanup
Yuki Kimoto authored on 2011-08-10
163
}
164
{
165
     package MyDBI5;
166

            
167
    use strict;
168
    use warnings;
169

            
170
    use base 'DBIx::Custom';
171

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
236
# Create table
test cleanup
Yuki Kimoto authored on 2011-08-10
237
$dbi = DBIx::Custom->connect;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
238
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
239
$dbi->execute($create_table1);
240
$model = $dbi->create_model(table => $table1);
241
$model->insert({$key1 => 1, $key2 => 2});
242
is_deeply($model->select->all, [{$key1 => 1, $key2 => 2}]);
cleanup
Yuki Kimoto authored on 2011-08-15
243

            
cleanup test
Yuki Kimoto authored on 2011-08-15
244
test 'DBIx::Custom::Result test';
245
$dbi->delete_all(table => $table1);
246
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
247
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
248
$source = "select $key1, $key2 from $table1";
249
$query = $dbi->create_query($source);
250
$result = $dbi->execute($query);
cleanup
Yuki Kimoto authored on 2011-08-15
251

            
cleanup test
Yuki Kimoto authored on 2011-08-15
252
@rows = ();
253
while (my $row = $result->fetch) {
254
    push @rows, [@$row];
255
}
256
is_deeply(\@rows, [[1, 2], [3, 4]], "fetch");
cleanup
Yuki Kimoto authored on 2011-08-15
257

            
cleanup test
Yuki Kimoto authored on 2011-08-15
258
$result = $dbi->execute($query);
259
@rows = ();
260
while (my $row = $result->fetch_hash) {
261
    push @rows, {%$row};
262
}
263
is_deeply(\@rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], "fetch_hash");
test cleanup
Yuki Kimoto authored on 2011-08-10
264

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
273
test 'Insert query return value';
274
$source = "insert into $table1 {insert_param $key1 $key2}";
275
$query = $dbi->execute($source, {}, query => 1);
276
$ret_val = $dbi->execute($query, param => {$key1 => 1, $key2 => 2});
277
ok($ret_val);
test cleanup
Yuki Kimoto authored on 2011-08-10
278

            
cleanup test
Yuki Kimoto authored on 2011-08-15
279
test 'Direct query';
280
$dbi->delete_all(table => $table1);
281
$insert_source = "insert into $table1 {insert_param $key1 $key2}";
282
$dbi->execute($insert_source, param => {$key1 => 1, $key2 => 2});
283
$result = $dbi->execute("select * from $table1");
284
$rows = $result->all;
285
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-08
286

            
287
test 'Filter basic';
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
288
$dbi->delete_all(table => $table1);
cleanup test
Yuki Kimoto authored on 2011-08-08
289
$dbi->register_filter(twice       => sub { $_[0] * 2}, 
290
                    three_times => sub { $_[0] * 3});
291

            
cleanup test
Yuki Kimoto authored on 2011-08-15
292
$insert_source  = "insert into $table1 {insert_param $key1 $key2}";
cleanup test
Yuki Kimoto authored on 2011-08-08
293
$insert_query = $dbi->execute($insert_source, {}, query => 1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
294
$insert_query->filter({$key1 => 'twice'});
295
$dbi->execute($insert_query, param => {$key1 => 1, $key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-15
296
$result = $dbi->execute("select * from $table1");
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
297
$rows = $result->filter({$key2 => 'three_times'})->all;
298
is_deeply($rows, [{$key1 => 2, $key2 => 6}], "filter fetch_filter");
cleanup test
Yuki Kimoto authored on 2011-08-08
299

            
300
test 'Filter in';
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
301
$dbi->delete_all(table => $table1);
cleanup test
Yuki Kimoto authored on 2011-08-15
302
$insert_source  = "insert into $table1 {insert_param $key1 $key2}";
cleanup test
Yuki Kimoto authored on 2011-08-08
303
$insert_query = $dbi->execute($insert_source, {}, query => 1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
304
$dbi->execute($insert_query, param => {$key1 => 2, $key2 => 4});
305
$select_source = "select * from $table1 where {in $table1.$key1 2} and {in $table1.$key2 2}";
cleanup test
Yuki Kimoto authored on 2011-08-08
306
$select_query = $dbi->execute($select_source,{}, query => 1);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
307
$select_query->filter({"$table1.$key1" => 'twice'});
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
308
$result = $dbi->execute($select_query, param => {"$table1.$key1" => [1,5], "$table1.$key2" => [2,4]});
cleanup test
Yuki Kimoto authored on 2011-08-08
309
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
310
is_deeply($rows, [{$key1 => 2, $key2 => 4}], "filter");
cleanup test
Yuki Kimoto authored on 2011-08-08
311

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
318
$source = "select * from $table1 where $key1 = :$key1 and {<> $key2} and {< $key3} and {> $key4} and {>= $key5}";
cleanup test
Yuki Kimoto authored on 2011-08-08
319
$query = $dbi->execute($source, {}, query => 1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
320
$result = $dbi->execute($query, param => {$key1 => 1, $key2 => 3, $key3 => 4, $key4 => 3, $key5 => 5});
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 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}], "basic tag1");
cleanup test
Yuki Kimoto authored on 2011-08-08
323

            
cleanup test
Yuki Kimoto authored on 2011-08-15
324
$source = "select * from $table1 where $key1 = :$key1 and {<> $key2} and {< $key3} and {> $key4} and {>= $key5}";
cleanup test
Yuki Kimoto authored on 2011-08-08
325
$query = $dbi->execute($source, {}, query => 1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
326
$result = $dbi->execute($query, {$key1 => 1, $key2 => 3, $key3 => 4, $key4 => 3, $key5 => 5});
cleanup test
Yuki Kimoto authored on 2011-08-08
327
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
328
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}], "basic tag1");
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} and {like $key2}";
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 => '%2%'});
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 tag2");
cleanup test
Yuki Kimoto authored on 2011-08-08
335

            
336
test 'DIB::Custom::SQLTemplate in tag';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
337
$dbi->execute("drop table $table1");
cleanup test
Yuki Kimoto authored on 2011-08-08
338
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
339
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
340
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
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 {in $key1 2}";
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 => [9, 1]});
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");
cleanup test
Yuki Kimoto authored on 2011-08-08
347

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

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

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

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

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

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
377
$source = "select * from $table1 where $key1 = :$key1 and $key2 = :$key2";
378
$result = $dbi->execute($source, param => {$key1 => 1, $key2 => 2});
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 => 2, $key3 => 3, $key4 => 4, $key5 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-08
381

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

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
392
$source = "select * from $table1 where $key1 = :$table1.$key1 and $key2 = :$table1.$key2";
cleanup test
Yuki Kimoto authored on 2011-08-08
393
$result = $dbi->execute(
394
    $source,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
395
    param => {"$table1.$key1" => 1, "$table1.$key2" => 1},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
396
    filter => {"$table1.$key2" => sub { $_[0] * 2 }}
cleanup test
Yuki Kimoto authored on 2011-08-08
397
);
398
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
399
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-08
400

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
401
$dbi->execute("drop table $table1");
cleanup test
Yuki Kimoto authored on 2011-08-08
402
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
403
$dbi->insert(table => $table1, param => {$key1 => '2011-10-14 12:19:18', $key2 => 2});
404
$source = "select * from $table1 where $key1 = '2011-10-14 12:19:18' and $key2 = :$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 => {$key2 => 2},
cleanup test
Yuki Kimoto authored on 2011-08-08
408
);
409

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
414
$dbi->delete_all(table => $table1);
415
$dbi->insert(table => $table1, param => {$key1 => 'a:b c:d', $key2 => 2});
416
$source = "select * from $table1 where $key1 = 'a\\:b c\\:d' 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
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
422
is_deeply($rows, [{$key1 => 'a:b c:d', $key2 => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-08
423

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

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

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

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
440
$dbi->execute("delete from $table1");
cleanup test
Yuki Kimoto authored on 2011-08-10
441
$dbi->register_filter(
442
    twice       => sub { $_[0] * 2 },
443
    three_times => sub { $_[0] * 3 }
444
);
445
$dbi->default_bind_filter('twice');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
446
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}, filter => {$key1 => 'three_times'});
cleanup test
Yuki Kimoto authored on 2011-08-15
447
$result = $dbi->execute("select * from $table1");
cleanup test
Yuki Kimoto authored on 2011-08-10
448
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
449
is_deeply($rows, [{$key1 => 3, $key2 => 4}], "filter");
cleanup test
Yuki Kimoto authored on 2011-08-10
450
$dbi->default_bind_filter(undef);
451

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
452
$dbi->execute("drop table $table1");
cleanup test
Yuki Kimoto authored on 2011-08-10
453
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
454
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}, append => '   ');
455
$rows = $dbi->select(table => $table1)->all;
456
is_deeply($rows, [{$key1 => 1, $key2 => 2}], 'insert append');
cleanup test
Yuki Kimoto authored on 2011-08-10
457

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

            
461
eval{$dbi->insert(table => 'table', param => {';' => 1})};
462
like($@, qr/safety/);
463

            
cleanup test
Yuki Kimoto authored on 2011-08-10
464
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
465
$dbi->execute($create_table_reserved);
cleanup test
Yuki Kimoto authored on 2011-08-10
466
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
467
$dbi->insert(table => 'table', param => {select => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
468
$result = $dbi->execute("select * from ${q}table$p");
cleanup test
Yuki Kimoto authored on 2011-08-10
469
$rows   = $result->all;
test cleanup
Yuki Kimoto authored on 2011-08-10
470
is_deeply($rows, [{select => 2, update => undef}], "reserved word");
cleanup test
Yuki Kimoto authored on 2011-08-10
471

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
472
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
473
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
474
$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1);
475
$dbi->insert({$key1 => 3, $key2 => 4}, table => $table1);
cleanup test
Yuki Kimoto authored on 2011-08-15
476
$result = $dbi->execute("select * from $table1");
cleanup test
Yuki Kimoto authored on 2011-08-10
477
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
478
is_deeply($rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], "basic");
cleanup test
Yuki Kimoto authored on 2011-08-10
479

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
488
test 'update';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
489
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
490
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
491
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
492
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
493
$dbi->update(table => $table1, param => {$key2 => 11}, where => {$key1 => 1});
cleanup test
Yuki Kimoto authored on 2011-08-15
494
$result = $dbi->execute("select * from $table1 order by $key1");
test cleanup
Yuki Kimoto authored on 2011-08-10
495
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
496
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
497
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
498
                  "basic");
499
                  
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
500
$dbi->execute("delete from $table1");
501
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
502
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
503
$dbi->update(table => $table1, param => {$key2 => 12}, where => {$key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-15
504
$result = $dbi->execute("select * from $table1 order by $key1");
test cleanup
Yuki Kimoto authored on 2011-08-10
505
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
506
is_deeply($rows, [{$key1 => 1, $key2 => 12, $key3 => 3, $key4 => 4, $key5 => 5},
507
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
508
                  "update key same as search key");
509

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
517
$dbi->execute("delete from $table1");
518
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
519
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
test cleanup
Yuki Kimoto authored on 2011-08-10
520
$dbi->register_filter(twice => sub { $_[0] * 2 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
521
$dbi->update(table => $table1, param => {$key2 => 11}, where => {$key1 => 1},
522
              filter => {$key2 => sub { $_[0] * 2 }});
cleanup test
Yuki Kimoto authored on 2011-08-15
523
$result = $dbi->execute("select * from $table1 order by $key1");
test cleanup
Yuki Kimoto authored on 2011-08-10
524
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
525
is_deeply($rows, [{$key1 => 1, $key2 => 22, $key3 => 3, $key4 => 4, $key5 => 5},
526
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
527
                  "filter");
528

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

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

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

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
537
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
538
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
539
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
540
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
541
$where->clause(['and', "$key1 = :$key1", "$key2 = :$key2"]);
542
$where->param({$key1 => 1, $key2 => 2});
543
$dbi->update(table => $table1, param => {$key1 => 3}, where => $where);
544
$result = $dbi->select(table => $table1);
545
is_deeply($result->all, [{$key1 => 3, $key2 => 2}], 'update() where');
test cleanup
Yuki Kimoto authored on 2011-08-10
546

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
547
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
548
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
549
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
550
$dbi->update(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
551
    table => $table1,
552
    param => {$key1 => 3},
test cleanup
Yuki Kimoto authored on 2011-08-10
553
    where => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
554
        ['and', "$key1 = :$key1", "$key2 = :$key2"],
555
        {$key1 => 1, $key2 => 2}
test cleanup
Yuki Kimoto authored on 2011-08-10
556
    ]
557
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
558
$result = $dbi->select(table => $table1);
559
is_deeply($result->all, [{$key1 => 3, $key2 => 2}], 'update() where');
test cleanup
Yuki Kimoto authored on 2011-08-10
560

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
561
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
562
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
563
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
564
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
565
$where->clause(['and', "$key2 = :$key2"]);
566
$where->param({$key2 => 2});
567
$dbi->update(table => $table1, param => {$key1 => 3}, where => $where);
568
$result = $dbi->select(table => $table1);
569
is_deeply($result->all, [{$key1 => 3, $key2 => 2}], 'update() where');
test cleanup
Yuki Kimoto authored on 2011-08-10
570

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

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

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
577
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
578
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
579
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
580
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
581
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
582
$dbi->insert(table => 'table', param => {select => 1});
583
$dbi->update(table => 'table', where => {select => 1}, param => {update => 2});
584
$result = $dbi->execute("select * from ${q}table$p");
585
$rows   = $result->all;
586
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
587

            
588
eval {$dbi->update_all(table => 'table', param => {';' => 2}) };
589
like($@, qr/safety/);
590

            
591
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
592
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
593
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
594
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
595
$dbi->insert(table => 'table', param => {select => 1});
596
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
597
$result = $dbi->execute("select * from ${q}table$p");
598
$rows   = $result->all;
599
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
600

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
601
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
602
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
603
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
604
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
605
$dbi->update({$key2 => 11}, table => $table1, where => {$key1 => 1});
cleanup test
Yuki Kimoto authored on 2011-08-15
606
$result = $dbi->execute("select * from $table1 order by $key1");
test cleanup
Yuki Kimoto authored on 2011-08-10
607
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
608
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
609
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
610
                  "basic");
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_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
614
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
615
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
616
$dbi->update(table => $table1, param => {$key2 => \"'11'"}, where => {$key1 => 1});
cleanup test
Yuki Kimoto authored on 2011-08-15
617
$result = $dbi->execute("select * from $table1 order by $key1");
test cleanup
Yuki Kimoto authored on 2011-08-10
618
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
619
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
620
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
621
                  "basic");
622

            
623
test 'update_all';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
624
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
625
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
626
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
627
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
test cleanup
Yuki Kimoto authored on 2011-08-10
628
$dbi->register_filter(twice => sub { $_[0] * 2 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
629
$dbi->update_all(table => $table1, param => {$key2 => 10}, filter => {$key2 => 'twice'});
cleanup test
Yuki Kimoto authored on 2011-08-15
630
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
631
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
632
is_deeply($rows, [{$key1 => 1, $key2 => 20, $key3 => 3, $key4 => 4, $key5 => 5},
633
                  {$key1 => 6, $key2 => 20, $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
634
                  "filter");
635

            
636

            
637
test 'delete';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
638
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
639
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
640
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
641
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
642
$dbi->delete(table => $table1, where => {$key1 => 1});
cleanup test
Yuki Kimoto authored on 2011-08-15
643
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
644
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
645
is_deeply($rows, [{$key1 => 3, $key2 => 4}], "basic");
test cleanup
Yuki Kimoto authored on 2011-08-10
646

            
cleanup test
Yuki Kimoto authored on 2011-08-15
647
$dbi->execute("delete from $table1");
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
648
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
649
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-10
650
$dbi->register_filter(twice => sub { $_[0] * 2 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
651
$dbi->delete(table => $table1, where => {$key2 => 1}, filter => {$key2 => 'twice'});
cleanup test
Yuki Kimoto authored on 2011-08-15
652
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
653
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
654
is_deeply($rows, [{$key1 => 3, $key2 => 4}], "filter");
test cleanup
Yuki Kimoto authored on 2011-08-10
655

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
658
$dbi->delete_all(table => $table1);
659
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
660
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
661
$dbi->delete(table => $table1, where => {$key1 => 1, $key2 => 2});
662
$rows = $dbi->select(table => $table1)->all;
663
is_deeply($rows, [{$key1 => 3, $key2 => 4}], "delete multi key");
test cleanup
Yuki Kimoto authored on 2011-08-10
664

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

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
668
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
669
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
670
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
671
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-10
672
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
673
$where->clause(['and', "$key1 = :$key1", "$key2 = :$key2"]);
674
$where->param({ke1 => 1, $key2 => 2});
675
$dbi->delete(table => $table1, where => $where);
676
$result = $dbi->select(table => $table1);
677
is_deeply($result->all, [{$key1 => 3, $key2 => 4}], 'delete() where');
test cleanup
Yuki Kimoto authored on 2011-08-10
678

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
679
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
680
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
681
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
682
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-10
683
$dbi->delete(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
684
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
685
    where => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
686
        ['and', "$key1 = :$key1", "$key2 = :$key2"],
687
        {ke1 => 1, $key2 => 2}
test cleanup
Yuki Kimoto authored on 2011-08-10
688
    ]
689
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
690
$result = $dbi->select(table => $table1);
691
is_deeply($result->all, [{$key1 => 3, $key2 => 4}], 'delete() where');
test cleanup
Yuki Kimoto authored on 2011-08-10
692

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
693
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
694
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
695
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
696
$dbi->delete(table => $table1, where => {$key1 => 1}, prefix => '    ');
cleanup test
Yuki Kimoto authored on 2011-08-15
697
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
698
$rows   = $result->all;
699
is_deeply($rows, [], "basic");
700

            
701
test 'delete error';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
702
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
703
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
704
eval{$dbi->delete(table => $table1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
705
like($@, qr/"where" must be specified/,
706
         "where key-value pairs not specified");
707

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
711
$dbi = undef;
test cleanup
Yuki Kimoto authored on 2011-08-10
712
$dbi = DBIx::Custom->connect;
713
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
714
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
715
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
716
$dbi->insert(table => 'table', param => {select => 1});
717
$dbi->delete(table => 'table', where => {select => 1});
718
$result = $dbi->execute("select * from ${q}table$p");
719
$rows   = $result->all;
720
is_deeply($rows, [], "reserved word");
721

            
722
test 'delete_all';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
723
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
724
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
725
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
726
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
727
$dbi->delete_all(table => $table1);
cleanup test
Yuki Kimoto authored on 2011-08-15
728
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
729
$rows   = $result->all;
730
is_deeply($rows, [], "basic");
731

            
732

            
733
test 'select';
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
$rows = $dbi->select(table => $table1)->all;
739
is_deeply($rows, [{$key1 => 1, $key2 => 2},
740
                  {$key1 => 3, $key2 => 4}], "table");
test cleanup
Yuki Kimoto authored on 2011-08-10
741

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

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

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

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
756
eval { $dbi->execute("drop table $table2") };
test cleanup
Yuki Kimoto authored on 2011-08-10
757
$dbi->execute($create_table2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
758
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
test cleanup
Yuki Kimoto authored on 2011-08-10
759
$rows = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
760
    table => [$table1, $table2],
761
    column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
762
    where   => {"$table1.$key2" => 2},
763
    relation  => {"$table1.$key1" => "$table2.$key1"}
test cleanup
Yuki Kimoto authored on 2011-08-10
764
)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
765
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
766

            
767
$rows = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
768
    table => [$table1, $table2],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
769
    column => ["$table1.$key1 as ${table1}_$key1", "${table2}.$key1 as ${table2}_$key1", $key2, $key3],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
770
    relation  => {"$table1.$key1" => "$table2.$key1"}
test cleanup
Yuki Kimoto authored on 2011-08-10
771
)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
772
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
773

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

            
777
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
778
eval { $dbi->execute("drop table ${q}table$p") };
779
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
780
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
781
$dbi->insert(table => 'table', param => {select => 1, update => 2});
782
$result = $dbi->select(table => 'table', where => {select => 1});
783
$rows   = $result->all;
784
is_deeply($rows, [{select => 2, update => 2}], "reserved word");
785

            
786
test 'fetch filter';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
787
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
788
$dbi->register_filter(
789
    twice       => sub { $_[0] * 2 },
790
    three_times => sub { $_[0] * 3 }
791
);
792
$dbi->default_fetch_filter('twice');
793
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
794
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
795
$result = $dbi->select(table => $table1);
796
$result->filter({$key1 => 'three_times'});
test cleanup
Yuki Kimoto authored on 2011-08-10
797
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
798
is_deeply($row, {$key1 => 3, $key2 => 4}, "default_fetch_filter and filter");
test cleanup
Yuki Kimoto authored on 2011-08-10
799

            
800
test 'filters';
801
$dbi = DBIx::Custom->new;
802

            
803
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')),
804
   'あ', "decode_utf8");
805

            
806
is($dbi->filters->{encode_utf8}->('あ'),
807
   encode_utf8('あ'), "encode_utf8");
808

            
cleanup test
Yuki Kimoto authored on 2011-08-10
809
test 'transaction1';
test cleanup
Yuki Kimoto authored on 2011-08-10
810
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
811
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
812
$dbi->execute($create_table1);
fixed transaction test
Yuki Kimoto authored on 2011-08-14
813
$dbi->begin_work;
814
$dbi->dbh->{AutoCommit} = 0;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
815
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
fixed transaction test
Yuki Kimoto authored on 2011-08-14
816
$dbi->rollback;
817
$dbi->dbh->{AutoCommit} = 1;
818

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

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

            
823
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
824
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
825
$dbi->execute($create_table1);
fixed transaction test
Yuki Kimoto authored on 2011-08-14
826
$dbi->begin_work;
827
$dbi->dbh->{AutoCommit} = 0;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
828
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
829
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
fixed transaction test
Yuki Kimoto authored on 2011-08-14
830
$dbi->commit;
831
$dbi->dbh->{AutoCommit} = 1;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
832
$result = $dbi->select(table => $table1);
833
is_deeply(scalar $result->all, [{$key1 => 1, $key2 => 2}, {$key1 => 2, $key2 => 3}],
fixed transaction test
Yuki Kimoto authored on 2011-08-14
834
          "commit");
test cleanup
Yuki Kimoto authored on 2011-08-10
835

            
836
test 'execute';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
837
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
838
$dbi->execute($create_table1);
839
{
840
    local $Carp::Verbose = 0;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
841
    eval{$dbi->execute("select * frm $table1")};
cleanup test
Yuki Kimoto authored on 2011-08-15
842
    like($@, qr/\Qselect * frm $table1/, "fail prepare");
test cleanup
Yuki Kimoto authored on 2011-08-10
843
    like($@, qr/\.t /, "fail : not verbose");
844
}
845
{
846
    local $Carp::Verbose = 1;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
847
    eval{$dbi->execute("select * frm $table1")};
test cleanup
Yuki Kimoto authored on 2011-08-10
848
    like($@, qr/Custom.*\.t /s, "fail : verbose");
849
}
850

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

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

            
859
{
860
    local $Carp::Verbose = 0;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
861
    eval{$dbi->execute("select * from $table1 where {0 $key1}", {}, query => 1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
862
    like($@, qr/\Q.t /, "caller spec : not vebose");
863
}
864
{
865
    local $Carp::Verbose = 1;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
866
    eval{$dbi->execute("select * from $table1 where {0 $key1}", {}, query => 1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
867
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
868
}
869

            
870

            
cleanup test
Yuki Kimoto authored on 2011-08-10
871
test 'transaction2';
test cleanup
Yuki Kimoto authored on 2011-08-10
872
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
873
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
874
$dbi->execute($create_table1);
875

            
876
$dbi->begin_work;
877

            
878
eval {
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
879
    $dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
880
    die "Error";
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
881
    $dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-10
882
};
883

            
884
$dbi->rollback if $@;
885

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

            
890
$dbi->begin_work;
891

            
892
eval {
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
893
    $dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
894
    $dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-10
895
};
896

            
897
$dbi->commit unless $@;
898

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

            
903
$dbi->dbh->{AutoCommit} = 0;
904
eval{ $dbi->begin_work };
905
ok($@, "exception");
906
$dbi->dbh->{AutoCommit} = 1;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
907

            
test cleanup
Yuki Kimoto authored on 2011-08-10
908
test 'cache';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
909
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
910
$dbi->cache(1);
911
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-15
912
$source = "select * from $table1 where $key1 = :$key1 and $key2 = :$key2";
test cleanup
Yuki Kimoto authored on 2011-08-10
913
$dbi->execute($source, {}, query => 1);
914
is_deeply($dbi->{_cached}->{$source}, 
cleanup test
Yuki Kimoto authored on 2011-08-15
915
          {sql => "select * from $table1 where $key1 = ? and $key2 = ?", columns => [$key1, $key2], tables => []}, "cache");
test cleanup
Yuki Kimoto authored on 2011-08-10
916

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
917
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
918
$dbi->execute($create_table1);
919
$dbi->{_cached} = {};
920
$dbi->cache(0);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
921
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
922
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
923

            
924
test 'execute';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
925
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
926
$dbi->execute($create_table1);
927
{
928
    local $Carp::Verbose = 0;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
929
    eval{$dbi->execute("select * frm $table1")};
cleanup test
Yuki Kimoto authored on 2011-08-15
930
    like($@, qr/\Qselect * frm $table1/, "fail prepare");
test cleanup
Yuki Kimoto authored on 2011-08-10
931
    like($@, qr/\.t /, "fail : not verbose");
932
}
933
{
934
    local $Carp::Verbose = 1;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
935
    eval{$dbi->execute('select * frm $table1')};
test cleanup
Yuki Kimoto authored on 2011-08-10
936
    like($@, qr/Custom.*\.t /s, "fail : verbose");
937
}
938

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

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

            
947
{
948
    local $Carp::Verbose = 0;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
949
    eval{$dbi->execute("select * from $table1 where {0 $key1}", {}, query => 1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
950
    like($@, qr/\Q.t /, "caller spec : not vebose");
951
}
952
{
953
    local $Carp::Verbose = 1;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
954
    eval{$dbi->execute("select * from $table1 where {0 $key1}", {}, query => 1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
955
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
956
}
957

            
958
test 'method';
959
$dbi->method(
960
    one => sub { 1 }
961
);
962
$dbi->method(
963
    two => sub { 2 }
964
);
965
$dbi->method({
966
    twice => sub {
967
        my $self = shift;
968
        return $_[0] * 2;
969
    }
970
});
971

            
972
is($dbi->one, 1, "first");
973
is($dbi->two, 2, "second");
974
is($dbi->twice(5), 10 , "second");
975

            
976
eval {$dbi->XXXXXX};
977
ok($@, "not exists");
978

            
979
test 'out filter';
980
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
981
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
982
$dbi->execute($create_table1);
983
$dbi->register_filter(twice => sub { $_[0] * 2 });
984
$dbi->register_filter(three_times => sub { $_[0] * 3});
985
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
986
    $table1, $key1 => {out => 'twice', in => 'three_times'}, 
987
              $key2 => {out => 'three_times', in => 'twice'});
988
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-15
989
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
990
$row   = $result->fetch_hash_first;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
991
is_deeply($row, {$key1 => 2, $key2 => 6}, "insert");
992
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
993
$row   = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
994
is_deeply($row, {$key1 => 6, $key2 => 12}, "insert");
test cleanup
Yuki Kimoto authored on 2011-08-10
995

            
996
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
997
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
998
$dbi->execute($create_table1);
999
$dbi->register_filter(twice => sub { $_[0] * 2 });
1000
$dbi->register_filter(three_times => sub { $_[0] * 3});
1001
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1002
    $table1, $key1 => {out => 'twice', in => 'three_times'}, 
1003
              $key2 => {out => 'three_times', in => 'twice'});
test cleanup
Yuki Kimoto authored on 2011-08-10
1004
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1005
    $table1, $key1 => {out => undef}
test cleanup
Yuki Kimoto authored on 2011-08-10
1006
); 
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1007
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-15
1008
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
1009
$row   = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1010
is_deeply($row, {$key1 => 1, $key2 => 6}, "insert");
test cleanup
Yuki Kimoto authored on 2011-08-10
1011

            
1012
$dbi = DBIx::Custom->connect;
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->register_filter(twice => sub { $_[0] * 2 });
1016
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1017
    $table1, $key1 => {out => 'twice', in => 'twice'}
test cleanup
Yuki Kimoto authored on 2011-08-10
1018
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1019
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}, filter => {$key1 => undef});
1020
$dbi->update(table => $table1, param => {$key1 => 2}, where => {$key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-15
1021
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
1022
$row   = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1023
is_deeply($row, {$key1 => 4, $key2 => 2}, "update");
test cleanup
Yuki Kimoto authored on 2011-08-10
1024

            
1025
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1026
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1027
$dbi->execute($create_table1);
1028
$dbi->register_filter(twice => sub { $_[0] * 2 });
1029
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1030
    $table1, $key1 => {out => 'twice', in => 'twice'}
test cleanup
Yuki Kimoto authored on 2011-08-10
1031
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1032
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 2}, filter => {$key1=> undef});
1033
$dbi->delete(table => $table1, where => {$key1 => 1});
cleanup test
Yuki Kimoto authored on 2011-08-15
1034
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
1035
$rows   = $result->all;
1036
is_deeply($rows, [], "delete");
1037

            
1038
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1039
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1040
$dbi->execute($create_table1);
1041
$dbi->register_filter(twice => sub { $_[0] * 2 });
1042
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1043
    $table1, $key1 => {out => 'twice', in => 'twice'}
test cleanup
Yuki Kimoto authored on 2011-08-10
1044
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1045
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 2}, filter => {$key1 => undef});
1046
$result = $dbi->select(table => $table1, where => {$key1 => 1});
1047
$result->filter({$key2 => 'twice'});
test cleanup
Yuki Kimoto authored on 2011-08-10
1048
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1049
is_deeply($rows, [{$key1 => 4, $key2 => 4}], "select");
test cleanup
Yuki Kimoto authored on 2011-08-10
1050

            
1051
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1052
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1053
$dbi->execute($create_table1);
1054
$dbi->register_filter(twice => sub { $_[0] * 2 });
1055
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1056
    $table1, $key1 => {out => 'twice', in => 'twice'}
test cleanup
Yuki Kimoto authored on 2011-08-10
1057
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1058
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 2}, filter => {$key1 => undef});
cleanup test
Yuki Kimoto authored on 2011-08-15
1059
$result = $dbi->execute("select * from $table1 where $key1 = :$key1 and $key2 = :$key2",
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1060
                        param => {$key1 => 1, $key2 => 2},
1061
                        table => [$table1]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1062
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1063
is_deeply($rows, [{$key1 => 4, $key2 => 2}], "execute");
test cleanup
Yuki Kimoto authored on 2011-08-10
1064

            
1065
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1066
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1067
$dbi->execute($create_table1);
1068
$dbi->register_filter(twice => sub { $_[0] * 2 });
1069
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1070
    $table1, $key1 => {out => 'twice', in => 'twice'}
test cleanup
Yuki Kimoto authored on 2011-08-10
1071
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1072
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 2}, filter => {$key1 => undef});
cleanup test
Yuki Kimoto authored on 2011-08-15
1073
$result = $dbi->execute("select * from {table $table1} where $key1 = :$key1 and $key2 = :$key2",
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1074
                        param => {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
1075
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1076
is_deeply($rows, [{$key1 => 4, $key2 => 2}], "execute table tag");
test cleanup
Yuki Kimoto authored on 2011-08-10
1077

            
1078
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1079
eval { $dbi->execute("drop table $table1") };
1080
eval { $dbi->execute("drop table $table2") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1081
$dbi->execute($create_table1);
1082
$dbi->execute($create_table2);
1083
$dbi->register_filter(twice => sub { $_[0] * 2 });
1084
$dbi->register_filter(three_times => sub { $_[0] * 3 });
1085
$dbi->apply_filter(
cleanup test
Yuki Kimoto authored on 2011-08-15
1086
    $table1, $key2 => {out => 'twice', in => 'twice'}
1087
);
1088
$dbi->apply_filter(
1089
    $table2, $key3 => {out => 'three_times', in => 'three_times'}
test cleanup
Yuki Kimoto authored on 2011-08-10
1090
);
cleanup test
Yuki Kimoto authored on 2011-08-15
1091
$dbi->insert(table => $table1, param => {$key1 => 5, $key2 => 2}, filter => {$key2 => undef});
1092
$dbi->insert(table => $table2, param => {$key1 => 5, $key3 => 6}, filter => {$key3 => undef});
1093
$result = $dbi->select(
1094
     table => [$table1, $table2],
1095
     column => [$key2, $key3],
1096
     where => {"$table1.$key2" => 1, "$table2.$key3" => 2}, relation => {"$table1.$key1" => "$table2.$key1"});
1097

            
1098
$result->filter({$key2 => 'twice'});
1099
$rows   = $result->all;
1100
is_deeply($rows, [{$key2 => 4, $key3 => 18}], "select : join");
1101

            
1102
$result = $dbi->select(
1103
     table => [$table1, $table2],
1104
     column => [$key2, $key3],
1105
     where => {$key2 => 1, $key3 => 2}, relation => {"$table1.$key1" => "$table2.$key1"});
1106

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

            
1111
test 'connect super';
test cleanup
Yuki Kimoto authored on 2011-08-10
1112
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1113
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1114
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1115
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1116
is($dbi->select(table => $table1)->one->{$key1}, 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1117

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1118
$dbi = DBIx::Custom->new;
test cleanup
Yuki Kimoto authored on 2011-08-10
1119
$dbi->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1120
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1121
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1122
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1123
is($dbi->select(table => $table1)->one->{$key1}, 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1124

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1125
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1126
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1127
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1128
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1129
is($dbi->select(table => $table1)->one->{$key1}, 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1130

            
1131
test 'end_filter';
1132
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1133
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1134
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1135
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1136
$result = $dbi->select(table => $table1);
1137
$result->filter($key1 => sub { $_[0] * 2 }, $key2 => sub { $_[0] * 4 });
1138
$result->end_filter($key1 => sub { $_[0] * 3 }, $key2 => sub { $_[0] * 5 });
test cleanup
Yuki Kimoto authored on 2011-08-10
1139
$row = $result->fetch_first;
1140
is_deeply($row, [6, 40]);
1141

            
1142
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1143
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1144
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1145
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1146
$result = $dbi->select(table => $table1);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1147
$result->filter([$key1, $key2] => sub { $_[0] * 2 });
1148
$result->end_filter([[$key1, $key2] => sub { $_[0] * 3 }]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1149
$row = $result->fetch_first;
1150
is_deeply($row, [6, 12]);
1151

            
1152
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1153
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1154
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1155
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1156
$result = $dbi->select(table => $table1);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1157
$result->filter([[$key1, $key2] => sub { $_[0] * 2 }]);
1158
$result->end_filter([$key1, $key2] => sub { $_[0] * 3 });
test cleanup
Yuki Kimoto authored on 2011-08-10
1159
$row = $result->fetch_first;
1160
is_deeply($row, [6, 12]);
1161

            
1162
$dbi->register_filter(five_times => sub { $_[0] * 5 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1163
$result = $dbi->select(table => $table1);
1164
$result->filter($key1 => sub { $_[0] * 2 }, $key2 => sub { $_[0] * 4 });
1165
$result->end_filter({$key1 => sub { $_[0] * 3 }, $key2 => 'five_times' });
test cleanup
Yuki Kimoto authored on 2011-08-10
1166
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1167
is_deeply($row, {$key1 => 6, $key2 => 40});
test cleanup
Yuki Kimoto authored on 2011-08-10
1168

            
1169
$dbi->register_filter(five_times => sub { $_[0] * 5 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1170
$dbi->apply_filter($table1,
1171
    $key1 => {end => sub { $_[0] * 3 } },
1172
    $key2 => {end => 'five_times'}
test cleanup
Yuki Kimoto authored on 2011-08-10
1173
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1174
$result = $dbi->select(table => $table1);
1175
$result->filter($key1 => sub { $_[0] * 2 }, $key2 => sub { $_[0] * 4 });
test cleanup
Yuki Kimoto authored on 2011-08-10
1176
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1177
is_deeply($row, {$key1 => 6, $key2 => 40}, 'apply_filter');
test cleanup
Yuki Kimoto authored on 2011-08-10
1178

            
1179
$dbi->register_filter(five_times => sub { $_[0] * 5 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1180
$dbi->apply_filter($table1,
1181
    $key1 => {end => sub { $_[0] * 3 } },
1182
    $key2 => {end => 'five_times'}
1183
);
1184
$result = $dbi->select(table => $table1);
1185
$result->filter($key1 => sub { $_[0] * 2 }, $key2 => sub { $_[0] * 4 });
1186
$result->filter($key1 => undef);
1187
$result->end_filter($key1 => undef);
test cleanup
Yuki Kimoto authored on 2011-08-10
1188
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1189
is_deeply($row, {$key1 => 1, $key2 => 40}, 'apply_filter overwrite');
test cleanup
Yuki Kimoto authored on 2011-08-10
1190

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1191
test 'remove_end_filter and remove_filter';
1192
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1193
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1194
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1195
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1196
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1197
$row = $result
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1198
       ->filter($key1 => sub { $_[0] * 2 }, $key2 => sub { $_[0] * 4 })
test cleanup
Yuki Kimoto authored on 2011-08-10
1199
       ->remove_filter
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1200
       ->end_filter($key1 => sub { $_[0] * 3 }, $key2 => sub { $_[0] * 5 })
test cleanup
Yuki Kimoto authored on 2011-08-10
1201
       ->remove_end_filter
1202
       ->fetch_first;
1203
is_deeply($row, [1, 2]);
1204

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

            
1214
test 'select query option';
1215
$dbi = DBIx::Custom->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
$query = $dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1219
is(ref $query, 'DBIx::Custom::Query');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1220
$query = $dbi->update(table => $table1, where => {$key1 => 1}, param => {$key2 => 2}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1221
is(ref $query, 'DBIx::Custom::Query');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1222
$query = $dbi->delete(table => $table1, where => {$key1 => 1}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1223
is(ref $query, 'DBIx::Custom::Query');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1224
$query = $dbi->select(table => $table1, where => {$key1 => 1, $key2 => 2}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1225
is(ref $query, 'DBIx::Custom::Query');
1226

            
1227
test 'where';
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
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
1233
$where = $dbi->where->clause(['and', "$key1 = :$key1", "$key2 = :$key2"]);
1234
is("$where", "where ( $key1 = :$key1 and $key2 = :$key2 )", 'no param');
test cleanup
Yuki Kimoto authored on 2011-08-10
1235

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

            
1240
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1241
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1242
    where => $where
1243
);
1244
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1245
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1246

            
1247
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1248
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1249
    where => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1250
        ['and', "$key1 = :$key1", "$key2 = :$key2"],
1251
        {$key1 => 1}
test cleanup
Yuki Kimoto authored on 2011-08-10
1252
    ]
1253
);
1254
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1255
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1256

            
1257
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1258
             ->clause(['and', "$key1 = :$key1", "$key2 = :$key2"])
1259
             ->param({$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
1260
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1261
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1262
    where => $where
1263
);
1264
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1265
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1266

            
1267
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1268
             ->clause(['and', "$key1 = :$key1", "$key2 = :$key2"])
test cleanup
Yuki Kimoto authored on 2011-08-10
1269
             ->param({});
1270
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1271
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1272
    where => $where,
1273
);
1274
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1275
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1276

            
1277
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1278
             ->clause(['and', ['or', "$key1 > :$key1", "$key1 < :$key1"], "$key2 = :$key2"])
1279
             ->param({$key1 => [0, 3], $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
1280
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1281
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1282
    where => $where,
1283
); 
1284
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1285
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1286

            
1287
$where = $dbi->where;
1288
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1289
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1290
    where => $where
1291
);
1292
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1293
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1294

            
1295
eval {
1296
$where = $dbi->where
1297
             ->clause(['uuu']);
1298
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1299
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1300
    where => $where
1301
);
1302
};
1303
ok($@);
1304

            
1305
$where = $dbi->where;
1306
is("$where", '');
1307

            
1308
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1309
             ->clause(['or', ("$key1 = :$key1") x 2])
1310
             ->param({$key1 => [1, 3]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1311
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1312
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1313
    where => $where,
1314
);
1315
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1316
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1317

            
1318
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1319
             ->clause(['or', ("$key1 = :$key1") x 2])
1320
             ->param({$key1 => [1]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1321
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1322
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1323
    where => $where,
1324
);
1325
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1326
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1327

            
1328
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1329
             ->clause(['or', ("$key1 = :$key1") x 2])
1330
             ->param({$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1331
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1332
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1333
    where => $where,
1334
);
1335
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1336
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1337

            
1338
$where = $dbi->where
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1339
             ->clause("$key1 = :$key1")
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1340
             ->param({$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1341
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1342
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1343
    where => $where,
1344
);
1345
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1346
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1347

            
1348
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1349
             ->clause("$key1 = :$key1 $key2 = :$key2")
1350
             ->param({$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1351
eval{$where->to_string};
1352
like($@, qr/one column/);
1353

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

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

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

            
1384
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1385
             ->clause(['or', ("$key1 = :$key1") x 3])
1386
             ->param({$key1 => [1, $dbi->not_exists, $dbi->not_exists]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1387
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1388
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1389
    where => $where,
1390
);
1391
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1392
is_deeply($row, [{$key1 => 1, $key2 => 2}], 'not_exists');
test cleanup
Yuki Kimoto authored on 2011-08-10
1393

            
1394
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1395
             ->clause(['or', ("$key1 = :$key1") x 3])
1396
             ->param({$key1 => [$dbi->not_exists, 1, $dbi->not_exists]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1397
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1398
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1399
    where => $where,
1400
);
1401
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1402
is_deeply($row, [{$key1 => 1, $key2 => 2}], 'not_exists');
test cleanup
Yuki Kimoto authored on 2011-08-10
1403

            
1404
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1405
             ->clause(['or', ("$key1 = :$key1") x 3])
1406
             ->param({$key1 => [$dbi->not_exists, $dbi->not_exists, 1]});
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}], 'not_exists');
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 3])
1416
             ->param({$key1 => [$dbi->not_exists, $dbi->not_exists, $dbi->not_exists]});
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}, {$key1 => 3, $key2 => 4}], 'not_exists');
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 3])
1426
             ->param({$key1 => []});
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}, {$key1 => 3, $key2 => 4}], 'not_exists');
test cleanup
Yuki Kimoto authored on 2011-08-10
1433

            
1434
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1435
             ->clause(['and', "{> $key1}", "{< $key1}" ])
1436
             ->param({$key1 => [2, $dbi->not_exists]});
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 => 3, $key2 => 4}], 'not_exists');
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(['and', "{> $key1}", "{< $key1}" ])
1446
             ->param({$key1 => [$dbi->not_exists, 2]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1447
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1448
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1449
    where => $where,
1450
);
1451
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1452
is_deeply($row, [{$key1 => 1, $key2 => 2}], 'not_exists');
test cleanup
Yuki Kimoto authored on 2011-08-10
1453

            
1454
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1455
             ->clause(['and', "{> $key1}", "{< $key1}" ])
1456
             ->param({$key1 => [$dbi->not_exists, $dbi->not_exists]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1457
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1458
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1459
    where => $where,
1460
);
1461
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1462
is_deeply($row, [{$key1 => 1, $key2 => 2},{$key1 => 3, $key2 => 4}], 'not_exists');
test cleanup
Yuki Kimoto authored on 2011-08-10
1463

            
1464
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1465
             ->clause(['and', "{> $key1}", "{< $key1}" ])
1466
             ->param({$key1 => [0, 2]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1467
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1468
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1469
    where => $where,
1470
);
1471
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1472
is_deeply($row, [{$key1 => 1, $key2 => 2}], 'not_exists');
test cleanup
Yuki Kimoto authored on 2011-08-10
1473

            
1474
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1475
             ->clause(['and',"$key1 is not null", "$key2 is not null" ]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1476
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1477
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1478
    where => $where,
1479
);
1480
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1481
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], 'not_exists');
test cleanup
Yuki Kimoto authored on 2011-08-10
1482

            
1483
eval {$dbi->where(ppp => 1) };
1484
like($@, qr/invalid/);
1485

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

            
1497

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

            
1509
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1510
$where->clause(['and', ":${key1}{=}"]);
1511
$where->param({$key1 => undef});
1512
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1513
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1514
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1515

            
1516
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1517
$where->clause(['and', ":${key1}{=}"]);
1518
$where->param({$key1 => undef});
test cleanup
Yuki Kimoto authored on 2011-08-10
1519
$where->if('defined');
1520
$where->map;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1521
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1522
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1523
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1524

            
1525
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1526
$where->clause(['or', ":${key1}{=}", ":${key1}{=}"]);
1527
$where->param({$key1 => [undef, undef]});
1528
$result = $dbi->execute("select * from $table1 $where", {$key1 => [1, 0]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1529
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1530
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
1531
$result = $dbi->execute("select * from $table1 $where", {$key1 => [0, 1]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1532
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1533
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1534

            
1535
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1536
$where->clause(['and', ":${key1}{=}"]);
1537
$where->param({$key1 => [undef, undef]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1538
$where->if('defined');
1539
$where->map;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1540
$result = $dbi->execute("select * from $table1 $where", {$key1 => [1, 0]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1541
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1542
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
1543
$result = $dbi->execute("select * from $table1 $where", {$key1 => [0, 1]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1544
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1545
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1546

            
1547
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1548
$where->clause(['and', ":${key1}{=}"]);
1549
$where->param({$key1 => 0});
test cleanup
Yuki Kimoto authored on 2011-08-10
1550
$where->if('length');
1551
$where->map;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1552
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1553
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1554
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1555

            
1556
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1557
$where->clause(['and', ":${key1}{=}"]);
1558
$where->param({$key1 => ''});
test cleanup
Yuki Kimoto authored on 2011-08-10
1559
$where->if('length');
1560
$where->map;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1561
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1562
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1563
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1564

            
1565
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1566
$where->clause(['and', ":${key1}{=}"]);
1567
$where->param({$key1 => 5});
test cleanup
Yuki Kimoto authored on 2011-08-10
1568
$where->if(sub { ($_[0] || '') eq 5 });
1569
$where->map;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1570
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1571
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1572
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1573

            
1574
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1575
$where->clause(['and', ":${key1}{=}"]);
1576
$where->param({$key1 => 7});
test cleanup
Yuki Kimoto authored on 2011-08-10
1577
$where->if(sub { ($_[0] || '') eq 5 });
1578
$where->map;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1579
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1580
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1581
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1582

            
1583
$where = $dbi->where;
1584
$where->param({id => 1, author => 'Ken', price => 1900});
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1585
$where->map(id => "$table1.id",
1586
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
1587
    price => ["$table1.price", {if => sub { $_[0] eq 1900 }}]
test cleanup
Yuki Kimoto authored on 2011-08-10
1588
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1589
is_deeply($where->param, {"$table1.id" => 1, "$table1.author" => '%Ken%',
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1590
  "$table1.price" => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
1591

            
1592
$where = $dbi->where;
1593
$where->param({id => 0, author => 0, price => 0});
1594
$where->map(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1595
    id => "$table1.id",
1596
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
1597
    price => ["$table1.price", sub { '%' . $_[0] . '%' },
test cleanup
Yuki Kimoto authored on 2011-08-10
1598
      {if => sub { $_[0] eq 0 }}]
1599
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1600
is_deeply($where->param, {"$table1.id" => 0, "$table1.author" => '%0%', "$table1.price" => '%0%'});
test cleanup
Yuki Kimoto authored on 2011-08-10
1601

            
1602
$where = $dbi->where;
1603
$where->param({id => '', author => '', price => ''});
1604
$where->if('length');
1605
$where->map(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1606
    id => "$table1.id",
1607
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
1608
    price => ["$table1.price", sub { '%' . $_[0] . '%' },
test cleanup
Yuki Kimoto authored on 2011-08-10
1609
      {if => sub { $_[0] eq 1 }}]
1610
);
1611
is_deeply($where->param, {});
1612

            
1613
$where = $dbi->where;
1614
$where->param({id => undef, author => undef, price => undef});
1615
$where->if('length');
1616
$where->map(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1617
    id => "$table1.id",
1618
    price => ["$table1.price", {if => 'exists'}]
test cleanup
Yuki Kimoto authored on 2011-08-10
1619
);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1620
is_deeply($where->param, {"$table1.price" => undef});
test cleanup
Yuki Kimoto authored on 2011-08-10
1621

            
1622
$where = $dbi->where;
1623
$where->param({price => 'a'});
1624
$where->if('length');
1625
$where->map(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1626
    id => ["$table1.id", {if => 'exists'}],
1627
    price => ["$table1.price", sub { '%' . $_[0] }, {if => 'exists'}]
test cleanup
Yuki Kimoto authored on 2011-08-10
1628
);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1629
is_deeply($where->param, {"$table1.price" => '%a'});
test cleanup
Yuki Kimoto authored on 2011-08-10
1630

            
1631
$where = $dbi->where;
1632
$where->param({id => [1, 2], author => 'Ken', price => 1900});
1633
$where->map(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1634
    id => "$table1.id",
1635
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
1636
    price => ["$table1.price", {if => sub { $_[0] eq 1900 }}]
test cleanup
Yuki Kimoto authored on 2011-08-10
1637
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1638
is_deeply($where->param, {"$table1.id" => [1, 2], "$table1.author" => '%Ken%',
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1639
  "$table1.price" => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
1640

            
1641
$where = $dbi->where;
1642
$where->if('length');
1643
$where->param({id => ['', ''], author => 'Ken', price => 1900});
1644
$where->map(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1645
    id => "$table1.id",
1646
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
1647
    price => ["$table1.price", {if => sub { $_[0] eq 1900 }}]
test cleanup
Yuki Kimoto authored on 2011-08-10
1648
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1649
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
1650
  "$table1.price" => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
1651

            
1652
$where = $dbi->where;
1653
$where->param({id => ['', ''], author => 'Ken', price => 1900});
1654
$where->map(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1655
    id => ["$table1.id", {if => 'length'}],
1656
    author => ["$table1.author", sub { '%' . $_[0] . '%' }, {if => 'defined'}],
1657
    price => ["$table1.price", {if => sub { $_[0] eq 1900 }}]
test cleanup
Yuki Kimoto authored on 2011-08-10
1658
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1659
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
1660
  "$table1.price" => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
1661

            
1662
test 'register_tag_processor';
1663
$dbi = DBIx::Custom->connect;
1664
$dbi->register_tag_processor(
1665
    a => sub { 1 }
1666
);
test cleanup
Yuki Kimoto authored on 2011-08-10
1667
is($dbi->{_tags}->{a}->(), 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1668

            
1669
test 'register_tag';
1670
$dbi = DBIx::Custom->connect;
1671
$dbi->register_tag(
1672
    b => sub { 2 }
1673
);
test cleanup
Yuki Kimoto authored on 2011-08-10
1674
is($dbi->{_tags}->{b}->(), 2);
test cleanup
Yuki Kimoto authored on 2011-08-10
1675

            
1676
test 'table not specify exception';
1677
$dbi = DBIx::Custom->connect;
1678
eval {$dbi->insert};
1679
like($@, qr/table/);
1680
eval {$dbi->update};
1681
like($@, qr/table/);
1682
eval {$dbi->delete};
1683
like($@, qr/table/);
1684
eval {$dbi->select};
1685
like($@, qr/table/);
test cleanup
Yuki Kimoto authored on 2011-08-10
1686

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1687
test 'more tests';
1688
$dbi = DBIx::Custom->connect;
1689
eval{$dbi->apply_filter('table', 'column', [])};
1690
like($@, qr/apply_filter/);
1691

            
1692
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
1693
like($@, qr/apply_filter/);
1694

            
1695
$dbi->apply_filter(
1696

            
1697
);
1698
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1699
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1700
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1701
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1702
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
1703
$dbi->apply_filter($table1, $key2, 
test cleanup
Yuki Kimoto authored on 2011-08-10
1704
                   {in => sub { $_[0] * 3 }, out => sub { $_[0] * 2 }});
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1705
$rows = $dbi->select(table => $table1, where => {$key2 => 1})->all;
1706
is_deeply($rows, [{$key1 => 1, $key2 => 6}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1707

            
1708
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1709
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1710
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1711
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1712
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
1713
$dbi->apply_filter($table1, $key2, {});
1714
$rows = $dbi->select(table => $table1, where => {$key2 => 2})->all;
1715
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1716

            
1717
$dbi = DBIx::Custom->connect;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1718
eval {$dbi->apply_filter($table1, $key2, {out => 'no'})};
test cleanup
Yuki Kimoto authored on 2011-08-10
1719
like($@, qr/not registered/);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1720
eval {$dbi->apply_filter($table1, $key2, {in => 'no'})};
test cleanup
Yuki Kimoto authored on 2011-08-10
1721
like($@, qr/not registered/);
1722
$dbi->method({one => sub { 1 }});
1723
is($dbi->one, 1);
1724

            
1725
eval{DBIx::Custom->connect(dsn => undef)};
1726
like($@, qr/_connect/);
1727

            
1728
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1729
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1730
$dbi->execute($create_table1);
1731
$dbi->register_filter(twice => sub { $_[0] * 2 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1732
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2},
1733
             filter => {$key1 => 'twice'});
1734
$row = $dbi->select(table => $table1)->one;
1735
is_deeply($row, {$key1 => 2, $key2 => 2});
1736
eval {$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2},
1737
             filter => {$key1 => 'no'}) };
test cleanup
Yuki Kimoto authored on 2011-08-10
1738
like($@, qr//);
1739

            
1740
$dbi->register_filter(one => sub { });
1741
$dbi->default_fetch_filter('one');
1742
ok($dbi->default_fetch_filter);
1743
$dbi->default_bind_filter('one');
1744
ok($dbi->default_bind_filter);
1745
eval{$dbi->default_fetch_filter('no')};
1746
like($@, qr/not registered/);
1747
eval{$dbi->default_bind_filter('no')};
1748
like($@, qr/not registered/);
1749
$dbi->default_bind_filter(undef);
1750
ok(!defined $dbi->default_bind_filter);
1751
$dbi->default_fetch_filter(undef);
1752
ok(!defined $dbi->default_fetch_filter);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1753
eval {$dbi->execute("select * from $table1 {} {= author") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1754
like($@, qr/Tag not finished/);
1755

            
1756
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1757
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1758
$dbi->execute($create_table1);
1759
$dbi->register_filter(one => sub { 1 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1760
$result = $dbi->select(table => $table1);
1761
eval {$result->filter($key1 => 'no')};
test cleanup
Yuki Kimoto authored on 2011-08-10
1762
like($@, qr/not registered/);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1763
eval {$result->end_filter($key1 => 'no')};
test cleanup
Yuki Kimoto authored on 2011-08-10
1764
like($@, qr/not registered/);
1765
$result->default_filter(undef);
1766
ok(!defined $result->default_filter);
1767
$result->default_filter('one');
1768
is($result->default_filter->(), 1);
1769

            
1770
test 'dbi_option';
1771
$dbi = DBIx::Custom->connect(dbi_option => {PrintError => 1});
1772
ok($dbi->dbh->{PrintError});
1773
$dbi = DBIx::Custom->connect(dbi_options => {PrintError => 1});
1774
ok($dbi->dbh->{PrintError});
1775

            
1776
test 'DBIx::Custom::Result stash()';
1777
$result = DBIx::Custom::Result->new;
1778
is_deeply($result->stash, {}, 'default');
1779
$result->stash->{foo} = 1;
1780
is($result->stash->{foo}, 1, 'get and set');
test cleanup
Yuki Kimoto authored on 2011-08-10
1781

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1782
test 'delete_at';
1783
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1784
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
1785
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1786
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1787
$dbi->delete_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1788
    table => $table1,
1789
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1790
    where => [1, 2],
1791
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1792
is_deeply($dbi->select(table => $table1)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
1793

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1794
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1795
$dbi->delete_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1796
    table => $table1,
1797
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
1798
    where => 1,
1799
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1800
is_deeply($dbi->select(table => $table1)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
1801

            
1802
test 'insert_at';
1803
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1804
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
1805
$dbi->execute($create_table1_2);
1806
$dbi->insert_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1807
    primary_key => [$key1, $key2], 
1808
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
1809
    where => [1, 2],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1810
    param => {$key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
1811
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1812
is($dbi->select(table => $table1)->one->{$key1}, 1);
1813
is($dbi->select(table => $table1)->one->{$key2}, 2);
1814
is($dbi->select(table => $table1)->one->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
1815

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1816
$dbi->delete_all(table => $table1);
1817
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1818
$dbi->insert_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1819
    primary_key => $key1, 
1820
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
1821
    where => 1,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1822
    param => {$key2 => 2, $key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
1823
);
1824

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

            
1829
eval {
1830
    $dbi->insert_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1831
        table => $table1,
1832
        primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1833
        where => {},
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1834
        param => {$key1 => 1, $key2 => 2, $key3 => 3},
cleanup test
Yuki Kimoto authored on 2011-08-10
1835
    );
1836
};
1837
like($@, qr/must be/);
1838

            
1839
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1840
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
1841
$dbi->execute($create_table1_2);
1842
$dbi->insert_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1843
    {$key3 => 3},
1844
    primary_key => [$key1, $key2], 
1845
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
1846
    where => [1, 2],
1847
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1848
is($dbi->select(table => $table1)->one->{$key1}, 1);
1849
is($dbi->select(table => $table1)->one->{$key2}, 2);
1850
is($dbi->select(table => $table1)->one->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
1851

            
1852
test 'update_at';
1853
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1854
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
1855
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1856
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1857
$dbi->update_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1858
    table => $table1,
1859
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1860
    where => [1, 2],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1861
    param => {$key3 => 4}
cleanup test
Yuki Kimoto authored on 2011-08-10
1862
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1863
is($dbi->select(table => $table1)->one->{$key1}, 1);
1864
is($dbi->select(table => $table1)->one->{$key2}, 2);
1865
is($dbi->select(table => $table1)->one->{$key3}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-10
1866

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1867
$dbi->delete_all(table => $table1);
1868
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1869
$dbi->update_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1870
    table => $table1,
1871
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
1872
    where => 1,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1873
    param => {$key3 => 4}
cleanup test
Yuki Kimoto authored on 2011-08-10
1874
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1875
is($dbi->select(table => $table1)->one->{$key1}, 1);
1876
is($dbi->select(table => $table1)->one->{$key2}, 2);
1877
is($dbi->select(table => $table1)->one->{$key3}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-10
1878

            
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->update_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1884
    {$key3 => 4},
1885
    table => $table1,
1886
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1887
    where => [1, 2]
1888
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1889
is($dbi->select(table => $table1)->one->{$key1}, 1);
1890
is($dbi->select(table => $table1)->one->{$key2}, 2);
1891
is($dbi->select(table => $table1)->one->{$key3}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-10
1892

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

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1920
$dbi->delete_all(table => $table1);
1921
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1922
$result = $dbi->select_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1923
    table => $table1,
1924
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1925
    where => [1, 2]
1926
);
1927
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1928
is($row->{$key1}, 1);
1929
is($row->{$key2}, 2);
1930
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
1931

            
1932
eval {
1933
    $result = $dbi->select_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1934
        table => $table1,
1935
        primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1936
        where => {},
1937
    );
1938
};
1939
like($@, qr/must be/);
1940

            
1941
eval {
1942
    $result = $dbi->select_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1943
        table => $table1,
1944
        primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1945
        where => [1],
1946
    );
1947
};
1948
like($@, qr/same/);
1949

            
1950
eval {
1951
    $result = $dbi->update_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1952
        table => $table1,
1953
        primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1954
        where => {},
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1955
        param => {$key1 => 1, $key2 => 2},
cleanup test
Yuki Kimoto authored on 2011-08-10
1956
    );
1957
};
1958
like($@, qr/must be/);
1959

            
1960
eval {
1961
    $result = $dbi->delete_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1962
        table => $table1,
1963
        primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1964
        where => {},
1965
    );
1966
};
1967
like($@, qr/must be/);
1968

            
1969
test 'columns';
test cleanup
Yuki Kimoto authored on 2011-08-15
1970
$DB::single = 1;
cleanup test
Yuki Kimoto authored on 2011-08-10
1971
$dbi = MyDBI1->connect;
test cleanup
Yuki Kimoto authored on 2011-08-15
1972
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
1973

            
1974

            
1975
test 'model delete_at';
1976
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1977
eval { $dbi->execute("drop table $table1") };
1978
eval { $dbi->execute("drop table $table2") };
1979
eval { $dbi->execute("drop table $table3") };
cleanup test
Yuki Kimoto authored on 2011-08-10
1980
$dbi->execute($create_table1_2);
1981
$dbi->execute($create_table2_2);
1982
$dbi->execute($create_table3);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1983
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
1984
$dbi->model($table1)->delete_at(where => [1, 2]);
1985
is_deeply($dbi->select(table => $table1)->all, []);
1986
$dbi->insert(table => $table2, param => {$key1 => 1, $key2 => 2, $key3 => 3});
1987
$dbi->model($table1)->delete_at(where => [1, 2]);
1988
is_deeply($dbi->select(table => $table1)->all, []);
1989
$dbi->insert(table => $table3, param => {$key1 => 1, $key2 => 2, $key3 => 3});
1990
$dbi->model($table3)->delete_at(where => [1, 2]);
1991
is_deeply($dbi->select(table => $table3)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
1992

            
1993
test 'model insert_at';
1994
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1995
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
1996
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1997
$dbi->model($table1)->insert_at(
cleanup test
Yuki Kimoto authored on 2011-08-10
1998
    where => [1, 2],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1999
    param => {$key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
2000
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2001
$result = $dbi->model($table1)->select;
cleanup test
Yuki Kimoto authored on 2011-08-10
2002
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2003
is($row->{$key1}, 1);
2004
is($row->{$key2}, 2);
2005
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2006

            
2007
test 'model update_at';
2008
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2009
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2010
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2011
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2012
$dbi->model($table1)->update_at(
cleanup test
Yuki Kimoto authored on 2011-08-10
2013
    where => [1, 2],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2014
    param => {$key3 => 4}
cleanup test
Yuki Kimoto authored on 2011-08-10
2015
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2016
$result = $dbi->model($table1)->select;
cleanup test
Yuki Kimoto authored on 2011-08-10
2017
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2018
is($row->{$key1}, 1);
2019
is($row->{$key2}, 2);
2020
is($row->{$key3}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-10
2021

            
2022
test 'model select_at';
2023
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2024
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2025
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2026
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2027
$result = $dbi->model($table1)->select_at(where => [1, 2]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2028
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2029
is($row->{$key1}, 1);
2030
is($row->{$key2}, 2);
2031
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2032

            
2033

            
2034
test 'mycolumn and column';
2035
$dbi = MyDBI7->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2036
eval { $dbi->execute("drop table $table1") };
2037
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2038
$dbi->execute($create_table1);
2039
$dbi->execute($create_table2);
2040
$dbi->separator('__');
2041
$dbi->setup_model;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2042
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2043
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
2044
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2045
$result = $model->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2046
    column => [$model->mycolumn, $model->column($table2)],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2047
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2048
);
2049
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2050
          {$key1 => 1, $key2 => 2, "${table2}__$key1" => 1, "${table2}__$key3" => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2051

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2052
test 'insert_param';
2053
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2054
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2055
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2056
$param = {$key1 => 1, $key2 => 2};
test cleanup
Yuki Kimoto authored on 2011-08-10
2057
$insert_param = $dbi->insert_param($param);
2058
$sql = <<"EOS";
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2059
insert into $table1 $insert_param
test cleanup
Yuki Kimoto authored on 2011-08-10
2060
EOS
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2061
$dbi->execute($sql, param => $param, table => $table1);
2062
is($dbi->select(table => $table1)->one->{$key1}, 1);
2063
is($dbi->select(table => $table1)->one->{$key2}, 2);
test cleanup
Yuki Kimoto authored on 2011-08-10
2064

            
2065
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2066
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2067
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2068
$param = {$key1 => 1, $key2 => 2};
test cleanup
Yuki Kimoto authored on 2011-08-10
2069
$insert_param = $dbi->insert_param($param);
2070
$sql = <<"EOS";
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2071
insert into $table1 $insert_param
test cleanup
Yuki Kimoto authored on 2011-08-10
2072
EOS
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2073
$dbi->execute($sql, param => $param, table => $table1);
2074
is($dbi->select(table => $table1)->one->{$key1}, 1);
2075
is($dbi->select(table => $table1)->one->{$key2}, 2);
test cleanup
Yuki Kimoto authored on 2011-08-10
2076

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2080
test 'mycolumn';
2081
$dbi = MyDBI8->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2082
eval { $dbi->execute("drop table $table1") };
2083
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2084
$dbi->execute($create_table1);
2085
$dbi->execute($create_table2);
2086
$dbi->setup_model;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2087
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2088
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
2089
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2090
$result = $model->select_at(
2091
    column => [
2092
        $model->mycolumn,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2093
        $model->column($table2)
cleanup test
Yuki Kimoto authored on 2011-08-10
2094
    ]
2095
);
2096
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2097
          {$key1 => 1, $key2 => 2, "$table2.$key1" => 1, "$table2.$key3" => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2098

            
2099
$result = $model->select_at(
2100
    column => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2101
        $model->mycolumn([$key1]),
2102
        $model->column($table2 => [$key1])
cleanup test
Yuki Kimoto authored on 2011-08-10
2103
    ]
2104
);
2105
is_deeply($result->one,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2106
          {$key1 => 1, "$table2.$key1" => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
2107
$result = $model->select_at(
2108
    column => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2109
        $model->mycolumn([$key1]),
2110
        {$table2 => [$key1]}
cleanup test
Yuki Kimoto authored on 2011-08-10
2111
    ]
2112
);
2113
is_deeply($result->one,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2114
          {$key1 => 1, "$table2.$key1" => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
2115

            
2116
$result = $model->select_at(
2117
    column => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2118
        $model->mycolumn([$key1]),
2119
        ["$table2.$key1", as => "$table2.$key1"]
cleanup test
Yuki Kimoto authored on 2011-08-10
2120
    ]
2121
);
2122
is_deeply($result->one,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2123
          {$key1 => 1, "$table2.$key1" => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
2124

            
2125
$result = $model->select_at(
2126
    column => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2127
        $model->mycolumn([$key1]),
2128
        ["$table2.$key1" => "$table2.$key1"]
cleanup test
Yuki Kimoto authored on 2011-08-10
2129
    ]
2130
);
2131
is_deeply($result->one,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2132
          {$key1 => 1, "$table2.$key1" => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
2133

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2134
test 'merge_param';
2135
$dbi = DBIx::Custom->new;
2136
$params = [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2137
    {$key1 => 1, $key2 => 2, $key3 => 3},
2138
    {$key1 => 1, $key2 => 2},
2139
    {$key1 => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2140
];
2141
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2142
is_deeply($param, {$key1 => [1, 1, 1], $key2 => [2, 2], $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2143

            
2144
$params = [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2145
    {$key1 => [1, 2], $key2 => 1, $key3 => [1, 2]},
2146
    {$key1 => [3, 4], $key2 => [2, 3], $key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
2147
];
2148
$param = $dbi->merge_param($params->[0], $params->[1]);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2149
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
2150

            
2151
test 'select() param option';
2152
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2153
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2154
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2155
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2156
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2157
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2158
$dbi->execute($create_table2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2159
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4});
2160
$dbi->insert(table => $table2, param => {$key1 => 2, $key3 => 5});
cleanup test
Yuki Kimoto authored on 2011-08-10
2161
$rows = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2162
    table => $table1,
2163
    column => "$table1.$key1 as ${table1}_$key1, $key2, $key3",
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2164
    where   => {"$table1.$key2" => 3},
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2165
    join  => ["inner join (select * from $table2 where {= $table2.$key3})" . 
test cleanup
Yuki Kimoto authored on 2011-08-15
2166
              " $table2 on $table1.$key1 = $table2.$key1"],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2167
    param => {"$table2.$key3" => 5}
cleanup test
Yuki Kimoto authored on 2011-08-10
2168
)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2169
is_deeply($rows, [{"${table1}_$key1" => 2, $key2 => 3, $key3 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2170

            
2171
test 'select() string where';
2172
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2173
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2174
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2175
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2176
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2177
$rows = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2178
    table => $table1,
2179
    where => "$key1 = :$key1 and $key2 = :$key2",
2180
    where_param => {$key1 => 1, $key2 => 2}
cleanup test
Yuki Kimoto authored on 2011-08-10
2181
)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2182
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2183

            
2184
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2185
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2186
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2187
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2188
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2189
$rows = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2190
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2191
    where => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2192
        "$key1 = :$key1 and $key2 = :$key2",
2193
        {$key1 => 1, $key2 => 2}
cleanup test
Yuki Kimoto authored on 2011-08-10
2194
    ]
2195
)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2196
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2197

            
2198
test 'delete() string where';
2199
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2200
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2201
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2202
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2203
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2204
$dbi->delete(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2205
    table => $table1,
2206
    where => "$key1 = :$key1 and $key2 = :$key2",
2207
    where_param => {$key1 => 1, $key2 => 2}
cleanup test
Yuki Kimoto authored on 2011-08-10
2208
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2209
$rows = $dbi->select(table => $table1)->all;
2210
is_deeply($rows, [{$key1 => 2, $key2 => 3}]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2211

            
2212
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2213
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2214
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2215
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2216
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2217
$dbi->delete(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2218
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2219
    where => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2220
        "$key1 = :$key1 and $key2 = :$key2",
2221
         {$key1 => 1, $key2 => 2}
cleanup test
Yuki Kimoto authored on 2011-08-10
2222
    ]
2223
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2224
$rows = $dbi->select(table => $table1)->all;
2225
is_deeply($rows, [{$key1 => 2, $key2 => 3}]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2226

            
2227

            
2228
test 'update() string where';
2229
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2230
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2231
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2232
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-10
2233
$dbi->update(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2234
    table => $table1,
2235
    param => {$key1 => 5},
2236
    where => "$key1 = :$key1 and $key2 = :$key2",
2237
    where_param => {$key1 => 1, $key2 => 2}
cleanup test
Yuki Kimoto authored on 2011-08-10
2238
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2239
$rows = $dbi->select(table => $table1)->all;
2240
is_deeply($rows, [{$key1 => 5, $key2 => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2241

            
2242
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2243
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2244
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2245
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-10
2246
$dbi->update(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2247
    table => $table1,
2248
    param => {$key1 => 5},
cleanup test
Yuki Kimoto authored on 2011-08-10
2249
    where => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2250
        "$key1 = :$key1 and $key2 = :$key2",
2251
        {$key1 => 1, $key2 => 2}
cleanup test
Yuki Kimoto authored on 2011-08-10
2252
    ]
2253
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2254
$rows = $dbi->select(table => $table1)->all;
2255
is_deeply($rows, [{$key1 => 5, $key2 => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2256

            
2257
test 'insert id and primary_key option';
2258
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2259
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2260
$dbi->execute($create_table1_2);
2261
$dbi->insert(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2262
    primary_key => [$key1, $key2], 
2263
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2264
    id => [1, 2],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2265
    param => {$key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
2266
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2267
is($dbi->select(table => $table1)->one->{$key1}, 1);
2268
is($dbi->select(table => $table1)->one->{$key2}, 2);
2269
is($dbi->select(table => $table1)->one->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2270

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2271
$dbi->delete_all(table => $table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2272
$dbi->insert(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2273
    primary_key => $key1, 
2274
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2275
    id => 0,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2276
    param => {$key2 => 2, $key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
2277
);
2278

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

            
2283
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2284
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2285
$dbi->execute($create_table1_2);
2286
$dbi->insert(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2287
    {$key3 => 3},
2288
    primary_key => [$key1, $key2], 
2289
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2290
    id => [1, 2],
2291
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2292
is($dbi->select(table => $table1)->one->{$key1}, 1);
2293
is($dbi->select(table => $table1)->one->{$key2}, 2);
2294
is($dbi->select(table => $table1)->one->{$key3}, 3);
test cleanup
Yuki Kimoto authored on 2011-08-10
2295

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2296
test 'model insert id and primary_key option';
2297
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2298
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2299
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2300
$dbi->model($table1)->insert(
cleanup test
Yuki Kimoto authored on 2011-08-10
2301
    id => [1, 2],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2302
    param => {$key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
2303
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2304
$result = $dbi->model($table1)->select;
cleanup test
Yuki Kimoto authored on 2011-08-10
2305
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2306
is($row->{$key1}, 1);
2307
is($row->{$key2}, 2);
2308
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2309

            
2310
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2311
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2312
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2313
$dbi->model($table1)->insert(
2314
    {$key3 => 3},
cleanup test
Yuki Kimoto authored on 2011-08-10
2315
    id => [1, 2]
2316
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2317
$result = $dbi->model($table1)->select;
cleanup test
Yuki Kimoto authored on 2011-08-10
2318
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2319
is($row->{$key1}, 1);
2320
is($row->{$key2}, 2);
2321
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2322

            
2323
test 'update and id option';
2324
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2325
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2326
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2327
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2328
$dbi->update(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2329
    table => $table1,
2330
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
2331
    id => [1, 2],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2332
    param => {$key3 => 4}
cleanup test
Yuki Kimoto authored on 2011-08-10
2333
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2334
is($dbi->select(table => $table1)->one->{$key1}, 1);
2335
is($dbi->select(table => $table1)->one->{$key2}, 2);
2336
is($dbi->select(table => $table1)->one->{$key3}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-10
2337

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2338
$dbi->delete_all(table => $table1);
2339
$dbi->insert(table => $table1, param => {$key1 => 0, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2340
$dbi->update(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2341
    table => $table1,
2342
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2343
    id => 0,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2344
    param => {$key3 => 4}
cleanup test
Yuki Kimoto authored on 2011-08-10
2345
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2346
is($dbi->select(table => $table1)->one->{$key1}, 0);
2347
is($dbi->select(table => $table1)->one->{$key2}, 2);
2348
is($dbi->select(table => $table1)->one->{$key3}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-10
2349

            
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);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2353
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2354
$dbi->update(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2355
    {$key3 => 4},
2356
    table => $table1,
2357
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
2358
    id => [1, 2]
2359
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2360
is($dbi->select(table => $table1)->one->{$key1}, 1);
2361
is($dbi->select(table => $table1)->one->{$key2}, 2);
2362
is($dbi->select(table => $table1)->one->{$key3}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-10
2363

            
2364

            
2365
test 'model update and id option';
2366
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2367
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2368
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2369
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2370
$dbi->model($table1)->update(
cleanup test
Yuki Kimoto authored on 2011-08-10
2371
    id => [1, 2],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2372
    param => {$key3 => 4}
cleanup test
Yuki Kimoto authored on 2011-08-10
2373
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2374
$result = $dbi->model($table1)->select;
cleanup test
Yuki Kimoto authored on 2011-08-10
2375
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2376
is($row->{$key1}, 1);
2377
is($row->{$key2}, 2);
2378
is($row->{$key3}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-10
2379

            
2380

            
2381
test 'delete and id option';
2382
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2383
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2384
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2385
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2386
$dbi->delete(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2387
    table => $table1,
2388
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
2389
    id => [1, 2],
2390
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2391
is_deeply($dbi->select(table => $table1)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
2392

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2393
$dbi->insert(table => $table1, param => {$key1 => 0, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2394
$dbi->delete(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2395
    table => $table1,
2396
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2397
    id => 0,
2398
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2399
is_deeply($dbi->select(table => $table1)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
2400

            
2401

            
2402
test 'model delete and id option';
2403
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2404
eval { $dbi->execute("drop table $table1") };
2405
eval { $dbi->execute("drop table $table2") };
2406
eval { $dbi->execute("drop table $table3") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2407
$dbi->execute($create_table1_2);
2408
$dbi->execute($create_table2_2);
2409
$dbi->execute($create_table3);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2410
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2411
$dbi->model($table1)->delete(id => [1, 2]);
2412
is_deeply($dbi->select(table => $table1)->all, []);
2413
$dbi->insert(table => $table2, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2414
$dbi->model($table1)->delete(id => [1, 2]);
2415
is_deeply($dbi->select(table => $table1)->all, []);
2416
$dbi->insert(table => $table3, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2417
$dbi->model($table3)->delete(id => [1, 2]);
2418
is_deeply($dbi->select(table => $table3)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
2419

            
2420

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2436
$dbi->delete_all(table => $table1);
2437
$dbi->insert(table => $table1, param => {$key1 => 0, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2438
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2439
    table => $table1,
2440
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2441
    id => 0,
2442
);
2443
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2444
is($row->{$key1}, 0);
2445
is($row->{$key2}, 2);
2446
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2447

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2448
$dbi->delete_all(table => $table1);
2449
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2450
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2451
    table => $table1,
2452
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
2453
    id => [1, 2]
2454
);
2455
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2456
is($row->{$key1}, 1);
2457
is($row->{$key2}, 2);
2458
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2459

            
2460

            
2461
test 'model select_at';
2462
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2463
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2464
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2465
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2466
$result = $dbi->model($table1)->select(id => [1, 2]);
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}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2471

            
2472
test 'column separator is default .';
2473
$dbi = MyDBI7->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2474
eval { $dbi->execute("drop table $table1") };
2475
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2476
$dbi->execute($create_table1);
2477
$dbi->execute($create_table2);
2478
$dbi->setup_model;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2479
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2480
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
2481
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2482
$result = $model->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2483
    column => [$model->column($table2)],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2484
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2485
);
2486
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2487
          {"$table2.$key1" => 1, "$table2.$key3" => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2488

            
2489
$result = $model->select(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2490
    column => [$model->column($table2 => [$key1, $key3])],
2491
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2492
);
2493
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2494
          {"$table2.$key1" => 1, "$table2.$key3" => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2495

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2496
test 'separator';
2497
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2498
eval { $dbi->execute("drop table $table1") };
2499
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2500
$dbi->execute($create_table1);
2501
$dbi->execute($create_table2);
test cleanup
Yuki Kimoto authored on 2011-08-10
2502

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2503
$dbi->create_model(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2504
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2505
    join => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2506
       "left outer join $table2 on $table1.$key1 = $table2.$key1"
cleanup test
Yuki Kimoto authored on 2011-08-10
2507
    ],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2508
    primary_key => [$key1],
cleanup test
Yuki Kimoto authored on 2011-08-10
2509
);
2510
$model2 = $dbi->create_model(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2511
    table => $table2,
cleanup test
Yuki Kimoto authored on 2011-08-10
2512
);
2513
$dbi->setup_model;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2514
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2515
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
2516
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2517
$result = $model->select(
2518
    column => [
2519
        $model->mycolumn,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2520
        {$table2 => [$key1, $key3]}
cleanup test
Yuki Kimoto authored on 2011-08-10
2521
    ],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2522
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2523
);
2524
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2525
          {$key1 => 1, $key2 => 2, "$table2.$key1" => 1, "$table2.$key3" => 3});
2526
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2527

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2528
$dbi->separator('__');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2529
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2530
$result = $model->select(
2531
    column => [
2532
        $model->mycolumn,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2533
        {$table2 => [$key1, $key3]}
cleanup test
Yuki Kimoto authored on 2011-08-10
2534
    ],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2535
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2536
);
2537
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2538
          {$key1 => 1, $key2 => 2, "${table2}__$key1" => 1, "${table2}__$key3" => 3});
2539
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2540

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2541
$dbi->separator('-');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2542
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2543
$result = $model->select(
2544
    column => [
2545
        $model->mycolumn,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2546
        {$table2 => [$key1, $key3]}
cleanup test
Yuki Kimoto authored on 2011-08-10
2547
    ],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2548
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2549
);
2550
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2551
          {$key1 => 1, $key2 => 2, "$table2-$key1" => 1, "$table2-$key3" => 3});
2552
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2553

            
2554

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2555
test 'filter_off';
2556
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2557
eval { $dbi->execute("drop table $table1") };
2558
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2559
$dbi->execute($create_table1);
2560
$dbi->execute($create_table2);
test cleanup
Yuki Kimoto authored on 2011-08-10
2561

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2562
$dbi->create_model(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2563
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2564
    join => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2565
       "left outer join $table2 on $table1.$key1 = $table2.$key1"
cleanup test
Yuki Kimoto authored on 2011-08-10
2566
    ],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2567
    primary_key => [$key1],
cleanup test
Yuki Kimoto authored on 2011-08-10
2568
);
2569
$dbi->setup_model;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2570
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2571
$model = $dbi->model($table1);
2572
$result = $model->select(column => $key1);
2573
$result->filter($key1 => sub { $_[0] * 2 });
2574
is_deeply($result->one, {$key1 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
2575

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2576
test 'available_datetype';
2577
$dbi = DBIx::Custom->connect;
2578
ok($dbi->can('available_datatype'));
test cleanup
Yuki Kimoto authored on 2011-08-10
2579

            
2580

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2581
test 'select prefix option';
2582
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2583
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2584
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2585
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2586
$rows = $dbi->select(prefix => "$key1,", column => $key2, table => $table1)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2587
is_deeply($rows, [{$key1 => 1, $key2 => 2}], "table");
test cleanup
Yuki Kimoto authored on 2011-08-10
2588

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2589
test 'map_param';
2590
$dbi = DBIx::Custom->connect;
2591
$param = $dbi->map_param(
2592
    {id => 1, author => 'Ken', price => 1900},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2593
    id => "$table1.id",
2594
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2595
    price => ["$table1.price", {if => sub { $_[0] eq 1900 }}]
cleanup test
Yuki Kimoto authored on 2011-08-10
2596
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2597
is_deeply($param, {"$table1.id" => 1, "$table1.author" => '%Ken%',
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2598
  "$table1.price" => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
2599

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2600
$param = $dbi->map_param(
2601
    {id => 0, author => 0, price => 0},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2602
    id => "$table1.id",
2603
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2604
    price => ["$table1.price", sub { '%' . $_[0] . '%' },
cleanup test
Yuki Kimoto authored on 2011-08-10
2605
      {if => sub { $_[0] eq 0 }}]
2606
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2607
is_deeply($param, {"$table1.id" => 0, "$table1.author" => '%0%', "$table1.price" => '%0%'});
test cleanup
Yuki Kimoto authored on 2011-08-10
2608

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2609
$param = $dbi->map_param(
2610
    {id => '', author => '', price => ''},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2611
    id => "$table1.id",
2612
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2613
    price => ["$table1.price", sub { '%' . $_[0] . '%' },
cleanup test
Yuki Kimoto authored on 2011-08-10
2614
      {if => sub { $_[0] eq 1 }}]
2615
);
2616
is_deeply($param, {});
test cleanup
Yuki Kimoto authored on 2011-08-10
2617

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2618
$param = $dbi->map_param(
2619
    {id => undef, author => undef, price => undef},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2620
    id => "$table1.id",
2621
    price => ["$table1.price", {if => 'exists'}]
cleanup test
Yuki Kimoto authored on 2011-08-10
2622
);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2623
is_deeply($param, {"$table1.price" => undef});
test cleanup
Yuki Kimoto authored on 2011-08-10
2624

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2625
$param = $dbi->map_param(
2626
    {price => 'a'},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2627
    id => ["$table1.id", {if => 'exists'}],
2628
    price => ["$table1.price", sub { '%' . $_[0] }, {if => 'exists'}]
cleanup test
Yuki Kimoto authored on 2011-08-10
2629
);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2630
is_deeply($param, {"$table1.price" => '%a'});
test cleanup
Yuki Kimoto authored on 2011-08-10
2631

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2632
test 'order';
2633
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2634
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2635
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2636
$dbi->insert({$key1 => 1, $key2 => 1}, table => $table1);
2637
$dbi->insert({$key1 => 1, $key2 => 3}, table => $table1);
2638
$dbi->insert({$key1 => 2, $key2 => 2}, table => $table1);
2639
$dbi->insert({$key1 => 2, $key2 => 4}, table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2640
my $order = $dbi->order;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2641
$order->prepend($key1, "$key2 desc");
2642
$result = $dbi->select(table => $table1, append => $order);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2643
is_deeply($result->all, [{$key1 => 1, $key2 => 3}, {$key1 => 1, $key2 => 1},
2644
  {$key1 => 2, $key2 => 4}, {$key1 => 2, $key2 => 2}]);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2645
$order->prepend("$key1 desc");
2646
$result = $dbi->select(table => $table1, append => $order);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2647
is_deeply($result->all, [{$key1 => 2, $key2 => 4}, {$key1 => 2, $key2 => 2},
2648
  {$key1 => 1, $key2 => 3}, {$key1 => 1, $key2 => 1}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2649

            
2650
$order = $dbi->order;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2651
$order->prepend(["$table1-$key1"], ["$table1-$key2", 'desc']);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2652
$result = $dbi->select(table => $table1,
2653
  column => [[$key1 => "$table1-$key1"], [$key2 => "$table1-$key2"]],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2654
  append => $order);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2655
is_deeply($result->all, [{"$table1-$key1" => 1, "$table1-$key2" => 3},
2656
  {"$table1-$key1" => 1, "$table1-$key2" => 1},
2657
  {"$table1-$key1" => 2, "$table1-$key2" => 4},
2658
  {"$table1-$key1" => 2, "$table1-$key2" => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2659

            
2660
test 'tag_parse';
2661
$dbi = DBIx::Custom->connect;
2662
$dbi->tag_parse(0);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2663
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2664
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2665
$dbi->insert({$key1 => 1, $key2 => 1}, table => $table1);
2666
eval {$dbi->execute("select * from $table1 where {= $key1}", {$key1 => 1})};
test cleanup
Yuki Kimoto authored on 2011-08-10
2667
ok($@);
2668

            
2669
test 'last_sql';
2670
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2671
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2672
$dbi->execute($create_table1);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2673
$dbi->execute("select * from $table1");
cleanup test
Yuki Kimoto authored on 2011-08-15
2674
is($dbi->last_sql, "select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
2675

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

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

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

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

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

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2707
$source = "select * from $table1 where :$table1.${key1}{=} and :$table1.${key2}{=}";
test cleanup
Yuki Kimoto authored on 2011-08-10
2708
$result = $dbi->execute(
2709
    $source,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2710
    param => {"$table1.$key1" => 1, "$table1.$key2" => 1},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2711
    filter => {"$table1.$key2" => sub { $_[0] * 2 }}
test cleanup
Yuki Kimoto authored on 2011-08-10
2712
);
2713
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2714
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2715

            
2716
test 'high perfomance way';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2717
$dbi->execute("drop table $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
2718
$dbi->execute($create_table1_highperformance);
2719
$rows = [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2720
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
2721
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
2722
];
2723
{
2724
    my $query;
2725
    foreach my $row (@$rows) {
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2726
      $query ||= $dbi->insert($row, table => $table1, query => 1);
2727
      $dbi->execute($query, $row, filter => {$key7 => sub { $_[0] * 2 }});
test cleanup
Yuki Kimoto authored on 2011-08-10
2728
    }
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2729
    is_deeply($dbi->select(table => $table1)->all,
test cleanup
Yuki Kimoto authored on 2011-08-10
2730
      [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2731
          {$key7 => 2, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
2732
          {$key7 => 2, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
2733
      ]
2734
    );
2735
}
2736

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2737
$dbi->execute("drop table $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
2738
$dbi->execute($create_table1_highperformance);
2739
$rows = [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2740
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
2741
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
2742
];
2743
{
2744
    my $query;
2745
    my $sth;
2746
    foreach my $row (@$rows) {
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2747
      $query ||= $dbi->insert($row, table => $table1, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2748
      $sth ||= $query->sth;
2749
      $sth->execute(map { $row->{$_} } sort keys %$row);
2750
    }
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2751
    is_deeply($dbi->select(table => $table1)->all,
test cleanup
Yuki Kimoto authored on 2011-08-10
2752
      [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2753
          {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
2754
          {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
2755
      ]
2756
    );
2757
}
2758

            
2759
test 'result';
2760
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2761
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2762
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2763
$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1);
2764
$dbi->insert({$key1 => 3, $key2 => 4}, table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2765

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2766
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2767
@rows = ();
2768
while (my $row = $result->fetch) {
2769
    push @rows, [@$row];
2770
}
2771
is_deeply(\@rows, [[1, 2], [3, 4]]);
2772

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2773
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2774
@rows = ();
2775
while (my $row = $result->fetch_hash) {
2776
    push @rows, {%$row};
2777
}
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2778
is_deeply(\@rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2779

            
2780
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2781
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2782
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2783
$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1);
2784
$dbi->insert({$key1 => 3, $key2 => 4}, table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2785

            
2786
test 'fetch_all';
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2787
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2788
$rows = $result->fetch_all;
2789
is_deeply($rows, [[1, 2], [3, 4]]);
2790

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

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

            
2799
$rows = $result->fetch_all;
2800
is_deeply($rows, [[3, 2], [9, 4]], "array");
2801

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

            
2808
test "query_builder";
2809
$datas = [
2810
    # Basic tests
2811
    {   name            => 'placeholder basic',
2812
        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
cleanup test
Yuki Kimoto authored on 2011-08-15
2813
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?",
test cleanup
Yuki Kimoto authored on 2011-08-10
2814
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
2815
    },
2816
    {
2817
        name            => 'placeholder in',
cleanup test
Yuki Kimoto authored on 2011-08-15
2818
        source            => "{in k1 3}",
2819
        sql_expected    => "k1 in (?, ?, ?)",
test cleanup
Yuki Kimoto authored on 2011-08-10
2820
        columns_expected   => [qw/k1 k1 k1/]
2821
    },
2822
    
2823
    # Table name
2824
    {
2825
        name            => 'placeholder with table name',
2826
        source            => "{= a.k1} {= a.k2}",
cleanup test
Yuki Kimoto authored on 2011-08-15
2827
        sql_expected    => "a.k1 = ? a.k2 = ?",
test cleanup
Yuki Kimoto authored on 2011-08-10
2828
        columns_expected  => [qw/a.k1 a.k2/]
2829
    },
2830
    {   
2831
        name            => 'placeholder in with table name',
2832
        source            => "{in a.k1 2} {in b.k2 2}",
cleanup test
Yuki Kimoto authored on 2011-08-15
2833
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?)",
test cleanup
Yuki Kimoto authored on 2011-08-10
2834
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
2835
    },
2836
    {
2837
        name            => 'not contain tag',
2838
        source            => "aaa",
cleanup test
Yuki Kimoto authored on 2011-08-15
2839
        sql_expected    => "aaa",
test cleanup
Yuki Kimoto authored on 2011-08-10
2840
        columns_expected  => [],
2841
    }
2842
];
2843

            
2844
for (my $i = 0; $i < @$datas; $i++) {
2845
    my $data = $datas->[$i];
cleanup
Yuki Kimoto authored on 2011-08-13
2846
    my $dbi = DBIx::Custom->new;
2847
    my $builder = $dbi->query_builder;
test cleanup
Yuki Kimoto authored on 2011-08-10
2848
    my $query = $builder->build_query($data->{source});
2849
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
2850
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
2851
}
2852

            
cleanup
Yuki Kimoto authored on 2011-08-13
2853
$dbi = DBIx::Custom->new;
2854
$builder = $dbi->query_builder;
2855
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
2856
    p => sub {
2857
        my @args = @_;
2858
        
2859
        my $expand    = "? $args[0] $args[1]";
2860
        my $columns = [2];
2861
        return [$expand, $columns];
2862
    }
2863
);
2864

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
2875
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
2876
    q => 'string'
2877
});
2878

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
2882
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
2883
   r => sub {} 
2884
});
2885

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
2889
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
2890
   s => sub { return ["a", ""]} 
2891
});
2892

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
2896
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
2897
    t => sub {return ["a", []]}
2898
);
2899

            
2900

            
cleanup
Yuki Kimoto authored on 2011-08-13
2901
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
2902
    a => sub {
2903
        return ["? ? ?", ['']];
2904
    }
2905
);
2906
eval{$builder->build_query("{a}")};
2907
like($@, qr/\QPlaceholder count/, "placeholder count is invalid");
2908

            
2909

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

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

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

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

            
2925
test 'variouse source';
2926
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d";
2927
$query = $builder->build_query($source);
2928
is($query->sql, 'a b = ? c { } { = ? } = ? d', "basic : 1");
2929

            
2930
$source = "abc";
2931
$query = $builder->build_query($source);
2932
is($query->sql, 'abc', "basic : 2");
2933

            
2934
$source = "{= a}";
2935
$query = $builder->build_query($source);
2936
is($query->sql, 'a = ?', "only tag");
2937

            
2938
$source = "000";
2939
$query = $builder->build_query($source);
2940
is($query->sql, '000', "contain 0 value");
2941

            
2942
$source = "a {= b} }";
2943
eval{$builder->build_query($source)};
2944
like($@, qr/unexpected "}"/, "error : 1");
2945

            
2946
$source = "a {= {}";
2947
eval{$builder->build_query($source)};
2948
like($@, qr/unexpected "{"/, "error : 2");
2949

            
2950
test 'select() wrap option';
2951
$dbi = DBIx::Custom->connect;
2952
eval { $dbi->execute("drop table $table1") };
2953
$dbi->execute($create_table1);
2954
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2955
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
2956
$rows = $dbi->select(
2957
    table => $table1,
2958
    column => $key1,
test cleanup
Yuki Kimoto authored on 2011-08-15
2959
    wrap => ["select * from (", ") t where $key1 = 1"]
cleanup test
Yuki Kimoto authored on 2011-08-15
2960
)->all;
2961
is_deeply($rows, [{$key1 => 1}]);
2962

            
2963
eval {
2964
$dbi->select(
2965
    table => $table1,
2966
    column => $key1,
2967
    wrap => 'select * from ('
2968
)
2969
};
2970
like($@, qr/array/);
2971

            
2972
test 'select() sqlfilter option';
2973
$dbi = DBIx::Custom->connect;
2974
eval { $dbi->execute("drop table $table1") };
2975
$dbi->execute($create_table1);
2976
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2977
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
2978
$rows = $dbi->select(
2979
    table => $table1,
2980
    column => $key1,
2981
    sqlfilter => sub {
2982
        my $sql = shift;
test cleanup
Yuki Kimoto authored on 2011-08-15
2983
        $sql = "select * from ( $sql ) t where $key1 = 1";
cleanup test
Yuki Kimoto authored on 2011-08-15
2984
        return $sql;
2985
    }
2986
)->all;
2987
is_deeply($rows, [{$key1 => 1}]);
2988

            
2989
test 'dbi method from model';
2990
$dbi = MyDBI9->connect;
2991
eval { $dbi->execute("drop table $table1") };
2992
$dbi->execute($create_table1);
2993
$dbi->setup_model;
2994
$model = $dbi->model($table1);
2995
eval{$model->execute("select * from $table1")};
2996
ok(!$@);
2997

            
2998
test 'column table option';
2999
$dbi = MyDBI9->connect;
3000
eval { $dbi->execute("drop table $table1") };
3001
$dbi->execute($create_table1);
3002
eval { $dbi->execute("drop table $table2") };
3003
$dbi->execute($create_table2);
3004
$dbi->setup_model;
3005
$dbi->execute("insert into $table1 ($key1, $key2) values (1, 2)");
3006
$dbi->execute("insert into $table2 ($key1, $key3) values (1, 4)");
3007
$model = $dbi->model($table1);
3008
$result = $model->select(
3009
    column => [
3010
        $model->column($table2, {alias => $table2_alias})
3011
    ],
3012
    where => {"$table2_alias.$key3" => 4}
3013
);
3014
is_deeply($result->one, 
3015
          {"$table2_alias.$key1" => 1, "$table2_alias.$key3" => 4});
3016

            
3017
$dbi->separator('__');
3018
$result = $model->select(
3019
    column => [
3020
        $model->column($table2, {alias => $table2_alias})
3021
    ],
3022
    where => {"$table2_alias.$key3" => 4}
3023
);
3024
is_deeply($result->one, 
3025
          {"${table2_alias}__$key1" => 1, "${table2_alias}__$key3" => 4});
3026

            
3027
$dbi->separator('-');
3028
$result = $model->select(
3029
    column => [
3030
        $model->column($table2, {alias => $table2_alias})
3031
    ],
3032
    where => {"$table2_alias.$key3" => 4}
3033
);
3034
is_deeply($result->one, 
3035
          {"$table2_alias-$key1" => 1, "$table2_alias-$key3" => 4});
3036

            
3037
test 'create_model';
3038
$dbi = DBIx::Custom->connect;
3039
eval { $dbi->execute("drop table $table1") };
3040
eval { $dbi->execute("drop table $table2") };
3041
$dbi->execute($create_table1);
3042
$dbi->execute($create_table2);
3043

            
3044
$dbi->create_model(
3045
    table => $table1,
3046
    join => [
3047
       "left outer join $table2 on $table1.$key1 = $table2.$key1"
3048
    ],
3049
    primary_key => [$key1]
3050
);
3051
$model2 = $dbi->create_model(
3052
    table => $table2
3053
);
3054
$dbi->create_model(
3055
    table => $table3,
3056
    filter => [
3057
        $key1 => {in => sub { uc $_[0] }}
3058
    ]
3059
);
3060
$dbi->setup_model;
3061
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3062
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
3063
$model = $dbi->model($table1);
3064
$result = $model->select(
3065
    column => [$model->mycolumn, $model->column($table2)],
3066
    where => {"$table1.$key1" => 1}
3067
);
3068
is_deeply($result->one,
3069
          {$key1 => 1, $key2 => 2, "$table2.$key1" => 1, "$table2.$key3" => 3});
3070
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3});
3071

            
3072
test 'model method';
3073
$dbi = DBIx::Custom->connect;
3074
eval { $dbi->execute("drop table $table2") };
3075
$dbi->execute($create_table2);
3076
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
3077
$model = $dbi->create_model(
3078
    table => $table2
3079
);
3080
$model->method(foo => sub { shift->select(@_) });
3081
is_deeply($model->foo->one, {$key1 => 1, $key3 => 3});
3082

            
3083
test 'update_param';
3084
$dbi = DBIx::Custom->connect;
3085
eval { $dbi->execute("drop table $table1") };
3086
$dbi->execute($create_table1_2);
3087
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3088
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3089

            
3090
$param = {$key2 => 11};
3091
$update_param = $dbi->update_param($param);
3092
$sql = <<"EOS";
3093
update $table1 $update_param
3094
where $key1 = 1
3095
EOS
3096
$dbi->execute($sql, param => $param);
3097
$result = $dbi->execute("select * from $table1 order by $key1", table => $table1);
3098
$rows   = $result->all;
3099
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
3100
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3101
                  "basic");
3102

            
3103

            
3104
$dbi = DBIx::Custom->connect;
3105
eval { $dbi->execute("drop table $table1") };
3106
$dbi->execute($create_table1_2);
3107
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3108
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3109

            
3110
$param = {$key2 => 11, $key3 => 33};
3111
$update_param = $dbi->update_param($param);
3112
$sql = <<"EOS";
3113
update $table1 $update_param
3114
where $key1 = 1
3115
EOS
3116
$dbi->execute($sql, param => $param);
3117
$result = $dbi->execute("select * from $table1 order by $key1", table => $table1);
3118
$rows   = $result->all;
3119
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 33, $key4 => 4, $key5 => 5},
3120
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3121
                  "basic");
3122

            
3123
$dbi = DBIx::Custom->connect;
3124
eval { $dbi->execute("drop table $table1") };
3125
$dbi->execute($create_table1_2);
3126
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3127
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3128

            
3129
$param = {$key2 => 11, $key3 => 33};
3130
$update_param = $dbi->update_param($param, {no_set => 1});
3131
$sql = <<"EOS";
3132
update $table1 set $update_param
3133
where $key1 = 1
3134
EOS
3135
$dbi->execute($sql, param => $param);
3136
$result = $dbi->execute("select * from $table1 order by $key1", table => $table1);
3137
$rows   = $result->all;
3138
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 33, $key4 => 4, $key5 => 5},
3139
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3140
                  "update param no_set");
3141

            
3142
            
3143
eval { $dbi->update_param({";" => 1}) };
3144
like($@, qr/not safety/);
3145

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3147
test 'update_param';
3148
$dbi = DBIx::Custom->connect;
3149
eval { $dbi->execute("drop table $table1") };
3150
$dbi->execute($create_table1_2);
3151
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3152
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
test cleanup
Yuki Kimoto authored on 2011-08-10
3153

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3154
$param = {$key2 => 11};
3155
$update_param = $dbi->assign_param($param);
3156
$sql = <<"EOS";
3157
update $table1 set $update_param
3158
where $key1 = 1
3159
EOS
3160
$dbi->execute($sql, param => $param, table => $table1);
3161
$result = $dbi->execute("select * from $table1 order by $key1");
3162
$rows   = $result->all;
3163
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
3164
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3165
                  "basic");
test cleanup
Yuki Kimoto authored on 2011-08-10
3166

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3167
test 'join';
3168
$dbi = DBIx::Custom->connect;
3169
eval { $dbi->execute("drop table $table1") };
3170
$dbi->execute($create_table1);
3171
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3172
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
3173
eval { $dbi->execute("drop table $table2") };
3174
$dbi->execute($create_table2);
3175
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3176
eval { $dbi->execute("drop table $table3") };
3177
$dbi->execute("create table $table3 ($key3 int, $key4 int)");
3178
$dbi->insert(table => $table3, param => {$key3 => 5, $key4 => 4});
3179
$rows = $dbi->select(
3180
    table => $table1,
3181
    column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
3182
    where   => {"$table1.$key2" => 2},
3183
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1"]
3184
)->all;
3185
is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
3186

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3187
$rows = $dbi->select(
3188
    table => $table1,
3189
    where   => {$key1 => 1},
3190
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1"]
3191
)->all;
3192
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
3193

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3194
eval {
3195
    $rows = $dbi->select(
3196
        table => $table1,
3197
        column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
3198
        where   => {"$table1.$key2" => 2},
3199
        join  => {"$table1.$key1" => "$table2.$key1"}
3200
    );
3201
};
3202
like ($@, qr/array/);
test cleanup
Yuki Kimoto authored on 2011-08-10
3203

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3204
$rows = $dbi->select(
3205
    table => $table1,
3206
    where   => {$key1 => 1},
3207
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
3208
              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
3209
)->all;
3210
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
3211

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3212
$rows = $dbi->select(
3213
    column => "$table3.$key4 as ${table3}__$key4",
3214
    table => $table1,
3215
    where   => {"$table1.$key1" => 1},
3216
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
3217
              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
3218
)->all;
3219
is_deeply($rows, [{"${table3}__$key4" => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
3220

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3221
$rows = $dbi->select(
3222
    column => "$table1.$key1 as ${table1}__$key1",
3223
    table => $table1,
3224
    where   => {"$table3.$key4" => 4},
3225
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
3226
              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
3227
)->all;
3228
is_deeply($rows, [{"${table1}__$key1" => 1}]);
3229

            
3230
$dbi = DBIx::Custom->connect;
3231
eval { $dbi->execute("drop table $table1") };
3232
$dbi->execute($create_table1);
3233
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3234
eval { $dbi->execute("drop table $table2") };
3235
$dbi->execute($create_table2);
3236
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3237
$rows = $dbi->select(
3238
    table => $table1,
3239
    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",
3240
    where   => {"$table1.$key2" => 2},
3241
    join  => ["left outer join ${q}$table2$p on ${q}$table1$p.${q}$key1$p = ${q}$table2$p.${q}$key1$p"],
3242
)->all;
3243
is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}],
3244
          'quote');
test cleanup
Yuki Kimoto authored on 2011-08-10
3245

            
3246

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3247
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3248
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
3249
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3250
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-15
3251
$sql = <<"EOS";
3252
left outer join (
test cleanup
Yuki Kimoto authored on 2011-08-15
3253
  select * from $table1 t1
cleanup test
Yuki Kimoto authored on 2011-08-15
3254
  where t1.$key2 = (
test cleanup
Yuki Kimoto authored on 2011-08-15
3255
    select max(t2.$key2) from $table1 t2
cleanup test
Yuki Kimoto authored on 2011-08-15
3256
    where t1.$key1 = t2.$key1
3257
  )
test cleanup
Yuki Kimoto authored on 2011-08-15
3258
) latest_$table1 on $table1.$key1 = latest_$table1.$key1
cleanup test
Yuki Kimoto authored on 2011-08-15
3259
EOS
3260
$join = [$sql];
cleanup test
Yuki Kimoto authored on 2011-08-10
3261
$rows = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3262
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-15
3263
    column => "latest_$table1.$key1 as latest_${table1}__$key1",
3264
    join  => $join
cleanup test
Yuki Kimoto authored on 2011-08-10
3265
)->all;
cleanup test
Yuki Kimoto authored on 2011-08-15
3266
is_deeply($rows, [{"latest_${table1}__$key1" => 1}]);
cleanup test
Yuki Kimoto authored on 2011-08-10
3267

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3268
$dbi = DBIx::Custom->connect;
3269
eval { $dbi->execute("drop table $table1") };
3270
eval { $dbi->execute("drop table $table2") };
3271
$dbi->execute($create_table1);
3272
$dbi->execute($create_table2);
3273
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3274
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4});
3275
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3276
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3277
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-15
3278
    join => [
3279
        "left outer join $table2 on $table2.$key2 = '4' and $table1.$key1 = $table2.$key1"
3280
    ]
3281
);
3282
is_deeply($result->all, [{$key1 => 1, $key2 => 2}]);
3283
$result = $dbi->select(
3284
    table => $table1,
3285
    column => [{$table2 => [$key3]}],
3286
    join => [
3287
        "left outer join $table2 on $table2.$key3 = '4' and $table1.$key1 = $table2.$key1"
3288
    ]
3289
);
3290
is_deeply($result->all, [{"$table2.$key3" => 4}]);
3291
$result = $dbi->select(
3292
    table => $table1,
3293
    column => [{$table2 => [$key3]}],
3294
    join => [
3295
        "left outer join $table2 on $table1.$key1 = $table2.$key1 and $table2.$key3 = '4'"
3296
    ]
3297
);
3298
is_deeply($result->all, [{"$table2.$key3" => 4}]);
3299

            
3300
$dbi = DBIx::Custom->connect;
3301
eval { $dbi->execute("drop table $table1") };
3302
eval { $dbi->execute("drop table $table2") };
3303
$dbi->execute($create_table1);
3304
$dbi->execute($create_table2);
3305
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3306
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4});
3307
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3308
$result = $dbi->select(
3309
    table => $table1,
3310
    column => [{$table2 => [$key3]}],
3311
    join => [
3312
        {
3313
            clause => "left outer join $table2 on $table2.$key3 = '4' and $table1.$key1 = $table2.$key1",
3314
            table => [$table1, $table2]
3315
        }
3316
    ]
3317
);
3318
is_deeply($result->all, [{"$table2.$key3" => 4}]);
3319

            
3320
test 'Model class';
3321
$dbi = MyDBI1->connect;
3322
eval { $dbi->execute("drop table $table1") };
3323
$dbi->execute($create_table1);
3324
$model = $dbi->model($table1);
3325
$model->insert({$key1 => 'a', $key2 => 'b'});
3326
is_deeply($model->list->all, [{$key1 => 'a', $key2 => 'b'}], 'basic');
3327
eval { $dbi->execute("drop table $table2") };
3328
$dbi->execute($create_table2);
3329
$model = $dbi->model($table2);
3330
$model->insert({$key1 => 'a'});
3331
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'basic');
3332
is($dbi->models->{$table1}, $dbi->model($table1));
3333
is($dbi->models->{$table2}, $dbi->model($table2));
3334

            
3335
$dbi = MyDBI4->connect;
3336
eval { $dbi->execute("drop table $table1") };
3337
$dbi->execute($create_table1);
3338
$model = $dbi->model($table1);
3339
$model->insert({$key1 => 'a', $key2 => 'b'});
3340
is_deeply($model->list->all, [{$key1 => 'a', $key2 => 'b'}], 'basic');
3341
eval { $dbi->execute("drop table $table2") };
3342
$dbi->execute($create_table2);
3343
$model = $dbi->model($table2);
3344
$model->insert({$key1 => 'a'});
3345
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'basic');
3346

            
3347
$dbi = MyDBI5->connect;
3348
eval { $dbi->execute("drop table $table1") };
3349
eval { $dbi->execute("drop table $table2") };
3350
$dbi->execute($create_table1);
3351
$dbi->execute($create_table2);
3352
$model = $dbi->model($table2);
3353
$model->insert({$key1 => 'a'});
3354
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'include all model');
3355
$dbi->insert(table => $table1, param => {$key1 => 1});
3356
$model = $dbi->model($table1);
3357
is_deeply($model->list->all, [{$key1 => 1, $key2 => undef}], 'include all model');
3358

            
3359
test 'primary_key';
3360
$dbi = MyDBI1->connect;
3361
$model = $dbi->model($table1);
3362
$model->primary_key([$key1, $key2]);
3363
is_deeply($model->primary_key, [$key1, $key2]);
3364

            
3365
test 'columns';
3366
$dbi = MyDBI1->connect;
3367
$model = $dbi->model($table1);
3368
$model->columns([$key1, $key2]);
3369
is_deeply($model->columns, [$key1, $key2]);
cleanup test
Yuki Kimoto authored on 2011-08-10
3370

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3371
test 'setup_model';
3372
$dbi = MyDBI1->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3373
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3374
eval { $dbi->execute("drop table $table2") };
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
3375

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3376
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-15
3377
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-10
3378
$dbi->setup_model;
cleanup test
Yuki Kimoto authored on 2011-08-15
3379
is_deeply([sort @{$dbi->model($table1)->columns}], [$key1, $key2]);
3380
is_deeply([sort @{$dbi->model($table2)->columns}], [$key1, $key3]);
cleanup test
Yuki Kimoto authored on 2011-08-10
3381

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3382
test 'each_column';
3383
$dbi = DBIx::Custom->connect;
3384
eval { $dbi->execute("drop table ${q}table$p") };
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3385
eval { $dbi->execute("drop table $table1") };
3386
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3387
eval { $dbi->execute("drop table $table3") };
3388
$dbi->execute($create_table1_type);
cleanup test
Yuki Kimoto authored on 2011-08-10
3389
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-15
3390

            
3391
$infos = [];
3392
$dbi->each_column(sub {
3393
    my ($self, $table, $column, $cinfo) = @_;
3394
    
3395
    if ($table =~ /^table\d/i) {
3396
         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
3397
         push @$infos, $info;
3398
    }
3399
});
3400
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3401
is_deeply($infos, 
3402
    [
3403
        [$table1, $key1, $key1],
3404
        [$table1, $key2, $key2],
3405
        [$table2, $key1, $key1],
3406
        [$table2, $key3, $key3]
3407
    ]
3408
    
cleanup test
Yuki Kimoto authored on 2011-08-10
3409
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3410
test 'each_table';
3411
$dbi = DBIx::Custom->connect;
3412
eval { $dbi->execute("drop table $table1") };
3413
eval { $dbi->execute("drop table $table2") };
3414
$dbi->execute($create_table2);
3415
$dbi->execute($create_table1_type);
cleanup test
Yuki Kimoto authored on 2011-08-10
3416

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3417
$infos = [];
3418
$dbi->each_table(sub {
3419
    my ($self, $table, $table_info) = @_;
3420
    
3421
    if ($table =~ /^table\d/i) {
3422
         my $info = [$table, $table_info->{TABLE_NAME}];
3423
         push @$infos, $info;
3424
    }
3425
});
3426
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3427
is_deeply($infos, 
3428
    [
3429
        [$table1, $table1],
3430
        [$table2, $table2],
3431
    ]
cleanup test
Yuki Kimoto authored on 2011-08-10
3432
);
3433

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3434
test 'type_rule into';
3435
eval { $dbi->execute("drop table $table1") };
3436
$dbi->execute($create_table1_type);
3437
$dbi = DBIx::Custom->connect;
3438
eval { $dbi->execute("drop table $table1") };
3439
$dbi->execute($create_table1_type);
3440

            
3441
$dbi->type_rule(
3442
    into1 => {
3443
        $date_typename => sub { '2010-' . $_[0] }
3444
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3445
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3446
$dbi->insert({$key1 => '01-01'}, table => $table1);
3447
$result = $dbi->select(table => $table1);
3448
like($result->one->{$key1}, qr/^2010-01-01/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3449

            
3450
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3451
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3452
$dbi->execute($create_table1_type);
3453
$dbi->type_rule(
3454
    into1 => [
3455
         [$date_typename, $datetime_typename] => sub {
3456
            my $value = shift;
3457
            $value =~ s/02/03/g;
3458
            return $value;
3459
         }
3460
    ]
3461
);
3462
$dbi->insert({$key1 => '2010-01-02', $key2 => '2010-01-01 01:01:02'}, table => $table1);
3463
$result = $dbi->select(table => $table1);
3464
$row = $result->one;
3465
like($row->{$key1}, qr/^2010-01-03/);
3466
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3467

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3468
$dbi = DBIx::Custom->connect;
3469
eval { $dbi->execute("drop table $table1") };
3470
$dbi->execute($create_table1_type);
3471
$dbi->insert({$key1 => '2010-01-03', $key2 => '2010-01-01 01:01:03'}, table => $table1);
3472
$dbi->type_rule(
3473
    into1 => [
3474
        [$date_typename, $datetime_typename] => sub {
3475
            my $value = shift;
3476
            $value =~ s/02/03/g;
3477
            return $value;
3478
        }
3479
    ]
cleanup test
Yuki Kimoto authored on 2011-08-10
3480
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3481
$result = $dbi->execute(
3482
    "select * from $table1 where $key1 = :$key1 and $key2 = :$table1.$key2",
3483
    param => {$key1 => '2010-01-03', "$table1.$key2" => '2010-01-01 01:01:02'}
cleanup test
Yuki Kimoto authored on 2011-08-10
3484
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3485
$row = $result->one;
3486
like($row->{$key1}, qr/^2010-01-03/);
3487
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
3488

            
3489
$dbi = DBIx::Custom->connect;
3490
eval { $dbi->execute("drop table $table1") };
3491
$dbi->execute($create_table1_type);
3492
$dbi->insert({$key1 => '2010-01-03', $key2 => '2010-01-01 01:01:03'}, table => $table1);
3493
$dbi->type_rule(
3494
    into1 => [
3495
        [$date_typename, $datetime_typename] => sub {
3496
            my $value = shift;
3497
            $value =~ s/02/03/g;
3498
            return $value;
3499
        }
cleanup test
Yuki Kimoto authored on 2011-08-10
3500
    ]
3501
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3502
$result = $dbi->execute(
3503
    "select * from $table1 where $key1 = :$key1 and $key2 = :$table1.$key2",
3504
    param => {$key1 => '2010-01-02', "$table1.$key2" => '2010-01-01 01:01:02'},
3505
    table => $table1
3506
);
3507
$row = $result->one;
3508
like($row->{$key1}, qr/^2010-01-03/);
3509
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3510

            
3511
$dbi = DBIx::Custom->connect;
3512
eval { $dbi->execute("drop table $table1") };
3513
$dbi->execute($create_table1_type);
3514
$dbi->register_filter(convert => sub {
3515
    my $value = shift || '';
3516
    $value =~ s/02/03/;
3517
    return $value;
3518
});
3519
$dbi->type_rule(
3520
    from1 => {
3521
        $date_datatype => 'convert',
3522
    },
3523
    into1 => {
3524
        $date_typename => 'convert',
3525
    }
3526
);
3527
$dbi->insert({$key1 => '2010-02-02'}, table => $table1);
3528
$result = $dbi->select(table => $table1);
3529
like($result->fetch->[0], qr/^2010-03-03/);
3530

            
3531
test 'type_rule and filter order';
3532
$dbi = DBIx::Custom->connect;
3533
eval { $dbi->execute("drop table $table1") };
3534
$dbi->execute($create_table1_type);
3535
$dbi->type_rule(
3536
    into1 => {
3537
        $date_typename => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3538
    },
3539
    into2 => {
3540
        $date_typename => sub { my $v = shift || ''; $v =~ s/5/6/; return $v }
3541
    },
3542
    from1 => {
3543
        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/7/; return $v }
3544
    },
3545
    from2 => {
3546
        $date_datatype => sub { my $v = shift || ''; $v =~ s/7/8/; return $v }
3547
    }
3548
);
3549
$dbi->insert({$key1 => '2010-01-03'}, 
3550
  table => $table1, filter => {$key1 => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }});
3551
$result = $dbi->select(table => $table1);
3552
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/8/9/; return $v });
3553
like($result->fetch_first->[0], qr/^2010-01-09/);
3554

            
3555

            
3556
$dbi = DBIx::Custom->connect;
3557
eval { $dbi->execute("drop table $table1") };
3558
$dbi->execute($create_table1_type);
3559
$dbi->type_rule(
3560
    from1 => {
3561
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3562
    },
3563
    from2 => {
3564
        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3565
    },
3566
);
3567
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3568
$result = $dbi->select(table => $table1);
3569
$result->type_rule(
3570
    from1 => {
3571
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/6/; return $v }
3572
    },
3573
    from2 => {
3574
        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/8/; return $v }
3575
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3576
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3577
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/8/9/; return $v });
3578
like($result->fetch_first->[0], qr/^2010-01-09/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3579

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3580
test 'type_rule_off';
cleanup test
Yuki Kimoto authored on 2011-08-10
3581
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-15
3582
eval { $dbi->execute("drop table $table1") };
3583
$dbi->execute($create_table1_type);
3584
$dbi->type_rule(
3585
    from1 => {
3586
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3587
    },
3588
    into1 => {
3589
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3590
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3591
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3592
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule_off => 1);
3593
$result = $dbi->select(table => $table1, type_rule_off => 1);
3594
like($result->type_rule_off->fetch->[0], qr/^2010-01-03/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3595

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3596
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3597
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3598
$dbi->execute($create_table1_type);
3599
$dbi->type_rule(
3600
    from1 => {
3601
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3602
    },
3603
    into1 => {
3604
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3605
    }
3606
);
3607
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule_off => 1);
3608
$result = $dbi->select(table => $table1, type_rule_off => 1);
3609
like($result->one->{$key1}, qr/^2010-01-04/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3610

            
3611
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3612
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3613
$dbi->execute($create_table1_type);
3614
$dbi->type_rule(
3615
    from1 => {
3616
        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3617
    },
3618
    into1 => {
3619
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3620
    }
3621
);
3622
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3623
$result = $dbi->select(table => $table1);
3624
like($result->one->{$key1}, qr/^2010-01-05/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3625

            
3626
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3627
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3628
$dbi->execute($create_table1_type);
3629
$dbi->type_rule(
3630
    from1 => {
3631
        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3632
    },
3633
    into1 => {
3634
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3635
    }
3636
);
3637
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3638
$result = $dbi->select(table => $table1);
3639
like($result->fetch->[0], qr/2010-01-05/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3640

            
3641
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3642
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3643
$dbi->execute($create_table1_type);
3644
$dbi->register_filter(ppp => sub { my $v = shift || ''; $v =~ s/3/4/; return $v });
3645
$dbi->type_rule(
3646
    into1 => {
3647
        $date_typename => 'ppp'
3648
    }
3649
);
3650
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3651
$result = $dbi->select(table => $table1);
3652
like($result->one->{$key1}, qr/^2010-01-04/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3653

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3654
eval{$dbi->type_rule(
3655
    into1 => {
3656
        $date_typename => 'pp'
3657
    }
3658
)};
3659
like($@, qr/not registered/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3660

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3661
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3662
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3663
$dbi->execute($create_table1_type);
cleanup test
Yuki Kimoto authored on 2011-08-10
3664
eval {
cleanup test
Yuki Kimoto authored on 2011-08-15
3665
    $dbi->type_rule(
3666
        from1 => {
3667
            Date => sub { $_[0] * 2 },
3668
        }
cleanup test
Yuki Kimoto authored on 2011-08-10
3669
    );
3670
};
cleanup test
Yuki Kimoto authored on 2011-08-15
3671
like($@, qr/lower/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3672

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3673
eval {
3674
    $dbi->type_rule(
3675
        into1 => {
3676
            Date => sub { $_[0] * 2 },
3677
        }
3678
    );
3679
};
3680
like($@, qr/lower/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3681

            
3682
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3683
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3684
$dbi->execute($create_table1_type);
3685
$dbi->type_rule(
3686
    from1 => {
3687
        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3688
    },
3689
    into1 => {
3690
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3691
    }
3692
);
3693
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3694
$result = $dbi->select(table => $table1);
3695
$result->type_rule_off;
3696
like($result->one->{$key1}, qr/^2010-01-04/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3697

            
3698
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3699
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3700
$dbi->execute($create_table1_type);
3701
$dbi->type_rule(
3702
    from1 => {
3703
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v },
3704
        $datetime_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3705
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
3706
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3707
$dbi->insert({$key1 => '2010-01-03', $key2 => '2010-01-01 01:01:03'}, table => $table1);
3708
$result = $dbi->select(table => $table1);
3709
$result->type_rule(
3710
    from1 => {
3711
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3712
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3713
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3714
$row = $result->one;
3715
like($row->{$key1}, qr/^2010-01-05/);
3716
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
3717

            
3718
$result = $dbi->select(table => $table1);
3719
$result->type_rule(
3720
    from1 => {
3721
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3722
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3723
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3724
$row = $result->one;
3725
like($row->{$key1}, qr/2010-01-05/);
3726
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3727

            
3728
$result = $dbi->select(table => $table1);
3729
$result->type_rule(
3730
    from1 => {
3731
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3732
    }
3733
);
3734
$row = $result->one;
3735
like($row->{$key1}, qr/2010-01-05/);
3736
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3737

            
3738
$result = $dbi->select(table => $table1);
3739
$result->type_rule(
3740
    from1 => [$date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }]
3741
);
3742
$row = $result->one;
3743
like($row->{$key1}, qr/2010-01-05/);
3744
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3745

            
3746
$dbi->register_filter(five => sub { my $v = shift || ''; $v =~ s/3/5/; return $v });
3747
$result = $dbi->select(table => $table1);
3748
$result->type_rule(
3749
    from1 => [$date_datatype => 'five']
3750
);
3751
$row = $result->one;
3752
like($row->{$key1}, qr/^2010-01-05/);
3753
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
3754

            
3755
$result = $dbi->select(table => $table1);
3756
$result->type_rule(
3757
    from1 => [$date_datatype => undef]
3758
);
3759
$row = $result->one;
3760
like($row->{$key1}, qr/^2010-01-03/);
3761
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3762

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3789
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3790
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3791
$dbi->execute($create_table1_type);
3792
$dbi->type_rule(
3793
    into1 => {
3794
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3795
    },
3796
    into2 => {
3797
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3798
    },
3799
    from1 => {
3800
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/6/; return $v }
3801
    },
3802
    from2 => {
3803
        $date_datatype => sub { my $v = shift || ''; $v =~ s/(3|6)/7/; return $v }
3804
    }
3805
);
3806
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule_off => 1);
3807
$result = $dbi->select(table => $table1);
3808
like($result->type_rule_off->fetch_first->[0], qr/^2010-01-03/);
3809
$result = $dbi->select(table => $table1);
3810
like($result->type_rule_on->fetch_first->[0], qr/^2010-01-07/);
test cleanup
Yuki Kimoto authored on 2011-08-10
3811

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3812
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3813
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3814
$dbi->execute($create_table1_type);
3815
$dbi->type_rule(
3816
    into1 => {
3817
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3818
    },
3819
    into2 => {
3820
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3821
    },
3822
    from1 => {
3823
        $date_datatype => sub { my $v = shift || ''; $v =~ s/(3|5)/6/; return $v }
3824
    },
3825
    from2 => {
3826
        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/7/; return $v }
3827
    }
3828
);
3829
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule1_off => 1);
3830
$result = $dbi->select(table => $table1);
3831
like($result->type_rule1_off->fetch_first->[0], qr/^2010-01-05/);
3832
$result = $dbi->select(table => $table1);
3833
like($result->type_rule1_on->fetch_first->[0], qr/^2010-01-07/);
test cleanup
Yuki Kimoto authored on 2011-08-10
3834

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3835
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3836
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3837
$dbi->execute($create_table1_type);
3838
$dbi->type_rule(
3839
    into1 => {
3840
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3841
    },
3842
    into2 => {
3843
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3844
    },
3845
    from1 => {
3846
        $date_datatype => sub { my $v = shift || ''; $v =~ s/5/6/; return $v }
3847
    },
3848
    from2 => {
3849
        $date_datatype => sub { my $v = shift || ''; $v =~ s/(3|6)/7/; return $v }
3850
    }
3851
);
3852
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule2_off => 1);
3853
$result = $dbi->select(table => $table1);
3854
like($result->type_rule2_off->fetch_first->[0], qr/^2010-01-06/);
3855
$result = $dbi->select(table => $table1);
3856
like($result->type_rule2_on->fetch_first->[0], qr/^2010-01-07/);
test cleanup
Yuki Kimoto authored on 2011-08-10
3857

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

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