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

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

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

            
14
plan 'no_plan';
15

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

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

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

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

            
86
    use strict;
87
    use warnings;
88

            
89
    use base 'DBIx::Custom';
90

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

            
102
    package MyModel2::Base1;
103

            
104
    use strict;
105
    use warnings;
106

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

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

            
111
    use strict;
112
    use warnings;
113

            
114
    use base 'MyModel2::Base1';
115

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

            
122
    sub list { shift->select; }
123

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

            
126
    use strict;
127
    use warnings;
128

            
129
    use base 'MyModel2::Base1';
130

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

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

            
139
    package MyModel2::TABLE1;
140

            
141
    use strict;
142
    use warnings;
143

            
144
    use base 'MyModel2::Base1';
145

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

            
152
    sub list { shift->select; }
153

            
154
    package MyModel2::TABLE2;
155

            
156
    use strict;
157
    use warnings;
158

            
159
    use base 'MyModel2::Base1';
160

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

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

            
172
    use strict;
173
    use warnings;
174

            
175
    use base 'DBIx::Custom';
176

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
669

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

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

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

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

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

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

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

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

            
734
test 'delete error';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
735
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
736
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
737
eval{$dbi->delete(table => $table1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
738
like($@, qr/"where" must be specified/,
739
         "where key-value pairs not specified");
740

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

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

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

            
765

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

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

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

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

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

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

            
800
$rows = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
801
    table => [$table1, $table2],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
802
    column => ["$table1.$key1 as ${table1}_$key1", "${table2}.$key1 as ${table2}_$key1", $key2, $key3],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
803
    relation  => {"$table1.$key1" => "$table2.$key1"}
test cleanup
Yuki Kimoto authored on 2011-08-10
804
)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
805
is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}], "relation : no exists where");
test cleanup
Yuki Kimoto authored on 2011-08-10
806

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
903

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

            
909
$dbi->begin_work;
910

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

            
917
$dbi->rollback if $@;
918

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

            
923
$dbi->begin_work;
924

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

            
930
$dbi->commit unless $@;
931

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1530

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1728
$dbi->apply_filter(
1729

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2060

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2256

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

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

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

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

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

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

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

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

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

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

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

            
2393

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

            
2409

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

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

            
2430

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

            
2449

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

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

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

            
2489

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

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

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

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

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

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

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

            
2585

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

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

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

            
2612

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

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

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

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

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

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

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

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

            
- added EXPERIMENTAL pass at...
Yuki Kimoto authored on 2011-09-02
2666
eval { $dbi->execute("drop table $table1") };
2667
$dbi->execute($create_table1);
2668
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2669
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
2670

            
2671
$where = $dbi->where;
2672
$where->clause(['and', ":${key1}{=}"]);
2673
$param = $dbi->mapper(param => {$key1 => undef}, condition => 'defined')->map;
2674
$where->param($param);
2675
$result = $dbi->execute("select * from $table1 $where", {$key1 => 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(['or', ":${key1}{=}", ":${key1}{=}"]);
2681
$param = $dbi->mapper(param => {$key1 => [undef, undef]}, condition => 'exists')->map;
2682
$result = $dbi->execute("select * from $table1 $where", {$key1 => [1, 0]});
2683
$row = $result->all;
2684
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
2685
$result = $dbi->execute("select * from $table1 $where", {$key1 => [0, 1]});
2686
$row = $result->all;
2687
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
2688

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

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

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

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

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

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

            
2744
$where = $dbi->where;
2745
$param = $dbi->mapper(param => {id => 0, author => 0, price => 0})->map(
2746
    id => "$table1.id",
2747
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2748
    price => ["$table1.price", sub { '%' . $_[0] . '%' }, {condition => sub { $_[0] eq 0 }}]
2749
);
2750
$where->param($param);
2751
is_deeply($where->param, {"$table1.id" => 0, "$table1.author" => '%0%', "$table1.price" => '%0%'});
2752

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

            
2762
$where = $dbi->where;
2763
$param = $dbi->mapper(param => {id => undef, author => undef, price => undef}, condition => 'exists')->map(
2764
    id => "$table1.id",
2765
    price => ["$table1.price", {condition => 'exists'}]
2766
);
2767
is_deeply($param, {"$table1.id"  => undef,"$table1.price" => undef});
2768

            
2769
$where = $dbi->where;
2770
$param = $dbi->mapper(param => {price => 'a'})->map(
2771
    id => ["$table1.id", {condition => 'exists'}],
2772
    price => ["$table1.price", sub { '%' . $_[0] }, {condition => 'exists'}]
2773
);
2774
is_deeply($param, {"$table1.price" => '%a'});
2775

            
2776
$where = $dbi->where;
2777
$param = $dbi->mapper(param => {id => [1, 2], author => 'Ken', price => 1900})->map(
2778
    id => "$table1.id",
2779
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2780
    price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
2781
);
2782
is_deeply($param, {"$table1.id" => [1, 2], "$table1.author" => '%Ken%',
2783
  "$table1.price" => 1900});
2784

            
2785
$where = $dbi->where;
2786
$param = $dbi->mapper(param => {id => ['', ''], author => 'Ken', price => 1900}, condition => 'length')->map(
2787
    id => "$table1.id",
2788
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2789
    price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
2790
);
2791
is_deeply($param, {"$table1.id" => [$dbi->not_exists, $dbi->not_exists], "$table1.author" => '%Ken%',
2792
  "$table1.price" => 1900});
2793

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

            
2803
$where = $dbi->where;
2804
$param = $dbi->mapper(param => {id => 'a', author => 'b', price => 'c'}, pass => [qw/id author/])
2805
  ->map(price => 'book.price');
2806
is_deeply($param, {id => 'a', author => 'b', 'book.price' => 'c'});
2807

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
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 = ();
3008
while (my $row = $result->fetch) {
3009
    push @rows, [@$row];
3010
}
3011
is_deeply(\@rows, [[1, 2], [3, 4]]);
3012

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

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

            
3026
test 'fetch_all';
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
3027
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
3028
$rows = $result->fetch_all;
3029
is_deeply($rows, [[1, 2], [3, 4]]);
3030

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

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

            
3039
$rows = $result->fetch_all;
3040
is_deeply($rows, [[3, 2], [9, 4]], "array");
3041

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

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

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
3115
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
3116
    q => 'string'
3117
});
3118

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
3122
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
3123
   r => sub {} 
3124
});
3125

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
3129
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
3130
   s => sub { return ["a", ""]} 
