DBIx-Custom / t / common.t /
Newer Older
1781 lines | 61.301kb
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 DBIx::Custom;
5
use Encode qw/encode_utf8/;
cleanup test
Yuki Kimoto authored on 2011-08-10
6
use FindBin;
7
use lib "$FindBin::Bin/common";
8

            
added common test executing ...
Yuki Kimoto authored on 2011-08-07
9

            
10
my $dbi;
11

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

            
16
plan 'no_plan';
17

            
cleanup test
Yuki Kimoto authored on 2011-08-08
18
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/};
19
sub test { print "# $_[0]\n" }
20

            
added common test executing ...
Yuki Kimoto authored on 2011-08-07
21
# Constant
22
my $create_table1 = $dbi->create_table1;
cleanup test
Yuki Kimoto authored on 2011-08-08
23
my $create_table1_2 = $dbi->create_table1_2;
test cleanup
Yuki Kimoto authored on 2011-08-10
24
my $create_table1_type = $dbi->create_table1_type;
test cleanup
Yuki Kimoto authored on 2011-08-10
25
my $create_table2 = $dbi->create_table2;
26
my $create_table_reserved = $dbi->create_table_reserved;
cleanup test
Yuki Kimoto authored on 2011-08-10
27
my $q = substr($dbi->quote, 0, 1);
28
my $p = substr($dbi->quote, 1, 1) || $q;
added common test executing ...
Yuki Kimoto authored on 2011-08-07
29

            
30
# Variable
cleanup test
Yuki Kimoto authored on 2011-08-08
31
# Variables
32
my $builder;
33
my $datas;
34
my $sth;
35
my $source;
36
my @sources;
37
my $select_source;
38
my $insert_source;
39
my $update_source;
40
my $param;
41
my $params;
42
my $sql;
43
my $result;
44
my $row;
45
my @rows;
46
my $rows;
47
my $query;
48
my @queries;
49
my $select_query;
50
my $insert_query;
51
my $update_query;
52
my $ret_val;
53
my $infos;
added common test executing ...
Yuki Kimoto authored on 2011-08-07
54
my $model;
cleanup test
Yuki Kimoto authored on 2011-08-08
55
my $model2;
56
my $where;
57
my $update_param;
58
my $insert_param;
59
my $join;
cleanup test
Yuki Kimoto authored on 2011-08-10
60
my $binary;
added common test executing ...
Yuki Kimoto authored on 2011-08-07
61

            
62
# Drop table
63
eval { $dbi->execute('drop table table1') };
64

            
65
# Create table
66
$dbi->execute($create_table1);
67
$model = $dbi->create_model(table => 'table1');
68
$model->insert({key1 => 1, key2 => 2});
69
is_deeply($model->select->all, [{key1 => 1, key2 => 2}]);
70

            
cleanup test
Yuki Kimoto authored on 2011-08-08
71
test 'DBIx::Custom::Result test';
72
$dbi->delete_all(table => 'table1');
73
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
74
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
75
$source = "select key1, key2 from table1";
76
$query = $dbi->create_query($source);
77
$result = $dbi->execute($query);
78

            
79
@rows = ();
80
while (my $row = $result->fetch) {
81
    push @rows, [@$row];
82
}
83
is_deeply(\@rows, [[1, 2], [3, 4]], "fetch");
84

            
85
$result = $dbi->execute($query);
86
@rows = ();
87
while (my $row = $result->fetch_hash) {
88
    push @rows, {%$row};
89
}
90
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "fetch_hash");
91

            
92
$result = $dbi->execute($query);
93
$rows = $result->fetch_all;
94
is_deeply($rows, [[1, 2], [3, 4]], "fetch_all");
95

            
96
$result = $dbi->execute($query);
97
$rows = $result->fetch_hash_all;
98
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "all");
99

            
100
test 'Insert query return value';
101
$source = "insert into table1 {insert_param key1 key2}";
102
$query = $dbi->execute($source, {}, query => 1);
103
$ret_val = $dbi->execute($query, param => {key1 => 1, key2 => 2});
104
ok($ret_val);
105

            
106
test 'Direct query';
107
$dbi->delete_all(table => 'table1');
108
$insert_source = "insert into table1 {insert_param key1 key2}";
109
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2});
110
$result = $dbi->execute('select * from table1;');
111
$rows = $result->all;
112
is_deeply($rows, [{key1 => 1, key2 => 2}]);
113

            
114
test 'Filter basic';
115
$dbi->delete_all(table => 'table1');
116
$dbi->register_filter(twice       => sub { $_[0] * 2}, 
117
                    three_times => sub { $_[0] * 3});
