DBIx-Custom / t / common.t /
Newer Older
1184 lines | 43.081kb
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/;
added common test executing ...
Yuki Kimoto authored on 2011-08-07
6

            
7
my $dbi;
8

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

            
13
plan 'no_plan';
14

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

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

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

            
59
# Drop table
60
eval { $dbi->execute('drop table table1') };
61

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

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

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

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

            
89
$result = $dbi->execute($query);
90
$rows = $result->fetch_all;
91
is_deeply($rows, [[1, 2], [3, 4]], "fetch_all");
92

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
284
eval{$dbi->insert(table => 'table', param => {';' => 1})};
285
like($@, qr/safety/);
286

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

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

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

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

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

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

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

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

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

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

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

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

            
394
eval{$dbi->update(table => 'table1', param => {';' => 1})};
395
like($@, qr/safety/);
396

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

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

            
411
eval {$dbi->update_all(table => 'table', param => {';' => 2}) };
412
like($@, qr/safety/);
413

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

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

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

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

            
459

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

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

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

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

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

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

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

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

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

            
531
eval{$dbi->delete(table => 'table1', where => {';' => 1})};
532
like($@, qr/safety/);
533

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

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

            
554

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

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

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

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

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

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

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

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

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

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

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

            
625
test 'filters';
626
$dbi = DBIx::Custom->new;
627

            
628
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')),
629
   'あ', "decode_utf8");
630

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

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

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

            
653
$result = $dbi->select(table => 'table1');
654
ok(! $result->fetch_first, "rollback");
655

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

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

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

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

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

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

            
706

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

            
712
$dbi->begin_work;
713

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

            
720
$dbi->rollback if $@;
721

            
722
$result = $dbi->select(table => 'table1');
723
$rows = $result->all;
724
is_deeply($rows, [], "rollback");
725

            
726
$dbi->begin_work;
727

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

            
733
$dbi->commit unless $@;
734

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

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

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

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

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

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

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

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

            
794

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

            
800
$dbi->begin_work;
801

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

            
808
$dbi->rollback if $@;
809

            
810
$result = $dbi->select(table => 'table1');
811
$rows = $result->all;
812
is_deeply($rows, [], "rollback");
813

            
814
$dbi->begin_work;
815

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

            
821
$dbi->commit unless $@;
822

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

            
827
$dbi->dbh->{AutoCommit} = 0;
828
eval{ $dbi->begin_work };
829
ok($@, "exception");
830
$dbi->dbh->{AutoCommit} = 1;
831

            
832

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

            
847
is($dbi->one, 1, "first");
848
is($dbi->two, 2, "second");
849
is($dbi->twice(5), 10 , "second");
850

            
851
eval {$dbi->XXXXXX};
852
ok($@, "not exists");
853

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

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

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

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

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

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

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

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

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

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

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

            
986
test 'each_column';
987
$dbi = DBIx::Custom->connect;
988
eval { $dbi->execute("drop table ${q}table$p") };
989
eval { $dbi->execute('drop table table1') };
990
eval { $dbi->execute('drop table table2') };
991
$dbi->execute($create_table1_type);
992
$dbi->execute($create_table2);
993

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

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

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

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

            
1092
$dbi = MyDBI->connect;
1093
eval { $dbi->execute('drop table table1') };
1094
$dbi->execute($create_table1);
1095
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1096
is($dbi->select(table => 'table1')->one->{key1}, 1);
1097

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

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

            
1117
$dbi = MyDBI->connect;
1118
eval { $dbi->execute('drop table table1') };
1119
$dbi->execute($create_table1);
1120
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1121
is($dbi->select(table => 'table1')->one->{key1}, 1);
1122

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

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

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

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

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

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

            
1183

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