3131
});
3132

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

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

            
3140

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

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

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

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

            
3156
test 'variouse source';
3157
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d";
3158
$query = $builder->build_query($source);
3159
is($query->sql, 'a b = ? c { } { = ? } = ? d', "basic : 1");
3160

            
3161
$source = "abc";
3162
$query = $builder->build_query($source);
3163
is($query->sql, 'abc', "basic : 2");
3164

            
3165
$source = "{= a}";
3166
$query = $builder->build_query($source);
3167
is($query->sql, 'a = ?', "only tag");
3168

            
3169
$source = "000";
3170
$query = $builder->build_query($source);
3171
is($query->sql, '000', "contain 0 value");
3172

            
3173
$source = "a {= b} }";
3174
eval{$builder->build_query($source)};
3175
like($@, qr/unexpected "}"/, "error : 1");
3176

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

            
3181
test 'select() wrap option';
3182
$dbi = DBIx::Custom->connect;
3183
eval { $dbi->execute("drop table $table1") };
3184
$dbi->execute($create_table1);
3185
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3186
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
3187
$rows = $dbi->select(
3188
    table => $table1,
3189
    column => $key1,
test cleanup
Yuki Kimoto authored on 2011-08-15
3190
    wrap => ["select * from (", ") t where $key1 = 1"]
cleanup test
Yuki Kimoto authored on 2011-08-15
3191
)->all;
3192
is_deeply($rows, [{$key1 => 1}]);
3193

            
3194
eval {
3195
$dbi->select(
3196
    table => $table1,
3197
    column => $key1,
3198
    wrap => 'select * from ('
3199
)
3200
};
3201
like($@, qr/array/);
3202

            
3203
test 'select() sqlfilter option';
3204
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3205
$dbi->user_table_info($user_table_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3206
eval { $dbi->execute("drop table $table1") };
3207
$dbi->execute($create_table1);
3208
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3209
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
3210
$rows = $dbi->select(
3211
    table => $table1,
3212
    column => $key1,
3213
    sqlfilter => sub {
3214
        my $sql = shift;
test cleanup
Yuki Kimoto authored on 2011-08-15
3215
        $sql = "select * from ( $sql ) t where $key1 = 1";
cleanup test
Yuki Kimoto authored on 2011-08-15
3216
        return $sql;
3217
    }
3218
)->all;
3219
is_deeply($rows, [{$key1 => 1}]);
3220

            
3221
test 'dbi method from model';
3222
$dbi = MyDBI9->connect;
3223
eval { $dbi->execute("drop table $table1") };
3224
$dbi->execute($create_table1);
3225
$dbi->setup_model;
3226
$model = $dbi->model($table1);
3227
eval{$model->execute("select * from $table1")};
3228
ok(!$@);
3229

            
3230
test 'column table option';
3231
$dbi = MyDBI9->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3232
$dbi->user_table_info($user_table_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3233
eval { $dbi->execute("drop table $table1") };
3234
$dbi->execute($create_table1);
3235
eval { $dbi->execute("drop table $table2") };
3236
$dbi->execute($create_table2);
3237
$dbi->setup_model;
3238
$dbi->execute("insert into $table1 ($key1, $key2) values (1, 2)");
3239
$dbi->execute("insert into $table2 ($key1, $key3) values (1, 4)");
3240
$model = $dbi->model($table1);
3241
$result = $model->select(
3242
    column => [
3243
        $model->column($table2, {alias => $table2_alias})
3244
    ],
3245
    where => {"$table2_alias.$key3" => 4}
3246
);
3247
is_deeply($result->one, 
3248
          {"$table2_alias.$key1" => 1, "$table2_alias.$key3" => 4});
3249

            
3250
$dbi->separator('__');
3251
$result = $model->select(
3252
    column => [
3253
        $model->column($table2, {alias => $table2_alias})
3254
    ],
3255
    where => {"$table2_alias.$key3" => 4}
3256
);
3257
is_deeply($result->one, 
3258
          {"${table2_alias}__$key1" => 1, "${table2_alias}__$key3" => 4});
3259

            
3260
$dbi->separator('-');
3261
$result = $model->select(
3262
    column => [
3263
        $model->column($table2, {alias => $table2_alias})
3264
    ],
3265
    where => {"$table2_alias.$key3" => 4}
3266
);
3267
is_deeply($result->one, 
3268
          {"$table2_alias-$key1" => 1, "$table2_alias-$key3" => 4});
3269

            
3270
test 'create_model';
3271
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3272
$dbi->user_table_info($user_table_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3273
eval { $dbi->execute("drop table $table1") };
3274
eval { $dbi->execute("drop table $table2") };
3275
$dbi->execute($create_table1);
3276
$dbi->execute($create_table2);
3277

            
3278
$dbi->create_model(
3279
    table => $table1,
3280
    join => [
3281
       "left outer join $table2 on $table1.$key1 = $table2.$key1"
3282
    ],
3283
    primary_key => [$key1]
3284
);
3285
$model2 = $dbi->create_model(
3286
    table => $table2
3287
);
3288
$dbi->create_model(
3289
    table => $table3,
3290
    filter => [
3291
        $key1 => {in => sub { uc $_[0] }}
3292
    ]
3293
);
3294
$dbi->setup_model;
3295
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3296
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
3297
$model = $dbi->model($table1);
3298
$result = $model->select(
3299
    column => [$model->mycolumn, $model->column($table2)],
3300
    where => {"$table1.$key1" => 1}
3301
);
3302
is_deeply($result->one,
3303
          {$key1 => 1, $key2 => 2, "$table2.$key1" => 1, "$table2.$key3" => 3});
3304
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3});
3305

            
3306
test 'model method';
3307
$dbi = DBIx::Custom->connect;
3308
eval { $dbi->execute("drop table $table2") };
3309
$dbi->execute($create_table2);
3310
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
3311
$model = $dbi->create_model(
3312
    table => $table2
3313
);
3314
$model->method(foo => sub { shift->select(@_) });
3315
is_deeply($model->foo->one, {$key1 => 1, $key3 => 3});
3316

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

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

            
3337

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

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

            
3357
$dbi = DBIx::Custom->connect;
3358
eval { $dbi->execute("drop table $table1") };
3359
$dbi->execute($create_table1_2);
3360
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3361
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3362

            
3363
$param = {$key2 => 11, $key3 => 33};
3364
$update_param = $dbi->update_param($param, {no_set => 1});
3365
$sql = <<"EOS";
3366
update $table1 set $update_param
3367
where $key1 = 1
3368
EOS
3369
$dbi->execute($sql, param => $param);
3370
$result = $dbi->execute("select * from $table1 order by $key1", table => $table1);
3371
$rows   = $result->all;
3372
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 33, $key4 => 4, $key5 => 5},
3373
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3374
                  "update param no_set");