118

            
119
$insert_source  = "insert into table1 {insert_param key1 key2};";
120
$insert_query = $dbi->execute($insert_source, {}, query => 1);
121
$insert_query->filter({key1 => 'twice'});
122
$dbi->execute($insert_query, param => {key1 => 1, key2 => 2});
123
$result = $dbi->execute('select * from table1;');
124
$rows = $result->filter({key2 => 'three_times'})->all;
125
is_deeply($rows, [{key1 => 2, key2 => 6}], "filter fetch_filter");
126

            
127
test 'Filter in';
128
$dbi->delete_all(table => 'table1');
129
$insert_source  = "insert into table1 {insert_param key1 key2};";
130
$insert_query = $dbi->execute($insert_source, {}, query => 1);
131
$dbi->execute($insert_query, param => {key1 => 2, key2 => 4});
132
$select_source = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}";
133
$select_query = $dbi->execute($select_source,{}, query => 1);
134
$select_query->filter({'table1.key1' => 'twice'});
135
$result = $dbi->execute($select_query, param => {'table1.key1' => [1,5], 'table1.key2' => [2,4]});
136
$rows = $result->all;
137
is_deeply($rows, [{key1 => 2, key2 => 4}], "filter");
138

            
cleanup test
Yuki Kimoto authored on 2011-08-08
139
test 'DBIx::Custom::SQLTemplate basic tag';
140
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-08
141
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-08
142
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
143
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
144

            
145
$source = "select * from table1 where key1 = :key1 and {<> key2} and {< key3} and {> key4} and {>= key5};";
146
$query = $dbi->execute($source, {}, query => 1);
147
$result = $dbi->execute($query, param => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
148
$rows = $result->all;
149
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1");
150

            
151
$source = "select * from table1 where key1 = :key1 and {<> key2} and {< key3} and {> key4} and {>= key5};";
152
$query = $dbi->execute($source, {}, query => 1);
153
$result = $dbi->execute($query, {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
154
$rows = $result->all;
155
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1");
156

            
157
$source = "select * from table1 where {<= key1} and {like key2};";
158
$query = $dbi->execute($source, {}, query => 1);
159
$result = $dbi->execute($query, param => {key1 => 1, key2 => '%2%'});
160
$rows = $result->all;
161
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag2");
162

            
163
test 'DIB::Custom::SQLTemplate in tag';
164
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-08
165
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-08
166
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
167
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
168

            
169
$source = "select * from table1 where {in key1 2};";
170
$query = $dbi->execute($source, {}, query => 1);
171
$result = $dbi->execute($query, param => {key1 => [9, 1]});
172
$rows = $result->all;
173
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic");
174

            
175
test 'DBIx::Custom::SQLTemplate insert tag';
176
$dbi->delete_all(table => 'table1');
177
$insert_source = 'insert into table1 {insert_param key1 key2 key3 key4 key5}';
178
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
179

            
180
$result = $dbi->execute('select * from table1;');
181
$rows = $result->all;
182
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic");
183

            
184
test 'DBIx::Custom::SQLTemplate update tag';
185
$dbi->delete_all(table => 'table1');
186
$insert_source = "insert into table1 {insert_param key1 key2 key3 key4 key5}";
187
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
188
$dbi->execute($insert_source, param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
189

            
190
$update_source = 'update table1 {update_param key1 key2 key3 key4} where {= key5}';
191
$dbi->execute($update_source, param => {key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5});
192

            
193
$result = $dbi->execute('select * from table1 order by key1;');
194
$rows = $result->all;
195
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5},
196
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "basic");
197

            
cleanup test
Yuki Kimoto authored on 2011-08-08
198
test 'Named placeholder';
199
$dbi->execute('drop table table1');
200
$dbi->execute($create_table1_2);
201
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
202
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
203

            
204
$source = "select * from table1 where key1 = :key1 and key2 = :key2";
205
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
206
$rows = $result->all;
207
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
208

            
209
$source = "select * from table1 where key1 = \n:key1\n and key2 = :key2";
210
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
211
$rows = $result->all;
212
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
213

            
214
$source = "select * from table1 where key1 = :key1 or key1 = :key1";
215
$result = $dbi->execute($source, param => {key1 => [1, 2]});
216
$rows = $result->all;
217
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
218

            
219
$source = "select * from table1 where key1 = :table1.key1 and key2 = :table1.key2";
220
$result = $dbi->execute(
221
    $source,
222
    param => {'table1.key1' => 1, 'table1.key2' => 1},
223
    filter => {'table1.key2' => sub { $_[0] * 2 }}
224
);
225
$rows = $result->all;
226
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
227

            
228
$dbi->execute('drop table table1');
229
$dbi->execute($create_table1);
230
$dbi->insert(table => 'table1', param => {key1 => '2011-10-14 12:19:18', key2 => 2});
231
$source = "select * from table1 where key1 = '2011-10-14 12:19:18' and key2 = :key2";
232
$result = $dbi->execute(
233
    $source,
234
    param => {'key2' => 2},
235
);
236

            
237
$rows = $result->all;
238
is_deeply($rows, [{key1 => '2011-10-14 12:19:18', key2 => 2}]);
239

            
240
$dbi->delete_all(table => 'table1');
241
$dbi->insert(table => 'table1', param => {key1 => 'a:b c:d', key2 => 2});
242
$source = "select * from table1 where key1 = 'a\\:b c\\:d' and key2 = :key2";
243
$result = $dbi->execute(
244
    $source,
245
    param => {'key2' => 2},
246
);
247
$rows = $result->all;
248
is_deeply($rows, [{key1 => 'a:b c:d', key2 => 2}]);
249

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
257
test 'insert';
cleanup test
Yuki Kimoto authored on 2011-08-10
258
eval { $dbi->execute('drop table table1') };
259
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
260
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
261
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
262
$result = $dbi->execute('select * from table1;');
263
$rows   = $result->all;
264
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
265

            
266
$dbi->execute('delete from table1');
267
$dbi->register_filter(
268
    twice       => sub { $_[0] * 2 },
269
    three_times => sub { $_[0] * 3 }
270
);
271
$dbi->default_bind_filter('twice');
272
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => 'three_times'});
273
$result = $dbi->execute('select * from table1;');
274
$rows   = $result->all;
275
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter");
276
$dbi->default_bind_filter(undef);
277

            
cleanup test
Yuki Kimoto authored on 2011-08-10
278
$dbi->execute('drop table table1');
279
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
280
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, append => '   ');
281
$rows = $dbi->select(table => 'table1')->all;
282
is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append');
283

            
284
eval{$dbi->insert(table => 'table1', noexist => 1)};
285
like($@, qr/noexist/, "invalid");
286

            
287
eval{$dbi->insert(table => 'table', param => {';' => 1})};
288
like($@, qr/safety/);
289

            
cleanup test
Yuki Kimoto authored on 2011-08-10
290
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
291
$dbi->execute($create_table_reserved);
cleanup test
Yuki Kimoto authored on 2011-08-10
292
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
293
$dbi->insert(table => 'table', param => {select => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
294
$result = $dbi->execute("select * from ${q}table$p");
cleanup test
Yuki Kimoto authored on 2011-08-10
295
$rows   = $result->all;
test cleanup
Yuki Kimoto authored on 2011-08-10
296
is_deeply($rows, [{select => 2, update => undef}], "reserved word");
cleanup test
Yuki Kimoto authored on 2011-08-10
297

            
cleanup test
Yuki Kimoto authored on 2011-08-10
298
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
299
$dbi->execute($create_table1);
300
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
301
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
302
$result = $dbi->execute('select * from table1;');
303
$rows   = $result->all;
304
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
305

            
cleanup test
Yuki Kimoto authored on 2011-08-10
306
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
307
$dbi->execute($create_table1);
308
$dbi->insert(table => 'table1', param => {key1 => \"'1'", key2 => 2});
309
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
310
$result = $dbi->execute('select * from table1;');
311
$rows   = $result->all;
312
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
313

            
test cleanup
Yuki Kimoto authored on 2011-08-10
314
test 'update';
315
eval { $dbi->execute('drop table table1') };
316
$dbi->execute($create_table1_2);
317
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
318
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
319
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
320
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
321
$rows   = $result->all;
322
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
323
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
324
                  "basic");
325
                  
326
$dbi->execute("delete from table1");
327
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
328
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
329
$dbi->update(table => 'table1', param => {key2 => 12}, where => {key2 => 2, key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
330
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
331
$rows   = $result->all;
332
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
333
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
334
                  "update key same as search key");
335

            
336
$dbi->update(table => 'table1', param => {key2 => [12]}, where => {key2 => 2, key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
337
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
338
$rows   = $result->all;
339
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
340
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
341
                  "update key same as search key : param is array ref");
342

            
343
$dbi->execute("delete from table1");
344
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
345
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
346
$dbi->register_filter(twice => sub { $_[0] * 2 });
347
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1},
348
              filter => {key2 => sub { $_[0] * 2 }});
test cleanup
Yuki Kimoto authored on 2011-08-10
349
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
350
$rows   = $result->all;
351
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5},
352
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
353
                  "filter");
354

            
355
$result = $dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}, append => '   ');
356

            
357
eval{$dbi->update(table => 'table1', where => {key1 => 1}, noexist => 1)};
358
like($@, qr/noexist/, "invalid");
359

            
360
eval{$dbi->update(table => 'table1')};
361
like($@, qr/where/, "not contain where");
362

            
363
eval { $dbi->execute('drop table table1') };
364
$dbi->execute($create_table1);
365
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
366
$where = $dbi->where;
367
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
368
$where->param({key1 => 1, key2 => 2});
369
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
370
$result = $dbi->select(table => 'table1');
371
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
372

            
373
eval { $dbi->execute('drop table table1') };
374
$dbi->execute($create_table1);
375
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
376
$dbi->update(
377
    table => 'table1',
378
    param => {key1 => 3},
379
    where => [
380
        ['and', 'key1 = :key1', 'key2 = :key2'],
381
        {key1 => 1, key2 => 2}
382
    ]
383
);
384
$result = $dbi->select(table => 'table1');
385
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
386

            
387
eval { $dbi->execute('drop table table1') };
388
$dbi->execute($create_table1);
389
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
390
$where = $dbi->where;
391
$where->clause(['and', 'key2 = :key2']);
392
$where->param({key2 => 2});
393
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
394
$result = $dbi->select(table => 'table1');
395
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
396

            
397
eval{$dbi->update(table => 'table1', param => {';' => 1})};
398
like($@, qr/safety/);
399

            
400
eval{$dbi->update(table => 'table1', param => {'key1' => 1}, where => {';' => 1})};
401
like($@, qr/safety/);
402

            
403
eval { $dbi->execute('drop table table1') };
404
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
405
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
406
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
407
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
408
$dbi->insert(table => 'table', param => {select => 1});
409
$dbi->update(table => 'table', where => {select => 1}, param => {update => 2});
410
$result = $dbi->execute("select * from ${q}table$p");
411
$rows   = $result->all;
412
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
413

            
414
eval {$dbi->update_all(table => 'table', param => {';' => 2}) };
415
like($@, qr/safety/);
416

            
417
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
418
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
419
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
420
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
421
$dbi->insert(table => 'table', param => {select => 1});
422
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
423
$result = $dbi->execute("select * from ${q}table$p");
424
$rows   = $result->all;
425
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
426

            
427
eval { $dbi->execute('drop table table1') };
428
$dbi->execute($create_table1_2);
429
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
430
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
431
$dbi->update({key2 => 11}, table => 'table1', where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
432
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
433
$rows   = $result->all;
434
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
435
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
436
                  "basic");
