DBIx-Custom / t / common.t /
Newer Older
3857 lines | 126.805kb
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;
79

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

            
84
    use strict;
85
    use warnings;
86

            
87
    use base 'DBIx::Custom';
88

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

            
100
    package MyModel2::Base1;
101

            
102
    use strict;
103
    use warnings;
104

            
105
    use base 'DBIx::Custom::Model';
106

            
test cleanup
Yuki Kimoto authored on 2011-08-10
107
    package MyModel2::table1;
test cleanup
Yuki Kimoto authored on 2011-08-10
108

            
109
    use strict;
110
    use warnings;
111

            
112
    use base 'MyModel2::Base1';
113

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

            
120
    sub list { shift->select; }
121

            
test cleanup
Yuki Kimoto authored on 2011-08-10
122
    package MyModel2::table2;
test cleanup
Yuki Kimoto authored on 2011-08-10
123

            
124
    use strict;
125
    use warnings;
126

            
127
    use base 'MyModel2::Base1';
128

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

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

            
137
    package MyModel2::TABLE1;
138

            
139
    use strict;
140
    use warnings;
141

            
142
    use base 'MyModel2::Base1';
143

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

            
150
    sub list { shift->select; }
151

            
152
    package MyModel2::TABLE2;
153

            
154
    use strict;
155
    use warnings;
156

            
157
    use base 'MyModel2::Base1';
158

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

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

            
170
    use strict;
171
    use warnings;
172

            
173
    use base 'DBIx::Custom';
174

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
236
# Create table
test cleanup
Yuki Kimoto authored on 2011-08-10
237
$dbi = DBIx::Custom->connect;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
238
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
239
$dbi->execute($create_table1);
240
$model = $dbi->create_model(table => $table1);
241
$model->insert({$key1 => 1, $key2 => 2});
242
is_deeply($model->select->all, [{$key1 => 1, $key2 => 2}]);
cleanup
Yuki Kimoto authored on 2011-08-15
243

            
cleanup test
Yuki Kimoto authored on 2011-08-15
244
test 'DBIx::Custom::Result test';
245
$dbi->delete_all(table => $table1);
246
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
247
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
248
$source = "select $key1, $key2 from $table1";
249
$query = $dbi->create_query($source);
250
$result = $dbi->execute($query);
cleanup
Yuki Kimoto authored on 2011-08-15
251

            
cleanup test
Yuki Kimoto authored on 2011-08-15
252
@rows = ();
253
while (my $row = $result->fetch) {
254
    push @rows, [@$row];
255
}
256
is_deeply(\@rows, [[1, 2], [3, 4]], "fetch");
cleanup
Yuki Kimoto authored on 2011-08-15
257

            
cleanup test
Yuki Kimoto authored on 2011-08-15
258
$result = $dbi->execute($query);
259
@rows = ();
260
while (my $row = $result->fetch_hash) {
261
    push @rows, {%$row};
262
}
263
is_deeply(\@rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], "fetch_hash");
test cleanup
Yuki Kimoto authored on 2011-08-10
264

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
273
test 'Insert query return value';
274
$source = "insert into $table1 {insert_param $key1 $key2}";
275
$query = $dbi->execute($source, {}, query => 1);
276
$ret_val = $dbi->execute($query, param => {$key1 => 1, $key2 => 2});
277
ok($ret_val);
test cleanup
Yuki Kimoto authored on 2011-08-10
278

            
cleanup test
Yuki Kimoto authored on 2011-08-15
279
test 'Direct query';
280
$dbi->delete_all(table => $table1);
281
$insert_source = "insert into $table1 {insert_param $key1 $key2}";
282
$dbi->execute($insert_source, param => {$key1 => 1, $key2 => 2});
283
$result = $dbi->execute("select * from $table1");
284
$rows = $result->all;
285
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-08
286

            
287
test 'Filter basic';
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
288
$dbi->delete_all(table => $table1);
cleanup test
Yuki Kimoto authored on 2011-08-08
289
$dbi->register_filter(twice       => sub { $_[0] * 2}, 
290
                    three_times => sub { $_[0] * 3});
291

            
cleanup test
Yuki Kimoto authored on 2011-08-15
292
$insert_source  = "insert into $table1 {insert_param $key1 $key2}";
cleanup test
Yuki Kimoto authored on 2011-08-08
293
$insert_query = $dbi->execute($insert_source, {}, query => 1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
294
$insert_query->filter({$key1 => 'twice'});
295
$dbi->execute($insert_query, param => {$key1 => 1, $key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-15
296
$result = $dbi->execute("select * from $table1");
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
297
$rows = $result->filter({$key2 => 'three_times'})->all;
298
is_deeply($rows, [{$key1 => 2, $key2 => 6}], "filter fetch_filter");
cleanup test
Yuki Kimoto authored on 2011-08-08
299

            
300
test 'Filter in';
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
301
$dbi->delete_all(table => $table1);
cleanup test
Yuki Kimoto authored on 2011-08-15
302
$insert_source  = "insert into $table1 {insert_param $key1 $key2}";
cleanup test
Yuki Kimoto authored on 2011-08-08
303
$insert_query = $dbi->execute($insert_source, {}, query => 1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
304
$dbi->execute($insert_query, param => {$key1 => 2, $key2 => 4});
305
$select_source = "select * from $table1 where {in $table1.$key1 2} and {in $table1.$key2 2}";
cleanup test
Yuki Kimoto authored on 2011-08-08
306
$select_query = $dbi->execute($select_source,{}, query => 1);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
307
$select_query->filter({"$table1.$key1" => 'twice'});
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
308
$result = $dbi->execute($select_query, param => {"$table1.$key1" => [1,5], "$table1.$key2" => [2,4]});
cleanup test
Yuki Kimoto authored on 2011-08-08
309
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
310
is_deeply($rows, [{$key1 => 2, $key2 => 4}], "filter");
cleanup test
Yuki Kimoto authored on 2011-08-08
311

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
318
$source = "select * from $table1 where $key1 = :$key1 and {<> $key2} and {< $key3} and {> $key4} and {>= $key5}";
cleanup test
Yuki Kimoto authored on 2011-08-08
319
$query = $dbi->execute($source, {}, query => 1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
320
$result = $dbi->execute($query, param => {$key1 => 1, $key2 => 3, $key3 => 4, $key4 => 3, $key5 => 5});
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 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}], "basic tag1");
cleanup test
Yuki Kimoto authored on 2011-08-08
323

            
cleanup test
Yuki Kimoto authored on 2011-08-15
324
$source = "select * from $table1 where $key1 = :$key1 and {<> $key2} and {< $key3} and {> $key4} and {>= $key5}";
cleanup test
Yuki Kimoto authored on 2011-08-08
325
$query = $dbi->execute($source, {}, query => 1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
326
$result = $dbi->execute($query, {$key1 => 1, $key2 => 3, $key3 => 4, $key4 => 3, $key5 => 5});
cleanup test
Yuki Kimoto authored on 2011-08-08
327
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
328
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}], "basic tag1");
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} and {like $key2}";
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 => '%2%'});
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 tag2");
cleanup test
Yuki Kimoto authored on 2011-08-08
335

            
336
test 'DIB::Custom::SQLTemplate in tag';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
337
$dbi->execute("drop table $table1");
cleanup test
Yuki Kimoto authored on 2011-08-08
338
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
339
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
340
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
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 {in $key1 2}";
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 => [9, 1]});
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");
cleanup test
Yuki Kimoto authored on 2011-08-08
347

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

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

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

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

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

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
377
$source = "select * from $table1 where $key1 = :$key1 and $key2 = :$key2";
378
$result = $dbi->execute($source, param => {$key1 => 1, $key2 => 2});
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 => 2, $key3 => 3, $key4 => 4, $key5 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-08
381

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

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
392
$source = "select * from $table1 where $key1 = :$table1.$key1 and $key2 = :$table1.$key2";
cleanup test
Yuki Kimoto authored on 2011-08-08
393
$result = $dbi->execute(
394
    $source,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
395
    param => {"$table1.$key1" => 1, "$table1.$key2" => 1},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
396
    filter => {"$table1.$key2" => sub { $_[0] * 2 }}
cleanup test
Yuki Kimoto authored on 2011-08-08
397
);
398
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
399
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-08
400

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
401
$dbi->execute("drop table $table1");
cleanup test
Yuki Kimoto authored on 2011-08-08
402
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
403
$dbi->insert(table => $table1, param => {$key1 => '2011-10-14 12:19:18', $key2 => 2});
404
$source = "select * from $table1 where $key1 = '2011-10-14 12:19:18' and $key2 = :$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 => {$key2 => 2},
cleanup test
Yuki Kimoto authored on 2011-08-08
408
);
409

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
414
$dbi->delete_all(table => $table1);
415
$dbi->insert(table => $table1, param => {$key1 => 'a:b c:d', $key2 => 2});
416
$source = "select * from $table1 where $key1 = 'a\\:b c\\:d' 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
$rows = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
422
is_deeply($rows, [{$key1 => 'a:b c:d', $key2 => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-08
423

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
431
test 'insert';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
432
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
433
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
434
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
435
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
cleanup test
Yuki Kimoto authored on 2011-08-15
436
$result = $dbi->execute("select * from $table1");
cleanup test
Yuki Kimoto authored on 2011-08-10
437
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
438
is_deeply($rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], "basic");
cleanup test
Yuki Kimoto authored on 2011-08-10
439

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
440
$dbi->execute("delete from $table1");
cleanup test
Yuki Kimoto authored on 2011-08-10
441
$dbi->register_filter(
442
    twice       => sub { $_[0] * 2 },
443
    three_times => sub { $_[0] * 3 }
444
);
445
$dbi->default_bind_filter('twice');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
446
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}, filter => {$key1 => 'three_times'});
cleanup test
Yuki Kimoto authored on 2011-08-15
447
$result = $dbi->execute("select * from $table1");
cleanup test
Yuki Kimoto authored on 2011-08-10
448
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
449
is_deeply($rows, [{$key1 => 3, $key2 => 4}], "filter");
cleanup test
Yuki Kimoto authored on 2011-08-10
450
$dbi->default_bind_filter(undef);
451

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
452
$dbi->execute("drop table $table1");
cleanup test
Yuki Kimoto authored on 2011-08-10
453
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
454
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}, append => '   ');
455
$rows = $dbi->select(table => $table1)->all;
456
is_deeply($rows, [{$key1 => 1, $key2 => 2}], 'insert append');
cleanup test
Yuki Kimoto authored on 2011-08-10
457

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

            
461
eval{$dbi->insert(table => 'table', param => {';' => 1})};
462
like($@, qr/safety/);
463

            
cleanup test
Yuki Kimoto authored on 2011-08-10
464
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
465
$dbi->execute($create_table_reserved);
cleanup test
Yuki Kimoto authored on 2011-08-10
466
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
467
$dbi->insert(table => 'table', param => {select => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
468
$result = $dbi->execute("select * from ${q}table$p");
cleanup test
Yuki Kimoto authored on 2011-08-10
469
$rows   = $result->all;
test cleanup
Yuki Kimoto authored on 2011-08-10
470
is_deeply($rows, [{select => 2, update => undef}], "reserved word");
cleanup test
Yuki Kimoto authored on 2011-08-10
471

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
472
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
473
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
474
$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1);
475
$dbi->insert({$key1 => 3, $key2 => 4}, table => $table1);
cleanup test
Yuki Kimoto authored on 2011-08-15
476
$result = $dbi->execute("select * from $table1");
cleanup test
Yuki Kimoto authored on 2011-08-10
477
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
478
is_deeply($rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], "basic");
cleanup test
Yuki Kimoto authored on 2011-08-10
479

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
488
test 'update';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
489
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
490
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
491
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
492
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
493
$dbi->update(table => $table1, param => {$key2 => 11}, where => {$key1 => 1});
cleanup test
Yuki Kimoto authored on 2011-08-15
494
$result = $dbi->execute("select * from $table1 order by $key1");
test cleanup
Yuki Kimoto authored on 2011-08-10
495
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
496
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
497
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
498
                  "basic");