3375

            
3376
            
3377
eval { $dbi->update_param({";" => 1}) };
3378
like($@, qr/not safety/);
3379

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3381
test 'update_param';
3382
$dbi = DBIx::Custom->connect;
3383
eval { $dbi->execute("drop table $table1") };
3384
$dbi->execute($create_table1_2);
3385
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3386
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
test cleanup
Yuki Kimoto authored on 2011-08-10
3387

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3388
$param = {$key2 => 11};
3389
$update_param = $dbi->assign_param($param);
3390
$sql = <<"EOS";
3391
update $table1 set $update_param
3392
where $key1 = 1
3393
EOS
3394
$dbi->execute($sql, param => $param, table => $table1);
3395
$result = $dbi->execute("select * from $table1 order by $key1");
3396
$rows   = $result->all;
3397
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
3398
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3399
                  "basic");
test cleanup
Yuki Kimoto authored on 2011-08-10
3400

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3401
test 'Model class';
3402
$dbi = MyDBI1->connect;
3403
eval { $dbi->execute("drop table $table1") };
3404
$dbi->execute($create_table1);
3405
$model = $dbi->model($table1);
3406
$model->insert({$key1 => 'a', $key2 => 'b'});
3407
is_deeply($model->list->all, [{$key1 => 'a', $key2 => 'b'}], 'basic');
3408
eval { $dbi->execute("drop table $table2") };
3409
$dbi->execute($create_table2);
3410
$model = $dbi->model($table2);
3411
$model->insert({$key1 => 'a'});
3412
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'basic');
3413
is($dbi->models->{$table1}, $dbi->model($table1));
3414
is($dbi->models->{$table2}, $dbi->model($table2));
3415

            
3416
$dbi = MyDBI4->connect;
3417
eval { $dbi->execute("drop table $table1") };
3418
$dbi->execute($create_table1);
3419
$model = $dbi->model($table1);
3420
$model->insert({$key1 => 'a', $key2 => 'b'});
3421
is_deeply($model->list->all, [{$key1 => 'a', $key2 => 'b'}], 'basic');
3422
eval { $dbi->execute("drop table $table2") };
3423
$dbi->execute($create_table2);
3424
$model = $dbi->model($table2);
3425
$model->insert({$key1 => 'a'});
3426
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'basic');
3427

            
3428
$dbi = MyDBI5->connect;
3429
eval { $dbi->execute("drop table $table1") };
3430
eval { $dbi->execute("drop table $table2") };
3431
$dbi->execute($create_table1);
3432
$dbi->execute($create_table2);
3433
$model = $dbi->model($table2);
3434
$model->insert({$key1 => 'a'});
3435
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'include all model');
3436
$dbi->insert(table => $table1, param => {$key1 => 1});
3437
$model = $dbi->model($table1);
3438
is_deeply($model->list->all, [{$key1 => 1, $key2 => undef}], 'include all model');
3439

            
3440
test 'primary_key';
3441
$dbi = MyDBI1->connect;
3442
$model = $dbi->model($table1);
3443
$model->primary_key([$key1, $key2]);
3444
is_deeply($model->primary_key, [$key1, $key2]);
3445

            
3446
test 'columns';
3447
$dbi = MyDBI1->connect;
3448
$model = $dbi->model($table1);
3449
$model->columns([$key1, $key2]);
3450
is_deeply($model->columns, [$key1, $key2]);
cleanup test
Yuki Kimoto authored on 2011-08-10
3451

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3452
test 'setup_model';
3453
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3454
$dbi->user_table_info($user_table_info);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3455
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3456
eval { $dbi->execute("drop table $table2") };
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
3457

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3458
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-15
3459
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-10
3460
$dbi->setup_model;
cleanup test
Yuki Kimoto authored on 2011-08-15
3461
is_deeply([sort @{$dbi->model($table1)->columns}], [$key1, $key2]);
3462
is_deeply([sort @{$dbi->model($table2)->columns}], [$key1, $key3]);
cleanup test
Yuki Kimoto authored on 2011-08-10
3463

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3464
test 'each_column';
3465
$dbi = DBIx::Custom->connect;
3466
eval { $dbi->execute("drop table ${q}table$p") };
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3467
eval { $dbi->execute("drop table $table1") };
3468
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3469
eval { $dbi->execute("drop table $table3") };
3470
$dbi->execute($create_table1_type);
cleanup test
Yuki Kimoto authored on 2011-08-10
3471
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-15
3472

            
3473
$infos = [];
3474
$dbi->each_column(sub {
3475
    my ($self, $table, $column, $cinfo) = @_;
3476
    
3477
    if ($table =~ /^table\d/i) {
3478
         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
3479
         push @$infos, $info;
3480
    }
3481
});
3482
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3483
is_deeply($infos, 
3484
    [
3485
        [$table1, $key1, $key1],
3486
        [$table1, $key2, $key2],
3487
        [$table2, $key1, $key1],
3488
        [$table2, $key3, $key3]
3489
    ]
3490
    
cleanup test
Yuki Kimoto authored on 2011-08-10
3491
);
cleanup test
Yuki Kimoto authored on 2011-08-16
3492

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3493
test 'each_table';
3494
$dbi = DBIx::Custom->connect;
3495
eval { $dbi->execute("drop table $table1") };
3496
eval { $dbi->execute("drop table $table2") };
3497
$dbi->execute($create_table2);
3498
$dbi->execute($create_table1_type);
cleanup test
Yuki Kimoto authored on 2011-08-10
3499

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3500
$infos = [];
3501
$dbi->each_table(sub {
3502
    my ($self, $table, $table_info) = @_;
3503
    
3504
    if ($table =~ /^table\d/i) {
3505
         my $info = [$table, $table_info->{TABLE_NAME}];
3506
         push @$infos, $info;
3507
    }
3508
});
3509
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3510
is_deeply($infos, 
3511
    [
3512
        [$table1, $table1],
3513
        [$table2, $table2],
3514
    ]
cleanup test
Yuki Kimoto authored on 2011-08-10
3515
);
3516

            
cleanup test
Yuki Kimoto authored on 2011-08-16
3517
$dbi = DBIx::Custom->connect;
3518
eval { $dbi->execute("drop table $table1") };
3519
eval { $dbi->execute("drop table $table2") };
3520
$dbi->execute($create_table2);
3521
$dbi->execute($create_table1_type);
3522

            
3523
$infos = [];
3524
$dbi->user_table_info($user_table_info);
3525
$dbi->each_table(sub {
3526
    my ($self, $table, $table_info) = @_;
3527
    
3528
    if ($table =~ /^table\d/i) {
3529
         my $info = [$table, $table_info->{TABLE_NAME}];
3530
         push @$infos, $info;
3531
    }
3532
});
3533
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3534
is_deeply($infos, 
3535
    [
3536
        [$table1, $table1],
3537
        [$table2, $table2],
3538
        [$table3, $table3],
3539
    ]
3540
);
3541

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3542
test 'type_rule into';
3543
eval { $dbi->execute("drop table $table1") };
3544
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3545
$user_column_info = $dbi->get_column_info(exclude_table => $dbi->exclude_table);
3546

            
3547

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3548
$dbi = DBIx::Custom->connect;
3549
eval { $dbi->execute("drop table $table1") };
3550
$dbi->execute($create_table1_type);
3551

            
cleanup
Yuki Kimoto authored on 2011-08-16
3552
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3553
$dbi->type_rule(
3554
    into1 => {
3555
        $date_typename => sub { '2010-' . $_[0] }
3556
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3557
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3558
$dbi->insert({$key1 => '01-01'}, table => $table1);
3559
$result = $dbi->select(table => $table1);
3560
like($result->one->{$key1}, qr/^2010-01-01/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3561

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

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

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

            
3626
$dbi = DBIx::Custom->connect;
3627
eval { $dbi->execute("drop table $table1") };
3628
$dbi->execute($create_table1_type);
3629
$dbi->register_filter(convert => sub {
3630
    my $value = shift || '';
3631
    $value =~ s/02/03/;
3632
    return $value;
3633
});
cleanup
Yuki Kimoto authored on 2011-08-16
3634
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3635
$dbi->type_rule(
3636
    from1 => {
3637
        $date_datatype => 'convert',
3638
    },
3639
    into1 => {
3640
        $date_typename => 'convert',
3641
    }
3642
);
3643
$dbi->insert({$key1 => '2010-02-02'}, table => $table1);
3644
$result = $dbi->select(table => $table1);
3645
like($result->fetch->[0], qr/^2010-03-03/);
3646

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

            
3672

            
3673
$dbi = DBIx::Custom->connect;
3674
eval { $dbi->execute("drop table $table1") };
3675
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3676
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3677
$dbi->type_rule(
3678
    from1 => {
3679
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3680
    },
3681
    from2 => {
3682
        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3683
    },
3684
);
3685
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3686
$result = $dbi->select(table => $table1);
cleanup
Yuki Kimoto authored on 2011-08-16
3687
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3688
$result->type_rule(
3689
    from1 => {
3690
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/6/; return $v }
3691
    },
3692
    from2 => {
3693
        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/8/; return $v }
3694
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3695
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3696
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/8/9/; return $v });
3697
like($result->fetch_first->[0], qr/^2010-01-09/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3698

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

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

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

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

            
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);
3767
$dbi->register_filter(ppp => sub { my $v = shift || ''; $v =~ s/3/4/; return $v });
cleanup
Yuki Kimoto authored on 2011-08-16
3768
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3769
$dbi->type_rule(
3770
    into1 => {
3771
        $date_typename => 'ppp'
3772
    }
3773
);
3774
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3775
$result = $dbi->select(table => $table1);
3776
like($result->one->{$key1}, qr/^2010-01-04/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3777

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3778
eval{$dbi->type_rule(
3779
    into1 => {
3780
        $date_typename => 'pp'
3781
    }
3782
)};
3783
like($@, qr/not registered/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3784

            
cleanup test
Yuki Kimoto authored on 2011-08-10
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 test
Yuki Kimoto authored on 2011-08-10
3788
eval {
cleanup test
Yuki Kimoto authored on 2011-08-15
3789
    $dbi->type_rule(
3790
        from1 => {
3791
            Date => sub { $_[0] * 2 },
3792
        }
cleanup test
Yuki Kimoto authored on 2011-08-10
3793
    );
3794
};
cleanup test
Yuki Kimoto authored on 2011-08-15
3795
like($@, qr/lower/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3796

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3797
eval {
3798
    $dbi->type_rule(
3799
        into1 => {
3800
            Date => sub { $_[0] * 2 },
3801
        }
3802
    );
3803
};
3804
like($@, qr/lower/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3805

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

            
3823
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3824
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3825
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3826
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3827
$dbi->type_rule(
3828
    from1 => {
3829
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v },
3830
        $datetime_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3831
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
3832
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3833
$dbi->insert({$key1 => '2010-01-03', $key2 => '2010-01-01 01:01:03'}, table => $table1);
3834
$result = $dbi->select(table => $table1);
3835
$result->type_rule(
3836
    from1 => {
3837
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3838
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3839
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3840
$row = $result->one;
3841
like($row->{$key1}, qr/^2010-01-05/);
3842
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
3843

            
3844
$result = $dbi->select(table => $table1);
3845
$result->type_rule(
3846
    from1 => {
3847
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3848
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3849
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3850
$row = $result->one;
3851
like($row->{$key1}, qr/2010-01-05/);
3852
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3853

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

            
3864
$result = $dbi->select(table => $table1);
3865
$result->type_rule(
3866
    from1 => [$date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }]
3867
);
3868
$row = $result->one;
3869
like($row->{$key1}, qr/2010-01-05/);
3870
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3871

            
3872
$dbi->register_filter(five => sub { my $v = shift || ''; $v =~ s/3/5/; return $v });
3873
$result = $dbi->select(table => $table1);
3874
$result->type_rule(
3875
    from1 => [$date_datatype => 'five']
3876
);
3877
$row = $result->one;
3878
like($row->{$key1}, qr/^2010-01-05/);
3879
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
3880

            
3881
$result = $dbi->select(table => $table1);
3882
$result->type_rule(
3883
    from1 => [$date_datatype => undef]
3884
);
3885
$row = $result->one;
3886
like($row->{$key1}, qr/^2010-01-03/);
3887
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3888

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3903
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3904
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3905
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3906
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3907
$dbi->type_rule(
3908
    from1 => {
3909
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3910
    },
3911
);
3912
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3913
$result = $dbi->select(table => $table1);
3914
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/4/5/; return $v });
3915
like($result->fetch->[0], qr/^2010-01-05/);
test cleanup
Yuki Kimoto authored on 2011-08-10
3916

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

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

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

            
added memory leak check test
Yuki Kimoto authored on 2011-08-15
3989
test 'join';
3990
$dbi = DBIx::Custom->connect;
3991
eval { $dbi->execute("drop table $table1") };
3992
$dbi->execute($create_table1);
3993
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3994
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
3995
eval { $dbi->execute("drop table $table2") };
3996
$dbi->execute($create_table2);
3997
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3998
eval { $dbi->execute("drop table $table3") };
3999
$dbi->execute("create table $table3 ($key3 int, $key4 int)");
4000
$dbi->insert(table => $table3, param => {$key3 => 5, $key4 => 4});
4001
$rows = $dbi->select(
4002
    table => $table1,
4003
    column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
4004
    where   => {"$table1.$key2" => 2},
4005
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1"]
4006
)->all;
4007
is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}]);
4008

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

            
4016
eval {
4017
    $rows = $dbi->select(
4018
        table => $table1,
4019
        column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
4020
        where   => {"$table1.$key2" => 2},
4021
        join  => {"$table1.$key1" => "$table2.$key1"}
4022
    );
4023
};
4024
like ($@, qr/array/);
4025

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

            
4034
$rows = $dbi->select(
4035
    column => "$table3.$key4 as ${table3}__$key4",
4036
    table => $table1,
4037
    where   => {"$table1.$key1" => 1},
4038
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
4039
              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
4040
)->all;
4041
is_deeply($rows, [{"${table3}__$key4" => 4}]);
4042

            
4043
$rows = $dbi->select(
4044
    column => "$table1.$key1 as ${table1}__$key1",
4045
    table => $table1,
4046
    where   => {"$table3.$key4" => 4},
4047
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
4048
              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
4049
)->all;
4050
is_deeply($rows, [{"${table1}__$key1" => 1}]);
4051

            
4052
$dbi = DBIx::Custom->connect;
4053
eval { $dbi->execute("drop table $table1") };
4054
$dbi->execute($create_table1);
4055
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
4056
eval { $dbi->execute("drop table $table2") };
4057
$dbi->execute($create_table2);
4058
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
4059
$rows = $dbi->select(
4060
    table => $table1,
4061
    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",
4062
    where   => {"$table1.$key2" => 2},
4063
    join  => ["left outer join ${q}$table2$p on ${q}$table1$p.${q}$key1$p = ${q}$table2$p.${q}$key1$p"],
4064
)->all;
4065
is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}],
4066
          'quote');
4067

            
4068

            
4069
$dbi = DBIx::Custom->connect;
4070
eval { $dbi->execute("drop table $table1") };
4071
$dbi->execute($create_table1);
4072
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
4073
$sql = <<"EOS";
4074
left outer join (
4075
  select * from $table1 t1
4076
  where t1.$key2 = (
4077
    select max(t2.$key2) from $table1 t2
4078
    where t1.$key1 = t2.$key1
4079
  )
4080
) $table3 on $table1.$key1 = $table3.$key1
4081
EOS
4082
$join = [$sql];
4083
$rows = $dbi->select(
4084
    table => $table1,
4085
    column => "$table3.$key1 as ${table3}__$key1",
4086
    join  => $join
4087
)->all;
4088
is_deeply($rows, [{"${table3}__$key1" => 1}]);
4089

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

            
4122
$dbi = DBIx::Custom->connect;
4123
eval { $dbi->execute("drop table $table1") };
4124
eval { $dbi->execute("drop table $table2") };
4125
$dbi->execute($create_table1);
4126
$dbi->execute($create_table2);
4127
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
4128
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4});
4129
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
4130
$result = $dbi->select(
4131
    table => $table1,
4132
    column => [{$table2 => [$key3]}],
4133
    join => [
4134
        {
4135
            clause => "left outer join $table2 on $table2.$key3 = '4' and $table1.$key1 = $table2.$key1",
4136
            table => [$table1, $table2]
4137
        }
4138
    ]
4139
);
4140
is_deeply($result->all, [{"$table2.$key3" => 4}]);
4141

            
4142
test 'columns';
4143
$dbi = MyDBI1->connect;
4144
$model = $dbi->model($table1);
4145

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
4146
test 'count';
4147
$dbi = DBIx::Custom->connect;
4148
eval { $dbi->execute("drop table $table1") };
4149
$dbi->execute($create_table1);
4150
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
4151
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 3});
4152
is($dbi->count(table => $table1), 2);
4153
is($dbi->count(table => $table1, where => {$key2 => 2}), 1);
4154
$model = $dbi->create_model(table => $table1);
4155
is($model->count, 2);
cleanup test
Yuki Kimoto authored on 2011-08-10
4156

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