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

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

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

            
14
plan 'no_plan';
15

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

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

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

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

            
86
    use strict;
87
    use warnings;
88

            
89
    use base 'DBIx::Custom';
90

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

            
102
    package MyModel2::Base1;
103

            
104
    use strict;
105
    use warnings;
106

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

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

            
111
    use strict;
112
    use warnings;
113

            
114
    use base 'MyModel2::Base1';
115

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

            
122
    sub list { shift->select; }
123

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

            
126
    use strict;
127
    use warnings;
128

            
129
    use base 'MyModel2::Base1';
130

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

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

            
139
    package MyModel2::TABLE1;
140

            
141
    use strict;
142
    use warnings;
143

            
144
    use base 'MyModel2::Base1';
145

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

            
152
    sub list { shift->select; }
153

            
154
    package MyModel2::TABLE2;
155

            
156
    use strict;
157
    use warnings;
158

            
159
    use base 'MyModel2::Base1';
160

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

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

            
172
    use strict;
173
    use warnings;
174

            
175
    use base 'DBIx::Custom';
176

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
648

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

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

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

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

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

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

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

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

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

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

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

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

            
744

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
882

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

            
888
$dbi->begin_work;
889

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

            
896
$dbi->rollback if $@;
897

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

            
902
$dbi->begin_work;
903

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

            
909
$dbi->commit unless $@;
910

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1509

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1707
$dbi->apply_filter(
1708

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2039

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2235

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

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

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

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

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

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

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

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

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

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

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

            
2372

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

            
2388

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

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

            
2409

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

            
2428

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

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

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

            
2468

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

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

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

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

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

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

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

            
2564

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

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

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

            
2591

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
2770
$dbi->execute("drop table $table1");
2771
$dbi->execute($create_table1_highperformance);
2772
$rows = [
2773
    {$key7 => 10, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 5},
2774
    {$key7 => 11, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6},
2775
];
2776
{
2777
    $model = $dbi->create_model(table => $table1, primary_key => $key1);
2778
    my $query;
2779
    foreach my $row (@$rows) {
2780
      $query ||= $model->insert($row, id => 1, query => 1);
2781
      $model->execute($query, $row, id => 1, filter => {$key7 => sub { $_[0] * 2 }});
2782
    }
2783
    is_deeply($dbi->select(table => $table1, append => 'order by key2')->all,
2784
      [
2785
          {$key7 => 20, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 5, $key1 => 1},
2786
          {$key7 => 22, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 1},
2787
      ]
2788
    );
2789
}
2790

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2798
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2799
@rows = ();
2800
while (my $row = $result->fetch) {
2801
    push @rows, [@$row];
2802
}
2803
is_deeply(\@rows, [[1, 2], [3, 4]]);
2804

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2805
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2806
@rows = ();
2807
while (my $row = $result->fetch_hash) {
2808
    push @rows, {%$row};
2809
}
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2810
is_deeply(\@rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2811

            
2812
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2813
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2814
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2815
$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1);
2816
$dbi->insert({$key1 => 3, $key2 => 4}, table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2817

            
2818
test 'fetch_all';
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2819
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2820
$rows = $result->fetch_all;
2821
is_deeply($rows, [[1, 2], [3, 4]]);
2822

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

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

            
2831
$rows = $result->fetch_all;
2832
is_deeply($rows, [[3, 2], [9, 4]], "array");
2833

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

            
2840
test "query_builder";
2841
$datas = [
2842
    # Basic tests
2843
    {   name            => 'placeholder basic',
2844
        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
cleanup test
Yuki Kimoto authored on 2011-08-15
2845
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?",
test cleanup
Yuki Kimoto authored on 2011-08-10
2846
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
2847
    },
2848
    {
2849
        name            => 'placeholder in',
cleanup test
Yuki Kimoto authored on 2011-08-15
2850
        source            => "{in k1 3}",
2851
        sql_expected    => "k1 in (?, ?, ?)",
test cleanup
Yuki Kimoto authored on 2011-08-10
2852
        columns_expected   => [qw/k1 k1 k1/]
2853
    },
2854
    
2855
    # Table name
2856
    {
2857
        name            => 'placeholder with table name',
2858
        source            => "{= a.k1} {= a.k2}",
cleanup test
Yuki Kimoto authored on 2011-08-15
2859
        sql_expected    => "a.k1 = ? a.k2 = ?",
test cleanup
Yuki Kimoto authored on 2011-08-10
2860
        columns_expected  => [qw/a.k1 a.k2/]
2861
    },
2862
    {   
2863
        name            => 'placeholder in with table name',
2864
        source            => "{in a.k1 2} {in b.k2 2}",
cleanup test
Yuki Kimoto authored on 2011-08-15
2865
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?)",
test cleanup
Yuki Kimoto authored on 2011-08-10
2866
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
2867
    },
2868
    {
2869
        name            => 'not contain tag',
2870
        source            => "aaa",
cleanup test
Yuki Kimoto authored on 2011-08-15
2871
        sql_expected    => "aaa",
test cleanup
Yuki Kimoto authored on 2011-08-10
2872
        columns_expected  => [],
2873
    }
2874
];
2875

            
2876
for (my $i = 0; $i < @$datas; $i++) {
2877
    my $data = $datas->[$i];
cleanup
Yuki Kimoto authored on 2011-08-13
2878
    my $dbi = DBIx::Custom->new;
2879
    my $builder = $dbi->query_builder;
test cleanup
Yuki Kimoto authored on 2011-08-10
2880
    my $query = $builder->build_query($data->{source});
2881
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
2882
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
2883
}
2884

            
cleanup
Yuki Kimoto authored on 2011-08-13
2885
$dbi = DBIx::Custom->new;
2886
$builder = $dbi->query_builder;
2887
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
2888
    p => sub {
2889
        my @args = @_;
2890
        
2891
        my $expand    = "? $args[0] $args[1]";
2892
        my $columns = [2];
2893
        return [$expand, $columns];
2894
    }
2895
);
2896

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
2907
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
2908
    q => 'string'