499
                  
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
500
$dbi->execute("delete from $table1");
501
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
502
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
503
$dbi->update(table => $table1, param => {$key2 => 12}, where => {$key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-15
504
$result = $dbi->execute("select * from $table1 order by $key1");
test cleanup
Yuki Kimoto authored on 2011-08-10
505
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
506
is_deeply($rows, [{$key1 => 1, $key2 => 12, $key3 => 3, $key4 => 4, $key5 => 5},
507
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
508
                  "update key same as search key");
509

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
517
$dbi->execute("delete from $table1");
518
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
519
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
test cleanup
Yuki Kimoto authored on 2011-08-10
520
$dbi->register_filter(twice => sub { $_[0] * 2 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
521
$dbi->update(table => $table1, param => {$key2 => 11}, where => {$key1 => 1},
522
              filter => {$key2 => sub { $_[0] * 2 }});
cleanup test
Yuki Kimoto authored on 2011-08-15
523
$result = $dbi->execute("select * from $table1 order by $key1");
test cleanup
Yuki Kimoto authored on 2011-08-10
524
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
525
is_deeply($rows, [{$key1 => 1, $key2 => 22, $key3 => 3, $key4 => 4, $key5 => 5},
526
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
527
                  "filter");
528

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

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

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

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
537
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
538
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
539
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
540
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
541
$where->clause(['and', "$key1 = :$key1", "$key2 = :$key2"]);
542
$where->param({$key1 => 1, $key2 => 2});
543
$dbi->update(table => $table1, param => {$key1 => 3}, where => $where);
544
$result = $dbi->select(table => $table1);
545
is_deeply($result->all, [{$key1 => 3, $key2 => 2}], 'update() where');
test cleanup
Yuki Kimoto authored on 2011-08-10
546

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

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

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

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

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
577
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
578
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
579
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
580
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
581
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
582
$dbi->insert(table => 'table', param => {select => 1});
583
$dbi->update(table => 'table', where => {select => 1}, param => {update => 2});
584
$result = $dbi->execute("select * from ${q}table$p");
585
$rows   = $result->all;
586
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
587

            
588
eval {$dbi->update_all(table => 'table', param => {';' => 2}) };
589
like($@, qr/safety/);
590

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

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
601
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
602
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
603
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
604
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
605
$dbi->update({$key2 => 11}, table => $table1, where => {$key1 => 1});
cleanup test
Yuki Kimoto authored on 2011-08-15
606
$result = $dbi->execute("select * from $table1 order by $key1");
test cleanup
Yuki Kimoto authored on 2011-08-10
607
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
608
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
609
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
610
                  "basic");
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_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
614
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
615
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
616
$dbi->update(table => $table1, param => {$key2 => \"'11'"}, where => {$key1 => 1});
cleanup test
Yuki Kimoto authored on 2011-08-15
617
$result = $dbi->execute("select * from $table1 order by $key1");
test cleanup
Yuki Kimoto authored on 2011-08-10
618
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
619
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
620
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
test cleanup
Yuki Kimoto authored on 2011-08-10
621
                  "basic");
622

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

            
636

            
637
test 'delete';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
638
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
639
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
640
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
641
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
642
$dbi->delete(table => $table1, where => {$key1 => 1});
cleanup test
Yuki Kimoto authored on 2011-08-15
643
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
644
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
645
is_deeply($rows, [{$key1 => 3, $key2 => 4}], "basic");
test cleanup
Yuki Kimoto authored on 2011-08-10
646

            
cleanup test
Yuki Kimoto authored on 2011-08-15
647
$dbi->execute("delete from $table1");
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
648
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
649
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-10
650
$dbi->register_filter(twice => sub { $_[0] * 2 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
651
$dbi->delete(table => $table1, where => {$key2 => 1}, filter => {$key2 => 'twice'});
cleanup test
Yuki Kimoto authored on 2011-08-15
652
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
653
$rows   = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
654
is_deeply($rows, [{$key1 => 3, $key2 => 4}], "filter");
test cleanup
Yuki Kimoto authored on 2011-08-10
655

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
658
$dbi->delete_all(table => $table1);
659
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
660
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
661
$dbi->delete(table => $table1, where => {$key1 => 1, $key2 => 2});
662
$rows = $dbi->select(table => $table1)->all;
663
is_deeply($rows, [{$key1 => 3, $key2 => 4}], "delete multi key");
test cleanup
Yuki Kimoto authored on 2011-08-10
664

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

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

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

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
693
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
694
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
695
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
696
$dbi->delete(table => $table1, where => {$key1 => 1}, prefix => '    ');
cleanup test
Yuki Kimoto authored on 2011-08-15
697
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
698
$rows   = $result->all;
699
is_deeply($rows, [], "basic");
700

            
701
test 'delete error';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
702
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
703
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
704
eval{$dbi->delete(table => $table1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
705
like($@, qr/"where" must be specified/,
706
         "where key-value pairs not specified");
707

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
711
$dbi = undef;
test cleanup
Yuki Kimoto authored on 2011-08-10
712
$dbi = DBIx::Custom->connect;
713
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
714
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
715
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
716
$dbi->insert(table => 'table', param => {select => 1});
717
$dbi->delete(table => 'table', where => {select => 1});
718
$result = $dbi->execute("select * from ${q}table$p");
719
$rows   = $result->all;
720
is_deeply($rows, [], "reserved word");
721

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

            
732

            
733
test 'select';
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
$rows = $dbi->select(table => $table1)->all;
739
is_deeply($rows, [{$key1 => 1, $key2 => 2},
740
                  {$key1 => 3, $key2 => 4}], "table");
test cleanup
Yuki Kimoto authored on 2011-08-10
741

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

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

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

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
756
eval { $dbi->execute("drop table $table2") };
test cleanup
Yuki Kimoto authored on 2011-08-10
757
$dbi->execute($create_table2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
758
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
test cleanup
Yuki Kimoto authored on 2011-08-10
759
$rows = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
760
    table => [$table1, $table2],
761
    column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
762
    where   => {"$table1.$key2" => 2},
763
    relation  => {"$table1.$key1" => "$table2.$key1"}
test cleanup
Yuki Kimoto authored on 2011-08-10
764
)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
765
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
766

            
767
$rows = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
768
    table => [$table1, $table2],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
769
    column => ["$table1.$key1 as ${table1}_$key1", "${table2}.$key1 as ${table2}_$key1", $key2, $key3],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
770
    relation  => {"$table1.$key1" => "$table2.$key1"}
test cleanup
Yuki Kimoto authored on 2011-08-10
771
)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
772
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
773

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

            
777
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
778
eval { $dbi->execute("drop table ${q}table$p") };
779
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
780
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
781
$dbi->insert(table => 'table', param => {select => 1, update => 2});
782
$result = $dbi->select(table => 'table', where => {select => 1});
783
$rows   = $result->all;
784
is_deeply($rows, [{select => 2, update => 2}], "reserved word");
785

            
786
test 'fetch filter';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
787
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
788
$dbi->register_filter(
789
    twice       => sub { $_[0] * 2 },
790
    three_times => sub { $_[0] * 3 }
791
);
792
$dbi->default_fetch_filter('twice');
793
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
794
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
795
$result = $dbi->select(table => $table1);
796
$result->filter({$key1 => 'three_times'});
test cleanup
Yuki Kimoto authored on 2011-08-10
797
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
798
is_deeply($row, {$key1 => 3, $key2 => 4}, "default_fetch_filter and filter");
test cleanup
Yuki Kimoto authored on 2011-08-10
799

            
800
test 'filters';
801
$dbi = DBIx::Custom->new;
802

            
803
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')),
804
   'あ', "decode_utf8");
805

            
806
is($dbi->filters->{encode_utf8}->('あ'),
807
   encode_utf8('あ'), "encode_utf8");
808

            
cleanup test
Yuki Kimoto authored on 2011-08-10
809
test 'transaction1';
test cleanup
Yuki Kimoto authored on 2011-08-10
810
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
811
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
812
$dbi->execute($create_table1);
fixed transaction test
Yuki Kimoto authored on 2011-08-14
813
$dbi->begin_work;
814
$dbi->dbh->{AutoCommit} = 0;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
815
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
fixed transaction test
Yuki Kimoto authored on 2011-08-14
816
$dbi->rollback;
817
$dbi->dbh->{AutoCommit} = 1;
818

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

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

            
823
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
824
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
825
$dbi->execute($create_table1);
fixed transaction test
Yuki Kimoto authored on 2011-08-14
826
$dbi->begin_work;
827
$dbi->dbh->{AutoCommit} = 0;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
828
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
829
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
fixed transaction test
Yuki Kimoto authored on 2011-08-14
830
$dbi->commit;
831
$dbi->dbh->{AutoCommit} = 1;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
832
$result = $dbi->select(table => $table1);
833
is_deeply(scalar $result->all, [{$key1 => 1, $key2 => 2}, {$key1 => 2, $key2 => 3}],
fixed transaction test
Yuki Kimoto authored on 2011-08-14
834
          "commit");
test cleanup
Yuki Kimoto authored on 2011-08-10
835

            
836
test 'execute';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
837
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
838
$dbi->execute($create_table1);
839
{
840
    local $Carp::Verbose = 0;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
841
    eval{$dbi->execute("select * frm $table1")};
cleanup test
Yuki Kimoto authored on 2011-08-15
842
    like($@, qr/\Qselect * frm $table1/, "fail prepare");
test cleanup
Yuki Kimoto authored on 2011-08-10
843
    like($@, qr/\.t /, "fail : not verbose");
844
}
845
{
846
    local $Carp::Verbose = 1;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
847
    eval{$dbi->execute("select * frm $table1")};
test cleanup
Yuki Kimoto authored on 2011-08-10
848
    like($@, qr/Custom.*\.t /s, "fail : verbose");
849
}
850

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

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

            
859
{
860
    local $Carp::Verbose = 0;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
861
    eval{$dbi->execute("select * from $table1 where {0 $key1}", {}, query => 1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
862
    like($@, qr/\Q.t /, "caller spec : not vebose");
863
}
864
{
865
    local $Carp::Verbose = 1;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
866
    eval{$dbi->execute("select * from $table1 where {0 $key1}", {}, query => 1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
867
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
868
}
869

            
870

            
cleanup test
Yuki Kimoto authored on 2011-08-10
871
test 'transaction2';
test cleanup
Yuki Kimoto authored on 2011-08-10
872
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
873
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
874
$dbi->execute($create_table1);
875

            
876
$dbi->begin_work;
877

            
878
eval {
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
879
    $dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
880
    die "Error";
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
881
    $dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-10
882
};
883

            
884
$dbi->rollback if $@;
885

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

            
890
$dbi->begin_work;
891

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

            
897
$dbi->commit unless $@;
898

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

            
903
$dbi->dbh->{AutoCommit} = 0;
904
eval{ $dbi->begin_work };
905
ok($@, "exception");
906
$dbi->dbh->{AutoCommit} = 1;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
907

            
test cleanup
Yuki Kimoto authored on 2011-08-10
908
test 'cache';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
909
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
910
$dbi->cache(1);
911
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-15
912
$source = "select * from $table1 where $key1 = :$key1 and $key2 = :$key2";
test cleanup
Yuki Kimoto authored on 2011-08-10
913
$dbi->execute($source, {}, query => 1);
914
is_deeply($dbi->{_cached}->{$source}, 
cleanup test
Yuki Kimoto authored on 2011-08-15
915
          {sql => "select * from $table1 where $key1 = ? and $key2 = ?", columns => [$key1, $key2], tables => []}, "cache");
test cleanup
Yuki Kimoto authored on 2011-08-10
916

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
917
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
918
$dbi->execute($create_table1);
919
$dbi->{_cached} = {};
920
$dbi->cache(0);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
921
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
922
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
923

            
924
test 'execute';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
925
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
926
$dbi->execute($create_table1);
927
{
928
    local $Carp::Verbose = 0;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
929
    eval{$dbi->execute("select * frm $table1")};
cleanup test
Yuki Kimoto authored on 2011-08-15
930
    like($@, qr/\Qselect * frm $table1/, "fail prepare");
test cleanup
Yuki Kimoto authored on 2011-08-10
931
    like($@, qr/\.t /, "fail : not verbose");
932
}
933
{
934
    local $Carp::Verbose = 1;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
935
    eval{$dbi->execute('select * frm $table1')};
test cleanup
Yuki Kimoto authored on 2011-08-10
936
    like($@, qr/Custom.*\.t /s, "fail : verbose");
937
}
938

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

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

            
947
{
948
    local $Carp::Verbose = 0;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
949
    eval{$dbi->execute("select * from $table1 where {0 $key1}", {}, query => 1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
950
    like($@, qr/\Q.t /, "caller spec : not vebose");
951
}
952
{
953
    local $Carp::Verbose = 1;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
954
    eval{$dbi->execute("select * from $table1 where {0 $key1}", {}, query => 1)};
test cleanup
Yuki Kimoto authored on 2011-08-10
955
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
956
}
957

            
958
test 'method';
959
$dbi->method(
960
    one => sub { 1 }
961
);
962
$dbi->method(
963
    two => sub { 2 }
964
);
965
$dbi->method({
966
    twice => sub {
967
        my $self = shift;
968
        return $_[0] * 2;
969
    }
970
});
971

            
972
is($dbi->one, 1, "first");
973
is($dbi->two, 2, "second");
974
is($dbi->twice(5), 10 , "second");
975

            
976
eval {$dbi->XXXXXX};
977
ok($@, "not exists");
978

            
979
test 'out filter';
980
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
981
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
982
$dbi->execute($create_table1);
983
$dbi->register_filter(twice => sub { $_[0] * 2 });
984
$dbi->register_filter(three_times => sub { $_[0] * 3});
985
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
986
    $table1, $key1 => {out => 'twice', in => 'three_times'}, 
987
              $key2 => {out => 'three_times', in => 'twice'});
988
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-15
989
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
990
$row   = $result->fetch_hash_first;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
991
is_deeply($row, {$key1 => 2, $key2 => 6}, "insert");
992
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
993
$row   = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
994
is_deeply($row, {$key1 => 6, $key2 => 12}, "insert");
test cleanup
Yuki Kimoto authored on 2011-08-10
995

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

            
1012
$dbi = DBIx::Custom->connect;
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->register_filter(twice => sub { $_[0] * 2 });
1016
$dbi->apply_filter(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1017
    $table1, $key1 => {out => 'twice', in => 'twice'}
test cleanup
Yuki Kimoto authored on 2011-08-10
1018
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1019
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}, filter => {$key1 => undef});
1020
$dbi->update(table => $table1, param => {$key1 => 2}, where => {$key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-15
1021
$result = $dbi->execute("select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
1022
$row   = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1023
is_deeply($row, {$key1 => 4, $key2 => 2}, "update");
test cleanup
Yuki Kimoto authored on 2011-08-10
1024

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

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

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

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

            
1078
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1079
eval { $dbi->execute("drop table $table1") };
1080
eval { $dbi->execute("drop table $table2") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1081
$dbi->execute($create_table1);
1082
$dbi->execute($create_table2);
1083
$dbi->register_filter(twice => sub { $_[0] * 2 });
1084
$dbi->register_filter(three_times => sub { $_[0] * 3 });
1085
$dbi->apply_filter(
cleanup test
Yuki Kimoto authored on 2011-08-15
1086
    $table1, $key2 => {out => 'twice', in => 'twice'}
1087
);
1088
$dbi->apply_filter(
1089
    $table2, $key3 => {out => 'three_times', in => 'three_times'}
test cleanup
Yuki Kimoto authored on 2011-08-10
1090
);
cleanup test
Yuki Kimoto authored on 2011-08-15
1091
$dbi->insert(table => $table1, param => {$key1 => 5, $key2 => 2}, filter => {$key2 => undef});
1092
$dbi->insert(table => $table2, param => {$key1 => 5, $key3 => 6}, filter => {$key3 => undef});
1093
$result = $dbi->select(
1094
     table => [$table1, $table2],
1095
     column => [$key2, $key3],
1096
     where => {"$table1.$key2" => 1, "$table2.$key3" => 2}, relation => {"$table1.$key1" => "$table2.$key1"});
1097

            
1098
$result->filter({$key2 => 'twice'});
1099
$rows   = $result->all;
1100
is_deeply($rows, [{$key2 => 4, $key3 => 18}], "select : join");
1101

            
1102
$result = $dbi->select(
1103
     table => [$table1, $table2],
1104
     column => [$key2, $key3],
1105
     where => {$key2 => 1, $key3 => 2}, relation => {"$table1.$key1" => "$table2.$key1"});
1106

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

            
1111
test 'connect super';
test cleanup
Yuki Kimoto authored on 2011-08-10
1112
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1113
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1114
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1115
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1116
is($dbi->select(table => $table1)->one->{$key1}, 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1117

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1118
$dbi = DBIx::Custom->new;
test cleanup
Yuki Kimoto authored on 2011-08-10
1119
$dbi->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1120
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1121
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1122
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1123
is($dbi->select(table => $table1)->one->{$key1}, 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1124

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

            
1131
test 'end_filter';
1132
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1133
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1134
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1135
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1136
$result = $dbi->select(table => $table1);
1137
$result->filter($key1 => sub { $_[0] * 2 }, $key2 => sub { $_[0] * 4 });
1138
$result->end_filter($key1 => sub { $_[0] * 3 }, $key2 => sub { $_[0] * 5 });
test cleanup
Yuki Kimoto authored on 2011-08-10
1139
$row = $result->fetch_first;
1140
is_deeply($row, [6, 40]);
1141

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

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

            
1162
$dbi->register_filter(five_times => sub { $_[0] * 5 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1163
$result = $dbi->select(table => $table1);
1164
$result->filter($key1 => sub { $_[0] * 2 }, $key2 => sub { $_[0] * 4 });
1165
$result->end_filter({$key1 => sub { $_[0] * 3 }, $key2 => 'five_times' });
test cleanup
Yuki Kimoto authored on 2011-08-10
1166
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1167
is_deeply($row, {$key1 => 6, $key2 => 40});
test cleanup
Yuki Kimoto authored on 2011-08-10
1168

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

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1191
test 'remove_end_filter and remove_filter';
1192
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1193
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1194
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1195
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1196
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1197
$row = $result
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1198
       ->filter($key1 => sub { $_[0] * 2 }, $key2 => sub { $_[0] * 4 })
test cleanup
Yuki Kimoto authored on 2011-08-10
1199
       ->remove_filter
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1200
       ->end_filter($key1 => sub { $_[0] * 3 }, $key2 => sub { $_[0] * 5 })
test cleanup
Yuki Kimoto authored on 2011-08-10
1201
       ->remove_end_filter
1202
       ->fetch_first;
1203
is_deeply($row, [1, 2]);
1204

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

            
1214
test 'select query option';
1215
$dbi = DBIx::Custom->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
$query = $dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1219
is(ref $query, 'DBIx::Custom::Query');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1220
$query = $dbi->update(table => $table1, where => {$key1 => 1}, param => {$key2 => 2}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1221
is(ref $query, 'DBIx::Custom::Query');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1222
$query = $dbi->delete(table => $table1, where => {$key1 => 1}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1223
is(ref $query, 'DBIx::Custom::Query');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1224
$query = $dbi->select(table => $table1, where => {$key1 => 1, $key2 => 2}, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1225
is(ref $query, 'DBIx::Custom::Query');
1226

            
1227
test 'where';
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
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
1233
$where = $dbi->where->clause(['and', "$key1 = :$key1", "$key2 = :$key2"]);
1234
is("$where", "where ( $key1 = :$key1 and $key2 = :$key2 )", 'no param');
test cleanup
Yuki Kimoto authored on 2011-08-10
1235

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

            
1240
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1241
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1242
    where => $where
1243
);
1244
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1245
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1246

            
1247
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1248
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1249
    where => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1250
        ['and', "$key1 = :$key1", "$key2 = :$key2"],
1251
        {$key1 => 1}
test cleanup
Yuki Kimoto authored on 2011-08-10
1252
    ]
1253
);
1254
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1255
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1256

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

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

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

            
1287
$where = $dbi->where;
1288
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1289
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1290
    where => $where
1291
);
1292
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1293
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1294

            
1295
eval {
1296
$where = $dbi->where
1297
             ->clause(['uuu']);
1298
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1299
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1300
    where => $where
1301
);
1302
};
1303
ok($@);
1304

            
1305
$where = $dbi->where;
1306
is("$where", '');
1307

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

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

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

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

            
1348
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1349
             ->clause("$key1 = :$key1 $key2 = :$key2")
1350
             ->param({$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1351
eval{$where->to_string};
1352
like($@, qr/one column/);
1353

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

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

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

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

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

            
1404
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1405
             ->clause(['or', ("$key1 = :$key1") x 3])
1406
             ->param({$key1 => [$dbi->not_exists, $dbi->not_exists, 1]});
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}], 'not_exists');
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 3])
1416
             ->param({$key1 => [$dbi->not_exists, $dbi->not_exists, $dbi->not_exists]});
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}, {$key1 => 3, $key2 => 4}], 'not_exists');
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 3])
1426
             ->param({$key1 => []});
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}, {$key1 => 3, $key2 => 4}], 'not_exists');
test cleanup
Yuki Kimoto authored on 2011-08-10
1433

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

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

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

            
1474
$where = $dbi->where
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1475
             ->clause(['and',"$key1 is not null", "$key2 is not null" ]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1476
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1477
    table => $table1,
test cleanup
Yuki Kimoto authored on 2011-08-10
1478
    where => $where,
1479
);
1480
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1481
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], 'not_exists');
test cleanup
Yuki Kimoto authored on 2011-08-10
1482

            
1483
eval {$dbi->where(ppp => 1) };
1484
like($@, qr/invalid/);
1485

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

            
1497

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

            
1509
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1510
$where->clause(['and', ":${key1}{=}"]);
1511
$where->param({$key1 => undef});
1512
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1513
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1514
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1515

            
1516
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1517
$where->clause(['and', ":${key1}{=}"]);
1518
$where->param({$key1 => undef});
test cleanup
Yuki Kimoto authored on 2011-08-10
1519
$where->if('defined');
1520
$where->map;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1521
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1522
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1523
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1524

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

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

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

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

            
1565
$where = $dbi->where;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1566
$where->clause(['and', ":${key1}{=}"]);
1567
$where->param({$key1 => 5});
test cleanup
Yuki Kimoto authored on 2011-08-10
1568
$where->if(sub { ($_[0] || '') eq 5 });
1569
$where->map;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1570
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
1571
$row = $result->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1572
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1573

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

            
1583
$where = $dbi->where;
1584
$where->param({id => 1, author => 'Ken', price => 1900});
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1585
$where->map(id => "$table1.id",
1586
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
1587
    price => ["$table1.price", {if => sub { $_[0] eq 1900 }}]
test cleanup
Yuki Kimoto authored on 2011-08-10
1588
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1589
is_deeply($where->param, {"$table1.id" => 1, "$table1.author" => '%Ken%',
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1590
  "$table1.price" => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
1591

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

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

            
1613
$where = $dbi->where;
1614
$where->param({id => undef, author => undef, price => undef});
1615
$where->if('length');
1616
$where->map(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1617
    id => "$table1.id",
1618
    price => ["$table1.price", {if => 'exists'}]
test cleanup
Yuki Kimoto authored on 2011-08-10
1619
);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1620
is_deeply($where->param, {"$table1.price" => undef});
test cleanup
Yuki Kimoto authored on 2011-08-10
1621

            
1622
$where = $dbi->where;
1623
$where->param({price => 'a'});
1624
$where->if('length');
1625
$where->map(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1626
    id => ["$table1.id", {if => 'exists'}],
1627
    price => ["$table1.price", sub { '%' . $_[0] }, {if => 'exists'}]
test cleanup
Yuki Kimoto authored on 2011-08-10
1628
);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1629
is_deeply($where->param, {"$table1.price" => '%a'});
test cleanup
Yuki Kimoto authored on 2011-08-10
1630

            
1631
$where = $dbi->where;
1632
$where->param({id => [1, 2], author => 'Ken', price => 1900});
1633
$where->map(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1634
    id => "$table1.id",
1635
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
1636
    price => ["$table1.price", {if => sub { $_[0] eq 1900 }}]
test cleanup
Yuki Kimoto authored on 2011-08-10
1637
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1638
is_deeply($where->param, {"$table1.id" => [1, 2], "$table1.author" => '%Ken%',
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1639
  "$table1.price" => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
1640

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

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

            
1662
test 'register_tag_processor';
1663
$dbi = DBIx::Custom->connect;
1664
$dbi->register_tag_processor(
1665
    a => sub { 1 }
1666
);
test cleanup
Yuki Kimoto authored on 2011-08-10
1667
is($dbi->{_tags}->{a}->(), 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
1668

            
1669
test 'register_tag';
1670
$dbi = DBIx::Custom->connect;
1671
$dbi->register_tag(
1672
    b => sub { 2 }
1673
);
test cleanup
Yuki Kimoto authored on 2011-08-10
1674
is($dbi->{_tags}->{b}->(), 2);
test cleanup
Yuki Kimoto authored on 2011-08-10
1675

            
1676
test 'table not specify exception';
1677
$dbi = DBIx::Custom->connect;
1678
eval {$dbi->insert};
1679
like($@, qr/table/);
1680
eval {$dbi->update};
1681
like($@, qr/table/);
1682
eval {$dbi->delete};
1683
like($@, qr/table/);
1684
eval {$dbi->select};
1685
like($@, qr/table/);
test cleanup
Yuki Kimoto authored on 2011-08-10
1686

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1687
test 'more tests';
1688
$dbi = DBIx::Custom->connect;
1689
eval{$dbi->apply_filter('table', 'column', [])};
1690
like($@, qr/apply_filter/);
1691

            
1692
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
1693
like($@, qr/apply_filter/);
1694

            
1695
$dbi->apply_filter(
1696

            
1697
);
1698
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1699
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1700
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1701
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
1702
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
1703
$dbi->apply_filter($table1, $key2, 
test cleanup
Yuki Kimoto authored on 2011-08-10
1704
                   {in => sub { $_[0] * 3 }, out => sub { $_[0] * 2 }});
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1705
$rows = $dbi->select(table => $table1, where => {$key2 => 1})->all;
1706
is_deeply($rows, [{$key1 => 1, $key2 => 6}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
1707

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

            
1717
$dbi = DBIx::Custom->connect;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1718
eval {$dbi->apply_filter($table1, $key2, {out => 'no'})};
test cleanup
Yuki Kimoto authored on 2011-08-10
1719
like($@, qr/not registered/);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1720
eval {$dbi->apply_filter($table1, $key2, {in => 'no'})};
test cleanup
Yuki Kimoto authored on 2011-08-10
1721
like($@, qr/not registered/);
1722
$dbi->method({one => sub { 1 }});
1723
is($dbi->one, 1);
1724

            
1725
eval{DBIx::Custom->connect(dsn => undef)};
1726
like($@, qr/_connect/);
1727

            
1728
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1729
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1730
$dbi->execute($create_table1);
1731
$dbi->register_filter(twice => sub { $_[0] * 2 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1732
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2},
1733
             filter => {$key1 => 'twice'});
1734
$row = $dbi->select(table => $table1)->one;
1735
is_deeply($row, {$key1 => 2, $key2 => 2});
1736
eval {$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2},
1737
             filter => {$key1 => 'no'}) };
test cleanup
Yuki Kimoto authored on 2011-08-10
1738
like($@, qr//);
1739

            
1740
$dbi->register_filter(one => sub { });
1741
$dbi->default_fetch_filter('one');
1742
ok($dbi->default_fetch_filter);
1743
$dbi->default_bind_filter('one');
1744
ok($dbi->default_bind_filter);
1745
eval{$dbi->default_fetch_filter('no')};
1746
like($@, qr/not registered/);
1747
eval{$dbi->default_bind_filter('no')};
1748
like($@, qr/not registered/);
1749
$dbi->default_bind_filter(undef);
1750
ok(!defined $dbi->default_bind_filter);
1751
$dbi->default_fetch_filter(undef);
1752
ok(!defined $dbi->default_fetch_filter);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1753
eval {$dbi->execute("select * from $table1 {} {= author") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1754
like($@, qr/Tag not finished/);
1755

            
1756
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1757
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
1758
$dbi->execute($create_table1);
1759
$dbi->register_filter(one => sub { 1 });
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1760
$result = $dbi->select(table => $table1);
1761
eval {$result->filter($key1 => 'no')};
test cleanup
Yuki Kimoto authored on 2011-08-10
1762
like($@, qr/not registered/);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1763
eval {$result->end_filter($key1 => 'no')};
test cleanup
Yuki Kimoto authored on 2011-08-10
1764
like($@, qr/not registered/);
1765
$result->default_filter(undef);
1766
ok(!defined $result->default_filter);
1767
$result->default_filter('one');
1768
is($result->default_filter->(), 1);
1769

            
1770
test 'dbi_option';
1771
$dbi = DBIx::Custom->connect(dbi_option => {PrintError => 1});
1772
ok($dbi->dbh->{PrintError});
1773
$dbi = DBIx::Custom->connect(dbi_options => {PrintError => 1});
1774
ok($dbi->dbh->{PrintError});
1775

            
1776
test 'DBIx::Custom::Result stash()';
1777
$result = DBIx::Custom::Result->new;
1778
is_deeply($result->stash, {}, 'default');
1779
$result->stash->{foo} = 1;
1780
is($result->stash->{foo}, 1, 'get and set');
test cleanup
Yuki Kimoto authored on 2011-08-10
1781

            
cleanup test
Yuki Kimoto authored on 2011-08-10
1782
test 'delete_at';
1783
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1784
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
1785
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1786
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1787
$dbi->delete_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1788
    table => $table1,
1789
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1790
    where => [1, 2],
1791
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1792
is_deeply($dbi->select(table => $table1)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
1793

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1794
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1795
$dbi->delete_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1796
    table => $table1,
1797
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
1798
    where => 1,
1799
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1800
is_deeply($dbi->select(table => $table1)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
1801

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1816
$dbi->delete_all(table => $table1);
1817
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1818
$dbi->insert_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1819
    primary_key => $key1, 
1820
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
1821
    where => 1,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1822
    param => {$key2 => 2, $key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
1823
);
1824

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

            
1829
eval {
1830
    $dbi->insert_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1831
        table => $table1,
1832
        primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1833
        where => {},
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1834
        param => {$key1 => 1, $key2 => 2, $key3 => 3},
cleanup test
Yuki Kimoto authored on 2011-08-10
1835
    );
1836
};
1837
like($@, qr/must be/);
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);
1842
$dbi->insert_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1843
    {$key3 => 3},
1844
    primary_key => [$key1, $key2], 
1845
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
1846
    where => [1, 2],
1847
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1848
is($dbi->select(table => $table1)->one->{$key1}, 1);
1849
is($dbi->select(table => $table1)->one->{$key2}, 2);
1850
is($dbi->select(table => $table1)->one->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
1851

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

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

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

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1908
$dbi->delete_all(table => $table1);
1909
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
1910
$result = $dbi->select_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1911
    table => $table1,
1912
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
1913
    where => 1,
1914
);
1915
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1916
is($row->{$key1}, 1);
1917
is($row->{$key2}, 2);
1918
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
1919

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

            
1932
eval {
1933
    $result = $dbi->select_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1934
        table => $table1,
1935
        primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1936
        where => {},
1937
    );
1938
};
1939
like($@, qr/must be/);
1940

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

            
1950
eval {
1951
    $result = $dbi->update_at(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1952
        table => $table1,
1953
        primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
1954
        where => {},
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1955
        param => {$key1 => 1, $key2 => 2},
cleanup test
Yuki Kimoto authored on 2011-08-10
1956
    );
1957
};
1958
like($@, qr/must be/);
1959

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

            
1969
test 'model delete_at';
1970
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1971
eval { $dbi->execute("drop table $table1") };
1972
eval { $dbi->execute("drop table $table2") };
1973
eval { $dbi->execute("drop table $table3") };
cleanup test
Yuki Kimoto authored on 2011-08-10
1974
$dbi->execute($create_table1_2);
1975
$dbi->execute($create_table2_2);
1976
$dbi->execute($create_table3);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1977
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
1978
$dbi->model($table1)->delete_at(where => [1, 2]);
1979
is_deeply($dbi->select(table => $table1)->all, []);
1980
$dbi->insert(table => $table2, param => {$key1 => 1, $key2 => 2, $key3 => 3});
1981
$dbi->model($table1)->delete_at(where => [1, 2]);
1982
is_deeply($dbi->select(table => $table1)->all, []);
1983
$dbi->insert(table => $table3, param => {$key1 => 1, $key2 => 2, $key3 => 3});
1984
$dbi->model($table3)->delete_at(where => [1, 2]);
1985
is_deeply($dbi->select(table => $table3)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
1986

            
1987
test 'model insert_at';
1988
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
1989
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
1990
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1991
$dbi->model($table1)->insert_at(
cleanup test
Yuki Kimoto authored on 2011-08-10
1992
    where => [1, 2],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1993
    param => {$key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
1994
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1995
$result = $dbi->model($table1)->select;
cleanup test
Yuki Kimoto authored on 2011-08-10
1996
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
1997
is($row->{$key1}, 1);
1998
is($row->{$key2}, 2);
1999
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2000

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

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

            
2027

            
2028
test 'mycolumn and column';
2029
$dbi = MyDBI7->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2030
eval { $dbi->execute("drop table $table1") };
2031
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2032
$dbi->execute($create_table1);
2033
$dbi->execute($create_table2);
2034
$dbi->separator('__');
2035
$dbi->setup_model;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2036
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2037
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
2038
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2039
$result = $model->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2040
    column => [$model->mycolumn, $model->column($table2)],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2041
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2042
);
2043
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2044
          {$key1 => 1, $key2 => 2, "${table2}__$key1" => 1, "${table2}__$key3" => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2045

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2046
test 'insert_param';
2047
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2048
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2049
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2050
$param = {$key1 => 1, $key2 => 2};
test cleanup
Yuki Kimoto authored on 2011-08-10
2051
$insert_param = $dbi->insert_param($param);
2052
$sql = <<"EOS";
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2053
insert into $table1 $insert_param
test cleanup
Yuki Kimoto authored on 2011-08-10
2054
EOS
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2055
$dbi->execute($sql, param => $param, table => $table1);
2056
is($dbi->select(table => $table1)->one->{$key1}, 1);
2057
is($dbi->select(table => $table1)->one->{$key2}, 2);
test cleanup
Yuki Kimoto authored on 2011-08-10
2058

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2074
test 'mycolumn';
2075
$dbi = MyDBI8->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2076
eval { $dbi->execute("drop table $table1") };
2077
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2078
$dbi->execute($create_table1);
2079
$dbi->execute($create_table2);
2080
$dbi->setup_model;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2081
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2082
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
2083
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2084
$result = $model->select_at(
2085
    column => [
2086
        $model->mycolumn,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2087
        $model->column($table2)
cleanup test
Yuki Kimoto authored on 2011-08-10
2088
    ]
2089
);
2090
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2091
          {$key1 => 1, $key2 => 2, "$table2.$key1" => 1, "$table2.$key3" => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2092

            
2093
$result = $model->select_at(
2094
    column => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2095
        $model->mycolumn([$key1]),
2096
        $model->column($table2 => [$key1])
cleanup test
Yuki Kimoto authored on 2011-08-10
2097
    ]
2098
);
2099
is_deeply($result->one,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2100
          {$key1 => 1, "$table2.$key1" => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
2101
$result = $model->select_at(
2102
    column => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2103
        $model->mycolumn([$key1]),
2104
        {$table2 => [$key1]}
cleanup test
Yuki Kimoto authored on 2011-08-10
2105
    ]
2106
);
2107
is_deeply($result->one,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2108
          {$key1 => 1, "$table2.$key1" => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
2109

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

            
2119
$result = $model->select_at(
2120
    column => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2121
        $model->mycolumn([$key1]),
2122
        ["$table2.$key1" => "$table2.$key1"]
cleanup test
Yuki Kimoto authored on 2011-08-10
2123
    ]
2124
);
2125
is_deeply($result->one,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2126
          {$key1 => 1, "$table2.$key1" => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
2127

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2128
test 'merge_param';
2129
$dbi = DBIx::Custom->new;
2130
$params = [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2131
    {$key1 => 1, $key2 => 2, $key3 => 3},
2132
    {$key1 => 1, $key2 => 2},
2133
    {$key1 => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2134
];
2135
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2136
is_deeply($param, {$key1 => [1, 1, 1], $key2 => [2, 2], $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2137

            
2138
$params = [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2139
    {$key1 => [1, 2], $key2 => 1, $key3 => [1, 2]},
2140
    {$key1 => [3, 4], $key2 => [2, 3], $key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
2141
];
2142
$param = $dbi->merge_param($params->[0], $params->[1]);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2143
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
2144

            
2145
test 'select() param option';
2146
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2147
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2148
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2149
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2150
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2151
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2152
$dbi->execute($create_table2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2153
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4});
2154
$dbi->insert(table => $table2, param => {$key1 => 2, $key3 => 5});
cleanup test
Yuki Kimoto authored on 2011-08-10
2155
$rows = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2156
    table => $table1,
2157
    column => "$table1.$key1 as ${table1}_$key1, $key2, $key3",
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2158
    where   => {"$table1.$key2" => 3},
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2159
    join  => ["inner join (select * from $table2 where {= $table2.$key3})" . 
test cleanup
Yuki Kimoto authored on 2011-08-15
2160
              " $table2 on $table1.$key1 = $table2.$key1"],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2161
    param => {"$table2.$key3" => 5}
cleanup test
Yuki Kimoto authored on 2011-08-10
2162
)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2163
is_deeply($rows, [{"${table1}_$key1" => 2, $key2 => 3, $key3 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-10
2164

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

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

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

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

            
2221

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

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

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2265
$dbi->delete_all(table => $table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2266
$dbi->insert(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2267
    primary_key => $key1, 
2268
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2269
    id => 0,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2270
    param => {$key2 => 2, $key3 => 3}
cleanup test
Yuki Kimoto authored on 2011-08-10
2271
);
2272

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

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

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

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

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2332
$dbi->delete_all(table => $table1);
2333
$dbi->insert(table => $table1, param => {$key1 => 0, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2334
$dbi->update(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2335
    table => $table1,
2336
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2337
    id => 0,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2338
    param => {$key3 => 4}
cleanup test
Yuki Kimoto authored on 2011-08-10
2339
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2340
is($dbi->select(table => $table1)->one->{$key1}, 0);
2341
is($dbi->select(table => $table1)->one->{$key2}, 2);
2342
is($dbi->select(table => $table1)->one->{$key3}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-10
2343

            
2344
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2345
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2346
$dbi->execute($create_table1_2);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2347
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2348
$dbi->update(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2349
    {$key3 => 4},
2350
    table => $table1,
2351
    primary_key => [$key1, $key2],
cleanup test
Yuki Kimoto authored on 2011-08-10
2352
    id => [1, 2]
2353
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2354
is($dbi->select(table => $table1)->one->{$key1}, 1);
2355
is($dbi->select(table => $table1)->one->{$key2}, 2);
2356
is($dbi->select(table => $table1)->one->{$key3}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-10
2357

            
2358

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

            
2374

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2387
$dbi->insert(table => $table1, param => {$key1 => 0, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2388
$dbi->delete(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2389
    table => $table1,
2390
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2391
    id => 0,
2392
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2393
is_deeply($dbi->select(table => $table1)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
2394

            
2395

            
2396
test 'model delete and id option';
2397
$dbi = MyDBI6->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2398
eval { $dbi->execute("drop table $table1") };
2399
eval { $dbi->execute("drop table $table2") };
2400
eval { $dbi->execute("drop table $table3") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2401
$dbi->execute($create_table1_2);
2402
$dbi->execute($create_table2_2);
2403
$dbi->execute($create_table3);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2404
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2405
$dbi->model($table1)->delete(id => [1, 2]);
2406
is_deeply($dbi->select(table => $table1)->all, []);
2407
$dbi->insert(table => $table2, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2408
$dbi->model($table1)->delete(id => [1, 2]);
2409
is_deeply($dbi->select(table => $table1)->all, []);
2410
$dbi->insert(table => $table3, param => {$key1 => 1, $key2 => 2, $key3 => 3});
2411
$dbi->model($table3)->delete(id => [1, 2]);
2412
is_deeply($dbi->select(table => $table3)->all, []);
cleanup test
Yuki Kimoto authored on 2011-08-10
2413

            
2414

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

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2430
$dbi->delete_all(table => $table1);
2431
$dbi->insert(table => $table1, param => {$key1 => 0, $key2 => 2, $key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2432
$result = $dbi->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2433
    table => $table1,
2434
    primary_key => $key1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2435
    id => 0,
2436
);
2437
$row = $result->one;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2438
is($row->{$key1}, 0);
2439
is($row->{$key2}, 2);
2440
is($row->{$key3}, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2441

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

            
2454

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

            
2466
test 'column separator is default .';
2467
$dbi = MyDBI7->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2468
eval { $dbi->execute("drop table $table1") };
2469
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2470
$dbi->execute($create_table1);
2471
$dbi->execute($create_table2);
2472
$dbi->setup_model;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2473
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2474
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
2475
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2476
$result = $model->select(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2477
    column => [$model->column($table2)],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2478
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2479
);
2480
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2481
          {"$table2.$key1" => 1, "$table2.$key3" => 3});
cleanup test
Yuki Kimoto authored on 2011-08-10
2482

            
2483
$result = $model->select(
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2484
    column => [$model->column($table2 => [$key1, $key3])],
2485
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2486
);
2487
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2488
          {"$table2.$key1" => 1, "$table2.$key3" => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2489

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2490
test 'separator';
2491
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2492
eval { $dbi->execute("drop table $table1") };
2493
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2494
$dbi->execute($create_table1);
2495
$dbi->execute($create_table2);
test cleanup
Yuki Kimoto authored on 2011-08-10
2496

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2497
$dbi->create_model(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2498
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2499
    join => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2500
       "left outer join $table2 on $table1.$key1 = $table2.$key1"
cleanup test
Yuki Kimoto authored on 2011-08-10
2501
    ],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2502
    primary_key => [$key1],
cleanup test
Yuki Kimoto authored on 2011-08-10
2503
);
2504
$model2 = $dbi->create_model(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2505
    table => $table2,
cleanup test
Yuki Kimoto authored on 2011-08-10
2506
);
2507
$dbi->setup_model;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2508
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2509
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
2510
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2511
$result = $model->select(
2512
    column => [
2513
        $model->mycolumn,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2514
        {$table2 => [$key1, $key3]}
cleanup test
Yuki Kimoto authored on 2011-08-10
2515
    ],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2516
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2517
);
2518
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2519
          {$key1 => 1, $key2 => 2, "$table2.$key1" => 1, "$table2.$key3" => 3});
2520
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2521

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2522
$dbi->separator('__');
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2523
$model = $dbi->model($table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
2524
$result = $model->select(
2525
    column => [
2526
        $model->mycolumn,
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2527
        {$table2 => [$key1, $key3]}
cleanup test
Yuki Kimoto authored on 2011-08-10
2528
    ],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2529
    where => {"$table1.$key1" => 1}
cleanup test
Yuki Kimoto authored on 2011-08-10
2530
);
2531
is_deeply($result->one,
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2532
          {$key1 => 1, $key2 => 2, "${table2}__$key1" => 1, "${table2}__$key3" => 3});
2533
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
2534

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

            
2548

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2549
test 'filter_off';
2550
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2551
eval { $dbi->execute("drop table $table1") };
2552
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2553
$dbi->execute($create_table1);
2554
$dbi->execute($create_table2);
test cleanup
Yuki Kimoto authored on 2011-08-10
2555

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2556
$dbi->create_model(
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2557
    table => $table1,
cleanup test
Yuki Kimoto authored on 2011-08-10
2558
    join => [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2559
       "left outer join $table2 on $table1.$key1 = $table2.$key1"
cleanup test
Yuki Kimoto authored on 2011-08-10
2560
    ],
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2561
    primary_key => [$key1],
cleanup test
Yuki Kimoto authored on 2011-08-10
2562
);
2563
$dbi->setup_model;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2564
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2565
$model = $dbi->model($table1);
2566
$result = $model->select(column => $key1);
2567
$result->filter($key1 => sub { $_[0] * 2 });
2568
is_deeply($result->one, {$key1 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-10
2569

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2570
test 'available_datetype';
2571
$dbi = DBIx::Custom->connect;
2572
ok($dbi->can('available_datatype'));
test cleanup
Yuki Kimoto authored on 2011-08-10
2573

            
2574

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2575
test 'select prefix option';
2576
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2577
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-10
2578
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2579
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2580
$rows = $dbi->select(prefix => "$key1,", column => $key2, table => $table1)->all;
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2581
is_deeply($rows, [{$key1 => 1, $key2 => 2}], "table");
test cleanup
Yuki Kimoto authored on 2011-08-10
2582

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2583
test 'map_param';
2584
$dbi = DBIx::Custom->connect;
2585
$param = $dbi->map_param(
2586
    {id => 1, author => 'Ken', price => 1900},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2587
    id => "$table1.id",
2588
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2589
    price => ["$table1.price", {if => sub { $_[0] eq 1900 }}]
cleanup test
Yuki Kimoto authored on 2011-08-10
2590
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2591
is_deeply($param, {"$table1.id" => 1, "$table1.author" => '%Ken%',
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2592
  "$table1.price" => 1900});
test cleanup
Yuki Kimoto authored on 2011-08-10
2593

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2594
$param = $dbi->map_param(
2595
    {id => 0, author => 0, price => 0},
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2596
    id => "$table1.id",
2597
    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2598
    price => ["$table1.price", sub { '%' . $_[0] . '%' },
cleanup test
Yuki Kimoto authored on 2011-08-10
2599
      {if => sub { $_[0] eq 0 }}]
2600
);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2601
is_deeply($param, {"$table1.id" => 0, "$table1.author" => '%0%', "$table1.price" => '%0%'});
test cleanup
Yuki Kimoto authored on 2011-08-10
2602

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

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

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2626
test 'order';
2627
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2628
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2629
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2630
$dbi->insert({$key1 => 1, $key2 => 1}, table => $table1);
2631
$dbi->insert({$key1 => 1, $key2 => 3}, table => $table1);
2632
$dbi->insert({$key1 => 2, $key2 => 2}, table => $table1);
2633
$dbi->insert({$key1 => 2, $key2 => 4}, table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2634
my $order = $dbi->order;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2635
$order->prepend($key1, "$key2 desc");
2636
$result = $dbi->select(table => $table1, append => $order);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2637
is_deeply($result->all, [{$key1 => 1, $key2 => 3}, {$key1 => 1, $key2 => 1},
2638
  {$key1 => 2, $key2 => 4}, {$key1 => 2, $key2 => 2}]);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2639
$order->prepend("$key1 desc");
2640
$result = $dbi->select(table => $table1, append => $order);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2641
is_deeply($result->all, [{$key1 => 2, $key2 => 4}, {$key1 => 2, $key2 => 2},
2642
  {$key1 => 1, $key2 => 3}, {$key1 => 1, $key2 => 1}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2643

            
2644
$order = $dbi->order;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2645
$order->prepend(["$table1-$key1"], ["$table1-$key2", 'desc']);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2646
$result = $dbi->select(table => $table1,
2647
  column => [[$key1 => "$table1-$key1"], [$key2 => "$table1-$key2"]],
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2648
  append => $order);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2649
is_deeply($result->all, [{"$table1-$key1" => 1, "$table1-$key2" => 3},
2650
  {"$table1-$key1" => 1, "$table1-$key2" => 1},
2651
  {"$table1-$key1" => 2, "$table1-$key2" => 4},
2652
  {"$table1-$key1" => 2, "$table1-$key2" => 2}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2653

            
2654
test 'tag_parse';
2655
$dbi = DBIx::Custom->connect;
2656
$dbi->tag_parse(0);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2657
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2658
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2659
$dbi->insert({$key1 => 1, $key2 => 1}, table => $table1);
2660
eval {$dbi->execute("select * from $table1 where {= $key1}", {$key1 => 1})};
test cleanup
Yuki Kimoto authored on 2011-08-10
2661
ok($@);
2662

            
2663
test 'last_sql';
2664
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2665
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2666
$dbi->execute($create_table1);
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2667
$dbi->execute("select * from $table1");
cleanup test
Yuki Kimoto authored on 2011-08-15
2668
is($dbi->last_sql, "select * from $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
2669

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

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

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

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

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

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

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

            
2710
test 'high perfomance way';
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2711
$dbi->execute("drop table $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
2712
$dbi->execute($create_table1_highperformance);
2713
$rows = [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2714
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
2715
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
2716
];
2717
{
2718
    my $query;
2719
    foreach my $row (@$rows) {
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2720
      $query ||= $dbi->insert($row, table => $table1, query => 1);
2721
      $dbi->execute($query, $row, filter => {$key7 => sub { $_[0] * 2 }});
test cleanup
Yuki Kimoto authored on 2011-08-10
2722
    }
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2723
    is_deeply($dbi->select(table => $table1)->all,
test cleanup
Yuki Kimoto authored on 2011-08-10
2724
      [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2725
          {$key7 => 2, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
2726
          {$key7 => 2, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
2727
      ]
2728
    );
2729
}
2730

            
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2731
$dbi->execute("drop table $table1");
test cleanup
Yuki Kimoto authored on 2011-08-10
2732
$dbi->execute($create_table1_highperformance);
2733
$rows = [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2734
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
2735
    {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
2736
];
2737
{
2738
    my $query;
2739
    my $sth;
2740
    foreach my $row (@$rows) {
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2741
      $query ||= $dbi->insert($row, table => $table1, query => 1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2742
      $sth ||= $query->sth;
2743
      $sth->execute(map { $row->{$_} } sort keys %$row);
2744
    }
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2745
    is_deeply($dbi->select(table => $table1)->all,
test cleanup
Yuki Kimoto authored on 2011-08-10
2746
      [
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2747
          {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7},
2748
          {$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8},
test cleanup
Yuki Kimoto authored on 2011-08-10
2749
      ]
2750
    );
2751
}
2752

            
2753
test 'result';
2754
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
2755
eval { $dbi->execute("drop table $table1") };
test cleanup
Yuki Kimoto authored on 2011-08-10
2756
$dbi->execute($create_table1);
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2757
$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1);
2758
$dbi->insert({$key1 => 3, $key2 => 4}, table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2759

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2760
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2761
@rows = ();
2762
while (my $row = $result->fetch) {
2763
    push @rows, [@$row];
2764
}
2765
is_deeply(\@rows, [[1, 2], [3, 4]]);
2766

            
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2767
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2768
@rows = ();
2769
while (my $row = $result->fetch_hash) {
2770
    push @rows, {%$row};
2771
}
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2772
is_deeply(\@rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
test cleanup
Yuki Kimoto authored on 2011-08-10
2773

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

            
2780
test 'fetch_all';
test cleanup in progress
Yuki Kimoto authored on 2011-08-15
2781
$result = $dbi->select(table => $table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2782
$rows = $result->fetch_all;
2783
is_deeply($rows, [[1, 2], [3, 4]]);
2784

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

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

            
2793
$rows = $result->fetch_all;
2794
is_deeply($rows, [[3, 2], [9, 4]], "array");
2795

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

            
2802
test "query_builder";
2803
$datas = [
2804
    # Basic tests
2805
    {   name            => 'placeholder basic',
2806
        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
cleanup test
Yuki Kimoto authored on 2011-08-15
2807
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?",
test cleanup
Yuki Kimoto authored on 2011-08-10
2808
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
2809
    },
2810
    {
2811
        name            => 'placeholder in',
cleanup test
Yuki Kimoto authored on 2011-08-15
2812
        source            => "{in k1 3}",
2813
        sql_expected    => "k1 in (?, ?, ?)",
test cleanup
Yuki Kimoto authored on 2011-08-10
2814
        columns_expected   => [qw/k1 k1 k1/]
2815
    },
2816
    
2817
    # Table name
2818
    {
2819
        name            => 'placeholder with table name',
2820
        source            => "{= a.k1} {= a.k2}",
cleanup test
Yuki Kimoto authored on 2011-08-15
2821
        sql_expected    => "a.k1 = ? a.k2 = ?",
test cleanup
Yuki Kimoto authored on 2011-08-10
2822
        columns_expected  => [qw/a.k1 a.k2/]
2823
    },
2824
    {   
2825
        name            => 'placeholder in with table name',
2826
        source            => "{in a.k1 2} {in b.k2 2}",
cleanup test
Yuki Kimoto authored on 2011-08-15
2827
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?)",
test cleanup
Yuki Kimoto authored on 2011-08-10
2828
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
2829
    },
2830
    {
2831
        name            => 'not contain tag',
2832
        source            => "aaa",
cleanup test
Yuki Kimoto authored on 2011-08-15
2833
        sql_expected    => "aaa",
test cleanup
Yuki Kimoto authored on 2011-08-10
2834
        columns_expected  => [],
2835
    }
2836
];
2837

            
2838
for (my $i = 0; $i < @$datas; $i++) {
2839
    my $data = $datas->[$i];
cleanup
Yuki Kimoto authored on 2011-08-13
2840
    my $dbi = DBIx::Custom->new;
2841
    my $builder = $dbi->query_builder;
test cleanup
Yuki Kimoto authored on 2011-08-10
2842
    my $query = $builder->build_query($data->{source});
2843
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
2844
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
2845
}
2846

            
cleanup
Yuki Kimoto authored on 2011-08-13
2847
$dbi = DBIx::Custom->new;
2848
$builder = $dbi->query_builder;
2849
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
2850
    p => sub {
2851
        my @args = @_;
2852
        
2853
        my $expand    = "? $args[0] $args[1]";
2854
        my $columns = [2];
2855
        return [$expand, $columns];
2856
    }
2857
);
2858

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
2869
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
2870
    q => 'string'
2871
});
2872

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
2876
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
2877
   r => sub {} 
2878
});
2879

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
2883
$dbi->register_tag({
test cleanup
Yuki Kimoto authored on 2011-08-10
2884
   s => sub { return ["a", ""]} 
2885
});
2886

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

            
cleanup
Yuki Kimoto authored on 2011-08-13
2890
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
2891
    t => sub {return ["a", []]}
2892
);
2893

            
2894

            
cleanup
Yuki Kimoto authored on 2011-08-13
2895
$dbi->register_tag(
test cleanup
Yuki Kimoto authored on 2011-08-10
2896
    a => sub {
2897
        return ["? ? ?", ['']];
2898
    }
2899
);
2900
eval{$builder->build_query("{a}")};
2901
like($@, qr/\QPlaceholder count/, "placeholder count is invalid");
2902

            
2903

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

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

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

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

            
2919
test 'variouse source';
2920
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d";
2921
$query = $builder->build_query($source);
2922
is($query->sql, 'a b = ? c { } { = ? } = ? d', "basic : 1");
2923

            
2924
$source = "abc";
2925
$query = $builder->build_query($source);
2926
is($query->sql, 'abc', "basic : 2");
2927

            
2928
$source = "{= a}";
2929
$query = $builder->build_query($source);
2930
is($query->sql, 'a = ?', "only tag");
2931

            
2932
$source = "000";
2933
$query = $builder->build_query($source);
2934
is($query->sql, '000', "contain 0 value");
2935

            
2936
$source = "a {= b} }";
2937
eval{$builder->build_query($source)};
2938
like($@, qr/unexpected "}"/, "error : 1");
2939

            
2940
$source = "a {= {}";
2941
eval{$builder->build_query($source)};
2942
like($@, qr/unexpected "{"/, "error : 2");
2943

            
2944
test 'select() wrap option';
2945
$dbi = DBIx::Custom->connect;
2946
eval { $dbi->execute("drop table $table1") };
2947
$dbi->execute($create_table1);
2948
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2949
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
2950
$rows = $dbi->select(
2951
    table => $table1,
2952
    column => $key1,
test cleanup
Yuki Kimoto authored on 2011-08-15
2953
    wrap => ["select * from (", ") t where $key1 = 1"]
cleanup test
Yuki Kimoto authored on 2011-08-15
2954
)->all;
2955
is_deeply($rows, [{$key1 => 1}]);
2956

            
2957
eval {
2958
$dbi->select(
2959
    table => $table1,
2960
    column => $key1,
2961
    wrap => 'select * from ('
2962
)
2963
};
2964
like($@, qr/array/);
2965

            
2966
test 'select() sqlfilter option';
2967
$dbi = DBIx::Custom->connect;
2968
eval { $dbi->execute("drop table $table1") };
2969
$dbi->execute($create_table1);
2970
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2971
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3});
2972
$rows = $dbi->select(
2973
    table => $table1,
2974
    column => $key1,
2975
    sqlfilter => sub {
2976
        my $sql = shift;
test cleanup
Yuki Kimoto authored on 2011-08-15
2977
        $sql = "select * from ( $sql ) t where $key1 = 1";
cleanup test
Yuki Kimoto authored on 2011-08-15
2978
        return $sql;
2979
    }
2980
)->all;
2981
is_deeply($rows, [{$key1 => 1}]);
2982

            
2983
test 'dbi method from model';
2984
$dbi = MyDBI9->connect;
2985
eval { $dbi->execute("drop table $table1") };
2986
$dbi->execute($create_table1);
2987
$dbi->setup_model;
2988
$model = $dbi->model($table1);
2989
eval{$model->execute("select * from $table1")};
2990
ok(!$@);
2991

            
2992
test 'column table option';
2993
$dbi = MyDBI9->connect;
2994
eval { $dbi->execute("drop table $table1") };
2995
$dbi->execute($create_table1);
2996
eval { $dbi->execute("drop table $table2") };
2997
$dbi->execute($create_table2);
2998
$dbi->setup_model;
2999
$dbi->execute("insert into $table1 ($key1, $key2) values (1, 2)");
3000
$dbi->execute("insert into $table2 ($key1, $key3) values (1, 4)");
3001
$model = $dbi->model($table1);
3002
$result = $model->select(
3003
    column => [
3004
        $model->column($table2, {alias => $table2_alias})
3005
    ],
3006
    where => {"$table2_alias.$key3" => 4}
3007
);
3008
is_deeply($result->one, 
3009
          {"$table2_alias.$key1" => 1, "$table2_alias.$key3" => 4});
3010

            
3011
$dbi->separator('__');
3012
$result = $model->select(
3013
    column => [
3014
        $model->column($table2, {alias => $table2_alias})
3015
    ],
3016
    where => {"$table2_alias.$key3" => 4}
3017
);
3018
is_deeply($result->one, 
3019
          {"${table2_alias}__$key1" => 1, "${table2_alias}__$key3" => 4});
3020

            
3021
$dbi->separator('-');
3022
$result = $model->select(
3023
    column => [
3024
        $model->column($table2, {alias => $table2_alias})
3025
    ],
3026
    where => {"$table2_alias.$key3" => 4}
3027
);
3028
is_deeply($result->one, 
3029
          {"$table2_alias-$key1" => 1, "$table2_alias-$key3" => 4});
3030

            
3031
test 'create_model';
3032
$dbi = DBIx::Custom->connect;
3033
eval { $dbi->execute("drop table $table1") };
3034
eval { $dbi->execute("drop table $table2") };
3035
$dbi->execute($create_table1);
3036
$dbi->execute($create_table2);
3037

            
3038
$dbi->create_model(
3039
    table => $table1,
3040
    join => [
3041
       "left outer join $table2 on $table1.$key1 = $table2.$key1"
3042
    ],
3043
    primary_key => [$key1]
3044
);
3045
$model2 = $dbi->create_model(
3046
    table => $table2
3047
);
3048
$dbi->create_model(
3049
    table => $table3,
3050
    filter => [
3051
        $key1 => {in => sub { uc $_[0] }}
3052
    ]
3053
);
3054
$dbi->setup_model;
3055
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3056
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
3057
$model = $dbi->model($table1);
3058
$result = $model->select(
3059
    column => [$model->mycolumn, $model->column($table2)],
3060
    where => {"$table1.$key1" => 1}
3061
);
3062
is_deeply($result->one,
3063
          {$key1 => 1, $key2 => 2, "$table2.$key1" => 1, "$table2.$key3" => 3});
3064
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3});
3065

            
3066
test 'model method';
3067
$dbi = DBIx::Custom->connect;
3068
eval { $dbi->execute("drop table $table2") };
3069
$dbi->execute($create_table2);
3070
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3});
3071
$model = $dbi->create_model(
3072
    table => $table2
3073
);
3074
$model->method(foo => sub { shift->select(@_) });
3075
is_deeply($model->foo->one, {$key1 => 1, $key3 => 3});
3076

            
3077
test 'update_param';
3078
$dbi = DBIx::Custom->connect;
3079
eval { $dbi->execute("drop table $table1") };
3080
$dbi->execute($create_table1_2);
3081
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3082
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
3083

            
3084
$param = {$key2 => 11};
3085
$update_param = $dbi->update_param($param);
3086
$sql = <<"EOS";
3087
update $table1 $update_param
3088
where $key1 = 1
3089
EOS
3090
$dbi->execute($sql, param => $param);
3091
$result = $dbi->execute("select * from $table1 order by $key1", table => $table1);
3092
$rows   = $result->all;
3093
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
3094
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3095
                  "basic");
3096

            
3097

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

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

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

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

            
3136
            
3137
eval { $dbi->update_param({";" => 1}) };
3138
like($@, qr/not safety/);
3139

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3141
test 'update_param';
3142
$dbi = DBIx::Custom->connect;
3143
eval { $dbi->execute("drop table $table1") };
3144
$dbi->execute($create_table1_2);
3145
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
3146
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
test cleanup
Yuki Kimoto authored on 2011-08-10
3147

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3148
$param = {$key2 => 11};
3149
$update_param = $dbi->assign_param($param);
3150
$sql = <<"EOS";
3151
update $table1 set $update_param
3152
where $key1 = 1
3153
EOS
3154
$dbi->execute($sql, param => $param, table => $table1);
3155
$result = $dbi->execute("select * from $table1 order by $key1");
3156
$rows   = $result->all;
3157
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
3158
                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
3159
                  "basic");
test cleanup
Yuki Kimoto authored on 2011-08-10
3160

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3161
test 'Model class';
3162
$dbi = MyDBI1->connect;
3163
eval { $dbi->execute("drop table $table1") };
3164
$dbi->execute($create_table1);
3165
$model = $dbi->model($table1);
3166
$model->insert({$key1 => 'a', $key2 => 'b'});
3167
is_deeply($model->list->all, [{$key1 => 'a', $key2 => 'b'}], 'basic');
3168
eval { $dbi->execute("drop table $table2") };
3169
$dbi->execute($create_table2);
3170
$model = $dbi->model($table2);
3171
$model->insert({$key1 => 'a'});
3172
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'basic');
3173
is($dbi->models->{$table1}, $dbi->model($table1));
3174
is($dbi->models->{$table2}, $dbi->model($table2));
3175

            
3176
$dbi = MyDBI4->connect;
3177
eval { $dbi->execute("drop table $table1") };
3178
$dbi->execute($create_table1);
3179
$model = $dbi->model($table1);
3180
$model->insert({$key1 => 'a', $key2 => 'b'});
3181
is_deeply($model->list->all, [{$key1 => 'a', $key2 => 'b'}], 'basic');
3182
eval { $dbi->execute("drop table $table2") };
3183
$dbi->execute($create_table2);
3184
$model = $dbi->model($table2);
3185
$model->insert({$key1 => 'a'});
3186
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'basic');
3187

            
3188
$dbi = MyDBI5->connect;
3189
eval { $dbi->execute("drop table $table1") };
3190
eval { $dbi->execute("drop table $table2") };
3191
$dbi->execute($create_table1);
3192
$dbi->execute($create_table2);
3193
$model = $dbi->model($table2);
3194
$model->insert({$key1 => 'a'});
3195
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'include all model');
3196
$dbi->insert(table => $table1, param => {$key1 => 1});
3197
$model = $dbi->model($table1);
3198
is_deeply($model->list->all, [{$key1 => 1, $key2 => undef}], 'include all model');
3199

            
3200
test 'primary_key';
3201
$dbi = MyDBI1->connect;
3202
$model = $dbi->model($table1);
3203
$model->primary_key([$key1, $key2]);
3204
is_deeply($model->primary_key, [$key1, $key2]);
3205

            
3206
test 'columns';
3207
$dbi = MyDBI1->connect;
3208
$model = $dbi->model($table1);
3209
$model->columns([$key1, $key2]);
3210
is_deeply($model->columns, [$key1, $key2]);
cleanup test
Yuki Kimoto authored on 2011-08-10
3211

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3212
test 'setup_model';
3213
$dbi = MyDBI1->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3214
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3215
eval { $dbi->execute("drop table $table2") };
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
3216

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3217
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-15
3218
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-10
3219
$dbi->setup_model;
cleanup test
Yuki Kimoto authored on 2011-08-15
3220
is_deeply([sort @{$dbi->model($table1)->columns}], [$key1, $key2]);
3221
is_deeply([sort @{$dbi->model($table2)->columns}], [$key1, $key3]);
cleanup test
Yuki Kimoto authored on 2011-08-10
3222

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3223
test 'each_column';
3224
$dbi = DBIx::Custom->connect;
3225
eval { $dbi->execute("drop table ${q}table$p") };
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3226
eval { $dbi->execute("drop table $table1") };
3227
eval { $dbi->execute("drop table $table2") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3228
eval { $dbi->execute("drop table $table3") };
3229
$dbi->execute($create_table1_type);
cleanup test
Yuki Kimoto authored on 2011-08-10
3230
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-15
3231

            
3232
$infos = [];
3233
$dbi->each_column(sub {
3234
    my ($self, $table, $column, $cinfo) = @_;
3235
    
3236
    if ($table =~ /^table\d/i) {
3237
         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
3238
         push @$infos, $info;
3239
    }
3240
});
3241
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3242
is_deeply($infos, 
3243
    [
3244
        [$table1, $key1, $key1],
3245
        [$table1, $key2, $key2],
3246
        [$table2, $key1, $key1],
3247
        [$table2, $key3, $key3]
3248
    ]
3249
    
cleanup test
Yuki Kimoto authored on 2011-08-10
3250
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3251
test 'each_table';
3252
$dbi = DBIx::Custom->connect;
3253
eval { $dbi->execute("drop table $table1") };
3254
eval { $dbi->execute("drop table $table2") };
3255
$dbi->execute($create_table2);
3256
$dbi->execute($create_table1_type);
cleanup test
Yuki Kimoto authored on 2011-08-10
3257

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3258
$infos = [];
3259
$dbi->each_table(sub {
3260
    my ($self, $table, $table_info) = @_;
3261
    
3262
    if ($table =~ /^table\d/i) {
3263
         my $info = [$table, $table_info->{TABLE_NAME}];
3264
         push @$infos, $info;
3265
    }
3266
});
3267
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
3268
is_deeply($infos, 
3269
    [
3270
        [$table1, $table1],
3271
        [$table2, $table2],
3272
    ]
cleanup test
Yuki Kimoto authored on 2011-08-10
3273
);
3274

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3275
test 'type_rule into';
3276
eval { $dbi->execute("drop table $table1") };
3277
$dbi->execute($create_table1_type);
3278
$dbi = DBIx::Custom->connect;
3279
eval { $dbi->execute("drop table $table1") };
3280
$dbi->execute($create_table1_type);
3281

            
3282
$dbi->type_rule(
3283
    into1 => {
3284
        $date_typename => sub { '2010-' . $_[0] }
3285
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3286
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3287
$dbi->insert({$key1 => '01-01'}, table => $table1);
3288
$result = $dbi->select(table => $table1);
3289
like($result->one->{$key1}, qr/^2010-01-01/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3290

            
3291
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3292
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3293
$dbi->execute($create_table1_type);
3294
$dbi->type_rule(
3295
    into1 => [
3296
         [$date_typename, $datetime_typename] => sub {
3297
            my $value = shift;
3298
            $value =~ s/02/03/g;
3299
            return $value;
3300
         }
3301
    ]
3302
);
3303
$dbi->insert({$key1 => '2010-01-02', $key2 => '2010-01-01 01:01:02'}, table => $table1);
3304
$result = $dbi->select(table => $table1);
3305
$row = $result->one;
3306
like($row->{$key1}, qr/^2010-01-03/);
3307
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3308

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3309
$dbi = DBIx::Custom->connect;
3310
eval { $dbi->execute("drop table $table1") };
3311
$dbi->execute($create_table1_type);
3312
$dbi->insert({$key1 => '2010-01-03', $key2 => '2010-01-01 01:01:03'}, table => $table1);
3313
$dbi->type_rule(
3314
    into1 => [
3315
        [$date_typename, $datetime_typename] => sub {
3316
            my $value = shift;
3317
            $value =~ s/02/03/g;
3318
            return $value;
3319
        }
3320
    ]
cleanup test
Yuki Kimoto authored on 2011-08-10
3321
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3322
$result = $dbi->execute(
3323
    "select * from $table1 where $key1 = :$key1 and $key2 = :$table1.$key2",
3324
    param => {$key1 => '2010-01-03', "$table1.$key2" => '2010-01-01 01:01:02'}
cleanup test
Yuki Kimoto authored on 2011-08-10
3325
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3326
$row = $result->one;
3327
like($row->{$key1}, qr/^2010-01-03/);
3328
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
3329

            
3330
$dbi = DBIx::Custom->connect;
3331
eval { $dbi->execute("drop table $table1") };
3332
$dbi->execute($create_table1_type);
3333
$dbi->insert({$key1 => '2010-01-03', $key2 => '2010-01-01 01:01:03'}, table => $table1);
3334
$dbi->type_rule(
3335
    into1 => [
3336
        [$date_typename, $datetime_typename] => sub {
3337
            my $value = shift;
3338
            $value =~ s/02/03/g;
3339
            return $value;
3340
        }
cleanup test
Yuki Kimoto authored on 2011-08-10
3341
    ]
3342
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3343
$result = $dbi->execute(
3344
    "select * from $table1 where $key1 = :$key1 and $key2 = :$table1.$key2",
3345
    param => {$key1 => '2010-01-02', "$table1.$key2" => '2010-01-01 01:01:02'},
3346
    table => $table1
3347
);
3348
$row = $result->one;
3349
like($row->{$key1}, qr/^2010-01-03/);
3350
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3351

            
3352
$dbi = DBIx::Custom->connect;
3353
eval { $dbi->execute("drop table $table1") };
3354
$dbi->execute($create_table1_type);
3355
$dbi->register_filter(convert => sub {
3356
    my $value = shift || '';
3357
    $value =~ s/02/03/;
3358
    return $value;
3359
});
3360
$dbi->type_rule(
3361
    from1 => {
3362
        $date_datatype => 'convert',
3363
    },
3364
    into1 => {
3365
        $date_typename => 'convert',
3366
    }
3367
);
3368
$dbi->insert({$key1 => '2010-02-02'}, table => $table1);
3369
$result = $dbi->select(table => $table1);
3370
like($result->fetch->[0], qr/^2010-03-03/);
3371

            
3372
test 'type_rule and filter order';
3373
$dbi = DBIx::Custom->connect;
3374
eval { $dbi->execute("drop table $table1") };
3375
$dbi->execute($create_table1_type);
3376
$dbi->type_rule(
3377
    into1 => {
3378
        $date_typename => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3379
    },
3380
    into2 => {
3381
        $date_typename => sub { my $v = shift || ''; $v =~ s/5/6/; return $v }
3382
    },
3383
    from1 => {
3384
        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/7/; return $v }
3385
    },
3386
    from2 => {
3387
        $date_datatype => sub { my $v = shift || ''; $v =~ s/7/8/; return $v }
3388
    }
3389
);
3390
$dbi->insert({$key1 => '2010-01-03'}, 
3391
  table => $table1, filter => {$key1 => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }});
3392
$result = $dbi->select(table => $table1);
3393
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/8/9/; return $v });
3394
like($result->fetch_first->[0], qr/^2010-01-09/);
3395

            
3396

            
3397
$dbi = DBIx::Custom->connect;
3398
eval { $dbi->execute("drop table $table1") };
3399
$dbi->execute($create_table1_type);
3400
$dbi->type_rule(
3401
    from1 => {
3402
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3403
    },
3404
    from2 => {
3405
        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3406
    },
3407
);
3408
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3409
$result = $dbi->select(table => $table1);
3410
$result->type_rule(
3411
    from1 => {
3412
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/6/; return $v }
3413
    },
3414
    from2 => {
3415
        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/8/; return $v }
3416
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3417
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3418
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/8/9/; return $v });
3419
like($result->fetch_first->[0], qr/^2010-01-09/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3420

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3421
test 'type_rule_off';
cleanup test
Yuki Kimoto authored on 2011-08-10
3422
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-15
3423
eval { $dbi->execute("drop table $table1") };
3424
$dbi->execute($create_table1_type);
3425
$dbi->type_rule(
3426
    from1 => {
3427
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3428
    },
3429
    into1 => {
3430
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3431
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3432
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3433
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule_off => 1);
3434
$result = $dbi->select(table => $table1, type_rule_off => 1);
3435
like($result->type_rule_off->fetch->[0], qr/^2010-01-03/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3436

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3437
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3438
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3439
$dbi->execute($create_table1_type);
3440
$dbi->type_rule(
3441
    from1 => {
3442
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3443
    },
3444
    into1 => {
3445
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3446
    }
3447
);
3448
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule_off => 1);
3449
$result = $dbi->select(table => $table1, type_rule_off => 1);
3450
like($result->one->{$key1}, qr/^2010-01-04/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3451

            
3452
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3453
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3454
$dbi->execute($create_table1_type);
3455
$dbi->type_rule(
3456
    from1 => {
3457
        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3458
    },
3459
    into1 => {
3460
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3461
    }
3462
);
3463
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3464
$result = $dbi->select(table => $table1);
3465
like($result->one->{$key1}, qr/^2010-01-05/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3466

            
3467
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3468
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3469
$dbi->execute($create_table1_type);
3470
$dbi->type_rule(
3471
    from1 => {
3472
        $date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }
3473
    },
3474
    into1 => {
3475
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3476
    }
3477
);
3478
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3479
$result = $dbi->select(table => $table1);
3480
like($result->fetch->[0], qr/2010-01-05/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3481

            
3482
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3483
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3484
$dbi->execute($create_table1_type);
3485
$dbi->register_filter(ppp => sub { my $v = shift || ''; $v =~ s/3/4/; return $v });
3486
$dbi->type_rule(
3487
    into1 => {
3488
        $date_typename => 'ppp'
3489
    }
3490
);
3491
$dbi->insert({$key1 => '2010-01-03'}, table => $table1);
3492
$result = $dbi->select(table => $table1);
3493
like($result->one->{$key1}, qr/^2010-01-04/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3494

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3495
eval{$dbi->type_rule(
3496
    into1 => {
3497
        $date_typename => 'pp'
3498
    }
3499
)};
3500
like($@, qr/not registered/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3501

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3502
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3503
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3504
$dbi->execute($create_table1_type);
cleanup test
Yuki Kimoto authored on 2011-08-10
3505
eval {
cleanup test
Yuki Kimoto authored on 2011-08-15
3506
    $dbi->type_rule(
3507
        from1 => {
3508
            Date => sub { $_[0] * 2 },
3509
        }
cleanup test
Yuki Kimoto authored on 2011-08-10
3510
    );
3511
};
cleanup test
Yuki Kimoto authored on 2011-08-15
3512
like($@, qr/lower/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3513

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3514
eval {
3515
    $dbi->type_rule(
3516
        into1 => {
3517
            Date => sub { $_[0] * 2 },
3518
        }
3519
    );
3520
};
3521
like($@, qr/lower/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3522

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

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

            
3559
$result = $dbi->select(table => $table1);
3560
$result->type_rule(
3561
    from1 => {
3562
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3563
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3564
);
cleanup test
Yuki Kimoto authored on 2011-08-15
3565
$row = $result->one;
3566
like($row->{$key1}, qr/2010-01-05/);
3567
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3568

            
3569
$result = $dbi->select(table => $table1);
3570
$result->type_rule(
3571
    from1 => {
3572
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3573
    }
3574
);
3575
$row = $result->one;
3576
like($row->{$key1}, qr/2010-01-05/);
3577
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3578

            
3579
$result = $dbi->select(table => $table1);
3580
$result->type_rule(
3581
    from1 => [$date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }]
3582
);
3583
$row = $result->one;
3584
like($row->{$key1}, qr/2010-01-05/);
3585
like($row->{$key2}, qr/2010-01-01 01:01:03/);
3586

            
3587
$dbi->register_filter(five => sub { my $v = shift || ''; $v =~ s/3/5/; return $v });
3588
$result = $dbi->select(table => $table1);
3589
$result->type_rule(
3590
    from1 => [$date_datatype => 'five']
3591
);
3592
$row = $result->one;
3593
like($row->{$key1}, qr/^2010-01-05/);
3594
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
3595

            
3596
$result = $dbi->select(table => $table1);
3597
$result->type_rule(
3598
    from1 => [$date_datatype => undef]
3599
);
3600
$row = $result->one;
3601
like($row->{$key1}, qr/^2010-01-03/);
3602
like($row->{$key2}, qr/^2010-01-01 01:01:03/);
cleanup test
Yuki Kimoto authored on 2011-08-10
3603

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3630
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3631
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3632
$dbi->execute($create_table1_type);
3633
$dbi->type_rule(
3634
    into1 => {
3635
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3636
    },
3637
    into2 => {
3638
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3639
    },
3640
    from1 => {
3641
        $date_datatype => sub { my $v = shift || ''; $v =~ s/3/6/; return $v }
3642
    },
3643
    from2 => {
3644
        $date_datatype => sub { my $v = shift || ''; $v =~ s/(3|6)/7/; return $v }
3645
    }
3646
);
3647
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule_off => 1);
3648
$result = $dbi->select(table => $table1);
3649
like($result->type_rule_off->fetch_first->[0], qr/^2010-01-03/);
3650
$result = $dbi->select(table => $table1);
3651
like($result->type_rule_on->fetch_first->[0], qr/^2010-01-07/);
test cleanup
Yuki Kimoto authored on 2011-08-10
3652

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3653
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3654
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3655
$dbi->execute($create_table1_type);
3656
$dbi->type_rule(
3657
    into1 => {
3658
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3659
    },
3660
    into2 => {
3661
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3662
    },
3663
    from1 => {
3664
        $date_datatype => sub { my $v = shift || ''; $v =~ s/(3|5)/6/; return $v }
3665
    },
3666
    from2 => {
3667
        $date_datatype => sub { my $v = shift || ''; $v =~ s/6/7/; return $v }
3668
    }
3669
);
3670
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule1_off => 1);
3671
$result = $dbi->select(table => $table1);
3672
like($result->type_rule1_off->fetch_first->[0], qr/^2010-01-05/);
3673
$result = $dbi->select(table => $table1);
3674
like($result->type_rule1_on->fetch_first->[0], qr/^2010-01-07/);
test cleanup
Yuki Kimoto authored on 2011-08-10
3675

            
cleanup test
Yuki Kimoto authored on 2011-08-15
3676
$dbi = DBIx::Custom->connect;
finishied sqlite test cleanu...
Yuki Kimoto authored on 2011-08-15
3677
eval { $dbi->execute("drop table $table1") };
cleanup test
Yuki Kimoto authored on 2011-08-15
3678
$dbi->execute($create_table1_type);
3679
$dbi->type_rule(
3680
    into1 => {
3681
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }
3682
    },
3683
    into2 => {
3684
        $date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }
3685
    },
3686
    from1 => {
3687
        $date_datatype => sub { my $v = shift || ''; $v =~ s/5/6/; return $v }
3688
    },
3689
    from2 => {
3690
        $date_datatype => sub { my $v = shift || ''; $v =~ s/(3|6)/7/; return $v }
3691
    }
3692
);
3693
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule2_off => 1);
3694
$result = $dbi->select(table => $table1);
3695
like($result->type_rule2_off->fetch_first->[0], qr/^2010-01-06/);
3696
$result = $dbi->select(table => $table1);
3697
like($result->type_rule2_on->fetch_first->[0], qr/^2010-01-07/);
test cleanup
Yuki Kimoto authored on 2011-08-10
3698

            
added memory leak check test
Yuki Kimoto authored on 2011-08-15
3699
test 'join';
3700
$dbi = DBIx::Custom->connect;
3701
eval { $dbi->execute("drop table $table1") };
3702
$dbi->execute($create_table1);
3703
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3704
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
3705
eval { $dbi->execute("drop table $table2") };
3706
$dbi->execute($create_table2);
3707
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3708
eval { $dbi->execute("drop table $table3") };
3709
$dbi->execute("create table $table3 ($key3 int, $key4 int)");
3710
$dbi->insert(table => $table3, param => {$key3 => 5, $key4 => 4});
3711
$rows = $dbi->select(
3712
    table => $table1,
3713
    column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
3714
    where   => {"$table1.$key2" => 2},
3715
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1"]
3716
)->all;
3717
is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}]);
3718

            
3719
$rows = $dbi->select(
3720
    table => $table1,
3721
    where   => {$key1 => 1},
3722
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1"]
3723
)->all;
3724
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
3725

            
3726
eval {
3727
    $rows = $dbi->select(
3728
        table => $table1,
3729
        column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3",
3730
        where   => {"$table1.$key2" => 2},
3731
        join  => {"$table1.$key1" => "$table2.$key1"}
3732
    );
3733
};
3734
like ($@, qr/array/);
3735

            
3736
$rows = $dbi->select(
3737
    table => $table1,
3738
    where   => {$key1 => 1},
3739
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
3740
              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
3741
)->all;
3742
is_deeply($rows, [{$key1 => 1, $key2 => 2}]);
3743

            
3744
$rows = $dbi->select(
3745
    column => "$table3.$key4 as ${table3}__$key4",
3746
    table => $table1,
3747
    where   => {"$table1.$key1" => 1},
3748
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
3749
              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
3750
)->all;
3751
is_deeply($rows, [{"${table3}__$key4" => 4}]);
3752

            
3753
$rows = $dbi->select(
3754
    column => "$table1.$key1 as ${table1}__$key1",
3755
    table => $table1,
3756
    where   => {"$table3.$key4" => 4},
3757
    join  => ["left outer join $table2 on $table1.$key1 = $table2.$key1",
3758
              "left outer join $table3 on $table2.$key3 = $table3.$key3"]
3759
)->all;
3760
is_deeply($rows, [{"${table1}__$key1" => 1}]);
3761

            
3762
$dbi = DBIx::Custom->connect;
3763
eval { $dbi->execute("drop table $table1") };
3764
$dbi->execute($create_table1);
3765
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3766
eval { $dbi->execute("drop table $table2") };
3767
$dbi->execute($create_table2);
3768
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3769
$rows = $dbi->select(
3770
    table => $table1,
3771
    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",
3772
    where   => {"$table1.$key2" => 2},
3773
    join  => ["left outer join ${q}$table2$p on ${q}$table1$p.${q}$key1$p = ${q}$table2$p.${q}$key1$p"],
3774
)->all;
3775
is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}],
3776
          'quote');
3777

            
3778

            
3779
$dbi = DBIx::Custom->connect;
3780
eval { $dbi->execute("drop table $table1") };
3781
$dbi->execute($create_table1);
3782
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3783
$sql = <<"EOS";
3784
left outer join (
3785
  select * from $table1 t1
3786
  where t1.$key2 = (
3787
    select max(t2.$key2) from $table1 t2
3788
    where t1.$key1 = t2.$key1
3789
  )
3790
) $table3 on $table1.$key1 = $table3.$key1
3791
EOS
3792
$join = [$sql];
3793
$rows = $dbi->select(
3794
    table => $table1,
3795
    column => "$table3.$key1 as ${table3}__$key1",
3796
    join  => $join
3797
)->all;
3798
is_deeply($rows, [{"${table3}__$key1" => 1}]);
3799

            
3800
$dbi = DBIx::Custom->connect;
3801
eval { $dbi->execute("drop table $table1") };
3802
eval { $dbi->execute("drop table $table2") };
3803
$dbi->execute($create_table1);
3804
$dbi->execute($create_table2);
3805
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3806
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4});
3807
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3808
$result = $dbi->select(
3809
    table => $table1,
3810
    join => [
3811
        "left outer join $table2 on $table2.$key2 = '4' and $table1.$key1 = $table2.$key1"
3812
    ]
3813
);
3814
is_deeply($result->all, [{$key1 => 1, $key2 => 2}]);
3815
$result = $dbi->select(
3816
    table => $table1,
3817
    column => [{$table2 => [$key3]}],
3818
    join => [
3819
        "left outer join $table2 on $table2.$key3 = '4' and $table1.$key1 = $table2.$key1"
3820
    ]
3821
);
3822
is_deeply($result->all, [{"$table2.$key3" => 4}]);
3823
$result = $dbi->select(
3824
    table => $table1,
3825
    column => [{$table2 => [$key3]}],
3826
    join => [
3827
        "left outer join $table2 on $table1.$key1 = $table2.$key1 and $table2.$key3 = '4'"
3828
    ]
3829
);
3830
is_deeply($result->all, [{"$table2.$key3" => 4}]);
3831

            
3832
$dbi = DBIx::Custom->connect;
3833
eval { $dbi->execute("drop table $table1") };
3834
eval { $dbi->execute("drop table $table2") };
3835
$dbi->execute($create_table1);
3836
$dbi->execute($create_table2);
3837
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3838
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4});
3839
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5});
3840
$result = $dbi->select(
3841
    table => $table1,
3842
    column => [{$table2 => [$key3]}],
3843
    join => [
3844
        {
3845
            clause => "left outer join $table2 on $table2.$key3 = '4' and $table1.$key1 = $table2.$key1",
3846
            table => [$table1, $table2]
3847
        }
3848
    ]
3849
);
3850
is_deeply($result->all, [{"$table2.$key3" => 4}]);
3851

            
3852
test 'columns';
3853
$dbi = MyDBI1->connect;
3854
$model = $dbi->model($table1);
3855

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

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