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

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

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

            
14
plan 'no_plan';
15

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

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

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

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

            
86
    use strict;
87
    use warnings;
88

            
89
    use base 'DBIx::Custom';
90

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

            
102
    package MyModel2::Base1;
103

            
104
    use strict;
105
    use warnings;
106

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

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

            
111
    use strict;
112
    use warnings;
113

            
114
    use base 'MyModel2::Base1';
115

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

            
122
    sub list { shift->select; }
123

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

            
126
    use strict;
127
    use warnings;
128

            
129
    use base 'MyModel2::Base1';
130

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

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

            
139
    package MyModel2::TABLE1;
140

            
141
    use strict;
142
    use warnings;
143

            
144
    use base 'MyModel2::Base1';
145

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

            
152
    sub list { shift->select; }
153

            
154
    package MyModel2::TABLE2;
155

            
156
    use strict;
157
    use warnings;
158

            
159
    use base 'MyModel2::Base1';
160

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

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

            
172
    use strict;
173
    use warnings;
174

            
175
    use base 'DBIx::Custom';
176

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
509
test 'update';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
510
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
511
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
512
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
513
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
514
$dbi->update(table => $table1, param => {$key2 => 11}, where => {$key1 => 1});
cleanup test
Yuki Kimoto authored on 2011-08-15
515
$result = $dbi->execute("select * from $table1 order by $key1");
test cleanup
Yuki Kimoto authored on 2011-08-10
516
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
517
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
518
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
519
                  "basic");
