DBIx-Custom / t / common.t /
Newer Older
3865 lines | 126.933kb
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;
6
use lib "$FindBin::Bin/common";
cleanup
Yuki Kimoto authored on 2011-08-13
7
use Scalar::Util 'isweak';
cleanup test
Yuki Kimoto authored on 2011-08-10
8

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

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

            
15
plan 'no_plan';
16

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

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
78
use MyDBI1;
79
{
80
    package MyDBI4;
81

            
82
    use strict;
83
    use warnings;
84

            
85
    use base 'DBIx::Custom';
86

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

            
98
    package MyModel2::Base1;
99

            
100
    use strict;
101
    use warnings;
102

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

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

            
107
    use strict;
108
    use warnings;
109

            
110
    use base 'MyModel2::Base1';
111

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

            
118
    sub list { shift->select; }
119

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

            
122
    use strict;
123
    use warnings;
124

            
125
    use base 'MyModel2::Base1';
126

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

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

            
135
    package MyModel2::TABLE1;
136

            
137
    use strict;
138
    use warnings;
139

            
140
    use base 'MyModel2::Base1';
141

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

            
148
    sub list { shift->select; }
149

            
150
    package MyModel2::TABLE2;
151

            
152
    use strict;
153
    use warnings;
154

            
155
    use base 'MyModel2::Base1';
156

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

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

            
168
    use strict;
169
    use warnings;
170

            
171
    use base 'DBIx::Custom';
172

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
415
$dbi->delete_all(table => $table1);
416
$dbi->insert(table => $table1, param => {$key1 => 'a:b c:d', $key2 => 2});
417
$source = "select * from $table1 where $key1 = 'a\\:b c\\:d' and $key2 = :$key2";
cleanup test
Yuki Kimoto authored on 2011-08-08
418
$result = $dbi->execute(
419
    $source,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
420
    param => {$key2 => 2},
cleanup test
Yuki Kimoto authored on 2011-08-08
421
);
422
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
423
is_deeply($rows, [{$key1 => 'a:b c:d', $key2 => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-08
424

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
602
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
603
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
604
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
605
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
606
$dbi->update({$key2 => 11}, table => $table1, where => {$key1 => 1});
cleanup test
Yuki Kimoto authored on 2011-08-15
607
$result = $dbi->execute("select * from $table1 order by $key1");
test cleanup
Yuki Kimoto authored on 2011-08-10
608
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
609
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
610
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
611
                  "basic");
612

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

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

            
637

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

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

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

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

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

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

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

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

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

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

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

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

            
733

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
871

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

            
877
$dbi->begin_work;
878

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

            
885
$dbi->rollback if $@;
886

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

            
891
$dbi->begin_work;
892

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

            
898
$dbi->commit unless $@;
899

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1013
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1014
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1015
$dbi->execute($create_table1);
1016
$dbi->register_filter(twice => sub { $_[0] * 2 });
1017
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1018
    $table1, $key1 => {out => 'twice', in => 'twice'}
test cleanup
Yuki Kimoto authored on 2011-08-10
1019
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1020
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}, filter => {$key1 => undef});
1021
$dbi->update(table => $table1, param => {$key1 => 2}, where => {$key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-15
1022
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
1023
$row   = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1024
is_deeply($row, {$key1 => 4, $key2 => 2}, "update");
test cleanup
Yuki Kimoto authored on 2011-08-10
1025

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1215
test 'select query option';
1216
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1217
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1218
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1219
$query = $dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1220
is(ref $query, 'DBIx::Custom::Query');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1221
$query = $dbi->update(table => $table1, where => {$key1 => 1}, param => {$key2 => 2}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1222
is(ref $query, 'DBIx::Custom::Query');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1223
$query = $dbi->delete(table => $table1, where => {$key1 => 1}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1224
is(ref $query, 'DBIx::Custom::Query');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1225
$query = $dbi->select(table => $table1, where => {$key1 => 1, $key2 => 2}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1226
is(ref $query, 'DBIx::Custom::Query');
1227

            
1228
test 'where';
1229
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1230
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1231
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1232
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1233
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
1234
$where = $dbi->where->clause(['and', "$key1 = :$key1", "$key2 = :$key2"]);
1235
is("$where", "where ( $key1 = :$key1 and $key2 = :$key2 )", 'no param');
test cleanup
Yuki Kimoto authored on 2011-08-10
1236

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1435
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1436
             ->clause(['and', "{> $key1}", "{< $key1}" ])
1437
             ->param({$key1 => [2, $dbi->not_exists]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1438
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1439
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1440
    where => $where,
1441
);
1442
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1443
is_deeply($row, [{$key1 => 3, $key2 => 4}], 'not_exists');
test cleanup
Yuki Kimoto authored on 2011-08-10
1444

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

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

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

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

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

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

            
1498

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1696
$dbi->apply_filter(
1697

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1970
test 'columns';
1971
use MyDBI1;
1972
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-15
1973
$model = $dbi->model(lc $table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
1974

            
1975

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

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

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

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

            
2034

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2228

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

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

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

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

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

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

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

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

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

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

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

            
2365

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

            
2381

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

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

            
2402

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

            
2421

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

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

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

            
2461

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

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

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

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

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

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

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

            
2555

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

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

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

            
2581

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2901

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

            
2910

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
3104

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
3247

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

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

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

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

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

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3375
test 'setup_model';
3376
use MyDBI1;
3377
$dbi = MyDBI1->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3378
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3379
eval { $dbi->execute("drop table $table2") };
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
3380

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3381
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-15
3382
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-10
3383
$dbi->setup_model;
cleanup test
Yuki Kimoto authored on 2011-08-15
3384
is_deeply([sort @{$dbi->model($table1)->columns}], [$key1, $key2]);
3385
is_deeply([sort @{$dbi->model($table2)->columns}], [$key1, $key3]);
cleanup test
Yuki Kimoto authored on 2011-08-10
3386

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3440
test 'type_rule into';
3441
eval { $dbi->execute("drop table $table1") };
3442
$dbi->execute($create_table1_type);
3443
$dbi = DBIx::Custom->connect;
3444
eval { $dbi->execute("drop table $table1") };
3445
$dbi->execute($create_table1_type);
3446

            
3447
$dbi->type_rule(
3448
    into1 => {
3449
        $date_typename => sub { '2010-' . $_[0] }
3450
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3451
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3452
$dbi->insert({$key1 => '01-01'}, table => $table1);
3453
$result = $dbi->select(table => $table1);
3454
like($result->one->{$key1}, qr/^2010-01-01/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3455

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

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

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

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

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

            
3561

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

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

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

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3660
eval{$dbi->type_rule(
3661
    into1 => {
3662
        $date_typename => 'pp'
3663
    }
3664
)};
3665
like($@, qr/not registered/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3666

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3679
eval {
3680
    $dbi->type_rule(
3681
        into1 => {
3682
            Date => sub { $_[0] * 2 },
3683
        }
3684
    );
3685
};
3686
like($@, qr/lower/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3687

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

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

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

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

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

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

            
3761
$result = $dbi->select(table => $table1);
3762
$result->type_rule(
3763
    from1 => [$date_datatype => undef]
3764
);
3765
$row = $result->one;
3766
like($row->{$key1}, qr/^2010-01-03/);
3767
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3768

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

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

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

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

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

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

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