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

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

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

            
14
plan 'no_plan';
15

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

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

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

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

            
86
    use strict;
87
    use warnings;
88

            
89
    use base 'DBIx::Custom';
90

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

            
102
    package MyModel2::Base1;
103

            
104
    use strict;
105
    use warnings;
106

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

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

            
111
    use strict;
112
    use warnings;
113

            
114
    use base 'MyModel2::Base1';
115

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

            
122
    sub list { shift->select; }
123

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

            
126
    use strict;
127
    use warnings;
128

            
129
    use base 'MyModel2::Base1';
130

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

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

            
139
    package MyModel2::TABLE1;
140

            
141
    use strict;
142
    use warnings;
143

            
144
    use base 'MyModel2::Base1';
145

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

            
152
    sub list { shift->select; }
153

            
154
    package MyModel2::TABLE2;
155

            
156
    use strict;
157
    use warnings;
158

            
159
    use base 'MyModel2::Base1';
160

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

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

            
172
    use strict;
173
    use warnings;
174

            
175
    use base 'DBIx::Custom';
176

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
509
eval { $dbi->execute("drop table $table1") };
510
$dbi->execute($create_table1);
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
511
$dbi->insert_timestamp(
512
    $key1 => '5'
513
);
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
514
$dbi->insert(table => $table1, param => {$key2 => 2}, timestamp => 1);
515
$result = $dbi->execute("select * from $table1");
516
$rows   = $result->all;
517
is_deeply($rows, [{$key1 => 5, $key2 => 2}], "basic");
518

            
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
519
eval { $dbi->execute("drop table $table1") };
520
$dbi->execute($create_table1);
521
$dbi->insert_timestamp(
522
    [$key1, $key2] => sub { 5 }
523
);
524
$dbi->insert(table => $table1, timestamp => 1);
525
$result = $dbi->execute("select * from $table1");
526
$rows   = $result->all;
527
is_deeply($rows, [{$key1 => 5, $key2 => 5}], "basic");
528

            
529
eval { $dbi->execute("drop table $table1") };
530
$dbi->execute($create_table1);
531
$dbi->insert_timestamp(
532
    [$key1, $key2] => sub { "" . DBIx::Custom->new }
533
);
534
$dbi->insert(table => $table1, timestamp => 1);
535
$result = $dbi->execute("select * from $table1");
536
$rows   = $result->all;
537
is($rows->[0]->{$key1}, $rows->[0]->{$key2});
538

            
test cleanup
Yuki Kimoto authored on 2011-08-10
539
test 'update';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
540
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
541
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
542
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
543
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
544
$dbi->update(table => $table1, param => {$key2 => 11}, where => {$key1 => 1});
cleanup test
Yuki Kimoto authored on 2011-08-15
545
$result = $dbi->execute("select * from $table1 order by $key1");
test cleanup
Yuki Kimoto authored on 2011-08-10
546
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
547
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
548
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
549
                  "basic");