437

            
438
eval { $dbi->execute('drop table table1') };
439
$dbi->execute($create_table1_2);
440
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
441
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
442
$dbi->update(table => 'table1', param => {key2 => \"'11'"}, where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
443
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
444
$rows   = $result->all;
445
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
446
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
447
                  "basic");
448

            
449
test 'update_all';
450
eval { $dbi->execute('drop table table1') };
451
$dbi->execute($create_table1_2);
452
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
453
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
454
$dbi->register_filter(twice => sub { $_[0] * 2 });
455
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'});
456
$result = $dbi->execute('select * from table1;');
457
$rows   = $result->all;
458
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
459
                  {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
460
                  "filter");
461

            
462

            
463
test 'delete';
464
eval { $dbi->execute('drop table table1') };
465
$dbi->execute($create_table1);
466
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
467
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
468
$dbi->delete(table => 'table1', where => {key1 => 1});
469
$result = $dbi->execute('select * from table1;');
470
$rows   = $result->all;
471
is_deeply($rows, [{key1 => 3, key2 => 4}], "basic");
472

            
473
$dbi->execute("delete from table1;");
474
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
475
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
476
$dbi->register_filter(twice => sub { $_[0] * 2 });
477
$dbi->delete(table => 'table1', where => {key2 => 1}, filter => {key2 => 'twice'});
478
$result = $dbi->execute('select * from table1;');
479
$rows   = $result->all;
480
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter");
481

            
482
$dbi->delete(table => 'table1', where => {key1 => 1}, append => '   ');
483

            
484
$dbi->delete_all(table => 'table1');
485
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
486
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
487
$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2});
488
$rows = $dbi->select(table => 'table1')->all;
489
is_deeply($rows, [{key1 => 3, key2 => 4}], "delete multi key");
490

            
491
eval{$dbi->delete(table => 'table1', where => {key1 => 1}, noexist => 1)};
492
like($@, qr/noexist/, "invalid");
493

            
494
eval { $dbi->execute('drop table table1') };
495
$dbi->execute($create_table1);
496
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
497
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
498
$where = $dbi->where;
499
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
500
$where->param({ke1 => 1, key2 => 2});
501
$dbi->delete(table => 'table1', where => $where);
502
$result = $dbi->select(table => 'table1');
503
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
504

            
505
eval { $dbi->execute('drop table table1') };
506
$dbi->execute($create_table1);
507
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
508
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
509
$dbi->delete(
510
    table => 'table1',
511
    where => [
512
        ['and', 'key1 = :key1', 'key2 = :key2'],
513
        {ke1 => 1, key2 => 2}
514
    ]
515
);
516
$result = $dbi->select(table => 'table1');
517
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
518

            
519
eval { $dbi->execute('drop table table1') };
test cleanup
Yuki Kimoto authored on 2011-08-10
520
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
521
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
522
$dbi->delete(table => 'table1', where => {key1 => 1}, prefix => '    ');
523
$result = $dbi->execute('select * from table1;');
524
$rows   = $result->all;
525
is_deeply($rows, [], "basic");
526

            
527
test 'delete error';
528
eval { $dbi->execute('drop table table1') };
529
$dbi->execute($create_table1);
530
eval{$dbi->delete(table => 'table1')};
531
like($@, qr/"where" must be specified/,
532
         "where key-value pairs not specified");
533

            
534
eval{$dbi->delete(table => 'table1', where => {';' => 1})};
535
like($@, qr/safety/);
536

            
537
$dbi = DBIx::Custom->connect;
538
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
539
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
540
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
541
$dbi->insert(table => 'table', param => {select => 1});
542
$dbi->delete(table => 'table', where => {select => 1});
543
$result = $dbi->execute("select * from ${q}table$p");
544
$rows   = $result->all;
545
is_deeply($rows, [], "reserved word");
546

            
547
test 'delete_all';
548
eval { $dbi->execute('drop table table1') };
549
$dbi->execute($create_table1);
550
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
551
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
552
$dbi->delete_all(table => 'table1');
553
$result = $dbi->execute('select * from table1;');
554
$rows   = $result->all;
555
is_deeply($rows, [], "basic");
556

            
557

            
558
test 'select';
559
eval { $dbi->execute('drop table table1') };
560
$dbi->execute($create_table1);
561
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
562
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
563
$rows = $dbi->select(table => 'table1')->all;
564
is_deeply($rows, [{key1 => 1, key2 => 2},
565
                  {key1 => 3, key2 => 4}], "table");
566

            
567
$rows = $dbi->select(table => 'table1', column => ['key1'])->all;
568
is_deeply($rows, [{key1 => 1}, {key1 => 3}], "table and columns and where key");
569

            
570
$rows = $dbi->select(table => 'table1', where => {key1 => 1})->all;
571
is_deeply($rows, [{key1 => 1, key2 => 2}], "table and columns and where key");
572

            
573
$rows = $dbi->select(table => 'table1', column => ['key1'], where => {key1 => 3})->all;
574
is_deeply($rows, [{key1 => 3}], "table and columns and where key");
575

            
576
$rows = $dbi->select(table => 'table1', append => "order by key1 desc limit 1")->all;
577
is_deeply($rows, [{key1 => 3, key2 => 4}], "append statement");
578

            
579
$dbi->register_filter(decrement => sub { $_[0] - 1 });
580
$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'})
581
            ->all;
582
is_deeply($rows, [{key1 => 1, key2 => 2}], "filter");
583

            
test cleanup
Yuki Kimoto authored on 2011-08-10
584
eval { $dbi->execute("drop table table2") };
585
$dbi->execute($create_table2);
test cleanup
Yuki Kimoto authored on 2011-08-10
586
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
587
$rows = $dbi->select(
588
    table => [qw/table1 table2/],
589
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
590
    where   => {'table1.key2' => 2},
591
    relation  => {'table1.key1' => 'table2.key1'}
592
)->all;
593
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : exists where");
594

            
595
$rows = $dbi->select(
596
    table => [qw/table1 table2/],
597
    column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
598
    relation  => {'table1.key1' => 'table2.key1'}
599
)->all;
600
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : no exists where");
601

            
602
eval{$dbi->select(table => 'table1', noexist => 1)};
603
like($@, qr/noexist/, "invalid");
604

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

            
614
test 'fetch filter';
615
eval { $dbi->execute('drop table table1') };
616
$dbi->register_filter(
617
    twice       => sub { $_[0] * 2 },
618
    three_times => sub { $_[0] * 3 }
619
);
620
$dbi->default_fetch_filter('twice');
621
$dbi->execute($create_table1);
622
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
623
$result = $dbi->select(table => 'table1');
624
$result->filter({key1 => 'three_times'});
625
$row = $result->one;
626
is_deeply($row, {key1 => 3, key2 => 4}, "default_fetch_filter and filter");
627

            
628
test 'filters';
629
$dbi = DBIx::Custom->new;
630

            
631
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')),
632
   'あ', "decode_utf8");
