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

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

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

            
14
plan 'no_plan';
15

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

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

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

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

            
86
    use strict;
87
    use warnings;
88

            
89
    use base 'DBIx::Custom';
90

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

            
102
    package MyModel2::Base1;
103

            
104
    use strict;
105
    use warnings;
106

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

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

            
111
    use strict;
112
    use warnings;
113

            
114
    use base 'MyModel2::Base1';
115

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

            
122
    sub list { shift->select; }
123

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

            
126
    use strict;
127
    use warnings;
128

            
129
    use base 'MyModel2::Base1';
130

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

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

            
139
    package MyModel2::TABLE1;
140

            
141
    use strict;
142
    use warnings;
143

            
144
    use base 'MyModel2::Base1';
145

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

            
152
    sub list { shift->select; }
153

            
154
    package MyModel2::TABLE2;
155

            
156
    use strict;
157
    use warnings;
158

            
159
    use base 'MyModel2::Base1';
160

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

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

            
172
    use strict;
173
    use warnings;
174

            
175
    use base 'DBIx::Custom';
176

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
648

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

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

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

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

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

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

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

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

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

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

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

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

            
744

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

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

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

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

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

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

            
779
$rows = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
780
    table => [$table1, $table2],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
781
    column => ["$table1.$key1 as ${table1}_$key1", "${table2}.$key1 as ${table2}_$key1", $key2, $key3],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
782
    relation  => {"$table1.$key1" => "$table2.$key1"}
