DBIx-Custom / t / common.t /
Newer Older
761 lines | 29.972kb
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;
cleanup test
Yuki Kimoto authored on 2011-08-10
21
my $q = substr($dbi->quote, 0, 1);
22
my $p = substr($dbi->quote, 1, 1) || $q;
added common test executing ...
Yuki Kimoto authored on 2011-08-07
23

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

            
56
# Drop table
57
eval { $dbi->execute('drop table table1') };
58

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

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

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

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

            
86
$result = $dbi->execute($query);
87
$rows = $result->fetch_all;
88
is_deeply($rows, [[1, 2], [3, 4]], "fetch_all");
89

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

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

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

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

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

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

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

            
139
$source = "select * from table1 where key1 = :key1 and {<> key2} and {< key3} and {> key4} and {>= key5};";
140
$query = $dbi->execute($source, {}, query => 1);
141
$result = $dbi->execute($query, param => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
142
$rows = $result->all;
143
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1");
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, {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} and {like key2};";
152
$query = $dbi->execute($source, {}, query => 1);
153
$result = $dbi->execute($query, param => {key1 => 1, key2 => '%2%'});
154
$rows = $result->all;
155
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag2");
156

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
281
eval{$dbi->insert(table => 'table', param => {';' => 1})};
282
like($@, qr/safety/);
283

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
301
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
302
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
303
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
304
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace');
305
$result = $dbi->execute('select * from table1;');
306
$rows   = $result->all;
307
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
308

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

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

            
339
$dbi->update(table => 'table1', param => {key2 => [12]}, where => {key2 => 2, key3 => 3});
340
$result = $dbi->execute('select * from table1;');
341
$rows   = $result->all;
342
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
343
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
344
                  "update key same as search key : param is array ref");
345

            
346
$dbi->execute("delete from table1");
347
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
348
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
349
$dbi->register_filter(twice => sub { $_[0] * 2 });
350
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1},
351
              filter => {key2 => sub { $_[0] * 2 }});
352
$result = $dbi->execute('select * from table1;');
353
$rows   = $result->all;
354
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5},
355
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
356
                  "filter");
357

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

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

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

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

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

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

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

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

            
406
eval { $dbi->execute('drop table table1') };
407
$dbi->quote('"');
408
eval { $dbi->execute("drop table ${q}table$p") };
409
$dbi->execute("create table ${q}table$p (${q}select$p, ${q}update$p)");
410
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
411
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
412
$dbi->insert(table => 'table', param => {select => 1});
413
$dbi->update(table => 'table', where => {select => 1}, param => {update => 2});
414
$result = $dbi->execute("select * from ${q}table$p");
415
$rows   = $result->all;
416
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
417

            
418
eval {$dbi->update_all(table => 'table', param => {';' => 2}) };
419
like($@, qr/safety/);
420

            
421
eval { $dbi->execute("drop table ${q}table$p") };
422
$dbi->reserved_word_quote('"');
423
$dbi->execute("create table ${q}table$p (${q}select$p, ${q}update$p)");
424
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
425
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
426
$dbi->insert(table => 'table', param => {select => 1});
427
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
428
$result = $dbi->execute("select * from ${q}table$p");
429
$rows   = $result->all;
430
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
431

            
432
eval { $dbi->execute('drop table table1') };
433
$dbi->execute($create_table1_2);
434
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
435
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
436
$dbi->update({key2 => 11}, table => 'table1', where => {key1 => 1});
437
$result = $dbi->execute('select * from table1;');
438
$rows   = $result->all;
439
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
440
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
441
                  "basic");
442

            
443
eval { $dbi->execute('drop table table1') };
444
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
445
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
446
$dbi->update(table => 'table1', param => {key2 => 4},
447
  where => {key1 => 1}, prefix => 'or replace');
448
$result = $dbi->execute('select * from table1;');
449
$rows   = $result->all;
450
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
451

            
452
eval { $dbi->execute('drop table table1') };
453
$dbi->execute($create_table1_2);
454
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
455
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
456
$dbi->update(table => 'table1', param => {key2 => \"'11'"}, where => {key1 => 1});
457
$result = $dbi->execute('select * from table1;');
458
$rows   = $result->all;
459
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
460
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
461
                  "basic");
462

            
463
test 'update_all';
464
eval { $dbi->execute('drop table table1') };
465
$dbi->execute($create_table1_2);
466
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
467
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
468
$dbi->register_filter(twice => sub { $_[0] * 2 });
469
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'});
470
$result = $dbi->execute('select * from table1;');
471
$rows   = $result->all;
472
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
473
                  {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
474
                  "filter");