633

            
634
is($dbi->filters->{encode_utf8}->('あ'),
635
   encode_utf8('あ'), "encode_utf8");
636

            
637
test 'transaction';
638
$dbi = DBIx::Custom->connect;
639
eval { $dbi->execute('drop table table1') };
640
$dbi->execute($create_table1);
641
$dbi->dbh->begin_work;
642
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
643
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
644
$dbi->dbh->commit;
645
$result = $dbi->select(table => 'table1');
646
is_deeply(scalar $result->all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}],
647
          "commit");
648

            
649
$dbi = DBIx::Custom->connect;
650
eval { $dbi->execute('drop table table1') };
651
$dbi->execute($create_table1);
652
$dbi->dbh->begin_work(0);
653
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
654
$dbi->dbh->rollback;
655

            
656
$result = $dbi->select(table => 'table1');
657
ok(! $result->fetch_first, "rollback");
658

            
659
test 'cache';
660
eval { $dbi->execute('drop table table1') };
661
$dbi->cache(1);
662
$dbi->execute($create_table1);
663
$source = 'select * from table1 where key1 = :key1 and key2 = :key2;';
664
$dbi->execute($source, {}, query => 1);
665
is_deeply($dbi->{_cached}->{$source}, 
666
          {sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2'], tables => []}, "cache");
667

            
668
eval { $dbi->execute('drop table table1') };
669
$dbi->execute($create_table1);
670
$dbi->{_cached} = {};
671
$dbi->cache(0);
672
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
673
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
674

            
675
test 'execute';
676
eval { $dbi->execute('drop table table1') };
677
$dbi->execute($create_table1);
678
{
679
    local $Carp::Verbose = 0;
680
    eval{$dbi->execute('select * frm table1')};
681
    like($@, qr/\Qselect * frm table1;/, "fail prepare");
682
    like($@, qr/\.t /, "fail : not verbose");
683
}
684
{
685
    local $Carp::Verbose = 1;
686
    eval{$dbi->execute('select * frm table1')};
687
    like($@, qr/Custom.*\.t /s, "fail : verbose");
688
}
689

            
690
eval{$dbi->execute('select * from table1', no_exists => 1)};
691
like($@, qr/wrong/, "invald SQL");
692

            
693
$query = $dbi->execute('select * from table1 where key1 = :key1', {}, query => 1);
694
$dbi->dbh->disconnect;
695
eval{$dbi->execute($query, param => {key1 => {a => 1}})};
696
ok($@, "execute fail");
697

            
698
{
699
    local $Carp::Verbose = 0;
700
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
701
    like($@, qr/\Q.t /, "caller spec : not vebose");
702
}
703
{
704
    local $Carp::Verbose = 1;
705
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
706
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
707
}
708

            
709

            
710
test 'transaction';
711
$dbi = DBIx::Custom->connect;
712
eval { $dbi->execute('drop table table1') };
713
$dbi->execute($create_table1);
714

            
715
$dbi->begin_work;
716

            
717
eval {
718
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
719
    die "Error";
720
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
721
};
722

            
723
$dbi->rollback if $@;
724

            
725
$result = $dbi->select(table => 'table1');
726
$rows = $result->all;
727
is_deeply($rows, [], "rollback");
728

            
729
$dbi->begin_work;
730

            
731
eval {
732
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
733
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
734
};
735

            
736
$dbi->commit unless $@;
737

            
738
$result = $dbi->select(table => 'table1');
739
$rows = $result->all;
740
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "commit");
741

            
742
$dbi->dbh->{AutoCommit} = 0;
743
eval{ $dbi->begin_work };
744
ok($@, "exception");
745
$dbi->dbh->{AutoCommit} = 1;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
746

            
test cleanup
Yuki Kimoto authored on 2011-08-10
747
test 'cache';
748
eval { $dbi->execute('drop table table1') };
749
$dbi->cache(1);
750
$dbi->execute($create_table1);
751
$source = 'select * from table1 where key1 = :key1 and key2 = :key2;';
752
$dbi->execute($source, {}, query => 1);
753
is_deeply($dbi->{_cached}->{$source}, 
754
          {sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2'], tables => []}, "cache");
755

            
756
eval { $dbi->execute('drop table table1') };
757
$dbi->execute($create_table1);
758
$dbi->{_cached} = {};
759
$dbi->cache(0);
760
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
761
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
762

            
763
test 'execute';
764
eval { $dbi->execute('drop table table1') };
765
$dbi->execute($create_table1);
766
{
767
    local $Carp::Verbose = 0;
768
    eval{$dbi->execute('select * frm table1')};
769
    like($@, qr/\Qselect * frm table1;/, "fail prepare");
770
    like($@, qr/\.t /, "fail : not verbose");
771
}
772
{
773
    local $Carp::Verbose = 1;
774
    eval{$dbi->execute('select * frm table1')};
775
    like($@, qr/Custom.*\.t /s, "fail : verbose");
776
}
777

            
778
eval{$dbi->execute('select * from table1', no_exists => 1)};
779
like($@, qr/wrong/, "invald SQL");
780

            
781
$query = $dbi->execute('select * from table1 where key1 = :key1', {}, query => 1);
782
$dbi->dbh->disconnect;
783
eval{$dbi->execute($query, param => {key1 => {a => 1}})};
784
ok($@, "execute fail");
785

            
786
{
787
    local $Carp::Verbose = 0;
788
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
789
    like($@, qr/\Q.t /, "caller spec : not vebose");
790
}
791
{
792
    local $Carp::Verbose = 1;
793
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
794
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
795
}
796

            
797

            
798
test 'transaction';
799
$dbi = DBIx::Custom->connect;
800
eval { $dbi->execute('drop table table1') };
801
$dbi->execute($create_table1);
802

            
803
$dbi->begin_work;
804

            
805
eval {
806
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
807
    die "Error";
808
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
809
};
810

            
811
$dbi->rollback if $@;
812

            
813
$result = $dbi->select(table => 'table1');
814
$rows = $result->all;
815
is_deeply($rows, [], "rollback");
816

            
817
$dbi->begin_work;
818

            
819
eval {
820
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
821
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
822
};
823

            
824
$dbi->commit unless $@;
825

            
826
$result = $dbi->select(table => 'table1');
827
$rows = $result->all;
828
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "commit");
829

            
830
$dbi->dbh->{AutoCommit} = 0;
831
eval{ $dbi->begin_work };
832
ok($@, "exception");
833
$dbi->dbh->{AutoCommit} = 1;
834

            
835

            
836
test 'method';
837
$dbi->method(
838
    one => sub { 1 }
839
);
840
$dbi->method(
841
    two => sub { 2 }
842
);
843
$dbi->method({
844
    twice => sub {
845
        my $self = shift;
846
        return $_[0] * 2;
847
    }
848
});
849

            
850
is($dbi->one, 1, "first");
851
is($dbi->two, 2, "second");
852
is($dbi->twice(5), 10 , "second");
853

            
854
eval {$dbi->XXXXXX};
855
ok($@, "not exists");
856

            
857
test 'out filter';
858
$dbi = DBIx::Custom->connect;
859
eval { $dbi->execute('drop table table1') };
860
$dbi->execute($create_table1);
861
$dbi->register_filter(twice => sub { $_[0] * 2 });
862
$dbi->register_filter(three_times => sub { $_[0] * 3});
863
$dbi->apply_filter(
864
    'table1', 'key1' => {out => 'twice', in => 'three_times'}, 
865
              'key2' => {out => 'three_times', in => 'twice'});
866
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
867
$result = $dbi->execute('select * from table1;');
868
$row   = $result->fetch_hash_first;
869
is_deeply($row, {key1 => 2, key2 => 6}, "insert");
870
$result = $dbi->select(table => 'table1');
871
$row   = $result->one;
872
is_deeply($row, {key1 => 6, key2 => 12}, "insert");
873

            
874
$dbi = DBIx::Custom->connect;
875
eval { $dbi->execute('drop table table1') };
876
$dbi->execute($create_table1);
877
$dbi->register_filter(twice => sub { $_[0] * 2 });
878
$dbi->register_filter(three_times => sub { $_[0] * 3});
879
$dbi->apply_filter(
880
    'table1', 'key1' => {out => 'twice', in => 'three_times'}, 
881
              'key2' => {out => 'three_times', in => 'twice'});
882
$dbi->apply_filter(
883
    'table1', 'key1' => {out => undef}
884
); 
885
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
886
$result = $dbi->execute('select * from table1;');
887
$row   = $result->one;
888
is_deeply($row, {key1 => 1, key2 => 6}, "insert");
889

            
890
$dbi = DBIx::Custom->connect;
891
eval { $dbi->execute('drop table table1') };
892
$dbi->execute($create_table1);
893
$dbi->register_filter(twice => sub { $_[0] * 2 });
894
$dbi->apply_filter(
895
    'table1', 'key1' => {out => 'twice', in => 'twice'}
896
);
897
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => undef});
898
$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2});
899
$result = $dbi->execute('select * from table1;');
900
$row   = $result->one;
901
is_deeply($row, {key1 => 4, key2 => 2}, "update");
902

            
903
$dbi = DBIx::Custom->connect;
904
eval { $dbi->execute('drop table table1') };
905
$dbi->execute($create_table1);
906
$dbi->register_filter(twice => sub { $_[0] * 2 });
907
$dbi->apply_filter(
908
    'table1', 'key1' => {out => 'twice', in => 'twice'}
909
);
910
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1=> undef});
911
$dbi->delete(table => 'table1', where => {key1 => 1});
912
$result = $dbi->execute('select * from table1;');
913
$rows   = $result->all;
914
is_deeply($rows, [], "delete");
915

            
916
$dbi = DBIx::Custom->connect;
917
eval { $dbi->execute('drop table table1') };
918
$dbi->execute($create_table1);
919
$dbi->register_filter(twice => sub { $_[0] * 2 });
920
$dbi->apply_filter(
921
    'table1', 'key1' => {out => 'twice', in => 'twice'}
922
);
923
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
924
$result = $dbi->select(table => 'table1', where => {key1 => 1});
925
$result->filter({'key2' => 'twice'});
926
$rows   = $result->all;
927
is_deeply($rows, [{key1 => 4, key2 => 4}], "select");
928

            
929
$dbi = DBIx::Custom->connect;
930
eval { $dbi->execute('drop table table1') };
931
$dbi->execute($create_table1);
932
$dbi->register_filter(twice => sub { $_[0] * 2 });
933
$dbi->apply_filter(
934
    'table1', 'key1' => {out => 'twice', in => 'twice'}
935
);
936
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
937
$result = $dbi->execute("select * from table1 where key1 = :key1 and key2 = :key2;",
938
                        param => {key1 => 1, key2 => 2},
939
                        table => ['table1']);