test cleanup
Yuki Kimoto authored on 2011-08-10
783
)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
784
is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}], "relation : no exists where");
test cleanup
Yuki Kimoto authored on 2011-08-10
785

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
882

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

            
888
$dbi->begin_work;
889

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

            
896
$dbi->rollback if $@;
897

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

            
902
$dbi->begin_work;
903

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

            
909
$dbi->commit unless $@;
910

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1509

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1674
test 'register_tag_processor';
1675
$dbi = DBIx::Custom->connect;
1676
$dbi->register_tag_processor(
1677
    a => sub { 1 }
1678
);
test cleanup
Yuki Kimoto authored on 2011-08-10
1679
is($dbi->{_tags}->{a}->(), 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1680

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

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

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

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

            
1707
$dbi->apply_filter(
1708

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2039

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2235

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

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

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

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

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

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

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

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

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

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

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

            
2372

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

            
2388

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

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

            
2409

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

            
2428

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

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

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

            
2468

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

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

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

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

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

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

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

            
2564

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

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

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

            
2591

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2620
$param = $dbi->map_param(
2621
    {id => '', author => '', price => ''},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2622
    id => "$table1.id",
2623
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2624
    price => ["$table1.price", sub { '%' . $_[0] . '%' },
cleanup test
Yuki Kimoto authored on 2011-08-10
2625
      {if => sub { $_[0] eq 1 }}]
2626
);
2627
is_deeply($param, {});
test cleanup
Yuki Kimoto authored on 2011-08-10
2628

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2629
$param = $dbi->map_param(
2630
    {id => undef, author => undef, price => undef},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2631
    id => "$table1.id",
2632
    price => ["$table1.price", {if => 'exists'}]
cleanup test
Yuki Kimoto authored on 2011-08-10
2633
);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2634
is_deeply($param, {"$table1.price" => undef});
test cleanup
Yuki Kimoto authored on 2011-08-10
2635

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2636
$param = $dbi->map_param(
2637
    {price => 'a'},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2638
    id => ["$table1.id", {if => 'exists'}],
2639
    price => ["$table1.price", sub { '%' . $_[0] }, {if => 'exists'}]
cleanup test
Yuki Kimoto authored on 2011-08-10
2640
);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2641
is_deeply($param, {"$table1.price" => '%a'});
test cleanup
Yuki Kimoto authored on 2011-08-10
2642

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2643
test 'order';
2644
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2645
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2646
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2647
$dbi->insert({$key1 => 1, $key2 => 1}, table => $table1);
2648
$dbi->insert({$key1 => 1, $key2 => 3}, table => $table1);
2649
$dbi->insert({$key1 => 2, $key2 => 2}, table => $table1);
2650
$dbi->insert({$key1 => 2, $key2 => 4}, table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2651
my $order = $dbi->order;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2652
$order->prepend($key1, "$key2 desc");
2653
$result = $dbi->select(table => $table1, append => $order);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2654
is_deeply($result->all, [{$key1 => 1, $key2 => 3}, {$key1 => 1, $key2 => 1},
2655
  {$key1 => 2, $key2 => 4}, {$key1 => 2, $key2 => 2}]);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2656
$order->prepend("$key1 desc");
2657
$result = $dbi->select(table => $table1, append => $order);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2658
is_deeply($result->all, [{$key1 => 2, $key2 => 4}, {$key1 => 2, $key2 => 2},
2659
  {$key1 => 1, $key2 => 3}, {$key1 => 1, $key2 => 1}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2660

            
2661
$order = $dbi->order;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2662
$order->prepend(["$table1-$key1"], ["$table1-$key2", 'desc']);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2663
$result = $dbi->select(table => $table1,
2664
  column => [[$key1 => "$table1-$key1"], [$key2 => "$table1-$key2"]],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2665
  append => $order);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2666
is_deeply($result->all, [{"$table1-$key1" => 1, "$table1-$key2" => 3},
2667
  {"$table1-$key1" => 1, "$table1-$key2" => 1},
2668
  {"$table1-$key1" => 2, "$table1-$key2" => 4},
2669
  {"$table1-$key1" => 2, "$table1-$key2" => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2670

            
2671
test 'tag_parse';
2672
$dbi = DBIx::Custom->connect;
2673
$dbi->tag_parse(0);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2674
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2675
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2676
$dbi->insert({$key1 => 1, $key2 => 1}, table => $table1);
2677
eval {$dbi->execute("select * from $table1 where {= $key1}", {$key1 => 1})};
test cleanup
Yuki Kimoto authored on 2011-08-10
2678
ok($@);
2679

            
2680
test 'last_sql';
2681
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2682
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2683
$dbi->execute($create_table1);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2684
$dbi->execute("select * from $table1");
cleanup test
Yuki Kimoto authored on 2011-08-15
2685
is($dbi->last_sql, "select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
2686

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

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

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

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

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

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2718
$source = "select * from $table1 where :$table1.${key1}{=} and :$table1.${key2}{=}";
test cleanup
Yuki Kimoto authored on 2011-08-10
2719
$result = $dbi->execute(
2720
    $source,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2721
    param => {"$table1.$key1" => 1, "$table1.$key2" => 1},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2722
    filter => {"$table1.$key2" => sub { $_[0] * 2 }}
test cleanup
Yuki Kimoto authored on 2011-08-10
2723
);
2724
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2725
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2726

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

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2748
$dbi->execute("drop table $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
2749
$dbi->execute($create_table1_highperformance);
2750
$rows = [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2751
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
2752
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
2753
];
2754
{
2755
    my $query;
2756
    my $sth;
2757
    foreach my $row (@$rows) {
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2758
      $query ||= $dbi->insert($row, table => $table1, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2759
      $sth ||= $query->sth;
2760
      $sth->execute(map { $row->{$_} } sort keys %$row);
2761
    }
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2762
    is_deeply($dbi->select(table => $table1)->all,
test cleanup
Yuki Kimoto authored on 2011-08-10
2763
      [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2764
          {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
2765
          {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
2766
      ]
2767
    );
2768
}
2769

            
2770
test 'result';
2771
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2772
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2773
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2774
$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1);
2775
$dbi->insert({$key1 => 3, $key2 => 4}, table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2776

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2784
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2785
@rows = ();
2786
while (my $row = $result->fetch_hash) {
2787
    push @rows, {%$row};
2788
}
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2789
is_deeply(\@rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2790

            
2791
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2792
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2793
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2794
$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1);
2795
$dbi->insert({$key1 => 3, $key2 => 4}, table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2796

            
2797
test 'fetch_all';
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2798
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2799
$rows = $result->fetch_all;
2800
is_deeply($rows, [[1, 2], [3, 4]]);
2801

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

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

            
2810
$rows = $result->fetch_all;
2811
is_deeply($rows, [[3, 2], [9, 4]], "array");
2812

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

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

            
2855
for (my $i = 0; $i < @$datas; $i++) {
2856
    my $data = $datas->[$i];
cleanup
Yuki Kimoto authored on 2011-08-13
2857
    my $dbi = DBIx::Custom->new;
2858
    my $builder = $dbi->query_builder;
test cleanup
Yuki Kimoto authored on 2011-08-10
2859
    my $query = $builder->build_query($data->{source});
2860
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
2861
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
2862
}
2863

            
cleanup
Yuki Kimoto authored on 2011-08-13
2864
$dbi = DBIx::Custom->new;
2865
$builder = $dbi->query_builder;
2866
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
2867
    p => sub {
2868
        my @args = @_;
2869
        
2870
        my $expand    = "? $args[0] $args[1]";
2871
        my $columns = [2];
2872
        return [$expand, $columns];
2873
    }
2874
);
2875

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
2886
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
2887
    q => 'string'
2888
});
2889

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
2893
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
2894
   r => sub {} 
2895
});
2896

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
2900
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
2901
   s => sub { return ["a", ""]} 
2902
});
2903

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
2907
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
2908
    t => sub {return ["a", []]}
2909
);
2910

            
2911

            
cleanup
Yuki Kimoto authored on 2011-08-13
2912
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
2913
    a => sub {
2914
        return ["? ? ?", ['']];
2915
    }
2916
);
2917
eval{$builder->build_query("{a}")};
2918
like($@, qr/\QPlaceholder count/, "placeholder count is invalid");
2919

            
2920

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

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

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

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

            
2936
test 'variouse source';
2937
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d";
2938
$query = $builder->build_query($source);
2939
is($query->sql, 'a b = ? c { } { = ? } = ? d', "basic : 1");
2940

            
2941
$source = "abc";
2942
$query = $builder->build_query($source);
2943
is($query->sql, 'abc', "basic : 2");
2944

            
2945
$source = "{= a}";
2946
$query = $builder->build_query($source);
2947
is($query->sql, 'a = ?', "only tag");
2948

            
2949
$source = "000";
2950
$query = $builder->build_query($source);
2951
is($query->sql, '000', "contain 0 value");
2952

            
2953
$source = "a {= b} }";
2954
eval{$builder->build_query($source)};
2955
like($@, qr/unexpected "}"/, "error : 1");
2956

            
2957
$source = "a {= {}";
2958
eval{$builder->build_query($source)};
2959
like($@, qr/unexpected "{"/, "error : 2");
2960

            
2961
test 'select() wrap option';
2962
$dbi = DBIx::Custom->connect;
2963
eval { $dbi->execute("drop table $table1") };
2964
$dbi->execute($create_table1);
2965
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2966
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
2967
$rows = $dbi->select(
2968
    table => $table1,
2969
    column => $key1,
test cleanup
Yuki Kimoto authored on 2011-08-15
2970
    wrap => ["select * from (", ") t where $key1 = 1"]
cleanup test
Yuki Kimoto authored on 2011-08-15
2971
)->all;
2972
is_deeply($rows, [{$key1 => 1}]);
2973

            
2974
eval {
2975
$dbi->select(
2976
    table => $table1,
2977
    column => $key1,
2978
    wrap => 'select * from ('
2979
)
2980
};
2981
like($@, qr/array/);
2982

            
2983
test 'select() sqlfilter option';
2984
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
2985
$dbi->user_table_info($user_table_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
2986
eval { $dbi->execute("drop table $table1") };
2987
$dbi->execute($create_table1);
2988
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2989
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
2990
$rows = $dbi->select(
2991
    table => $table1,
2992
    column => $key1,
2993
    sqlfilter => sub {
2994
        my $sql = shift;
test cleanup
Yuki Kimoto authored on 2011-08-15
2995
        $sql = "select * from ( $sql ) t where $key1 = 1";
cleanup test
Yuki Kimoto authored on 2011-08-15
2996
        return $sql;
2997
    }
2998
)->all;
2999
is_deeply($rows, [{$key1 => 1}]);
3000

            
3001
test 'dbi method from model';
3002
$dbi = MyDBI9->connect;
3003
eval { $dbi->execute("drop table $table1") };
3004
$dbi->execute($create_table1);
3005
$dbi->setup_model;
3006
$model = $dbi->model($table1);
3007
eval{$model->execute("select * from $table1")};
3008
ok(!$@);
3009

            
3010
test 'column table option';
3011
$dbi = MyDBI9->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3012
$dbi->user_table_info($user_table_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3013
eval { $dbi->execute("drop table $table1") };
3014
$dbi->execute($create_table1);
3015
eval { $dbi->execute("drop table $table2") };
3016
$dbi->execute($create_table2);
3017
$dbi->setup_model;
3018
$dbi->execute("insert into $table1 ($key1, $key2) values (1, 2)");
3019
$dbi->execute("insert into $table2 ($key1, $key3) values (1, 4)");
3020
$model = $dbi->model($table1);
3021
$result = $model->select(
3022
    column => [
3023
        $model->column($table2, {alias => $table2_alias})
3024
    ],
3025
    where => {"$table2_alias.$key3" => 4}
3026
);
3027
is_deeply($result->one, 
3028
          {"$table2_alias.$key1" => 1, "$table2_alias.$key3" => 4});
3029

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

            
3040
$dbi->separator('-');
3041
$result = $model->select(
3042
    column => [
3043
        $model->column($table2, {alias => $table2_alias})
3044
    ],
3045
    where => {"$table2_alias.$key3" => 4}
3046
);
3047
is_deeply($result->one, 
3048
          {"$table2_alias-$key1" => 1, "$table2_alias-$key3" => 4});
3049

            
3050
test 'create_model';
3051
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3052
$dbi->user_table_info($user_table_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3053
eval { $dbi->execute("drop table $table1") };
3054
eval { $dbi->execute("drop table $table2") };
3055
$dbi->execute($create_table1);
3056
$dbi->execute($create_table2);
3057

            
3058
$dbi->create_model(
3059
    table => $table1,
3060
    join => [
3061
       "left outer join $table2 on $table1.$key1 = $table2.$key1"
3062
    ],
3063
    primary_key => [$key1]
3064
);
3065
$model2 = $dbi->create_model(
3066
    table => $table2
3067
);
3068
$dbi->create_model(
3069
    table => $table3,
3070
    filter => [
3071
        $key1 => {in => sub { uc $_[0] }}
3072
    ]
3073
);
3074
$dbi->setup_model;
3075
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3076
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
3077
$model = $dbi->model($table1);
3078
$result = $model->select(
3079
    column => [$model->mycolumn, $model->column($table2)],
3080
    where => {"$table1.$key1" => 1}
3081
);
3082
is_deeply($result->one,
3083
          {$key1 => 1, $key2 => 2, "$table2.$key1" => 1, "$table2.$key3" => 3});
3084
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3});
3085

            
3086
test 'model method';
3087
$dbi = DBIx::Custom->connect;
3088
eval { $dbi->execute("drop table $table2") };
3089
$dbi->execute($create_table2);
3090
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
3091
$model = $dbi->create_model(
3092
    table => $table2
3093
);
3094
$model->method(foo => sub { shift->select(@_) });
3095
is_deeply($model->foo->one, {$key1 => 1, $key3 => 3});
3096

            
3097
test 'update_param';
3098
$dbi = DBIx::Custom->connect;
3099
eval { $dbi->execute("drop table $table1") };
3100
$dbi->execute($create_table1_2);
3101
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3102
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3103

            
3104
$param = {$key2 => 11};
3105
$update_param = $dbi->update_param($param);
3106
$sql = <<"EOS";
3107
update $table1 $update_param
3108
where $key1 = 1
3109
EOS
3110
$dbi->execute($sql, param => $param);
3111
$result = $dbi->execute("select * from $table1 order by $key1", table => $table1);
3112
$rows   = $result->all;
3113
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
3114
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3115
                  "basic");
3116

            
3117

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

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

            
3137
$dbi = DBIx::Custom->connect;
3138
eval { $dbi->execute("drop table $table1") };
3139
$dbi->execute($create_table1_2);
3140
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3141
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3142

            
3143
$param = {$key2 => 11, $key3 => 33};
3144
$update_param = $dbi->update_param($param, {no_set => 1});
3145
$sql = <<"EOS";
3146
update $table1 set $update_param
3147
where $key1 = 1
3148
EOS
3149
$dbi->execute($sql, param => $param);
3150
$result = $dbi->execute("select * from $table1 order by $key1", table => $table1);
3151
$rows   = $result->all;
3152
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 33, $key4 => 4, $key5 => 5},
3153
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3154
                  "update param no_set");
3155

            
3156
            
3157
eval { $dbi->update_param({";" => 1}) };
3158
like($@, qr/not safety/);
3159

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3161
test 'update_param';
3162
$dbi = DBIx::Custom->connect;
3163
eval { $dbi->execute("drop table $table1") };
3164
$dbi->execute($create_table1_2);
3165
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3166
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
test cleanup
Yuki Kimoto authored on 2011-08-10
3167

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3168
$param = {$key2 => 11};
3169
$update_param = $dbi->assign_param($param);
3170
$sql = <<"EOS";
3171
update $table1 set $update_param
3172
where $key1 = 1
3173
EOS
3174
$dbi->execute($sql, param => $param, table => $table1);
3175
$result = $dbi->execute("select * from $table1 order by $key1");
3176
$rows   = $result->all;
3177
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
3178
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3179
                  "basic");
test cleanup
Yuki Kimoto authored on 2011-08-10
3180

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3181
test 'Model class';
3182
$dbi = MyDBI1->connect;
3183
eval { $dbi->execute("drop table $table1") };
3184
$dbi->execute($create_table1);
3185
$model = $dbi->model($table1);
3186
$model->insert({$key1 => 'a', $key2 => 'b'});
3187
is_deeply($model->list->all, [{$key1 => 'a', $key2 => 'b'}], 'basic');
3188
eval { $dbi->execute("drop table $table2") };
3189
$dbi->execute($create_table2);
3190
$model = $dbi->model($table2);
3191
$model->insert({$key1 => 'a'});
3192
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'basic');
3193
is($dbi->models->{$table1}, $dbi->model($table1));
3194
is($dbi->models->{$table2}, $dbi->model($table2));
3195

            
3196
$dbi = MyDBI4->connect;
3197
eval { $dbi->execute("drop table $table1") };
3198
$dbi->execute($create_table1);
3199
$model = $dbi->model($table1);
3200
$model->insert({$key1 => 'a', $key2 => 'b'});
3201
is_deeply($model->list->all, [{$key1 => 'a', $key2 => 'b'}], 'basic');
3202
eval { $dbi->execute("drop table $table2") };
3203
$dbi->execute($create_table2);
3204
$model = $dbi->model($table2);
3205
$model->insert({$key1 => 'a'});
3206
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'basic');
3207

            
3208
$dbi = MyDBI5->connect;
3209
eval { $dbi->execute("drop table $table1") };
3210
eval { $dbi->execute("drop table $table2") };
3211
$dbi->execute($create_table1);
3212
$dbi->execute($create_table2);
3213
$model = $dbi->model($table2);
3214
$model->insert({$key1 => 'a'});
3215
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'include all model');
3216
$dbi->insert(table => $table1, param => {$key1 => 1});
3217
$model = $dbi->model($table1);
3218
is_deeply($model->list->all, [{$key1 => 1, $key2 => undef}], 'include all model');
3219

            
3220
test 'primary_key';
3221
$dbi = MyDBI1->connect;
3222
$model = $dbi->model($table1);
3223
$model->primary_key([$key1, $key2]);
3224
is_deeply($model->primary_key, [$key1, $key2]);
3225

            
3226
test 'columns';
3227
$dbi = MyDBI1->connect;
3228
$model = $dbi->model($table1);
3229
$model->columns([$key1, $key2]);
3230
is_deeply($model->columns, [$key1, $key2]);
cleanup test
Yuki Kimoto authored on 2011-08-10
3231

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3232
test 'setup_model';
3233
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3234
$dbi->user_table_info($user_table_info);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3235
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3236
eval { $dbi->execute("drop table $table2") };
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
3237

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3238
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-15
3239
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-10
3240
$dbi->setup_model;
cleanup test
Yuki Kimoto authored on 2011-08-15
3241
is_deeply([sort @{$dbi->model($table1)->columns}], [$key1, $key2]);
3242
is_deeply([sort @{$dbi->model($table2)->columns}], [$key1, $key3]);
cleanup test
Yuki Kimoto authored on 2011-08-10
3243

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3244
test 'each_column';
3245
$dbi = DBIx::Custom->connect;
3246
eval { $dbi->execute("drop table ${q}table$p") };
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3247
eval { $dbi->execute("drop table $table1") };
3248
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3249
eval { $dbi->execute("drop table $table3") };
3250
$dbi->execute($create_table1_type);
cleanup test
Yuki Kimoto authored on 2011-08-10
3251
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-15
3252

            
3253
$infos = [];
3254
$dbi->each_column(sub {
3255
    my ($self, $table, $column, $cinfo) = @_;
3256
    
3257
    if ($table =~ /^table\d/i) {
3258
         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
3259
         push @$infos, $info;
3260
    }
3261
});
3262
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3263
is_deeply($infos, 
3264
    [
3265
        [$table1, $key1, $key1],
3266
        [$table1, $key2, $key2],
3267
        [$table2, $key1, $key1],
3268
        [$table2, $key3, $key3]
3269
    ]
3270
    
cleanup test
Yuki Kimoto authored on 2011-08-10
3271
);
cleanup test
Yuki Kimoto authored on 2011-08-16
3272

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3273
test 'each_table';
3274
$dbi = DBIx::Custom->connect;
3275
eval { $dbi->execute("drop table $table1") };
3276
eval { $dbi->execute("drop table $table2") };
3277
$dbi->execute($create_table2);
3278
$dbi->execute($create_table1_type);
cleanup test
Yuki Kimoto authored on 2011-08-10
3279

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3280
$infos = [];
3281
$dbi->each_table(sub {
3282
    my ($self, $table, $table_info) = @_;
3283
    
3284
    if ($table =~ /^table\d/i) {
3285
         my $info = [$table, $table_info->{TABLE_NAME}];
3286
         push @$infos, $info;
3287
    }
3288
});
3289
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3290
is_deeply($infos, 
3291
    [
3292
        [$table1, $table1],
3293
        [$table2, $table2],
3294
    ]
cleanup test
Yuki Kimoto authored on 2011-08-10
3295
);
3296

            
cleanup test
Yuki Kimoto authored on 2011-08-16
3297
$dbi = DBIx::Custom->connect;
3298
eval { $dbi->execute("drop table $table1") };
3299
eval { $dbi->execute("drop table $table2") };
3300
$dbi->execute($create_table2);
3301
$dbi->execute($create_table1_type);
3302

            
3303
$infos = [];
3304
$dbi->user_table_info($user_table_info);
3305
$dbi->each_table(sub {
3306
    my ($self, $table, $table_info) = @_;
3307
    
3308
    if ($table =~ /^table\d/i) {
3309
         my $info = [$table, $table_info->{TABLE_NAME}];
3310
         push @$infos, $info;
3311
    }
3312
});
3313
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3314
is_deeply($infos, 
3315
    [
3316
        [$table1, $table1],
3317
        [$table2, $table2],
3318
        [$table3, $table3],
3319
    ]
3320
);
3321

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3322
test 'type_rule into';
3323
eval { $dbi->execute("drop table $table1") };
3324
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3325
$user_column_info = $dbi->get_column_info(exclude_table => $dbi->exclude_table);
3326

            
3327

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3328
$dbi = DBIx::Custom->connect;
3329
eval { $dbi->execute("drop table $table1") };
3330
$dbi->execute($create_table1_type);
3331

            
cleanup
Yuki Kimoto authored on 2011-08-16
3332
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3333
$dbi->type_rule(
3334
    into1 => {
3335
        $date_typename => sub { '2010-' . $_[0] }
3336
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3337
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3338
$dbi->insert({$key1 => '01-01'}, table => $table1);
3339
$result = $dbi->select(table => $table1);
3340
like($result->one->{$key1}, qr/^2010-01-01/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3341

            
3342
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3343
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3344
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3345
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3346
$dbi->type_rule(
3347
    into1 => [
3348
         [$date_typename, $datetime_typename] => sub {
3349
            my $value = shift;
3350
            $value =~ s/02/03/g;
3351
            return $value;
3352
         }
3353
    ]
3354
);
3355
$dbi->insert({$key1 => '2010-01-02', $key2 => '2010-01-01 01:01:02'}, table => $table1);
3356
$result = $dbi->select(table => $table1);
3357
$row = $result->one;
3358
like($row->{$key1}, qr/^2010-01-03/);
3359
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3360

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3361
$dbi = DBIx::Custom->connect;
3362
eval { $dbi->execute("drop table $table1") };
3363
$dbi->execute($create_table1_type);
3364
$dbi->insert({$key1 => '2010-01-03', $key2 => '2010-01-01 01:01:03'}, table => $table1);
cleanup
Yuki Kimoto authored on 2011-08-16
3365
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3366
$dbi->type_rule(
3367
    into1 => [
3368
        [$date_typename, $datetime_typename] => sub {
3369
            my $value = shift;
3370
            $value =~ s/02/03/g;
3371
            return $value;
3372
        }
3373
    ]
cleanup test
Yuki Kimoto authored on 2011-08-10
3374
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3375
$result = $dbi->execute(
3376
    "select * from $table1 where $key1 = :$key1 and $key2 = :$table1.$key2",
3377
    param => {$key1 => '2010-01-03', "$table1.$key2" => '2010-01-01 01:01:02'}
cleanup test
Yuki Kimoto authored on 2011-08-10
3378
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3379
$row = $result->one;
3380
like($row->{$key1}, qr/^2010-01-03/);
3381
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
3382

            
3383
$dbi = DBIx::Custom->connect;
3384
eval { $dbi->execute("drop table $table1") };
3385
$dbi->execute($create_table1_type);
3386
$dbi->insert({$key1 => '2010-01-03', $key2 => '2010-01-01 01:01:03'}, table => $table1);
cleanup
Yuki Kimoto authored on 2011-08-16
3387
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3388
$dbi->type_rule(
3389
    into1 => [
3390
        [$date_typename, $datetime_typename] => sub {
3391
            my $value = shift;
3392
            $value =~ s/02/03/g;
3393
            return $value;
3394
        }
cleanup test
Yuki Kimoto authored on 2011-08-10
3395
    ]
3396
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3397
$result = $dbi->execute(
3398
    "select * from $table1 where $key1 = :$key1 and $key2 = :$table1.$key2",
3399
    param => {$key1 => '2010-01-02', "$table1.$key2" => '2010-01-01 01:01:02'},
3400
    table => $table1
3401
);
3402
$row = $result->one;
3403
like($row->{$key1}, qr/^2010-01-03/);
3404
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3405

            
3406
$dbi = DBIx::Custom->connect;
3407
eval { $dbi->execute("drop table $table1") };
3408
$dbi->execute($create_table1_type);
3409
$dbi->register_filter(convert => sub {
3410
    my $value = shift || '';
3411
    $value =~ s/02/03/;
3412
    return $value;
3413
});
cleanup
Yuki Kimoto authored on 2011-08-16
3414
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3415
$dbi->type_rule(
3416
    from1 => {
3417
        $date_datatype => 'convert',
3418
    },
3419
    into1 => {
3420
        $date_typename => 'convert',
3421
    }
3422
);
3423
$dbi->insert({$key1 => '2010-02-02'}, table => $table1);
3424
$result = $dbi->select(table => $table1);
3425
like($result->fetch->[0], qr/^2010-03-03/);
3426

            
3427
test 'type_rule and filter order';
3428
$dbi = DBIx::Custom->connect;
3429
eval { $dbi->execute("drop table $table1") };
3430
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3431
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3432
$dbi->type_rule(
3433
    into1 => {
3434
        $date_typename => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3435
    },
3436
    into2 => {
3437
        $date_typename => sub { my $v = shift || ''; $v =~ s/5/6/; return $v }
3438
    },
3439
    from1 => {
3440
        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/7/; return $v }
3441
    },
3442
    from2 => {
3443
        $date_datatype => sub { my $v = shift || ''; $v =~ s/7/8/; return $v }
3444
    }
3445
);
3446
$dbi->insert({$key1 => '2010-01-03'}, 
3447
  table => $table1, filter => {$key1 => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }});
3448
$result = $dbi->select(table => $table1);
3449
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/8/9/; return $v });
3450
like($result->fetch_first->[0], qr/^2010-01-09/);
3451

            
3452

            
3453
$dbi = DBIx::Custom->connect;
3454
eval { $dbi->execute("drop table $table1") };
3455
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3456
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3457
$dbi->type_rule(
3458
    from1 => {
3459
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3460
    },
3461
    from2 => {
3462
        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3463
    },
3464
);
3465
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3466
$result = $dbi->select(table => $table1);
cleanup
Yuki Kimoto authored on 2011-08-16
3467
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3468
$result->type_rule(
3469
    from1 => {
3470
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/6/; return $v }
3471
    },
3472
    from2 => {
3473
        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/8/; return $v }
3474
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3475
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3476
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/8/9/; return $v });
3477
like($result->fetch_first->[0], qr/^2010-01-09/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3478

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3479
test 'type_rule_off';
cleanup test
Yuki Kimoto authored on 2011-08-10
3480
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-15
3481
eval { $dbi->execute("drop table $table1") };
3482
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3483
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3484
$dbi->type_rule(
3485
    from1 => {
3486
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3487
    },
3488
    into1 => {
3489
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3490
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3491
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3492
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule_off => 1);
3493
$result = $dbi->select(table => $table1, type_rule_off => 1);
3494
like($result->type_rule_off->fetch->[0], qr/^2010-01-03/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3495

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3496
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3497
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3498
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3499
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3500
$dbi->type_rule(
3501
    from1 => {
3502
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3503
    },
3504
    into1 => {
3505
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3506
    }
3507
);
3508
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule_off => 1);
3509
$result = $dbi->select(table => $table1, type_rule_off => 1);
3510
like($result->one->{$key1}, qr/^2010-01-04/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3511

            
3512
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3513
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3514
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3515
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3516
$dbi->type_rule(
3517
    from1 => {
3518
        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3519
    },
3520
    into1 => {
3521
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3522
    }
3523
);
3524
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3525
$result = $dbi->select(table => $table1);
3526
like($result->one->{$key1}, qr/^2010-01-05/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3527

            
3528
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3529
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3530
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3531
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3532
$dbi->type_rule(
3533
    from1 => {
3534
        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3535
    },
3536
    into1 => {
3537
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3538
    }
3539
);
3540
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3541
$result = $dbi->select(table => $table1);
3542
like($result->fetch->[0], qr/2010-01-05/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3543

            
3544
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3545
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3546
$dbi->execute($create_table1_type);
3547
$dbi->register_filter(ppp => sub { my $v = shift || ''; $v =~ s/3/4/; return $v });
cleanup
Yuki Kimoto authored on 2011-08-16
3548
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3549
$dbi->type_rule(
3550
    into1 => {
3551
        $date_typename => 'ppp'
3552
    }
3553
);
3554
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3555
$result = $dbi->select(table => $table1);
3556
like($result->one->{$key1}, qr/^2010-01-04/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3557

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3558
eval{$dbi->type_rule(
3559
    into1 => {
3560
        $date_typename => 'pp'
3561
    }
3562
)};
3563
like($@, qr/not registered/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3564

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3565
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3566
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3567
$dbi->execute($create_table1_type);
cleanup test
Yuki Kimoto authored on 2011-08-10
3568
eval {
cleanup test
Yuki Kimoto authored on 2011-08-15
3569
    $dbi->type_rule(
3570
        from1 => {
3571
            Date => sub { $_[0] * 2 },
3572
        }
cleanup test
Yuki Kimoto authored on 2011-08-10
3573
    );
3574
};
cleanup test
Yuki Kimoto authored on 2011-08-15
3575
like($@, qr/lower/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3576

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3577
eval {
3578
    $dbi->type_rule(
3579
        into1 => {
3580
            Date => sub { $_[0] * 2 },
3581
        }
3582
    );
3583
};
3584
like($@, qr/lower/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3585

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

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

            
3624
$result = $dbi->select(table => $table1);
3625
$result->type_rule(
3626
    from1 => {
3627
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3628
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3629
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3630
$row = $result->one;
3631
like($row->{$key1}, qr/2010-01-05/);
3632
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3633

            
3634
$result = $dbi->select(table => $table1);
3635
$result->type_rule(
3636
    from1 => {
3637
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3638
    }
3639
);
3640
$row = $result->one;
3641
like($row->{$key1}, qr/2010-01-05/);
3642
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3643

            
3644
$result = $dbi->select(table => $table1);
3645
$result->type_rule(
3646
    from1 => [$date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }]
3647
);
3648
$row = $result->one;
3649
like($row->{$key1}, qr/2010-01-05/);
3650
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3651

            
3652
$dbi->register_filter(five => sub { my $v = shift || ''; $v =~ s/3/5/; return $v });
3653
$result = $dbi->select(table => $table1);
3654
$result->type_rule(
3655
    from1 => [$date_datatype => 'five']
3656
);
3657
$row = $result->one;
3658
like($row->{$key1}, qr/^2010-01-05/);
3659
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
3660

            
3661
$result = $dbi->select(table => $table1);
3662
$result->type_rule(
3663
    from1 => [$date_datatype => undef]
3664
);
3665
$row = $result->one;
3666
like($row->{$key1}, qr/^2010-01-03/);
3667
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3668

            
3669
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3670
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3671
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3672
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3673
$dbi->type_rule(
3674
    from1 => {
3675
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v },
3676
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
3677
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3678
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3679
$result = $dbi->select(table => $table1);
3680
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/4/5/; return $v });
3681
like($result->one->{$key1}, qr/^2010-01-05/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3682

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

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

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

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

            
added memory leak check test
Yuki Kimoto authored on 2011-08-15
3769
test 'join';
3770
$dbi = DBIx::Custom->connect;
3771
eval { $dbi->execute("drop table $table1") };
3772
$dbi->execute($create_table1);
3773
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3774
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
3775
eval { $dbi->execute("drop table $table2") };
3776
$dbi->execute($create_table2);
3777
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3778
eval { $dbi->execute("drop table $table3") };
3779
$dbi->execute("create table $table3 ($key3 int, $key4 int)");
3780
$dbi->insert(table => $table3, param => {$key3 => 5, $key4 => 4});
3781
$rows = $dbi->select(
3782
    table => $table1,
3783
    column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
3784
    where   => {"$table1.$key2" => 2},
3785
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1"]
3786
)->all;
3787
is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}]);
3788

            
3789
$rows = $dbi->select(
3790
    table => $table1,
3791
    where   => {$key1 => 1},
3792
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1"]
3793
)->all;
3794
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
3795

            
3796
eval {
3797
    $rows = $dbi->select(
3798
        table => $table1,
3799
        column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
3800
        where   => {"$table1.$key2" => 2},
3801
        join  => {"$table1.$key1" => "$table2.$key1"}
3802
    );
3803
};
3804
like ($@, qr/array/);
3805

            
3806
$rows = $dbi->select(
3807
    table => $table1,
3808
    where   => {$key1 => 1},
3809
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
3810
              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
3811
)->all;
3812
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
3813

            
3814
$rows = $dbi->select(
3815
    column => "$table3.$key4 as ${table3}__$key4",
3816
    table => $table1,
3817
    where   => {"$table1.$key1" => 1},
3818
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
3819
              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
3820
)->all;
3821
is_deeply($rows, [{"${table3}__$key4" => 4}]);
3822

            
3823
$rows = $dbi->select(
3824
    column => "$table1.$key1 as ${table1}__$key1",
3825
    table => $table1,
3826
    where   => {"$table3.$key4" => 4},
3827
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
3828
              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
3829
)->all;
3830
is_deeply($rows, [{"${table1}__$key1" => 1}]);
3831

            
3832
$dbi = DBIx::Custom->connect;
3833
eval { $dbi->execute("drop table $table1") };
3834
$dbi->execute($create_table1);
3835
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3836
eval { $dbi->execute("drop table $table2") };
3837
$dbi->execute($create_table2);
3838
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3839
$rows = $dbi->select(
3840
    table => $table1,
3841
    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",
3842
    where   => {"$table1.$key2" => 2},
3843
    join  => ["left outer join ${q}$table2$p on ${q}$table1$p.${q}$key1$p = ${q}$table2$p.${q}$key1$p"],
3844
)->all;
3845
is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}],
3846
          'quote');
3847

            
3848

            
3849
$dbi = DBIx::Custom->connect;
3850
eval { $dbi->execute("drop table $table1") };
3851
$dbi->execute($create_table1);
3852
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3853
$sql = <<"EOS";
3854
left outer join (
3855
  select * from $table1 t1
3856
  where t1.$key2 = (
3857
    select max(t2.$key2) from $table1 t2
3858
    where t1.$key1 = t2.$key1
3859
  )
3860
) $table3 on $table1.$key1 = $table3.$key1
3861
EOS
3862
$join = [$sql];
3863
$rows = $dbi->select(
3864
    table => $table1,
3865
    column => "$table3.$key1 as ${table3}__$key1",
3866
    join  => $join
3867
)->all;
3868
is_deeply($rows, [{"${table3}__$key1" => 1}]);
3869

            
3870
$dbi = DBIx::Custom->connect;
3871
eval { $dbi->execute("drop table $table1") };
3872
eval { $dbi->execute("drop table $table2") };
3873
$dbi->execute($create_table1);
3874
$dbi->execute($create_table2);
3875
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3876
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4});
3877
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3878
$result = $dbi->select(
3879
    table => $table1,
3880
    join => [
3881
        "left outer join $table2 on $table2.$key2 = '4' and $table1.$key1 = $table2.$key1"
3882
    ]
3883
);
3884
is_deeply($result->all, [{$key1 => 1, $key2 => 2}]);
3885
$result = $dbi->select(
3886
    table => $table1,
3887
    column => [{$table2 => [$key3]}],
3888
    join => [
3889
        "left outer join $table2 on $table2.$key3 = '4' and $table1.$key1 = $table2.$key1"
3890
    ]
3891
);
3892
is_deeply($result->all, [{"$table2.$key3" => 4}]);
3893
$result = $dbi->select(
3894
    table => $table1,
3895
    column => [{$table2 => [$key3]}],
3896
    join => [
3897
        "left outer join $table2 on $table1.$key1 = $table2.$key1 and $table2.$key3 = '4'"
3898
    ]
3899
);
3900
is_deeply($result->all, [{"$table2.$key3" => 4}]);
3901

            
3902
$dbi = DBIx::Custom->connect;
3903
eval { $dbi->execute("drop table $table1") };
3904
eval { $dbi->execute("drop table $table2") };
3905
$dbi->execute($create_table1);
3906
$dbi->execute($create_table2);
3907
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3908
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4});
3909
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3910
$result = $dbi->select(
3911
    table => $table1,
3912
    column => [{$table2 => [$key3]}],
3913
    join => [
3914
        {
3915
            clause => "left outer join $table2 on $table2.$key3 = '4' and $table1.$key1 = $table2.$key1",
3916
            table => [$table1, $table2]
3917
        }
3918
    ]
3919
);
3920
is_deeply($result->all, [{"$table2.$key3" => 4}]);
3921

            
3922
test 'columns';
3923
$dbi = MyDBI1->connect;
3924
$model = $dbi->model($table1);
3925

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

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