475

            
476

            
477
test 'delete';
478
eval { $dbi->execute('drop table table1') };
479
$dbi->execute($create_table1);
480
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
481
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
482
$dbi->delete(table => 'table1', where => {key1 => 1});
483
$result = $dbi->execute('select * from table1;');
484
$rows   = $result->all;
485
is_deeply($rows, [{key1 => 3, key2 => 4}], "basic");
486

            
487
$dbi->execute("delete from table1;");
488
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
489
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
490
$dbi->register_filter(twice => sub { $_[0] * 2 });
491
$dbi->delete(table => 'table1', where => {key2 => 1}, filter => {key2 => 'twice'});
492
$result = $dbi->execute('select * from table1;');
493
$rows   = $result->all;
494
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter");
495

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

            
498
$dbi->delete_all(table => 'table1');
499
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
500
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
501
$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2});
502
$rows = $dbi->select(table => 'table1')->all;
503
is_deeply($rows, [{key1 => 3, key2 => 4}], "delete multi key");
504

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

            
508
eval { $dbi->execute('drop table table1') };
509
$dbi->execute($create_table1);
510
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
511
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
512
$where = $dbi->where;
513
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
514
$where->param({ke1 => 1, key2 => 2});
515
$dbi->delete(table => 'table1', where => $where);
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') };
520
$dbi->execute($create_table1);
521
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
522
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
523
$dbi->delete(
524
    table => 'table1',
525
    where => [
526
        ['and', 'key1 = :key1', 'key2 = :key2'],
527
        {ke1 => 1, key2 => 2}
528
    ]
529
);
530
$result = $dbi->select(table => 'table1');
531
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
532

            
533
eval { $dbi->execute('drop table table1') };
534
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
535
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
536
$dbi->delete(table => 'table1', where => {key1 => 1}, prefix => '    ');
537
$result = $dbi->execute('select * from table1;');
538
$rows   = $result->all;
539
is_deeply($rows, [], "basic");
540

            
541
test 'delete error';
542
eval { $dbi->execute('drop table table1') };
543
$dbi->execute($create_table1);
544
eval{$dbi->delete(table => 'table1')};
545
like($@, qr/"where" must be specified/,
546
         "where key-value pairs not specified");
547

            
548
eval{$dbi->delete(table => 'table1', where => {';' => 1})};
549
like($@, qr/safety/);
550

            
551
$dbi = DBIx::Custom->connect;
552
$dbi->quote('"');
553
eval { $dbi->execute("drop table ${q}table$p") };
554
$dbi->execute("create table ${q}table$p (${q}select$p, ${q}update$p)");
555
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
556
$dbi->insert(table => 'table', param => {select => 1});
557
$dbi->delete(table => 'table', where => {select => 1});
558
$result = $dbi->execute("select * from ${q}table$p");
559
$rows   = $result->all;
560
is_deeply($rows, [], "reserved word");
561

            
562
test 'delete_all';
563
eval { $dbi->execute('drop table table1') };
564
$dbi->execute($create_table1);
565
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
566
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
567
$dbi->delete_all(table => 'table1');
568
$result = $dbi->execute('select * from table1;');
569
$rows   = $result->all;
570
is_deeply($rows, [], "basic");
571

            
572

            
573
test 'select';
574
eval { $dbi->execute('drop table table1') };
575
$dbi->execute($create_table1);
576
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
577
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
578
$rows = $dbi->select(table => 'table1')->all;
579
is_deeply($rows, [{key1 => 1, key2 => 2},
580
                  {key1 => 3, key2 => 4}], "table");
581

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

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

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

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

            
594
$dbi->register_filter(decrement => sub { $_[0] - 1 });
595
$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'})
596
            ->all;