940
$rows   = $result->all;
941
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute");
942

            
943
$dbi = DBIx::Custom->connect;
944
eval { $dbi->execute('drop table table1') };
945
$dbi->execute($create_table1);
946
$dbi->register_filter(twice => sub { $_[0] * 2 });
947
$dbi->apply_filter(
948
    'table1', 'key1' => {out => 'twice', in => 'twice'}
949
);
950
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
951
$result = $dbi->execute("select * from {table table1} where key1 = :key1 and key2 = :key2;",
952
                        param => {key1 => 1, key2 => 2});
953
$rows   = $result->all;
954
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute table tag");
955

            
956
$dbi = DBIx::Custom->connect;
957
eval { $dbi->execute('drop table table1') };
958
eval { $dbi->execute('drop table table2') };
959
$dbi->execute($create_table1);
960
$dbi->execute($create_table2);
961
$dbi->register_filter(twice => sub { $_[0] * 2 });
962
$dbi->register_filter(three_times => sub { $_[0] * 3 });
963
$dbi->apply_filter(
964
    'table1', 'key2' => {out => 'twice', in => 'twice'}
965
);
966
$dbi->apply_filter(
967
    'table2', 'key3' => {out => 'three_times', in => 'three_times'}
968
);
969
$dbi->insert(table => 'table1', param => {key1 => 5, key2 => 2}, filter => {key2 => undef});
970
$dbi->insert(table => 'table2', param => {key1 => 5, key3 => 6}, filter => {key3 => undef});
971
$result = $dbi->select(
972
     table => ['table1', 'table2'],
973
     column => ['key2', 'key3'],
974
     where => {'table1.key2' => 1, 'table2.key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
975

            
976
$result->filter({'key2' => 'twice'});
977
$rows   = $result->all;
978
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join");
979

            
980
$result = $dbi->select(
981
     table => ['table1', 'table2'],
982
     column => ['key2', 'key3'],
983
     where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
984

            
985
$result->filter({'key2' => 'twice'});
986
$rows   = $result->all;
987
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join : omit");
988

            
989
test 'each_column';
990
$dbi = DBIx::Custom->connect;
991
eval { $dbi->execute("drop table ${q}table$p") };
992
eval { $dbi->execute('drop table table1') };
993
eval { $dbi->execute('drop table table2') };
test cleranup
Yuki Kimoto authored on 2011-08-10
994
eval { $dbi->execute('drop table table3') };
test cleanup
Yuki Kimoto authored on 2011-08-10
995
$dbi->execute($create_table1_type);
996
$dbi->execute($create_table2);
997

            
998
$infos = [];
999
$dbi->each_column(sub {
1000
    my ($self, $table, $column, $cinfo) = @_;
1001
    
1002
    if ($table =~ /^table\d/) {
1003
         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
1004
         push @$infos, $info;
1005
    }
1006
});
1007
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
1008
is_deeply($infos, 
1009
    [
1010
        ['table1', 'key1', 'key1'],
1011
        ['table1', 'key2', 'key2'],
1012
        ['table2', 'key1', 'key1'],
1013
        ['table2', 'key3', 'key3']
1014
    ]
1015
    
1016
);
1017
test 'each_table';
1018
$dbi = DBIx::Custom->connect;
1019
eval { $dbi->execute('drop table table1') };
1020
eval { $dbi->execute('drop table table2') };
1021
$dbi->execute($create_table2);
1022
$dbi->execute($create_table1_type);
1023

            
1024
$infos = [];
1025
$dbi->each_table(sub {
1026
    my ($self, $table, $table_info) = @_;
1027
    
1028
    if ($table =~ /^table\d/) {
1029
         my $info = [$table, $table_info->{TABLE_NAME}];
1030
         push @$infos, $info;
1031
    }
1032
});
1033
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
1034
is_deeply($infos, 
1035
    [
1036
        ['table1', 'table1'],
1037
        ['table2', 'table2'],
1038
    ]
1039
);
1040

            
1041
test 'limit';
1042
$dbi = DBIx::Custom->connect;
1043
eval { $dbi->execute('drop table table1') };
1044
$dbi->execute($create_table1);
1045
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1046
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
1047
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
1048
$dbi->register_tag(
1049
    limit => sub {
1050
        my ($count, $offset) = @_;
1051
        
1052
        my $s = '';
1053
        $s .= "limit $count";
1054
        $s .= " offset $offset" if defined $offset;
1055
        
1056
        return [$s, []];
1057
    }
1058
);
1059
$rows = $dbi->select(
1060
  table => 'table1',
1061
  where => {key1 => 1},
1062
  append => "order by key2 {limit 1 0}"
1063
)->all;
1064
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1065
$rows = $dbi->select(
1066
  table => 'table1',
1067
  where => {key1 => 1},
1068
  append => "order by key2 {limit 2 1}"
1069
)->all;
1070
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
1071
$rows = $dbi->select(
1072
  table => 'table1',
1073
  where => {key1 => 1},
1074
  append => "order by key2 {limit 1}"
1075
)->all;
1076
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1077

            
1078
test 'connect super';
1079
{
1080
    package MyDBI;
1081
    
1082
    use base 'DBIx::Custom';
1083
    sub connect {
1084
        my $self = shift->SUPER::connect(@_);
1085
        
1086
        return $self;
1087
    }
1088
    
1089
    sub new {
1090
        my $self = shift->SUPER::new(@_);
1091
        
1092
        return $self;
1093
    }
1094
}
1095

            
1096
$dbi = MyDBI->connect;
1097
eval { $dbi->execute('drop table table1') };
1098
$dbi->execute($create_table1);
1099
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1100
is($dbi->select(table => 'table1')->one->{key1}, 1);
1101

            
1102
$dbi = MyDBI->new;
1103
$dbi->connect;
1104
eval { $dbi->execute('drop table table1') };
1105
$dbi->execute($create_table1);
1106
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1107
is($dbi->select(table => 'table1')->one->{key1}, 1);
1108

            
1109
{
1110
    package MyDBI2;
1111
    
1112
    use base 'DBIx::Custom';
1113
    sub connect {
1114
        my $self = shift->SUPER::new(@_);
1115
        $self->connect;
1116
        
1117
        return $self;
1118
    }
1119
}
1120

            
1121
$dbi = MyDBI->connect;
1122
eval { $dbi->execute('drop table table1') };
1123
$dbi->execute($create_table1);
1124
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1125
is($dbi->select(table => 'table1')->one->{key1}, 1);
1126

            
1127
test 'end_filter';
1128
$dbi = DBIx::Custom->connect;
1129
eval { $dbi->execute('drop table table1') };
1130
$dbi->execute($create_table1);
1131
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1132
$result = $dbi->select(table => 'table1');
1133
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1134
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 });
1135
$row = $result->fetch_first;
1136
is_deeply($row, [6, 40]);
1137

            
1138
$dbi = DBIx::Custom->connect;
1139
eval { $dbi->execute('drop table table1') };
1140
$dbi->execute($create_table1);
1141
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1142
$result = $dbi->select(table => 'table1');
1143
$result->filter([qw/key1 key2/] => sub { $_[0] * 2 });
1144
$result->end_filter([[qw/key1 key2/] => sub { $_[0] * 3 }]);
1145
$row = $result->fetch_first;
1146
is_deeply($row, [6, 12]);
1147

            
1148
$dbi = DBIx::Custom->connect;
1149
eval { $dbi->execute('drop table table1') };
1150
$dbi->execute($create_table1);
1151
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1152
$result = $dbi->select(table => 'table1');
1153
$result->filter([[qw/key1 key2/] => sub { $_[0] * 2 }]);
1154
$result->end_filter([qw/key1 key2/] => sub { $_[0] * 3 });
1155
$row = $result->fetch_first;
1156
is_deeply($row, [6, 12]);
1157

            
1158
$dbi->register_filter(five_times => sub { $_[0] * 5 });
1159
$result = $dbi->select(table => 'table1');
1160
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1161
$result->end_filter({key1 => sub { $_[0] * 3 }, key2 => 'five_times' });
1162
$row = $result->one;
1163
is_deeply($row, {key1 => 6, key2 => 40});
1164

            
1165
$dbi->register_filter(five_times => sub { $_[0] * 5 });
1166
$dbi->apply_filter('table1',
1167
    key1 => {end => sub { $_[0] * 3 } },
1168
    key2 => {end => 'five_times'}
1169
);
1170
$result = $dbi->select(table => 'table1');
1171
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1172
$row = $result->one;
1173
is_deeply($row, {key1 => 6, key2 => 40}, 'apply_filter');
1174

            
1175
$dbi->register_filter(five_times => sub { $_[0] * 5 });
1176
$dbi->apply_filter('table1',
1177
    key1 => {end => sub { $_[0] * 3 } },
1178
    key2 => {end => 'five_times'}
1179
);
1180
$result = $dbi->select(table => 'table1');
1181
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1182
$result->filter(key1 => undef);
1183
$result->end_filter(key1 => undef);
1184
$row = $result->one;
1185
is_deeply($row, {key1 => 1, key2 => 40}, 'apply_filter overwrite');
1186

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

            
1201
test 'empty where select';
1202
$dbi = DBIx::Custom->connect;
1203
eval { $dbi->execute('drop table table1') };
1204
$dbi->execute($create_table1);
1205
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1206
$result = $dbi->select(table => 'table1', where => {});
1207
$row = $result->one;
1208
is_deeply($row, {key1 => 1, key2 => 2});
1209

            
1210
test 'select query option';
1211
$dbi = DBIx::Custom->connect;
1212
eval { $dbi->execute('drop table table1') };
1213
$dbi->execute($create_table1);
1214
$query = $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, query => 1);
1215
is(ref $query, 'DBIx::Custom::Query');
1216
$query = $dbi->update(table => 'table1', where => {key1 => 1}, param => {key2 => 2}, query => 1);
1217
is(ref $query, 'DBIx::Custom::Query');
1218
$query = $dbi->delete(table => 'table1', where => {key1 => 1}, query => 1);
1219
is(ref $query, 'DBIx::Custom::Query');
1220
$query = $dbi->select(table => 'table1', where => {key1 => 1, key2 => 2}, query => 1);
1221
is(ref $query, 'DBIx::Custom::Query');
1222

            
1223
test 'where';
1224
$dbi = DBIx::Custom->connect;
1225
eval { $dbi->execute('drop table table1') };
1226
$dbi->execute($create_table1);
1227
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1228
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1229
$where = $dbi->where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
1230
is("$where", "where ( key1 = :key1 and key2 = :key2 )", 'no param');
1231

            
1232
$where = $dbi->where
1233
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
1234
             ->param({key1 => 1});
