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

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

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

            
14
plan 'no_plan';
15

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

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

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

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

            
86
    use strict;
87
    use warnings;
88

            
89
    use base 'DBIx::Custom';
90

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

            
102
    package MyModel2::Base1;
103

            
104
    use strict;
105
    use warnings;
106

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

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

            
111
    use strict;
112
    use warnings;
113

            
114
    use base 'MyModel2::Base1';
115

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

            
122
    sub list { shift->select; }
123

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

            
126
    use strict;
127
    use warnings;
128

            
129
    use base 'MyModel2::Base1';
130

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

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

            
139
    package MyModel2::TABLE1;
140

            
141
    use strict;
142
    use warnings;
143

            
144
    use base 'MyModel2::Base1';
145

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

            
152
    sub list { shift->select; }
153

            
154
    package MyModel2::TABLE2;
155

            
156
    use strict;
157
    use warnings;
158

            
159
    use base 'MyModel2::Base1';
160

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

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

            
172
    use strict;
173
    use warnings;
174

            
175
    use base 'DBIx::Custom';
176

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
648

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

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

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

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

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

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

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

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

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

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

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

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

            
744

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
882

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

            
888
$dbi->begin_work;
889

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

            
896
$dbi->rollback if $@;
897

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

            
902
$dbi->begin_work;
903

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

            
909
$dbi->commit unless $@;
910

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1509

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1707
$dbi->apply_filter(
1708

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2039

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2235

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

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

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

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

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

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

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

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

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

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

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

            
2372

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

            
2388

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

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

            
2409

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

            
2428

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

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

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

            
2468

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

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

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

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

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

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

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

            
2564

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

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

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

            
2591

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

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

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

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

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2646
test 'map_param';
2647
$dbi = DBIx::Custom->connect;
2648
$param = $dbi->map_param(
2649
    {id => 1, author => 'Ken', price => 1900},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2650
    id => "$table1.id",
2651
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2652
    price => ["$table1.price", {if => sub { $_[0] eq 1900 }}]
cleanup test
Yuki Kimoto authored on 2011-08-10
2653
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2654
is_deeply($param, {"$table1.id" => 1, "$table1.author" => '%Ken%',
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2655
  "$table1.price" => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
2656

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2657
$param = $dbi->map_param(
2658
    {id => 0, author => 0, price => 0},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2659
    id => "$table1.id",
2660
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2661
    price => ["$table1.price", sub { '%' . $_[0] . '%' },
cleanup test
Yuki Kimoto authored on 2011-08-10
2662
      {if => sub { $_[0] eq 0 }}]
2663
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2664
is_deeply($param, {"$table1.id" => 0, "$table1.author" => '%0%', "$table1.price" => '%0%'});
test cleanup
Yuki Kimoto authored on 2011-08-10
2665

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2666
$param = $dbi->map_param(
2667
    {id => '', author => '', price => ''},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2668
    id => "$table1.id",
2669
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2670
    price => ["$table1.price", sub { '%' . $_[0] . '%' },
cleanup test
Yuki Kimoto authored on 2011-08-10
2671
      {if => sub { $_[0] eq 1 }}]
2672
);
2673
is_deeply($param, {});
test cleanup
Yuki Kimoto authored on 2011-08-10
2674

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2675
$param = $dbi->map_param(
2676
    {id => undef, author => undef, price => undef},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2677
    id => "$table1.id",
2678
    price => ["$table1.price", {if => 'exists'}]
cleanup test
Yuki Kimoto authored on 2011-08-10
2679
);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2680
is_deeply($param, {"$table1.price" => undef});
test cleanup
Yuki Kimoto authored on 2011-08-10
2681

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2682
$param = $dbi->map_param(
2683
    {price => 'a'},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2684
    id => ["$table1.id", {if => 'exists'}],
2685
    price => ["$table1.price", sub { '%' . $_[0] }, {if => 'exists'}]
cleanup test
Yuki Kimoto authored on 2011-08-10
2686
);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2687
is_deeply($param, {"$table1.price" => '%a'});
test cleanup
Yuki Kimoto authored on 2011-08-10
2688

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2689
test 'order';
2690
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2691
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2692
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2693
$dbi->insert({$key1 => 1, $key2 => 1}, table => $table1);
2694
$dbi->insert({$key1 => 1, $key2 => 3}, table => $table1);
2695
$dbi->insert({$key1 => 2, $key2 => 2}, table => $table1);
2696
$dbi->insert({$key1 => 2, $key2 => 4}, table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2697
my $order = $dbi->order;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2698
$order->prepend($key1, "$key2 desc");
2699
$result = $dbi->select(table => $table1, append => $order);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2700
is_deeply($result->all, [{$key1 => 1, $key2 => 3}, {$key1 => 1, $key2 => 1},
2701
  {$key1 => 2, $key2 => 4}, {$key1 => 2, $key2 => 2}]);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2702
$order->prepend("$key1 desc");
2703
$result = $dbi->select(table => $table1, append => $order);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2704
is_deeply($result->all, [{$key1 => 2, $key2 => 4}, {$key1 => 2, $key2 => 2},
2705
  {$key1 => 1, $key2 => 3}, {$key1 => 1, $key2 => 1}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2706

            
2707
$order = $dbi->order;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2708
$order->prepend(["$table1-$key1"], ["$table1-$key2", 'desc']);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2709
$result = $dbi->select(table => $table1,
2710
  column => [[$key1 => "$table1-$key1"], [$key2 => "$table1-$key2"]],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2711
  append => $order);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2712
is_deeply($result->all, [{"$table1-$key1" => 1, "$table1-$key2" => 3},
2713
  {"$table1-$key1" => 1, "$table1-$key2" => 1},
2714
  {"$table1-$key1" => 2, "$table1-$key2" => 4},
2715
  {"$table1-$key1" => 2, "$table1-$key2" => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2716

            
2717
test 'tag_parse';
2718
$dbi = DBIx::Custom->connect;
2719
$dbi->tag_parse(0);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2720
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2721
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2722
$dbi->insert({$key1 => 1, $key2 => 1}, table => $table1);
2723
eval {$dbi->execute("select * from $table1 where {= $key1}", {$key1 => 1})};
test cleanup
Yuki Kimoto authored on 2011-08-10
2724
ok($@);
2725

            
2726
test 'last_sql';
2727
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2728
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2729
$dbi->execute($create_table1);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2730
$dbi->execute("select * from $table1");
cleanup test
Yuki Kimoto authored on 2011-08-15
2731
is($dbi->last_sql, "select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
2732

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

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

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

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

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

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2764
$source = "select * from $table1 where :$table1.${key1}{=} and :$table1.${key2}{=}";
test cleanup
Yuki Kimoto authored on 2011-08-10
2765
$result = $dbi->execute(
2766
    $source,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2767
    param => {"$table1.$key1" => 1, "$table1.$key2" => 1},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2768
    filter => {"$table1.$key2" => sub { $_[0] * 2 }}
test cleanup
Yuki Kimoto authored on 2011-08-10
2769
);
2770
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2771
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2772

            
2773
test 'high perfomance way';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2774
$dbi->execute("drop table $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
2775
$dbi->execute($create_table1_highperformance);
2776
$rows = [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2777
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
2778
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
2779
];
2780
{
2781
    my $query;
2782
    foreach my $row (@$rows) {
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2783
      $query ||= $dbi->insert($row, table => $table1, query => 1);
2784
      $dbi->execute($query, $row, filter => {$key7 => sub { $_[0] * 2 }});
test cleanup
Yuki Kimoto authored on 2011-08-10
2785
    }
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2786
    is_deeply($dbi->select(table => $table1)->all,
test cleanup
Yuki Kimoto authored on 2011-08-10
2787
      [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2788
          {$key7 => 2, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
2789
          {$key7 => 2, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
2790
      ]
2791
    );
2792
}
2793

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2794
$dbi->execute("drop table $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
2795
$dbi->execute($create_table1_highperformance);
2796
$rows = [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2797
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
2798
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
2799
];
2800
{
2801
    my $query;
2802
    my $sth;
2803
    foreach my $row (@$rows) {
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2804
      $query ||= $dbi->insert($row, table => $table1, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2805
      $sth ||= $query->sth;
2806
      $sth->execute(map { $row->{$_} } sort keys %$row);
2807
    }
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2808
    is_deeply($dbi->select(table => $table1)->all,
test cleanup
Yuki Kimoto authored on 2011-08-10
2809
      [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2810
          {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
2811
          {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
2812
      ]
2813
    );
2814
}
2815

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
2816
$dbi->execute("drop table $table1");
2817
$dbi->execute($create_table1_highperformance);
2818
$rows = [
2819
    {$key7 => 10, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 5},
2820
    {$key7 => 11, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6},
2821
];
2822
{
2823
    $model = $dbi->create_model(table => $table1, primary_key => $key1);
2824
    my $query;
2825
    foreach my $row (@$rows) {
2826
      $query ||= $model->insert($row, id => 1, query => 1);
2827
      $model->execute($query, $row, id => 1, filter => {$key7 => sub { $_[0] * 2 }});
2828
    }
2829
    is_deeply($dbi->select(table => $table1, append => 'order by key2')->all,
2830
      [
2831
          {$key7 => 20, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 5, $key1 => 1},
2832
          {$key7 => 22, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 1},
2833
      ]
2834
    );
2835
}
2836

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2837
test 'result';
2838
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2839
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2840
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2841
$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1);
2842
$dbi->insert({$key1 => 3, $key2 => 4}, table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2843

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2844
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2845
@rows = ();
2846
while (my $row = $result->fetch) {
2847
    push @rows, [@$row];
2848
}
2849
is_deeply(\@rows, [[1, 2], [3, 4]]);
2850

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2851
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2852
@rows = ();
2853
while (my $row = $result->fetch_hash) {
2854
    push @rows, {%$row};
2855
}
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2856
is_deeply(\@rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2857

            
2858
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2859
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2860
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2861
$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1);
2862
$dbi->insert({$key1 => 3, $key2 => 4}, table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2863

            
2864
test 'fetch_all';
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2865
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2866
$rows = $result->fetch_all;
2867
is_deeply($rows, [[1, 2], [3, 4]]);
2868

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

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

            
2877
$rows = $result->fetch_all;
2878
is_deeply($rows, [[3, 2], [9, 4]], "array");
2879

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

            
2886
test "query_builder";
2887
$datas = [
2888
    # Basic tests
2889
    {   name            => 'placeholder basic',
2890
        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
cleanup test
Yuki Kimoto authored on 2011-08-15
2891
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?",
test cleanup
Yuki Kimoto authored on 2011-08-10
2892
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
2893
    },
2894
    {
2895
        name            => 'placeholder in',
cleanup test
Yuki Kimoto authored on 2011-08-15
2896
        source            => "{in k1 3}",
2897
        sql_expected    => "k1 in (?, ?, ?)",
test cleanup
Yuki Kimoto authored on 2011-08-10
2898
        columns_expected   => [qw/k1 k1 k1/]
2899
    },
2900
    
2901
    # Table name
2902
    {
2903
        name            => 'placeholder with table name',
2904
        source            => "{= a.k1} {= a.k2}",
cleanup test
Yuki Kimoto authored on 2011-08-15
2905
        sql_expected    => "a.k1 = ? a.k2 = ?",
test cleanup
Yuki Kimoto authored on 2011-08-10
2906
        columns_expected  => [qw/a.k1 a.k2/]
2907
    },
2908
    {   
2909
        name            => 'placeholder in with table name',
2910
        source            => "{in a.k1 2} {in b.k2 2}",
cleanup test
Yuki Kimoto authored on 2011-08-15
2911
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?)",
test cleanup
Yuki Kimoto authored on 2011-08-10
2912
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
2913
    },
2914
    {
2915
        name            => 'not contain tag',
2916
        source            => "aaa",
cleanup test
Yuki Kimoto authored on 2011-08-15
2917
        sql_expected    => "aaa",
test cleanup
Yuki Kimoto authored on 2011-08-10
2918
        columns_expected  => [],
2919
    }
2920
];
2921

            
2922
for (my $i = 0; $i < @$datas; $i++) {
2923
    my $data = $datas->[$i];
cleanup
Yuki Kimoto authored on 2011-08-13
2924
    my $dbi = DBIx::Custom->new;
2925
    my $builder = $dbi->query_builder;
test cleanup
Yuki Kimoto authored on 2011-08-10
2926
    my $query = $builder->build_query($data->{source});
2927
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
2928
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
2929
}
2930

            
cleanup
Yuki Kimoto authored on 2011-08-13
2931
$dbi = DBIx::Custom->new;
2932
$builder = $dbi->query_builder;
2933
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
2934
    p => sub {
2935
        my @args = @_;
2936
        
2937
        my $expand    = "? $args[0] $args[1]";
2938
        my $columns = [2];
2939
        return [$expand, $columns];
2940
    }
2941
);
2942

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
2953
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
2954
    q => 'string'
2955
});
2956

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
2960
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
2961
   r => sub {} 
2962
});
2963

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
2967
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
2968
   s => sub { return ["a", ""]} 
2969
});
2970

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
2974
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
2975
    t => sub {return ["a", []]}
2976
);
2977

            
2978

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

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

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

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

            
2994
test 'variouse source';
2995
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d";
2996
$query = $builder->build_query($source);
2997
is($query->sql, 'a b = ? c { } { = ? } = ? d', "basic : 1");
2998

            
2999
$source = "abc";
3000
$query = $builder->build_query($source);
3001
is($query->sql, 'abc', "basic : 2");
3002

            
3003
$source = "{= a}";
3004
$query = $builder->build_query($source);
3005
is($query->sql, 'a = ?', "only tag");
3006

            
3007
$source = "000";
3008
$query = $builder->build_query($source);
3009
is($query->sql, '000', "contain 0 value");
3010

            
3011
$source = "a {= b} }";
3012
eval{$builder->build_query($source)};
3013
like($@, qr/unexpected "}"/, "error : 1");
3014

            
3015
$source = "a {= {}";
3016
eval{$builder->build_query($source)};
3017
like($@, qr/unexpected "{"/, "error : 2");
3018

            
3019
test 'select() wrap option';
3020
$dbi = DBIx::Custom->connect;
3021
eval { $dbi->execute("drop table $table1") };
3022
$dbi->execute($create_table1);
3023
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3024
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
3025
$rows = $dbi->select(
3026
    table => $table1,
3027
    column => $key1,
test cleanup
Yuki Kimoto authored on 2011-08-15
3028
    wrap => ["select * from (", ") t where $key1 = 1"]
cleanup test
Yuki Kimoto authored on 2011-08-15
3029
)->all;
3030
is_deeply($rows, [{$key1 => 1}]);
3031

            
3032
eval {
3033
$dbi->select(
3034
    table => $table1,
3035
    column => $key1,
3036
    wrap => 'select * from ('
3037
)
3038
};
3039
like($@, qr/array/);
3040

            
3041
test 'select() sqlfilter option';
3042
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3043
$dbi->user_table_info($user_table_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3044
eval { $dbi->execute("drop table $table1") };
3045
$dbi->execute($create_table1);
3046
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3047
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
3048
$rows = $dbi->select(
3049
    table => $table1,
3050
    column => $key1,
3051
    sqlfilter => sub {
3052
        my $sql = shift;
test cleanup
Yuki Kimoto authored on 2011-08-15
3053
        $sql = "select * from ( $sql ) t where $key1 = 1";
cleanup test
Yuki Kimoto authored on 2011-08-15
3054
        return $sql;
3055
    }
3056
)->all;
3057
is_deeply($rows, [{$key1 => 1}]);
3058

            
3059
test 'dbi method from model';
3060
$dbi = MyDBI9->connect;
3061
eval { $dbi->execute("drop table $table1") };
3062
$dbi->execute($create_table1);
3063
$dbi->setup_model;
3064
$model = $dbi->model($table1);
3065
eval{$model->execute("select * from $table1")};
3066
ok(!$@);
3067

            
3068
test 'column table option';
3069
$dbi = MyDBI9->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3070
$dbi->user_table_info($user_table_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3071
eval { $dbi->execute("drop table $table1") };
3072
$dbi->execute($create_table1);
3073
eval { $dbi->execute("drop table $table2") };
3074
$dbi->execute($create_table2);
3075
$dbi->setup_model;
3076
$dbi->execute("insert into $table1 ($key1, $key2) values (1, 2)");
3077
$dbi->execute("insert into $table2 ($key1, $key3) values (1, 4)");
3078
$model = $dbi->model($table1);
3079
$result = $model->select(
3080
    column => [
3081
        $model->column($table2, {alias => $table2_alias})
3082
    ],
3083
    where => {"$table2_alias.$key3" => 4}
3084
);
3085
is_deeply($result->one, 
3086
          {"$table2_alias.$key1" => 1, "$table2_alias.$key3" => 4});
3087

            
3088
$dbi->separator('__');
3089
$result = $model->select(
3090
    column => [
3091
        $model->column($table2, {alias => $table2_alias})
3092
    ],
3093
    where => {"$table2_alias.$key3" => 4}
3094
);
3095
is_deeply($result->one, 
3096
          {"${table2_alias}__$key1" => 1, "${table2_alias}__$key3" => 4});
3097

            
3098
$dbi->separator('-');
3099
$result = $model->select(
3100
    column => [
3101
        $model->column($table2, {alias => $table2_alias})
3102
    ],
3103
    where => {"$table2_alias.$key3" => 4}
3104
);
3105
is_deeply($result->one, 
3106
          {"$table2_alias-$key1" => 1, "$table2_alias-$key3" => 4});
3107

            
3108
test 'create_model';
3109
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3110
$dbi->user_table_info($user_table_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3111
eval { $dbi->execute("drop table $table1") };
3112
eval { $dbi->execute("drop table $table2") };
3113
$dbi->execute($create_table1);
3114
$dbi->execute($create_table2);
3115

            
3116
$dbi->create_model(
3117
    table => $table1,
3118
    join => [
3119
       "left outer join $table2 on $table1.$key1 = $table2.$key1"
3120
    ],
3121
    primary_key => [$key1]
3122
);
3123
$model2 = $dbi->create_model(
3124
    table => $table2
3125
);
3126
$dbi->create_model(
3127
    table => $table3,
3128
    filter => [
3129
        $key1 => {in => sub { uc $_[0] }}
3130
    ]
3131
);
3132
$dbi->setup_model;
3133
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3134
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
3135
$model = $dbi->model($table1);
3136
$result = $model->select(
3137
    column => [$model->mycolumn, $model->column($table2)],
3138
    where => {"$table1.$key1" => 1}
3139
);
3140
is_deeply($result->one,
3141
          {$key1 => 1, $key2 => 2, "$table2.$key1" => 1, "$table2.$key3" => 3});
3142
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3});
3143

            
3144
test 'model method';
3145
$dbi = DBIx::Custom->connect;
3146
eval { $dbi->execute("drop table $table2") };
3147
$dbi->execute($create_table2);
3148
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
3149
$model = $dbi->create_model(
3150
    table => $table2
3151
);
3152
$model->method(foo => sub { shift->select(@_) });
3153
is_deeply($model->foo->one, {$key1 => 1, $key3 => 3});
3154

            
3155
test 'update_param';
3156
$dbi = DBIx::Custom->connect;
3157
eval { $dbi->execute("drop table $table1") };
3158
$dbi->execute($create_table1_2);
3159
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3160
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3161

            
3162
$param = {$key2 => 11};
3163
$update_param = $dbi->update_param($param);
3164
$sql = <<"EOS";
3165
update $table1 $update_param
3166
where $key1 = 1
3167
EOS
3168
$dbi->execute($sql, param => $param);
3169
$result = $dbi->execute("select * from $table1 order by $key1", table => $table1);
3170
$rows   = $result->all;
3171
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
3172
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3173
                  "basic");
3174

            
3175

            
3176
$dbi = DBIx::Custom->connect;
3177
eval { $dbi->execute("drop table $table1") };
3178
$dbi->execute($create_table1_2);
3179
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3180
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3181

            
3182
$param = {$key2 => 11, $key3 => 33};
3183
$update_param = $dbi->update_param($param);
3184
$sql = <<"EOS";
3185
update $table1 $update_param
3186
where $key1 = 1
3187
EOS
3188
$dbi->execute($sql, param => $param);
3189
$result = $dbi->execute("select * from $table1 order by $key1", table => $table1);
3190
$rows   = $result->all;
3191
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 33, $key4 => 4, $key5 => 5},
3192
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3193
                  "basic");
3194

            
3195
$dbi = DBIx::Custom->connect;
3196
eval { $dbi->execute("drop table $table1") };
3197
$dbi->execute($create_table1_2);
3198
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3199
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3200

            
3201
$param = {$key2 => 11, $key3 => 33};
3202
$update_param = $dbi->update_param($param, {no_set => 1});
3203
$sql = <<"EOS";
3204
update $table1 set $update_param
3205
where $key1 = 1
3206
EOS
3207
$dbi->execute($sql, param => $param);
3208
$result = $dbi->execute("select * from $table1 order by $key1", table => $table1);
3209
$rows   = $result->all;
3210
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 33, $key4 => 4, $key5 => 5},
3211
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3212
                  "update param no_set");
3213

            
3214
            
3215
eval { $dbi->update_param({";" => 1}) };
3216
like($@, qr/not safety/);
3217

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3219
test 'update_param';
3220
$dbi = DBIx::Custom->connect;
3221
eval { $dbi->execute("drop table $table1") };
3222
$dbi->execute($create_table1_2);
3223
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3224
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
test cleanup
Yuki Kimoto authored on 2011-08-10
3225

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3226
$param = {$key2 => 11};
3227
$update_param = $dbi->assign_param($param);
3228
$sql = <<"EOS";
3229
update $table1 set $update_param
3230
where $key1 = 1
3231
EOS
3232
$dbi->execute($sql, param => $param, table => $table1);
3233
$result = $dbi->execute("select * from $table1 order by $key1");
3234
$rows   = $result->all;
3235
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
3236
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3237
                  "basic");
test cleanup
Yuki Kimoto authored on 2011-08-10
3238

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3239
test 'Model class';
3240
$dbi = MyDBI1->connect;
3241
eval { $dbi->execute("drop table $table1") };
3242
$dbi->execute($create_table1);
3243
$model = $dbi->model($table1);
3244
$model->insert({$key1 => 'a', $key2 => 'b'});
3245
is_deeply($model->list->all, [{$key1 => 'a', $key2 => 'b'}], 'basic');
3246
eval { $dbi->execute("drop table $table2") };
3247
$dbi->execute($create_table2);
3248
$model = $dbi->model($table2);
3249
$model->insert({$key1 => 'a'});
3250
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'basic');
3251
is($dbi->models->{$table1}, $dbi->model($table1));
3252
is($dbi->models->{$table2}, $dbi->model($table2));
3253

            
3254
$dbi = MyDBI4->connect;
3255
eval { $dbi->execute("drop table $table1") };
3256
$dbi->execute($create_table1);
3257
$model = $dbi->model($table1);
3258
$model->insert({$key1 => 'a', $key2 => 'b'});
3259
is_deeply($model->list->all, [{$key1 => 'a', $key2 => 'b'}], 'basic');
3260
eval { $dbi->execute("drop table $table2") };
3261
$dbi->execute($create_table2);
3262
$model = $dbi->model($table2);
3263
$model->insert({$key1 => 'a'});
3264
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'basic');
3265

            
3266
$dbi = MyDBI5->connect;
3267
eval { $dbi->execute("drop table $table1") };
3268
eval { $dbi->execute("drop table $table2") };
3269
$dbi->execute($create_table1);
3270
$dbi->execute($create_table2);
3271
$model = $dbi->model($table2);
3272
$model->insert({$key1 => 'a'});
3273
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'include all model');
3274
$dbi->insert(table => $table1, param => {$key1 => 1});
3275
$model = $dbi->model($table1);
3276
is_deeply($model->list->all, [{$key1 => 1, $key2 => undef}], 'include all model');
3277

            
3278
test 'primary_key';
3279
$dbi = MyDBI1->connect;
3280
$model = $dbi->model($table1);
3281
$model->primary_key([$key1, $key2]);
3282
is_deeply($model->primary_key, [$key1, $key2]);
3283

            
3284
test 'columns';
3285
$dbi = MyDBI1->connect;
3286
$model = $dbi->model($table1);
3287
$model->columns([$key1, $key2]);
3288
is_deeply($model->columns, [$key1, $key2]);
cleanup test
Yuki Kimoto authored on 2011-08-10
3289

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3290
test 'setup_model';
3291
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3292
$dbi->user_table_info($user_table_info);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3293
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3294
eval { $dbi->execute("drop table $table2") };
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
3295

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3296
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-15
3297
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-10
3298
$dbi->setup_model;
cleanup test
Yuki Kimoto authored on 2011-08-15
3299
is_deeply([sort @{$dbi->model($table1)->columns}], [$key1, $key2]);
3300
is_deeply([sort @{$dbi->model($table2)->columns}], [$key1, $key3]);
cleanup test
Yuki Kimoto authored on 2011-08-10
3301

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3302
test 'each_column';
3303
$dbi = DBIx::Custom->connect;
3304
eval { $dbi->execute("drop table ${q}table$p") };
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3305
eval { $dbi->execute("drop table $table1") };
3306
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3307
eval { $dbi->execute("drop table $table3") };
3308
$dbi->execute($create_table1_type);
cleanup test
Yuki Kimoto authored on 2011-08-10
3309
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-15
3310

            
3311
$infos = [];
3312
$dbi->each_column(sub {
3313
    my ($self, $table, $column, $cinfo) = @_;
3314
    
3315
    if ($table =~ /^table\d/i) {
3316
         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
3317
         push @$infos, $info;
3318
    }
3319
});
3320
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3321
is_deeply($infos, 
3322
    [
3323
        [$table1, $key1, $key1],
3324
        [$table1, $key2, $key2],
3325
        [$table2, $key1, $key1],
3326
        [$table2, $key3, $key3]
3327
    ]
3328
    
cleanup test
Yuki Kimoto authored on 2011-08-10
3329
);
cleanup test
Yuki Kimoto authored on 2011-08-16
3330

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3331
test 'each_table';
3332
$dbi = DBIx::Custom->connect;
3333
eval { $dbi->execute("drop table $table1") };
3334
eval { $dbi->execute("drop table $table2") };
3335
$dbi->execute($create_table2);
3336
$dbi->execute($create_table1_type);
cleanup test
Yuki Kimoto authored on 2011-08-10
3337

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3338
$infos = [];
3339
$dbi->each_table(sub {
3340
    my ($self, $table, $table_info) = @_;
3341
    
3342
    if ($table =~ /^table\d/i) {
3343
         my $info = [$table, $table_info->{TABLE_NAME}];
3344
         push @$infos, $info;
3345
    }
3346
});
3347
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3348
is_deeply($infos, 
3349
    [
3350
        [$table1, $table1],
3351
        [$table2, $table2],
3352
    ]
cleanup test
Yuki Kimoto authored on 2011-08-10
3353
);
3354

            
cleanup test
Yuki Kimoto authored on 2011-08-16
3355
$dbi = DBIx::Custom->connect;
3356
eval { $dbi->execute("drop table $table1") };
3357
eval { $dbi->execute("drop table $table2") };
3358
$dbi->execute($create_table2);
3359
$dbi->execute($create_table1_type);
3360

            
3361
$infos = [];
3362
$dbi->user_table_info($user_table_info);
3363
$dbi->each_table(sub {
3364
    my ($self, $table, $table_info) = @_;
3365
    
3366
    if ($table =~ /^table\d/i) {
3367
         my $info = [$table, $table_info->{TABLE_NAME}];
3368
         push @$infos, $info;
3369
    }
3370
});
3371
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3372
is_deeply($infos, 
3373
    [
3374
        [$table1, $table1],
3375
        [$table2, $table2],
3376
        [$table3, $table3],
3377
    ]
3378
);
3379

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3380
test 'type_rule into';
3381
eval { $dbi->execute("drop table $table1") };
3382
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3383
$user_column_info = $dbi->get_column_info(exclude_table => $dbi->exclude_table);
3384

            
3385

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3386
$dbi = DBIx::Custom->connect;
3387
eval { $dbi->execute("drop table $table1") };
3388
$dbi->execute($create_table1_type);
3389

            
cleanup
Yuki Kimoto authored on 2011-08-16
3390
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3391
$dbi->type_rule(
3392
    into1 => {
3393
        $date_typename => sub { '2010-' . $_[0] }
3394
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3395
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3396
$dbi->insert({$key1 => '01-01'}, table => $table1);
3397
$result = $dbi->select(table => $table1);
3398
like($result->one->{$key1}, qr/^2010-01-01/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3399

            
3400
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3401
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3402
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3403
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3404
$dbi->type_rule(
3405
    into1 => [
3406
         [$date_typename, $datetime_typename] => sub {
3407
            my $value = shift;
3408
            $value =~ s/02/03/g;
3409
            return $value;
3410
         }
3411
    ]
3412
);
3413
$dbi->insert({$key1 => '2010-01-02', $key2 => '2010-01-01 01:01:02'}, table => $table1);
3414
$result = $dbi->select(table => $table1);
3415
$row = $result->one;
3416
like($row->{$key1}, qr/^2010-01-03/);
3417
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3418

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3419
$dbi = DBIx::Custom->connect;
3420
eval { $dbi->execute("drop table $table1") };
3421
$dbi->execute($create_table1_type);
3422
$dbi->insert({$key1 => '2010-01-03', $key2 => '2010-01-01 01:01:03'}, table => $table1);
cleanup
Yuki Kimoto authored on 2011-08-16
3423
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3424
$dbi->type_rule(
3425
    into1 => [
3426
        [$date_typename, $datetime_typename] => sub {
3427
            my $value = shift;
3428
            $value =~ s/02/03/g;
3429
            return $value;
3430
        }
3431
    ]
cleanup test
Yuki Kimoto authored on 2011-08-10
3432
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3433
$result = $dbi->execute(
3434
    "select * from $table1 where $key1 = :$key1 and $key2 = :$table1.$key2",
3435
    param => {$key1 => '2010-01-03', "$table1.$key2" => '2010-01-01 01:01:02'}
cleanup test
Yuki Kimoto authored on 2011-08-10
3436
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3437
$row = $result->one;
3438
like($row->{$key1}, qr/^2010-01-03/);
3439
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
3440

            
3441
$dbi = DBIx::Custom->connect;
3442
eval { $dbi->execute("drop table $table1") };
3443
$dbi->execute($create_table1_type);
3444
$dbi->insert({$key1 => '2010-01-03', $key2 => '2010-01-01 01:01:03'}, table => $table1);
cleanup
Yuki Kimoto authored on 2011-08-16
3445
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3446
$dbi->type_rule(
3447
    into1 => [
3448
        [$date_typename, $datetime_typename] => sub {
3449
            my $value = shift;
3450
            $value =~ s/02/03/g;
3451
            return $value;
3452
        }
cleanup test
Yuki Kimoto authored on 2011-08-10
3453
    ]
3454
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3455
$result = $dbi->execute(
3456
    "select * from $table1 where $key1 = :$key1 and $key2 = :$table1.$key2",
3457
    param => {$key1 => '2010-01-02', "$table1.$key2" => '2010-01-01 01:01:02'},
3458
    table => $table1
3459
);
3460
$row = $result->one;
3461
like($row->{$key1}, qr/^2010-01-03/);
3462
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3463

            
3464
$dbi = DBIx::Custom->connect;
3465
eval { $dbi->execute("drop table $table1") };
3466
$dbi->execute($create_table1_type);
3467
$dbi->register_filter(convert => sub {
3468
    my $value = shift || '';
3469
    $value =~ s/02/03/;
3470
    return $value;
3471
});
cleanup
Yuki Kimoto authored on 2011-08-16
3472
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3473
$dbi->type_rule(
3474
    from1 => {
3475
        $date_datatype => 'convert',
3476
    },
3477
    into1 => {
3478
        $date_typename => 'convert',
3479
    }
3480
);
3481
$dbi->insert({$key1 => '2010-02-02'}, table => $table1);
3482
$result = $dbi->select(table => $table1);
3483
like($result->fetch->[0], qr/^2010-03-03/);
3484

            
3485
test 'type_rule and filter order';
3486
$dbi = DBIx::Custom->connect;
3487
eval { $dbi->execute("drop table $table1") };
3488
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3489
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3490
$dbi->type_rule(
3491
    into1 => {
3492
        $date_typename => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3493
    },
3494
    into2 => {
3495
        $date_typename => sub { my $v = shift || ''; $v =~ s/5/6/; return $v }
3496
    },
3497
    from1 => {
3498
        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/7/; return $v }
3499
    },
3500
    from2 => {
3501
        $date_datatype => sub { my $v = shift || ''; $v =~ s/7/8/; return $v }
3502
    }
3503
);
3504
$dbi->insert({$key1 => '2010-01-03'}, 
3505
  table => $table1, filter => {$key1 => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }});
3506
$result = $dbi->select(table => $table1);
3507
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/8/9/; return $v });
3508
like($result->fetch_first->[0], qr/^2010-01-09/);
3509

            
3510

            
3511
$dbi = DBIx::Custom->connect;
3512
eval { $dbi->execute("drop table $table1") };
3513
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3514
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3515
$dbi->type_rule(
3516
    from1 => {
3517
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3518
    },
3519
    from2 => {
3520
        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3521
    },
3522
);
3523
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3524
$result = $dbi->select(table => $table1);
cleanup
Yuki Kimoto authored on 2011-08-16
3525
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3526
$result->type_rule(
3527
    from1 => {
3528
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/6/; return $v }
3529
    },
3530
    from2 => {
3531
        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/8/; return $v }
3532
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3533
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3534
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/8/9/; return $v });
3535
like($result->fetch_first->[0], qr/^2010-01-09/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3536

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3537
test 'type_rule_off';
cleanup test
Yuki Kimoto authored on 2011-08-10
3538
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-15
3539
eval { $dbi->execute("drop table $table1") };
3540
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3541
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3542
$dbi->type_rule(
3543
    from1 => {
3544
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3545
    },
3546
    into1 => {
3547
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3548
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3549
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3550
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule_off => 1);
3551
$result = $dbi->select(table => $table1, type_rule_off => 1);
3552
like($result->type_rule_off->fetch->[0], qr/^2010-01-03/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3553

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3554
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3555
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3556
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3557
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3558
$dbi->type_rule(
3559
    from1 => {
3560
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3561
    },
3562
    into1 => {
3563
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3564
    }
3565
);
3566
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule_off => 1);
3567
$result = $dbi->select(table => $table1, type_rule_off => 1);
3568
like($result->one->{$key1}, qr/^2010-01-04/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3569

            
3570
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3571
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3572
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3573
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3574
$dbi->type_rule(
3575
    from1 => {
3576
        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3577
    },
3578
    into1 => {
3579
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3580
    }
3581
);
3582
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3583
$result = $dbi->select(table => $table1);
3584
like($result->one->{$key1}, qr/^2010-01-05/);
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
like($result->fetch->[0], qr/2010-01-05/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3601

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3616
eval{$dbi->type_rule(
3617
    into1 => {
3618
        $date_typename => 'pp'
3619
    }
3620
)};
3621
like($@, qr/not registered/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3622

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3623
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3624
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3625
$dbi->execute($create_table1_type);
cleanup test
Yuki Kimoto authored on 2011-08-10
3626
eval {
cleanup test
Yuki Kimoto authored on 2011-08-15
3627
    $dbi->type_rule(
3628
        from1 => {
3629
            Date => sub { $_[0] * 2 },
3630
        }
cleanup test
Yuki Kimoto authored on 2011-08-10
3631
    );
3632
};
cleanup test
Yuki Kimoto authored on 2011-08-15
3633
like($@, qr/lower/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3634

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3635
eval {
3636
    $dbi->type_rule(
3637
        into1 => {
3638
            Date => sub { $_[0] * 2 },
3639
        }
3640
    );
3641
};
3642
like($@, qr/lower/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3643

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

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

            
3682
$result = $dbi->select(table => $table1);
3683
$result->type_rule(
3684
    from1 => {
3685
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3686
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3687
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3688
$row = $result->one;
3689
like($row->{$key1}, qr/2010-01-05/);
3690
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3691

            
3692
$result = $dbi->select(table => $table1);
3693
$result->type_rule(
3694
    from1 => {
3695
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3696
    }
3697
);
3698
$row = $result->one;
3699
like($row->{$key1}, qr/2010-01-05/);
3700
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3701

            
3702
$result = $dbi->select(table => $table1);
3703
$result->type_rule(
3704
    from1 => [$date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }]
3705
);
3706
$row = $result->one;
3707
like($row->{$key1}, qr/2010-01-05/);
3708
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3709

            
3710
$dbi->register_filter(five => sub { my $v = shift || ''; $v =~ s/3/5/; return $v });
3711
$result = $dbi->select(table => $table1);
3712
$result->type_rule(
3713
    from1 => [$date_datatype => 'five']
3714
);
3715
$row = $result->one;
3716
like($row->{$key1}, qr/^2010-01-05/);
3717
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
3718

            
3719
$result = $dbi->select(table => $table1);
3720
$result->type_rule(
3721
    from1 => [$date_datatype => undef]
3722
);
3723
$row = $result->one;
3724
like($row->{$key1}, qr/^2010-01-03/);
3725
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3726

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3741
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3742
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3743
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3744
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3745
$dbi->type_rule(
3746
    from1 => {
3747
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3748
    },
3749
);
3750
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3751
$result = $dbi->select(table => $table1);
3752
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/4/5/; return $v });
3753
like($result->fetch->[0], qr/^2010-01-05/);
test cleanup
Yuki Kimoto authored on 2011-08-10
3754

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3755
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3756
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3757
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3758
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3759
$dbi->type_rule(
3760
    into1 => {
3761
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3762
    },
3763
    into2 => {
3764
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3765
    },
3766
    from1 => {
3767
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/6/; return $v }
3768
    },
3769
    from2 => {
3770
        $date_datatype => sub { my $v = shift || ''; $v =~ s/(3|6)/7/; return $v }
3771
    }
3772
);
3773
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule_off => 1);
3774
$result = $dbi->select(table => $table1);
3775
like($result->type_rule_off->fetch_first->[0], qr/^2010-01-03/);
3776
$result = $dbi->select(table => $table1);
3777
like($result->type_rule_on->fetch_first->[0], qr/^2010-01-07/);
test cleanup
Yuki Kimoto authored on 2011-08-10
3778

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3779
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3780
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3781
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3782
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3783
$dbi->type_rule(
3784
    into1 => {
3785
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3786
    },
3787
    into2 => {
3788
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3789
    },
3790
    from1 => {
3791
        $date_datatype => sub { my $v = shift || ''; $v =~ s/(3|5)/6/; return $v }
3792
    },
3793
    from2 => {
3794
        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/7/; return $v }
3795
    }
3796
);
3797
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule1_off => 1);
3798
$result = $dbi->select(table => $table1);
3799
like($result->type_rule1_off->fetch_first->[0], qr/^2010-01-05/);
3800
$result = $dbi->select(table => $table1);
3801
like($result->type_rule1_on->fetch_first->[0], qr/^2010-01-07/);
test cleanup
Yuki Kimoto authored on 2011-08-10
3802

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3803
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3804
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3805
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3806
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3807
$dbi->type_rule(
3808
    into1 => {
3809
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3810
    },
3811
    into2 => {
3812
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3813
    },
3814
    from1 => {
3815
        $date_datatype => sub { my $v = shift || ''; $v =~ s/5/6/; return $v }
3816
    },
3817
    from2 => {
3818
        $date_datatype => sub { my $v = shift || ''; $v =~ s/(3|6)/7/; return $v }
3819
    }
3820
);
3821
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule2_off => 1);
3822
$result = $dbi->select(table => $table1);
3823
like($result->type_rule2_off->fetch_first->[0], qr/^2010-01-06/);
3824
$result = $dbi->select(table => $table1);
3825
like($result->type_rule2_on->fetch_first->[0], qr/^2010-01-07/);
test cleanup
Yuki Kimoto authored on 2011-08-10
3826

            
added memory leak check test
Yuki Kimoto authored on 2011-08-15
3827
test 'join';
3828
$dbi = DBIx::Custom->connect;
3829
eval { $dbi->execute("drop table $table1") };
3830
$dbi->execute($create_table1);
3831
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3832
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
3833
eval { $dbi->execute("drop table $table2") };
3834
$dbi->execute($create_table2);
3835
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3836
eval { $dbi->execute("drop table $table3") };
3837
$dbi->execute("create table $table3 ($key3 int, $key4 int)");
3838
$dbi->insert(table => $table3, param => {$key3 => 5, $key4 => 4});
3839
$rows = $dbi->select(
3840
    table => $table1,
3841
    column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
3842
    where   => {"$table1.$key2" => 2},
3843
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1"]
3844
)->all;
3845
is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}]);
3846

            
3847
$rows = $dbi->select(
3848
    table => $table1,
3849
    where   => {$key1 => 1},
3850
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1"]
3851
)->all;
3852
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
3853

            
3854
eval {
3855
    $rows = $dbi->select(
3856
        table => $table1,
3857
        column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
3858
        where   => {"$table1.$key2" => 2},
3859
        join  => {"$table1.$key1" => "$table2.$key1"}
3860
    );
3861
};
3862
like ($@, qr/array/);
3863

            
3864
$rows = $dbi->select(
3865
    table => $table1,
3866
    where   => {$key1 => 1},
3867
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
3868
              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
3869
)->all;
3870
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
3871

            
3872
$rows = $dbi->select(
3873
    column => "$table3.$key4 as ${table3}__$key4",
3874
    table => $table1,
3875
    where   => {"$table1.$key1" => 1},
3876
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
3877
              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
3878
)->all;
3879
is_deeply($rows, [{"${table3}__$key4" => 4}]);
3880

            
3881
$rows = $dbi->select(
3882
    column => "$table1.$key1 as ${table1}__$key1",
3883
    table => $table1,
3884
    where   => {"$table3.$key4" => 4},
3885
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
3886
              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
3887
)->all;
3888
is_deeply($rows, [{"${table1}__$key1" => 1}]);
3889

            
3890
$dbi = DBIx::Custom->connect;
3891
eval { $dbi->execute("drop table $table1") };
3892
$dbi->execute($create_table1);
3893
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3894
eval { $dbi->execute("drop table $table2") };
3895
$dbi->execute($create_table2);
3896
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3897
$rows = $dbi->select(
3898
    table => $table1,
3899
    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",
3900
    where   => {"$table1.$key2" => 2},
3901
    join  => ["left outer join ${q}$table2$p on ${q}$table1$p.${q}$key1$p = ${q}$table2$p.${q}$key1$p"],
3902
)->all;
3903
is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}],
3904
          'quote');