2909
});
2910

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
2914
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
2915
   r => sub {} 
2916
});
2917

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
2921
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
2922
   s => sub { return ["a", ""]} 
2923
});
2924

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
2928
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
2929
    t => sub {return ["a", []]}
2930
);
2931

            
2932

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

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

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

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

            
2948
test 'variouse source';
2949
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d";
2950
$query = $builder->build_query($source);
2951
is($query->sql, 'a b = ? c { } { = ? } = ? d', "basic : 1");
2952

            
2953
$source = "abc";
2954
$query = $builder->build_query($source);
2955
is($query->sql, 'abc', "basic : 2");
2956

            
2957
$source = "{= a}";
2958
$query = $builder->build_query($source);
2959
is($query->sql, 'a = ?', "only tag");
2960

            
2961
$source = "000";
2962
$query = $builder->build_query($source);
2963
is($query->sql, '000', "contain 0 value");
2964

            
2965
$source = "a {= b} }";
2966
eval{$builder->build_query($source)};
2967
like($@, qr/unexpected "}"/, "error : 1");
2968

            
2969
$source = "a {= {}";
2970
eval{$builder->build_query($source)};
2971
like($@, qr/unexpected "{"/, "error : 2");
2972

            
2973
test 'select() wrap option';
2974
$dbi = DBIx::Custom->connect;
2975
eval { $dbi->execute("drop table $table1") };
2976
$dbi->execute($create_table1);
2977
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2978
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
2979
$rows = $dbi->select(
2980
    table => $table1,
2981
    column => $key1,
test cleanup
Yuki Kimoto authored on 2011-08-15
2982
    wrap => ["select * from (", ") t where $key1 = 1"]
cleanup test
Yuki Kimoto authored on 2011-08-15
2983
)->all;
2984
is_deeply($rows, [{$key1 => 1}]);
2985

            
2986
eval {
2987
$dbi->select(
2988
    table => $table1,
2989
    column => $key1,
2990
    wrap => 'select * from ('
2991
)
2992
};
2993
like($@, qr/array/);
2994

            
2995
test 'select() sqlfilter option';
2996
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
2997
$dbi->user_table_info($user_table_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
2998
eval { $dbi->execute("drop table $table1") };
2999
$dbi->execute($create_table1);
3000
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3001
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
3002
$rows = $dbi->select(
3003
    table => $table1,
3004
    column => $key1,
3005
    sqlfilter => sub {
3006
        my $sql = shift;
test cleanup
Yuki Kimoto authored on 2011-08-15
3007
        $sql = "select * from ( $sql ) t where $key1 = 1";
cleanup test
Yuki Kimoto authored on 2011-08-15
3008
        return $sql;
3009
    }
3010
)->all;
3011
is_deeply($rows, [{$key1 => 1}]);
3012

            
3013
test 'dbi method from model';
3014
$dbi = MyDBI9->connect;
3015
eval { $dbi->execute("drop table $table1") };
3016
$dbi->execute($create_table1);
3017
$dbi->setup_model;
3018
$model = $dbi->model($table1);
3019
eval{$model->execute("select * from $table1")};
3020
ok(!$@);
3021

            
3022
test 'column table option';
3023
$dbi = MyDBI9->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3024
$dbi->user_table_info($user_table_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3025
eval { $dbi->execute("drop table $table1") };
3026
$dbi->execute($create_table1);
3027
eval { $dbi->execute("drop table $table2") };
3028
$dbi->execute($create_table2);
3029
$dbi->setup_model;
3030
$dbi->execute("insert into $table1 ($key1, $key2) values (1, 2)");
3031
$dbi->execute("insert into $table2 ($key1, $key3) values (1, 4)");
3032
$model = $dbi->model($table1);
3033
$result = $model->select(
3034
    column => [
3035
        $model->column($table2, {alias => $table2_alias})
3036
    ],
3037
    where => {"$table2_alias.$key3" => 4}
3038
);
3039
is_deeply($result->one, 
3040
          {"$table2_alias.$key1" => 1, "$table2_alias.$key3" => 4});
3041

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

            
3052
$dbi->separator('-');
3053
$result = $model->select(
3054
    column => [
3055
        $model->column($table2, {alias => $table2_alias})
3056
    ],
3057
    where => {"$table2_alias.$key3" => 4}
3058
);
3059
is_deeply($result->one, 
3060
          {"$table2_alias-$key1" => 1, "$table2_alias-$key3" => 4});
3061

            
3062
test 'create_model';
3063
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3064
$dbi->user_table_info($user_table_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3065
eval { $dbi->execute("drop table $table1") };
3066
eval { $dbi->execute("drop table $table2") };
3067
$dbi->execute($create_table1);
3068
$dbi->execute($create_table2);
3069

            
3070
$dbi->create_model(
3071
    table => $table1,
3072
    join => [
3073
       "left outer join $table2 on $table1.$key1 = $table2.$key1"
3074
    ],
3075
    primary_key => [$key1]
3076
);
3077
$model2 = $dbi->create_model(
3078
    table => $table2
3079
);
3080
$dbi->create_model(
3081
    table => $table3,
3082
    filter => [
3083
        $key1 => {in => sub { uc $_[0] }}
3084
    ]
3085
);
3086
$dbi->setup_model;
3087
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3088
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
3089
$model = $dbi->model($table1);
3090
$result = $model->select(
3091
    column => [$model->mycolumn, $model->column($table2)],
3092
    where => {"$table1.$key1" => 1}
3093
);
3094
is_deeply($result->one,
3095
          {$key1 => 1, $key2 => 2, "$table2.$key1" => 1, "$table2.$key3" => 3});
3096
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3});
3097

            
3098
test 'model method';
3099
$dbi = DBIx::Custom->connect;
3100
eval { $dbi->execute("drop table $table2") };
3101
$dbi->execute($create_table2);
3102
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
3103
$model = $dbi->create_model(
3104
    table => $table2
3105
);
3106
$model->method(foo => sub { shift->select(@_) });
3107
is_deeply($model->foo->one, {$key1 => 1, $key3 => 3});
3108

            
3109
test 'update_param';
3110
$dbi = DBIx::Custom->connect;
3111
eval { $dbi->execute("drop table $table1") };
3112
$dbi->execute($create_table1_2);
3113
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3114
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3115

            
3116
$param = {$key2 => 11};
3117
$update_param = $dbi->update_param($param);
3118
$sql = <<"EOS";
3119
update $table1 $update_param
3120
where $key1 = 1
3121
EOS
3122
$dbi->execute($sql, param => $param);
3123
$result = $dbi->execute("select * from $table1 order by $key1", table => $table1);
3124
$rows   = $result->all;
3125
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
3126
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3127
                  "basic");