1235

            
1236
$result = $dbi->select(
1237
    table => 'table1',
1238
    where => $where
1239
);
1240
$row = $result->all;
1241
is_deeply($row, [{key1 => 1, key2 => 2}]);
1242

            
1243
$result = $dbi->select(
1244
    table => 'table1',
1245
    where => [
1246
        ['and', 'key1 = :key1', 'key2 = :key2'],
1247
        {key1 => 1}
1248
    ]
1249
);
1250
$row = $result->all;
1251
is_deeply($row, [{key1 => 1, key2 => 2}]);
1252

            
1253
$where = $dbi->where
1254
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
1255
             ->param({key1 => 1, key2 => 2});
1256
$result = $dbi->select(
1257
    table => 'table1',
1258
    where => $where
1259
);
1260
$row = $result->all;
1261
is_deeply($row, [{key1 => 1, key2 => 2}]);
1262

            
1263
$where = $dbi->where
1264
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
1265
             ->param({});
1266
$result = $dbi->select(
1267
    table => 'table1',
1268
    where => $where,
1269
);
1270
$row = $result->all;
1271
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1272

            
1273
$where = $dbi->where
1274
             ->clause(['and', ['or', 'key1 > :key1', 'key1 < :key1'], 'key2 = :key2'])
1275
             ->param({key1 => [0, 3], key2 => 2});