597
is_deeply($rows, [{key1 => 1, key2 => 2}], "filter");
598

            
599
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
600
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
601
$rows = $dbi->select(
602
    table => [qw/table1 table2/],
603
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
604
    where   => {'table1.key2' => 2},
605
    relation  => {'table1.key1' => 'table2.key1'}
606
)->all;
607
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : exists where");
608

            
609
$rows = $dbi->select(
610
    table => [qw/table1 table2/],
611
    column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
612
    relation  => {'table1.key1' => 'table2.key1'}
613
)->all;
614
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : no exists where");
615

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

            
619
$dbi = DBIx::Custom->connect;
620
$dbi->quote('"');
621
$dbi->execute("create table ${q}table$p (${q}select$p, ${q}update$p)");
622
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
623
$dbi->insert(table => 'table', param => {select => 1, update => 2});
624
$result = $dbi->select(table => 'table', where => {select => 1});
625
$rows   = $result->all;
626
is_deeply($rows, [{select => 2, update => 2}], "reserved word");
627

            
628
test 'fetch filter';
629
eval { $dbi->execute('drop table table1') };
630
$dbi->register_filter(
631
    twice       => sub { $_[0] * 2 },
632
    three_times => sub { $_[0] * 3 }
633
);
634
$dbi->default_fetch_filter('twice');
635
$dbi->execute($create_table1);
636
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
637
$result = $dbi->select(table => 'table1');
638
$result->filter({key1 => 'three_times'});
639
$row = $result->one;
640
is_deeply($row, {key1 => 3, key2 => 4}, "default_fetch_filter and filter");
641

            
642
test 'filters';
643
$dbi = DBIx::Custom->new;
644

            
645
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')),
646
   'あ', "decode_utf8");
647

            
648
is($dbi->filters->{encode_utf8}->('あ'),
649
   encode_utf8('あ'), "encode_utf8");
650

            
651
test 'transaction';
652
$dbi = DBIx::Custom->connect;
653
eval { $dbi->execute('drop table table1') };
654
$dbi->execute($create_table1);
655
$dbi->dbh->begin_work;
656
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
657
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
658
$dbi->dbh->commit;
659
$result = $dbi->select(table => 'table1');
660
is_deeply(scalar $result->all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}],
661
          "commit");
662

            
663
$dbi = DBIx::Custom->connect;
664
eval { $dbi->execute('drop table table1') };
665
$dbi->execute($create_table1);
666
$dbi->dbh->begin_work(0);
667
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
668
$dbi->dbh->rollback;
669

            
670
$result = $dbi->select(table => 'table1');
671
ok(! $result->fetch_first, "rollback");
672

            
673
test 'cache';
674
eval { $dbi->execute('drop table table1') };
675
$dbi->cache(1);
676
$dbi->execute($create_table1);
677
$source = 'select * from table1 where key1 = :key1 and key2 = :key2;';
678
$dbi->execute($source, {}, query => 1);
679
is_deeply($dbi->{_cached}->{$source}, 
680
          {sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2'], tables => []}, "cache");
681

            
682
eval { $dbi->execute('drop table table1') };
683
$dbi->execute($create_table1);
684
$dbi->{_cached} = {};
685
$dbi->cache(0);
686
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
687
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
688

            
689
test 'execute';
690
eval { $dbi->execute('drop table table1') };
691
$dbi->execute($create_table1);
692
{
693
    local $Carp::Verbose = 0;
694
    eval{$dbi->execute('select * frm table1')};
695
    like($@, qr/\Qselect * frm table1;/, "fail prepare");
696
    like($@, qr/\.t /, "fail : not verbose");
697
}
698
{
699
    local $Carp::Verbose = 1;
700
    eval{$dbi->execute('select * frm table1')};
701
    like($@, qr/Custom.*\.t /s, "fail : verbose");
702
}
703

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

            
707
$query = $dbi->execute('select * from table1 where key1 = :key1', {}, query => 1);
708
$dbi->dbh->disconnect;
709
eval{$dbi->execute($query, param => {key1 => {a => 1}})};
710
ok($@, "execute fail");
711

            
712
{
713
    local $Carp::Verbose = 0;
714
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
715
    like($@, qr/\Q.t /, "caller spec : not vebose");
716
}
717
{
718
    local $Carp::Verbose = 1;
719
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
720
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
721
}
722

            
723

            
724
test 'transaction';
725
$dbi = DBIx::Custom->connect;
726
eval { $dbi->execute('drop table table1') };
727
$dbi->execute($create_table1);
728

            
729
$dbi->begin_work;
730

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

            
737
$dbi->rollback if $@;
738

            
739
$result = $dbi->select(table => 'table1');
740
$rows = $result->all;
741
is_deeply($rows, [], "rollback");
742

            
743
$dbi->begin_work;
744

            
745
eval {
746
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
747
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
748
};
749

            
750
$dbi->commit unless $@;
751

            
752
$result = $dbi->select(table => 'table1');
753
$rows = $result->all;
754
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "commit");
755

            
756
$dbi->dbh->{AutoCommit} = 0;
757
eval{ $dbi->begin_work };
758
ok($@, "exception");
759
$dbi->dbh->{AutoCommit} = 1;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
760

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