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

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

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
529
$dbi->execute("delete from $table1");
530
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
531
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
test cleanup
Yuki Kimoto authored on 2011-08-10
532
$dbi->register_filter(twice => sub { $_[0] * 2 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
533
$dbi->update(table => $table1, param => {$key2 => 11}, where => {$key1 => 1},
534
              filter => {$key2 => sub { $_[0] * 2 }});
cleanup test
Yuki Kimoto authored on 2011-08-15
535
$result = $dbi->execute("select * from $table1 order by $key1");
test cleanup
Yuki Kimoto authored on 2011-08-10
536
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
537
is_deeply($rows, [{$key1 => 1, $key2 => 22, $key3 => 3, $key4 => 4, $key5 => 5},
538
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
539
                  "filter");
540

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

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

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

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

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

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

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

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

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

            
600
eval {$dbi->update_all(table => 'table', param => {';' => 2}) };
601
like($@, qr/safety/);
602

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

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

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

            
635
test 'update_all';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
636
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
637
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
638
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
639
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
test cleanup
Yuki Kimoto authored on 2011-08-10
640
$dbi->register_filter(twice => sub { $_[0] * 2 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
641
$dbi->update_all(table => $table1, param => {$key2 => 10}, filter => {$key2 => 'twice'});
cleanup test
Yuki Kimoto authored on 2011-08-15
642
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
643
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
644
is_deeply($rows, [{$key1 => 1, $key2 => 20, $key3 => 3, $key4 => 4, $key5 => 5},
645
                  {$key1 => 6, $key2 => 20, $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
646
                  "filter");
647

            
648

            
649
test 'delete';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
650
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
651
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
652
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
653
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
654
$dbi->delete(table => $table1, where => {$key1 => 1});
cleanup test
Yuki Kimoto authored on 2011-08-15
655
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
656
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
657
is_deeply($rows, [{$key1 => 3, $key2 => 4}], "basic");
test cleanup
Yuki Kimoto authored on 2011-08-10
658

            
cleanup test
Yuki Kimoto authored on 2011-08-15
659
$dbi->execute("delete from $table1");
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
660
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
661
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-10
662
$dbi->register_filter(twice => sub { $_[0] * 2 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
663
$dbi->delete(table => $table1, where => {$key2 => 1}, filter => {$key2 => 'twice'});
cleanup test
Yuki Kimoto authored on 2011-08-15
664
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
665
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
666
is_deeply($rows, [{$key1 => 3, $key2 => 4}], "filter");
test cleanup
Yuki Kimoto authored on 2011-08-10
667

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
670
$dbi->delete_all(table => $table1);
671
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
672
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
673
$dbi->delete(table => $table1, where => {$key1 => 1, $key2 => 2});
674
$rows = $dbi->select(table => $table1)->all;
675
is_deeply($rows, [{$key1 => 3, $key2 => 4}], "delete multi key");
test cleanup
Yuki Kimoto authored on 2011-08-10
676

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

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

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
691
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
692
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
693
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
694
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-10
695
$dbi->delete(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
696
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
697
    where => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
698
        ['and', "$key1 = :$key1", "$key2 = :$key2"],
699
        {ke1 => 1, $key2 => 2}
test cleanup
Yuki Kimoto authored on 2011-08-10
700
    ]
701
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
702
$result = $dbi->select(table => $table1);
703
is_deeply($result->all, [{$key1 => 3, $key2 => 4}], 'delete() where');
test cleanup
Yuki Kimoto authored on 2011-08-10
704

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
705
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
706
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
707
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
708
$dbi->delete(table => $table1, where => {$key1 => 1}, prefix => '    ');
cleanup test
Yuki Kimoto authored on 2011-08-15
709
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
710
$rows   = $result->all;
711
is_deeply($rows, [], "basic");
712

            
713
test 'delete error';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
714
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
715
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
716
eval{$dbi->delete(table => $table1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
717
like($@, qr/"where" must be specified/,
718
         "where key-value pairs not specified");
719

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
723
$dbi = undef;
test cleanup
Yuki Kimoto authored on 2011-08-10
724
$dbi = DBIx::Custom->connect;
725
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
726
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
727
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
728
$dbi->insert(table => 'table', param => {select => 1});
729
$dbi->delete(table => 'table', where => {select => 1});
730
$result = $dbi->execute("select * from ${q}table$p");
731
$rows   = $result->all;
732
is_deeply($rows, [], "reserved word");
733

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

            
744

            
745
test 'select';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
746
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
747
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
748
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
749
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
750
$rows = $dbi->select(table => $table1)->all;
751
is_deeply($rows, [{$key1 => 1, $key2 => 2},
752
                  {$key1 => 3, $key2 => 4}], "table");
test cleanup
Yuki Kimoto authored on 2011-08-10
753

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

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

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

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
768
eval { $dbi->execute("drop table $table2") };
test cleanup
Yuki Kimoto authored on 2011-08-10
769
$dbi->execute($create_table2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
770
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
test cleanup
Yuki Kimoto authored on 2011-08-10
771
$rows = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
772
    table => [$table1, $table2],
773
    column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
774
    where   => {"$table1.$key2" => 2},
775
    relation  => {"$table1.$key1" => "$table2.$key1"}
test cleanup
Yuki Kimoto authored on 2011-08-10
776
)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
777
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
778

            
779
$rows = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
780
    table => [$table1, $table2],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
781
    column => ["$table1.$key1 as ${table1}_$key1", "${table2}.$key1 as ${table2}_$key1", $key2, $key3],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
782
    relation  => {"$table1.$key1" => "$table2.$key1"}
test cleanup
Yuki Kimoto authored on 2011-08-10
783
)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
784
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
785

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

            
789
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
790
eval { $dbi->execute("drop table ${q}table$p") };
791
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
792
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
793
$dbi->insert(table => 'table', param => {select => 1, update => 2});
794
$result = $dbi->select(table => 'table', where => {select => 1});
795
$rows   = $result->all;
796
is_deeply($rows, [{select => 2, update => 2}], "reserved word");
797

            
798
test 'fetch filter';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
799
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
800
$dbi->register_filter(
801
    twice       => sub { $_[0] * 2 },
802
    three_times => sub { $_[0] * 3 }
803
);
804
$dbi->default_fetch_filter('twice');
805
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
806
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
807
$result = $dbi->select(table => $table1);
808
$result->filter({$key1 => 'three_times'});
test cleanup
Yuki Kimoto authored on 2011-08-10
809
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
810
is_deeply($row, {$key1 => 3, $key2 => 4}, "default_fetch_filter and filter");
test cleanup
Yuki Kimoto authored on 2011-08-10
811

            
812
test 'filters';
813
$dbi = DBIx::Custom->new;
814

            
815
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')),
816
   'あ', "decode_utf8");
817

            
818
is($dbi->filters->{encode_utf8}->('あ'),
819
   encode_utf8('あ'), "encode_utf8");
820

            
cleanup test
Yuki Kimoto authored on 2011-08-10
821
test 'transaction1';
test cleanup
Yuki Kimoto authored on 2011-08-10
822
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
823
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
824
$dbi->execute($create_table1);
fixed transaction test
Yuki Kimoto authored on 2011-08-14
825
$dbi->begin_work;
826
$dbi->dbh->{AutoCommit} = 0;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
827
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
fixed transaction test
Yuki Kimoto authored on 2011-08-14
828
$dbi->rollback;
829
$dbi->dbh->{AutoCommit} = 1;
830

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

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

            
835
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
836
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
837
$dbi->execute($create_table1);
fixed transaction test
Yuki Kimoto authored on 2011-08-14
838
$dbi->begin_work;
839
$dbi->dbh->{AutoCommit} = 0;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
840
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
841
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
fixed transaction test
Yuki Kimoto authored on 2011-08-14
842
$dbi->commit;
843
$dbi->dbh->{AutoCommit} = 1;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
844
$result = $dbi->select(table => $table1);
845
is_deeply(scalar $result->all, [{$key1 => 1, $key2 => 2}, {$key1 => 2, $key2 => 3}],
fixed transaction test
Yuki Kimoto authored on 2011-08-14
846
          "commit");
test cleanup
Yuki Kimoto authored on 2011-08-10
847

            
848
test 'execute';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
849
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
850
$dbi->execute($create_table1);
851
{
852
    local $Carp::Verbose = 0;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
853
    eval{$dbi->execute("select * frm $table1")};
cleanup test
Yuki Kimoto authored on 2011-08-15
854
    like($@, qr/\Qselect * frm $table1/, "fail prepare");
test cleanup
Yuki Kimoto authored on 2011-08-10
855
    like($@, qr/\.t /, "fail : not verbose");
856
}
857
{
858
    local $Carp::Verbose = 1;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
859
    eval{$dbi->execute("select * frm $table1")};
test cleanup
Yuki Kimoto authored on 2011-08-10
860
    like($@, qr/Custom.*\.t /s, "fail : verbose");
861
}
862

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

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

            
871
{
872
    local $Carp::Verbose = 0;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
873
    eval{$dbi->execute("select * from $table1 where {0 $key1}", {}, query => 1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
874
    like($@, qr/\Q.t /, "caller spec : not vebose");
875
}
876
{
877
    local $Carp::Verbose = 1;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
878
    eval{$dbi->execute("select * from $table1 where {0 $key1}", {}, query => 1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
879
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
880
}
881

            
882

            
cleanup test
Yuki Kimoto authored on 2011-08-10
883
test 'transaction2';
test cleanup
Yuki Kimoto authored on 2011-08-10
884
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
885
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
886
$dbi->execute($create_table1);
887

            
888
$dbi->begin_work;
889

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

            
896
$dbi->rollback if $@;
897

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

            
902
$dbi->begin_work;
903

            
904
eval {
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
905
    $dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
906
    $dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-10
907
};
908

            
909
$dbi->commit unless $@;
910

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

            
915
$dbi->dbh->{AutoCommit} = 0;
916
eval{ $dbi->begin_work };
917
ok($@, "exception");
918
$dbi->dbh->{AutoCommit} = 1;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
919

            
test cleanup
Yuki Kimoto authored on 2011-08-10
920
test 'cache';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
921
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
922
$dbi->cache(1);
923
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-15
924
$source = "select * from $table1 where $key1 = :$key1 and $key2 = :$key2";
test cleanup
Yuki Kimoto authored on 2011-08-10
925
$dbi->execute($source, {}, query => 1);
926
is_deeply($dbi->{_cached}->{$source}, 
cleanup test
Yuki Kimoto authored on 2011-08-15
927
          {sql => "select * from $table1 where $key1 = ? and $key2 = ?", columns => [$key1, $key2], tables => []}, "cache");
test cleanup
Yuki Kimoto authored on 2011-08-10
928

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
929
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
930
$dbi->execute($create_table1);
931
$dbi->{_cached} = {};
932
$dbi->cache(0);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
933
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
934
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
935

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

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

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

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

            
970
test 'method';
971
$dbi->method(
972
    one => sub { 1 }
973
);
974
$dbi->method(
975
    two => sub { 2 }
976
);
977
$dbi->method({
978
    twice => sub {
979
        my $self = shift;
980
        return $_[0] * 2;
981
    }
982
});
983

            
984
is($dbi->one, 1, "first");
985
is($dbi->two, 2, "second");
986
is($dbi->twice(5), 10 , "second");
987

            
988
eval {$dbi->XXXXXX};
989
ok($@, "not exists");
990

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

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

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

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

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

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

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

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

            
1110
$result->filter({$key2 => 'twice'});
1111
$rows   = $result->all;
1112
is_deeply($rows, [{$key2 => 4, $key3 => 18}], "select : join");
1113

            
1114
$result = $dbi->select(
1115
     table => [$table1, $table2],
1116
     column => [$key2, $key3],
1117
     where => {$key2 => 1, $key3 => 2}, relation => {"$table1.$key1" => "$table2.$key1"});
1118

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

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

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1137
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1138
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1139
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1140
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1141
is($dbi->select(table => $table1)->one->{$key1}, 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1142

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

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

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

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

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

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1203
test 'remove_end_filter and remove_filter';
1204
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1205
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1206
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1207
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1208
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1209
$row = $result
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1210
       ->filter($key1 => sub { $_[0] * 2 }, $key2 => sub { $_[0] * 4 })
test cleanup
Yuki Kimoto authored on 2011-08-10
1211
       ->remove_filter
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1212
       ->end_filter($key1 => sub { $_[0] * 3 }, $key2 => sub { $_[0] * 5 })
test cleanup
Yuki Kimoto authored on 2011-08-10
1213
       ->remove_end_filter
1214
       ->fetch_first;
1215
is_deeply($row, [1, 2]);
1216

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

            
1226
test 'select query option';
1227
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1228
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1229
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1230
$query = $dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1231
is(ref $query, 'DBIx::Custom::Query');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1232
$query = $dbi->update(table => $table1, where => {$key1 => 1}, param => {$key2 => 2}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1233
is(ref $query, 'DBIx::Custom::Query');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1234
$query = $dbi->delete(table => $table1, where => {$key1 => 1}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1235
is(ref $query, 'DBIx::Custom::Query');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1236
$query = $dbi->select(table => $table1, where => {$key1 => 1, $key2 => 2}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1237
is(ref $query, 'DBIx::Custom::Query');
1238

            
1239
test 'where';
1240
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1241
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1242
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1243
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1244
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
1245
$where = $dbi->where->clause(['and', "$key1 = :$key1", "$key2 = :$key2"]);
1246
is("$where", "where ( $key1 = :$key1 and $key2 = :$key2 )", 'no param');
test cleanup
Yuki Kimoto authored on 2011-08-10
1247

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

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

            
1259
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1260
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1261
    where => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1262
        ['and', "$key1 = :$key1", "$key2 = :$key2"],
1263
        {$key1 => 1}
test cleanup
Yuki Kimoto authored on 2011-08-10
1264
    ]
1265
);
1266
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1267
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
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, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
1272
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1273
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1274
    where => $where
1275
);
1276
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1277
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1278

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

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

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

            
1307
eval {
1308
$where = $dbi->where
1309
             ->clause(['uuu']);
1310
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1311
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1312
    where => $where
1313
);
1314
};
1315
ok($@);
1316

            
1317
$where = $dbi->where;
1318
is("$where", '');
1319

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

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

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

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

            
1360
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1361
             ->clause("$key1 = :$key1 $key2 = :$key2")
1362
             ->param({$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1363
eval{$where->to_string};
1364
like($@, qr/one column/);
1365

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1495
eval {$dbi->where(ppp => 1) };
1496
like($@, qr/invalid/);
1497

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

            
1509

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

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

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

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

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

            
1559
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1560
$where->clause(['and', ":${key1}{=}"]);
1561
$where->param({$key1 => 0});
test cleanup
Yuki Kimoto authored on 2011-08-10
1562
$where->if('length');
1563
$where->map;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1564
$result = $dbi->execute("select * from $table1 $where", {$key1 => 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 => ''});
test cleanup
Yuki Kimoto authored on 2011-08-10
1571
$where->if('length');
1572
$where->map;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1573
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
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}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1576

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

            
1586
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1587
$where->clause(['and', ":${key1}{=}"]);
1588
$where->param({$key1 => 7});
test cleanup
Yuki Kimoto authored on 2011-08-10
1589
$where->if(sub { ($_[0] || '') eq 5 });
1590
$where->map;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1591
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1592
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1593
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1594

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

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

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

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

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

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

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

            
1664
$where = $dbi->where;
1665
$where->param({id => ['', ''], author => 'Ken', price => 1900});
1666
$where->map(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1667
    id => ["$table1.id", {if => 'length'}],
1668
    author => ["$table1.author", sub { '%' . $_[0] . '%' }, {if => 'defined'}],
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" => [$dbi->not_exists, $dbi->not_exists], "$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
test 'register_tag_processor';
1675
$dbi = DBIx::Custom->connect;
1676
$dbi->register_tag_processor(
1677
    a => sub { 1 }
1678
);
test cleanup
Yuki Kimoto authored on 2011-08-10
1679
is($dbi->{_tags}->{a}->(), 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1680

            
1681
test 'register_tag';
1682
$dbi = DBIx::Custom->connect;
1683
$dbi->register_tag(
1684
    b => sub { 2 }
1685
);
test cleanup
Yuki Kimoto authored on 2011-08-10
1686
is($dbi->{_tags}->{b}->(), 2);
test cleanup
Yuki Kimoto authored on 2011-08-10
1687

            
1688
test 'table not specify exception';
1689
$dbi = DBIx::Custom->connect;
1690
eval {$dbi->insert};
1691
like($@, qr/table/);
1692
eval {$dbi->update};
1693
like($@, qr/table/);
1694
eval {$dbi->delete};
1695
like($@, qr/table/);
1696
eval {$dbi->select};
1697
like($@, qr/table/);
test cleanup
Yuki Kimoto authored on 2011-08-10
1698

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1699
test 'more tests';
1700
$dbi = DBIx::Custom->connect;
1701
eval{$dbi->apply_filter('table', 'column', [])};
1702
like($@, qr/apply_filter/);
1703

            
1704
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
1705
like($@, qr/apply_filter/);
1706

            
1707
$dbi->apply_filter(
1708

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

            
1720
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1721
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1722
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1723
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1724
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
1725
$dbi->apply_filter($table1, $key2, {});
1726
$rows = $dbi->select(table => $table1, where => {$key2 => 2})->all;
1727
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1728

            
1729
$dbi = DBIx::Custom->connect;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1730
eval {$dbi->apply_filter($table1, $key2, {out => 'no'})};
test cleanup
Yuki Kimoto authored on 2011-08-10
1731
like($@, qr/not registered/);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1732
eval {$dbi->apply_filter($table1, $key2, {in => 'no'})};
test cleanup
Yuki Kimoto authored on 2011-08-10
1733
like($@, qr/not registered/);
1734
$dbi->method({one => sub { 1 }});
1735
is($dbi->one, 1);
1736

            
1737
eval{DBIx::Custom->connect(dsn => undef)};
1738
like($@, qr/_connect/);
1739

            
1740
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1741
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1742
$dbi->execute($create_table1);
1743
$dbi->register_filter(twice => sub { $_[0] * 2 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1744
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2},
1745
             filter => {$key1 => 'twice'});
1746
$row = $dbi->select(table => $table1)->one;
1747
is_deeply($row, {$key1 => 2, $key2 => 2});
1748
eval {$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2},
1749
             filter => {$key1 => 'no'}) };
test cleanup
Yuki Kimoto authored on 2011-08-10
1750
like($@, qr//);
1751

            
1752
$dbi->register_filter(one => sub { });
1753
$dbi->default_fetch_filter('one');
1754
ok($dbi->default_fetch_filter);
1755
$dbi->default_bind_filter('one');
1756
ok($dbi->default_bind_filter);
1757
eval{$dbi->default_fetch_filter('no')};
1758
like($@, qr/not registered/);
1759
eval{$dbi->default_bind_filter('no')};
1760
like($@, qr/not registered/);
1761
$dbi->default_bind_filter(undef);
1762
ok(!defined $dbi->default_bind_filter);
1763
$dbi->default_fetch_filter(undef);
1764
ok(!defined $dbi->default_fetch_filter);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1765
eval {$dbi->execute("select * from $table1 {} {= author") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1766
like($@, qr/Tag not finished/);
1767

            
1768
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1769
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1770
$dbi->execute($create_table1);
1771
$dbi->register_filter(one => sub { 1 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1772
$result = $dbi->select(table => $table1);
1773
eval {$result->filter($key1 => 'no')};
test cleanup
Yuki Kimoto authored on 2011-08-10
1774
like($@, qr/not registered/);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1775
eval {$result->end_filter($key1 => 'no')};
test cleanup
Yuki Kimoto authored on 2011-08-10
1776
like($@, qr/not registered/);
1777
$result->default_filter(undef);
1778
ok(!defined $result->default_filter);
1779
$result->default_filter('one');
1780
is($result->default_filter->(), 1);
1781

            
1782
test 'dbi_option';
1783
$dbi = DBIx::Custom->connect(dbi_option => {PrintError => 1});
1784
ok($dbi->dbh->{PrintError});
1785
$dbi = DBIx::Custom->connect(dbi_options => {PrintError => 1});
1786
ok($dbi->dbh->{PrintError});
1787

            
1788
test 'DBIx::Custom::Result stash()';
1789
$result = DBIx::Custom::Result->new;
1790
is_deeply($result->stash, {}, 'default');
1791
$result->stash->{foo} = 1;
1792
is($result->stash->{foo}, 1, 'get and set');
test cleanup
Yuki Kimoto authored on 2011-08-10
1793

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1794
test 'delete_at';
1795
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1796
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
1797
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1798
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1799
$dbi->delete_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1800
    table => $table1,
1801
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1802
    where => [1, 2],
1803
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1804
is_deeply($dbi->select(table => $table1)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
1805

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1806
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1807
$dbi->delete_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1808
    table => $table1,
1809
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
1810
    where => 1,
1811
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1812
is_deeply($dbi->select(table => $table1)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
1813

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1828
$dbi->delete_all(table => $table1);
1829
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1830
$dbi->insert_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1831
    primary_key => $key1, 
1832
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
1833
    where => 1,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1834
    param => {$key2 => 2, $key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
1835
);
1836

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

            
1841
eval {
1842
    $dbi->insert_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1843
        table => $table1,
1844
        primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1845
        where => {},
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1846
        param => {$key1 => 1, $key2 => 2, $key3 => 3},
cleanup test
Yuki Kimoto authored on 2011-08-10
1847
    );
1848
};
1849
like($@, qr/must be/);
1850

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

            
1864
test 'update_at';
1865
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1866
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
1867
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1868
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1869
$dbi->update_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1870
    table => $table1,
1871
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1872
    where => [1, 2],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1873
    param => {$key3 => 4}
cleanup test
Yuki Kimoto authored on 2011-08-10
1874
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1875
is($dbi->select(table => $table1)->one->{$key1}, 1);
1876
is($dbi->select(table => $table1)->one->{$key2}, 2);
1877
is($dbi->select(table => $table1)->one->{$key3}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-10
1878

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1879
$dbi->delete_all(table => $table1);
1880
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1881
$dbi->update_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1882
    table => $table1,
1883
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
1884
    where => 1,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1885
    param => {$key3 => 4}
cleanup test
Yuki Kimoto authored on 2011-08-10
1886
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1887
is($dbi->select(table => $table1)->one->{$key1}, 1);
1888
is($dbi->select(table => $table1)->one->{$key2}, 2);
1889
is($dbi->select(table => $table1)->one->{$key3}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-10
1890

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

            
1905
test 'select_at';
1906
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1907
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
1908
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1909
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1910
$result = $dbi->select_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1911
    table => $table1,
1912
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1913
    where => [1, 2]
1914
);
1915
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1916
is($row->{$key1}, 1);
1917
is($row->{$key2}, 2);
1918
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
1919

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1932
$dbi->delete_all(table => $table1);
1933
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1934
$result = $dbi->select_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1935
    table => $table1,
1936
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1937
    where => [1, 2]
1938
);
1939
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1940
is($row->{$key1}, 1);
1941
is($row->{$key2}, 2);
1942
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
1943

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

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

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

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

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

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

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

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

            
2039

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

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

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

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

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

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2142
test 'merge_param';
2143
$dbi = DBIx::Custom->new;
2144
$params = [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2145
    {$key1 => 1, $key2 => 2, $key3 => 3},
2146
    {$key1 => 1, $key2 => 2},
2147
    {$key1 => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2148
];
2149
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2150
is_deeply($param, {$key1 => [1, 1, 1], $key2 => [2, 2], $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2151

            
2152
$params = [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2153
    {$key1 => [1, 2], $key2 => 1, $key3 => [1, 2]},
2154
    {$key1 => [3, 4], $key2 => [2, 3], $key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
2155
];
2156
$param = $dbi->merge_param($params->[0], $params->[1]);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2157
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
2158

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

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

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

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

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

            
2235

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

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

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2279
$dbi->delete_all(table => $table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2280
$dbi->insert(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2281
    primary_key => $key1, 
2282
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2283
    id => 0,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2284
    param => {$key2 => 2, $key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
2285
);
2286

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

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

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

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

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2346
$dbi->delete_all(table => $table1);
2347
$dbi->insert(table => $table1, param => {$key1 => 0, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2348
$dbi->update(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2349
    table => $table1,
2350
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2351
    id => 0,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2352
    param => {$key3 => 4}
cleanup test
Yuki Kimoto authored on 2011-08-10
2353
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2354
is($dbi->select(table => $table1)->one->{$key1}, 0);
2355
is($dbi->select(table => $table1)->one->{$key2}, 2);
2356
is($dbi->select(table => $table1)->one->{$key3}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-10
2357

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

            
2372

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

            
2388

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2401
$dbi->insert(table => $table1, param => {$key1 => 0, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2402
$dbi->delete(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2403
    table => $table1,
2404
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2405
    id => 0,
2406
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2407
is_deeply($dbi->select(table => $table1)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
2408

            
2409

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

            
2428

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

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2456
$dbi->delete_all(table => $table1);
2457
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2458
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2459
    table => $table1,
2460
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
2461
    id => [1, 2]
2462
);
2463
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2464
is($row->{$key1}, 1);
2465
is($row->{$key2}, 2);
2466
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2467

            
2468

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

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

            
2498
$result = $model->select(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2499
    column => [$model->column($table2 => [$key1, $key3])],
2500
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2501
);
2502
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2503
          {"$table2.$key1" => 1, "$table2.$key3" => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2504

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2505
test 'separator';
2506
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
2507
$dbi->user_table_info($user_table_info);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2508
eval { $dbi->execute("drop table $table1") };
2509
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2510
$dbi->execute($create_table1);
2511
$dbi->execute($create_table2);
test cleanup
Yuki Kimoto authored on 2011-08-10
2512

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2551
$dbi->separator('-');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2552
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2553
$result = $model->select(
2554
    column => [
2555
        $model->mycolumn,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2556
        {$table2 => [$key1, $key3]}
cleanup test
Yuki Kimoto authored on 2011-08-10
2557
    ],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2558
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2559
);
2560
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2561
          {$key1 => 1, $key2 => 2, "$table2-$key1" => 1, "$table2-$key3" => 3});
2562
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2563

            
2564

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2565
test 'filter_off';
2566
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
2567
$dbi->user_table_info($user_table_info);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2568
eval { $dbi->execute("drop table $table1") };
2569
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2570
$dbi->execute($create_table1);
2571
$dbi->execute($create_table2);
test cleanup
Yuki Kimoto authored on 2011-08-10
2572

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2573
$dbi->create_model(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2574
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2575
    join => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2576
       "left outer join $table2 on $table1.$key1 = $table2.$key1"
cleanup test
Yuki Kimoto authored on 2011-08-10
2577
    ],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2578
    primary_key => [$key1],
cleanup test
Yuki Kimoto authored on 2011-08-10
2579
);
2580
$dbi->setup_model;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2581
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2582
$model = $dbi->model($table1);
2583
$result = $model->select(column => $key1);
2584
$result->filter($key1 => sub { $_[0] * 2 });
2585
is_deeply($result->one, {$key1 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
2586

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2587
test 'available_datetype';
2588
$dbi = DBIx::Custom->connect;
2589
ok($dbi->can('available_datatype'));
test cleanup
Yuki Kimoto authored on 2011-08-10
2590

            
2591

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2592
test 'select prefix option';
2593
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2594
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2595
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2596
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2597
$rows = $dbi->select(prefix => "$key1,", column => $key2, table => $table1)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2598
is_deeply($rows, [{$key1 => 1, $key2 => 2}], "table");
test cleanup
Yuki Kimoto authored on 2011-08-10
2599

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

            
added tests
Yuki Kimoto authored on 2011-08-26
2601
test 'mapper';
2602
$dbi = DBIx::Custom->connect;
2603
$param = $dbi->mapper(param => {id => 1, author => 'Ken', price => 1900})->map(
2604
    id => "$table1.id",
2605
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2606
    price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
2607
);
2608
is_deeply($param, {"$table1.id" => 1, "$table1.author" => '%Ken%',
2609
  "$table1.price" => 1900});
2610

            
2611
$param = $dbi->mapper(param => {id => 0, author => 0, price => 0})->map(
2612
    id => "$table1.id",
2613
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2614
    price => ["$table1.price", sub { '%' . $_[0] . '%' },
2615
      {condition => sub { $_[0] eq 0 }}]
2616
);
2617
is_deeply($param, {"$table1.id" => 0, "$table1.author" => '%0%', "$table1.price" => '%0%'});
2618

            
2619
$param = $dbi->mapper(param => {id => '', author => '', price => ''})->map(
2620
    id => "$table1.id",
2621
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2622
    price => ["$table1.price", sub { '%' . $_[0] . '%' },
2623
      {condition => sub { $_[0] eq 1 }}]
2624
);
2625
is_deeply($param, {});
2626

            
2627
$param = $dbi->mapper(param => {id => undef, author => undef, price => undef})->map(
2628
    id => "$table1.id",
2629
    price => ["$table1.price", {condition => 'exists'}]
2630
);
2631
is_deeply($param, {"$table1.price" => undef});
2632

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

            
2639
$param = $dbi->mapper(param => {price => 'a'}, condition => 'exists')->map(
2640
    id => ["$table1.id"],
2641
    price => ["$table1.price", sub { '%' . $_[0] }]
2642
);
2643
is_deeply($param, {"$table1.price" => '%a'});
2644

            
- added EXPERIMENTAL pass at...
Yuki Kimoto authored on 2011-09-02
2645
eval { $dbi->execute("drop table $table1") };
2646
$dbi->execute($create_table1);
2647
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2648
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
2649

            
2650
$where = $dbi->where;
2651
$where->clause(['and', ":${key1}{=}"]);
2652
$param = $dbi->mapper(param => {$key1 => undef}, condition => 'defined')->map;
2653
$where->param($param);
2654
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
2655
$row = $result->all;
2656
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
2657

            
2658
$where = $dbi->where;
2659
$where->clause(['or', ":${key1}{=}", ":${key1}{=}"]);
2660
$param = $dbi->mapper(param => {$key1 => [undef, undef]}, condition => 'exists')->map;
2661
$result = $dbi->execute("select * from $table1 $where", {$key1 => [1, 0]});
2662
$row = $result->all;
2663
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
2664
$result = $dbi->execute("select * from $table1 $where", {$key1 => [0, 1]});
2665
$row = $result->all;
2666
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
2667

            
2668
$where = $dbi->where;
2669
$where->clause(['and', ":${key1}{=}"]);
2670
$param = $dbi->mapper(param => {$key1 => [undef, undef]}, condition => 'defined')->map;
2671
$where->param($param);
2672
$result = $dbi->execute("select * from $table1 $where", {$key1 => [1, 0]});
2673
$row = $result->all;
2674
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
2675
$result = $dbi->execute("select * from $table1 $where", {$key1 => [0, 1]});
2676
$row = $result->all;
2677
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
2678

            
2679
$where = $dbi->where;
2680
$where->clause(['and', ":${key1}{=}"]);
2681
$param = $dbi->mapper(param => {$key1 => 0}, condition => 'length')
2682
  ->pass([$key1, $key2])->map;
2683
$where->param($param);
2684
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
2685
$row = $result->all;
2686
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
2687

            
2688
$where = $dbi->where;
2689
$where->clause(['and', ":${key1}{=}"]);
2690
$param = $dbi->mapper(param => {$key1 => ''}, condition => 'length')->map;
2691
$where->param($param);
2692
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
2693
$row = $result->all;
2694
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
2695

            
2696
$where = $dbi->where;
2697
$where->clause(['and', ":${key1}{=}"]);
2698
$param = $dbi->mapper(param => {$key1 => 5}, condition => sub { ($_[0] || '') eq 5 })
2699
  ->pass([$key1, $key2])->map;
2700
$where->param($param);
2701
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
2702
$row = $result->all;
2703
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
2704

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

            
2713
$where = $dbi->where;
2714
$param = $dbi->mapper(param => {id => 1, author => 'Ken', price => 1900})->map(
2715
    id => "$table1.id",
2716
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2717
    price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
2718
);
2719
$where->param($param);
2720
is_deeply($where->param, {"$table1.id" => 1, "$table1.author" => '%Ken%',
2721
  "$table1.price" => 1900});
2722

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

            
2732
$where = $dbi->where;
2733
$param = $dbi->mapper(param => {id => '', author => '', price => ''})->map(
2734
    id => "$table1.id",
2735
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2736
    price => ["$table1.price", sub { '%' . $_[0] . '%' }, {condition => sub { $_[0] eq 1 }}]
2737
);
2738
$where->param($param);
2739
is_deeply($where->param, {});
2740

            
2741
$where = $dbi->where;
2742
$param = $dbi->mapper(param => {id => undef, author => undef, price => undef}, condition => 'exists')->map(
2743
    id => "$table1.id",
2744
    price => ["$table1.price", {condition => 'exists'}]
2745
);
2746
is_deeply($param, {"$table1.id"  => undef,"$table1.price" => undef});
2747

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

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

            
2764
$where = $dbi->where;
2765
$param = $dbi->mapper(param => {id => ['', ''], author => 'Ken', price => 1900}, condition => 'length')->map(
2766
    id => "$table1.id",
2767
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2768
    price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
2769
);
2770
is_deeply($param, {"$table1.id" => [$dbi->not_exists, $dbi->not_exists], "$table1.author" => '%Ken%',
2771
  "$table1.price" => 1900});
2772

            
2773
$where = $dbi->where;
2774
$param = $dbi->mapper(param => {id => ['', ''], author => 'Ken', price => 1900})->map(
2775
    id => ["$table1.id", {condition => 'length'}],
2776
    author => ["$table1.author", sub { '%' . $_[0] . '%' }, {condition => 'defined'}],
2777
    price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
2778
);
2779
is_deeply($param, {"$table1.id" => [$dbi->not_exists, $dbi->not_exists], "$table1.author" => '%Ken%',
2780
  "$table1.price" => 1900});
2781

            
2782
$where = $dbi->where;
2783
$param = $dbi->mapper(param => {id => 'a', author => 'b', price => 'c'}, pass => [qw/id author/])
2784
  ->map(price => 'book.price');
2785
is_deeply($param, {id => 'a', author => 'b', 'book.price' => 'c'});
2786

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2787
test 'map_param';
2788
$dbi = DBIx::Custom->connect;
2789
$param = $dbi->map_param(
2790
    {id => 1, author => 'Ken', price => 1900},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2791
    id => "$table1.id",
2792
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2793
    price => ["$table1.price", {if => sub { $_[0] eq 1900 }}]
cleanup test
Yuki Kimoto authored on 2011-08-10
2794
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2795
is_deeply($param, {"$table1.id" => 1, "$table1.author" => '%Ken%',
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2796
  "$table1.price" => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
2797

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2798
$param = $dbi->map_param(
2799
    {id => 0, author => 0, price => 0},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2800
    id => "$table1.id",
2801
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2802
    price => ["$table1.price", sub { '%' . $_[0] . '%' },
cleanup test
Yuki Kimoto authored on 2011-08-10
2803
      {if => sub { $_[0] eq 0 }}]
2804
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2805
is_deeply($param, {"$table1.id" => 0, "$table1.author" => '%0%', "$table1.price" => '%0%'});
test cleanup
Yuki Kimoto authored on 2011-08-10
2806

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2807
$param = $dbi->map_param(
2808
    {id => '', author => '', price => ''},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2809
    id => "$table1.id",
2810
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2811
    price => ["$table1.price", sub { '%' . $_[0] . '%' },
cleanup test
Yuki Kimoto authored on 2011-08-10
2812
      {if => sub { $_[0] eq 1 }}]
2813
);
2814
is_deeply($param, {});
test cleanup
Yuki Kimoto authored on 2011-08-10
2815

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2816
$param = $dbi->map_param(
2817
    {id => undef, author => undef, price => undef},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2818
    id => "$table1.id",
2819
    price => ["$table1.price", {if => 'exists'}]
cleanup test
Yuki Kimoto authored on 2011-08-10
2820
);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2821
is_deeply($param, {"$table1.price" => undef});
test cleanup
Yuki Kimoto authored on 2011-08-10
2822

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2830
test 'order';
2831
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2832
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2833
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2834
$dbi->insert({$key1 => 1, $key2 => 1}, table => $table1);
2835
$dbi->insert({$key1 => 1, $key2 => 3}, table => $table1);
2836
$dbi->insert({$key1 => 2, $key2 => 2}, table => $table1);
2837
$dbi->insert({$key1 => 2, $key2 => 4}, table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2838
my $order = $dbi->order;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2839
$order->prepend($key1, "$key2 desc");
2840
$result = $dbi->select(table => $table1, append => $order);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2841
is_deeply($result->all, [{$key1 => 1, $key2 => 3}, {$key1 => 1, $key2 => 1},
2842
  {$key1 => 2, $key2 => 4}, {$key1 => 2, $key2 => 2}]);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2843
$order->prepend("$key1 desc");
2844
$result = $dbi->select(table => $table1, append => $order);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2845
is_deeply($result->all, [{$key1 => 2, $key2 => 4}, {$key1 => 2, $key2 => 2},
2846
  {$key1 => 1, $key2 => 3}, {$key1 => 1, $key2 => 1}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2847

            
2848
$order = $dbi->order;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2849
$order->prepend(["$table1-$key1"], ["$table1-$key2", 'desc']);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2850
$result = $dbi->select(table => $table1,
2851
  column => [[$key1 => "$table1-$key1"], [$key2 => "$table1-$key2"]],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2852
  append => $order);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2853
is_deeply($result->all, [{"$table1-$key1" => 1, "$table1-$key2" => 3},
2854
  {"$table1-$key1" => 1, "$table1-$key2" => 1},
2855
  {"$table1-$key1" => 2, "$table1-$key2" => 4},
2856
  {"$table1-$key1" => 2, "$table1-$key2" => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2857

            
2858
test 'tag_parse';
2859
$dbi = DBIx::Custom->connect;
2860
$dbi->tag_parse(0);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2861
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2862
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2863
$dbi->insert({$key1 => 1, $key2 => 1}, table => $table1);
2864
eval {$dbi->execute("select * from $table1 where {= $key1}", {$key1 => 1})};
test cleanup
Yuki Kimoto authored on 2011-08-10
2865
ok($@);
2866

            
2867
test 'last_sql';
2868
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2869
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2870
$dbi->execute($create_table1);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2871
$dbi->execute("select * from $table1");
cleanup test
Yuki Kimoto authored on 2011-08-15
2872
is($dbi->last_sql, "select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
2873

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

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

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

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

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

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2905
$source = "select * from $table1 where :$table1.${key1}{=} and :$table1.${key2}{=}";
test cleanup
Yuki Kimoto authored on 2011-08-10
2906
$result = $dbi->execute(
2907
    $source,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2908
    param => {"$table1.$key1" => 1, "$table1.$key2" => 1},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2909
    filter => {"$table1.$key2" => sub { $_[0] * 2 }}
test cleanup
Yuki Kimoto authored on 2011-08-10
2910
);
2911
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2912
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2913

            
2914
test 'high perfomance way';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2915
$dbi->execute("drop table $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
2916
$dbi->execute($create_table1_highperformance);
2917
$rows = [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2918
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
2919
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
2920
];
2921
{
2922
    my $query;
2923
    foreach my $row (@$rows) {
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2924
      $query ||= $dbi->insert($row, table => $table1, query => 1);
2925
      $dbi->execute($query, $row, filter => {$key7 => sub { $_[0] * 2 }});
test cleanup
Yuki Kimoto authored on 2011-08-10
2926
    }
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2927
    is_deeply($dbi->select(table => $table1)->all,
test cleanup
Yuki Kimoto authored on 2011-08-10
2928
      [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2929
          {$key7 => 2, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
2930
          {$key7 => 2, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
2931
      ]
2932
    );
2933
}
2934

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2935
$dbi->execute("drop table $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
2936
$dbi->execute($create_table1_highperformance);
2937
$rows = [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2938
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
2939
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
2940
];
2941
{
2942
    my $query;
2943
    my $sth;
2944
    foreach my $row (@$rows) {
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2945
      $query ||= $dbi->insert($row, table => $table1, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2946
      $sth ||= $query->sth;
2947
      $sth->execute(map { $row->{$_} } sort keys %$row);
2948
    }
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2949
    is_deeply($dbi->select(table => $table1)->all,
test cleanup
Yuki Kimoto authored on 2011-08-10
2950
      [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2951
          {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
2952
          {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
2953
      ]
2954
    );
2955
}
2956

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
2957
$dbi->execute("drop table $table1");
2958
$dbi->execute($create_table1_highperformance);
2959
$rows = [
2960
    {$key7 => 10, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 5},
2961
    {$key7 => 11, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6},
2962
];
2963
{
2964
    $model = $dbi->create_model(table => $table1, primary_key => $key1);
2965
    my $query;
2966
    foreach my $row (@$rows) {
2967
      $query ||= $model->insert($row, id => 1, query => 1);
2968
      $model->execute($query, $row, id => 1, filter => {$key7 => sub { $_[0] * 2 }});
2969
    }
2970
    is_deeply($dbi->select(table => $table1, append => 'order by key2')->all,
2971
      [
2972
          {$key7 => 20, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 5, $key1 => 1},
2973
          {$key7 => 22, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 1},
2974
      ]
2975
    );
2976
}
2977

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2978
test 'result';
2979
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2980
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2981
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2982
$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1);
2983
$dbi->insert({$key1 => 3, $key2 => 4}, table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2984

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2985
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2986
@rows = ();
2987
while (my $row = $result->fetch) {
2988
    push @rows, [@$row];
2989
}
2990
is_deeply(\@rows, [[1, 2], [3, 4]]);
2991

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2992
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2993
@rows = ();
2994
while (my $row = $result->fetch_hash) {
2995
    push @rows, {%$row};
2996
}
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2997
is_deeply(\@rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2998

            
2999
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3000
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
3001
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3002
$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1);
3003
$dbi->insert({$key1 => 3, $key2 => 4}, table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
3004

            
3005
test 'fetch_all';
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3006
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
3007
$rows = $result->fetch_all;
3008
is_deeply($rows, [[1, 2], [3, 4]]);
3009

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

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

            
3018
$rows = $result->fetch_all;
3019
is_deeply($rows, [[3, 2], [9, 4]], "array");
3020

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

            
3027
test "query_builder";
3028
$datas = [
3029
    # Basic tests
3030
    {   name            => 'placeholder basic',
3031
        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
cleanup test
Yuki Kimoto authored on 2011-08-15
3032
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?",
test cleanup
Yuki Kimoto authored on 2011-08-10
3033
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
3034
    },
3035
    {
3036
        name            => 'placeholder in',
cleanup test
Yuki Kimoto authored on 2011-08-15
3037
        source            => "{in k1 3}",
3038
        sql_expected    => "k1 in (?, ?, ?)",
test cleanup
Yuki Kimoto authored on 2011-08-10
3039
        columns_expected   => [qw/k1 k1 k1/]
3040
    },
3041
    
3042
    # Table name
3043
    {
3044
        name            => 'placeholder with table name',
3045
        source            => "{= a.k1} {= a.k2}",
cleanup test
Yuki Kimoto authored on 2011-08-15
3046
        sql_expected    => "a.k1 = ? a.k2 = ?",
test cleanup
Yuki Kimoto authored on 2011-08-10
3047
        columns_expected  => [qw/a.k1 a.k2/]
3048
    },
3049
    {   
3050
        name            => 'placeholder in with table name',
3051
        source            => "{in a.k1 2} {in b.k2 2}",
cleanup test
Yuki Kimoto authored on 2011-08-15
3052
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?)",
test cleanup
Yuki Kimoto authored on 2011-08-10
3053
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
3054
    },
3055
    {
3056
        name            => 'not contain tag',
3057
        source            => "aaa",
cleanup test
Yuki Kimoto authored on 2011-08-15
3058
        sql_expected    => "aaa",
test cleanup
Yuki Kimoto authored on 2011-08-10
3059
        columns_expected  => [],
3060
    }
3061
];
3062

            
3063
for (my $i = 0; $i < @$datas; $i++) {
3064
    my $data = $datas->[$i];
cleanup
Yuki Kimoto authored on 2011-08-13
3065
    my $dbi = DBIx::Custom->new;
3066
    my $builder = $dbi->query_builder;
test cleanup
Yuki Kimoto authored on 2011-08-10
3067
    my $query = $builder->build_query($data->{source});
3068
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
3069
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
3070
}
3071

            
cleanup
Yuki Kimoto authored on 2011-08-13
3072
$dbi = DBIx::Custom->new;
3073
$builder = $dbi->query_builder;
3074
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
3075
    p => sub {
3076
        my @args = @_;
3077
        
3078
        my $expand    = "? $args[0] $args[1]";
3079
        my $columns = [2];
3080
        return [$expand, $columns];
3081
    }
3082
);
3083

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
3094
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
3095
    q => 'string'
3096
});
3097

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
3101
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
3102
   r => sub {} 
3103
});
3104

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
3108
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
3109
   s => sub { return ["a", ""]} 
3110
});
3111

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
3115
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
3116
    t => sub {return ["a", []]}
3117
);
3118

            
3119

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

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

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

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

            
3135
test 'variouse source';
3136
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d";
3137
$query = $builder->build_query($source);
3138
is($query->sql, 'a b = ? c { } { = ? } = ? d', "basic : 1");
3139

            
3140
$source = "abc";
3141
$query = $builder->build_query($source);
3142
is($query->sql, 'abc', "basic : 2");
3143

            
3144
$source = "{= a}";
3145
$query = $builder->build_query($source);
3146
is($query->sql, 'a = ?', "only tag");
3147

            
3148
$source = "000";
3149
$query = $builder->build_query($source);
3150
is($query->sql, '000', "contain 0 value");
3151

            
3152
$source = "a {= b} }";
3153
eval{$builder->build_query($source)};
3154
like($@, qr/unexpected "}"/, "error : 1");
3155

            
3156
$source = "a {= {}";
3157
eval{$builder->build_query($source)};
3158
like($@, qr/unexpected "{"/, "error : 2");
3159

            
3160
test 'select() wrap option';
3161
$dbi = DBIx::Custom->connect;
3162
eval { $dbi->execute("drop table $table1") };
3163
$dbi->execute($create_table1);
3164
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3165
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
3166
$rows = $dbi->select(
3167
    table => $table1,
3168
    column => $key1,
test cleanup
Yuki Kimoto authored on 2011-08-15
3169
    wrap => ["select * from (", ") t where $key1 = 1"]
cleanup test
Yuki Kimoto authored on 2011-08-15
3170
)->all;
3171
is_deeply($rows, [{$key1 => 1}]);
3172

            
3173
eval {
3174
$dbi->select(
3175
    table => $table1,
3176
    column => $key1,
3177
    wrap => 'select * from ('
3178
)
3179
};
3180
like($@, qr/array/);
3181

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

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

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

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

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

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

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

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

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

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

            
3316

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

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

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

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

            
3355
            
3356
eval { $dbi->update_param({";" => 1}) };
3357
like($@, qr/not safety/);
3358

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

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

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

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

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

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

            
3419
test 'primary_key';
3420
$dbi = MyDBI1->connect;
3421
$model = $dbi->model($table1);
3422
$model->primary_key([$key1, $key2]);
3423
is_deeply($model->primary_key, [$key1, $key2]);
3424

            
3425
test 'columns';
3426
$dbi = MyDBI1->connect;
3427
$model = $dbi->model($table1);
3428
$model->columns([$key1, $key2]);
3429
is_deeply($model->columns, [$key1, $key2]);
cleanup test
Yuki Kimoto authored on 2011-08-10
3430

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

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

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

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

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

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

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

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

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

            
3526

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3527
$dbi = DBIx::Custom->connect;
3528
eval { $dbi->execute("drop table $table1") };
3529
$dbi->execute($create_table1_type);
3530

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

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

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

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

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

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

            
3651

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
4047

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

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

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

            
4121
test 'columns';
4122
$dbi = MyDBI1->connect;
4123
$model = $dbi->model($table1);
4124

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

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