550
                  
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
551
$dbi->execute("delete from $table1");
552
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
553
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
554
$dbi->update(table => $table1, param => {$key2 => 12}, where => {$key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-15
555
$result = $dbi->execute("select * from $table1 order by $key1");
test cleanup
Yuki Kimoto authored on 2011-08-10
556
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
557
is_deeply($rows, [{$key1 => 1, $key2 => 12, $key3 => 3, $key4 => 4, $key5 => 5},
558
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
559
                  "update key same as search key");
560

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
568
$dbi->execute("delete from $table1");
569
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
570
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
test cleanup
Yuki Kimoto authored on 2011-08-10
571
$dbi->register_filter(twice => sub { $_[0] * 2 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
572
$dbi->update(table => $table1, param => {$key2 => 11}, where => {$key1 => 1},
573
              filter => {$key2 => sub { $_[0] * 2 }});
cleanup test
Yuki Kimoto authored on 2011-08-15
574
$result = $dbi->execute("select * from $table1 order by $key1");
test cleanup
Yuki Kimoto authored on 2011-08-10
575
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
576
is_deeply($rows, [{$key1 => 1, $key2 => 22, $key3 => 3, $key4 => 4, $key5 => 5},
577
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
578
                  "filter");
579

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

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

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

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

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
598
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
599
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
600
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
601
$dbi->update(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
602
    table => $table1,
603
    param => {$key1 => 3},
test cleanup
Yuki Kimoto authored on 2011-08-10
604
    where => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
605
        ['and', "$key1 = :$key1", "$key2 = :$key2"],
606
        {$key1 => 1, $key2 => 2}
test cleanup
Yuki Kimoto authored on 2011-08-10
607
    ]
608
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
609
$result = $dbi->select(table => $table1);
610
is_deeply($result->all, [{$key1 => 3, $key2 => 2}], 'update() where');
test cleanup
Yuki Kimoto authored on 2011-08-10
611

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
612
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
613
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
614
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
615
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
616
$where->clause(['and', "$key2 = :$key2"]);
617
$where->param({$key2 => 2});
618
$dbi->update(table => $table1, param => {$key1 => 3}, where => $where);
619
$result = $dbi->select(table => $table1);
620
is_deeply($result->all, [{$key1 => 3, $key2 => 2}], 'update() where');
test cleanup
Yuki Kimoto authored on 2011-08-10
621

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

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

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
628
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
629
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
630
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
631
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
632
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
633
$dbi->insert(table => 'table', param => {select => 1});
634
$dbi->update(table => 'table', where => {select => 1}, param => {update => 2});
635
$result = $dbi->execute("select * from ${q}table$p");
636
$rows   = $result->all;
637
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
638

            
639
eval {$dbi->update_all(table => 'table', param => {';' => 2}) };
640
like($@, qr/safety/);
641

            
642
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
643
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
644
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
645
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
646
$dbi->insert(table => 'table', param => {select => 1});
647
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
648
$result = $dbi->execute("select * from ${q}table$p");
649
$rows   = $result->all;
650
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
651

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

            
updated pod
Yuki Kimoto authored on 2011-09-02
663
eval { $dbi->execute("drop table $table1") };
664
$dbi->execute($create_table1_2);
665
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
666
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
667
$dbi->update(table => $table1, param => {$key2 => 11}, where => {$key1 => 1},
668
wrap => {$key2 => sub { "$_[0] - 1" }});
669
$result = $dbi->execute("select * from $table1 order by $key1");
670
$rows   = $result->all;
671
is_deeply($rows, [{$key1 => 1, $key2 => 10, $key3 => 3, $key4 => 4, $key5 => 5},
672
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
673
                  "basic");
674

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
675
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
676
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
677
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
678
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
679
$dbi->update(table => $table1, param => {$key2 => \"'11'"}, where => {$key1 => 1});
cleanup test
Yuki Kimoto authored on 2011-08-15
680
$result = $dbi->execute("select * from $table1 order by $key1");
test cleanup
Yuki Kimoto authored on 2011-08-10
681
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
682
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
683
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
684
                  "basic");
685

            
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
686
eval { $dbi->execute("drop table $table1") };
687
$dbi->execute($create_table1);
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
688
$dbi->update_timestamp(
689
    $key1 => '5'
690
);
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
691
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
692
$dbi->update(table => $table1, timestamp => 1, where => {$key2 => 2});
693
$result = $dbi->execute("select * from $table1");
694
$rows   = $result->all;
695
is_deeply($rows, [{$key1 => 5, $key2 => 2}], "basic");
696

            
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
697
eval { $dbi->execute("drop table $table1") };
698
$dbi->execute($create_table1);
699
$dbi->update_timestamp(
700
    [$key1, $key2] => sub { '5' }
701
);
702
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
703
$dbi->update_all(table => $table1, timestamp => 1);
704
$result = $dbi->execute("select * from $table1");
705
$rows   = $result->all;
706
is_deeply($rows, [{$key1 => 5, $key2 => 5}], "basic");
707

            
708
eval { $dbi->execute("drop table $table1") };
709
$dbi->execute($create_table1);
710
$dbi->update_timestamp(
711
    [$key1, $key2] => sub { "" . DBIx::Custom->new }
712
);
713
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
714
$dbi->update_all(table => $table1, timestamp => 1);
715
$result = $dbi->execute("select * from $table1");
716
$rows   = $result->all;
717
is($rows->[0]->{$key1}, $rows->[0]->{$key2});
718

            
test cleanup
Yuki Kimoto authored on 2011-08-10
719
test 'update_all';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
720
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
721
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
722
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
723
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
test cleanup
Yuki Kimoto authored on 2011-08-10
724
$dbi->register_filter(twice => sub { $_[0] * 2 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
725
$dbi->update_all(table => $table1, param => {$key2 => 10}, filter => {$key2 => 'twice'});
cleanup test
Yuki Kimoto authored on 2011-08-15
726
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
727
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
728
is_deeply($rows, [{$key1 => 1, $key2 => 20, $key3 => 3, $key4 => 4, $key5 => 5},
729
                  {$key1 => 6, $key2 => 20, $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
730
                  "filter");
731

            
732

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
743
$dbi->execute("delete from $table1");
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
744
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
745
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-10
746
$dbi->register_filter(twice => sub { $_[0] * 2 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
747
$dbi->delete(table => $table1, where => {$key2 => 1}, filter => {$key2 => 'twice'});
cleanup test
Yuki Kimoto authored on 2011-08-15
748
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
749
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
750
is_deeply($rows, [{$key1 => 3, $key2 => 4}], "filter");
test cleanup
Yuki Kimoto authored on 2011-08-10
751

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
754
$dbi->delete_all(table => $table1);
755
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
756
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
757
$dbi->delete(table => $table1, where => {$key1 => 1, $key2 => 2});
758
$rows = $dbi->select(table => $table1)->all;
759
is_deeply($rows, [{$key1 => 3, $key2 => 4}], "delete multi key");
test cleanup
Yuki Kimoto authored on 2011-08-10
760

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

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

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
775
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
776
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
777
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
778
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-10
779
$dbi->delete(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
780
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
781
    where => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
782
        ['and', "$key1 = :$key1", "$key2 = :$key2"],
783
        {ke1 => 1, $key2 => 2}
test cleanup
Yuki Kimoto authored on 2011-08-10
784
    ]
785
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
786
$result = $dbi->select(table => $table1);
787
is_deeply($result->all, [{$key1 => 3, $key2 => 4}], 'delete() where');
test cleanup
Yuki Kimoto authored on 2011-08-10
788

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
789
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
790
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
791
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
792
$dbi->delete(table => $table1, where => {$key1 => 1}, prefix => '    ');
cleanup test
Yuki Kimoto authored on 2011-08-15
793
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
794
$rows   = $result->all;
795
is_deeply($rows, [], "basic");
796

            
797
test 'delete error';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
798
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
799
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
800
eval{$dbi->delete(table => $table1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
801
like($@, qr/"where" must be specified/,
802
         "where key-value pairs not specified");
803

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

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

            
818
test 'delete_all';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
819
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
820
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
821
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
822
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
823
$dbi->delete_all(table => $table1);
cleanup test
Yuki Kimoto authored on 2011-08-15
824
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
825
$rows   = $result->all;
826
is_deeply($rows, [], "basic");
827

            
828

            
829
test 'select';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
830
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
831
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
832
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
833
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
834
$rows = $dbi->select(table => $table1)->all;
835
is_deeply($rows, [{$key1 => 1, $key2 => 2},
836
                  {$key1 => 3, $key2 => 4}], "table");
test cleanup
Yuki Kimoto authored on 2011-08-10
837

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

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

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

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
852
eval { $dbi->execute("drop table $table2") };
test cleanup
Yuki Kimoto authored on 2011-08-10
853
$dbi->execute($create_table2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
854
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
test cleanup
Yuki Kimoto authored on 2011-08-10
855
$rows = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
856
    table => [$table1, $table2],
857
    column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
858
    where   => {"$table1.$key2" => 2},
859
    relation  => {"$table1.$key1" => "$table2.$key1"}
test cleanup
Yuki Kimoto authored on 2011-08-10
860
)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
861
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
862

            
863
$rows = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
864
    table => [$table1, $table2],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
865
    column => ["$table1.$key1 as ${table1}_$key1", "${table2}.$key1 as ${table2}_$key1", $key2, $key3],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
866
    relation  => {"$table1.$key1" => "$table2.$key1"}
test cleanup
Yuki Kimoto authored on 2011-08-10
867
)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
868
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
869

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

            
873
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
874
eval { $dbi->execute("drop table ${q}table$p") };
875
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
876
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
877
$dbi->insert(table => 'table', param => {select => 1, update => 2});
878
$result = $dbi->select(table => 'table', where => {select => 1});
879
$rows   = $result->all;
880
is_deeply($rows, [{select => 2, update => 2}], "reserved word");
881

            
882
test 'fetch filter';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
883
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
884
$dbi->register_filter(
885
    twice       => sub { $_[0] * 2 },
886
    three_times => sub { $_[0] * 3 }
887
);
888
$dbi->default_fetch_filter('twice');
889
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
890
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
891
$result = $dbi->select(table => $table1);
892
$result->filter({$key1 => 'three_times'});
test cleanup
Yuki Kimoto authored on 2011-08-10
893
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
894
is_deeply($row, {$key1 => 3, $key2 => 4}, "default_fetch_filter and filter");
test cleanup
Yuki Kimoto authored on 2011-08-10
895

            
896
test 'filters';
897
$dbi = DBIx::Custom->new;
898

            
899
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')),
900
   'あ', "decode_utf8");
901

            
902
is($dbi->filters->{encode_utf8}->('あ'),
903
   encode_utf8('あ'), "encode_utf8");
904

            
cleanup test
Yuki Kimoto authored on 2011-08-10
905
test 'transaction1';
test cleanup
Yuki Kimoto authored on 2011-08-10
906
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
907
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
908
$dbi->execute($create_table1);
fixed transaction test
Yuki Kimoto authored on 2011-08-14
909
$dbi->begin_work;
910
$dbi->dbh->{AutoCommit} = 0;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
911
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
fixed transaction test
Yuki Kimoto authored on 2011-08-14
912
$dbi->rollback;
913
$dbi->dbh->{AutoCommit} = 1;
914

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

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

            
919
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
920
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
921
$dbi->execute($create_table1);
fixed transaction test
Yuki Kimoto authored on 2011-08-14
922
$dbi->begin_work;
923
$dbi->dbh->{AutoCommit} = 0;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
924
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
925
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
fixed transaction test
Yuki Kimoto authored on 2011-08-14
926
$dbi->commit;
927
$dbi->dbh->{AutoCommit} = 1;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
928
$result = $dbi->select(table => $table1);
929
is_deeply(scalar $result->all, [{$key1 => 1, $key2 => 2}, {$key1 => 2, $key2 => 3}],
fixed transaction test
Yuki Kimoto authored on 2011-08-14
930
          "commit");
test cleanup
Yuki Kimoto authored on 2011-08-10
931

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

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

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

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

            
966

            
cleanup test
Yuki Kimoto authored on 2011-08-10
967
test 'transaction2';
test cleanup
Yuki Kimoto authored on 2011-08-10
968
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
969
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
970
$dbi->execute($create_table1);
971

            
972
$dbi->begin_work;
973

            
974
eval {
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
975
    $dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
976
    die "Error";
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
977
    $dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-10
978
};
979

            
980
$dbi->rollback if $@;
981

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

            
986
$dbi->begin_work;
987

            
988
eval {
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
989
    $dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
990
    $dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-10
991
};
992

            
993
$dbi->commit unless $@;
994

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

            
999
$dbi->dbh->{AutoCommit} = 0;
1000
eval{ $dbi->begin_work };
1001
ok($@, "exception");
1002
$dbi->dbh->{AutoCommit} = 1;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
1003

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1004
test 'cache';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1005
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1006
$dbi->cache(1);
1007
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-15
1008
$source = "select * from $table1 where $key1 = :$key1 and $key2 = :$key2";
test cleanup
Yuki Kimoto authored on 2011-08-10
1009
$dbi->execute($source, {}, query => 1);
1010
is_deeply($dbi->{_cached}->{$source}, 
cleanup test
Yuki Kimoto authored on 2011-08-15
1011
          {sql => "select * from $table1 where $key1 = ? and $key2 = ?", columns => [$key1, $key2], tables => []}, "cache");
test cleanup
Yuki Kimoto authored on 2011-08-10
1012

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1013
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1014
$dbi->execute($create_table1);
1015
$dbi->{_cached} = {};
1016
$dbi->cache(0);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1017
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
1018
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
1019

            
1020
test 'execute';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1021
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1022
$dbi->execute($create_table1);
1023
{
1024
    local $Carp::Verbose = 0;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1025
    eval{$dbi->execute("select * frm $table1")};
cleanup test
Yuki Kimoto authored on 2011-08-15
1026
    like($@, qr/\Qselect * frm $table1/, "fail prepare");
test cleanup
Yuki Kimoto authored on 2011-08-10
1027
    like($@, qr/\.t /, "fail : not verbose");
1028
}
1029
{
1030
    local $Carp::Verbose = 1;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1031
    eval{$dbi->execute('select * frm $table1')};
test cleanup
Yuki Kimoto authored on 2011-08-10
1032
    like($@, qr/Custom.*\.t /s, "fail : verbose");
1033
}
1034

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

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

            
1043
{
1044
    local $Carp::Verbose = 0;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1045
    eval{$dbi->execute("select * from $table1 where {0 $key1}", {}, query => 1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
1046
    like($@, qr/\Q.t /, "caller spec : not vebose");
1047
}
1048
{
1049
    local $Carp::Verbose = 1;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1050
    eval{$dbi->execute("select * from $table1 where {0 $key1}", {}, query => 1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
1051
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
1052
}
1053

            
1054
test 'method';
1055
$dbi->method(
1056
    one => sub { 1 }
1057
);
1058
$dbi->method(
1059
    two => sub { 2 }
1060
);
1061
$dbi->method({
1062
    twice => sub {
1063
        my $self = shift;
1064
        return $_[0] * 2;
1065
    }
1066
});
1067

            
1068
is($dbi->one, 1, "first");
1069
is($dbi->two, 2, "second");
1070
is($dbi->twice(5), 10 , "second");
1071

            
1072
eval {$dbi->XXXXXX};
1073
ok($@, "not exists");
1074

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

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

            
1108
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1109
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1110
$dbi->execute($create_table1);
1111
$dbi->register_filter(twice => sub { $_[0] * 2 });
1112
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1113
    $table1, $key1 => {out => 'twice', in => 'twice'}
test cleanup
Yuki Kimoto authored on 2011-08-10
1114
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1115
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}, filter => {$key1 => undef});
1116
$dbi->update(table => $table1, param => {$key1 => 2}, where => {$key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-15
1117
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
1118
$row   = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1119
is_deeply($row, {$key1 => 4, $key2 => 2}, "update");
test cleanup
Yuki Kimoto authored on 2011-08-10
1120

            
1121
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1122
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1123
$dbi->execute($create_table1);
1124
$dbi->register_filter(twice => sub { $_[0] * 2 });
1125
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1126
    $table1, $key1 => {out => 'twice', in => 'twice'}
test cleanup
Yuki Kimoto authored on 2011-08-10
1127
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1128
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 2}, filter => {$key1=> undef});
1129
$dbi->delete(table => $table1, where => {$key1 => 1});
cleanup test
Yuki Kimoto authored on 2011-08-15
1130
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
1131
$rows   = $result->all;
1132
is_deeply($rows, [], "delete");
1133

            
1134
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1135
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1136
$dbi->execute($create_table1);
1137
$dbi->register_filter(twice => sub { $_[0] * 2 });
1138
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1139
    $table1, $key1 => {out => 'twice', in => 'twice'}
test cleanup
Yuki Kimoto authored on 2011-08-10
1140
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1141
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 2}, filter => {$key1 => undef});
1142
$result = $dbi->select(table => $table1, where => {$key1 => 1});
1143
$result->filter({$key2 => 'twice'});
test cleanup
Yuki Kimoto authored on 2011-08-10
1144
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1145
is_deeply($rows, [{$key1 => 4, $key2 => 4}], "select");
test cleanup
Yuki Kimoto authored on 2011-08-10
1146

            
1147
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1148
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1149
$dbi->execute($create_table1);
1150
$dbi->register_filter(twice => sub { $_[0] * 2 });
1151
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1152
    $table1, $key1 => {out => 'twice', in => 'twice'}
test cleanup
Yuki Kimoto authored on 2011-08-10
1153
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1154
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 2}, filter => {$key1 => undef});
cleanup test
Yuki Kimoto authored on 2011-08-15
1155
$result = $dbi->execute("select * from $table1 where $key1 = :$key1 and $key2 = :$key2",
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1156
                        param => {$key1 => 1, $key2 => 2},
1157
                        table => [$table1]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1158
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1159
is_deeply($rows, [{$key1 => 4, $key2 => 2}], "execute");
test cleanup
Yuki Kimoto authored on 2011-08-10
1160

            
1161
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1162
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1163
$dbi->execute($create_table1);
1164
$dbi->register_filter(twice => sub { $_[0] * 2 });
1165
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1166
    $table1, $key1 => {out => 'twice', in => 'twice'}
test cleanup
Yuki Kimoto authored on 2011-08-10
1167
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1168
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 2}, filter => {$key1 => undef});
cleanup test
Yuki Kimoto authored on 2011-08-15
1169
$result = $dbi->execute("select * from {table $table1} where $key1 = :$key1 and $key2 = :$key2",
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1170
                        param => {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
1171
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1172
is_deeply($rows, [{$key1 => 4, $key2 => 2}], "execute table tag");
test cleanup
Yuki Kimoto authored on 2011-08-10
1173

            
1174
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1175
eval { $dbi->execute("drop table $table1") };
1176
eval { $dbi->execute("drop table $table2") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1177
$dbi->execute($create_table1);
1178
$dbi->execute($create_table2);
1179
$dbi->register_filter(twice => sub { $_[0] * 2 });
1180
$dbi->register_filter(three_times => sub { $_[0] * 3 });
1181
$dbi->apply_filter(
cleanup test
Yuki Kimoto authored on 2011-08-15
1182
    $table1, $key2 => {out => 'twice', in => 'twice'}
1183
);
1184
$dbi->apply_filter(
1185
    $table2, $key3 => {out => 'three_times', in => 'three_times'}
test cleanup
Yuki Kimoto authored on 2011-08-10
1186
);
cleanup test
Yuki Kimoto authored on 2011-08-15
1187
$dbi->insert(table => $table1, param => {$key1 => 5, $key2 => 2}, filter => {$key2 => undef});
1188
$dbi->insert(table => $table2, param => {$key1 => 5, $key3 => 6}, filter => {$key3 => undef});
1189
$result = $dbi->select(
1190
     table => [$table1, $table2],
1191
     column => [$key2, $key3],
1192
     where => {"$table1.$key2" => 1, "$table2.$key3" => 2}, relation => {"$table1.$key1" => "$table2.$key1"});
1193

            
1194
$result->filter({$key2 => 'twice'});
1195
$rows   = $result->all;
1196
is_deeply($rows, [{$key2 => 4, $key3 => 18}], "select : join");
1197

            
1198
$result = $dbi->select(
1199
     table => [$table1, $table2],
1200
     column => [$key2, $key3],
1201
     where => {$key2 => 1, $key3 => 2}, relation => {"$table1.$key1" => "$table2.$key1"});
1202

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

            
1207
test 'connect super';
test cleanup
Yuki Kimoto authored on 2011-08-10
1208
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1209
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1210
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1211
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1212
is($dbi->select(table => $table1)->one->{$key1}, 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1213

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1214
$dbi = DBIx::Custom->new;
test cleanup
Yuki Kimoto authored on 2011-08-10
1215
$dbi->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1216
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1217
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1218
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1219
is($dbi->select(table => $table1)->one->{$key1}, 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1220

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

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

            
1238
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1239
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1240
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1241
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1242
$result = $dbi->select(table => $table1);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1243
$result->filter([$key1, $key2] => sub { $_[0] * 2 });
1244
$result->end_filter([[$key1, $key2] => sub { $_[0] * 3 }]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1245
$row = $result->fetch_first;
1246
is_deeply($row, [6, 12]);
1247

            
1248
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1249
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1250
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1251
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1252
$result = $dbi->select(table => $table1);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1253
$result->filter([[$key1, $key2] => sub { $_[0] * 2 }]);
1254
$result->end_filter([$key1, $key2] => sub { $_[0] * 3 });
test cleanup
Yuki Kimoto authored on 2011-08-10
1255
$row = $result->fetch_first;
1256
is_deeply($row, [6, 12]);
1257

            
1258
$dbi->register_filter(five_times => sub { $_[0] * 5 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1259
$result = $dbi->select(table => $table1);
1260
$result->filter($key1 => sub { $_[0] * 2 }, $key2 => sub { $_[0] * 4 });
1261
$result->end_filter({$key1 => sub { $_[0] * 3 }, $key2 => 'five_times' });
test cleanup
Yuki Kimoto authored on 2011-08-10
1262
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1263
is_deeply($row, {$key1 => 6, $key2 => 40});
test cleanup
Yuki Kimoto authored on 2011-08-10
1264

            
1265
$dbi->register_filter(five_times => sub { $_[0] * 5 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1266
$dbi->apply_filter($table1,
1267
    $key1 => {end => sub { $_[0] * 3 } },
1268
    $key2 => {end => 'five_times'}
test cleanup
Yuki Kimoto authored on 2011-08-10
1269
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1270
$result = $dbi->select(table => $table1);
1271
$result->filter($key1 => sub { $_[0] * 2 }, $key2 => sub { $_[0] * 4 });
test cleanup
Yuki Kimoto authored on 2011-08-10
1272
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1273
is_deeply($row, {$key1 => 6, $key2 => 40}, 'apply_filter');
test cleanup
Yuki Kimoto authored on 2011-08-10
1274

            
1275
$dbi->register_filter(five_times => sub { $_[0] * 5 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1276
$dbi->apply_filter($table1,
1277
    $key1 => {end => sub { $_[0] * 3 } },
1278
    $key2 => {end => 'five_times'}
1279
);
1280
$result = $dbi->select(table => $table1);
1281
$result->filter($key1 => sub { $_[0] * 2 }, $key2 => sub { $_[0] * 4 });
1282
$result->filter($key1 => undef);
1283
$result->end_filter($key1 => undef);
test cleanup
Yuki Kimoto authored on 2011-08-10
1284
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1285
is_deeply($row, {$key1 => 1, $key2 => 40}, 'apply_filter overwrite');
test cleanup
Yuki Kimoto authored on 2011-08-10
1286

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1287
test 'remove_end_filter and remove_filter';
1288
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1289
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1290
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1291
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1292
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1293
$row = $result
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1294
       ->filter($key1 => sub { $_[0] * 2 }, $key2 => sub { $_[0] * 4 })
test cleanup
Yuki Kimoto authored on 2011-08-10
1295
       ->remove_filter
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1296
       ->end_filter($key1 => sub { $_[0] * 3 }, $key2 => sub { $_[0] * 5 })
test cleanup
Yuki Kimoto authored on 2011-08-10
1297
       ->remove_end_filter
1298
       ->fetch_first;
1299
is_deeply($row, [1, 2]);
1300

            
1301
test 'empty where select';
1302
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1303
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1304
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1305
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1306
$result = $dbi->select(table => $table1, where => {});
test cleanup
Yuki Kimoto authored on 2011-08-10
1307
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1308
is_deeply($row, {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
1309

            
1310
test 'select query option';
1311
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1312
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1313
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1314
$query = $dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1315
is(ref $query, 'DBIx::Custom::Query');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1316
$query = $dbi->update(table => $table1, where => {$key1 => 1}, param => {$key2 => 2}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1317
is(ref $query, 'DBIx::Custom::Query');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1318
$query = $dbi->delete(table => $table1, where => {$key1 => 1}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1319
is(ref $query, 'DBIx::Custom::Query');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1320
$query = $dbi->select(table => $table1, where => {$key1 => 1, $key2 => 2}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1321
is(ref $query, 'DBIx::Custom::Query');
1322

            
1323
test 'where';
1324
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1325
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1326
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1327
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1328
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
1329
$where = $dbi->where->clause(['and', "$key1 = :$key1", "$key2 = :$key2"]);
1330
is("$where", "where ( $key1 = :$key1 and $key2 = :$key2 )", 'no param');
test cleanup
Yuki Kimoto authored on 2011-08-10
1331

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

            
1336
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1337
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1338
    where => $where
1339
);
1340
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1341
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1342

            
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 => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1346
        ['and', "$key1 = :$key1", "$key2 = :$key2"],
1347
        {$key1 => 1}
test cleanup
Yuki Kimoto authored on 2011-08-10
1348
    ]
1349
);
1350
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1351
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1352

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

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

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

            
1383
$where = $dbi->where;
1384
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1385
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1386
    where => $where
1387
);
1388
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1389
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1390

            
1391
eval {
1392
$where = $dbi->where
1393
             ->clause(['uuu']);
1394
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1395
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1396
    where => $where
1397
);
1398
};
1399
ok($@);
1400

            
1401
$where = $dbi->where;
1402
is("$where", '');
1403

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

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

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

            
1434
$where = $dbi->where
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1435
             ->clause("$key1 = :$key1")
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1436
             ->param({$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1437
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1438
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1439
    where => $where,
1440
);
1441
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1442
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1443

            
1444
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1445
             ->clause("$key1 = :$key1 $key2 = :$key2")
1446
             ->param({$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1447
eval{$where->to_string};
1448
like($@, qr/one column/);
1449

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

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

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

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

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

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

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

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

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

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

            
1550
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1551
             ->clause(['and', "{> $key1}", "{< $key1}" ])
1552
             ->param({$key1 => [$dbi->not_exists, $dbi->not_exists]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1553
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1554
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1555
    where => $where,
1556
);
1557
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1558
is_deeply($row, [{$key1 => 1, $key2 => 2},{$key1 => 3, $key2 => 4}], 'not_exists');
test cleanup
Yuki Kimoto authored on 2011-08-10
1559

            
1560
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1561
             ->clause(['and', "{> $key1}", "{< $key1}" ])
1562
             ->param({$key1 => [0, 2]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1563
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1564
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1565
    where => $where,
1566
);
1567
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1568
is_deeply($row, [{$key1 => 1, $key2 => 2}], 'not_exists');
test cleanup
Yuki Kimoto authored on 2011-08-10
1569

            
1570
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1571
             ->clause(['and',"$key1 is not null", "$key2 is not null" ]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1572
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1573
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1574
    where => $where,
1575
);
1576
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1577
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], 'not_exists');
test cleanup
Yuki Kimoto authored on 2011-08-10
1578

            
1579
eval {$dbi->where(ppp => 1) };
1580
like($@, qr/invalid/);
1581

            
1582
$where = $dbi->where(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1583
    clause => ['and', ['or'], ['and', "$key1 = :$key1", "$key2 = :$key2"]],
1584
    param => {$key1 => 1, $key2 => 2}
test cleanup
Yuki Kimoto authored on 2011-08-10
1585
);
1586
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1587
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1588
    where => $where,
1589
);
1590
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1591
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1592

            
1593

            
1594
$where = $dbi->where(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1595
    clause => ['and', ['or'], ['or', ":$key1", ":$key2"]],
test cleanup
Yuki Kimoto authored on 2011-08-10
1596
    param => {}
1597
);
1598
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1599
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1600
    where => $where,
1601
);
1602
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1603
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1604

            
1605
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1606
$where->clause(['and', ":${key1}{=}"]);
1607
$where->param({$key1 => undef});
1608
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1609
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1610
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1611

            
1612
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1613
$where->clause(['or', ":${key1}{=}", ":${key1}{=}"]);
1614
$where->param({$key1 => [undef, undef]});
1615
$result = $dbi->execute("select * from $table1 $where", {$key1 => [1, 0]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1616
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1617
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
1618
$result = $dbi->execute("select * from $table1 $where", {$key1 => [0, 1]});
test cleanup
Yuki Kimoto authored on 2011-08-10
1619
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1620
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1621

            
1622
test 'register_tag_processor';
1623
$dbi = DBIx::Custom->connect;
1624
$dbi->register_tag_processor(
1625
    a => sub { 1 }
1626
);
test cleanup
Yuki Kimoto authored on 2011-08-10
1627
is($dbi->{_tags}->{a}->(), 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1628

            
1629
test 'register_tag';
1630
$dbi = DBIx::Custom->connect;
1631
$dbi->register_tag(
1632
    b => sub { 2 }
1633
);
test cleanup
Yuki Kimoto authored on 2011-08-10
1634
is($dbi->{_tags}->{b}->(), 2);
test cleanup
Yuki Kimoto authored on 2011-08-10
1635

            
1636
test 'table not specify exception';
1637
$dbi = DBIx::Custom->connect;
1638
eval {$dbi->insert};
1639
like($@, qr/table/);
1640
eval {$dbi->update};
1641
like($@, qr/table/);
1642
eval {$dbi->delete};
1643
like($@, qr/table/);
1644
eval {$dbi->select};
1645
like($@, qr/table/);
test cleanup
Yuki Kimoto authored on 2011-08-10
1646

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1647
test 'more tests';
1648
$dbi = DBIx::Custom->connect;
1649
eval{$dbi->apply_filter('table', 'column', [])};
1650
like($@, qr/apply_filter/);
1651

            
1652
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
1653
like($@, qr/apply_filter/);
1654

            
1655
$dbi->apply_filter(
1656

            
1657
);
1658
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1659
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1660
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1661
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1662
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
1663
$dbi->apply_filter($table1, $key2, 
test cleanup
Yuki Kimoto authored on 2011-08-10
1664
                   {in => sub { $_[0] * 3 }, out => sub { $_[0] * 2 }});
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1665
$rows = $dbi->select(table => $table1, where => {$key2 => 1})->all;
1666
is_deeply($rows, [{$key1 => 1, $key2 => 6}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1667

            
1668
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1669
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1670
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1671
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1672
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
1673
$dbi->apply_filter($table1, $key2, {});
1674
$rows = $dbi->select(table => $table1, where => {$key2 => 2})->all;
1675
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1676

            
1677
$dbi = DBIx::Custom->connect;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1678
eval {$dbi->apply_filter($table1, $key2, {out => 'no'})};
test cleanup
Yuki Kimoto authored on 2011-08-10
1679
like($@, qr/not registered/);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1680
eval {$dbi->apply_filter($table1, $key2, {in => 'no'})};
test cleanup
Yuki Kimoto authored on 2011-08-10
1681
like($@, qr/not registered/);
1682
$dbi->method({one => sub { 1 }});
1683
is($dbi->one, 1);
1684

            
1685
eval{DBIx::Custom->connect(dsn => undef)};
1686
like($@, qr/_connect/);
1687

            
1688
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1689
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1690
$dbi->execute($create_table1);
1691
$dbi->register_filter(twice => sub { $_[0] * 2 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1692
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2},
1693
             filter => {$key1 => 'twice'});
1694
$row = $dbi->select(table => $table1)->one;
1695
is_deeply($row, {$key1 => 2, $key2 => 2});
1696
eval {$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2},
1697
             filter => {$key1 => 'no'}) };
test cleanup
Yuki Kimoto authored on 2011-08-10
1698
like($@, qr//);
1699

            
1700
$dbi->register_filter(one => sub { });
1701
$dbi->default_fetch_filter('one');
1702
ok($dbi->default_fetch_filter);
1703
$dbi->default_bind_filter('one');
1704
ok($dbi->default_bind_filter);
1705
eval{$dbi->default_fetch_filter('no')};
1706
like($@, qr/not registered/);
1707
eval{$dbi->default_bind_filter('no')};
1708
like($@, qr/not registered/);
1709
$dbi->default_bind_filter(undef);
1710
ok(!defined $dbi->default_bind_filter);
1711
$dbi->default_fetch_filter(undef);
1712
ok(!defined $dbi->default_fetch_filter);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1713
eval {$dbi->execute("select * from $table1 {} {= author") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1714
like($@, qr/Tag not finished/);
1715

            
1716
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1717
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1718
$dbi->execute($create_table1);
1719
$dbi->register_filter(one => sub { 1 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1720
$result = $dbi->select(table => $table1);
1721
eval {$result->filter($key1 => 'no')};
test cleanup
Yuki Kimoto authored on 2011-08-10
1722
like($@, qr/not registered/);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1723
eval {$result->end_filter($key1 => 'no')};
test cleanup
Yuki Kimoto authored on 2011-08-10
1724
like($@, qr/not registered/);
1725
$result->default_filter(undef);
1726
ok(!defined $result->default_filter);
1727
$result->default_filter('one');
1728
is($result->default_filter->(), 1);
1729

            
1730
test 'dbi_option';
1731
$dbi = DBIx::Custom->connect(dbi_option => {PrintError => 1});
1732
ok($dbi->dbh->{PrintError});
1733
$dbi = DBIx::Custom->connect(dbi_options => {PrintError => 1});
1734
ok($dbi->dbh->{PrintError});
1735

            
1736
test 'DBIx::Custom::Result stash()';
1737
$result = DBIx::Custom::Result->new;
1738
is_deeply($result->stash, {}, 'default');
1739
$result->stash->{foo} = 1;
1740
is($result->stash->{foo}, 1, 'get and set');
test cleanup
Yuki Kimoto authored on 2011-08-10
1741

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1754
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1755
$dbi->delete_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1756
    table => $table1,
1757
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
1758
    where => 1,
1759
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1760
is_deeply($dbi->select(table => $table1)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
1761

            
1762
test 'insert_at';
1763
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1764
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
1765
$dbi->execute($create_table1_2);
1766
$dbi->insert_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1767
    primary_key => [$key1, $key2], 
1768
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
1769
    where => [1, 2],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1770
    param => {$key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
1771
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1772
is($dbi->select(table => $table1)->one->{$key1}, 1);
1773
is($dbi->select(table => $table1)->one->{$key2}, 2);
1774
is($dbi->select(table => $table1)->one->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
1775

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1776
$dbi->delete_all(table => $table1);
1777
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1778
$dbi->insert_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1779
    primary_key => $key1, 
1780
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
1781
    where => 1,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1782
    param => {$key2 => 2, $key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
1783
);
1784

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

            
1789
eval {
1790
    $dbi->insert_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1791
        table => $table1,
1792
        primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1793
        where => {},
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1794
        param => {$key1 => 1, $key2 => 2, $key3 => 3},
cleanup test
Yuki Kimoto authored on 2011-08-10
1795
    );
1796
};
1797
like($@, qr/must be/);
1798

            
1799
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1800
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
1801
$dbi->execute($create_table1_2);
1802
$dbi->insert_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1803
    {$key3 => 3},
1804
    primary_key => [$key1, $key2], 
1805
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
1806
    where => [1, 2],
1807
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1808
is($dbi->select(table => $table1)->one->{$key1}, 1);
1809
is($dbi->select(table => $table1)->one->{$key2}, 2);
1810
is($dbi->select(table => $table1)->one->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
1811

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

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

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

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1868
$dbi->delete_all(table => $table1);
1869
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1870
$result = $dbi->select_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1871
    table => $table1,
1872
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
1873
    where => 1,
1874
);
1875
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1876
is($row->{$key1}, 1);
1877
is($row->{$key2}, 2);
1878
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
1879

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

            
1892
eval {
1893
    $result = $dbi->select_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1894
        table => $table1,
1895
        primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1896
        where => {},
1897
    );
1898
};
1899
like($@, qr/must be/);
1900

            
1901
eval {
1902
    $result = $dbi->select_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1903
        table => $table1,
1904
        primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1905
        where => [1],
1906
    );
1907
};
1908
like($@, qr/same/);
1909

            
1910
eval {
1911
    $result = $dbi->update_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1912
        table => $table1,
1913
        primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1914
        where => {},
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1915
        param => {$key1 => 1, $key2 => 2},
cleanup test
Yuki Kimoto authored on 2011-08-10
1916
    );
1917
};
1918
like($@, qr/must be/);
1919

            
1920
eval {
1921
    $result = $dbi->delete_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1922
        table => $table1,
1923
        primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1924
        where => {},
1925
    );
1926
};
1927
like($@, qr/must be/);
1928

            
1929
test 'model delete_at';
1930
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1931
eval { $dbi->execute("drop table $table1") };
1932
eval { $dbi->execute("drop table $table2") };
1933
eval { $dbi->execute("drop table $table3") };
cleanup test
Yuki Kimoto authored on 2011-08-10
1934
$dbi->execute($create_table1_2);
1935
$dbi->execute($create_table2_2);
1936
$dbi->execute($create_table3);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1937
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
1938
$dbi->model($table1)->delete_at(where => [1, 2]);
1939
is_deeply($dbi->select(table => $table1)->all, []);
1940
$dbi->insert(table => $table2, param => {$key1 => 1, $key2 => 2, $key3 => 3});
1941
$dbi->model($table1)->delete_at(where => [1, 2]);
1942
is_deeply($dbi->select(table => $table1)->all, []);
1943
$dbi->insert(table => $table3, param => {$key1 => 1, $key2 => 2, $key3 => 3});
1944
$dbi->model($table3)->delete_at(where => [1, 2]);
1945
is_deeply($dbi->select(table => $table3)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
1946

            
1947
test 'model insert_at';
1948
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1949
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
1950
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1951
$dbi->model($table1)->insert_at(
cleanup test
Yuki Kimoto authored on 2011-08-10
1952
    where => [1, 2],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1953
    param => {$key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
1954
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1955
$result = $dbi->model($table1)->select;
cleanup test
Yuki Kimoto authored on 2011-08-10
1956
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1957
is($row->{$key1}, 1);
1958
is($row->{$key2}, 2);
1959
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
1960

            
1961
test 'model update_at';
1962
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1963
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
1964
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1965
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
1966
$dbi->model($table1)->update_at(
cleanup test
Yuki Kimoto authored on 2011-08-10
1967
    where => [1, 2],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1968
    param => {$key3 => 4}
cleanup test
Yuki Kimoto authored on 2011-08-10
1969
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1970
$result = $dbi->model($table1)->select;
cleanup test
Yuki Kimoto authored on 2011-08-10
1971
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1972
is($row->{$key1}, 1);
1973
is($row->{$key2}, 2);
1974
is($row->{$key3}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-10
1975

            
1976
test 'model select_at';
1977
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1978
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
1979
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1980
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
1981
$result = $dbi->model($table1)->select_at(where => [1, 2]);
cleanup test
Yuki Kimoto authored on 2011-08-10
1982
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1983
is($row->{$key1}, 1);
1984
is($row->{$key2}, 2);
1985
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
1986

            
1987

            
1988
test 'mycolumn and column';
1989
$dbi = MyDBI7->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
1990
$dbi->user_table_info($user_table_info);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1991
eval { $dbi->execute("drop table $table1") };
1992
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
1993
$dbi->execute($create_table1);
1994
$dbi->execute($create_table2);
1995
$dbi->separator('__');
1996
$dbi->setup_model;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1997
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1998
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
1999
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2000
$result = $model->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2001
    column => [$model->mycolumn, $model->column($table2)],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2002
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2003
);
2004
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2005
          {$key1 => 1, $key2 => 2, "${table2}__$key1" => 1, "${table2}__$key3" => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2006

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2007
test 'insert_param';
2008
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2009
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2010
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2011
$param = {$key1 => 1, $key2 => 2};
test cleanup
Yuki Kimoto authored on 2011-08-10
2012
$insert_param = $dbi->insert_param($param);
2013
$sql = <<"EOS";
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2014
insert into $table1 $insert_param
test cleanup
Yuki Kimoto authored on 2011-08-10
2015
EOS
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2016
$dbi->execute($sql, param => $param, table => $table1);
2017
is($dbi->select(table => $table1)->one->{$key1}, 1);
2018
is($dbi->select(table => $table1)->one->{$key2}, 2);
test cleanup
Yuki Kimoto authored on 2011-08-10
2019

            
2020
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2021
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2022
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2023
$param = {$key1 => 1, $key2 => 2};
test cleanup
Yuki Kimoto authored on 2011-08-10
2024
$insert_param = $dbi->insert_param($param);
2025
$sql = <<"EOS";
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2026
insert into $table1 $insert_param
test cleanup
Yuki Kimoto authored on 2011-08-10
2027
EOS
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2028
$dbi->execute($sql, param => $param, table => $table1);
2029
is($dbi->select(table => $table1)->one->{$key1}, 1);
2030
is($dbi->select(table => $table1)->one->{$key2}, 2);
test cleanup
Yuki Kimoto authored on 2011-08-10
2031

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

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

            
2055
$result = $model->select_at(
2056
    column => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2057
        $model->mycolumn([$key1]),
2058
        $model->column($table2 => [$key1])
cleanup test
Yuki Kimoto authored on 2011-08-10
2059
    ]
2060
);
2061
is_deeply($result->one,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2062
          {$key1 => 1, "$table2.$key1" => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
2063
$result = $model->select_at(
2064
    column => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2065
        $model->mycolumn([$key1]),
2066
        {$table2 => [$key1]}
cleanup test
Yuki Kimoto authored on 2011-08-10
2067
    ]
2068
);
2069
is_deeply($result->one,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2070
          {$key1 => 1, "$table2.$key1" => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
2071

            
2072
$result = $model->select_at(
2073
    column => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2074
        $model->mycolumn([$key1]),
2075
        ["$table2.$key1", as => "$table2.$key1"]
cleanup test
Yuki Kimoto authored on 2011-08-10
2076
    ]
2077
);
2078
is_deeply($result->one,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2079
          {$key1 => 1, "$table2.$key1" => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
2080

            
2081
$result = $model->select_at(
2082
    column => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2083
        $model->mycolumn([$key1]),
2084
        ["$table2.$key1" => "$table2.$key1"]
cleanup test
Yuki Kimoto authored on 2011-08-10
2085
    ]
2086
);
2087
is_deeply($result->one,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2088
          {$key1 => 1, "$table2.$key1" => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
2089

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2090
test 'merge_param';
2091
$dbi = DBIx::Custom->new;
2092
$params = [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2093
    {$key1 => 1, $key2 => 2, $key3 => 3},
2094
    {$key1 => 1, $key2 => 2},
2095
    {$key1 => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2096
];
2097
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2098
is_deeply($param, {$key1 => [1, 1, 1], $key2 => [2, 2], $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2099

            
2100
$params = [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2101
    {$key1 => [1, 2], $key2 => 1, $key3 => [1, 2]},
2102
    {$key1 => [3, 4], $key2 => [2, 3], $key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
2103
];
2104
$param = $dbi->merge_param($params->[0], $params->[1]);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2105
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
2106

            
2107
test 'select() param option';
2108
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2109
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2110
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2111
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2112
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2113
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2114
$dbi->execute($create_table2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2115
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4});
2116
$dbi->insert(table => $table2, param => {$key1 => 2, $key3 => 5});
cleanup test
Yuki Kimoto authored on 2011-08-10
2117
$rows = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2118
    table => $table1,
2119
    column => "$table1.$key1 as ${table1}_$key1, $key2, $key3",
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2120
    where   => {"$table1.$key2" => 3},
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2121
    join  => ["inner join (select * from $table2 where {= $table2.$key3})" . 
test cleanup
Yuki Kimoto authored on 2011-08-15
2122
              " $table2 on $table1.$key1 = $table2.$key1"],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2123
    param => {"$table2.$key3" => 5}
cleanup test
Yuki Kimoto authored on 2011-08-10
2124
)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2125
is_deeply($rows, [{"${table1}_$key1" => 2, $key2 => 3, $key3 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2126

            
2127
test 'select() string where';
2128
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2129
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2130
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2131
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2132
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2133
$rows = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2134
    table => $table1,
2135
    where => "$key1 = :$key1 and $key2 = :$key2",
2136
    where_param => {$key1 => 1, $key2 => 2}
cleanup test
Yuki Kimoto authored on 2011-08-10
2137
)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2138
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2139

            
2140
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2141
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2142
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2143
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2144
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2145
$rows = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2146
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2147
    where => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2148
        "$key1 = :$key1 and $key2 = :$key2",
2149
        {$key1 => 1, $key2 => 2}
cleanup test
Yuki Kimoto authored on 2011-08-10
2150
    ]
2151
)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2152
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2153

            
2154
test 'delete() string where';
2155
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2156
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2157
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2158
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2159
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2160
$dbi->delete(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2161
    table => $table1,
2162
    where => "$key1 = :$key1 and $key2 = :$key2",
2163
    where_param => {$key1 => 1, $key2 => 2}
cleanup test
Yuki Kimoto authored on 2011-08-10
2164
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2165
$rows = $dbi->select(table => $table1)->all;
2166
is_deeply($rows, [{$key1 => 2, $key2 => 3}]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2167

            
2168
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2169
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2170
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2171
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2172
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2173
$dbi->delete(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2174
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2175
    where => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2176
        "$key1 = :$key1 and $key2 = :$key2",
2177
         {$key1 => 1, $key2 => 2}
cleanup test
Yuki Kimoto authored on 2011-08-10
2178
    ]
2179
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2180
$rows = $dbi->select(table => $table1)->all;
2181
is_deeply($rows, [{$key1 => 2, $key2 => 3}]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2182

            
2183

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

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

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2227
$dbi->delete_all(table => $table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2228
$dbi->insert(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2229
    primary_key => $key1, 
2230
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2231
    id => 0,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2232
    param => {$key2 => 2, $key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
2233
);
2234

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

            
2239
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2240
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2241
$dbi->execute($create_table1_2);
2242
$dbi->insert(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2243
    {$key3 => 3},
2244
    primary_key => [$key1, $key2], 
2245
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2246
    id => [1, 2],
2247
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2248
is($dbi->select(table => $table1)->one->{$key1}, 1);
2249
is($dbi->select(table => $table1)->one->{$key2}, 2);
2250
is($dbi->select(table => $table1)->one->{$key3}, 3);
test cleanup
Yuki Kimoto authored on 2011-08-10
2251

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2252
test 'model insert id and primary_key option';
2253
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2254
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2255
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2256
$dbi->model($table1)->insert(
cleanup test
Yuki Kimoto authored on 2011-08-10
2257
    id => [1, 2],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2258
    param => {$key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
2259
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2260
$result = $dbi->model($table1)->select;
cleanup test
Yuki Kimoto authored on 2011-08-10
2261
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2262
is($row->{$key1}, 1);
2263
is($row->{$key2}, 2);
2264
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2265

            
2266
$dbi = MyDBI6->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);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2269
$dbi->model($table1)->insert(
2270
    {$key3 => 3},
cleanup test
Yuki Kimoto authored on 2011-08-10
2271
    id => [1, 2]
2272
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2273
$result = $dbi->model($table1)->select;
cleanup test
Yuki Kimoto authored on 2011-08-10
2274
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2275
is($row->{$key1}, 1);
2276
is($row->{$key2}, 2);
2277
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2278

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2294
$dbi->delete_all(table => $table1);
2295
$dbi->insert(table => $table1, param => {$key1 => 0, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2296
$dbi->update(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2297
    table => $table1,
2298
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2299
    id => 0,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2300
    param => {$key3 => 4}
cleanup test
Yuki Kimoto authored on 2011-08-10
2301
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2302
is($dbi->select(table => $table1)->one->{$key1}, 0);
2303
is($dbi->select(table => $table1)->one->{$key2}, 2);
2304
is($dbi->select(table => $table1)->one->{$key3}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-10
2305

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

            
2320

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

            
2336

            
2337
test 'delete and id option';
2338
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2339
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2340
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2341
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2342
$dbi->delete(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2343
    table => $table1,
2344
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
2345
    id => [1, 2],
2346
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2347
is_deeply($dbi->select(table => $table1)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
2348

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2349
$dbi->insert(table => $table1, param => {$key1 => 0, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2350
$dbi->delete(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2351
    table => $table1,
2352
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2353
    id => 0,
2354
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2355
is_deeply($dbi->select(table => $table1)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
2356

            
2357

            
2358
test 'model delete and id option';
2359
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2360
eval { $dbi->execute("drop table $table1") };
2361
eval { $dbi->execute("drop table $table2") };
2362
eval { $dbi->execute("drop table $table3") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2363
$dbi->execute($create_table1_2);
2364
$dbi->execute($create_table2_2);
2365
$dbi->execute($create_table3);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2366
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2367
$dbi->model($table1)->delete(id => [1, 2]);
2368
is_deeply($dbi->select(table => $table1)->all, []);
2369
$dbi->insert(table => $table2, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2370
$dbi->model($table1)->delete(id => [1, 2]);
2371
is_deeply($dbi->select(table => $table1)->all, []);
2372
$dbi->insert(table => $table3, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2373
$dbi->model($table3)->delete(id => [1, 2]);
2374
is_deeply($dbi->select(table => $table3)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
2375

            
2376

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2392
$dbi->delete_all(table => $table1);
2393
$dbi->insert(table => $table1, param => {$key1 => 0, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2394
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2395
    table => $table1,
2396
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2397
    id => 0,
2398
);
2399
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2400
is($row->{$key1}, 0);
2401
is($row->{$key2}, 2);
2402
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2403

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2404
$dbi->delete_all(table => $table1);
2405
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2406
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2407
    table => $table1,
2408
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
2409
    id => [1, 2]
2410
);
2411
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2412
is($row->{$key1}, 1);
2413
is($row->{$key2}, 2);
2414
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2415

            
2416

            
2417
test 'model select_at';
2418
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2419
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2420
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2421
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2422
$result = $dbi->model($table1)->select(id => [1, 2]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2423
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2424
is($row->{$key1}, 1);
2425
is($row->{$key2}, 2);
2426
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2427

            
2428
test 'column separator is default .';
2429
$dbi = MyDBI7->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
2430
$dbi->user_table_info($user_table_info);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2431
eval { $dbi->execute("drop table $table1") };
2432
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2433
$dbi->execute($create_table1);
2434
$dbi->execute($create_table2);
2435
$dbi->setup_model;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2436
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2437
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
2438
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2439
$result = $model->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2440
    column => [$model->column($table2)],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2441
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2442
);
2443
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2444
          {"$table2.$key1" => 1, "$table2.$key3" => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2445

            
2446
$result = $model->select(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2447
    column => [$model->column($table2 => [$key1, $key3])],
2448
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2449
);
2450
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2451
          {"$table2.$key1" => 1, "$table2.$key3" => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2452

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2453
test 'separator';
2454
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
2455
$dbi->user_table_info($user_table_info);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2456
eval { $dbi->execute("drop table $table1") };
2457
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2458
$dbi->execute($create_table1);
2459
$dbi->execute($create_table2);
test cleanup
Yuki Kimoto authored on 2011-08-10
2460

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2461
$dbi->create_model(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2462
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2463
    join => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2464
       "left outer join $table2 on $table1.$key1 = $table2.$key1"
cleanup test
Yuki Kimoto authored on 2011-08-10
2465
    ],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2466
    primary_key => [$key1],
cleanup test
Yuki Kimoto authored on 2011-08-10
2467
);
2468
$model2 = $dbi->create_model(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2469
    table => $table2,
cleanup test
Yuki Kimoto authored on 2011-08-10
2470
);
2471
$dbi->setup_model;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2472
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2473
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
2474
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2475
$result = $model->select(
2476
    column => [
2477
        $model->mycolumn,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2478
        {$table2 => [$key1, $key3]}
cleanup test
Yuki Kimoto authored on 2011-08-10
2479
    ],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2480
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2481
);
2482
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2483
          {$key1 => 1, $key2 => 2, "$table2.$key1" => 1, "$table2.$key3" => 3});
2484
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2485

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2486
$dbi->separator('__');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2487
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2488
$result = $model->select(
2489
    column => [
2490
        $model->mycolumn,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2491
        {$table2 => [$key1, $key3]}
cleanup test
Yuki Kimoto authored on 2011-08-10
2492
    ],
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
          {$key1 => 1, $key2 => 2, "${table2}__$key1" => 1, "${table2}__$key3" => 3});
2497
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2498

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2499
$dbi->separator('-');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2500
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2501
$result = $model->select(
2502
    column => [
2503
        $model->mycolumn,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2504
        {$table2 => [$key1, $key3]}
cleanup test
Yuki Kimoto authored on 2011-08-10
2505
    ],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2506
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2507
);
2508
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2509
          {$key1 => 1, $key2 => 2, "$table2-$key1" => 1, "$table2-$key3" => 3});
2510
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2511

            
2512

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2513
test 'filter_off';
2514
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
2515
$dbi->user_table_info($user_table_info);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2516
eval { $dbi->execute("drop table $table1") };
2517
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2518
$dbi->execute($create_table1);
2519
$dbi->execute($create_table2);
test cleanup
Yuki Kimoto authored on 2011-08-10
2520

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2521
$dbi->create_model(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2522
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2523
    join => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2524
       "left outer join $table2 on $table1.$key1 = $table2.$key1"
cleanup test
Yuki Kimoto authored on 2011-08-10
2525
    ],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2526
    primary_key => [$key1],
cleanup test
Yuki Kimoto authored on 2011-08-10
2527
);
2528
$dbi->setup_model;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2529
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2530
$model = $dbi->model($table1);
2531
$result = $model->select(column => $key1);
2532
$result->filter($key1 => sub { $_[0] * 2 });
2533
is_deeply($result->one, {$key1 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
2534

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2535
test 'available_datetype';
2536
$dbi = DBIx::Custom->connect;
2537
ok($dbi->can('available_datatype'));
test cleanup
Yuki Kimoto authored on 2011-08-10
2538

            
2539

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2540
test 'select prefix option';
2541
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2542
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2543
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2544
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2545
$rows = $dbi->select(prefix => "$key1,", column => $key2, table => $table1)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2546
is_deeply($rows, [{$key1 => 1, $key2 => 2}], "table");
test cleanup
Yuki Kimoto authored on 2011-08-10
2547

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

            
added tests
Yuki Kimoto authored on 2011-08-26
2549
test 'mapper';
2550
$dbi = DBIx::Custom->connect;
2551
$param = $dbi->mapper(param => {id => 1, author => 'Ken', price => 1900})->map(
2552
    id => "$table1.id",
2553
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2554
    price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
2555
);
2556
is_deeply($param, {"$table1.id" => 1, "$table1.author" => '%Ken%',
2557
  "$table1.price" => 1900});
2558

            
2559
$param = $dbi->mapper(param => {id => 0, author => 0, price => 0})->map(
2560
    id => "$table1.id",
2561
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2562
    price => ["$table1.price", sub { '%' . $_[0] . '%' },
2563
      {condition => sub { $_[0] eq 0 }}]
2564
);
2565
is_deeply($param, {"$table1.id" => 0, "$table1.author" => '%0%', "$table1.price" => '%0%'});
2566

            
2567
$param = $dbi->mapper(param => {id => '', author => '', price => ''})->map(
2568
    id => "$table1.id",
2569
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2570
    price => ["$table1.price", sub { '%' . $_[0] . '%' },
2571
      {condition => sub { $_[0] eq 1 }}]
2572
);
2573
is_deeply($param, {});
2574

            
2575
$param = $dbi->mapper(param => {id => undef, author => undef, price => undef})->map(
2576
    id => "$table1.id",
2577
    price => ["$table1.price", {condition => 'exists'}]
2578
);
2579
is_deeply($param, {"$table1.price" => undef});
2580

            
2581
$param = $dbi->mapper(param => {price => 'a'})->map(
2582
    id => ["$table1.id", {condition => 'exists'}],
2583
    price => ["$table1.price", sub { '%' . $_[0] }, {condition => 'exists'}]
2584
);
2585
is_deeply($param, {"$table1.price" => '%a'});
2586

            
2587
$param = $dbi->mapper(param => {price => 'a'}, condition => 'exists')->map(
2588
    id => ["$table1.id"],
2589
    price => ["$table1.price", sub { '%' . $_[0] }]
2590
);
2591
is_deeply($param, {"$table1.price" => '%a'});
2592

            
- added {KEY => sub { VALUE ...
Yuki Kimoto authored on 2011-09-02
2593
$param = $dbi->mapper(param => {price => 'a'})->map(
2594
    price => sub { '%' . $_[0] }
2595
);
2596
is_deeply($param, {price => '%a'});
2597

            
- added EXPERIMENTAL pass at...
Yuki Kimoto authored on 2011-09-02
2598
eval { $dbi->execute("drop table $table1") };
2599
$dbi->execute($create_table1);
2600
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2601
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
2602

            
2603
$where = $dbi->where;
2604
$where->clause(['and', ":${key1}{=}"]);
2605
$param = $dbi->mapper(param => {$key1 => undef}, condition => 'defined')->map;
2606
$where->param($param);
2607
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
2608
$row = $result->all;
2609
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
2610

            
2611
$where = $dbi->where;
2612
$where->clause(['or', ":${key1}{=}", ":${key1}{=}"]);
2613
$param = $dbi->mapper(param => {$key1 => [undef, undef]}, condition => 'exists')->map;
2614
$result = $dbi->execute("select * from $table1 $where", {$key1 => [1, 0]});
2615
$row = $result->all;
2616
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
2617
$result = $dbi->execute("select * from $table1 $where", {$key1 => [0, 1]});
2618
$row = $result->all;
2619
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
2620

            
2621
$where = $dbi->where;
2622
$where->clause(['and', ":${key1}{=}"]);
2623
$param = $dbi->mapper(param => {$key1 => [undef, undef]}, condition => 'defined')->map;
2624
$where->param($param);
2625
$result = $dbi->execute("select * from $table1 $where", {$key1 => [1, 0]});
2626
$row = $result->all;
2627
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
2628
$result = $dbi->execute("select * from $table1 $where", {$key1 => [0, 1]});
2629
$row = $result->all;
2630
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
2631

            
2632
$where = $dbi->where;
2633
$where->clause(['and', ":${key1}{=}"]);
2634
$param = $dbi->mapper(param => {$key1 => 0}, condition => 'length')
2635
  ->pass([$key1, $key2])->map;
2636
$where->param($param);
2637
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
2638
$row = $result->all;
2639
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
2640

            
2641
$where = $dbi->where;
2642
$where->clause(['and', ":${key1}{=}"]);
2643
$param = $dbi->mapper(param => {$key1 => ''}, condition => 'length')->map;
2644
$where->param($param);
2645
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
2646
$row = $result->all;
2647
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
2648

            
2649
$where = $dbi->where;
2650
$where->clause(['and', ":${key1}{=}"]);
2651
$param = $dbi->mapper(param => {$key1 => 5}, condition => sub { ($_[0] || '') eq 5 })
2652
  ->pass([$key1, $key2])->map;
2653
$where->param($param);
2654
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
2655
$row = $result->all;
2656
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
2657

            
2658
$where = $dbi->where;
2659
$where->clause(['and', ":${key1}{=}"]);
2660
$param = $dbi->mapper(param => {$key1 => 7}, condition => sub { ($_[0] || '') eq 5 })->map;
2661
$where->param($param);
2662
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
2663
$row = $result->all;
2664
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
2665

            
2666
$where = $dbi->where;
2667
$param = $dbi->mapper(param => {id => 1, author => 'Ken', price => 1900})->map(
2668
    id => "$table1.id",
2669
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2670
    price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
2671
);
2672
$where->param($param);
2673
is_deeply($where->param, {"$table1.id" => 1, "$table1.author" => '%Ken%',
2674
  "$table1.price" => 1900});
2675

            
2676
$where = $dbi->where;
2677
$param = $dbi->mapper(param => {id => 0, author => 0, price => 0})->map(
2678
    id => "$table1.id",
2679
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2680
    price => ["$table1.price", sub { '%' . $_[0] . '%' }, {condition => sub { $_[0] eq 0 }}]
2681
);
2682
$where->param($param);
2683
is_deeply($where->param, {"$table1.id" => 0, "$table1.author" => '%0%', "$table1.price" => '%0%'});
2684

            
2685
$where = $dbi->where;
2686
$param = $dbi->mapper(param => {id => '', author => '', price => ''})->map(
2687
    id => "$table1.id",
2688
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2689
    price => ["$table1.price", sub { '%' . $_[0] . '%' }, {condition => sub { $_[0] eq 1 }}]
2690
);
2691
$where->param($param);
2692
is_deeply($where->param, {});
2693

            
2694
$where = $dbi->where;
2695
$param = $dbi->mapper(param => {id => undef, author => undef, price => undef}, condition => 'exists')->map(
2696
    id => "$table1.id",
2697
    price => ["$table1.price", {condition => 'exists'}]
2698
);
2699
is_deeply($param, {"$table1.id"  => undef,"$table1.price" => undef});
2700

            
2701
$where = $dbi->where;
2702
$param = $dbi->mapper(param => {price => 'a'})->map(
2703
    id => ["$table1.id", {condition => 'exists'}],
2704
    price => ["$table1.price", sub { '%' . $_[0] }, {condition => 'exists'}]
2705
);
2706
is_deeply($param, {"$table1.price" => '%a'});
2707

            
2708
$where = $dbi->where;
2709
$param = $dbi->mapper(param => {id => [1, 2], author => 'Ken', price => 1900})->map(
2710
    id => "$table1.id",
2711
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2712
    price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
2713
);
2714
is_deeply($param, {"$table1.id" => [1, 2], "$table1.author" => '%Ken%',
2715
  "$table1.price" => 1900});
2716

            
2717
$where = $dbi->where;
2718
$param = $dbi->mapper(param => {id => ['', ''], author => 'Ken', price => 1900}, condition => 'length')->map(
2719
    id => "$table1.id",
2720
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2721
    price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
2722
);
2723
is_deeply($param, {"$table1.id" => [$dbi->not_exists, $dbi->not_exists], "$table1.author" => '%Ken%',
2724
  "$table1.price" => 1900});
2725

            
2726
$where = $dbi->where;
2727
$param = $dbi->mapper(param => {id => ['', ''], author => 'Ken', price => 1900})->map(
2728
    id => ["$table1.id", {condition => 'length'}],
2729
    author => ["$table1.author", sub { '%' . $_[0] . '%' }, {condition => 'defined'}],
2730
    price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
2731
);
2732
is_deeply($param, {"$table1.id" => [$dbi->not_exists, $dbi->not_exists], "$table1.author" => '%Ken%',
2733
  "$table1.price" => 1900});
2734

            
2735
$where = $dbi->where;
2736
$param = $dbi->mapper(param => {id => 'a', author => 'b', price => 'c'}, pass => [qw/id author/])
2737
  ->map(price => 'book.price');
2738
is_deeply($param, {id => 'a', author => 'b', 'book.price' => 'c'});
2739

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2740
test 'order';
2741
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2742
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2743
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2744
$dbi->insert({$key1 => 1, $key2 => 1}, table => $table1);
2745
$dbi->insert({$key1 => 1, $key2 => 3}, table => $table1);
2746
$dbi->insert({$key1 => 2, $key2 => 2}, table => $table1);
2747
$dbi->insert({$key1 => 2, $key2 => 4}, table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2748
my $order = $dbi->order;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2749
$order->prepend($key1, "$key2 desc");
2750
$result = $dbi->select(table => $table1, append => $order);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2751
is_deeply($result->all, [{$key1 => 1, $key2 => 3}, {$key1 => 1, $key2 => 1},
2752
  {$key1 => 2, $key2 => 4}, {$key1 => 2, $key2 => 2}]);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2753
$order->prepend("$key1 desc");
2754
$result = $dbi->select(table => $table1, append => $order);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2755
is_deeply($result->all, [{$key1 => 2, $key2 => 4}, {$key1 => 2, $key2 => 2},
2756
  {$key1 => 1, $key2 => 3}, {$key1 => 1, $key2 => 1}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2757

            
2758
$order = $dbi->order;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2759
$order->prepend(["$table1-$key1"], ["$table1-$key2", 'desc']);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2760
$result = $dbi->select(table => $table1,
2761
  column => [[$key1 => "$table1-$key1"], [$key2 => "$table1-$key2"]],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2762
  append => $order);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2763
is_deeply($result->all, [{"$table1-$key1" => 1, "$table1-$key2" => 3},
2764
  {"$table1-$key1" => 1, "$table1-$key2" => 1},
2765
  {"$table1-$key1" => 2, "$table1-$key2" => 4},
2766
  {"$table1-$key1" => 2, "$table1-$key2" => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2767

            
2768
test 'tag_parse';
2769
$dbi = DBIx::Custom->connect;
2770
$dbi->tag_parse(0);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2771
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2772
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2773
$dbi->insert({$key1 => 1, $key2 => 1}, table => $table1);
2774
eval {$dbi->execute("select * from $table1 where {= $key1}", {$key1 => 1})};
test cleanup
Yuki Kimoto authored on 2011-08-10
2775
ok($@);
2776

            
2777
test 'last_sql';
2778
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2779
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2780
$dbi->execute($create_table1);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2781
$dbi->execute("select * from $table1");
cleanup test
Yuki Kimoto authored on 2011-08-15
2782
is($dbi->last_sql, "select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
2783

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

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

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

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

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

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2815
$source = "select * from $table1 where :$table1.${key1}{=} and :$table1.${key2}{=}";
test cleanup
Yuki Kimoto authored on 2011-08-10
2816
$result = $dbi->execute(
2817
    $source,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2818
    param => {"$table1.$key1" => 1, "$table1.$key2" => 1},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2819
    filter => {"$table1.$key2" => sub { $_[0] * 2 }}
test cleanup
Yuki Kimoto authored on 2011-08-10
2820
);
2821
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2822
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2823

            
2824
test 'high perfomance way';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2825
$dbi->execute("drop table $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
2826
$dbi->execute($create_table1_highperformance);
2827
$rows = [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2828
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
2829
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
2830
];
2831
{
2832
    my $query;
2833
    foreach my $row (@$rows) {
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2834
      $query ||= $dbi->insert($row, table => $table1, query => 1);
2835
      $dbi->execute($query, $row, filter => {$key7 => sub { $_[0] * 2 }});
test cleanup
Yuki Kimoto authored on 2011-08-10
2836
    }
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2837
    is_deeply($dbi->select(table => $table1)->all,
test cleanup
Yuki Kimoto authored on 2011-08-10
2838
      [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2839
          {$key7 => 2, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
2840
          {$key7 => 2, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
2841
      ]
2842
    );
2843
}
2844

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2845
$dbi->execute("drop table $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
2846
$dbi->execute($create_table1_highperformance);
2847
$rows = [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2848
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
2849
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
2850
];
2851
{
2852
    my $query;
2853
    my $sth;
2854
    foreach my $row (@$rows) {
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2855
      $query ||= $dbi->insert($row, table => $table1, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2856
      $sth ||= $query->sth;
2857
      $sth->execute(map { $row->{$_} } sort keys %$row);
2858
    }
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2859
    is_deeply($dbi->select(table => $table1)->all,
test cleanup
Yuki Kimoto authored on 2011-08-10
2860
      [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2861
          {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
2862
          {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
2863
      ]
2864
    );
2865
}
2866

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
2867
$dbi->execute("drop table $table1");
2868
$dbi->execute($create_table1_highperformance);
2869
$rows = [
2870
    {$key7 => 10, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 5},
2871
    {$key7 => 11, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6},
2872
];
2873
{
2874
    $model = $dbi->create_model(table => $table1, primary_key => $key1);
2875
    my $query;
2876
    foreach my $row (@$rows) {
2877
      $query ||= $model->insert($row, id => 1, query => 1);
2878
      $model->execute($query, $row, id => 1, filter => {$key7 => sub { $_[0] * 2 }});
2879
    }
2880
    is_deeply($dbi->select(table => $table1, append => 'order by key2')->all,
2881
      [
2882
          {$key7 => 20, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 5, $key1 => 1},
2883
          {$key7 => 22, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 1},
2884
      ]
2885
    );
2886
}
2887

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2888
test 'result';
2889
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2890
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2891
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2892
$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1);
2893
$dbi->insert({$key1 => 3, $key2 => 4}, table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2894

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2895
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2896
@rows = ();
2897
while (my $row = $result->fetch) {
2898
    push @rows, [@$row];
2899
}
2900
is_deeply(\@rows, [[1, 2], [3, 4]]);
2901

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2902
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2903
@rows = ();
2904
while (my $row = $result->fetch_hash) {
2905
    push @rows, {%$row};
2906
}
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2907
is_deeply(\@rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2908

            
2909
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2910
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2911
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2912
$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1);
2913
$dbi->insert({$key1 => 3, $key2 => 4}, table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2914

            
2915
test 'fetch_all';
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2916
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2917
$rows = $result->fetch_all;
2918
is_deeply($rows, [[1, 2], [3, 4]]);
2919

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

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

            
2928
$rows = $result->fetch_all;
2929
is_deeply($rows, [[3, 2], [9, 4]], "array");
2930

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

            
2937
test "query_builder";
2938
$datas = [
2939
    # Basic tests
2940
    {   name            => 'placeholder basic',
2941
        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
cleanup test
Yuki Kimoto authored on 2011-08-15
2942
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?",
test cleanup
Yuki Kimoto authored on 2011-08-10
2943
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
2944
    },
2945
    {
2946
        name            => 'placeholder in',
cleanup test
Yuki Kimoto authored on 2011-08-15
2947
        source            => "{in k1 3}",
2948
        sql_expected    => "k1 in (?, ?, ?)",
test cleanup
Yuki Kimoto authored on 2011-08-10
2949
        columns_expected   => [qw/k1 k1 k1/]
2950
    },
2951
    
2952
    # Table name
2953
    {
2954
        name            => 'placeholder with table name',
2955
        source            => "{= a.k1} {= a.k2}",
cleanup test
Yuki Kimoto authored on 2011-08-15
2956
        sql_expected    => "a.k1 = ? a.k2 = ?",
test cleanup
Yuki Kimoto authored on 2011-08-10
2957
        columns_expected  => [qw/a.k1 a.k2/]
2958
    },
2959
    {   
2960
        name            => 'placeholder in with table name',
2961
        source            => "{in a.k1 2} {in b.k2 2}",
cleanup test
Yuki Kimoto authored on 2011-08-15
2962
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?)",
test cleanup
Yuki Kimoto authored on 2011-08-10
2963
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
2964
    },
2965
    {
2966
        name            => 'not contain tag',
2967
        source            => "aaa",
cleanup test
Yuki Kimoto authored on 2011-08-15
2968
        sql_expected    => "aaa",
test cleanup
Yuki Kimoto authored on 2011-08-10
2969
        columns_expected  => [],
2970
    }
2971
];
2972

            
2973
for (my $i = 0; $i < @$datas; $i++) {
2974
    my $data = $datas->[$i];
cleanup
Yuki Kimoto authored on 2011-08-13
2975
    my $dbi = DBIx::Custom->new;
2976
    my $builder = $dbi->query_builder;
test cleanup
Yuki Kimoto authored on 2011-08-10
2977
    my $query = $builder->build_query($data->{source});
2978
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
2979
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
2980
}
2981

            
cleanup
Yuki Kimoto authored on 2011-08-13
2982
$dbi = DBIx::Custom->new;
2983
$builder = $dbi->query_builder;
2984
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
2985
    p => sub {
2986
        my @args = @_;
2987
        
2988
        my $expand    = "? $args[0] $args[1]";
2989
        my $columns = [2];
2990
        return [$expand, $columns];
2991
    }
2992
);
2993

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
3004
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
3005
    q => 'string'
3006
});
3007

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
3011
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
3012
   r => sub {} 
3013
});
3014

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
3018
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
3019
   s => sub { return ["a", ""]} 
3020
});
3021

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
3025
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
3026
    t => sub {return ["a", []]}
3027
);
3028

            
3029

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

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

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

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

            
3045
test 'variouse source';
3046
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d";
3047
$query = $builder->build_query($source);
3048
is($query->sql, 'a b = ? c { } { = ? } = ? d', "basic : 1");
3049

            
3050
$source = "abc";
3051
$query = $builder->build_query($source);
3052
is($query->sql, 'abc', "basic : 2");
3053

            
3054
$source = "{= a}";
3055
$query = $builder->build_query($source);
3056
is($query->sql, 'a = ?', "only tag");
3057

            
3058
$source = "000";
3059
$query = $builder->build_query($source);
3060
is($query->sql, '000', "contain 0 value");
3061

            
3062
$source = "a {= b} }";
3063
eval{$builder->build_query($source)};
3064
like($@, qr/unexpected "}"/, "error : 1");
3065

            
3066
$source = "a {= {}";
3067
eval{$builder->build_query($source)};
3068
like($@, qr/unexpected "{"/, "error : 2");
3069

            
3070
test 'select() sqlfilter option';
3071
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3072
$dbi->user_table_info($user_table_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3073
eval { $dbi->execute("drop table $table1") };
3074
$dbi->execute($create_table1);
3075
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3076
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
3077
$rows = $dbi->select(
3078
    table => $table1,
3079
    column => $key1,
3080
    sqlfilter => sub {
3081
        my $sql = shift;
test cleanup
Yuki Kimoto authored on 2011-08-15
3082
        $sql = "select * from ( $sql ) t where $key1 = 1";
cleanup test
Yuki Kimoto authored on 2011-08-15
3083
        return $sql;
3084
    }
3085
)->all;
3086
is_deeply($rows, [{$key1 => 1}]);
3087

            
3088
test 'dbi method from model';
3089
$dbi = MyDBI9->connect;
3090
eval { $dbi->execute("drop table $table1") };
3091
$dbi->execute($create_table1);
3092
$dbi->setup_model;
3093
$model = $dbi->model($table1);
3094
eval{$model->execute("select * from $table1")};
3095
ok(!$@);
3096

            
3097
test 'column table option';
3098
$dbi = MyDBI9->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3099
$dbi->user_table_info($user_table_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3100
eval { $dbi->execute("drop table $table1") };
3101
$dbi->execute($create_table1);
3102
eval { $dbi->execute("drop table $table2") };
3103
$dbi->execute($create_table2);
3104
$dbi->setup_model;
3105
$dbi->execute("insert into $table1 ($key1, $key2) values (1, 2)");
3106
$dbi->execute("insert into $table2 ($key1, $key3) values (1, 4)");
3107
$model = $dbi->model($table1);
3108
$result = $model->select(
3109
    column => [
3110
        $model->column($table2, {alias => $table2_alias})
3111
    ],
3112
    where => {"$table2_alias.$key3" => 4}
3113
);
3114
is_deeply($result->one, 
3115
          {"$table2_alias.$key1" => 1, "$table2_alias.$key3" => 4});
3116

            
3117
$dbi->separator('__');
3118
$result = $model->select(
3119
    column => [
3120
        $model->column($table2, {alias => $table2_alias})
3121
    ],
3122
    where => {"$table2_alias.$key3" => 4}
3123
);
3124
is_deeply($result->one, 
3125
          {"${table2_alias}__$key1" => 1, "${table2_alias}__$key3" => 4});
3126

            
3127
$dbi->separator('-');
3128
$result = $model->select(
3129
    column => [
3130
        $model->column($table2, {alias => $table2_alias})
3131
    ],
3132
    where => {"$table2_alias.$key3" => 4}
3133
);
3134
is_deeply($result->one, 
3135
          {"$table2_alias-$key1" => 1, "$table2_alias-$key3" => 4});
3136

            
3137
test 'create_model';
3138
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3139
$dbi->user_table_info($user_table_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3140
eval { $dbi->execute("drop table $table1") };
3141
eval { $dbi->execute("drop table $table2") };
3142
$dbi->execute($create_table1);
3143
$dbi->execute($create_table2);
3144

            
3145
$dbi->create_model(
3146
    table => $table1,
3147
    join => [
3148
       "left outer join $table2 on $table1.$key1 = $table2.$key1"
3149
    ],
3150
    primary_key => [$key1]
3151
);
3152
$model2 = $dbi->create_model(
3153
    table => $table2
3154
);
3155
$dbi->create_model(
3156
    table => $table3,
3157
    filter => [
3158
        $key1 => {in => sub { uc $_[0] }}
3159
    ]
3160
);
3161
$dbi->setup_model;
3162
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3163
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
3164
$model = $dbi->model($table1);
3165
$result = $model->select(
3166
    column => [$model->mycolumn, $model->column($table2)],
3167
    where => {"$table1.$key1" => 1}
3168
);
3169
is_deeply($result->one,
3170
          {$key1 => 1, $key2 => 2, "$table2.$key1" => 1, "$table2.$key3" => 3});
3171
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3});
3172

            
3173
test 'model method';
3174
$dbi = DBIx::Custom->connect;
3175
eval { $dbi->execute("drop table $table2") };
3176
$dbi->execute($create_table2);
3177
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
3178
$model = $dbi->create_model(
3179
    table => $table2
3180
);
3181
$model->method(foo => sub { shift->select(@_) });
3182
is_deeply($model->foo->one, {$key1 => 1, $key3 => 3});
3183

            
3184
test 'update_param';
3185
$dbi = DBIx::Custom->connect;
3186
eval { $dbi->execute("drop table $table1") };
3187
$dbi->execute($create_table1_2);
3188
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3189
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3190

            
3191
$param = {$key2 => 11};
3192
$update_param = $dbi->update_param($param);
3193
$sql = <<"EOS";
3194
update $table1 $update_param
3195
where $key1 = 1
3196
EOS
3197
$dbi->execute($sql, param => $param);
3198
$result = $dbi->execute("select * from $table1 order by $key1", table => $table1);
3199
$rows   = $result->all;
3200
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
3201
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3202
                  "basic");
3203

            
3204

            
3205
$dbi = DBIx::Custom->connect;
3206
eval { $dbi->execute("drop table $table1") };
3207
$dbi->execute($create_table1_2);
3208
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3209
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3210

            
3211
$param = {$key2 => 11, $key3 => 33};
3212
$update_param = $dbi->update_param($param);
3213
$sql = <<"EOS";
3214
update $table1 $update_param
3215
where $key1 = 1
3216
EOS
3217
$dbi->execute($sql, param => $param);
3218
$result = $dbi->execute("select * from $table1 order by $key1", table => $table1);
3219
$rows   = $result->all;
3220
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 33, $key4 => 4, $key5 => 5},
3221
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3222
                  "basic");
3223

            
3224
$dbi = DBIx::Custom->connect;
3225
eval { $dbi->execute("drop table $table1") };
3226
$dbi->execute($create_table1_2);
3227
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3228
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3229

            
3230
$param = {$key2 => 11, $key3 => 33};
3231
$update_param = $dbi->update_param($param, {no_set => 1});
3232
$sql = <<"EOS";
3233
update $table1 set $update_param
3234
where $key1 = 1
3235
EOS
3236
$dbi->execute($sql, param => $param);
3237
$result = $dbi->execute("select * from $table1 order by $key1", table => $table1);
3238
$rows   = $result->all;
3239
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 33, $key4 => 4, $key5 => 5},
3240
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3241
                  "update param no_set");
3242

            
3243
            
3244
eval { $dbi->update_param({";" => 1}) };
3245
like($@, qr/not safety/);
3246

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3248
test 'update_param';
3249
$dbi = DBIx::Custom->connect;
3250
eval { $dbi->execute("drop table $table1") };
3251
$dbi->execute($create_table1_2);
3252
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3253
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
test cleanup
Yuki Kimoto authored on 2011-08-10
3254

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3255
$param = {$key2 => 11};
3256
$update_param = $dbi->assign_param($param);
3257
$sql = <<"EOS";
3258
update $table1 set $update_param
3259
where $key1 = 1
3260
EOS
3261
$dbi->execute($sql, param => $param, table => $table1);
3262
$result = $dbi->execute("select * from $table1 order by $key1");
3263
$rows   = $result->all;
3264
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
3265
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3266
                  "basic");
test cleanup
Yuki Kimoto authored on 2011-08-10
3267

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3268
test 'Model class';
3269
$dbi = MyDBI1->connect;
3270
eval { $dbi->execute("drop table $table1") };
3271
$dbi->execute($create_table1);
3272
$model = $dbi->model($table1);
3273
$model->insert({$key1 => 'a', $key2 => 'b'});
3274
is_deeply($model->list->all, [{$key1 => 'a', $key2 => 'b'}], 'basic');
3275
eval { $dbi->execute("drop table $table2") };
3276
$dbi->execute($create_table2);
3277
$model = $dbi->model($table2);
3278
$model->insert({$key1 => 'a'});
3279
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'basic');
3280
is($dbi->models->{$table1}, $dbi->model($table1));
3281
is($dbi->models->{$table2}, $dbi->model($table2));
3282

            
3283
$dbi = MyDBI4->connect;
3284
eval { $dbi->execute("drop table $table1") };
3285
$dbi->execute($create_table1);
3286
$model = $dbi->model($table1);
3287
$model->insert({$key1 => 'a', $key2 => 'b'});
3288
is_deeply($model->list->all, [{$key1 => 'a', $key2 => 'b'}], 'basic');
3289
eval { $dbi->execute("drop table $table2") };
3290
$dbi->execute($create_table2);
3291
$model = $dbi->model($table2);
3292
$model->insert({$key1 => 'a'});
3293
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'basic');
3294

            
3295
$dbi = MyDBI5->connect;
3296
eval { $dbi->execute("drop table $table1") };
3297
eval { $dbi->execute("drop table $table2") };
3298
$dbi->execute($create_table1);
3299
$dbi->execute($create_table2);
3300
$model = $dbi->model($table2);
3301
$model->insert({$key1 => 'a'});
3302
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'include all model');
3303
$dbi->insert(table => $table1, param => {$key1 => 1});
3304
$model = $dbi->model($table1);
3305
is_deeply($model->list->all, [{$key1 => 1, $key2 => undef}], 'include all model');
3306

            
3307
test 'primary_key';
3308
$dbi = MyDBI1->connect;
3309
$model = $dbi->model($table1);
3310
$model->primary_key([$key1, $key2]);
3311
is_deeply($model->primary_key, [$key1, $key2]);
3312

            
3313
test 'columns';
3314
$dbi = MyDBI1->connect;
3315
$model = $dbi->model($table1);
3316
$model->columns([$key1, $key2]);
3317
is_deeply($model->columns, [$key1, $key2]);
cleanup test
Yuki Kimoto authored on 2011-08-10
3318

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3319
test 'setup_model';
3320
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-16
3321
$dbi->user_table_info($user_table_info);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3322
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3323
eval { $dbi->execute("drop table $table2") };
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
3324

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3325
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-15
3326
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-10
3327
$dbi->setup_model;
cleanup test
Yuki Kimoto authored on 2011-08-15
3328
is_deeply([sort @{$dbi->model($table1)->columns}], [$key1, $key2]);
3329
is_deeply([sort @{$dbi->model($table2)->columns}], [$key1, $key3]);
cleanup test
Yuki Kimoto authored on 2011-08-10
3330

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3331
test 'each_column';
3332
$dbi = DBIx::Custom->connect;
3333
eval { $dbi->execute("drop table ${q}table$p") };
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3334
eval { $dbi->execute("drop table $table1") };
3335
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3336
eval { $dbi->execute("drop table $table3") };
3337
$dbi->execute($create_table1_type);
cleanup test
Yuki Kimoto authored on 2011-08-10
3338
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-15
3339

            
3340
$infos = [];
3341
$dbi->each_column(sub {
3342
    my ($self, $table, $column, $cinfo) = @_;
3343
    
3344
    if ($table =~ /^table\d/i) {
3345
         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
3346
         push @$infos, $info;
3347
    }
3348
});
3349
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3350
is_deeply($infos, 
3351
    [
3352
        [$table1, $key1, $key1],
3353
        [$table1, $key2, $key2],
3354
        [$table2, $key1, $key1],
3355
        [$table2, $key3, $key3]
3356
    ]
3357
    
cleanup test
Yuki Kimoto authored on 2011-08-10
3358
);
cleanup test
Yuki Kimoto authored on 2011-08-16
3359

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3360
test 'each_table';
3361
$dbi = DBIx::Custom->connect;
3362
eval { $dbi->execute("drop table $table1") };
3363
eval { $dbi->execute("drop table $table2") };
3364
$dbi->execute($create_table2);
3365
$dbi->execute($create_table1_type);
cleanup test
Yuki Kimoto authored on 2011-08-10
3366

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3367
$infos = [];
3368
$dbi->each_table(sub {
3369
    my ($self, $table, $table_info) = @_;
3370
    
3371
    if ($table =~ /^table\d/i) {
3372
         my $info = [$table, $table_info->{TABLE_NAME}];
3373
         push @$infos, $info;
3374
    }
3375
});
3376
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3377
is_deeply($infos, 
3378
    [
3379
        [$table1, $table1],
3380
        [$table2, $table2],
3381
    ]
cleanup test
Yuki Kimoto authored on 2011-08-10
3382
);
3383

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

            
3390
$infos = [];
3391
$dbi->user_table_info($user_table_info);
3392
$dbi->each_table(sub {
3393
    my ($self, $table, $table_info) = @_;
3394
    
3395
    if ($table =~ /^table\d/i) {
3396
         my $info = [$table, $table_info->{TABLE_NAME}];
3397
         push @$infos, $info;
3398
    }
3399
});
3400
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3401
is_deeply($infos, 
3402
    [
3403
        [$table1, $table1],
3404
        [$table2, $table2],
3405
        [$table3, $table3],
3406
    ]
3407
);
3408

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3409
test 'type_rule into';
3410
eval { $dbi->execute("drop table $table1") };
3411
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3412
$user_column_info = $dbi->get_column_info(exclude_table => $dbi->exclude_table);
3413

            
3414

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3415
$dbi = DBIx::Custom->connect;
3416
eval { $dbi->execute("drop table $table1") };
3417
$dbi->execute($create_table1_type);
3418

            
cleanup
Yuki Kimoto authored on 2011-08-16
3419
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3420
$dbi->type_rule(
3421
    into1 => {
3422
        $date_typename => sub { '2010-' . $_[0] }
3423
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3424
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3425
$dbi->insert({$key1 => '01-01'}, table => $table1);
3426
$result = $dbi->select(table => $table1);
3427
like($result->one->{$key1}, qr/^2010-01-01/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3428

            
3429
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3430
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3431
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3432
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3433
$dbi->type_rule(
3434
    into1 => [
3435
         [$date_typename, $datetime_typename] => sub {
3436
            my $value = shift;
3437
            $value =~ s/02/03/g;
3438
            return $value;
3439
         }
3440
    ]
3441
);
3442
$dbi->insert({$key1 => '2010-01-02', $key2 => '2010-01-01 01:01:02'}, table => $table1);
3443
$result = $dbi->select(table => $table1);
3444
$row = $result->one;
3445
like($row->{$key1}, qr/^2010-01-03/);
3446
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3447

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

            
3470
$dbi = DBIx::Custom->connect;
3471
eval { $dbi->execute("drop table $table1") };
3472
$dbi->execute($create_table1_type);
3473
$dbi->insert({$key1 => '2010-01-03', $key2 => '2010-01-01 01:01:03'}, table => $table1);
cleanup
Yuki Kimoto authored on 2011-08-16
3474
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3475
$dbi->type_rule(
3476
    into1 => [
3477
        [$date_typename, $datetime_typename] => sub {
3478
            my $value = shift;
3479
            $value =~ s/02/03/g;
3480
            return $value;
3481
        }
cleanup test
Yuki Kimoto authored on 2011-08-10
3482
    ]
3483
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3484
$result = $dbi->execute(
3485
    "select * from $table1 where $key1 = :$key1 and $key2 = :$table1.$key2",
3486
    param => {$key1 => '2010-01-02', "$table1.$key2" => '2010-01-01 01:01:02'},
3487
    table => $table1
3488
);
3489
$row = $result->one;
3490
like($row->{$key1}, qr/^2010-01-03/);
3491
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3492

            
3493
$dbi = DBIx::Custom->connect;
3494
eval { $dbi->execute("drop table $table1") };
3495
$dbi->execute($create_table1_type);
3496
$dbi->register_filter(convert => sub {
3497
    my $value = shift || '';
3498
    $value =~ s/02/03/;
3499
    return $value;
3500
});
cleanup
Yuki Kimoto authored on 2011-08-16
3501
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3502
$dbi->type_rule(
3503
    from1 => {
3504
        $date_datatype => 'convert',
3505
    },
3506
    into1 => {
3507
        $date_typename => 'convert',
3508
    }
3509
);
3510
$dbi->insert({$key1 => '2010-02-02'}, table => $table1);
3511
$result = $dbi->select(table => $table1);
3512
like($result->fetch->[0], qr/^2010-03-03/);
3513

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

            
3539

            
3540
$dbi = DBIx::Custom->connect;
3541
eval { $dbi->execute("drop table $table1") };
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/3/4/; return $v }
3547
    },
3548
    from2 => {
3549
        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3550
    },
3551
);
3552
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3553
$result = $dbi->select(table => $table1);
cleanup
Yuki Kimoto authored on 2011-08-16
3554
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3555
$result->type_rule(
3556
    from1 => {
3557
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/6/; return $v }
3558
    },
3559
    from2 => {
3560
        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/8/; return $v }
3561
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3562
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3563
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/8/9/; return $v });
3564
like($result->fetch_first->[0], qr/^2010-01-09/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3565

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3566
test 'type_rule_off';
cleanup test
Yuki Kimoto authored on 2011-08-10
3567
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-15
3568
eval { $dbi->execute("drop table $table1") };
3569
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3570
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3571
$dbi->type_rule(
3572
    from1 => {
3573
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3574
    },
3575
    into1 => {
3576
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3577
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3578
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3579
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule_off => 1);
3580
$result = $dbi->select(table => $table1, type_rule_off => 1);
3581
like($result->type_rule_off->fetch->[0], qr/^2010-01-03/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3582

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

            
3599
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3600
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3601
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3602
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3603
$dbi->type_rule(
3604
    from1 => {
3605
        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3606
    },
3607
    into1 => {
3608
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3609
    }
3610
);
3611
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3612
$result = $dbi->select(table => $table1);
3613
like($result->one->{$key1}, qr/^2010-01-05/);
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/4/5/; return $v }
3622
    },
3623
    into1 => {
3624
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3625
    }
3626
);
3627
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3628
$result = $dbi->select(table => $table1);
3629
like($result->fetch->[0], qr/2010-01-05/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3630

            
3631
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3632
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3633
$dbi->execute($create_table1_type);
3634
$dbi->register_filter(ppp => sub { my $v = shift || ''; $v =~ s/3/4/; return $v });
cleanup
Yuki Kimoto authored on 2011-08-16
3635
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3636
$dbi->type_rule(
3637
    into1 => {
3638
        $date_typename => 'ppp'
3639
    }
3640
);
3641
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3642
$result = $dbi->select(table => $table1);
3643
like($result->one->{$key1}, qr/^2010-01-04/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3644

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3645
eval{$dbi->type_rule(
3646
    into1 => {
3647
        $date_typename => 'pp'
3648
    }
3649
)};
3650
like($@, qr/not registered/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3651

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3652
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3653
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3654
$dbi->execute($create_table1_type);
cleanup test
Yuki Kimoto authored on 2011-08-10
3655
eval {
cleanup test
Yuki Kimoto authored on 2011-08-15
3656
    $dbi->type_rule(
3657
        from1 => {
3658
            Date => sub { $_[0] * 2 },
3659
        }
cleanup test
Yuki Kimoto authored on 2011-08-10
3660
    );
3661
};
cleanup test
Yuki Kimoto authored on 2011-08-15
3662
like($@, qr/lower/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3663

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3664
eval {
3665
    $dbi->type_rule(
3666
        into1 => {
3667
            Date => sub { $_[0] * 2 },
3668
        }
3669
    );
3670
};
3671
like($@, qr/lower/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3672

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

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

            
3711
$result = $dbi->select(table => $table1);
3712
$result->type_rule(
3713
    from1 => {
3714
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3715
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3716
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3717
$row = $result->one;
3718
like($row->{$key1}, qr/2010-01-05/);
3719
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3720

            
3721
$result = $dbi->select(table => $table1);
3722
$result->type_rule(
3723
    from1 => {
3724
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3725
    }
3726
);
3727
$row = $result->one;
3728
like($row->{$key1}, qr/2010-01-05/);
3729
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3730

            
3731
$result = $dbi->select(table => $table1);
3732
$result->type_rule(
3733
    from1 => [$date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }]
3734
);
3735
$row = $result->one;
3736
like($row->{$key1}, qr/2010-01-05/);
3737
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3738

            
3739
$dbi->register_filter(five => sub { my $v = shift || ''; $v =~ s/3/5/; return $v });
3740
$result = $dbi->select(table => $table1);
3741
$result->type_rule(
3742
    from1 => [$date_datatype => 'five']
3743
);
3744
$row = $result->one;
3745
like($row->{$key1}, qr/^2010-01-05/);
3746
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
3747

            
3748
$result = $dbi->select(table => $table1);
3749
$result->type_rule(
3750
    from1 => [$date_datatype => undef]
3751
);
3752
$row = $result->one;
3753
like($row->{$key1}, qr/^2010-01-03/);
3754
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3755

            
3756
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3757
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3758
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3759
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3760
$dbi->type_rule(
3761
    from1 => {
3762
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v },
3763
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
3764
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3765
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3766
$result = $dbi->select(table => $table1);
3767
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/4/5/; return $v });
3768
like($result->one->{$key1}, qr/^2010-01-05/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3769

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3770
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3771
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3772
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3773
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3774
$dbi->type_rule(
3775
    from1 => {
3776
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3777
    },
3778
);
3779
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3780
$result = $dbi->select(table => $table1);
3781
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/4/5/; return $v });
3782
like($result->fetch->[0], qr/^2010-01-05/);
test cleanup
Yuki Kimoto authored on 2011-08-10
3783

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3832
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3833
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3834
$dbi->execute($create_table1_type);
cleanup
Yuki Kimoto authored on 2011-08-16
3835
$dbi->user_column_info($user_column_info);
cleanup test
Yuki Kimoto authored on 2011-08-15
3836
$dbi->type_rule(
3837
    into1 => {
3838
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3839
    },
3840
    into2 => {
3841
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3842
    },
3843
    from1 => {
3844
        $date_datatype => sub { my $v = shift || ''; $v =~ s/5/6/; return $v }
3845
    },
3846
    from2 => {
3847
        $date_datatype => sub { my $v = shift || ''; $v =~ s/(3|6)/7/; return $v }
3848
    }
3849
);
3850
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule2_off => 1);
3851
$result = $dbi->select(table => $table1);
3852
like($result->type_rule2_off->fetch_first->[0], qr/^2010-01-06/);
3853
$result = $dbi->select(table => $table1);
3854
like($result->type_rule2_on->fetch_first->[0], qr/^2010-01-07/);
test cleanup
Yuki Kimoto authored on 2011-08-10
3855

            
added memory leak check test
Yuki Kimoto authored on 2011-08-15
3856
test 'join';
3857
$dbi = DBIx::Custom->connect;
3858
eval { $dbi->execute("drop table $table1") };
3859
$dbi->execute($create_table1);
3860
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3861
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
3862
eval { $dbi->execute("drop table $table2") };
3863
$dbi->execute($create_table2);
3864
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3865
eval { $dbi->execute("drop table $table3") };
3866
$dbi->execute("create table $table3 ($key3 int, $key4 int)");
3867
$dbi->insert(table => $table3, param => {$key3 => 5, $key4 => 4});
3868
$rows = $dbi->select(
3869
    table => $table1,
3870
    column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
3871
    where   => {"$table1.$key2" => 2},
3872
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1"]
3873
)->all;
3874
is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}]);
3875

            
3876
$rows = $dbi->select(
3877
    table => $table1,
3878
    where   => {$key1 => 1},
3879
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1"]
3880
)->all;
3881
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
3882

            
3883
eval {
3884
    $rows = $dbi->select(
3885
        table => $table1,
3886
        column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
3887
        where   => {"$table1.$key2" => 2},
3888
        join  => {"$table1.$key1" => "$table2.$key1"}
3889
    );
3890
};
3891
like ($@, qr/array/);
3892

            
3893
$rows = $dbi->select(
3894
    table => $table1,
3895
    where   => {$key1 => 1},
3896
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
3897
              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
3898
)->all;
3899
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
3900

            
3901
$rows = $dbi->select(
3902
    column => "$table3.$key4 as ${table3}__$key4",
3903
    table => $table1,
3904
    where   => {"$table1.$key1" => 1},
3905
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
3906
              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
3907
)->all;
3908
is_deeply($rows, [{"${table3}__$key4" => 4}]);
3909

            
3910
$rows = $dbi->select(
3911
    column => "$table1.$key1 as ${table1}__$key1",
3912
    table => $table1,
3913
    where   => {"$table3.$key4" => 4},
3914
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
3915
              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
3916
)->all;
3917
is_deeply($rows, [{"${table1}__$key1" => 1}]);
3918

            
3919
$dbi = DBIx::Custom->connect;
3920
eval { $dbi->execute("drop table $table1") };
3921
$dbi->execute($create_table1);
3922
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3923
eval { $dbi->execute("drop table $table2") };
3924
$dbi->execute($create_table2);
3925
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3926
$rows = $dbi->select(
3927
    table => $table1,
3928
    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",
3929
    where   => {"$table1.$key2" => 2},
3930
    join  => ["left outer join ${q}$table2$p on ${q}$table1$p.${q}$key1$p = ${q}$table2$p.${q}$key1$p"],
3931
)->all;
3932
is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}],
3933
          'quote');
3934

            
3935

            
3936
$dbi = DBIx::Custom->connect;
3937
eval { $dbi->execute("drop table $table1") };
3938
$dbi->execute($create_table1);
3939
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3940
$sql = <<"EOS";
3941
left outer join (
3942
  select * from $table1 t1
3943
  where t1.$key2 = (
3944
    select max(t2.$key2) from $table1 t2
3945
    where t1.$key1 = t2.$key1
3946
  )
3947
) $table3 on $table1.$key1 = $table3.$key1
3948
EOS
3949
$join = [$sql];
3950
$rows = $dbi->select(
3951
    table => $table1,
3952
    column => "$table3.$key1 as ${table3}__$key1",
3953
    join  => $join
3954
)->all;
3955
is_deeply($rows, [{"${table3}__$key1" => 1}]);
3956

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

            
3989
$dbi = DBIx::Custom->connect;
3990
eval { $dbi->execute("drop table $table1") };
3991
eval { $dbi->execute("drop table $table2") };
3992
$dbi->execute($create_table1);
3993
$dbi->execute($create_table2);
3994
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3995
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4});
3996
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3997
$result = $dbi->select(
3998
    table => $table1,
3999
    column => [{$table2 => [$key3]}],
4000
    join => [
4001
        {
4002
            clause => "left outer join $table2 on $table2.$key3 = '4' and $table1.$key1 = $table2.$key1",
4003
            table => [$table1, $table2]
4004
        }
4005
    ]
4006
);
4007
is_deeply($result->all, [{"$table2.$key3" => 4}]);
4008

            
4009
test 'columns';
4010
$dbi = MyDBI1->connect;
4011
$model = $dbi->model($table1);
4012

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
4013
test 'count';
4014
$dbi = DBIx::Custom->connect;
4015
eval { $dbi->execute("drop table $table1") };
4016
$dbi->execute($create_table1);
4017
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
4018
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 3});
4019
is($dbi->count(table => $table1), 2);
4020
is($dbi->count(table => $table1, where => {$key2 => 2}), 1);
4021
$model = $dbi->create_model(table => $table1);
4022
is($model->count, 2);
cleanup test
Yuki Kimoto authored on 2011-08-10
4023

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