1276
$result = $dbi->select(
1277
    table => 'table1',
1278
    where => $where,
1279
); 
1280
$row = $result->all;
1281
is_deeply($row, [{key1 => 1, key2 => 2}]);
1282

            
1283
$where = $dbi->where;
1284
$result = $dbi->select(
1285
    table => 'table1',
1286
    where => $where
1287
);
1288
$row = $result->all;
1289
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1290

            
1291
eval {
1292
$where = $dbi->where
1293
             ->clause(['uuu']);
1294
$result = $dbi->select(
1295
    table => 'table1',
1296
    where => $where
1297
);
1298
};
1299
ok($@);
1300

            
1301
$where = $dbi->where;
1302
is("$where", '');
1303

            
1304
$where = $dbi->where
1305
             ->clause(['or', ('key1 = :key1') x 2])
1306
             ->param({key1 => [1, 3]});
1307
$result = $dbi->select(
1308
    table => 'table1',
1309
    where => $where,
1310
);
1311
$row = $result->all;
1312
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1313

            
1314
$where = $dbi->where
1315
             ->clause(['or', ('key1 = :key1') x 2])
1316
             ->param({key1 => [1]});
1317
$result = $dbi->select(
1318
    table => 'table1',
1319
    where => $where,
1320
);
1321
$row = $result->all;
1322
is_deeply($row, [{key1 => 1, key2 => 2}]);
1323

            
1324
$where = $dbi->where
1325
             ->clause(['or', ('key1 = :key1') x 2])
1326
             ->param({key1 => 1});
1327
$result = $dbi->select(
1328
    table => 'table1',
1329
    where => $where,
1330
);
1331
$row = $result->all;
1332
is_deeply($row, [{key1 => 1, key2 => 2}]);
1333

            
1334
$where = $dbi->where
1335
             ->clause('key1 = :key1')
1336
             ->param({key1 => 1});
1337
$result = $dbi->select(
1338
    table => 'table1',
1339
    where => $where,
1340
);
1341
$row = $result->all;
1342
is_deeply($row, [{key1 => 1, key2 => 2}]);
1343

            
1344
$where = $dbi->where
1345
             ->clause('key1 = :key1 key2 = :key2')
1346
             ->param({key1 => 1});
1347
eval{$where->to_string};
1348
like($@, qr/one column/);
1349

            
1350
$where = $dbi->where
1351
             ->clause(['or', ('key1 = :key1') x 3])
1352
             ->param({key1 => [$dbi->not_exists, 1, 3]});
1353
$result = $dbi->select(
1354
    table => 'table1',
1355
    where => $where,
1356
);
1357
$row = $result->all;
1358
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1359

            
1360
$where = $dbi->where
1361
             ->clause(['or', ('key1 = :key1') x 3])
1362
             ->param({key1 => [1, $dbi->not_exists, 3]});
1363
$result = $dbi->select(
1364
    table => 'table1',
1365
    where => $where,
1366
);
1367
$row = $result->all;
1368
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1369

            
1370
$where = $dbi->where
1371
             ->clause(['or', ('key1 = :key1') x 3])
1372
             ->param({key1 => [1, 3, $dbi->not_exists]});
1373
$result = $dbi->select(
1374
    table => 'table1',
1375
    where => $where,
1376
);
1377
$row = $result->all;
1378
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1379

            
1380
$where = $dbi->where
1381
             ->clause(['or', ('key1 = :key1') x 3])
1382
             ->param({key1 => [1, $dbi->not_exists, $dbi->not_exists]});
1383
$result = $dbi->select(
1384
    table => 'table1',
1385
    where => $where,
1386
);
1387
$row = $result->all;
1388
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1389

            
1390
$where = $dbi->where
1391
             ->clause(['or', ('key1 = :key1') x 3])
1392
             ->param({key1 => [$dbi->not_exists, 1, $dbi->not_exists]});
1393
$result = $dbi->select(
1394
    table => 'table1',
1395
    where => $where,
1396
);
1397
$row = $result->all;
1398
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1399

            
1400
$where = $dbi->where
1401
             ->clause(['or', ('key1 = :key1') x 3])
1402
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists, 1]});
1403
$result = $dbi->select(
1404
    table => 'table1',
1405
    where => $where,
1406
);
1407
$row = $result->all;
1408
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1409

            
1410
$where = $dbi->where
1411
             ->clause(['or', ('key1 = :key1') x 3])
1412
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists, $dbi->not_exists]});
1413
$result = $dbi->select(
1414
    table => 'table1',
1415
    where => $where,
1416
);
1417
$row = $result->all;
1418
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1419

            
1420
$where = $dbi->where
1421
             ->clause(['or', ('key1 = :key1') x 3])
1422
             ->param({key1 => []});
1423
$result = $dbi->select(
1424
    table => 'table1',
1425
    where => $where,
1426
);
1427
$row = $result->all;
1428
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1429

            
1430
$where = $dbi->where
1431
             ->clause(['and', '{> key1}', '{< key1}' ])
1432
             ->param({key1 => [2, $dbi->not_exists]});
1433
$result = $dbi->select(
1434
    table => 'table1',
1435
    where => $where,
1436
);
1437
$row = $result->all;
1438
is_deeply($row, [{key1 => 3, key2 => 4}], 'not_exists');
1439

            
1440
$where = $dbi->where
1441
             ->clause(['and', '{> key1}', '{< key1}' ])
1442
             ->param({key1 => [$dbi->not_exists, 2]});
1443
$result = $dbi->select(
1444
    table => 'table1',
1445
    where => $where,
1446
);
1447
$row = $result->all;
1448
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1449

            
1450
$where = $dbi->where
1451
             ->clause(['and', '{> key1}', '{< key1}' ])
1452
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists]});
1453
$result = $dbi->select(
1454
    table => 'table1',
1455
    where => $where,
1456
);
1457
$row = $result->all;
1458
is_deeply($row, [{key1 => 1, key2 => 2},{key1 => 3, key2 => 4}], 'not_exists');
1459

            
1460
$where = $dbi->where
1461
             ->clause(['and', '{> key1}', '{< key1}' ])
1462
             ->param({key1 => [0, 2]});
1463
$result = $dbi->select(
1464
    table => 'table1',
1465
    where => $where,
1466
);
1467
$row = $result->all;
1468
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1469

            
1470
$where = $dbi->where
1471
             ->clause(['and', 'key1 is not null', 'key2 is not null' ]);