3128

            
3129

            
3130
$dbi = DBIx::Custom->connect;
3131
eval { $dbi->execute("drop table $table1") };
3132
$dbi->execute($create_table1_2);
3133
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3134
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3135

            
3136
$param = {$key2 => 11, $key3 => 33};
3137
$update_param = $dbi->update_param($param);
3138
$sql = <<"EOS";
3139
update $table1 $update_param
3140
where $key1 = 1
3141
EOS
3142
$dbi->execute($sql, param => $param);
3143
$result = $dbi->execute("select * from $table1 order by $key1", table => $table1);
3144
$rows   = $result->all;
3145
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 33, $key4 => 4, $key5 => 5},
3146
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3147
                  "basic");
3148

            
3149
$dbi = DBIx::Custom->connect;
3150
eval { $dbi->execute("drop table $table1") };
3151
$dbi->execute($create_table1_2);
3152
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3153
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3154

            
3155
$param = {$key2 => 11, $key3 => 33};
3156
$update_param = $dbi->update_param($param, {no_set => 1});
3157
$sql = <<"EOS";
3158
update $table1 set $update_param
3159
where $key1 = 1
3160
EOS
3161
$dbi->execute($sql, param => $param);
3162
$result = $dbi->execute("select * from $table1 order by $key1", table => $table1);
3163
$rows   = $result->all;
3164
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 33, $key4 => 4, $key5 => 5},
3165
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3166
                  "update param no_set");