3905

            
3906

            
3907
$dbi = DBIx::Custom->connect;
3908
eval { $dbi->execute("drop table $table1") };
3909
$dbi->execute($create_table1);
3910
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3911
$sql = <<"EOS";
3912
left outer join (
3913
  select * from $table1 t1
3914
  where t1.$key2 = (
3915
    select max(t2.$key2) from $table1 t2
3916
    where t1.$key1 = t2.$key1
3917
  )
3918
) $table3 on $table1.$key1 = $table3.$key1
3919
EOS
3920
$join = [$sql];
3921
$rows = $dbi->select(
3922
    table => $table1,
3923
    column => "$table3.$key1 as ${table3}__$key1",
3924
    join  => $join
3925
)->all;
3926
is_deeply($rows, [{"${table3}__$key1" => 1}]);
3927

            
3928
$dbi = DBIx::Custom->connect;
3929
eval { $dbi->execute("drop table $table1") };
3930
eval { $dbi->execute("drop table $table2") };
3931
$dbi->execute($create_table1);
3932
$dbi->execute($create_table2);
3933
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3934
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4});
3935
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3936
$result = $dbi->select(
3937
    table => $table1,
3938
    join => [
3939
        "left outer join $table2 on $table2.$key2 = '4' and $table1.$key1 = $table2.$key1"
3940
    ]
3941
);
3942
is_deeply($result->all, [{$key1 => 1, $key2 => 2}]);
3943
$result = $dbi->select(
3944
    table => $table1,
3945
    column => [{$table2 => [$key3]}],
3946
    join => [
3947
        "left outer join $table2 on $table2.$key3 = '4' and $table1.$key1 = $table2.$key1"
3948
    ]
3949
);
3950
is_deeply($result->all, [{"$table2.$key3" => 4}]);
3951
$result = $dbi->select(
3952
    table => $table1,
3953
    column => [{$table2 => [$key3]}],
3954
    join => [
3955
        "left outer join $table2 on $table1.$key1 = $table2.$key1 and $table2.$key3 = '4'"
3956
    ]
3957
);
3958
is_deeply($result->all, [{"$table2.$key3" => 4}]);
3959

            
3960
$dbi = DBIx::Custom->connect;
3961
eval { $dbi->execute("drop table $table1") };
3962
eval { $dbi->execute("drop table $table2") };
3963
$dbi->execute($create_table1);
3964
$dbi->execute($create_table2);
3965
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3966
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4});
3967
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3968
$result = $dbi->select(
3969
    table => $table1,
3970
    column => [{$table2 => [$key3]}],
3971
    join => [
3972
        {
3973
            clause => "left outer join $table2 on $table2.$key3 = '4' and $table1.$key1 = $table2.$key1",
3974
            table => [$table1, $table2]
3975
        }
3976
    ]
3977
);
3978
is_deeply($result->all, [{"$table2.$key3" => 4}]);
3979

            
3980
test 'columns';
3981
$dbi = MyDBI1->connect;
3982
$model = $dbi->model($table1);
3983

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
3984
test 'count';
3985
$dbi = DBIx::Custom->connect;
3986
eval { $dbi->execute("drop table $table1") };
3987
$dbi->execute($create_table1);
3988
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3989
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 3});
3990
is($dbi->count(table => $table1), 2);
3991
is($dbi->count(table => $table1, where => {$key2 => 2}), 1);
3992
$model = $dbi->create_model(table => $table1);
3993
is($model->count, 2);
cleanup test
Yuki Kimoto authored on 2011-08-10
3994

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