1472
$result = $dbi->select(
1473
    table => 'table1',
1474
    where => $where,
1475
);
1476
$row = $result->all;
1477
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1478

            
1479
eval {$dbi->where(ppp => 1) };
1480
like($@, qr/invalid/);
1481

            
1482
$where = $dbi->where(
1483
    clause => ['and', ['or'], ['and', 'key1 = :key1', 'key2 = :key2']],
1484
    param => {key1 => 1, key2 => 2}
1485
);
1486
$result = $dbi->select(
1487
    table => 'table1',
1488
    where => $where,
1489
);
1490
$row = $result->all;
1491
is_deeply($row, [{key1 => 1, key2 => 2}]);
1492

            
1493

            
1494
$where = $dbi->where(
1495
    clause => ['and', ['or'], ['or', ':key1', ':key2']],
1496
    param => {}
1497
);
1498
$result = $dbi->select(
1499
    table => 'table1',
1500
    where => $where,
1501
);
1502
$row = $result->all;
1503
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1504

            
1505
$where = $dbi->where;
1506
$where->clause(['and', ':key1{=}']);
1507
$where->param({key1 => undef});
1508
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1509
$row = $result->all;
1510
is_deeply($row, [{key1 => 1, key2 => 2}]);
1511

            
1512
$where = $dbi->where;
1513
$where->clause(['and', ':key1{=}']);
1514
$where->param({key1 => undef});
1515
$where->if('defined');
1516
$where->map;
1517
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1518
$row = $result->all;
1519
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1520

            
1521
$where = $dbi->where;
1522
$where->clause(['or', ':key1{=}', ':key1{=}']);
1523
$where->param({key1 => [undef, undef]});
1524
$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]});
1525
$row = $result->all;
1526
is_deeply($row, [{key1 => 1, key2 => 2}]);
1527
$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]});
1528
$row = $result->all;
1529
is_deeply($row, [{key1 => 1, key2 => 2}]);
1530

            
1531
$where = $dbi->where;
1532
$where->clause(['and', ':key1{=}']);
1533
$where->param({key1 => [undef, undef]});
1534
$where->if('defined');
1535
$where->map;
1536
$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]});
1537
$row = $result->all;
1538
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1539
$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]});
1540
$row = $result->all;
1541
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1542

            
1543
$where = $dbi->where;
1544
$where->clause(['and', ':key1{=}']);
1545
$where->param({key1 => 0});
1546
$where->if('length');
1547
$where->map;
1548
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1549
$row = $result->all;
1550
is_deeply($row, [{key1 => 1, key2 => 2}]);
1551

            
1552
$where = $dbi->where;
1553
$where->clause(['and', ':key1{=}']);
1554
$where->param({key1 => ''});
1555
$where->if('length');
1556
$where->map;
1557
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1558
$row = $result->all;
1559
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1560

            
1561
$where = $dbi->where;
1562
$where->clause(['and', ':key1{=}']);
1563
$where->param({key1 => 5});
1564
$where->if(sub { ($_[0] || '') eq 5 });
1565
$where->map;
1566
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1567
$row = $result->all;
1568
is_deeply($row, [{key1 => 1, key2 => 2}]);
1569

            
1570
$where = $dbi->where;
1571
$where->clause(['and', ':key1{=}']);
1572
$where->param({key1 => 7});
1573
$where->if(sub { ($_[0] || '') eq 5 });
1574
$where->map;
1575
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1576
$row = $result->all;
1577
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1578

            
1579
$where = $dbi->where;
1580
$where->param({id => 1, author => 'Ken', price => 1900});
1581
$where->map(id => 'book.id',
1582
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1583
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1584
);
1585
is_deeply($where->param, {'book.id' => 1, 'book.author' => '%Ken%',
1586
  'book.price' => 1900});
1587

            
1588
$where = $dbi->where;
1589
$where->param({id => 0, author => 0, price => 0});
1590
$where->map(
1591
    id => 'book.id',
1592
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1593
    price => ['book.price', sub { '%' . $_[0] . '%' },
1594
      {if => sub { $_[0] eq 0 }}]
1595
);
1596
is_deeply($where->param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
1597

            
1598
$where = $dbi->where;
1599
$where->param({id => '', author => '', price => ''});
1600
$where->if('length');
1601
$where->map(
1602
    id => 'book.id',
1603
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1604
    price => ['book.price', sub { '%' . $_[0] . '%' },
1605
      {if => sub { $_[0] eq 1 }}]
1606
);
1607
is_deeply($where->param, {});
1608

            
1609
$where = $dbi->where;
1610
$where->param({id => undef, author => undef, price => undef});
1611
$where->if('length');
1612
$where->map(
1613
    id => 'book.id',
1614
    price => ['book.price', {if => 'exists'}]
1615
);
1616
is_deeply($where->param, {'book.price' => undef});
1617

            
1618
$where = $dbi->where;
1619
$where->param({price => 'a'});
1620
$where->if('length');
1621
$where->map(
1622
    id => ['book.id', {if => 'exists'}],
1623
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
1624
);
1625
is_deeply($where->param, {'book.price' => '%a'});
1626

            
1627
$where = $dbi->where;
1628
$where->param({id => [1, 2], author => 'Ken', price => 1900});
1629
$where->map(
1630
    id => 'book.id',
1631
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1632
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1633
);
1634
is_deeply($where->param, {'book.id' => [1, 2], 'book.author' => '%Ken%',
1635
  'book.price' => 1900});
1636

            
1637
$where = $dbi->where;
1638
$where->if('length');
1639
$where->param({id => ['', ''], author => 'Ken', price => 1900});
1640
$where->map(
1641
    id => 'book.id',
1642
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1643
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1644
);
1645
is_deeply($where->param, {'book.id' => [$dbi->not_exists, $dbi->not_exists], 'book.author' => '%Ken%',
1646
  'book.price' => 1900});
1647

            
1648
$where = $dbi->where;
1649
$where->param({id => ['', ''], author => 'Ken', price => 1900});
1650
$where->map(
1651
    id => ['book.id', {if => 'length'}],
1652
    author => ['book.author', sub { '%' . $_[0] . '%' }, {if => 'defined'}],
1653
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1654
);
1655
is_deeply($where->param, {'book.id' => [$dbi->not_exists, $dbi->not_exists], 'book.author' => '%Ken%',
1656
  'book.price' => 1900});
1657

            
1658
test 'dbi_option default';
1659
$dbi = DBIx::Custom->new;
1660
is_deeply($dbi->dbi_option, {});
1661

            
1662
test 'register_tag_processor';
1663
$dbi = DBIx::Custom->connect;
1664
$dbi->register_tag_processor(
1665
    a => sub { 1 }
1666
);
1667
is($dbi->query_builder->tag_processors->{a}->(), 1);
1668

            
1669
test 'register_tag';
1670
$dbi = DBIx::Custom->connect;
1671
$dbi->register_tag(
1672
    b => sub { 2 }
1673
);
1674
is($dbi->query_builder->tags->{b}->(), 2);
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;
1699
eval { $dbi->execute('drop table table1') };
1700
$dbi->execute($create_table1);
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', 
1704
                   {in => sub { $_[0] * 3 }, out => sub { $_[0] * 2 }});
1705
$rows = $dbi->select(table => 'table1', where => {key2 => 1})->all;
1706
is_deeply($rows, [{key1 => 1, key2 => 6}]);
1707

            
1708
$dbi = DBIx::Custom->connect;
1709
eval { $dbi->execute('drop table table1') };
1710
$dbi->execute($create_table1);
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}]);
1716

            
1717
$dbi = DBIx::Custom->connect;
1718
eval {$dbi->apply_filter('table1', 'key2', {out => 'no'})};
1719
like($@, qr/not registered/);
1720
eval {$dbi->apply_filter('table1', 'key2', {in => 'no'})};
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;
1729
eval { $dbi->execute('drop table table1') };
1730
$dbi->execute($create_table1);
1731
$dbi->register_filter(twice => sub { $_[0] * 2 });
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'}) };
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);
1753
eval {$dbi->execute('select * from table1 {} {= author') };
1754
like($@, qr/Tag not finished/);
1755

            
1756
$dbi = DBIx::Custom->connect;
1757
eval { $dbi->execute('drop table table1') };
1758
$dbi->execute($create_table1);
1759
$dbi->register_filter(one => sub { 1 });
1760
$result = $dbi->select(table => 'table1');
1761
eval {$result->filter(key1 => 'no')};
1762
like($@, qr/not registered/);
1763
eval {$result->end_filter(key1 => 'no')};
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');
cleanup test
Yuki Kimoto authored on 2011-08-08
1781
1;