3167

            
3168
            
3169
eval { $dbi->update_param({";" => 1}) };
3170
like($@, qr/not safety/);
3171

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3173
test 'update_param';
3174
$dbi = DBIx::Custom->connect;
3175
eval { $dbi->execute("drop table $table1") };
3176
$dbi->execute($create_table1_2);
3177
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3178
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
test cleanup
Yuki Kimoto authored on 2011-08-10
3179

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3180
$param = {$key2 => 11};
3181
$update_param = $dbi->assign_param($param);
3182
$sql = <<"EOS";
3183
update $table1 set $update_param
3184
where $key1 = 1
3185
EOS
3186
$dbi->execute($sql, param => $param, table => $table1);
3187
$result = $dbi->execute("select * from $table1 order by $key1");
3188
$rows   = $result->all;
3189
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
3190
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3191
                  "basic");
test cleanup
Yuki Kimoto authored on 2011-08-10
3192

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3193
test 'Model class';
3194
$dbi = MyDBI1->connect;
3195
eval { $dbi->execute("drop table $table1") };
3196
$dbi->execute($create_table1);
3197
$model = $dbi->model($table1);
3198
$model->insert({$key1 => 'a', $key2 => 'b'});
3199
is_deeply($model->list->all, [{$key1 => 'a', $key2 => 'b'}], 'basic');
3200
eval { $dbi->execute("drop table $table2") };
3201
$dbi->execute($create_table2);
3202
$model = $dbi->model($table2);
3203
$model->insert({$key1 => 'a'});
3204
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'basic');
3205
is($dbi->models->{$table1}, $dbi->model($table1));
3206
is($dbi->models->{$table2}, $dbi->model($table2));
3207

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

            
3220
$dbi = MyDBI5->connect;
3221
eval { $dbi->execute("drop table $table1") };
3222
eval { $dbi->execute("drop table $table2") };
3223
$dbi->execute($create_table1);
3224
$dbi->execute($create_table2);
3225
$model = $dbi->model($table2);
3226
$model->insert({$key1 => 'a'});
3227
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'include all model');
3228
$dbi->insert(table => $table1, param => {$key1 => 1});
3229
$model = $dbi->model($table1);
3230
is_deeply($model->list->all, [{$key1 => 1, $key2 => undef}], 'include all model');
3231

            
3232
test 'primary_key';
3233
$dbi = MyDBI1->connect;
3234
$model = $dbi->model($table1);
3235
$model->primary_key([$key1, $key2]);
3236
is_deeply($model->primary_key, [$key1, $key2]);
3237

            
3238
test 'columns';
3239
$dbi = MyDBI1->connect;
3240
$model = $dbi->model($table1);
3241
$model->columns([$key1, $key2]);
3242
is_deeply($model->columns, [$key1, $key2]);
cleanup test
Yuki Kimoto authored on 2011-08-10
3243

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3244
test 'setup_model';
3245
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3246
$dbi->user_table_info($user_table_info);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3247
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3248
eval { $dbi->execute("drop table $table2") };
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
3249

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3250
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-15
3251
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-10
3252
$dbi->setup_model;
cleanup test
Yuki Kimoto authored on 2011-08-15
3253
is_deeply([sort @{$dbi->model($table1)->columns}], [$key1, $key2]);
3254
is_deeply([sort @{$dbi->model($table2)->columns}], [$key1, $key3]);
cleanup test
Yuki Kimoto authored on 2011-08-10
3255

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3256
test 'each_column';
3257
$dbi = DBIx::Custom->connect;
3258
eval { $dbi->execute("drop table ${q}table$p") };
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3259
eval { $dbi->execute("drop table $table1") };
3260
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3261
eval { $dbi->execute("drop table $table3") };
3262
$dbi->execute($create_table1_type);
cleanup test
Yuki Kimoto authored on 2011-08-10
3263
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-15
3264

            
3265
$infos = [];
3266
$dbi->each_column(sub {
3267
    my ($self, $table, $column, $cinfo) = @_;
3268
    
3269
    if ($table =~ /^table\d/i) {
3270
         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
3271
         push @$infos, $info;
3272
    }
3273
});
3274
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3275
is_deeply($infos, 
3276
    [
3277
        [$table1, $key1, $key1],
3278
        [$table1, $key2, $key2],
3279
        [$table2, $key1, $key1],
3280
        [$table2, $key3, $key3]
3281
    ]
3282
    
cleanup test
Yuki Kimoto authored on 2011-08-10
3283
);
cleanup test
Yuki Kimoto authored on 2011-08-16
3284

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3285
test 'each_table';
3286
$dbi = DBIx::Custom->connect;
3287
eval { $dbi->execute("drop table $table1") };
3288
eval { $dbi->execute("drop table $table2") };
3289
$dbi->execute($create_table2);
3290
$dbi->execute($create_table1_type);
cleanup test
Yuki Kimoto authored on 2011-08-10
3291

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3292
$infos = [];
3293
$dbi->each_table(sub {
3294
    my ($self, $table, $table_info) = @_;
3295
    
3296
    if ($table =~ /^table\d/i) {
3297
         my $info = [$table, $table_info->{TABLE_NAME}];
3298
         push @$infos, $info;
3299
    }
3300
});
3301
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3302
is_deeply($infos, 
3303
    [
3304
        [$table1, $table1],
3305
        [$table2, $table2],
3306
    ]
cleanup test
Yuki Kimoto authored on 2011-08-10
3307
);
3308

            
cleanup test
Yuki Kimoto authored on 2011-08-16
3309
$dbi = DBIx::Custom->connect;
3310
eval { $dbi->execute("drop table $table1") };
3311
eval { $dbi->execute("drop table $table2") };
3312
$dbi->execute($create_table2);
3313
$dbi->execute($create_table1_type);
3314

            
3315
$infos = [];
3316
$dbi->user_table_info($user_table_info);
3317
$dbi->each_table(sub {
3318
    my ($self, $table, $table_info) = @_;
3319
    
3320
    if ($table =~ /^table\d/i) {
3321
         my $info = [$table, $table_info->{TABLE_NAME}];
3322
         push @$infos, $info;
3323
    }
3324
});
3325
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3326
is_deeply($infos, 
3327
    [
3328
        [$table1, $table1],
3329
        [$table2, $table2],
3330
        [$table3, $table3],
3331
    ]
3332
);
3333

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3334
test 'type_rule into';
3335
eval { $dbi->execute("drop table $table1") };
3336
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3337
$user_column_info = $dbi->get_column_info(exclude_table => $dbi->exclude_table);
3338

            
3339

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3340
$dbi = DBIx::Custom->connect;
3341
eval { $dbi->execute("drop table $table1") };
3342
$dbi->execute($create_table1_type);
3343

            
cleanup
Yuki Kimoto authored on 2011-08-16
3344
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3345
$dbi->type_rule(
3346
    into1 => {
3347
        $date_typename => sub { '2010-' . $_[0] }
3348
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3349
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3350
$dbi->insert({$key1 => '01-01'}, table => $table1);
3351
$result = $dbi->select(table => $table1);
3352
like($result->one->{$key1}, qr/^2010-01-01/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3353

            
3354
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3355
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3356
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3357
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3358
$dbi->type_rule(
3359
    into1 => [
3360
         [$date_typename, $datetime_typename] => sub {
3361
            my $value = shift;
3362
            $value =~ s/02/03/g;
3363
            return $value;
3364
         }
3365
    ]
3366
);
3367
$dbi->insert({$key1 => '2010-01-02', $key2 => '2010-01-01 01:01:02'}, table => $table1);
3368
$result = $dbi->select(table => $table1);
3369
$row = $result->one;
3370
like($row->{$key1}, qr/^2010-01-03/);
3371
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3372

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3373
$dbi = DBIx::Custom->connect;
3374
eval { $dbi->execute("drop table $table1") };
3375
$dbi->execute($create_table1_type);
3376
$dbi->insert({$key1 => '2010-01-03', $key2 => '2010-01-01 01:01:03'}, table => $table1);
cleanup
Yuki Kimoto authored on 2011-08-16
3377
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3378
$dbi->type_rule(
3379
    into1 => [
3380
        [$date_typename, $datetime_typename] => sub {
3381
            my $value = shift;
3382
            $value =~ s/02/03/g;
3383
            return $value;
3384
        }
3385
    ]
cleanup test
Yuki Kimoto authored on 2011-08-10
3386
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3387
$result = $dbi->execute(
3388
    "select * from $table1 where $key1 = :$key1 and $key2 = :$table1.$key2",
3389
    param => {$key1 => '2010-01-03', "$table1.$key2" => '2010-01-01 01:01:02'}
cleanup test
Yuki Kimoto authored on 2011-08-10
3390
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3391
$row = $result->one;
3392
like($row->{$key1}, qr/^2010-01-03/);
3393
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
3394

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

            
3418
$dbi = DBIx::Custom->connect;
3419
eval { $dbi->execute("drop table $table1") };
3420
$dbi->execute($create_table1_type);
3421
$dbi->register_filter(convert => sub {
3422
    my $value = shift || '';
3423
    $value =~ s/02/03/;
3424
    return $value;
3425
});
cleanup
Yuki Kimoto authored on 2011-08-16
3426
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3427
$dbi->type_rule(
3428
    from1 => {
3429
        $date_datatype => 'convert',
3430
    },
3431
    into1 => {
3432
        $date_typename => 'convert',
3433
    }
3434
);
3435
$dbi->insert({$key1 => '2010-02-02'}, table => $table1);
3436
$result = $dbi->select(table => $table1);
3437
like($result->fetch->[0], qr/^2010-03-03/);
3438

            
3439
test 'type_rule and filter order';
3440
$dbi = DBIx::Custom->connect;
3441
eval { $dbi->execute("drop table $table1") };
3442
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3443
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3444
$dbi->type_rule(
3445
    into1 => {
3446
        $date_typename => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3447
    },
3448
    into2 => {
3449
        $date_typename => sub { my $v = shift || ''; $v =~ s/5/6/; return $v }
3450
    },
3451
    from1 => {
3452
        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/7/; return $v }
3453
    },
3454
    from2 => {
3455
        $date_datatype => sub { my $v = shift || ''; $v =~ s/7/8/; return $v }
3456
    }
3457
);
3458
$dbi->insert({$key1 => '2010-01-03'}, 
3459
  table => $table1, filter => {$key1 => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }});
3460
$result = $dbi->select(table => $table1);
3461
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/8/9/; return $v });
3462
like($result->fetch_first->[0], qr/^2010-01-09/);
3463

            
3464

            
3465
$dbi = DBIx::Custom->connect;
3466
eval { $dbi->execute("drop table $table1") };
3467
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3468
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3469
$dbi->type_rule(
3470
    from1 => {
3471
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3472
    },
3473
    from2 => {
3474
        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3475
    },
3476
);
3477
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3478
$result = $dbi->select(table => $table1);
cleanup
Yuki Kimoto authored on 2011-08-16
3479
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3480
$result->type_rule(
3481
    from1 => {
3482
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/6/; return $v }
3483
    },
3484
    from2 => {
3485
        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/8/; return $v }
3486
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3487
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3488
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/8/9/; return $v });
3489
like($result->fetch_first->[0], qr/^2010-01-09/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3490

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

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

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3570
eval{$dbi->type_rule(
3571
    into1 => {
3572
        $date_typename => 'pp'
3573
    }
3574
)};
3575
like($@, qr/not registered/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3576

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3577
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3578
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3579
$dbi->execute($create_table1_type);
cleanup test
Yuki Kimoto authored on 2011-08-10
3580
eval {
cleanup test
Yuki Kimoto authored on 2011-08-15
3581
    $dbi->type_rule(
3582
        from1 => {
3583
            Date => sub { $_[0] * 2 },
3584
        }
cleanup test
Yuki Kimoto authored on 2011-08-10
3585
    );
3586
};
cleanup test
Yuki Kimoto authored on 2011-08-15
3587
like($@, qr/lower/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3588

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3589
eval {
3590
    $dbi->type_rule(
3591
        into1 => {
3592
            Date => sub { $_[0] * 2 },
3593
        }
3594
    );
3595
};
3596
like($@, qr/lower/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3597

            
3598
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3599
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3600
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3601
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3602
$dbi->type_rule(
3603
    from1 => {
3604
        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3605
    },
3606
    into1 => {
3607
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3608
    }
3609
);
3610
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3611
$result = $dbi->select(table => $table1);
3612
$result->type_rule_off;
3613
like($result->one->{$key1}, qr/^2010-01-04/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3614

            
3615
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3616
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3617
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3618
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3619
$dbi->type_rule(
3620
    from1 => {
3621
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v },
3622
        $datetime_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3623
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
3624
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3625
$dbi->insert({$key1 => '2010-01-03', $key2 => '2010-01-01 01:01:03'}, table => $table1);
3626
$result = $dbi->select(table => $table1);
3627
$result->type_rule(
3628
    from1 => {
3629
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3630
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3631
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3632
$row = $result->one;
3633
like($row->{$key1}, qr/^2010-01-05/);
3634
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
3635

            
3636
$result = $dbi->select(table => $table1);
3637
$result->type_rule(
3638
    from1 => {
3639
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3640
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3641
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3642
$row = $result->one;
3643
like($row->{$key1}, qr/2010-01-05/);
3644
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3645

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

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

            
3664
$dbi->register_filter(five => sub { my $v = shift || ''; $v =~ s/3/5/; return $v });
3665
$result = $dbi->select(table => $table1);
3666
$result->type_rule(
3667
    from1 => [$date_datatype => 'five']
3668
);
3669
$row = $result->one;
3670
like($row->{$key1}, qr/^2010-01-05/);
3671
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
3672

            
3673
$result = $dbi->select(table => $table1);
3674
$result->type_rule(
3675
    from1 => [$date_datatype => undef]
3676
);
3677
$row = $result->one;
3678
like($row->{$key1}, qr/^2010-01-03/);
3679
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3680

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3733
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3734
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3735
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3736
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3737
$dbi->type_rule(
3738
    into1 => {
3739
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3740
    },
3741
    into2 => {
3742
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3743
    },
3744
    from1 => {
3745
        $date_datatype => sub { my $v = shift || ''; $v =~ s/(3|5)/6/; return $v }
3746
    },
3747
    from2 => {
3748
        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/7/; return $v }
3749
    }
3750
);
3751
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule1_off => 1);
3752
$result = $dbi->select(table => $table1);
3753
like($result->type_rule1_off->fetch_first->[0], qr/^2010-01-05/);
3754
$result = $dbi->select(table => $table1);
3755
like($result->type_rule1_on->fetch_first->[0], qr/^2010-01-07/);
test cleanup
Yuki Kimoto authored on 2011-08-10
3756

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

            
added memory leak check test
Yuki Kimoto authored on 2011-08-15
3781
test 'join';
3782
$dbi = DBIx::Custom->connect;
3783
eval { $dbi->execute("drop table $table1") };
3784
$dbi->execute($create_table1);
3785
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3786
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
3787
eval { $dbi->execute("drop table $table2") };
3788
$dbi->execute($create_table2);
3789
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3790
eval { $dbi->execute("drop table $table3") };
3791
$dbi->execute("create table $table3 ($key3 int, $key4 int)");
3792
$dbi->insert(table => $table3, param => {$key3 => 5, $key4 => 4});
3793
$rows = $dbi->select(
3794
    table => $table1,
3795
    column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
3796
    where   => {"$table1.$key2" => 2},
3797
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1"]
3798
)->all;
3799
is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}]);
3800

            
3801
$rows = $dbi->select(
3802
    table => $table1,
3803
    where   => {$key1 => 1},
3804
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1"]
3805
)->all;
3806
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
3807

            
3808
eval {
3809
    $rows = $dbi->select(
3810
        table => $table1,
3811
        column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
3812
        where   => {"$table1.$key2" => 2},
3813
        join  => {"$table1.$key1" => "$table2.$key1"}
3814
    );
3815
};
3816
like ($@, qr/array/);
3817

            
3818
$rows = $dbi->select(
3819
    table => $table1,
3820
    where   => {$key1 => 1},
3821
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
3822
              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
3823
)->all;
3824
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
3825

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

            
3835
$rows = $dbi->select(
3836
    column => "$table1.$key1 as ${table1}__$key1",
3837
    table => $table1,
3838
    where   => {"$table3.$key4" => 4},
3839
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
3840
              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
3841
)->all;
3842
is_deeply($rows, [{"${table1}__$key1" => 1}]);
3843

            
3844
$dbi = DBIx::Custom->connect;
3845
eval { $dbi->execute("drop table $table1") };
3846
$dbi->execute($create_table1);
3847
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3848
eval { $dbi->execute("drop table $table2") };
3849
$dbi->execute($create_table2);
3850
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3851
$rows = $dbi->select(
3852
    table => $table1,
3853
    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",
3854
    where   => {"$table1.$key2" => 2},
3855
    join  => ["left outer join ${q}$table2$p on ${q}$table1$p.${q}$key1$p = ${q}$table2$p.${q}$key1$p"],
3856
)->all;
3857
is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}],
3858
          'quote');
3859

            
3860

            
3861
$dbi = DBIx::Custom->connect;
3862
eval { $dbi->execute("drop table $table1") };
3863
$dbi->execute($create_table1);
3864
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3865
$sql = <<"EOS";
3866
left outer join (
3867
  select * from $table1 t1
3868
  where t1.$key2 = (
3869
    select max(t2.$key2) from $table1 t2
3870
    where t1.$key1 = t2.$key1
3871
  )
3872
) $table3 on $table1.$key1 = $table3.$key1
3873
EOS
3874
$join = [$sql];
3875
$rows = $dbi->select(
3876
    table => $table1,
3877
    column => "$table3.$key1 as ${table3}__$key1",
3878
    join  => $join
3879
)->all;
3880
is_deeply($rows, [{"${table3}__$key1" => 1}]);
3881

            
3882
$dbi = DBIx::Custom->connect;
3883
eval { $dbi->execute("drop table $table1") };
3884
eval { $dbi->execute("drop table $table2") };
3885
$dbi->execute($create_table1);
3886
$dbi->execute($create_table2);
3887
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3888
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4});
3889
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3890
$result = $dbi->select(
3891
    table => $table1,
3892
    join => [
3893
        "left outer join $table2 on $table2.$key2 = '4' and $table1.$key1 = $table2.$key1"
3894
    ]
3895
);
3896
is_deeply($result->all, [{$key1 => 1, $key2 => 2}]);
3897
$result = $dbi->select(
3898
    table => $table1,
3899
    column => [{$table2 => [$key3]}],
3900
    join => [
3901
        "left outer join $table2 on $table2.$key3 = '4' and $table1.$key1 = $table2.$key1"
3902
    ]
3903
);
3904
is_deeply($result->all, [{"$table2.$key3" => 4}]);
3905
$result = $dbi->select(
3906
    table => $table1,
3907
    column => [{$table2 => [$key3]}],
3908
    join => [
3909
        "left outer join $table2 on $table1.$key1 = $table2.$key1 and $table2.$key3 = '4'"
3910
    ]
3911
);
3912
is_deeply($result->all, [{"$table2.$key3" => 4}]);
3913

            
3914
$dbi = DBIx::Custom->connect;
3915
eval { $dbi->execute("drop table $table1") };
3916
eval { $dbi->execute("drop table $table2") };
3917
$dbi->execute($create_table1);
3918
$dbi->execute($create_table2);
3919
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3920
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4});
3921
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3922
$result = $dbi->select(
3923
    table => $table1,
3924
    column => [{$table2 => [$key3]}],
3925
    join => [
3926
        {
3927
            clause => "left outer join $table2 on $table2.$key3 = '4' and $table1.$key1 = $table2.$key1",
3928
            table => [$table1, $table2]
3929
        }
3930
    ]
3931
);
3932
is_deeply($result->all, [{"$table2.$key3" => 4}]);
3933

            
3934
test 'columns';
3935
$dbi = MyDBI1->connect;
3936
$model = $dbi->model($table1);
3937

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
3938
test 'count';
3939
$dbi = DBIx::Custom->connect;
3940
eval { $dbi->execute("drop table $table1") };
3941
$dbi->execute($create_table1);
3942
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3943
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 3});
3944
is($dbi->count(table => $table1), 2);
3945
is($dbi->count(table => $table1, where => {$key2 => 2}), 1);
3946
$model = $dbi->create_model(table => $table1);
3947
is($model->count, 2);
cleanup test
Yuki Kimoto authored on 2011-08-10
3948

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