520
                  
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
521
$dbi->execute("delete from $table1");
522
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
523
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
524
$dbi->update(table => $table1, param => {$key2 => 12}, where => {$key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-15
525
$result = $dbi->execute("select * from $table1 order by $key1");
test cleanup
Yuki Kimoto authored on 2011-08-10
526
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
527
is_deeply($rows, [{$key1 => 1, $key2 => 12, $key3 => 3, $key4 => 4, $key5 => 5},
528
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
529
                  "update key same as search key");
530

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
538
$dbi->execute("delete from $table1");
539
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
540
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
test cleanup
Yuki Kimoto authored on 2011-08-10
541
$dbi->register_filter(twice => sub { $_[0] * 2 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
542
$dbi->update(table => $table1, param => {$key2 => 11}, where => {$key1 => 1},
543
              filter => {$key2 => sub { $_[0] * 2 }});
cleanup test
Yuki Kimoto authored on 2011-08-15
544
$result = $dbi->execute("select * from $table1 order by $key1");
test cleanup
Yuki Kimoto authored on 2011-08-10
545
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
546
is_deeply($rows, [{$key1 => 1, $key2 => 22, $key3 => 3, $key4 => 4, $key5 => 5},
547
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
548
                  "filter");
549

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

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

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

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

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
568
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
569
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
570
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
571
$dbi->update(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
572
    table => $table1,
573
    param => {$key1 => 3},
test cleanup
Yuki Kimoto authored on 2011-08-10
574
    where => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
575
        ['and', "$key1 = :$key1", "$key2 = :$key2"],
576
        {$key1 => 1, $key2 => 2}
test cleanup
Yuki Kimoto authored on 2011-08-10
577
    ]
578
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
579
$result = $dbi->select(table => $table1);
580
is_deeply($result->all, [{$key1 => 3, $key2 => 2}], 'update() where');
test cleanup
Yuki Kimoto authored on 2011-08-10
581

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

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

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

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
598
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
599
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
600
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
601
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
602
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
603
$dbi->insert(table => 'table', param => {select => 1});
604
$dbi->update(table => 'table', where => {select => 1}, param => {update => 2});
605
$result = $dbi->execute("select * from ${q}table$p");
606
$rows   = $result->all;
607
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
608

            
609
eval {$dbi->update_all(table => 'table', param => {';' => 2}) };
610
like($@, qr/safety/);
611

            
612
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
613
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
614
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
615
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
616
$dbi->insert(table => 'table', param => {select => 1});
617
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
618
$result = $dbi->execute("select * from ${q}table$p");
619
$rows   = $result->all;
620
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
621

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
622
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
623
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
624
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
625
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
626
$dbi->update({$key2 => 11}, table => $table1, where => {$key1 => 1});
cleanup test
Yuki Kimoto authored on 2011-08-15
627
$result = $dbi->execute("select * from $table1 order by $key1");
test cleanup
Yuki Kimoto authored on 2011-08-10
628
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
629
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
630
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
631
                  "basic");
632

            
updated pod
Yuki Kimoto authored on 2011-09-02
633
eval { $dbi->execute("drop table $table1") };
634
$dbi->execute($create_table1_2);
635
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
636
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
637
$dbi->update(table => $table1, param => {$key2 => 11}, where => {$key1 => 1},
638
wrap => {$key2 => sub { "$_[0] - 1" }});
639
$result = $dbi->execute("select * from $table1 order by $key1");
640
$rows   = $result->all;
641
is_deeply($rows, [{$key1 => 1, $key2 => 10, $key3 => 3, $key4 => 4, $key5 => 5},
642
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
643
                  "basic");
644

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

            
656
test 'update_all';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
657
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
658
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
659
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
660
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
test cleanup
Yuki Kimoto authored on 2011-08-10
661
$dbi->register_filter(twice => sub { $_[0] * 2 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
662
$dbi->update_all(table => $table1, param => {$key2 => 10}, filter => {$key2 => 'twice'});
cleanup test
Yuki Kimoto authored on 2011-08-15
663
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
664
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
665
is_deeply($rows, [{$key1 => 1, $key2 => 20, $key3 => 3, $key4 => 4, $key5 => 5},
666
                  {$key1 => 6, $key2 => 20, $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
667
                  "filter");
668

            
669

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
680
$dbi->execute("delete from $table1");
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
681
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
682
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-10
683
$dbi->register_filter(twice => sub { $_[0] * 2 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
684
$dbi->delete(table => $table1, where => {$key2 => 1}, filter => {$key2 => 'twice'});
cleanup test
Yuki Kimoto authored on 2011-08-15
685
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
686
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
687
is_deeply($rows, [{$key1 => 3, $key2 => 4}], "filter");
test cleanup
Yuki Kimoto authored on 2011-08-10
688

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
691
$dbi->delete_all(table => $table1);
692
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
693
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
694
$dbi->delete(table => $table1, where => {$key1 => 1, $key2 => 2});
695
$rows = $dbi->select(table => $table1)->all;
696
is_deeply($rows, [{$key1 => 3, $key2 => 4}], "delete multi key");
test cleanup
Yuki Kimoto authored on 2011-08-10
697

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

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
701
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
702
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
703
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
704
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-10
705
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
706
$where->clause(['and', "$key1 = :$key1", "$key2 = :$key2"]);
707
$where->param({ke1 => 1, $key2 => 2});
708
$dbi->delete(table => $table1, where => $where);
709
$result = $dbi->select(table => $table1);
710
is_deeply($result->all, [{$key1 => 3, $key2 => 4}], 'delete() where');
test cleanup
Yuki Kimoto authored on 2011-08-10
711

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
712
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
713
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
714
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
715
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-10
716
$dbi->delete(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
717
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
718
    where => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
719
        ['and', "$key1 = :$key1", "$key2 = :$key2"],
720
        {ke1 => 1, $key2 => 2}
test cleanup
Yuki Kimoto authored on 2011-08-10
721
    ]
722
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
723
$result = $dbi->select(table => $table1);
724
is_deeply($result->all, [{$key1 => 3, $key2 => 4}], 'delete() where');
test cleanup
Yuki Kimoto authored on 2011-08-10
725

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

            
734
test 'delete error';
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
eval{$dbi->delete(table => $table1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
738
like($@, qr/"where" must be specified/,
739
         "where key-value pairs not specified");
740

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
744
$dbi = undef;
test cleanup
Yuki Kimoto authored on 2011-08-10
745
$dbi = DBIx::Custom->connect;
746
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
747
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
748
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
749
$dbi->insert(table => 'table', param => {select => 1});
750
$dbi->delete(table => 'table', where => {select => 1});
751
$result = $dbi->execute("select * from ${q}table$p");
752
$rows   = $result->all;
753
is_deeply($rows, [], "reserved word");
754

            
755
test 'delete_all';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
756
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
757
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
758
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
759
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
760
$dbi->delete_all(table => $table1);
cleanup test
Yuki Kimoto authored on 2011-08-15
761
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
762
$rows   = $result->all;
763
is_deeply($rows, [], "basic");
764

            
765

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

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

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

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

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
789
eval { $dbi->execute("drop table $table2") };
test cleanup
Yuki Kimoto authored on 2011-08-10
790
$dbi->execute($create_table2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
791
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
test cleanup
Yuki Kimoto authored on 2011-08-10
792
$rows = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
793
    table => [$table1, $table2],
794
    column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
795
    where   => {"$table1.$key2" => 2},
796
    relation  => {"$table1.$key1" => "$table2.$key1"}
test cleanup
Yuki Kimoto authored on 2011-08-10
797
)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
798
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
799

            
800
$rows = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
801
    table => [$table1, $table2],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
802
    column => ["$table1.$key1 as ${table1}_$key1", "${table2}.$key1 as ${table2}_$key1", $key2, $key3],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
803
    relation  => {"$table1.$key1" => "$table2.$key1"}
test cleanup
Yuki Kimoto authored on 2011-08-10
804
)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
805
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
806

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

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

            
819
test 'fetch filter';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
820
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
821
$dbi->register_filter(
822
    twice       => sub { $_[0] * 2 },
823
    three_times => sub { $_[0] * 3 }
824
);
825
$dbi->default_fetch_filter('twice');
826
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
827
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
828
$result = $dbi->select(table => $table1);
829
$result->filter({$key1 => 'three_times'});
test cleanup
Yuki Kimoto authored on 2011-08-10
830
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
831
is_deeply($row, {$key1 => 3, $key2 => 4}, "default_fetch_filter and filter");
test cleanup
Yuki Kimoto authored on 2011-08-10
832

            
833
test 'filters';
834
$dbi = DBIx::Custom->new;
835

            
836
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')),
837
   'あ', "decode_utf8");
838

            
839
is($dbi->filters->{encode_utf8}->('あ'),
840
   encode_utf8('あ'), "encode_utf8");
841

            
cleanup test
Yuki Kimoto authored on 2011-08-10
842
test 'transaction1';
test cleanup
Yuki Kimoto authored on 2011-08-10
843
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
844
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
845
$dbi->execute($create_table1);
fixed transaction test
Yuki Kimoto authored on 2011-08-14
846
$dbi->begin_work;
847
$dbi->dbh->{AutoCommit} = 0;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
848
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
fixed transaction test
Yuki Kimoto authored on 2011-08-14
849
$dbi->rollback;
850
$dbi->dbh->{AutoCommit} = 1;
851

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

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

            
856
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
857
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
858
$dbi->execute($create_table1);
fixed transaction test
Yuki Kimoto authored on 2011-08-14
859
$dbi->begin_work;
860
$dbi->dbh->{AutoCommit} = 0;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
861
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
862
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
fixed transaction test
Yuki Kimoto authored on 2011-08-14
863
$dbi->commit;
864
$dbi->dbh->{AutoCommit} = 1;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
865
$result = $dbi->select(table => $table1);
866
is_deeply(scalar $result->all, [{$key1 => 1, $key2 => 2}, {$key1 => 2, $key2 => 3}],
fixed transaction test
Yuki Kimoto authored on 2011-08-14
867
          "commit");
test cleanup
Yuki Kimoto authored on 2011-08-10
868

            
869
test 'execute';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
870
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
871
$dbi->execute($create_table1);
872
{
873
    local $Carp::Verbose = 0;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
874
    eval{$dbi->execute("select * frm $table1")};
cleanup test
Yuki Kimoto authored on 2011-08-15
875
    like($@, qr/\Qselect * frm $table1/, "fail prepare");
test cleanup
Yuki Kimoto authored on 2011-08-10
876
    like($@, qr/\.t /, "fail : not verbose");
877
}
878
{
879
    local $Carp::Verbose = 1;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
880
    eval{$dbi->execute("select * frm $table1")};
test cleanup
Yuki Kimoto authored on 2011-08-10
881
    like($@, qr/Custom.*\.t /s, "fail : verbose");
882
}
883

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

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

            
892
{
893
    local $Carp::Verbose = 0;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
894
    eval{$dbi->execute("select * from $table1 where {0 $key1}", {}, query => 1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
895
    like($@, qr/\Q.t /, "caller spec : not vebose");
896
}
897
{
898
    local $Carp::Verbose = 1;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
899
    eval{$dbi->execute("select * from $table1 where {0 $key1}", {}, query => 1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
900
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
901
}
902

            
903

            
cleanup test
Yuki Kimoto authored on 2011-08-10
904
test 'transaction2';
test cleanup
Yuki Kimoto authored on 2011-08-10
905
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
906
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
907
$dbi->execute($create_table1);
908

            
909
$dbi->begin_work;
910

            
911
eval {
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
912
    $dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
913
    die "Error";
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
914
    $dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-10
915
};
916

            
917
$dbi->rollback if $@;
918

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

            
923
$dbi->begin_work;
924

            
925
eval {
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
926
    $dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
927
    $dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-10
928
};
929

            
930
$dbi->commit unless $@;
931

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

            
936
$dbi->dbh->{AutoCommit} = 0;
937
eval{ $dbi->begin_work };
938
ok($@, "exception");
939
$dbi->dbh->{AutoCommit} = 1;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
940

            
test cleanup
Yuki Kimoto authored on 2011-08-10
941
test 'cache';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
942
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
943
$dbi->cache(1);
944
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-15
945
$source = "select * from $table1 where $key1 = :$key1 and $key2 = :$key2";
test cleanup
Yuki Kimoto authored on 2011-08-10
946
$dbi->execute($source, {}, query => 1);
947
is_deeply($dbi->{_cached}->{$source}, 
cleanup test
Yuki Kimoto authored on 2011-08-15
948
          {sql => "select * from $table1 where $key1 = ? and $key2 = ?", columns => [$key1, $key2], tables => []}, "cache");
test cleanup
Yuki Kimoto authored on 2011-08-10
949

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
950
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
951
$dbi->execute($create_table1);
952
$dbi->{_cached} = {};
953
$dbi->cache(0);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
954
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
955
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
956

            
957
test 'execute';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
958
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
959
$dbi->execute($create_table1);
960
{
961
    local $Carp::Verbose = 0;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
962
    eval{$dbi->execute("select * frm $table1")};
cleanup test
Yuki Kimoto authored on 2011-08-15
963
    like($@, qr/\Qselect * frm $table1/, "fail prepare");
test cleanup
Yuki Kimoto authored on 2011-08-10
964
    like($@, qr/\.t /, "fail : not verbose");
965
}
966
{
967
    local $Carp::Verbose = 1;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
968
    eval{$dbi->execute('select * frm $table1')};
test cleanup
Yuki Kimoto authored on 2011-08-10
969
    like($@, qr/Custom.*\.t /s, "fail : verbose");
970
}
971

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

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

            
980
{
981
    local $Carp::Verbose = 0;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
982
    eval{$dbi->execute("select * from $table1 where {0 $key1}", {}, query => 1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
983
    like($@, qr/\Q.t /, "caller spec : not vebose");
984
}
985
{
986
    local $Carp::Verbose = 1;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
987
    eval{$dbi->execute("select * from $table1 where {0 $key1}", {}, query => 1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
988
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
989
}
990

            
991
test 'method';
992
$dbi->method(
993
    one => sub { 1 }
994
);
995
$dbi->method(
996
    two => sub { 2 }
997
);
998
$dbi->method({
999
    twice => sub {
1000
        my $self = shift;
1001
        return $_[0] * 2;
1002
    }
1003
});
1004

            
1005
is($dbi->one, 1, "first");
1006
is($dbi->two, 2, "second");
1007
is($dbi->twice(5), 10 , "second");
1008

            
1009
eval {$dbi->XXXXXX};
1010
ok($@, "not exists");
1011

            
1012
test 'out filter';
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->register_filter(three_times => sub { $_[0] * 3});
1018
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1019
    $table1, $key1 => {out => 'twice', in => 'three_times'}, 
1020
              $key2 => {out => 'three_times', in => 'twice'});
1021
$dbi->insert(table => $table1, param => {$key1 => 1, $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->fetch_hash_first;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1024
is_deeply($row, {$key1 => 2, $key2 => 6}, "insert");
1025
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1026
$row   = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1027
is_deeply($row, {$key1 => 6, $key2 => 12}, "insert");
test cleanup
Yuki Kimoto authored on 2011-08-10
1028

            
1029
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1030
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1031
$dbi->execute($create_table1);
1032
$dbi->register_filter(twice => sub { $_[0] * 2 });
1033
$dbi->register_filter(three_times => sub { $_[0] * 3});
1034
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1035
    $table1, $key1 => {out => 'twice', in => 'three_times'}, 
1036
              $key2 => {out => 'three_times', in => 'twice'});
test cleanup
Yuki Kimoto authored on 2011-08-10
1037
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1038
    $table1, $key1 => {out => undef}
test cleanup
Yuki Kimoto authored on 2011-08-10
1039
); 
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1040
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-15
1041
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
1042
$row   = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1043
is_deeply($row, {$key1 => 1, $key2 => 6}, "insert");
test cleanup
Yuki Kimoto authored on 2011-08-10
1044

            
1045
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1046
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1047
$dbi->execute($create_table1);
1048
$dbi->register_filter(twice => sub { $_[0] * 2 });
1049
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1050
    $table1, $key1 => {out => 'twice', in => 'twice'}
test cleanup
Yuki Kimoto authored on 2011-08-10
1051
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1052
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}, filter => {$key1 => undef});
1053
$dbi->update(table => $table1, param => {$key1 => 2}, where => {$key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-15
1054
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
1055
$row   = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1056
is_deeply($row, {$key1 => 4, $key2 => 2}, "update");
test cleanup
Yuki Kimoto authored on 2011-08-10
1057

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

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

            
1084
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1085
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1086
$dbi->execute($create_table1);
1087
$dbi->register_filter(twice => sub { $_[0] * 2 });
1088
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1089
    $table1, $key1 => {out => 'twice', in => 'twice'}
test cleanup
Yuki Kimoto authored on 2011-08-10
1090
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1091
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 2}, filter => {$key1 => undef});
cleanup test
Yuki Kimoto authored on 2011-08-15
1092
$result = $dbi->execute("select * from $table1 where $key1 = :$key1 and $key2 = :$key2",
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1093
                        param => {$key1 => 1, $key2 => 2},
1094
                        table => [$table1]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1095
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1096
is_deeply($rows, [{$key1 => 4, $key2 => 2}], "execute");
test cleanup
Yuki Kimoto authored on 2011-08-10
1097

            
1098
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1099
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1100
$dbi->execute($create_table1);
1101
$dbi->register_filter(twice => sub { $_[0] * 2 });
1102
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1103
    $table1, $key1 => {out => 'twice', in => 'twice'}
test cleanup
Yuki Kimoto authored on 2011-08-10
1104
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1105
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 2}, filter => {$key1 => undef});
cleanup test
Yuki Kimoto authored on 2011-08-15
1106
$result = $dbi->execute("select * from {table $table1} where $key1 = :$key1 and $key2 = :$key2",
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1107
                        param => {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
1108
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1109
is_deeply($rows, [{$key1 => 4, $key2 => 2}], "execute table tag");
test cleanup
Yuki Kimoto authored on 2011-08-10
1110

            
1111
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1112
eval { $dbi->execute("drop table $table1") };
1113
eval { $dbi->execute("drop table $table2") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1114
$dbi->execute($create_table1);
1115
$dbi->execute($create_table2);
1116
$dbi->register_filter(twice => sub { $_[0] * 2 });
1117
$dbi->register_filter(three_times => sub { $_[0] * 3 });
1118
$dbi->apply_filter(
cleanup test
Yuki Kimoto authored on 2011-08-15
1119
    $table1, $key2 => {out => 'twice', in => 'twice'}
1120
);
1121
$dbi->apply_filter(
1122
    $table2, $key3 => {out => 'three_times', in => 'three_times'}
test cleanup
Yuki Kimoto authored on 2011-08-10
1123
);
cleanup test
Yuki Kimoto authored on 2011-08-15
1124
$dbi->insert(table => $table1, param => {$key1 => 5, $key2 => 2}, filter => {$key2 => undef});
1125
$dbi->insert(table => $table2, param => {$key1 => 5, $key3 => 6}, filter => {$key3 => undef});
1126
$result = $dbi->select(
1127
     table => [$table1, $table2],
1128
     column => [$key2, $key3],
1129
     where => {"$table1.$key2" => 1, "$table2.$key3" => 2}, relation => {"$table1.$key1" => "$table2.$key1"});
1130

            
1131
$result->filter({$key2 => 'twice'});
1132
$rows   = $result->all;
1133
is_deeply($rows, [{$key2 => 4, $key3 => 18}], "select : join");
1134

            
1135
$result = $dbi->select(
1136
     table => [$table1, $table2],
1137
     column => [$key2, $key3],
1138
     where => {$key2 => 1, $key3 => 2}, relation => {"$table1.$key1" => "$table2.$key1"});
1139

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

            
1144
test 'connect super';
test cleanup
Yuki Kimoto authored on 2011-08-10
1145
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1146
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1147
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1148
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1149
is($dbi->select(table => $table1)->one->{$key1}, 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1150

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1151
$dbi = DBIx::Custom->new;
test cleanup
Yuki Kimoto authored on 2011-08-10
1152
$dbi->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1153
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1154
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1155
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1156
is($dbi->select(table => $table1)->one->{$key1}, 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1157

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1158
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1159
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1160
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1161
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1162
is($dbi->select(table => $table1)->one->{$key1}, 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1163

            
1164
test 'end_filter';
1165
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1166
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1167
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1168
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1169
$result = $dbi->select(table => $table1);
1170
$result->filter($key1 => sub { $_[0] * 2 }, $key2 => sub { $_[0] * 4 });
1171
$result->end_filter($key1 => sub { $_[0] * 3 }, $key2 => sub { $_[0] * 5 });
test cleanup
Yuki Kimoto authored on 2011-08-10
1172
$row = $result->fetch_first;
1173
is_deeply($row, [6, 40]);
1174

            
1175
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1176
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1177
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1178
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1179
$result = $dbi->select(table => $table1);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1180
$result->filter([$key1, $key2] => sub { $_[0] * 2 });
1181
$result->end_filter([[$key1, $key2] => sub { $_[0] * 3 }]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1182
$row = $result->fetch_first;
1183
is_deeply($row, [6, 12]);
1184

            
1185
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1186
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1187
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1188
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1189
$result = $dbi->select(table => $table1);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1190
$result->filter([[$key1, $key2] => sub { $_[0] * 2 }]);
1191
$result->end_filter([$key1, $key2] => sub { $_[0] * 3 });
test cleanup
Yuki Kimoto authored on 2011-08-10
1192
$row = $result->fetch_first;
1193
is_deeply($row, [6, 12]);
1194

            
1195
$dbi->register_filter(five_times => sub { $_[0] * 5 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1196
$result = $dbi->select(table => $table1);
1197
$result->filter($key1 => sub { $_[0] * 2 }, $key2 => sub { $_[0] * 4 });
1198
$result->end_filter({$key1 => sub { $_[0] * 3 }, $key2 => 'five_times' });
test cleanup
Yuki Kimoto authored on 2011-08-10
1199
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1200
is_deeply($row, {$key1 => 6, $key2 => 40});
test cleanup
Yuki Kimoto authored on 2011-08-10
1201

            
1202
$dbi->register_filter(five_times => sub { $_[0] * 5 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1203
$dbi->apply_filter($table1,
1204
    $key1 => {end => sub { $_[0] * 3 } },
1205
    $key2 => {end => 'five_times'}
test cleanup
Yuki Kimoto authored on 2011-08-10
1206
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1207
$result = $dbi->select(table => $table1);
1208
$result->filter($key1 => sub { $_[0] * 2 }, $key2 => sub { $_[0] * 4 });
test cleanup
Yuki Kimoto authored on 2011-08-10
1209
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1210
is_deeply($row, {$key1 => 6, $key2 => 40}, 'apply_filter');
test cleanup
Yuki Kimoto authored on 2011-08-10
1211

            
1212
$dbi->register_filter(five_times => sub { $_[0] * 5 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1213
$dbi->apply_filter($table1,
1214
    $key1 => {end => sub { $_[0] * 3 } },
1215
    $key2 => {end => 'five_times'}
1216
);
1217
$result = $dbi->select(table => $table1);
1218
$result->filter($key1 => sub { $_[0] * 2 }, $key2 => sub { $_[0] * 4 });
1219
$result->filter($key1 => undef);
1220
$result->end_filter($key1 => undef);
test cleanup
Yuki Kimoto authored on 2011-08-10
1221
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1222
is_deeply($row, {$key1 => 1, $key2 => 40}, 'apply_filter overwrite');
test cleanup
Yuki Kimoto authored on 2011-08-10
1223

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

            
1238
test 'empty where select';
1239
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1240
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1241
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1242
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1243
$result = $dbi->select(table => $table1, where => {});
test cleanup
Yuki Kimoto authored on 2011-08-10
1244
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1245
is_deeply($row, {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
1246

            
1247
test 'select query option';
1248
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1249
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1250
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1251
$query = $dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1252
is(ref $query, 'DBIx::Custom::Query');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1253
$query = $dbi->update(table => $table1, where => {$key1 => 1}, param => {$key2 => 2}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1254
is(ref $query, 'DBIx::Custom::Query');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1255
$query = $dbi->delete(table => $table1, where => {$key1 => 1}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1256
is(ref $query, 'DBIx::Custom::Query');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1257
$query = $dbi->select(table => $table1, where => {$key1 => 1, $key2 => 2}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1258
is(ref $query, 'DBIx::Custom::Query');
1259

            
1260
test 'where';
1261
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1262
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1263
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1264
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1265
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
1266
$where = $dbi->where->clause(['and', "$key1 = :$key1", "$key2 = :$key2"]);
1267
is("$where", "where ( $key1 = :$key1 and $key2 = :$key2 )", 'no param');
test cleanup
Yuki Kimoto authored on 2011-08-10
1268

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

            
1273
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1274
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1275
    where => $where
1276
);
1277
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1278
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1279

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

            
1290
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1291
             ->clause(['and', "$key1 = :$key1", "$key2 = :$key2"])
1292
             ->param({$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
1293
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1294
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1295
    where => $where
1296
);
1297
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1298
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1299

            
1300
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1301
             ->clause(['and', "$key1 = :$key1", "$key2 = :$key2"])
test cleanup
Yuki Kimoto authored on 2011-08-10
1302
             ->param({});
1303
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1304
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1305
    where => $where,
1306
);
1307
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1308
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1309

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

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

            
1328
eval {
1329
$where = $dbi->where
1330
             ->clause(['uuu']);
1331
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1332
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1333
    where => $where
1334
);
1335
};
1336
ok($@);
1337

            
1338
$where = $dbi->where;
1339
is("$where", '');
1340

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

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

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

            
1371
$where = $dbi->where
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1372
             ->clause("$key1 = :$key1")
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1373
             ->param({$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1374
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1375
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1376
    where => $where,
1377
);
1378
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1379
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1380

            
1381
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1382
             ->clause("$key1 = :$key1 $key2 = :$key2")
1383
             ->param({$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1384
eval{$where->to_string};
1385
like($@, qr/one column/);
1386

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

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

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

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

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

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

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

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

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

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

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

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

            
1507
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1508
             ->clause(['and',"$key1 is not null", "$key2 is not null" ]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1509
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1510
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1511
    where => $where,
1512
);
1513
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1514
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], 'not_exists');
test cleanup
Yuki Kimoto authored on 2011-08-10
1515

            
1516
eval {$dbi->where(ppp => 1) };
1517
like($@, qr/invalid/);
1518

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

            
1530

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

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

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

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

            
1568
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1569
$where->clause(['and', ":${key1}{=}"]);
1570
$where->param({$key1 => [undef, undef]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1571
$where->if('defined');
1572
$where->map;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1573
$result = $dbi->execute("select * from $table1 $where", {$key1 => [1, 0]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1574
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1575
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
1576
$result = $dbi->execute("select * from $table1 $where", {$key1 => [0, 1]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1577
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1578
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1579

            
1580
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1581
$where->clause(['and', ":${key1}{=}"]);
1582
$where->param({$key1 => 0});
test cleanup
Yuki Kimoto authored on 2011-08-10
1583
$where->if('length');
1584
$where->map;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1585
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1586
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1587
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1588

            
1589
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1590
$where->clause(['and', ":${key1}{=}"]);
1591
$where->param({$key1 => ''});
test cleanup
Yuki Kimoto authored on 2011-08-10
1592
$where->if('length');
1593
$where->map;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1594
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1595
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1596
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1597

            
1598
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1599
$where->clause(['and', ":${key1}{=}"]);
1600
$where->param({$key1 => 5});
test cleanup
Yuki Kimoto authored on 2011-08-10
1601
$where->if(sub { ($_[0] || '') eq 5 });
1602
$where->map;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1603
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1604
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1605
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1606

            
1607
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1608
$where->clause(['and', ":${key1}{=}"]);
1609
$where->param({$key1 => 7});
test cleanup
Yuki Kimoto authored on 2011-08-10
1610
$where->if(sub { ($_[0] || '') eq 5 });
1611
$where->map;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1612
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1613
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1614
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1615

            
1616
$where = $dbi->where;
1617
$where->param({id => 1, author => 'Ken', price => 1900});
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1618
$where->map(id => "$table1.id",
1619
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
1620
    price => ["$table1.price", {if => sub { $_[0] eq 1900 }}]
test cleanup
Yuki Kimoto authored on 2011-08-10
1621
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1622
is_deeply($where->param, {"$table1.id" => 1, "$table1.author" => '%Ken%',
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1623
  "$table1.price" => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
1624

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

            
1635
$where = $dbi->where;
1636
$where->param({id => '', author => '', price => ''});
1637
$where->if('length');
1638
$where->map(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1639
    id => "$table1.id",
1640
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
1641
    price => ["$table1.price", sub { '%' . $_[0] . '%' },
test cleanup
Yuki Kimoto authored on 2011-08-10
1642
      {if => sub { $_[0] eq 1 }}]
1643
);
1644
is_deeply($where->param, {});
1645

            
1646
$where = $dbi->where;
1647
$where->param({id => undef, author => undef, price => undef});
1648
$where->if('length');
1649
$where->map(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1650
    id => "$table1.id",
1651
    price => ["$table1.price", {if => 'exists'}]
test cleanup
Yuki Kimoto authored on 2011-08-10
1652
);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1653
is_deeply($where->param, {"$table1.price" => undef});
test cleanup
Yuki Kimoto authored on 2011-08-10
1654

            
1655
$where = $dbi->where;
1656
$where->param({price => 'a'});
1657
$where->if('length');
1658
$where->map(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1659
    id => ["$table1.id", {if => 'exists'}],
1660
    price => ["$table1.price", sub { '%' . $_[0] }, {if => 'exists'}]
test cleanup
Yuki Kimoto authored on 2011-08-10
1661
);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1662
is_deeply($where->param, {"$table1.price" => '%a'});
test cleanup
Yuki Kimoto authored on 2011-08-10
1663

            
1664
$where = $dbi->where;
1665
$where->param({id => [1, 2], author => 'Ken', price => 1900});
1666
$where->map(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1667
    id => "$table1.id",
1668
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
1669
    price => ["$table1.price", {if => sub { $_[0] eq 1900 }}]
test cleanup
Yuki Kimoto authored on 2011-08-10
1670
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1671
is_deeply($where->param, {"$table1.id" => [1, 2], "$table1.author" => '%Ken%',
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1672
  "$table1.price" => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
1673

            
1674
$where = $dbi->where;
1675
$where->if('length');
1676
$where->param({id => ['', ''], author => 'Ken', price => 1900});
1677
$where->map(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1678
    id => "$table1.id",
1679
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
1680
    price => ["$table1.price", {if => sub { $_[0] eq 1900 }}]
test cleanup
Yuki Kimoto authored on 2011-08-10
1681
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1682
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
1683
  "$table1.price" => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
1684

            
1685
$where = $dbi->where;
1686
$where->param({id => ['', ''], author => 'Ken', price => 1900});
1687
$where->map(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1688
    id => ["$table1.id", {if => 'length'}],
1689
    author => ["$table1.author", sub { '%' . $_[0] . '%' }, {if => 'defined'}],
1690
    price => ["$table1.price", {if => sub { $_[0] eq 1900 }}]
test cleanup
Yuki Kimoto authored on 2011-08-10
1691
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1692
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
1693
  "$table1.price" => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
1694

            
1695
test 'register_tag_processor';
1696
$dbi = DBIx::Custom->connect;
1697
$dbi->register_tag_processor(
1698
    a => sub { 1 }
1699
);
test cleanup
Yuki Kimoto authored on 2011-08-10
1700
is($dbi->{_tags}->{a}->(), 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1701

            
1702
test 'register_tag';
1703
$dbi = DBIx::Custom->connect;
1704
$dbi->register_tag(
1705
    b => sub { 2 }
1706
);
test cleanup
Yuki Kimoto authored on 2011-08-10
1707
is($dbi->{_tags}->{b}->(), 2);
test cleanup
Yuki Kimoto authored on 2011-08-10
1708

            
1709
test 'table not specify exception';
1710
$dbi = DBIx::Custom->connect;
1711
eval {$dbi->insert};
1712
like($@, qr/table/);
1713
eval {$dbi->update};
1714
like($@, qr/table/);
1715
eval {$dbi->delete};
1716
like($@, qr/table/);
1717
eval {$dbi->select};
1718
like($@, qr/table/);
test cleanup
Yuki Kimoto authored on 2011-08-10
1719

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1720
test 'more tests';
1721
$dbi = DBIx::Custom->connect;
1722
eval{$dbi->apply_filter('table', 'column', [])};
1723
like($@, qr/apply_filter/);
1724

            
1725
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
1726
like($@, qr/apply_filter/);
1727

            
1728
$dbi->apply_filter(
1729

            
1730
);
1731
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1732
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1733
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1734
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1735
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
1736
$dbi->apply_filter($table1, $key2, 
test cleanup
Yuki Kimoto authored on 2011-08-10
1737
                   {in => sub { $_[0] * 3 }, out => sub { $_[0] * 2 }});
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1738
$rows = $dbi->select(table => $table1, where => {$key2 => 1})->all;
1739
is_deeply($rows, [{$key1 => 1, $key2 => 6}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1740

            
1741
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1742
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1743
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1744
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1745
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
1746
$dbi->apply_filter($table1, $key2, {});
1747
$rows = $dbi->select(table => $table1, where => {$key2 => 2})->all;
1748
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1749

            
1750
$dbi = DBIx::Custom->connect;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1751
eval {$dbi->apply_filter($table1, $key2, {out => 'no'})};
test cleanup
Yuki Kimoto authored on 2011-08-10
1752
like($@, qr/not registered/);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1753
eval {$dbi->apply_filter($table1, $key2, {in => 'no'})};
test cleanup
Yuki Kimoto authored on 2011-08-10
1754
like($@, qr/not registered/);
1755
$dbi->method({one => sub { 1 }});
1756
is($dbi->one, 1);
1757

            
1758
eval{DBIx::Custom->connect(dsn => undef)};
1759
like($@, qr/_connect/);
1760

            
1761
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1762
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1763
$dbi->execute($create_table1);
1764
$dbi->register_filter(twice => sub { $_[0] * 2 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1765
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2},
1766
             filter => {$key1 => 'twice'});
1767
$row = $dbi->select(table => $table1)->one;
1768
is_deeply($row, {$key1 => 2, $key2 => 2});
1769
eval {$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2},
1770
             filter => {$key1 => 'no'}) };
test cleanup
Yuki Kimoto authored on 2011-08-10
1771
like($@, qr//);
1772

            
1773
$dbi->register_filter(one => sub { });
1774
$dbi->default_fetch_filter('one');
1775
ok($dbi->default_fetch_filter);
1776
$dbi->default_bind_filter('one');
1777
ok($dbi->default_bind_filter);
1778
eval{$dbi->default_fetch_filter('no')};
1779
like($@, qr/not registered/);
1780
eval{$dbi->default_bind_filter('no')};
1781
like($@, qr/not registered/);
1782
$dbi->default_bind_filter(undef);
1783
ok(!defined $dbi->default_bind_filter);
1784
$dbi->default_fetch_filter(undef);
1785
ok(!defined $dbi->default_fetch_filter);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1786
eval {$dbi->execute("select * from $table1 {} {= author") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1787
like($@, qr/Tag not finished/);
1788

            
1789
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1790
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1791
$dbi->execute($create_table1);
1792
$dbi->register_filter(one => sub { 1 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1793
$result = $dbi->select(table => $table1);
1794
eval {$result->filter($key1 => 'no')};
test cleanup
Yuki Kimoto authored on 2011-08-10
1795
like($@, qr/not registered/);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1796
eval {$result->end_filter($key1 => 'no')};
test cleanup
Yuki Kimoto authored on 2011-08-10
1797
like($@, qr/not registered/);
1798
$result->default_filter(undef);
1799
ok(!defined $result->default_filter);
1800
$result->default_filter('one');
1801
is($result->default_filter->(), 1);
1802

            
1803
test 'dbi_option';
1804
$dbi = DBIx::Custom->connect(dbi_option => {PrintError => 1});
1805
ok($dbi->dbh->{PrintError});
1806
$dbi = DBIx::Custom->connect(dbi_options => {PrintError => 1});
1807
ok($dbi->dbh->{PrintError});
1808

            
1809
test 'DBIx::Custom::Result stash()';
1810
$result = DBIx::Custom::Result->new;
1811
is_deeply($result->stash, {}, 'default');
1812
$result->stash->{foo} = 1;
1813
is($result->stash->{foo}, 1, 'get and set');
test cleanup
Yuki Kimoto authored on 2011-08-10
1814

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1815
test 'delete_at';
1816
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1817
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
1818
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1819
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1820
$dbi->delete_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1821
    table => $table1,
1822
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1823
    where => [1, 2],
1824
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1825
is_deeply($dbi->select(table => $table1)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
1826

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1827
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1828
$dbi->delete_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1829
    table => $table1,
1830
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
1831
    where => 1,
1832
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1833
is_deeply($dbi->select(table => $table1)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
1834

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1849
$dbi->delete_all(table => $table1);
1850
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1851
$dbi->insert_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1852
    primary_key => $key1, 
1853
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
1854
    where => 1,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1855
    param => {$key2 => 2, $key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
1856
);
1857

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

            
1862
eval {
1863
    $dbi->insert_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1864
        table => $table1,
1865
        primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1866
        where => {},
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1867
        param => {$key1 => 1, $key2 => 2, $key3 => 3},
cleanup test
Yuki Kimoto authored on 2011-08-10
1868
    );
1869
};
1870
like($@, qr/must be/);
1871

            
1872
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1873
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
1874
$dbi->execute($create_table1_2);
1875
$dbi->insert_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1876
    {$key3 => 3},
1877
    primary_key => [$key1, $key2], 
1878
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
1879
    where => [1, 2],
1880
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1881
is($dbi->select(table => $table1)->one->{$key1}, 1);
1882
is($dbi->select(table => $table1)->one->{$key2}, 2);
1883
is($dbi->select(table => $table1)->one->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
1884

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1900
$dbi->delete_all(table => $table1);
1901
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1902
$dbi->update_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1903
    table => $table1,
1904
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
1905
    where => 1,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1906
    param => {$key3 => 4}
cleanup test
Yuki Kimoto authored on 2011-08-10
1907
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1908
is($dbi->select(table => $table1)->one->{$key1}, 1);
1909
is($dbi->select(table => $table1)->one->{$key2}, 2);
1910
is($dbi->select(table => $table1)->one->{$key3}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-10
1911

            
1912
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1913
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
1914
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1915
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1916
$dbi->update_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1917
    {$key3 => 4},
1918
    table => $table1,
1919
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1920
    where => [1, 2]
1921
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1922
is($dbi->select(table => $table1)->one->{$key1}, 1);
1923
is($dbi->select(table => $table1)->one->{$key2}, 2);
1924
is($dbi->select(table => $table1)->one->{$key3}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-10
1925

            
1926
test 'select_at';
1927
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1928
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
1929
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1930
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1931
$result = $dbi->select_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1932
    table => $table1,
1933
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1934
    where => [1, 2]
1935
);
1936
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1937
is($row->{$key1}, 1);
1938
is($row->{$key2}, 2);
1939
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
1940

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1941
$dbi->delete_all(table => $table1);
1942
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1943
$result = $dbi->select_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1944
    table => $table1,
1945
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
1946
    where => 1,
1947
);
1948
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1949
is($row->{$key1}, 1);
1950
is($row->{$key2}, 2);
1951
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
1952

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1953
$dbi->delete_all(table => $table1);
1954
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1955
$result = $dbi->select_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1956
    table => $table1,
1957
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1958
    where => [1, 2]
1959
);
1960
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1961
is($row->{$key1}, 1);
1962
is($row->{$key2}, 2);
1963
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
1964

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

            
1974
eval {
1975
    $result = $dbi->select_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1976
        table => $table1,
1977
        primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1978
        where => [1],
1979
    );
1980
};
1981
like($@, qr/same/);
1982

            
1983
eval {
1984
    $result = $dbi->update_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1985
        table => $table1,
1986
        primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1987
        where => {},
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1988
        param => {$key1 => 1, $key2 => 2},
cleanup test
Yuki Kimoto authored on 2011-08-10
1989
    );
1990
};
1991
like($@, qr/must be/);
1992

            
1993
eval {
1994
    $result = $dbi->delete_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1995
        table => $table1,
1996
        primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1997
        where => {},
1998
    );
1999
};
2000
like($@, qr/must be/);
2001

            
2002
test 'model delete_at';
2003
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2004
eval { $dbi->execute("drop table $table1") };
2005
eval { $dbi->execute("drop table $table2") };
2006
eval { $dbi->execute("drop table $table3") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2007
$dbi->execute($create_table1_2);
2008
$dbi->execute($create_table2_2);
2009
$dbi->execute($create_table3);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2010
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2011
$dbi->model($table1)->delete_at(where => [1, 2]);
2012
is_deeply($dbi->select(table => $table1)->all, []);
2013
$dbi->insert(table => $table2, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2014
$dbi->model($table1)->delete_at(where => [1, 2]);
2015
is_deeply($dbi->select(table => $table1)->all, []);
2016
$dbi->insert(table => $table3, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2017
$dbi->model($table3)->delete_at(where => [1, 2]);
2018
is_deeply($dbi->select(table => $table3)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
2019

            
2020
test 'model insert_at';
2021
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2022
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2023
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2024
$dbi->model($table1)->insert_at(
cleanup test
Yuki Kimoto authored on 2011-08-10
2025
    where => [1, 2],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2026
    param => {$key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
2027
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2028
$result = $dbi->model($table1)->select;
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
test 'model update_at';
2035
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2036
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2037
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2038
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2039
$dbi->model($table1)->update_at(
cleanup test
Yuki Kimoto authored on 2011-08-10
2040
    where => [1, 2],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2041
    param => {$key3 => 4}
cleanup test
Yuki Kimoto authored on 2011-08-10
2042
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2043
$result = $dbi->model($table1)->select;
cleanup test
Yuki Kimoto authored on 2011-08-10
2044
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2045
is($row->{$key1}, 1);
2046
is($row->{$key2}, 2);
2047
is($row->{$key3}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-10
2048

            
2049
test 'model select_at';
2050
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2051
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2052
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2053
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2054
$result = $dbi->model($table1)->select_at(where => [1, 2]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2055
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2056
is($row->{$key1}, 1);
2057
is($row->{$key2}, 2);
2058
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2059

            
2060

            
2061
test 'mycolumn and column';
2062
$dbi = MyDBI7->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
2063
$dbi->user_table_info($user_table_info);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2064
eval { $dbi->execute("drop table $table1") };
2065
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2066
$dbi->execute($create_table1);
2067
$dbi->execute($create_table2);
2068
$dbi->separator('__');
2069
$dbi->setup_model;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2070
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2071
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
2072
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2073
$result = $model->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2074
    column => [$model->mycolumn, $model->column($table2)],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2075
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2076
);
2077
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2078
          {$key1 => 1, $key2 => 2, "${table2}__$key1" => 1, "${table2}__$key3" => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2079

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2080
test 'insert_param';
2081
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2082
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2083
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2084
$param = {$key1 => 1, $key2 => 2};
test cleanup
Yuki Kimoto authored on 2011-08-10
2085
$insert_param = $dbi->insert_param($param);
2086
$sql = <<"EOS";
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2087
insert into $table1 $insert_param
test cleanup
Yuki Kimoto authored on 2011-08-10
2088
EOS
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2089
$dbi->execute($sql, param => $param, table => $table1);
2090
is($dbi->select(table => $table1)->one->{$key1}, 1);
2091
is($dbi->select(table => $table1)->one->{$key2}, 2);
test cleanup
Yuki Kimoto authored on 2011-08-10
2092

            
2093
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2094
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2095
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2096
$param = {$key1 => 1, $key2 => 2};
test cleanup
Yuki Kimoto authored on 2011-08-10
2097
$insert_param = $dbi->insert_param($param);
2098
$sql = <<"EOS";
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2099
insert into $table1 $insert_param
test cleanup
Yuki Kimoto authored on 2011-08-10
2100
EOS
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2101
$dbi->execute($sql, param => $param, table => $table1);
2102
is($dbi->select(table => $table1)->one->{$key1}, 1);
2103
is($dbi->select(table => $table1)->one->{$key2}, 2);
test cleanup
Yuki Kimoto authored on 2011-08-10
2104

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2108
test 'mycolumn';
2109
$dbi = MyDBI8->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
2110
$dbi->user_table_info($user_table_info);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2111
eval { $dbi->execute("drop table $table1") };
2112
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2113
$dbi->execute($create_table1);
2114
$dbi->execute($create_table2);
2115
$dbi->setup_model;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2116
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2117
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
2118
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2119
$result = $model->select_at(
2120
    column => [
2121
        $model->mycolumn,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2122
        $model->column($table2)
cleanup test
Yuki Kimoto authored on 2011-08-10
2123
    ]
2124
);
2125
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2126
          {$key1 => 1, $key2 => 2, "$table2.$key1" => 1, "$table2.$key3" => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2127

            
2128
$result = $model->select_at(
2129
    column => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2130
        $model->mycolumn([$key1]),
2131
        $model->column($table2 => [$key1])
cleanup test
Yuki Kimoto authored on 2011-08-10
2132
    ]
2133
);
2134
is_deeply($result->one,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2135
          {$key1 => 1, "$table2.$key1" => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
2136
$result = $model->select_at(
2137
    column => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2138
        $model->mycolumn([$key1]),
2139
        {$table2 => [$key1]}
cleanup test
Yuki Kimoto authored on 2011-08-10
2140
    ]
2141
);
2142
is_deeply($result->one,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2143
          {$key1 => 1, "$table2.$key1" => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
2144

            
2145
$result = $model->select_at(
2146
    column => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2147
        $model->mycolumn([$key1]),
2148
        ["$table2.$key1", as => "$table2.$key1"]
cleanup test
Yuki Kimoto authored on 2011-08-10
2149
    ]
2150
);
2151
is_deeply($result->one,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2152
          {$key1 => 1, "$table2.$key1" => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
2153

            
2154
$result = $model->select_at(
2155
    column => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2156
        $model->mycolumn([$key1]),
2157
        ["$table2.$key1" => "$table2.$key1"]
cleanup test
Yuki Kimoto authored on 2011-08-10
2158
    ]
2159
);
2160
is_deeply($result->one,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2161
          {$key1 => 1, "$table2.$key1" => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
2162

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2163
test 'merge_param';
2164
$dbi = DBIx::Custom->new;
2165
$params = [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2166
    {$key1 => 1, $key2 => 2, $key3 => 3},
2167
    {$key1 => 1, $key2 => 2},
2168
    {$key1 => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2169
];
2170
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2171
is_deeply($param, {$key1 => [1, 1, 1], $key2 => [2, 2], $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2172

            
2173
$params = [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2174
    {$key1 => [1, 2], $key2 => 1, $key3 => [1, 2]},
2175
    {$key1 => [3, 4], $key2 => [2, 3], $key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
2176
];
2177
$param = $dbi->merge_param($params->[0], $params->[1]);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2178
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
2179

            
2180
test 'select() param option';
2181
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2182
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2183
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2184
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2185
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2186
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2187
$dbi->execute($create_table2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2188
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4});
2189
$dbi->insert(table => $table2, param => {$key1 => 2, $key3 => 5});
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,
2192
    column => "$table1.$key1 as ${table1}_$key1, $key2, $key3",
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2193
    where   => {"$table1.$key2" => 3},
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2194
    join  => ["inner join (select * from $table2 where {= $table2.$key3})" . 
test cleanup
Yuki Kimoto authored on 2011-08-15
2195
              " $table2 on $table1.$key1 = $table2.$key1"],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2196
    param => {"$table2.$key3" => 5}
cleanup test
Yuki Kimoto authored on 2011-08-10
2197
)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2198
is_deeply($rows, [{"${table1}_$key1" => 2, $key2 => 3, $key3 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2199

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

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

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

            
2256

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

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

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2300
$dbi->delete_all(table => $table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2301
$dbi->insert(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2302
    primary_key => $key1, 
2303
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2304
    id => 0,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2305
    param => {$key2 => 2, $key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
2306
);
2307

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

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

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

            
2339
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2340
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2341
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2342
$dbi->model($table1)->insert(
2343
    {$key3 => 3},
cleanup test
Yuki Kimoto authored on 2011-08-10
2344
    id => [1, 2]
2345
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2346
$result = $dbi->model($table1)->select;
cleanup test
Yuki Kimoto authored on 2011-08-10
2347
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2348
is($row->{$key1}, 1);
2349
is($row->{$key2}, 2);
2350
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2351

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2367
$dbi->delete_all(table => $table1);
2368
$dbi->insert(table => $table1, param => {$key1 => 0, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2369
$dbi->update(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2370
    table => $table1,
2371
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2372
    id => 0,
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
is($dbi->select(table => $table1)->one->{$key1}, 0);
2376
is($dbi->select(table => $table1)->one->{$key2}, 2);
2377
is($dbi->select(table => $table1)->one->{$key3}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-10
2378

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

            
2393

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

            
2409

            
2410
test 'delete and id option';
2411
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2412
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2413
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2414
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2415
$dbi->delete(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2416
    table => $table1,
2417
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
2418
    id => [1, 2],
2419
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2420
is_deeply($dbi->select(table => $table1)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
2421

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2422
$dbi->insert(table => $table1, param => {$key1 => 0, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2423
$dbi->delete(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2424
    table => $table1,
2425
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2426
    id => 0,
2427
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2428
is_deeply($dbi->select(table => $table1)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
2429

            
2430

            
2431
test 'model delete and id option';
2432
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2433
eval { $dbi->execute("drop table $table1") };
2434
eval { $dbi->execute("drop table $table2") };
2435
eval { $dbi->execute("drop table $table3") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2436
$dbi->execute($create_table1_2);
2437
$dbi->execute($create_table2_2);
2438
$dbi->execute($create_table3);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2439
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2440
$dbi->model($table1)->delete(id => [1, 2]);
2441
is_deeply($dbi->select(table => $table1)->all, []);
2442
$dbi->insert(table => $table2, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2443
$dbi->model($table1)->delete(id => [1, 2]);
2444
is_deeply($dbi->select(table => $table1)->all, []);
2445
$dbi->insert(table => $table3, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2446
$dbi->model($table3)->delete(id => [1, 2]);
2447
is_deeply($dbi->select(table => $table3)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
2448

            
2449

            
2450
test 'select and id option';
2451
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2452
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2453
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2454
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2455
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2456
    table => $table1,
2457
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
2458
    id => [1, 2]
2459
);
2460
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2461
is($row->{$key1}, 1);
2462
is($row->{$key2}, 2);
2463
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2464

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2465
$dbi->delete_all(table => $table1);
2466
$dbi->insert(table => $table1, param => {$key1 => 0, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2467
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2468
    table => $table1,
2469
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2470
    id => 0,
2471
);
2472
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2473
is($row->{$key1}, 0);
2474
is($row->{$key2}, 2);
2475
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2476

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2477
$dbi->delete_all(table => $table1);
2478
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2479
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2480
    table => $table1,
2481
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
2482
    id => [1, 2]
2483
);
2484
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2485
is($row->{$key1}, 1);
2486
is($row->{$key2}, 2);
2487
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2488

            
2489

            
2490
test 'model select_at';
2491
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2492
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2493
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2494
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2495
$result = $dbi->model($table1)->select(id => [1, 2]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2496
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2497
is($row->{$key1}, 1);
2498
is($row->{$key2}, 2);
2499
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2500

            
2501
test 'column separator is default .';
2502
$dbi = MyDBI7->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
2503
$dbi->user_table_info($user_table_info);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2504
eval { $dbi->execute("drop table $table1") };
2505
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2506
$dbi->execute($create_table1);
2507
$dbi->execute($create_table2);
2508
$dbi->setup_model;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2509
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2510
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
2511
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2512
$result = $model->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2513
    column => [$model->column($table2)],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2514
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2515
);
2516
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2517
          {"$table2.$key1" => 1, "$table2.$key3" => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2518

            
2519
$result = $model->select(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2520
    column => [$model->column($table2 => [$key1, $key3])],
2521
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2522
);
2523
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2524
          {"$table2.$key1" => 1, "$table2.$key3" => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2525

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2526
test 'separator';
2527
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
2528
$dbi->user_table_info($user_table_info);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2529
eval { $dbi->execute("drop table $table1") };
2530
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2531
$dbi->execute($create_table1);
2532
$dbi->execute($create_table2);
test cleanup
Yuki Kimoto authored on 2011-08-10
2533

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2534
$dbi->create_model(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2535
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2536
    join => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2537
       "left outer join $table2 on $table1.$key1 = $table2.$key1"
cleanup test
Yuki Kimoto authored on 2011-08-10
2538
    ],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2539
    primary_key => [$key1],
cleanup test
Yuki Kimoto authored on 2011-08-10
2540
);
2541
$model2 = $dbi->create_model(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2542
    table => $table2,
cleanup test
Yuki Kimoto authored on 2011-08-10
2543
);
2544
$dbi->setup_model;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2545
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2546
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
2547
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2548
$result = $model->select(
2549
    column => [
2550
        $model->mycolumn,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2551
        {$table2 => [$key1, $key3]}
cleanup test
Yuki Kimoto authored on 2011-08-10
2552
    ],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2553
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2554
);
2555
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2556
          {$key1 => 1, $key2 => 2, "$table2.$key1" => 1, "$table2.$key3" => 3});
2557
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2558

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2559
$dbi->separator('__');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2560
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2561
$result = $model->select(
2562
    column => [
2563
        $model->mycolumn,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2564
        {$table2 => [$key1, $key3]}
cleanup test
Yuki Kimoto authored on 2011-08-10
2565
    ],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2566
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2567
);
2568
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2569
          {$key1 => 1, $key2 => 2, "${table2}__$key1" => 1, "${table2}__$key3" => 3});
2570
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2571

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2572
$dbi->separator('-');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2573
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2574
$result = $model->select(
2575
    column => [
2576
        $model->mycolumn,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2577
        {$table2 => [$key1, $key3]}
cleanup test
Yuki Kimoto authored on 2011-08-10
2578
    ],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2579
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2580
);
2581
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2582
          {$key1 => 1, $key2 => 2, "$table2-$key1" => 1, "$table2-$key3" => 3});
2583
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2584

            
2585

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2594
$dbi->create_model(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2595
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2596
    join => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2597
       "left outer join $table2 on $table1.$key1 = $table2.$key1"
cleanup test
Yuki Kimoto authored on 2011-08-10
2598
    ],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2599
    primary_key => [$key1],
cleanup test
Yuki Kimoto authored on 2011-08-10
2600
);
2601
$dbi->setup_model;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2602
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2603
$model = $dbi->model($table1);
2604
$result = $model->select(column => $key1);
2605
$result->filter($key1 => sub { $_[0] * 2 });
2606
is_deeply($result->one, {$key1 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
2607

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2608
test 'available_datetype';
2609
$dbi = DBIx::Custom->connect;
2610
ok($dbi->can('available_datatype'));
test cleanup
Yuki Kimoto authored on 2011-08-10
2611

            
2612

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2613
test 'select prefix option';
2614
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2615
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2616
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2617
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2618
$rows = $dbi->select(prefix => "$key1,", column => $key2, table => $table1)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2619
is_deeply($rows, [{$key1 => 1, $key2 => 2}], "table");
test cleanup
Yuki Kimoto authored on 2011-08-10
2620

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

            
added tests
Yuki Kimoto authored on 2011-08-26
2622
test 'mapper';
2623
$dbi = DBIx::Custom->connect;
2624
$param = $dbi->mapper(param => {id => 1, author => 'Ken', price => 1900})->map(
2625
    id => "$table1.id",
2626
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2627
    price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
2628
);
2629
is_deeply($param, {"$table1.id" => 1, "$table1.author" => '%Ken%',
2630
  "$table1.price" => 1900});
2631

            
2632
$param = $dbi->mapper(param => {id => 0, author => 0, price => 0})->map(
2633
    id => "$table1.id",
2634
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2635
    price => ["$table1.price", sub { '%' . $_[0] . '%' },
2636
      {condition => sub { $_[0] eq 0 }}]
2637
);
2638
is_deeply($param, {"$table1.id" => 0, "$table1.author" => '%0%', "$table1.price" => '%0%'});
2639

            
2640
$param = $dbi->mapper(param => {id => '', author => '', price => ''})->map(
2641
    id => "$table1.id",
2642
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2643
    price => ["$table1.price", sub { '%' . $_[0] . '%' },
2644
      {condition => sub { $_[0] eq 1 }}]
2645
);
2646
is_deeply($param, {});
2647

            
2648
$param = $dbi->mapper(param => {id => undef, author => undef, price => undef})->map(
2649
    id => "$table1.id",
2650
    price => ["$table1.price", {condition => 'exists'}]
2651
);
2652
is_deeply($param, {"$table1.price" => undef});
2653

            
2654
$param = $dbi->mapper(param => {price => 'a'})->map(
2655
    id => ["$table1.id", {condition => 'exists'}],
2656
    price => ["$table1.price", sub { '%' . $_[0] }, {condition => 'exists'}]
2657
);
2658
is_deeply($param, {"$table1.price" => '%a'});
2659

            
2660
$param = $dbi->mapper(param => {price => 'a'}, condition => 'exists')->map(
2661
    id => ["$table1.id"],
2662
    price => ["$table1.price", sub { '%' . $_[0] }]
2663
);
2664
is_deeply($param, {"$table1.price" => '%a'});
2665

            
- added {KEY => sub { VALUE ...
Yuki Kimoto authored on 2011-09-02
2666
$param = $dbi->mapper(param => {price => 'a'})->map(
2667
    price => sub { '%' . $_[0] }
2668
);
2669
is_deeply($param, {price => '%a'});
2670

            
- added EXPERIMENTAL pass at...
Yuki Kimoto authored on 2011-09-02
2671
eval { $dbi->execute("drop table $table1") };
2672
$dbi->execute($create_table1);
2673
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2674
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
2675

            
2676
$where = $dbi->where;
2677
$where->clause(['and', ":${key1}{=}"]);
2678
$param = $dbi->mapper(param => {$key1 => undef}, condition => 'defined')->map;
2679
$where->param($param);
2680
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
2681
$row = $result->all;
2682
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
2683

            
2684
$where = $dbi->where;
2685
$where->clause(['or', ":${key1}{=}", ":${key1}{=}"]);
2686
$param = $dbi->mapper(param => {$key1 => [undef, undef]}, condition => 'exists')->map;
2687
$result = $dbi->execute("select * from $table1 $where", {$key1 => [1, 0]});
2688
$row = $result->all;
2689
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
2690
$result = $dbi->execute("select * from $table1 $where", {$key1 => [0, 1]});
2691
$row = $result->all;
2692
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
2693

            
2694
$where = $dbi->where;
2695
$where->clause(['and', ":${key1}{=}"]);
2696
$param = $dbi->mapper(param => {$key1 => [undef, undef]}, condition => 'defined')->map;
2697
$where->param($param);
2698
$result = $dbi->execute("select * from $table1 $where", {$key1 => [1, 0]});
2699
$row = $result->all;
2700
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
2701
$result = $dbi->execute("select * from $table1 $where", {$key1 => [0, 1]});
2702
$row = $result->all;
2703
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
2704

            
2705
$where = $dbi->where;
2706
$where->clause(['and', ":${key1}{=}"]);
2707
$param = $dbi->mapper(param => {$key1 => 0}, condition => 'length')
2708
  ->pass([$key1, $key2])->map;
2709
$where->param($param);
2710
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
2711
$row = $result->all;
2712
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
2713

            
2714
$where = $dbi->where;
2715
$where->clause(['and', ":${key1}{=}"]);
2716
$param = $dbi->mapper(param => {$key1 => ''}, condition => 'length')->map;
2717
$where->param($param);
2718
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
2719
$row = $result->all;
2720
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
2721

            
2722
$where = $dbi->where;
2723
$where->clause(['and', ":${key1}{=}"]);
2724
$param = $dbi->mapper(param => {$key1 => 5}, condition => sub { ($_[0] || '') eq 5 })
2725
  ->pass([$key1, $key2])->map;
2726
$where->param($param);
2727
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
2728
$row = $result->all;
2729
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
2730

            
2731
$where = $dbi->where;
2732
$where->clause(['and', ":${key1}{=}"]);
2733
$param = $dbi->mapper(param => {$key1 => 7}, condition => sub { ($_[0] || '') eq 5 })->map;
2734
$where->param($param);
2735
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
2736
$row = $result->all;
2737
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
2738

            
2739
$where = $dbi->where;
2740
$param = $dbi->mapper(param => {id => 1, author => 'Ken', price => 1900})->map(
2741
    id => "$table1.id",
2742
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2743
    price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
2744
);
2745
$where->param($param);
2746
is_deeply($where->param, {"$table1.id" => 1, "$table1.author" => '%Ken%',
2747
  "$table1.price" => 1900});
2748

            
2749
$where = $dbi->where;
2750
$param = $dbi->mapper(param => {id => 0, author => 0, price => 0})->map(
2751
    id => "$table1.id",
2752
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2753
    price => ["$table1.price", sub { '%' . $_[0] . '%' }, {condition => sub { $_[0] eq 0 }}]
2754
);
2755
$where->param($param);
2756
is_deeply($where->param, {"$table1.id" => 0, "$table1.author" => '%0%', "$table1.price" => '%0%'});
2757

            
2758
$where = $dbi->where;
2759
$param = $dbi->mapper(param => {id => '', author => '', price => ''})->map(
2760
    id => "$table1.id",
2761
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2762
    price => ["$table1.price", sub { '%' . $_[0] . '%' }, {condition => sub { $_[0] eq 1 }}]
2763
);
2764
$where->param($param);
2765
is_deeply($where->param, {});
2766

            
2767
$where = $dbi->where;
2768
$param = $dbi->mapper(param => {id => undef, author => undef, price => undef}, condition => 'exists')->map(
2769
    id => "$table1.id",
2770
    price => ["$table1.price", {condition => 'exists'}]
2771
);
2772
is_deeply($param, {"$table1.id"  => undef,"$table1.price" => undef});
2773

            
2774
$where = $dbi->where;
2775
$param = $dbi->mapper(param => {price => 'a'})->map(
2776
    id => ["$table1.id", {condition => 'exists'}],
2777
    price => ["$table1.price", sub { '%' . $_[0] }, {condition => 'exists'}]
2778
);
2779
is_deeply($param, {"$table1.price" => '%a'});
2780

            
2781
$where = $dbi->where;
2782
$param = $dbi->mapper(param => {id => [1, 2], author => 'Ken', price => 1900})->map(
2783
    id => "$table1.id",
2784
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2785
    price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
2786
);
2787
is_deeply($param, {"$table1.id" => [1, 2], "$table1.author" => '%Ken%',
2788
  "$table1.price" => 1900});
2789

            
2790
$where = $dbi->where;
2791
$param = $dbi->mapper(param => {id => ['', ''], author => 'Ken', price => 1900}, condition => 'length')->map(
2792
    id => "$table1.id",
2793
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2794
    price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
2795
);
2796
is_deeply($param, {"$table1.id" => [$dbi->not_exists, $dbi->not_exists], "$table1.author" => '%Ken%',
2797
  "$table1.price" => 1900});
2798

            
2799
$where = $dbi->where;
2800
$param = $dbi->mapper(param => {id => ['', ''], author => 'Ken', price => 1900})->map(
2801
    id => ["$table1.id", {condition => 'length'}],
2802
    author => ["$table1.author", sub { '%' . $_[0] . '%' }, {condition => 'defined'}],
2803
    price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
2804
);
2805
is_deeply($param, {"$table1.id" => [$dbi->not_exists, $dbi->not_exists], "$table1.author" => '%Ken%',
2806
  "$table1.price" => 1900});
2807

            
2808
$where = $dbi->where;
2809
$param = $dbi->mapper(param => {id => 'a', author => 'b', price => 'c'}, pass => [qw/id author/])
2810
  ->map(price => 'book.price');
2811
is_deeply($param, {id => 'a', author => 'b', 'book.price' => 'c'});
2812

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2813
test 'map_param';
2814
$dbi = DBIx::Custom->connect;
2815
$param = $dbi->map_param(
2816
    {id => 1, author => 'Ken', price => 1900},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2817
    id => "$table1.id",
2818
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2819
    price => ["$table1.price", {if => sub { $_[0] eq 1900 }}]
cleanup test
Yuki Kimoto authored on 2011-08-10
2820
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2821
is_deeply($param, {"$table1.id" => 1, "$table1.author" => '%Ken%',
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2822
  "$table1.price" => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
2823

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2824
$param = $dbi->map_param(
2825
    {id => 0, author => 0, price => 0},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2826
    id => "$table1.id",
2827
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2828
    price => ["$table1.price", sub { '%' . $_[0] . '%' },
cleanup test
Yuki Kimoto authored on 2011-08-10
2829
      {if => sub { $_[0] eq 0 }}]
2830
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2831
is_deeply($param, {"$table1.id" => 0, "$table1.author" => '%0%', "$table1.price" => '%0%'});
test cleanup
Yuki Kimoto authored on 2011-08-10
2832

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2833
$param = $dbi->map_param(
2834
    {id => '', author => '', price => ''},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2835
    id => "$table1.id",
2836
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2837
    price => ["$table1.price", sub { '%' . $_[0] . '%' },
cleanup test
Yuki Kimoto authored on 2011-08-10
2838
      {if => sub { $_[0] eq 1 }}]
2839
);
2840
is_deeply($param, {});
test cleanup
Yuki Kimoto authored on 2011-08-10
2841

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2842
$param = $dbi->map_param(
2843
    {id => undef, author => undef, price => undef},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2844
    id => "$table1.id",
2845
    price => ["$table1.price", {if => 'exists'}]
cleanup test
Yuki Kimoto authored on 2011-08-10
2846
);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2847
is_deeply($param, {"$table1.price" => undef});
test cleanup
Yuki Kimoto authored on 2011-08-10
2848

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2849
$param = $dbi->map_param(
2850
    {price => 'a'},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2851
    id => ["$table1.id", {if => 'exists'}],
2852
    price => ["$table1.price", sub { '%' . $_[0] }, {if => 'exists'}]
cleanup test
Yuki Kimoto authored on 2011-08-10
2853
);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2854
is_deeply($param, {"$table1.price" => '%a'});
test cleanup
Yuki Kimoto authored on 2011-08-10
2855

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2856
test 'order';
2857
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2858
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2859
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2860
$dbi->insert({$key1 => 1, $key2 => 1}, table => $table1);
2861
$dbi->insert({$key1 => 1, $key2 => 3}, table => $table1);
2862
$dbi->insert({$key1 => 2, $key2 => 2}, table => $table1);
2863
$dbi->insert({$key1 => 2, $key2 => 4}, table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2864
my $order = $dbi->order;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2865
$order->prepend($key1, "$key2 desc");
2866
$result = $dbi->select(table => $table1, append => $order);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2867
is_deeply($result->all, [{$key1 => 1, $key2 => 3}, {$key1 => 1, $key2 => 1},
2868
  {$key1 => 2, $key2 => 4}, {$key1 => 2, $key2 => 2}]);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2869
$order->prepend("$key1 desc");
2870
$result = $dbi->select(table => $table1, append => $order);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2871
is_deeply($result->all, [{$key1 => 2, $key2 => 4}, {$key1 => 2, $key2 => 2},
2872
  {$key1 => 1, $key2 => 3}, {$key1 => 1, $key2 => 1}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2873

            
2874
$order = $dbi->order;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2875
$order->prepend(["$table1-$key1"], ["$table1-$key2", 'desc']);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2876
$result = $dbi->select(table => $table1,
2877
  column => [[$key1 => "$table1-$key1"], [$key2 => "$table1-$key2"]],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2878
  append => $order);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2879
is_deeply($result->all, [{"$table1-$key1" => 1, "$table1-$key2" => 3},
2880
  {"$table1-$key1" => 1, "$table1-$key2" => 1},
2881
  {"$table1-$key1" => 2, "$table1-$key2" => 4},
2882
  {"$table1-$key1" => 2, "$table1-$key2" => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2883

            
2884
test 'tag_parse';
2885
$dbi = DBIx::Custom->connect;
2886
$dbi->tag_parse(0);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2887
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2888
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2889
$dbi->insert({$key1 => 1, $key2 => 1}, table => $table1);
2890
eval {$dbi->execute("select * from $table1 where {= $key1}", {$key1 => 1})};
test cleanup
Yuki Kimoto authored on 2011-08-10
2891
ok($@);
2892

            
2893
test 'last_sql';
2894
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2895
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2896
$dbi->execute($create_table1);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2897
$dbi->execute("select * from $table1");
cleanup test
Yuki Kimoto authored on 2011-08-15
2898
is($dbi->last_sql, "select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
2899

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

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

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

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

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

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2931
$source = "select * from $table1 where :$table1.${key1}{=} and :$table1.${key2}{=}";
test cleanup
Yuki Kimoto authored on 2011-08-10
2932
$result = $dbi->execute(
2933
    $source,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2934
    param => {"$table1.$key1" => 1, "$table1.$key2" => 1},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2935
    filter => {"$table1.$key2" => sub { $_[0] * 2 }}
test cleanup
Yuki Kimoto authored on 2011-08-10
2936
);
2937
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2938
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2939

            
2940
test 'high perfomance way';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2941
$dbi->execute("drop table $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
2942
$dbi->execute($create_table1_highperformance);
2943
$rows = [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2944
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
2945
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
2946
];
2947
{
2948
    my $query;
2949
    foreach my $row (@$rows) {
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2950
      $query ||= $dbi->insert($row, table => $table1, query => 1);
2951
      $dbi->execute($query, $row, filter => {$key7 => sub { $_[0] * 2 }});
test cleanup
Yuki Kimoto authored on 2011-08-10
2952
    }
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2953
    is_deeply($dbi->select(table => $table1)->all,
test cleanup
Yuki Kimoto authored on 2011-08-10
2954
      [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2955
          {$key7 => 2, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
2956
          {$key7 => 2, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
2957
      ]
2958
    );
2959
}
2960

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

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
2983
$dbi->execute("drop table $table1");
2984
$dbi->execute($create_table1_highperformance);
2985
$rows = [
2986
    {$key7 => 10, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 5},
2987
    {$key7 => 11, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6},
2988
];
2989
{
2990
    $model = $dbi->create_model(table => $table1, primary_key => $key1);
2991
    my $query;
2992
    foreach my $row (@$rows) {
2993
      $query ||= $model->insert($row, id => 1, query => 1);
2994
      $model->execute($query, $row, id => 1, filter => {$key7 => sub { $_[0] * 2 }});
2995
    }
2996
    is_deeply($dbi->select(table => $table1, append => 'order by key2')->all,
2997
      [
2998
          {$key7 => 20, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 5, $key1 => 1},
2999
          {$key7 => 22, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 1},
3000
      ]
3001
    );
3002
}
3003

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3004
test 'result';
3005
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3006
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
3007
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3008
$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1);
3009
$dbi->insert({$key1 => 3, $key2 => 4}, table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
3010

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3011
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
3012
@rows = ();
3013
while (my $row = $result->fetch) {
3014
    push @rows, [@$row];
3015
}
3016
is_deeply(\@rows, [[1, 2], [3, 4]]);
3017

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3018
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
3019
@rows = ();
3020
while (my $row = $result->fetch_hash) {
3021
    push @rows, {%$row};
3022
}
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3023
is_deeply(\@rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
3024

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

            
3031
test 'fetch_all';
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3032
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
3033
$rows = $result->fetch_all;
3034
is_deeply($rows, [[1, 2], [3, 4]]);
3035

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

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

            
3044
$rows = $result->fetch_all;
3045
is_deeply($rows, [[3, 2], [9, 4]], "array");
3046

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

            
3053
test "query_builder";
3054
$datas = [
3055
    # Basic tests
3056
    {   name            => 'placeholder basic',
3057
        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
cleanup test
Yuki Kimoto authored on 2011-08-15
3058
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?",
test cleanup
Yuki Kimoto authored on 2011-08-10
3059
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
3060
    },
3061
    {
3062
        name            => 'placeholder in',
cleanup test
Yuki Kimoto authored on 2011-08-15
3063
        source            => "{in k1 3}",
3064
        sql_expected    => "k1 in (?, ?, ?)",
test cleanup
Yuki Kimoto authored on 2011-08-10
3065
        columns_expected   => [qw/k1 k1 k1/]
3066
    },
3067
    
3068
    # Table name
3069
    {
3070
        name            => 'placeholder with table name',
3071
        source            => "{= a.k1} {= a.k2}",
cleanup test
Yuki Kimoto authored on 2011-08-15
3072
        sql_expected    => "a.k1 = ? a.k2 = ?",
test cleanup
Yuki Kimoto authored on 2011-08-10
3073
        columns_expected  => [qw/a.k1 a.k2/]
3074
    },
3075
    {   
3076
        name            => 'placeholder in with table name',
3077
        source            => "{in a.k1 2} {in b.k2 2}",
cleanup test
Yuki Kimoto authored on 2011-08-15
3078
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?)",
test cleanup
Yuki Kimoto authored on 2011-08-10
3079
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
3080
    },
3081
    {
3082
        name            => 'not contain tag',
3083
        source            => "aaa",
cleanup test
Yuki Kimoto authored on 2011-08-15
3084
        sql_expected    => "aaa",
test cleanup
Yuki Kimoto authored on 2011-08-10
3085
        columns_expected  => [],
3086
    }
3087
];
3088

            
3089
for (my $i = 0; $i < @$datas; $i++) {
3090
    my $data = $datas->[$i];
cleanup
Yuki Kimoto authored on 2011-08-13
3091
    my $dbi = DBIx::Custom->new;
3092
    my $builder = $dbi->query_builder;
test cleanup
Yuki Kimoto authored on 2011-08-10
3093
    my $query = $builder->build_query($data->{source});
3094
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
3095
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
3096
}
3097

            
cleanup
Yuki Kimoto authored on 2011-08-13
3098
$dbi = DBIx::Custom->new;
3099
$builder = $dbi->query_builder;
3100
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
3101
    p => sub {
3102
        my @args = @_;
3103
        
3104
        my $expand    = "? $args[0] $args[1]";
3105
        my $columns = [2];
3106
        return [$expand, $columns];
3107
    }
3108
);
3109

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
3120
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
3121
    q => 'string'
3122
});
3123

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
3127
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
3128
   r => sub {} 
3129
});
3130

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
3134
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
3135
   s => sub { return ["a", ""]} 
3136
});
3137

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
3141
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
3142
    t => sub {return ["a", []]}
3143
);
3144

            
3145

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

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

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

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

            
3161
test 'variouse source';
3162
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d";
3163
$query = $builder->build_query($source);
3164
is($query->sql, 'a b = ? c { } { = ? } = ? d', "basic : 1");
3165

            
3166
$source = "abc";
3167
$query = $builder->build_query($source);
3168
is($query->sql, 'abc', "basic : 2");
3169

            
3170
$source = "{= a}";
3171
$query = $builder->build_query($source);
3172
is($query->sql, 'a = ?', "only tag");
3173

            
3174
$source = "000";
3175
$query = $builder->build_query($source);
3176
is($query->sql, '000', "contain 0 value");
3177

            
3178
$source = "a {= b} }";
3179
eval{$builder->build_query($source)};
3180
like($@, qr/unexpected "}"/, "error : 1");
3181

            
3182
$source = "a {= {}";
3183
eval{$builder->build_query($source)};
3184
like($@, qr/unexpected "{"/, "error : 2");
3185

            
3186
test 'select() sqlfilter option';
3187
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3188
$dbi->user_table_info($user_table_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3189
eval { $dbi->execute("drop table $table1") };
3190
$dbi->execute($create_table1);
3191
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3192
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
3193
$rows = $dbi->select(
3194
    table => $table1,
3195
    column => $key1,
3196
    sqlfilter => sub {
3197
        my $sql = shift;
test cleanup
Yuki Kimoto authored on 2011-08-15
3198
        $sql = "select * from ( $sql ) t where $key1 = 1";
cleanup test
Yuki Kimoto authored on 2011-08-15
3199
        return $sql;
3200
    }
3201
)->all;
3202
is_deeply($rows, [{$key1 => 1}]);
3203

            
3204
test 'dbi method from model';
3205
$dbi = MyDBI9->connect;
3206
eval { $dbi->execute("drop table $table1") };
3207
$dbi->execute($create_table1);
3208
$dbi->setup_model;
3209
$model = $dbi->model($table1);
3210
eval{$model->execute("select * from $table1")};
3211
ok(!$@);
3212

            
3213
test 'column table option';
3214
$dbi = MyDBI9->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3215
$dbi->user_table_info($user_table_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3216
eval { $dbi->execute("drop table $table1") };
3217
$dbi->execute($create_table1);
3218
eval { $dbi->execute("drop table $table2") };
3219
$dbi->execute($create_table2);
3220
$dbi->setup_model;
3221
$dbi->execute("insert into $table1 ($key1, $key2) values (1, 2)");
3222
$dbi->execute("insert into $table2 ($key1, $key3) values (1, 4)");
3223
$model = $dbi->model($table1);
3224
$result = $model->select(
3225
    column => [
3226
        $model->column($table2, {alias => $table2_alias})
3227
    ],
3228
    where => {"$table2_alias.$key3" => 4}
3229
);
3230
is_deeply($result->one, 
3231
          {"$table2_alias.$key1" => 1, "$table2_alias.$key3" => 4});
3232

            
3233
$dbi->separator('__');
3234
$result = $model->select(
3235
    column => [
3236
        $model->column($table2, {alias => $table2_alias})
3237
    ],
3238
    where => {"$table2_alias.$key3" => 4}
3239
);
3240
is_deeply($result->one, 
3241
          {"${table2_alias}__$key1" => 1, "${table2_alias}__$key3" => 4});
3242

            
3243
$dbi->separator('-');
3244
$result = $model->select(
3245
    column => [
3246
        $model->column($table2, {alias => $table2_alias})
3247
    ],
3248
    where => {"$table2_alias.$key3" => 4}
3249
);
3250
is_deeply($result->one, 
3251
          {"$table2_alias-$key1" => 1, "$table2_alias-$key3" => 4});
3252

            
3253
test 'create_model';
3254
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3255
$dbi->user_table_info($user_table_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3256
eval { $dbi->execute("drop table $table1") };
3257
eval { $dbi->execute("drop table $table2") };
3258
$dbi->execute($create_table1);
3259
$dbi->execute($create_table2);
3260

            
3261
$dbi->create_model(
3262
    table => $table1,
3263
    join => [
3264
       "left outer join $table2 on $table1.$key1 = $table2.$key1"
3265
    ],
3266
    primary_key => [$key1]
3267
);
3268
$model2 = $dbi->create_model(
3269
    table => $table2
3270
);
3271
$dbi->create_model(
3272
    table => $table3,
3273
    filter => [
3274
        $key1 => {in => sub { uc $_[0] }}
3275
    ]
3276
);
3277
$dbi->setup_model;
3278
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3279
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
3280
$model = $dbi->model($table1);
3281
$result = $model->select(
3282
    column => [$model->mycolumn, $model->column($table2)],
3283
    where => {"$table1.$key1" => 1}
3284
);
3285
is_deeply($result->one,
3286
          {$key1 => 1, $key2 => 2, "$table2.$key1" => 1, "$table2.$key3" => 3});
3287
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3});
3288

            
3289
test 'model method';
3290
$dbi = DBIx::Custom->connect;
3291
eval { $dbi->execute("drop table $table2") };
3292
$dbi->execute($create_table2);
3293
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
3294
$model = $dbi->create_model(
3295
    table => $table2
3296
);
3297
$model->method(foo => sub { shift->select(@_) });
3298
is_deeply($model->foo->one, {$key1 => 1, $key3 => 3});
3299

            
3300
test 'update_param';
3301
$dbi = DBIx::Custom->connect;
3302
eval { $dbi->execute("drop table $table1") };
3303
$dbi->execute($create_table1_2);
3304
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3305
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3306

            
3307
$param = {$key2 => 11};
3308
$update_param = $dbi->update_param($param);
3309
$sql = <<"EOS";
3310
update $table1 $update_param
3311
where $key1 = 1
3312
EOS
3313
$dbi->execute($sql, param => $param);
3314
$result = $dbi->execute("select * from $table1 order by $key1", table => $table1);
3315
$rows   = $result->all;
3316
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
3317
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3318
                  "basic");
3319

            
3320

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

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

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

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

            
3359
            
3360
eval { $dbi->update_param({";" => 1}) };
3361
like($@, qr/not safety/);
3362

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3384
test 'Model class';
3385
$dbi = MyDBI1->connect;
3386
eval { $dbi->execute("drop table $table1") };
3387
$dbi->execute($create_table1);
3388
$model = $dbi->model($table1);
3389
$model->insert({$key1 => 'a', $key2 => 'b'});
3390
is_deeply($model->list->all, [{$key1 => 'a', $key2 => 'b'}], 'basic');
3391
eval { $dbi->execute("drop table $table2") };
3392
$dbi->execute($create_table2);
3393
$model = $dbi->model($table2);
3394
$model->insert({$key1 => 'a'});
3395
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'basic');
3396
is($dbi->models->{$table1}, $dbi->model($table1));
3397
is($dbi->models->{$table2}, $dbi->model($table2));
3398

            
3399
$dbi = MyDBI4->connect;
3400
eval { $dbi->execute("drop table $table1") };
3401
$dbi->execute($create_table1);
3402
$model = $dbi->model($table1);
3403
$model->insert({$key1 => 'a', $key2 => 'b'});
3404
is_deeply($model->list->all, [{$key1 => 'a', $key2 => 'b'}], 'basic');
3405
eval { $dbi->execute("drop table $table2") };
3406
$dbi->execute($create_table2);
3407
$model = $dbi->model($table2);
3408
$model->insert({$key1 => 'a'});
3409
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'basic');
3410

            
3411
$dbi = MyDBI5->connect;
3412
eval { $dbi->execute("drop table $table1") };
3413
eval { $dbi->execute("drop table $table2") };
3414
$dbi->execute($create_table1);
3415
$dbi->execute($create_table2);
3416
$model = $dbi->model($table2);
3417
$model->insert({$key1 => 'a'});
3418
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'include all model');
3419
$dbi->insert(table => $table1, param => {$key1 => 1});
3420
$model = $dbi->model($table1);
3421
is_deeply($model->list->all, [{$key1 => 1, $key2 => undef}], 'include all model');
3422

            
3423
test 'primary_key';
3424
$dbi = MyDBI1->connect;
3425
$model = $dbi->model($table1);
3426
$model->primary_key([$key1, $key2]);
3427
is_deeply($model->primary_key, [$key1, $key2]);
3428

            
3429
test 'columns';
3430
$dbi = MyDBI1->connect;
3431
$model = $dbi->model($table1);
3432
$model->columns([$key1, $key2]);
3433
is_deeply($model->columns, [$key1, $key2]);
cleanup test
Yuki Kimoto authored on 2011-08-10
3434

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3435
test 'setup_model';
3436
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3437
$dbi->user_table_info($user_table_info);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3438
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3439
eval { $dbi->execute("drop table $table2") };
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
3440

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3441
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-15
3442
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-10
3443
$dbi->setup_model;
cleanup test
Yuki Kimoto authored on 2011-08-15
3444
is_deeply([sort @{$dbi->model($table1)->columns}], [$key1, $key2]);
3445
is_deeply([sort @{$dbi->model($table2)->columns}], [$key1, $key3]);
cleanup test
Yuki Kimoto authored on 2011-08-10
3446

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3447
test 'each_column';
3448
$dbi = DBIx::Custom->connect;
3449
eval { $dbi->execute("drop table ${q}table$p") };
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3450
eval { $dbi->execute("drop table $table1") };
3451
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3452
eval { $dbi->execute("drop table $table3") };
3453
$dbi->execute($create_table1_type);
cleanup test
Yuki Kimoto authored on 2011-08-10
3454
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-15
3455

            
3456
$infos = [];
3457
$dbi->each_column(sub {
3458
    my ($self, $table, $column, $cinfo) = @_;
3459
    
3460
    if ($table =~ /^table\d/i) {
3461
         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
3462
         push @$infos, $info;
3463
    }
3464
});
3465
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3466
is_deeply($infos, 
3467
    [
3468
        [$table1, $key1, $key1],
3469
        [$table1, $key2, $key2],
3470
        [$table2, $key1, $key1],
3471
        [$table2, $key3, $key3]
3472
    ]
3473
    
cleanup test
Yuki Kimoto authored on 2011-08-10
3474
);
cleanup test
Yuki Kimoto authored on 2011-08-16
3475

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3476
test 'each_table';
3477
$dbi = DBIx::Custom->connect;
3478
eval { $dbi->execute("drop table $table1") };
3479
eval { $dbi->execute("drop table $table2") };
3480
$dbi->execute($create_table2);
3481
$dbi->execute($create_table1_type);
cleanup test
Yuki Kimoto authored on 2011-08-10
3482

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3483
$infos = [];
3484
$dbi->each_table(sub {
3485
    my ($self, $table, $table_info) = @_;
3486
    
3487
    if ($table =~ /^table\d/i) {
3488
         my $info = [$table, $table_info->{TABLE_NAME}];
3489
         push @$infos, $info;
3490
    }
3491
});
3492
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3493
is_deeply($infos, 
3494
    [
3495
        [$table1, $table1],
3496
        [$table2, $table2],
3497
    ]
cleanup test
Yuki Kimoto authored on 2011-08-10
3498
);
3499

            
cleanup test
Yuki Kimoto authored on 2011-08-16
3500
$dbi = DBIx::Custom->connect;
3501
eval { $dbi->execute("drop table $table1") };
3502
eval { $dbi->execute("drop table $table2") };
3503
$dbi->execute($create_table2);
3504
$dbi->execute($create_table1_type);
3505

            
3506
$infos = [];
3507
$dbi->user_table_info($user_table_info);
3508
$dbi->each_table(sub {
3509
    my ($self, $table, $table_info) = @_;
3510
    
3511
    if ($table =~ /^table\d/i) {
3512
         my $info = [$table, $table_info->{TABLE_NAME}];
3513
         push @$infos, $info;
3514
    }
3515
});
3516
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3517
is_deeply($infos, 
3518
    [
3519
        [$table1, $table1],
3520
        [$table2, $table2],
3521
        [$table3, $table3],
3522
    ]
3523
);
3524

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3525
test 'type_rule into';
3526
eval { $dbi->execute("drop table $table1") };
3527
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3528
$user_column_info = $dbi->get_column_info(exclude_table => $dbi->exclude_table);
3529

            
3530

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3531
$dbi = DBIx::Custom->connect;
3532
eval { $dbi->execute("drop table $table1") };
3533
$dbi->execute($create_table1_type);
3534

            
cleanup
Yuki Kimoto authored on 2011-08-16
3535
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3536
$dbi->type_rule(
3537
    into1 => {
3538
        $date_typename => sub { '2010-' . $_[0] }
3539
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3540
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3541
$dbi->insert({$key1 => '01-01'}, table => $table1);
3542
$result = $dbi->select(table => $table1);
3543
like($result->one->{$key1}, qr/^2010-01-01/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3544

            
3545
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3546
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3547
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3548
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3549
$dbi->type_rule(
3550
    into1 => [
3551
         [$date_typename, $datetime_typename] => sub {
3552
            my $value = shift;
3553
            $value =~ s/02/03/g;
3554
            return $value;
3555
         }
3556
    ]
3557
);
3558
$dbi->insert({$key1 => '2010-01-02', $key2 => '2010-01-01 01:01:02'}, table => $table1);
3559
$result = $dbi->select(table => $table1);
3560
$row = $result->one;
3561
like($row->{$key1}, qr/^2010-01-03/);
3562
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3563

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

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

            
3609
$dbi = DBIx::Custom->connect;
3610
eval { $dbi->execute("drop table $table1") };
3611
$dbi->execute($create_table1_type);
3612
$dbi->register_filter(convert => sub {
3613
    my $value = shift || '';
3614
    $value =~ s/02/03/;
3615
    return $value;
3616
});
cleanup
Yuki Kimoto authored on 2011-08-16
3617
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3618
$dbi->type_rule(
3619
    from1 => {
3620
        $date_datatype => 'convert',
3621
    },
3622
    into1 => {
3623
        $date_typename => 'convert',
3624
    }
3625
);
3626
$dbi->insert({$key1 => '2010-02-02'}, table => $table1);
3627
$result = $dbi->select(table => $table1);
3628
like($result->fetch->[0], qr/^2010-03-03/);
3629

            
3630
test 'type_rule and filter order';
3631
$dbi = DBIx::Custom->connect;
3632
eval { $dbi->execute("drop table $table1") };
3633
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3634
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3635
$dbi->type_rule(
3636
    into1 => {
3637
        $date_typename => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3638
    },
3639
    into2 => {
3640
        $date_typename => sub { my $v = shift || ''; $v =~ s/5/6/; return $v }
3641
    },
3642
    from1 => {
3643
        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/7/; return $v }
3644
    },
3645
    from2 => {
3646
        $date_datatype => sub { my $v = shift || ''; $v =~ s/7/8/; return $v }
3647
    }
3648
);
3649
$dbi->insert({$key1 => '2010-01-03'}, 
3650
  table => $table1, filter => {$key1 => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }});
3651
$result = $dbi->select(table => $table1);
3652
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/8/9/; return $v });
3653
like($result->fetch_first->[0], qr/^2010-01-09/);
3654

            
3655

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

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

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

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

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

            
3747
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3748
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3749
$dbi->execute($create_table1_type);
3750
$dbi->register_filter(ppp => sub { my $v = shift || ''; $v =~ s/3/4/; return $v });
cleanup
Yuki Kimoto authored on 2011-08-16
3751
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3752
$dbi->type_rule(
3753
    into1 => {
3754
        $date_typename => 'ppp'
3755
    }
3756
);
3757
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3758
$result = $dbi->select(table => $table1);
3759
like($result->one->{$key1}, qr/^2010-01-04/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3760

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3761
eval{$dbi->type_rule(
3762
    into1 => {
3763
        $date_typename => 'pp'
3764
    }
3765
)};
3766
like($@, qr/not registered/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3767

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3768
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3769
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3770
$dbi->execute($create_table1_type);
cleanup test
Yuki Kimoto authored on 2011-08-10
3771
eval {
cleanup test
Yuki Kimoto authored on 2011-08-15
3772
    $dbi->type_rule(
3773
        from1 => {
3774
            Date => sub { $_[0] * 2 },
3775
        }
cleanup test
Yuki Kimoto authored on 2011-08-10
3776
    );
3777
};
cleanup test
Yuki Kimoto authored on 2011-08-15
3778
like($@, qr/lower/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3779

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3780
eval {
3781
    $dbi->type_rule(
3782
        into1 => {
3783
            Date => sub { $_[0] * 2 },
3784
        }
3785
    );
3786
};
3787
like($@, qr/lower/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3788

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

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

            
3827
$result = $dbi->select(table => $table1);
3828
$result->type_rule(
3829
    from1 => {
3830
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3831
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3832
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3833
$row = $result->one;
3834
like($row->{$key1}, qr/2010-01-05/);
3835
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3836

            
3837
$result = $dbi->select(table => $table1);
3838
$result->type_rule(
3839
    from1 => {
3840
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3841
    }
3842
);
3843
$row = $result->one;
3844
like($row->{$key1}, qr/2010-01-05/);
3845
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3846

            
3847
$result = $dbi->select(table => $table1);
3848
$result->type_rule(
3849
    from1 => [$date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }]
3850
);
3851
$row = $result->one;
3852
like($row->{$key1}, qr/2010-01-05/);
3853
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3854

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

            
3864
$result = $dbi->select(table => $table1);
3865
$result->type_rule(
3866
    from1 => [$date_datatype => undef]
3867
);
3868
$row = $result->one;
3869
like($row->{$key1}, qr/^2010-01-03/);
3870
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3871

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3900
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3901
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3902
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3903
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3904
$dbi->type_rule(
3905
    into1 => {
3906
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3907
    },
3908
    into2 => {
3909
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3910
    },
3911
    from1 => {
3912
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/6/; return $v }
3913
    },
3914
    from2 => {
3915
        $date_datatype => sub { my $v = shift || ''; $v =~ s/(3|6)/7/; return $v }
3916
    }
3917
);
3918
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule_off => 1);
3919
$result = $dbi->select(table => $table1);
3920
like($result->type_rule_off->fetch_first->[0], qr/^2010-01-03/);
3921
$result = $dbi->select(table => $table1);
3922
like($result->type_rule_on->fetch_first->[0], qr/^2010-01-07/);
test cleanup
Yuki Kimoto authored on 2011-08-10
3923

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

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

            
added memory leak check test
Yuki Kimoto authored on 2011-08-15
3972
test 'join';
3973
$dbi = DBIx::Custom->connect;
3974
eval { $dbi->execute("drop table $table1") };
3975
$dbi->execute($create_table1);
3976
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3977
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
3978
eval { $dbi->execute("drop table $table2") };
3979
$dbi->execute($create_table2);
3980
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3981
eval { $dbi->execute("drop table $table3") };
3982
$dbi->execute("create table $table3 ($key3 int, $key4 int)");
3983
$dbi->insert(table => $table3, param => {$key3 => 5, $key4 => 4});
3984
$rows = $dbi->select(
3985
    table => $table1,
3986
    column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
3987
    where   => {"$table1.$key2" => 2},
3988
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1"]
3989
)->all;
3990
is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}]);
3991

            
3992
$rows = $dbi->select(
3993
    table => $table1,
3994
    where   => {$key1 => 1},
3995
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1"]
3996
)->all;
3997
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
3998

            
3999
eval {
4000
    $rows = $dbi->select(
4001
        table => $table1,
4002
        column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
4003
        where   => {"$table1.$key2" => 2},
4004
        join  => {"$table1.$key1" => "$table2.$key1"}
4005
    );
4006
};
4007
like ($@, qr/array/);
4008

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

            
4017
$rows = $dbi->select(
4018
    column => "$table3.$key4 as ${table3}__$key4",
4019
    table => $table1,
4020
    where   => {"$table1.$key1" => 1},
4021
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
4022
              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
4023
)->all;
4024
is_deeply($rows, [{"${table3}__$key4" => 4}]);
4025

            
4026
$rows = $dbi->select(
4027
    column => "$table1.$key1 as ${table1}__$key1",
4028
    table => $table1,
4029
    where   => {"$table3.$key4" => 4},
4030
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
4031
              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
4032
)->all;
4033
is_deeply($rows, [{"${table1}__$key1" => 1}]);
4034

            
4035
$dbi = DBIx::Custom->connect;
4036
eval { $dbi->execute("drop table $table1") };
4037
$dbi->execute($create_table1);
4038
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
4039
eval { $dbi->execute("drop table $table2") };
4040
$dbi->execute($create_table2);
4041
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
4042
$rows = $dbi->select(
4043
    table => $table1,
4044
    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",
4045
    where   => {"$table1.$key2" => 2},
4046
    join  => ["left outer join ${q}$table2$p on ${q}$table1$p.${q}$key1$p = ${q}$table2$p.${q}$key1$p"],
4047
)->all;
4048
is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}],
4049
          'quote');
4050

            
4051

            
4052
$dbi = DBIx::Custom->connect;
4053
eval { $dbi->execute("drop table $table1") };
4054
$dbi->execute($create_table1);
4055
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
4056
$sql = <<"EOS";
4057
left outer join (
4058
  select * from $table1 t1
4059
  where t1.$key2 = (
4060
    select max(t2.$key2) from $table1 t2
4061
    where t1.$key1 = t2.$key1
4062
  )
4063
) $table3 on $table1.$key1 = $table3.$key1
4064
EOS
4065
$join = [$sql];
4066
$rows = $dbi->select(
4067
    table => $table1,
4068
    column => "$table3.$key1 as ${table3}__$key1",
4069
    join  => $join
4070
)->all;
4071
is_deeply($rows, [{"${table3}__$key1" => 1}]);
4072

            
4073
$dbi = DBIx::Custom->connect;
4074
eval { $dbi->execute("drop table $table1") };
4075
eval { $dbi->execute("drop table $table2") };
4076
$dbi->execute($create_table1);
4077
$dbi->execute($create_table2);
4078
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
4079
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4});
4080
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
4081
$result = $dbi->select(
4082
    table => $table1,
4083
    join => [
4084
        "left outer join $table2 on $table2.$key2 = '4' and $table1.$key1 = $table2.$key1"
4085
    ]
4086
);
4087
is_deeply($result->all, [{$key1 => 1, $key2 => 2}]);
4088
$result = $dbi->select(
4089
    table => $table1,
4090
    column => [{$table2 => [$key3]}],
4091
    join => [
4092
        "left outer join $table2 on $table2.$key3 = '4' and $table1.$key1 = $table2.$key1"
4093
    ]
4094
);
4095
is_deeply($result->all, [{"$table2.$key3" => 4}]);
4096
$result = $dbi->select(
4097
    table => $table1,
4098
    column => [{$table2 => [$key3]}],
4099
    join => [
4100
        "left outer join $table2 on $table1.$key1 = $table2.$key1 and $table2.$key3 = '4'"
4101
    ]
4102
);
4103
is_deeply($result->all, [{"$table2.$key3" => 4}]);
4104

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

            
4125
test 'columns';
4126
$dbi = MyDBI1->connect;
4127
$model = $dbi->model($table1);
4128

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
4129
test 'count';
4130
$dbi = DBIx::Custom->connect;
4131
eval { $dbi->execute("drop table $table1") };
4132
$dbi->execute($create_table1);
4133
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
4134
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 3});
4135
is($dbi->count(table => $table1), 2);
4136
is($dbi->count(table => $table1, where => {$key2 => 2}), 1);
4137
$model = $dbi->create_model(table => $table1);
4138
is($model->count, 2);
cleanup test
Yuki Kimoto authored on 2011-08-10
4139

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