DBIx-Custom / t / sqlite.t /
Newer Older
3707 lines | 116.768kb
cleanup test
Yuki Kimoto authored on 2011-08-06
1
use Test::More;
2
use strict;
3
use warnings;
4
use utf8;
5
use Encode qw/encode_utf8 decode_utf8/;
test cleanup
Yuki Kimoto authored on 2011-08-06
6
use FindBin;
7
use lib "$FindBin::Bin/basic";
cleanup test
Yuki Kimoto authored on 2011-08-06
8

            
9
BEGIN {
10
    eval { require DBD::SQLite; 1 }
11
        or plan skip_all => 'DBD::SQLite required';
12
    eval { DBD::SQLite->VERSION >= 1.25 }
13
        or plan skip_all => 'DBD::SQLite >= 1.25 required';
14

            
15
    plan 'no_plan';
16
    use_ok('DBIx::Custom');
17
}
18

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
22
# Constant
23
my %memory = (dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-10
24
my $create_table1 = 'create table table1 (key1 char(255), key2 char(255));';
25
my $create_table1_2 = 'create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));';
test cleanup
Yuki Kimoto authored on 2011-08-10
26
my $q = '"';
27
my $p = '"';
cleanup test
Yuki Kimoto authored on 2011-08-06
28

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

            
60
# Prepare table
cleanup test
Yuki Kimoto authored on 2011-08-06
61
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
62

            
test cleanup
Yuki Kimoto authored on 2011-08-10
63
=pod
cleanup test
Yuki Kimoto authored on 2011-08-06
64

            
65
test 'insert';
test cleanup
Yuki Kimoto authored on 2011-08-10
66
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
67
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
68
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
69
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-06
70
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
71
$rows   = $result->all;
72
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
73

            
74
$dbi->execute('delete from table1');
75
$dbi->register_filter(
76
    twice       => sub { $_[0] * 2 },
77
    three_times => sub { $_[0] * 3 }
78
);
79
$dbi->default_bind_filter('twice');
80
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => 'three_times'});
test cleanup
Yuki Kimoto authored on 2011-08-06
81
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
82
$rows   = $result->all;
83
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter");
84
$dbi->default_bind_filter(undef);
85

            
cleanup test
Yuki Kimoto authored on 2011-08-06
86
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
87
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
88
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, append => '   ');
89
$rows = $dbi->select(table => 'table1')->all;
90
is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append');
91

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

            
95
eval{$dbi->insert(table => 'table', param => {';' => 1})};
96
like($@, qr/safety/);
97

            
98
$dbi->quote('"');
99
$dbi->execute('create table "table" ("select")');
100
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
101
$dbi->insert(table => 'table', param => {select => 1});
102
$result = $dbi->execute('select * from "table"');
103
$rows   = $result->all;
104
is_deeply($rows, [{select => 2}], "reserved word");
105

            
test cleanup
Yuki Kimoto authored on 2011-08-10
106
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
107
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
108
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
109
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
test cleanup
Yuki Kimoto authored on 2011-08-06
110
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
111
$rows   = $result->all;
112
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
113

            
test cleanup
Yuki Kimoto authored on 2011-08-10
114
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
115
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
116
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
117
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace');
test cleanup
Yuki Kimoto authored on 2011-08-06
118
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
119
$rows   = $result->all;
120
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
121

            
test cleanup
Yuki Kimoto authored on 2011-08-10
122
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
123
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
124
$dbi->insert(table => 'table1', param => {key1 => \"'1'", key2 => 2});
125
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-06
126
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
127
$rows   = $result->all;
128
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
129

            
test cleanup
Yuki Kimoto authored on 2011-08-10
130
=cut
131

            
cleanup test
Yuki Kimoto authored on 2011-08-06
132
test 'update';
test cleanup
Yuki Kimoto authored on 2011-08-10
133
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
134
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
135
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
136
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
137
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
138
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
139
$rows   = $result->all;
140
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
141
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
142
                  "basic");
143
                  
144
$dbi->execute("delete from table1");
145
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
146
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
147
$dbi->update(table => 'table1', param => {key2 => 12}, where => {key2 => 2, key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-06
148
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
149
$rows   = $result->all;
150
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
151
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
152
                  "update key same as search key");
153

            
154
$dbi->update(table => 'table1', param => {key2 => [12]}, where => {key2 => 2, key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-06
155
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
156
$rows   = $result->all;
157
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
158
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
159
                  "update key same as search key : param is array ref");
160

            
161
$dbi->execute("delete from table1");
162
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
163
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
164
$dbi->register_filter(twice => sub { $_[0] * 2 });
165
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1},
166
              filter => {key2 => sub { $_[0] * 2 }});
test cleanup
Yuki Kimoto authored on 2011-08-06
167
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
168
$rows   = $result->all;
169
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5},
170
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
171
                  "filter");
172

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

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

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
181
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
182
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
183
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
184
$where = $dbi->where;
185
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
186
$where->param({key1 => 1, key2 => 2});
187
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
188
$result = $dbi->select(table => 'table1');
189
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
190

            
test cleanup
Yuki Kimoto authored on 2011-08-10
191
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
192
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
193
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
194
$dbi->update(
195
    table => 'table1',
196
    param => {key1 => 3},
197
    where => [
198
        ['and', 'key1 = :key1', 'key2 = :key2'],
199
        {key1 => 1, key2 => 2}
200
    ]
201
);
202
$result = $dbi->select(table => 'table1');
203
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
204

            
test cleanup
Yuki Kimoto authored on 2011-08-10
205
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
206
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
207
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
208
$where = $dbi->where;
209
$where->clause(['and', 'key2 = :key2']);
210
$where->param({key2 => 2});
211
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
212
$result = $dbi->select(table => 'table1');
213
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
214

            
215
eval{$dbi->update(table => 'table1', param => {';' => 1})};
216
like($@, qr/safety/);
217

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
221
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
222
$dbi->quote('"');
223
$dbi->execute('create table "table" ("select", "update")');
224
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
225
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
226
$dbi->insert(table => 'table', param => {select => 1});
227
$dbi->update(table => 'table', where => {select => 1}, param => {update => 2});
228
$result = $dbi->execute('select * from "table"');
229
$rows   = $result->all;
230
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
231

            
232
eval {$dbi->update_all(table => 'table', param => {';' => 2}) };
233
like($@, qr/safety/);
234

            
test cleanup
Yuki Kimoto authored on 2011-08-10
235
eval { $dbi->execute("drop table ${q}table$p") };
cleanup test
Yuki Kimoto authored on 2011-08-06
236
$dbi->reserved_word_quote('"');
237
$dbi->execute('create table "table" ("select", "update")');
238
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
239
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
240
$dbi->insert(table => 'table', param => {select => 1});
241
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
242
$result = $dbi->execute('select * from "table"');
243
$rows   = $result->all;
244
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
245

            
test cleanup
Yuki Kimoto authored on 2011-08-10
246
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
247
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
248
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
249
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
250
$dbi->update({key2 => 11}, table => 'table1', where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
251
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
252
$rows   = $result->all;
253
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
254
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
255
                  "basic");
256

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
266
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
267
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
268
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
269
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
270
$dbi->update(table => 'table1', param => {key2 => \"'11'"}, where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
271
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
272
$rows   = $result->all;
273
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
274
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
275
                  "basic");
276

            
277
test 'update_all';
test cleanup
Yuki Kimoto authored on 2011-08-10
278
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
279
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
280
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
281
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
282
$dbi->register_filter(twice => sub { $_[0] * 2 });
283
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'});
test cleanup
Yuki Kimoto authored on 2011-08-06
284
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
285
$rows   = $result->all;
286
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
287
                  {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
288
                  "filter");
289

            
290

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

            
301
$dbi->execute("delete from table1;");
302
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
303
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
304
$dbi->register_filter(twice => sub { $_[0] * 2 });
305
$dbi->delete(table => 'table1', where => {key2 => 1}, filter => {key2 => 'twice'});
test cleanup
Yuki Kimoto authored on 2011-08-06
306
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
307
$rows   = $result->all;
308
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter");
309

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

            
312
$dbi->delete_all(table => 'table1');
313
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
314
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
315
$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2});
316
$rows = $dbi->select(table => 'table1')->all;
317
is_deeply($rows, [{key1 => 3, key2 => 4}], "delete multi key");
318

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
322
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
323
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
324
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
325
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
326
$where = $dbi->where;
327
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
328
$where->param({ke1 => 1, key2 => 2});
329
$dbi->delete(table => 'table1', where => $where);
330
$result = $dbi->select(table => 'table1');
331
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
332

            
test cleanup
Yuki Kimoto authored on 2011-08-10
333
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
334
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
335
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
336
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
337
$dbi->delete(
338
    table => 'table1',
339
    where => [
340
        ['and', 'key1 = :key1', 'key2 = :key2'],
341
        {ke1 => 1, key2 => 2}
342
    ]
343
);
344
$result = $dbi->select(table => 'table1');
345
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
346

            
test cleanup
Yuki Kimoto authored on 2011-08-10
347
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
348
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
349
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
350
$dbi->delete(table => 'table1', where => {key1 => 1}, prefix => '    ');
test cleanup
Yuki Kimoto authored on 2011-08-06
351
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
352
$rows   = $result->all;
353
is_deeply($rows, [], "basic");
354

            
355
test 'delete error';
test cleanup
Yuki Kimoto authored on 2011-08-10
356
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
357
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
358
eval{$dbi->delete(table => 'table1')};
359
like($@, qr/"where" must be specified/,
360
         "where key-value pairs not specified");
361

            
362
eval{$dbi->delete(table => 'table1', where => {';' => 1})};
363
like($@, qr/safety/);
364

            
cleanup test
Yuki Kimoto authored on 2011-08-06
365
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
366
$dbi->quote('"');
367
$dbi->execute('create table "table" ("select", "update")');
368
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
369
$dbi->insert(table => 'table', param => {select => 1});
370
$dbi->delete(table => 'table', where => {select => 1});
371
$result = $dbi->execute('select * from "table"');
372
$rows   = $result->all;
373
is_deeply($rows, [], "reserved word");
374

            
375
test 'delete_all';
test cleanup
Yuki Kimoto authored on 2011-08-10
376
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
377
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
378
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
379
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
380
$dbi->delete_all(table => 'table1');
test cleanup
Yuki Kimoto authored on 2011-08-06
381
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
382
$rows   = $result->all;
383
is_deeply($rows, [], "basic");
384

            
385

            
386
test 'select';
test cleanup
Yuki Kimoto authored on 2011-08-10
387
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
388
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
389
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
390
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
391
$rows = $dbi->select(table => 'table1')->all;
392
is_deeply($rows, [{key1 => 1, key2 => 2},
393
                  {key1 => 3, key2 => 4}], "table");
394

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

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

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

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

            
407
$dbi->register_filter(decrement => sub { $_[0] - 1 });
408
$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'})
409
            ->all;
410
is_deeply($rows, [{key1 => 1, key2 => 2}], "filter");
411

            
cleanup test
Yuki Kimoto authored on 2011-08-06
412
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
413
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
414
$rows = $dbi->select(
415
    table => [qw/table1 table2/],
416
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
417
    where   => {'table1.key2' => 2},
418
    relation  => {'table1.key1' => 'table2.key1'}
419
)->all;
420
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : exists where");
421

            
422
$rows = $dbi->select(
423
    table => [qw/table1 table2/],
424
    column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
425
    relation  => {'table1.key1' => 'table2.key1'}
426
)->all;
427
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : no exists where");
428

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
432
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
433
$dbi->quote('"');
434
$dbi->execute('create table "table" ("select", "update")');
435
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
436
$dbi->insert(table => 'table', param => {select => 1, update => 2});
437
$result = $dbi->select(table => 'table', where => {select => 1});
438
$rows   = $result->all;
439
is_deeply($rows, [{select => 2, update => 2}], "reserved word");
440

            
441
test 'fetch filter';
test cleanup
Yuki Kimoto authored on 2011-08-10
442
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
443
$dbi->register_filter(
444
    twice       => sub { $_[0] * 2 },
445
    three_times => sub { $_[0] * 3 }
446
);
447
$dbi->default_fetch_filter('twice');
cleanup test
Yuki Kimoto authored on 2011-08-10
448
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
449
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
450
$result = $dbi->select(table => 'table1');
451
$result->filter({key1 => 'three_times'});
452
$row = $result->one;
453
is_deeply($row, {key1 => 3, key2 => 4}, "default_fetch_filter and filter");
454

            
455
test 'filters';
456
$dbi = DBIx::Custom->new;
457

            
458
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')),
459
   'あ', "decode_utf8");
460

            
461
is($dbi->filters->{encode_utf8}->('あ'),
462
   encode_utf8('あ'), "encode_utf8");
463

            
464
test 'transaction';
cleanup test
Yuki Kimoto authored on 2011-08-06
465
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
466
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
467
$dbi->dbh->begin_work;
468
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
469
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
470
$dbi->dbh->commit;
471
$result = $dbi->select(table => 'table1');
472
is_deeply(scalar $result->all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}],
473
          "commit");
474

            
cleanup test
Yuki Kimoto authored on 2011-08-06
475
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
476
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
477
$dbi->dbh->begin_work(0);
478
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
479
$dbi->dbh->rollback;
480

            
481
$result = $dbi->select(table => 'table1');
482
ok(! $result->fetch_first, "rollback");
483

            
484
test 'cache';
cleanup test
Yuki Kimoto authored on 2011-08-06
485
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
486
$dbi->cache(1);
cleanup test
Yuki Kimoto authored on 2011-08-10
487
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
488
$source = 'select * from table1 where key1 = :key1 and key2 = :key2;';
489
$dbi->execute($source, {}, query => 1);
490
is_deeply($dbi->{_cached}->{$source}, 
491
          {sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2'], tables => []}, "cache");
492

            
cleanup test
Yuki Kimoto authored on 2011-08-06
493
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
494
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
495
$dbi->{_cached} = {};
496
$dbi->cache(0);
497
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
498
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
499

            
500
test 'execute';
cleanup test
Yuki Kimoto authored on 2011-08-06
501
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
502
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
503
{
504
    local $Carp::Verbose = 0;
505
    eval{$dbi->execute('select * frm table1')};
506
    like($@, qr/\Qselect * frm table1;/, "fail prepare");
507
    like($@, qr/\.t /, "fail : not verbose");
508
}
509
{
510
    local $Carp::Verbose = 1;
511
    eval{$dbi->execute('select * frm table1')};
512
    like($@, qr/Custom.*\.t /s, "fail : verbose");
513
}
514

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

            
518
$query = $dbi->execute('select * from table1 where key1 = :key1', {}, query => 1);
519
$dbi->dbh->disconnect;
520
eval{$dbi->execute($query, param => {key1 => {a => 1}})};
521
ok($@, "execute fail");
522

            
523
{
524
    local $Carp::Verbose = 0;
525
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
526
    like($@, qr/\Q.t /, "caller spec : not vebose");
527
}
528
{
529
    local $Carp::Verbose = 1;
530
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
531
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
532
}
533

            
534

            
535
test 'transaction';
cleanup test
Yuki Kimoto authored on 2011-08-06
536
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
537
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
538

            
539
$dbi->begin_work;
540

            
541
eval {
542
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
543
    die "Error";
544
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
545
};
546

            
547
$dbi->rollback if $@;
548

            
549
$result = $dbi->select(table => 'table1');
550
$rows = $result->all;
551
is_deeply($rows, [], "rollback");
552

            
553
$dbi->begin_work;
554

            
555
eval {
556
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
557
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
558
};
559

            
560
$dbi->commit unless $@;
561

            
562
$result = $dbi->select(table => 'table1');
563
$rows = $result->all;
564
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "commit");
565

            
566
$dbi->dbh->{AutoCommit} = 0;
567
eval{ $dbi->begin_work };
568
ok($@, "exception");
569
$dbi->dbh->{AutoCommit} = 1;
570

            
571

            
572
test 'method';
cleanup test
Yuki Kimoto authored on 2011-08-06
573
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
574
$dbi->method(
575
    one => sub { 1 }
576
);
577
$dbi->method(
578
    two => sub { 2 }
579
);
580
$dbi->method({
581
    twice => sub {
582
        my $self = shift;
583
        return $_[0] * 2;
584
    }
585
});
586

            
587
is($dbi->one, 1, "first");
588
is($dbi->two, 2, "second");
589
is($dbi->twice(5), 10 , "second");
590

            
591
eval {$dbi->XXXXXX};
592
ok($@, "not exists");
593

            
594
test 'out filter';
cleanup test
Yuki Kimoto authored on 2011-08-06
595
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
596
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
597
$dbi->register_filter(twice => sub { $_[0] * 2 });
598
$dbi->register_filter(three_times => sub { $_[0] * 3});
599
$dbi->apply_filter(
600
    'table1', 'key1' => {out => 'twice', in => 'three_times'}, 
601
              'key2' => {out => 'three_times', in => 'twice'});
602
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-06
603
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
604
$row   = $result->fetch_hash_first;
605
is_deeply($row, {key1 => 2, key2 => 6}, "insert");
606
$result = $dbi->select(table => 'table1');
607
$row   = $result->one;
608
is_deeply($row, {key1 => 6, key2 => 12}, "insert");
609

            
cleanup test
Yuki Kimoto authored on 2011-08-06
610
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
611
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
612
$dbi->register_filter(twice => sub { $_[0] * 2 });
613
$dbi->register_filter(three_times => sub { $_[0] * 3});
614
$dbi->apply_filter(
615
    'table1', 'key1' => {out => 'twice', in => 'three_times'}, 
616
              'key2' => {out => 'three_times', in => 'twice'});
617
$dbi->apply_filter(
618
    'table1', 'key1' => {out => undef}
619
); 
620
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-06
621
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
622
$row   = $result->one;
623
is_deeply($row, {key1 => 1, key2 => 6}, "insert");
624

            
cleanup test
Yuki Kimoto authored on 2011-08-06
625
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
626
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
627
$dbi->register_filter(twice => sub { $_[0] * 2 });
628
$dbi->apply_filter(
629
    'table1', 'key1' => {out => 'twice', in => 'twice'}
630
);
631
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => undef});
632
$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-06
633
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
634
$row   = $result->one;
635
is_deeply($row, {key1 => 4, key2 => 2}, "update");
636

            
cleanup test
Yuki Kimoto authored on 2011-08-06
637
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
638
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
639
$dbi->register_filter(twice => sub { $_[0] * 2 });
640
$dbi->apply_filter(
641
    'table1', 'key1' => {out => 'twice', in => 'twice'}
642
);
643
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1=> undef});
644
$dbi->delete(table => 'table1', where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
645
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
646
$rows   = $result->all;
647
is_deeply($rows, [], "delete");
648

            
cleanup test
Yuki Kimoto authored on 2011-08-06
649
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
650
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
651
$dbi->register_filter(twice => sub { $_[0] * 2 });
652
$dbi->apply_filter(
653
    'table1', 'key1' => {out => 'twice', in => 'twice'}
654
);
655
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
656
$result = $dbi->select(table => 'table1', where => {key1 => 1});
657
$result->filter({'key2' => 'twice'});
658
$rows   = $result->all;
659
is_deeply($rows, [{key1 => 4, key2 => 4}], "select");
660

            
cleanup test
Yuki Kimoto authored on 2011-08-06
661
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
662
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
663
$dbi->register_filter(twice => sub { $_[0] * 2 });
664
$dbi->apply_filter(
665
    'table1', 'key1' => {out => 'twice', in => 'twice'}
666
);
667
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
668
$result = $dbi->execute("select * from table1 where key1 = :key1 and key2 = :key2;",
669
                        param => {key1 => 1, key2 => 2},
670
                        table => ['table1']);
671
$rows   = $result->all;
672
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute");
673

            
cleanup test
Yuki Kimoto authored on 2011-08-06
674
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
675
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
676
$dbi->register_filter(twice => sub { $_[0] * 2 });
677
$dbi->apply_filter(
678
    'table1', 'key1' => {out => 'twice', in => 'twice'}
679
);
680
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
681
$result = $dbi->execute("select * from {table table1} where key1 = :key1 and key2 = :key2;",
682
                        param => {key1 => 1, key2 => 2});
683
$rows   = $result->all;
684
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute table tag");
685

            
cleanup test
Yuki Kimoto authored on 2011-08-06
686
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
687
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
688
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
689
$dbi->register_filter(twice => sub { $_[0] * 2 });
690
$dbi->register_filter(three_times => sub { $_[0] * 3 });
691
$dbi->apply_filter(
692
    'table1', 'key2' => {out => 'twice', in => 'twice'}
693
);
694
$dbi->apply_filter(
695
    'table2', 'key3' => {out => 'three_times', in => 'three_times'}
696
);
697
$dbi->insert(table => 'table1', param => {key1 => 5, key2 => 2}, filter => {key2 => undef});
698
$dbi->insert(table => 'table2', param => {key1 => 5, key3 => 6}, filter => {key3 => undef});
699
$result = $dbi->select(
700
     table => ['table1', 'table2'],
701
     column => ['key2', 'key3'],
702
     where => {'table1.key2' => 1, 'table2.key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
703

            
704
$result->filter({'key2' => 'twice'});
705
$rows   = $result->all;
706
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join");
707

            
708
$result = $dbi->select(
709
     table => ['table1', 'table2'],
710
     column => ['key2', 'key3'],
711
     where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
712

            
713
$result->filter({'key2' => 'twice'});
714
$rows   = $result->all;
715
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join : omit");
716

            
717
test 'each_column';
cleanup test
Yuki Kimoto authored on 2011-08-06
718
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
719
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
720
$dbi->execute('create table table1 (key1 Date, key2 datetime);');
cleanup test
Yuki Kimoto authored on 2011-08-06
721

            
722
$infos = [];
723
$dbi->each_column(sub {
724
    my ($self, $table, $column, $cinfo) = @_;
725
    
726
    if ($table =~ /^table/) {
727
         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
728
         push @$infos, $info;
729
    }
730
});
731
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
732
is_deeply($infos, 
733
    [
734
        ['table1', 'key1', 'key1'],
735
        ['table1', 'key2', 'key2'],
736
        ['table2', 'key1', 'key1'],
737
        ['table2', 'key3', 'key3']
738
    ]
739
    
740
);
741
test 'each_table';
cleanup test
Yuki Kimoto authored on 2011-08-06
742
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
743
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
744
$dbi->execute('create table table1 (key1 Date, key2 datetime);');
cleanup test
Yuki Kimoto authored on 2011-08-06
745

            
746
$infos = [];
747
$dbi->each_table(sub {
748
    my ($self, $table, $table_info) = @_;
749
    
750
    if ($table =~ /^table/) {
751
         my $info = [$table, $table_info->{TABLE_NAME}];
752
         push @$infos, $info;
753
    }
754
});
755
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
756
is_deeply($infos, 
757
    [
758
        ['table1', 'table1'],
759
        ['table2', 'table2'],
760
    ]
761
);
762

            
763
test 'limit';
cleanup test
Yuki Kimoto authored on 2011-08-06
764
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
765
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
766
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
767
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
768
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
769
$dbi->register_tag(
770
    limit => sub {
771
        my ($count, $offset) = @_;
772
        
773
        my $s = '';
774
        $s .= "limit $count";
775
        $s .= " offset $offset" if defined $offset;
776
        
777
        return [$s, []];
778
    }
779
);
780
$rows = $dbi->select(
781
  table => 'table1',
782
  where => {key1 => 1},
783
  append => "order by key2 {limit 1 0}"
784
)->all;
785
is_deeply($rows, [{key1 => 1, key2 => 2}]);
786
$rows = $dbi->select(
787
  table => 'table1',
788
  where => {key1 => 1},
789
  append => "order by key2 {limit 2 1}"
790
)->all;
791
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
792
$rows = $dbi->select(
793
  table => 'table1',
794
  where => {key1 => 1},
795
  append => "order by key2 {limit 1}"
796
)->all;
797
is_deeply($rows, [{key1 => 1, key2 => 2}]);
798

            
799
test 'connect super';
800
{
801
    package MyDBI;
802
    
803
    use base 'DBIx::Custom';
804
    sub connect {
805
        my $self = shift->SUPER::connect(@_);
806
        
807
        return $self;
808
    }
809
    
810
    sub new {
811
        my $self = shift->SUPER::new(@_);
812
        
813
        return $self;
814
    }
815
}
816

            
cleanup test
Yuki Kimoto authored on 2011-08-06
817
$dbi = MyDBI->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
818
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
819
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
820
is($dbi->select(table => 'table1')->one->{key1}, 1);
821

            
cleanup test
Yuki Kimoto authored on 2011-08-06
822
$dbi = MyDBI->new(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
823
$dbi->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
824
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
825
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
826
is($dbi->select(table => 'table1')->one->{key1}, 1);
827

            
828
{
829
    package MyDBI2;
830
    
831
    use base 'DBIx::Custom';
832
    sub connect {
833
        my $self = shift->SUPER::new(@_);
834
        $self->connect;
835
        
836
        return $self;
837
    }
838
}
839

            
cleanup test
Yuki Kimoto authored on 2011-08-06
840
$dbi = MyDBI->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
841
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
842
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
843
is($dbi->select(table => 'table1')->one->{key1}, 1);
844

            
845
test 'end_filter';
cleanup test
Yuki Kimoto authored on 2011-08-06
846
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
847
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
848
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
849
$result = $dbi->select(table => 'table1');
850
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
851
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 });
852
$row = $result->fetch_first;
853
is_deeply($row, [6, 40]);
854

            
cleanup test
Yuki Kimoto authored on 2011-08-06
855
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
856
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
857
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
858
$result = $dbi->select(table => 'table1');
859
$result->filter([qw/key1 key2/] => sub { $_[0] * 2 });
860
$result->end_filter([[qw/key1 key2/] => sub { $_[0] * 3 }]);
861
$row = $result->fetch_first;
862
is_deeply($row, [6, 12]);
863

            
cleanup test
Yuki Kimoto authored on 2011-08-06
864
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
865
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
866
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
867
$result = $dbi->select(table => 'table1');
868
$result->filter([[qw/key1 key2/] => sub { $_[0] * 2 }]);
869
$result->end_filter([qw/key1 key2/] => sub { $_[0] * 3 });
870
$row = $result->fetch_first;
871
is_deeply($row, [6, 12]);
872

            
873
$dbi->register_filter(five_times => sub { $_[0] * 5 });
874
$result = $dbi->select(table => 'table1');
875
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
876
$result->end_filter({key1 => sub { $_[0] * 3 }, key2 => 'five_times' });
877
$row = $result->one;
878
is_deeply($row, {key1 => 6, key2 => 40});
879

            
880
$dbi->register_filter(five_times => sub { $_[0] * 5 });
881
$dbi->apply_filter('table1',
882
    key1 => {end => sub { $_[0] * 3 } },
883
    key2 => {end => 'five_times'}
884
);
885
$result = $dbi->select(table => 'table1');
886
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
887
$row = $result->one;
888
is_deeply($row, {key1 => 6, key2 => 40}, 'apply_filter');
889

            
890
$dbi->register_filter(five_times => sub { $_[0] * 5 });
891
$dbi->apply_filter('table1',
892
    key1 => {end => sub { $_[0] * 3 } },
893
    key2 => {end => 'five_times'}
894
);
895
$result = $dbi->select(table => 'table1');
896
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
897
$result->filter(key1 => undef);
898
$result->end_filter(key1 => undef);
899
$row = $result->one;
900
is_deeply($row, {key1 => 1, key2 => 40}, 'apply_filter overwrite');
901

            
902
test 'remove_end_filter and remove_filter';
cleanup test
Yuki Kimoto authored on 2011-08-06
903
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
904
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
905
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
906
$result = $dbi->select(table => 'table1');
907
$row = $result
908
       ->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 })
909
       ->remove_filter
910
       ->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 })
911
       ->remove_end_filter
912
       ->fetch_first;
913
is_deeply($row, [1, 2]);
914

            
915
test 'empty where select';
cleanup test
Yuki Kimoto authored on 2011-08-06
916
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
917
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
918
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
919
$result = $dbi->select(table => 'table1', where => {});
920
$row = $result->one;
921
is_deeply($row, {key1 => 1, key2 => 2});
922

            
923
test 'select query option';
cleanup test
Yuki Kimoto authored on 2011-08-06
924
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
925
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
926
$query = $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, query => 1);
927
is(ref $query, 'DBIx::Custom::Query');
928
$query = $dbi->update(table => 'table1', where => {key1 => 1}, param => {key2 => 2}, query => 1);
929
is(ref $query, 'DBIx::Custom::Query');
930
$query = $dbi->delete(table => 'table1', where => {key1 => 1}, query => 1);
931
is(ref $query, 'DBIx::Custom::Query');
932
$query = $dbi->select(table => 'table1', where => {key1 => 1, key2 => 2}, query => 1);
933
is(ref $query, 'DBIx::Custom::Query');
934

            
map cleanup
Yuki Kimoto authored on 2011-08-09
935
test 'where';
cleanup test
Yuki Kimoto authored on 2011-08-06
936
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
937
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
938
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
939
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
940
$where = $dbi->where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
941
is("$where", "where ( key1 = :key1 and key2 = :key2 )", 'no param');
942

            
943
$where = $dbi->where
944
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
945
             ->param({key1 => 1});
946

            
947
$result = $dbi->select(
948
    table => 'table1',
949
    where => $where
950
);
951
$row = $result->all;
952
is_deeply($row, [{key1 => 1, key2 => 2}]);
953

            
954
$result = $dbi->select(
955
    table => 'table1',
956
    where => [
957
        ['and', 'key1 = :key1', 'key2 = :key2'],
958
        {key1 => 1}
959
    ]
960
);
961
$row = $result->all;
962
is_deeply($row, [{key1 => 1, key2 => 2}]);
963

            
964
$where = $dbi->where
965
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
966
             ->param({key1 => 1, key2 => 2});
967
$result = $dbi->select(
968
    table => 'table1',
969
    where => $where
970
);
971
$row = $result->all;
972
is_deeply($row, [{key1 => 1, key2 => 2}]);
973

            
974
$where = $dbi->where
975
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
976
             ->param({});
977
$result = $dbi->select(
978
    table => 'table1',
979
    where => $where,
980
);
981
$row = $result->all;
982
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
983

            
984
$where = $dbi->where
985
             ->clause(['and', ['or', 'key1 > :key1', 'key1 < :key1'], 'key2 = :key2'])
986
             ->param({key1 => [0, 3], key2 => 2});
987
$result = $dbi->select(
988
    table => 'table1',
989
    where => $where,
990
); 
991
$row = $result->all;
992
is_deeply($row, [{key1 => 1, key2 => 2}]);
993

            
994
$where = $dbi->where;
995
$result = $dbi->select(
996
    table => 'table1',
997
    where => $where
998
);
999
$row = $result->all;
1000
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1001

            
1002
eval {
1003
$where = $dbi->where
1004
             ->clause(['uuu']);
1005
$result = $dbi->select(
1006
    table => 'table1',
1007
    where => $where
1008
);
1009
};
1010
ok($@);
1011

            
1012
$where = $dbi->where;
1013
is("$where", '');
1014

            
1015
$where = $dbi->where
1016
             ->clause(['or', ('key1 = :key1') x 2])
1017
             ->param({key1 => [1, 3]});
1018
$result = $dbi->select(
1019
    table => 'table1',
1020
    where => $where,
1021
);
1022
$row = $result->all;
1023
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1024

            
1025
$where = $dbi->where
1026
             ->clause(['or', ('key1 = :key1') x 2])
1027
             ->param({key1 => [1]});
1028
$result = $dbi->select(
1029
    table => 'table1',
1030
    where => $where,
1031
);
1032
$row = $result->all;
1033
is_deeply($row, [{key1 => 1, key2 => 2}]);
1034

            
1035
$where = $dbi->where
1036
             ->clause(['or', ('key1 = :key1') x 2])
1037
             ->param({key1 => 1});
1038
$result = $dbi->select(
1039
    table => 'table1',
1040
    where => $where,
1041
);
1042
$row = $result->all;
1043
is_deeply($row, [{key1 => 1, key2 => 2}]);
1044

            
1045
$where = $dbi->where
1046
             ->clause('key1 = :key1')
1047
             ->param({key1 => 1});
1048
$result = $dbi->select(
1049
    table => 'table1',
1050
    where => $where,
1051
);
1052
$row = $result->all;
1053
is_deeply($row, [{key1 => 1, key2 => 2}]);
1054

            
1055
$where = $dbi->where
1056
             ->clause('key1 = :key1 key2 = :key2')
1057
             ->param({key1 => 1});
1058
eval{$where->to_string};
1059
like($@, qr/one column/);
1060

            
1061
$where = $dbi->where
1062
             ->clause(['or', ('key1 = :key1') x 3])
1063
             ->param({key1 => [$dbi->not_exists, 1, 3]});
1064
$result = $dbi->select(
1065
    table => 'table1',
1066
    where => $where,
1067
);
1068
$row = $result->all;
1069
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1070

            
1071
$where = $dbi->where
1072
             ->clause(['or', ('key1 = :key1') x 3])
1073
             ->param({key1 => [1, $dbi->not_exists, 3]});
1074
$result = $dbi->select(
1075
    table => 'table1',
1076
    where => $where,
1077
);
1078
$row = $result->all;
1079
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1080

            
1081
$where = $dbi->where
1082
             ->clause(['or', ('key1 = :key1') x 3])
1083
             ->param({key1 => [1, 3, $dbi->not_exists]});
1084
$result = $dbi->select(
1085
    table => 'table1',
1086
    where => $where,
1087
);
1088
$row = $result->all;
1089
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1090

            
1091
$where = $dbi->where
1092
             ->clause(['or', ('key1 = :key1') x 3])
1093
             ->param({key1 => [1, $dbi->not_exists, $dbi->not_exists]});
1094
$result = $dbi->select(
1095
    table => 'table1',
1096
    where => $where,
1097
);
1098
$row = $result->all;
1099
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1100

            
1101
$where = $dbi->where
1102
             ->clause(['or', ('key1 = :key1') x 3])
1103
             ->param({key1 => [$dbi->not_exists, 1, $dbi->not_exists]});
1104
$result = $dbi->select(
1105
    table => 'table1',
1106
    where => $where,
1107
);
1108
$row = $result->all;
1109
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1110

            
1111
$where = $dbi->where
1112
             ->clause(['or', ('key1 = :key1') x 3])
1113
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists, 1]});
1114
$result = $dbi->select(
1115
    table => 'table1',
1116
    where => $where,
1117
);
1118
$row = $result->all;
1119
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1120

            
1121
$where = $dbi->where
1122
             ->clause(['or', ('key1 = :key1') x 3])
1123
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists, $dbi->not_exists]});
1124
$result = $dbi->select(
1125
    table => 'table1',
1126
    where => $where,
1127
);
1128
$row = $result->all;
1129
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1130

            
1131
$where = $dbi->where
1132
             ->clause(['or', ('key1 = :key1') x 3])
1133
             ->param({key1 => []});
1134
$result = $dbi->select(
1135
    table => 'table1',
1136
    where => $where,
1137
);
1138
$row = $result->all;
1139
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1140

            
1141
$where = $dbi->where
1142
             ->clause(['and', '{> key1}', '{< key1}' ])
1143
             ->param({key1 => [2, $dbi->not_exists]});
1144
$result = $dbi->select(
1145
    table => 'table1',
1146
    where => $where,
1147
);
1148
$row = $result->all;
1149
is_deeply($row, [{key1 => 3, key2 => 4}], 'not_exists');
1150

            
1151
$where = $dbi->where
1152
             ->clause(['and', '{> key1}', '{< key1}' ])
1153
             ->param({key1 => [$dbi->not_exists, 2]});
1154
$result = $dbi->select(
1155
    table => 'table1',
1156
    where => $where,
1157
);
1158
$row = $result->all;
1159
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1160

            
1161
$where = $dbi->where
1162
             ->clause(['and', '{> key1}', '{< key1}' ])
1163
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists]});
1164
$result = $dbi->select(
1165
    table => 'table1',
1166
    where => $where,
1167
);
1168
$row = $result->all;
1169
is_deeply($row, [{key1 => 1, key2 => 2},{key1 => 3, key2 => 4}], 'not_exists');
1170

            
1171
$where = $dbi->where
1172
             ->clause(['and', '{> key1}', '{< key1}' ])
1173
             ->param({key1 => [0, 2]});
1174
$result = $dbi->select(
1175
    table => 'table1',
1176
    where => $where,
1177
);
1178
$row = $result->all;
1179
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1180

            
1181
$where = $dbi->where
1182
             ->clause(['and', 'key1 is not null', 'key2 is not null' ]);
1183
$result = $dbi->select(
1184
    table => 'table1',
1185
    where => $where,
1186
);
1187
$row = $result->all;
1188
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1189

            
1190
eval {$dbi->where(ppp => 1) };
1191
like($@, qr/invalid/);
1192

            
1193
$where = $dbi->where(
1194
    clause => ['and', ['or'], ['and', 'key1 = :key1', 'key2 = :key2']],
1195
    param => {key1 => 1, key2 => 2}
1196
);
1197
$result = $dbi->select(
1198
    table => 'table1',
1199
    where => $where,
1200
);
1201
$row = $result->all;
1202
is_deeply($row, [{key1 => 1, key2 => 2}]);
1203

            
1204

            
1205
$where = $dbi->where(
1206
    clause => ['and', ['or'], ['or', ':key1', ':key2']],
1207
    param => {}
1208
);
1209
$result = $dbi->select(
1210
    table => 'table1',
1211
    where => $where,
1212
);
1213
$row = $result->all;
1214
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1215

            
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
1216
$where = $dbi->where;
1217
$where->clause(['and', ':key1{=}']);
1218
$where->param({key1 => undef});
1219
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1220
$row = $result->all;
1221
is_deeply($row, [{key1 => 1, key2 => 2}]);
1222

            
1223
$where = $dbi->where;
1224
$where->clause(['and', ':key1{=}']);
1225
$where->param({key1 => undef});
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
1226
$where->if('defined');
map cleanup
Yuki Kimoto authored on 2011-08-09
1227
$where->map;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
1228
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1229
$row = $result->all;
1230
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1231

            
1232
$where = $dbi->where;
1233
$where->clause(['or', ':key1{=}', ':key1{=}']);
1234
$where->param({key1 => [undef, undef]});
1235
$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]});
1236
$row = $result->all;
1237
is_deeply($row, [{key1 => 1, key2 => 2}]);
1238
$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]});
1239
$row = $result->all;
1240
is_deeply($row, [{key1 => 1, key2 => 2}]);
1241

            
1242
$where = $dbi->where;
1243
$where->clause(['and', ':key1{=}']);
1244
$where->param({key1 => [undef, undef]});
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
1245
$where->if('defined');
map cleanup
Yuki Kimoto authored on 2011-08-09
1246
$where->map;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
1247
$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]});
1248
$row = $result->all;
1249
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1250
$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]});
1251
$row = $result->all;
1252
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1253

            
1254
$where = $dbi->where;
1255
$where->clause(['and', ':key1{=}']);
1256
$where->param({key1 => 0});
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
1257
$where->if('length');
map cleanup
Yuki Kimoto authored on 2011-08-09
1258
$where->map;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
1259
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1260
$row = $result->all;
1261
is_deeply($row, [{key1 => 1, key2 => 2}]);
1262

            
1263
$where = $dbi->where;
1264
$where->clause(['and', ':key1{=}']);
1265
$where->param({key1 => ''});
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
1266
$where->if('length');
map cleanup
Yuki Kimoto authored on 2011-08-09
1267
$where->map;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
1268
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1269
$row = $result->all;
1270
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1271

            
1272
$where = $dbi->where;
1273
$where->clause(['and', ':key1{=}']);
1274
$where->param({key1 => 5});
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
1275
$where->if(sub { ($_[0] || '') eq 5 });
map cleanup
Yuki Kimoto authored on 2011-08-09
1276
$where->map;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
1277
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1278
$row = $result->all;
1279
is_deeply($row, [{key1 => 1, key2 => 2}]);
1280

            
1281
$where = $dbi->where;
1282
$where->clause(['and', ':key1{=}']);
1283
$where->param({key1 => 7});
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
1284
$where->if(sub { ($_[0] || '') eq 5 });
map cleanup
Yuki Kimoto authored on 2011-08-09
1285
$where->map;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
1286
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1287
$row = $result->all;
1288
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1289

            
added tests
Yuki Kimoto authored on 2011-08-09
1290
$where = $dbi->where;
1291
$where->param({id => 1, author => 'Ken', price => 1900});
1292
$where->map(id => 'book.id',
1293
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1294
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1295
);
1296
is_deeply($where->param, {'book.id' => 1, 'book.author' => '%Ken%',
1297
  'book.price' => 1900});
1298

            
1299
$where = $dbi->where;
1300
$where->param({id => 0, author => 0, price => 0});
1301
$where->map(
1302
    id => 'book.id',
1303
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1304
    price => ['book.price', sub { '%' . $_[0] . '%' },
1305
      {if => sub { $_[0] eq 0 }}]
1306
);
1307
is_deeply($where->param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
1308

            
1309
$where = $dbi->where;
1310
$where->param({id => '', author => '', price => ''});
1311
$where->if('length');
1312
$where->map(
1313
    id => 'book.id',
1314
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1315
    price => ['book.price', sub { '%' . $_[0] . '%' },
1316
      {if => sub { $_[0] eq 1 }}]
1317
);
1318
is_deeply($where->param, {});
1319

            
1320
$where = $dbi->where;
1321
$where->param({id => undef, author => undef, price => undef});
1322
$where->if('length');
1323
$where->map(
1324
    id => 'book.id',
1325
    price => ['book.price', {if => 'exists'}]
1326
);
1327
is_deeply($where->param, {'book.price' => undef});
1328

            
1329
$where = $dbi->where;
1330
$where->param({price => 'a'});
1331
$where->if('length');
1332
$where->map(
1333
    id => ['book.id', {if => 'exists'}],
1334
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
1335
);
1336
is_deeply($where->param, {'book.price' => '%a'});
1337

            
fixed if is not converted to...
Yuki Kimoto authored on 2011-08-09
1338
$where = $dbi->where;
1339
$where->param({id => [1, 2], author => 'Ken', price => 1900});
1340
$where->map(
1341
    id => 'book.id',
1342
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1343
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1344
);
1345
is_deeply($where->param, {'book.id' => [1, 2], 'book.author' => '%Ken%',
1346
  'book.price' => 1900});
1347

            
1348
$where = $dbi->where;
1349
$where->if('length');
1350
$where->param({id => ['', ''], author => 'Ken', price => 1900});
1351
$where->map(
1352
    id => 'book.id',
1353
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1354
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1355
);
1356
is_deeply($where->param, {'book.id' => [$dbi->not_exists, $dbi->not_exists], 'book.author' => '%Ken%',
1357
  'book.price' => 1900});
1358

            
1359
$where = $dbi->where;
1360
$where->param({id => ['', ''], author => 'Ken', price => 1900});
1361
$where->map(
1362
    id => ['book.id', {if => 'length'}],
1363
    author => ['book.author', sub { '%' . $_[0] . '%' }, {if => 'defined'}],
1364
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1365
);
1366
is_deeply($where->param, {'book.id' => [$dbi->not_exists, $dbi->not_exists], 'book.author' => '%Ken%',
1367
  'book.price' => 1900});
cleanup test
Yuki Kimoto authored on 2011-08-06
1368

            
1369
test 'dbi_option default';
1370
$dbi = DBIx::Custom->new;
1371
is_deeply($dbi->dbi_option, {});
1372

            
1373
test 'register_tag_processor';
cleanup test
Yuki Kimoto authored on 2011-08-06
1374
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1375
$dbi->register_tag_processor(
1376
    a => sub { 1 }
1377
);
1378
is($dbi->query_builder->tag_processors->{a}->(), 1);
1379

            
1380
test 'register_tag';
cleanup test
Yuki Kimoto authored on 2011-08-06
1381
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1382
$dbi->register_tag(
1383
    b => sub { 2 }
1384
);
1385
is($dbi->query_builder->tags->{b}->(), 2);
1386

            
1387
test 'table not specify exception';
cleanup test
Yuki Kimoto authored on 2011-08-06
1388
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1389
eval {$dbi->insert};
1390
like($@, qr/table/);
1391
eval {$dbi->update};
1392
like($@, qr/table/);
1393
eval {$dbi->delete};
1394
like($@, qr/table/);
1395
eval {$dbi->select};
1396
like($@, qr/table/);
1397

            
1398

            
1399
test 'more tests';
cleanup test
Yuki Kimoto authored on 2011-08-06
1400
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1401
eval{$dbi->apply_filter('table', 'column', [])};
1402
like($@, qr/apply_filter/);
1403

            
1404
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
1405
like($@, qr/apply_filter/);
1406

            
1407
$dbi->apply_filter(
1408

            
1409
);
cleanup test
Yuki Kimoto authored on 2011-08-06
1410
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1411
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1412
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1413
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1414
$dbi->apply_filter('table1', 'key2', 
1415
                   {in => sub { $_[0] * 3 }, out => sub { $_[0] * 2 }});
1416
$rows = $dbi->select(table => 'table1', where => {key2 => 1})->all;
1417
is_deeply($rows, [{key1 => 1, key2 => 6}]);
1418

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1419
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1420
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1421
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1422
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1423
$dbi->apply_filter('table1', 'key2', {});
1424
$rows = $dbi->select(table => 'table1', where => {key2 => 2})->all;
1425
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1426

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1427
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1428
eval {$dbi->apply_filter('table1', 'key2', {out => 'no'})};
1429
like($@, qr/not registered/);
1430
eval {$dbi->apply_filter('table1', 'key2', {in => 'no'})};
1431
like($@, qr/not registered/);
1432
$dbi->method({one => sub { 1 }});
1433
is($dbi->one, 1);
1434

            
1435
eval{DBIx::Custom->connect()};
1436
like($@, qr/_connect/);
1437

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1438
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1439
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1440
$dbi->register_filter(twice => sub { $_[0] * 2 });
1441
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
1442
             filter => {key1 => 'twice'});
1443
$row = $dbi->select(table => 'table1')->one;
1444
is_deeply($row, {key1 => 2, key2 => 2});
1445
eval {$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
1446
             filter => {key1 => 'no'}) };
1447
like($@, qr//);
1448

            
1449
$dbi->register_filter(one => sub { });
1450
$dbi->default_fetch_filter('one');
1451
ok($dbi->default_fetch_filter);
1452
$dbi->default_bind_filter('one');
1453
ok($dbi->default_bind_filter);
1454
eval{$dbi->default_fetch_filter('no')};
1455
like($@, qr/not registered/);
1456
eval{$dbi->default_bind_filter('no')};
1457
like($@, qr/not registered/);
1458
$dbi->default_bind_filter(undef);
1459
ok(!defined $dbi->default_bind_filter);
1460
$dbi->default_fetch_filter(undef);
1461
ok(!defined $dbi->default_fetch_filter);
1462
eval {$dbi->execute('select * from table1 {} {= author') };
1463
like($@, qr/Tag not finished/);
1464

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1465
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1466
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1467
$dbi->register_filter(one => sub { 1 });
1468
$result = $dbi->select(table => 'table1');
1469
eval {$result->filter(key1 => 'no')};
1470
like($@, qr/not registered/);
1471
eval {$result->end_filter(key1 => 'no')};
1472
like($@, qr/not registered/);
1473
$result->default_filter(undef);
1474
ok(!defined $result->default_filter);
1475
$result->default_filter('one');
1476
is($result->default_filter->(), 1);
1477

            
1478
test 'dbi_option';
cleanup test
Yuki Kimoto authored on 2011-08-06
1479
$dbi = DBIx::Custom->connect(%memory,
cleanup test
Yuki Kimoto authored on 2011-08-06
1480
                             dbi_option => {PrintError => 1});
1481
ok($dbi->dbh->{PrintError});
cleanup test
Yuki Kimoto authored on 2011-08-06
1482
$dbi = DBIx::Custom->connect(%memory,
cleanup test
Yuki Kimoto authored on 2011-08-06
1483
                             dbi_options => {PrintError => 1});
1484
ok($dbi->dbh->{PrintError});
1485

            
1486
test 'DBIx::Custom::Result stash()';
1487
$result = DBIx::Custom::Result->new;
1488
is_deeply($result->stash, {}, 'default');
1489
$result->stash->{foo} = 1;
1490
is($result->stash->{foo}, 1, 'get and set');
1491

            
1492
test 'filter __ expression';
cleanup test
Yuki Kimoto authored on 2011-08-06
1493
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1494
$dbi->execute('create table company (id, name, location_id)');
1495
$dbi->execute('create table location (id, name)');
1496
$dbi->apply_filter('location',
1497
  name => {in => sub { uc $_[0] } }
1498
);
1499

            
1500
$dbi->insert(table => 'company', param => {id => 1, name => 'a', location_id => 2});
1501
$dbi->insert(table => 'location', param => {id => 2, name => 'b'});
1502

            
1503
$result = $dbi->select(
1504
    table => ['company', 'location'], relation => {'company.location_id' => 'location.id'},
1505
    column => ['location.name as location__name']
1506
);
1507
is($result->fetch_first->[0], 'B');
1508

            
1509
$result = $dbi->select(
1510
    table => 'company', relation => {'company.location_id' => 'location.id'},
1511
    column => ['location.name as location__name']
1512
);
1513
is($result->fetch_first->[0], 'B');
1514

            
1515
$result = $dbi->select(
1516
    table => 'company', relation => {'company.location_id' => 'location.id'},
1517
    column => ['location.name as "location.name"']
1518
);
1519
is($result->fetch_first->[0], 'B');
1520

            
1521
test 'Model class';
1522
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1523
$dbi = MyDBI1->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1524
$dbi->execute("create table book (title, author)");
1525
$model = $dbi->model('book');
1526
$model->insert({title => 'a', author => 'b'});
1527
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
1528
$dbi->execute("create table company (name)");
1529
$model = $dbi->model('company');
1530
$model->insert({name => 'a'});
1531
is_deeply($model->list->all, [{name => 'a'}], 'basic');
1532
is($dbi->models->{'book'}, $dbi->model('book'));
1533
is($dbi->models->{'company'}, $dbi->model('company'));
1534

            
1535
{
1536
    package MyDBI4;
1537

            
1538
    use strict;
1539
    use warnings;
1540

            
1541
    use base 'DBIx::Custom';
1542

            
1543
    sub connect {
1544
        my $self = shift->SUPER::connect(@_);
1545
        
1546
        $self->include_model(
1547
            MyModel2 => [
1548
                'book',
1549
                {class => 'Company', name => 'company'}
1550
            ]
1551
        );
1552
    }
1553

            
1554
    package MyModel2::Base1;
1555

            
1556
    use strict;
1557
    use warnings;
1558

            
1559
    use base 'DBIx::Custom::Model';
1560

            
1561
    package MyModel2::book;
1562

            
1563
    use strict;
1564
    use warnings;
1565

            
1566
    use base 'MyModel2::Base1';
1567

            
1568
    sub insert {
1569
        my ($self, $param) = @_;
1570
        
1571
        return $self->SUPER::insert(param => $param);
1572
    }
1573

            
1574
    sub list { shift->select; }
1575

            
1576
    package MyModel2::Company;
1577

            
1578
    use strict;
1579
    use warnings;
1580

            
1581
    use base 'MyModel2::Base1';
1582

            
1583
    sub insert {
1584
        my ($self, $param) = @_;
1585
        
1586
        return $self->SUPER::insert(param => $param);
1587
    }
1588

            
1589
    sub list { shift->select; }
1590
}
cleanup test
Yuki Kimoto authored on 2011-08-06
1591
$dbi = MyDBI4->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1592
$dbi->execute("create table book (title, author)");
1593
$model = $dbi->model('book');
1594
$model->insert({title => 'a', author => 'b'});
1595
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
1596
$dbi->execute("create table company (name)");
1597
$model = $dbi->model('company');
1598
$model->insert({name => 'a'});
1599
is_deeply($model->list->all, [{name => 'a'}], 'basic');
1600

            
1601
{
1602
     package MyDBI5;
1603

            
1604
    use strict;
1605
    use warnings;
1606

            
1607
    use base 'DBIx::Custom';
1608

            
1609
    sub connect {
1610
        my $self = shift->SUPER::connect(@_);
1611
        
1612
        $self->include_model('MyModel4');
1613
    }
1614
}
cleanup test
Yuki Kimoto authored on 2011-08-06
1615
$dbi = MyDBI5->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1616
$dbi->execute("create table company (name)");
1617
$dbi->execute("create table table1 (key1)");
1618
$model = $dbi->model('company');
1619
$model->insert({name => 'a'});
1620
is_deeply($model->list->all, [{name => 'a'}], 'include all model');
1621
$dbi->insert(table => 'table1', param => {key1 => 1});
1622
$model = $dbi->model('book');
1623
is_deeply($model->list->all, [{key1 => 1}], 'include all model');
1624

            
1625
test 'primary_key';
1626
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1627
$dbi = MyDBI1->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1628
$model = $dbi->model('book');
1629
$model->primary_key(['id', 'number']);
1630
is_deeply($model->primary_key, ['id', 'number']);
1631

            
1632
test 'columns';
1633
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1634
$dbi = MyDBI1->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1635
$model = $dbi->model('book');
1636
$model->columns(['id', 'number']);
1637
is_deeply($model->columns, ['id', 'number']);
1638

            
1639
test 'setup_model';
1640
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1641
$dbi = MyDBI1->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1642
$dbi->execute('create table book (id)');
1643
$dbi->execute('create table company (id, name);');
1644
$dbi->execute('create table test (id, name, primary key (id, name));');
1645
$dbi->setup_model;
1646
is_deeply($dbi->model('book')->columns, ['id']);
1647
is_deeply($dbi->model('company')->columns, ['id', 'name']);
1648

            
1649
test 'delete_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1650
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1651
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1652
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1653
$dbi->delete_at(
1654
    table => 'table1',
1655
    primary_key => ['key1', 'key2'],
1656
    where => [1, 2],
1657
);
1658
is_deeply($dbi->select(table => 'table1')->all, []);
1659

            
1660
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1661
$dbi->delete_at(
1662
    table => 'table1',
1663
    primary_key => 'key1',
1664
    where => 1,
1665
);
1666
is_deeply($dbi->select(table => 'table1')->all, []);
1667

            
1668
test 'insert_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1669
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1670
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1671
$dbi->insert_at(
1672
    primary_key => ['key1', 'key2'], 
1673
    table => 'table1',
1674
    where => [1, 2],
1675
    param => {key3 => 3}
1676
);
1677
is($dbi->select(table => 'table1')->one->{key1}, 1);
1678
is($dbi->select(table => 'table1')->one->{key2}, 2);
1679
is($dbi->select(table => 'table1')->one->{key3}, 3);
1680

            
1681
$dbi->delete_all(table => 'table1');
1682
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1683
$dbi->insert_at(
1684
    primary_key => 'key1', 
1685
    table => 'table1',
1686
    where => 1,
1687
    param => {key2 => 2, key3 => 3}
1688
);
1689

            
1690
is($dbi->select(table => 'table1')->one->{key1}, 1);
1691
is($dbi->select(table => 'table1')->one->{key2}, 2);
1692
is($dbi->select(table => 'table1')->one->{key3}, 3);
1693

            
1694
eval {
1695
    $dbi->insert_at(
1696
        table => 'table1',
1697
        primary_key => ['key1', 'key2'],
1698
        where => {},
1699
        param => {key1 => 1, key2 => 2, key3 => 3},
1700
    );
1701
};
1702
like($@, qr/must be/);
1703

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1704
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1705
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1706
$dbi->insert_at(
1707
    {key3 => 3},
1708
    primary_key => ['key1', 'key2'], 
1709
    table => 'table1',
1710
    where => [1, 2],
1711
);
1712
is($dbi->select(table => 'table1')->one->{key1}, 1);
1713
is($dbi->select(table => 'table1')->one->{key2}, 2);
1714
is($dbi->select(table => 'table1')->one->{key3}, 3);
1715

            
1716
test 'update_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1717
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1718
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1719
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1720
$dbi->update_at(
1721
    table => 'table1',
1722
    primary_key => ['key1', 'key2'],
1723
    where => [1, 2],
1724
    param => {key3 => 4}
1725
);
1726
is($dbi->select(table => 'table1')->one->{key1}, 1);
1727
is($dbi->select(table => 'table1')->one->{key2}, 2);
1728
is($dbi->select(table => 'table1')->one->{key3}, 4);
1729

            
1730
$dbi->delete_all(table => 'table1');
1731
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1732
$dbi->update_at(
1733
    table => 'table1',
1734
    primary_key => 'key1',
1735
    where => 1,
1736
    param => {key3 => 4}
1737
);
1738
is($dbi->select(table => 'table1')->one->{key1}, 1);
1739
is($dbi->select(table => 'table1')->one->{key2}, 2);
1740
is($dbi->select(table => 'table1')->one->{key3}, 4);
1741

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1742
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1743
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1744
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1745
$dbi->update_at(
1746
    {key3 => 4},
1747
    table => 'table1',
1748
    primary_key => ['key1', 'key2'],
1749
    where => [1, 2]
1750
);
1751
is($dbi->select(table => 'table1')->one->{key1}, 1);
1752
is($dbi->select(table => 'table1')->one->{key2}, 2);
1753
is($dbi->select(table => 'table1')->one->{key3}, 4);
1754

            
1755
test 'select_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1756
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1757
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1758
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1759
$result = $dbi->select_at(
1760
    table => 'table1',
1761
    primary_key => ['key1', 'key2'],
1762
    where => [1, 2]
1763
);
1764
$row = $result->one;
1765
is($row->{key1}, 1);
1766
is($row->{key2}, 2);
1767
is($row->{key3}, 3);
1768

            
1769
$dbi->delete_all(table => 'table1');
1770
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1771
$result = $dbi->select_at(
1772
    table => 'table1',
1773
    primary_key => 'key1',
1774
    where => 1,
1775
);
1776
$row = $result->one;
1777
is($row->{key1}, 1);
1778
is($row->{key2}, 2);
1779
is($row->{key3}, 3);
1780

            
1781
$dbi->delete_all(table => 'table1');
1782
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1783
$result = $dbi->select_at(
1784
    table => 'table1',
1785
    primary_key => ['key1', 'key2'],
1786
    where => [1, 2]
1787
);
1788
$row = $result->one;
1789
is($row->{key1}, 1);
1790
is($row->{key2}, 2);
1791
is($row->{key3}, 3);
1792

            
1793
eval {
1794
    $result = $dbi->select_at(
1795
        table => 'table1',
1796
        primary_key => ['key1', 'key2'],
1797
        where => {},
1798
    );
1799
};
1800
like($@, qr/must be/);
1801

            
1802
eval {
1803
    $result = $dbi->select_at(
1804
        table => 'table1',
1805
        primary_key => ['key1', 'key2'],
1806
        where => [1],
1807
    );
1808
};
1809
like($@, qr/same/);
1810

            
1811
eval {
1812
    $result = $dbi->update_at(
1813
        table => 'table1',
1814
        primary_key => ['key1', 'key2'],
1815
        where => {},
1816
        param => {key1 => 1, key2 => 2},
1817
    );
1818
};
1819
like($@, qr/must be/);
1820

            
1821
eval {
1822
    $result = $dbi->delete_at(
1823
        table => 'table1',
1824
        primary_key => ['key1', 'key2'],
1825
        where => {},
1826
    );
1827
};
1828
like($@, qr/must be/);
1829

            
1830
test 'columns';
1831
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1832
$dbi = MyDBI1->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1833
$model = $dbi->model('book');
1834

            
1835

            
1836
test 'model delete_at';
1837
{
1838
    package MyDBI6;
1839
    
1840
    use base 'DBIx::Custom';
1841
    
1842
    sub connect {
1843
        my $self = shift->SUPER::connect(@_);
1844
        
1845
        $self->include_model('MyModel5');
1846
        
1847
        return $self;
1848
    }
1849
}
cleanup test
Yuki Kimoto authored on 2011-08-06
1850
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1851
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1852
$dbi->execute("create table table2 (key1, key2, key3)");
1853
$dbi->execute("create table table3 (key1, key2, key3)");
1854
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1855
$dbi->model('table1')->delete_at(where => [1, 2]);
1856
is_deeply($dbi->select(table => 'table1')->all, []);
1857
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
1858
$dbi->model('table1_1')->delete_at(where => [1, 2]);
1859
is_deeply($dbi->select(table => 'table1')->all, []);
1860
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
1861
$dbi->model('table1_3')->delete_at(where => [1, 2]);
1862
is_deeply($dbi->select(table => 'table1')->all, []);
1863

            
1864
test 'model insert_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1865
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1866
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1867
$dbi->model('table1')->insert_at(
1868
    where => [1, 2],
1869
    param => {key3 => 3}
1870
);
1871
$result = $dbi->model('table1')->select;
1872
$row = $result->one;
1873
is($row->{key1}, 1);
1874
is($row->{key2}, 2);
1875
is($row->{key3}, 3);
1876

            
1877
test 'model update_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1878
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1879
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1880
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1881
$dbi->model('table1')->update_at(
1882
    where => [1, 2],
1883
    param => {key3 => 4}
1884
);
1885
$result = $dbi->model('table1')->select;
1886
$row = $result->one;
1887
is($row->{key1}, 1);
1888
is($row->{key2}, 2);
1889
is($row->{key3}, 4);
1890

            
1891
test 'model select_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1892
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1893
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1894
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1895
$result = $dbi->model('table1')->select_at(where => [1, 2]);
1896
$row = $result->one;
1897
is($row->{key1}, 1);
1898
is($row->{key2}, 2);
1899
is($row->{key3}, 3);
1900

            
1901

            
1902
test 'mycolumn and column';
1903
{
1904
    package MyDBI7;
1905
    
1906
    use base 'DBIx::Custom';
1907
    
1908
    sub connect {
1909
        my $self = shift->SUPER::connect(@_);
1910
        
1911
        $self->include_model('MyModel6');
1912
        
1913
        
1914
        return $self;
1915
    }
1916
}
cleanup test
Yuki Kimoto authored on 2011-08-06
1917
$dbi = MyDBI7->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1918
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1919
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1920
$dbi->separator('__');
1921
$dbi->setup_model;
1922
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1923
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1924
$model = $dbi->model('table1');
1925
$result = $model->select(
1926
    column => [$model->mycolumn, $model->column('table2')],
1927
    where => {'table1.key1' => 1}
1928
);
1929
is_deeply($result->one,
1930
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
1931

            
1932
test 'update_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
1933
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1934
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1935
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1936
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1937

            
1938
$param = {key2 => 11};
1939
$update_param = $dbi->update_param($param);
1940
$sql = <<"EOS";
1941
update table1 $update_param
1942
where key1 = 1
1943
EOS
1944
$dbi->execute($sql, param => $param);
test cleanup
Yuki Kimoto authored on 2011-08-06
1945
$result = $dbi->execute('select * from table1;', table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
1946
$rows   = $result->all;
1947
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
1948
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1949
                  "basic");
1950

            
1951

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1952
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1953
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1954
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1955
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1956

            
1957
$param = {key2 => 11, key3 => 33};
1958
$update_param = $dbi->update_param($param);
1959
$sql = <<"EOS";
1960
update table1 $update_param
1961
where key1 = 1
1962
EOS
1963
$dbi->execute($sql, param => $param);
test cleanup
Yuki Kimoto authored on 2011-08-06
1964
$result = $dbi->execute('select * from table1;', table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
1965
$rows   = $result->all;
1966
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
1967
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1968
                  "basic");
1969

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1970
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1971
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1972
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1973
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1974

            
1975
$param = {key2 => 11, key3 => 33};
1976
$update_param = $dbi->update_param($param, {no_set => 1});
1977
$sql = <<"EOS";
1978
update table1 set $update_param
1979
where key1 = 1
1980
EOS
1981
$dbi->execute($sql, param => $param);
test cleanup
Yuki Kimoto authored on 2011-08-06
1982
$result = $dbi->execute('select * from table1;', table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
1983
$rows   = $result->all;
1984
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
1985
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1986
                  "update param no_set");
1987

            
1988
            
1989
eval { $dbi->update_param({";" => 1}) };
1990
like($@, qr/not safety/);
1991

            
1992

            
1993
test 'update_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
1994
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1995
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1996
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1997
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1998

            
1999
$param = {key2 => 11};
2000
$update_param = $dbi->assign_param($param);
2001
$sql = <<"EOS";
2002
update table1 set $update_param
2003
where key1 = 1
2004
EOS
2005
$dbi->execute($sql, param => $param, table => 'table1');
test cleanup
Yuki Kimoto authored on 2011-08-06
2006
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
2007
$rows   = $result->all;
2008
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
2009
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
2010
                  "basic");
2011

            
2012

            
2013
test 'insert_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
2014
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2015
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2016
$param = {key1 => 1, key2 => 2};
2017
$insert_param = $dbi->insert_param($param);
2018
$sql = <<"EOS";
2019
insert into table1 $insert_param
2020
EOS
2021
$dbi->execute($sql, param => $param, table => 'table1');
2022
is($dbi->select(table => 'table1')->one->{key1}, 1);
2023
is($dbi->select(table => 'table1')->one->{key2}, 2);
2024

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2025
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2026
$dbi->quote('"');
cleanup test
Yuki Kimoto authored on 2011-08-10
2027
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2028
$param = {key1 => 1, key2 => 2};
2029
$insert_param = $dbi->insert_param($param);
2030
$sql = <<"EOS";
2031
insert into table1 $insert_param
2032
EOS
2033
$dbi->execute($sql, param => $param, table => 'table1');
2034
is($dbi->select(table => 'table1')->one->{key1}, 1);
2035
is($dbi->select(table => 'table1')->one->{key2}, 2);
2036

            
2037
eval { $dbi->insert_param({";" => 1}) };
2038
like($@, qr/not safety/);
2039

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2040

            
2041
test 'join';
cleanup test
Yuki Kimoto authored on 2011-08-06
2042
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2043
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2044
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2045
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
cleanup test
Yuki Kimoto authored on 2011-08-06
2046
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2047
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
cleanup test
Yuki Kimoto authored on 2011-08-06
2048
$dbi->execute('create table table3 (key3 int, key4 int);');
cleanup test
Yuki Kimoto authored on 2011-08-06
2049
$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4});
2050
$rows = $dbi->select(
2051
    table => 'table1',
2052
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
2053
    where   => {'table1.key2' => 2},
2054
    join  => ['left outer join table2 on table1.key1 = table2.key1']
2055
)->all;
2056
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]);
2057

            
2058
$rows = $dbi->select(
2059
    table => 'table1',
2060
    where   => {'key1' => 1},
2061
    join  => ['left outer join table2 on table1.key1 = table2.key1']
2062
)->all;
2063
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2064

            
2065
eval {
2066
    $rows = $dbi->select(
2067
        table => 'table1',
2068
        column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
2069
        where   => {'table1.key2' => 2},
2070
        join  => {'table1.key1' => 'table2.key1'}
2071
    );
2072
};
2073
like ($@, qr/array/);
2074

            
2075
$rows = $dbi->select(
2076
    table => 'table1',
2077
    where   => {'key1' => 1},
2078
    join  => ['left outer join table2 on table1.key1 = table2.key1',
2079
              'left outer join table3 on table2.key3 = table3.key3']
2080
)->all;
2081
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2082

            
2083
$rows = $dbi->select(
2084
    column => 'table3.key4 as table3__key4',
2085
    table => 'table1',
2086
    where   => {'table1.key1' => 1},
2087
    join  => ['left outer join table2 on table1.key1 = table2.key1',
2088
              'left outer join table3 on table2.key3 = table3.key3']
2089
)->all;
2090
is_deeply($rows, [{table3__key4 => 4}]);
2091

            
2092
$rows = $dbi->select(
2093
    column => 'table1.key1 as table1__key1',
2094
    table => 'table1',
2095
    where   => {'table3.key4' => 4},
2096
    join  => ['left outer join table2 on table1.key1 = table2.key1',
2097
              'left outer join table3 on table2.key3 = table3.key3']
2098
)->all;
2099
is_deeply($rows, [{table1__key1 => 1}]);
2100

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2101
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2102
$dbi->quote('"');
cleanup test
Yuki Kimoto authored on 2011-08-10
2103
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2104
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-06
2105
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2106
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
2107
$rows = $dbi->select(
2108
    table => 'table1',
2109
    column => '"table1"."key1" as "table1_key1", "table2"."key1" as "table2_key1", "key2", "key3"',
2110
    where   => {'table1.key2' => 2},
2111
    join  => ['left outer join "table2" on "table1"."key1" = "table2"."key1"'],
2112
)->all;
2113
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}],
2114
          'quote');
2115

            
2116
{
2117
    package MyDBI8;
2118
    
2119
    use base 'DBIx::Custom';
2120
    
2121
    sub connect {
2122
        my $self = shift->SUPER::connect(@_);
2123
        
2124
        $self->include_model('MyModel7');
2125
        
2126
        return $self;
2127
    }
2128
}
cleanup test
Yuki Kimoto authored on 2011-08-06
2129

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2130
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2131
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2132
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2133
$sql = <<"EOS";
cleanup test
Yuki Kimoto authored on 2011-08-06
2134
left outer join (
2135
  select * from table1 as t1
2136
  where t1.key2 = (
2137
    select max(t2.key2) from table1 as t2
2138
    where t1.key1 = t2.key1
2139
  )
2140
) as latest_table1 on table1.key1 = latest_table1.key1
2141
EOS
cleanup test
Yuki Kimoto authored on 2011-08-06
2142
$join = [$sql];
2143
$rows = $dbi->select(
2144
    table => 'table1',
2145
    column => 'latest_table1.key1 as latest_table1__key1',
2146
    join  => $join
2147
)->all;
2148
is_deeply($rows, [{latest_table1__key1 => 1}]);
2149

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2150
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2151
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2152
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2153
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2154
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2155
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
2156
$result = $dbi->select(
2157
    table => 'table1',
2158
    join => [
2159
        "left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1"
2160
    ]
2161
);
2162
is_deeply($result->all, [{key1 => 1, key2 => 2}]);
2163
$result = $dbi->select(
2164
    table => 'table1',
2165
    column => [{table2 => ['key3']}],
2166
    join => [
2167
        "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1"
2168
    ]
2169
);
2170
is_deeply($result->all, [{'table2.key3' => 4}]);
2171
$result = $dbi->select(
2172
    table => 'table1',
2173
    column => [{table2 => ['key3']}],
2174
    join => [
2175
        "left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'"
2176
    ]
2177
);
2178
is_deeply($result->all, [{'table2.key3' => 4}]);
2179

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2180
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2181
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2182
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2183
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2184
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2185
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
2186
$result = $dbi->select(
2187
    table => 'table1',
2188
    column => [{table2 => ['key3']}],
2189
    join => [
2190
        {
2191
            clause => "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1",
2192
            table => ['table1', 'table2']
2193
        }
2194
    ]
2195
);
2196
is_deeply($result->all, [{'table2.key3' => 4}]);
2197

            
2198
test 'mycolumn';
cleanup test
Yuki Kimoto authored on 2011-08-06
2199
$dbi = MyDBI8->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2200
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2201
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2202
$dbi->setup_model;
2203
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2204
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2205
$model = $dbi->model('table1');
2206
$result = $model->select_at(
2207
    column => [
2208
        $model->mycolumn,
2209
        $model->column('table2')
2210
    ]
2211
);
2212
is_deeply($result->one,
2213
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
2214

            
2215
$result = $model->select_at(
2216
    column => [
2217
        $model->mycolumn(['key1']),
2218
        $model->column(table2 => ['key1'])
2219
    ]
2220
);
2221
is_deeply($result->one,
2222
          {key1 => 1, 'table2.key1' => 1});
2223
$result = $model->select_at(
2224
    column => [
2225
        $model->mycolumn(['key1']),
2226
        {table2 => ['key1']}
2227
    ]
2228
);
2229
is_deeply($result->one,
2230
          {key1 => 1, 'table2.key1' => 1});
2231

            
2232
$result = $model->select_at(
2233
    column => [
2234
        $model->mycolumn(['key1']),
2235
        ['table2.key1', as => 'table2.key1']
2236
    ]
2237
);
2238
is_deeply($result->one,
2239
          {key1 => 1, 'table2.key1' => 1});
2240

            
2241
$result = $model->select_at(
2242
    column => [
2243
        $model->mycolumn(['key1']),
2244
        ['table2.key1' => 'table2.key1']
2245
    ]
2246
);
2247
is_deeply($result->one,
2248
          {key1 => 1, 'table2.key1' => 1});
2249

            
2250
test 'dbi method from model';
2251
{
2252
    package MyDBI9;
2253
    
2254
    use base 'DBIx::Custom';
2255
    
2256
    sub connect {
2257
        my $self = shift->SUPER::connect(@_);
2258
        
2259
        $self->include_model('MyModel8')->setup_model;
2260
        
2261
        return $self;
2262
    }
2263
}
cleanup test
Yuki Kimoto authored on 2011-08-06
2264
$dbi = MyDBI9->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2265
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2266
$model = $dbi->model('table1');
2267
eval{$model->execute('select * from table1')};
2268
ok(!$@);
2269

            
2270
test 'column table option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2271
$dbi = MyDBI9->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2272
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2273
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2274
$dbi->setup_model;
2275
$dbi->execute('insert into table1 (key1, key2) values (1, 2);');
2276
$dbi->execute('insert into table2 (key1, key3) values (1, 4);');
2277
$model = $dbi->model('table1');
2278
$result = $model->select(
2279
    column => [
2280
        $model->column('table2', {alias => 'table2_alias'})
2281
    ],
2282
    where => {'table2_alias.key3' => 4}
2283
);
2284
is_deeply($result->one, 
2285
          {'table2_alias.key1' => 1, 'table2_alias.key3' => 4});
2286

            
2287
$dbi->separator('__');
2288
$result = $model->select(
2289
    column => [
2290
        $model->column('table2', {alias => 'table2_alias'})
2291
    ],
2292
    where => {'table2_alias.key3' => 4}
2293
);
2294
is_deeply($result->one, 
2295
          {'table2_alias__key1' => 1, 'table2_alias__key3' => 4});
2296

            
2297
$dbi->separator('-');
2298
$result = $model->select(
2299
    column => [
2300
        $model->column('table2', {alias => 'table2_alias'})
2301
    ],
2302
    where => {'table2_alias.key3' => 4}
2303
);
2304
is_deeply($result->one, 
2305
          {'table2_alias-key1' => 1, 'table2_alias-key3' => 4});
2306

            
2307
test 'type option'; # DEPRECATED!
2308
$dbi = DBIx::Custom->connect(
2309
    data_source => 'dbi:SQLite:dbname=:memory:',
2310
    dbi_option => {
2311
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
2312
    }
2313
);
2314
my $binary = pack("I3", 1, 2, 3);
2315
$dbi->execute('create table table1(key1, key2)');
2316
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
2317
$result = $dbi->select(table => 'table1');
2318
$row   = $result->one;
2319
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2320
$result = $dbi->execute('select length(key1) as key1_length from table1');
2321
$row = $result->one;
2322
is($row->{key1_length}, length $binary);
2323

            
2324
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [['key1'] => DBI::SQL_BLOB]);
2325
$result = $dbi->select(table => 'table1');
2326
$row   = $result->one;
2327
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2328
$result = $dbi->execute('select length(key1) as key1_length from table1');
2329
$row = $result->one;
2330
is($row->{key1_length}, length $binary);
2331

            
2332

            
2333
test 'bind_type option';
2334
$dbi = DBIx::Custom->connect(
2335
    data_source => 'dbi:SQLite:dbname=:memory:',
2336
    dbi_option => {
2337
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
2338
    }
2339
);
2340
$binary = pack("I3", 1, 2, 3);
2341
$dbi->execute('create table table1(key1, key2)');
2342
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, bind_type => [key1 => DBI::SQL_BLOB]);
2343
$result = $dbi->select(table => 'table1');
2344
$row   = $result->one;
2345
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2346
$result = $dbi->execute('select length(key1) as key1_length from table1');
2347
$row = $result->one;
2348
is($row->{key1_length}, length $binary);
2349

            
2350
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, bind_type => [['key1'] => DBI::SQL_BLOB]);
2351
$result = $dbi->select(table => 'table1');
2352
$row   = $result->one;
2353
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2354
$result = $dbi->execute('select length(key1) as key1_length from table1');
2355
$row = $result->one;
2356
is($row->{key1_length}, length $binary);
2357

            
2358
test 'model type attribute';
2359
$dbi = DBIx::Custom->connect(
2360
    data_source => 'dbi:SQLite:dbname=:memory:',
2361
    dbi_option => {
2362
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
2363
    }
2364
);
2365
$binary = pack("I3", 1, 2, 3);
2366
$dbi->execute('create table table1(key1, key2)');
2367
$model = $dbi->create_model(table => 'table1', bind_type => [key1 => DBI::SQL_BLOB]);
2368
$model->insert(param => {key1 => $binary, key2 => 'あ'});
2369
$result = $dbi->select(table => 'table1');
2370
$row   = $result->one;
2371
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2372
$result = $dbi->execute('select length(key1) as key1_length from table1');
2373
$row = $result->one;
2374
is($row->{key1_length}, length $binary);
2375

            
2376
test 'create_model';
cleanup test
Yuki Kimoto authored on 2011-08-06
2377
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2378
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2379
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2380

            
2381
$dbi->create_model(
2382
    table => 'table1',
2383
    join => [
2384
       'left outer join table2 on table1.key1 = table2.key1'
2385
    ],
2386
    primary_key => ['key1']
2387
);
2388
$model2 = $dbi->create_model(
2389
    table => 'table2'
2390
);
2391
$dbi->create_model(
2392
    table => 'table3',
2393
    filter => [
2394
        key1 => {in => sub { uc $_[0] }}
2395
    ]
2396
);
2397
$dbi->setup_model;
2398
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2399
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2400
$model = $dbi->model('table1');
2401
$result = $model->select(
2402
    column => [$model->mycolumn, $model->column('table2')],
2403
    where => {'table1.key1' => 1}
2404
);
2405
is_deeply($result->one,
2406
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
2407
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
2408

            
2409
test 'model method';
2410
test 'create_model';
cleanup test
Yuki Kimoto authored on 2011-08-06
2411
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2412
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2413
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2414
$model = $dbi->create_model(
2415
    table => 'table2'
2416
);
2417
$model->method(foo => sub { shift->select(@_) });
2418
is_deeply($model->foo->one, {key1 => 1, key3 => 3});
2419

            
2420
test 'merge_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
2421
$dbi = DBIx::Custom->new;
2422
$params = [
2423
    {key1 => 1, key2 => 2, key3 => 3},
2424
    {key1 => 1, key2 => 2},
2425
    {key1 => 1}
2426
];
2427
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]);
2428
is_deeply($param, {key1 => [1, 1, 1], key2 => [2, 2], key3 => 3});
2429

            
2430
$params = [
2431
    {key1 => [1, 2], key2 => 1, key3 => [1, 2]},
2432
    {key1 => [3, 4], key2 => [2, 3], key3 => 3}
2433
];
2434
$param = $dbi->merge_param($params->[0], $params->[1]);
2435
is_deeply($param, {key1 => [1, 2, 3, 4], key2 => [1, 2, 3], key3 => [1, 2, 3]});
cleanup test
Yuki Kimoto authored on 2011-08-06
2436

            
2437
test 'select() param option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2438
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2439
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2440
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2441
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
2442
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2443
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2444
$dbi->insert(table => 'table2', param => {key1 => 2, key3 => 5});
2445
$rows = $dbi->select(
2446
    table => 'table1',
2447
    column => 'table1.key1 as table1_key1, key2, key3',
2448
    where   => {'table1.key2' => 3},
2449
    join  => ['inner join (select * from table2 where {= table2.key3})' . 
2450
              ' as table2 on table1.key1 = table2.key1'],
2451
    param => {'table2.key3' => 5}
2452
)->all;
2453
is_deeply($rows, [{table1_key1 => 2, key2 => 3, key3 => 5}]);
2454

            
2455

            
2456
test 'select() wrap option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2457
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2458
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2459
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2460
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2461
$rows = $dbi->select(
2462
    table => 'table1',
2463
    column => 'key1',
2464
    wrap => ['select * from (', ') as t where key1 = 1']
2465
)->all;
2466
is_deeply($rows, [{key1 => 1}]);
2467

            
2468
eval {
2469
$dbi->select(
2470
    table => 'table1',
2471
    column => 'key1',
2472
    wrap => 'select * from ('
2473
)
2474
};
2475
like($@, qr/array/);
2476

            
2477
test 'select() string where';
cleanup test
Yuki Kimoto authored on 2011-08-06
2478
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2479
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2480
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2481
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2482
$rows = $dbi->select(
2483
    table => 'table1',
2484
    where => 'key1 = :key1 and key2 = :key2',
2485
    where_param => {key1 => 1, key2 => 2}
2486
)->all;
2487
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2488

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2489
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2490
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2491
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2492
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2493
$rows = $dbi->select(
2494
    table => 'table1',
2495
    where => [
2496
        'key1 = :key1 and key2 = :key2',
2497
        {key1 => 1, key2 => 2}
2498
    ]
2499
)->all;
2500
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2501

            
2502
test 'delete() string where';
cleanup test
Yuki Kimoto authored on 2011-08-06
2503
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2504
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2505
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2506
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2507
$dbi->delete(
2508
    table => 'table1',
2509
    where => 'key1 = :key1 and key2 = :key2',
2510
    where_param => {key1 => 1, key2 => 2}
2511
);
2512
$rows = $dbi->select(table => 'table1')->all;
2513
is_deeply($rows, [{key1 => 2, key2 => 3}]);
2514

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2515
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2516
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2517
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2518
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2519
$dbi->delete(
2520
    table => 'table1',
2521
    where => [
2522
        'key1 = :key1 and key2 = :key2',
2523
         {key1 => 1, key2 => 2}
2524
    ]
2525
);
2526
$rows = $dbi->select(table => 'table1')->all;
2527
is_deeply($rows, [{key1 => 2, key2 => 3}]);
2528

            
2529

            
2530
test 'update() string where';
cleanup test
Yuki Kimoto authored on 2011-08-06
2531
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2532
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2533
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2534
$dbi->update(
2535
    table => 'table1',
2536
    param => {key1 => 5},
2537
    where => 'key1 = :key1 and key2 = :key2',
2538
    where_param => {key1 => 1, key2 => 2}
2539
);
2540
$rows = $dbi->select(table => 'table1')->all;
2541
is_deeply($rows, [{key1 => 5, key2 => 2}]);
2542

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2543
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2544
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2545
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2546
$dbi->update(
2547
    table => 'table1',
2548
    param => {key1 => 5},
2549
    where => [
2550
        'key1 = :key1 and key2 = :key2',
2551
        {key1 => 1, key2 => 2}
2552
    ]
2553
);
2554
$rows = $dbi->select(table => 'table1')->all;
2555
is_deeply($rows, [{key1 => 5, key2 => 2}]);
2556

            
2557
test 'insert id and primary_key option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2558
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2559
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2560
$dbi->insert(
2561
    primary_key => ['key1', 'key2'], 
2562
    table => 'table1',
2563
    id => [1, 2],
2564
    param => {key3 => 3}
2565
);
2566
is($dbi->select(table => 'table1')->one->{key1}, 1);
2567
is($dbi->select(table => 'table1')->one->{key2}, 2);
2568
is($dbi->select(table => 'table1')->one->{key3}, 3);
2569

            
2570
$dbi->delete_all(table => 'table1');
2571
$dbi->insert(
2572
    primary_key => 'key1', 
2573
    table => 'table1',
2574
    id => 0,
2575
    param => {key2 => 2, key3 => 3}
2576
);
2577

            
2578
is($dbi->select(table => 'table1')->one->{key1}, 0);
2579
is($dbi->select(table => 'table1')->one->{key2}, 2);
2580
is($dbi->select(table => 'table1')->one->{key3}, 3);
2581

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2582
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2583
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2584
$dbi->insert(
2585
    {key3 => 3},
2586
    primary_key => ['key1', 'key2'], 
2587
    table => 'table1',
2588
    id => [1, 2],
2589
);
2590
is($dbi->select(table => 'table1')->one->{key1}, 1);
2591
is($dbi->select(table => 'table1')->one->{key2}, 2);
2592
is($dbi->select(table => 'table1')->one->{key3}, 3);
2593

            
2594

            
2595
test 'model insert id and primary_key option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2596
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2597
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2598
$dbi->model('table1')->insert(
2599
    id => [1, 2],
2600
    param => {key3 => 3}
2601
);
2602
$result = $dbi->model('table1')->select;
2603
$row = $result->one;
2604
is($row->{key1}, 1);
2605
is($row->{key2}, 2);
2606
is($row->{key3}, 3);
2607

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2608
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2609
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2610
$dbi->model('table1')->insert(
2611
    {key3 => 3},
2612
    id => [1, 2]
2613
);
2614
$result = $dbi->model('table1')->select;
2615
$row = $result->one;
2616
is($row->{key1}, 1);
2617
is($row->{key2}, 2);
2618
is($row->{key3}, 3);
2619

            
2620
test 'update and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2621
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2622
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2623
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2624
$dbi->update(
2625
    table => 'table1',
2626
    primary_key => ['key1', 'key2'],
2627
    id => [1, 2],
2628
    param => {key3 => 4}
2629
);
2630
is($dbi->select(table => 'table1')->one->{key1}, 1);
2631
is($dbi->select(table => 'table1')->one->{key2}, 2);
2632
is($dbi->select(table => 'table1')->one->{key3}, 4);
2633

            
2634
$dbi->delete_all(table => 'table1');
2635
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2636
$dbi->update(
2637
    table => 'table1',
2638
    primary_key => 'key1',
2639
    id => 0,
2640
    param => {key3 => 4}
2641
);
2642
is($dbi->select(table => 'table1')->one->{key1}, 0);
2643
is($dbi->select(table => 'table1')->one->{key2}, 2);
2644
is($dbi->select(table => 'table1')->one->{key3}, 4);
2645

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2646
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2647
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2648
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2649
$dbi->update(
2650
    {key3 => 4},
2651
    table => 'table1',
2652
    primary_key => ['key1', 'key2'],
2653
    id => [1, 2]
2654
);
2655
is($dbi->select(table => 'table1')->one->{key1}, 1);
2656
is($dbi->select(table => 'table1')->one->{key2}, 2);
2657
is($dbi->select(table => 'table1')->one->{key3}, 4);
2658

            
2659

            
2660
test 'model update and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2661
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2662
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2663
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2664
$dbi->model('table1')->update(
2665
    id => [1, 2],
2666
    param => {key3 => 4}
2667
);
2668
$result = $dbi->model('table1')->select;
2669
$row = $result->one;
2670
is($row->{key1}, 1);
2671
is($row->{key2}, 2);
2672
is($row->{key3}, 4);
2673

            
2674

            
2675
test 'delete and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2676
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2677
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2678
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2679
$dbi->delete(
2680
    table => 'table1',
2681
    primary_key => ['key1', 'key2'],
2682
    id => [1, 2],
2683
);
2684
is_deeply($dbi->select(table => 'table1')->all, []);
2685

            
2686
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2687
$dbi->delete(
2688
    table => 'table1',
2689
    primary_key => 'key1',
2690
    id => 0,
2691
);
2692
is_deeply($dbi->select(table => 'table1')->all, []);
2693

            
2694

            
2695
test 'model delete and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2696
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2697
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2698
$dbi->execute("create table table2 (key1, key2, key3)");
2699
$dbi->execute("create table table3 (key1, key2, key3)");
2700
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2701
$dbi->model('table1')->delete(id => [1, 2]);
2702
is_deeply($dbi->select(table => 'table1')->all, []);
2703
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
2704
$dbi->model('table1_1')->delete(id => [1, 2]);
2705
is_deeply($dbi->select(table => 'table1')->all, []);
2706
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
2707
$dbi->model('table1_3')->delete(id => [1, 2]);
2708
is_deeply($dbi->select(table => 'table1')->all, []);
2709

            
2710

            
2711
test 'select and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2712
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2713
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2714
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2715
$result = $dbi->select(
2716
    table => 'table1',
2717
    primary_key => ['key1', 'key2'],
2718
    id => [1, 2]
2719
);
2720
$row = $result->one;
2721
is($row->{key1}, 1);
2722
is($row->{key2}, 2);
2723
is($row->{key3}, 3);
2724

            
2725
$dbi->delete_all(table => 'table1');
2726
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2727
$result = $dbi->select(
2728
    table => 'table1',
2729
    primary_key => 'key1',
2730
    id => 0,
2731
);
2732
$row = $result->one;
2733
is($row->{key1}, 0);
2734
is($row->{key2}, 2);
2735
is($row->{key3}, 3);
2736

            
2737
$dbi->delete_all(table => 'table1');
2738
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2739
$result = $dbi->select(
2740
    table => 'table1',
2741
    primary_key => ['key1', 'key2'],
2742
    id => [1, 2]
2743
);
2744
$row = $result->one;
2745
is($row->{key1}, 1);
2746
is($row->{key2}, 2);
2747
is($row->{key3}, 3);
2748

            
2749

            
2750
test 'model select_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
2751
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2752
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2753
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2754
$result = $dbi->model('table1')->select(id => [1, 2]);
2755
$row = $result->one;
2756
is($row->{key1}, 1);
2757
is($row->{key2}, 2);
2758
is($row->{key3}, 3);
2759

            
2760
test 'column separator is default .';
cleanup test
Yuki Kimoto authored on 2011-08-06
2761
$dbi = MyDBI7->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2762
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2763
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2764
$dbi->setup_model;
2765
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2766
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2767
$model = $dbi->model('table1');
2768
$result = $model->select(
2769
    column => [$model->column('table2')],
2770
    where => {'table1.key1' => 1}
2771
);
2772
is_deeply($result->one,
2773
          {'table2.key1' => 1, 'table2.key3' => 3});
2774

            
2775
$result = $model->select(
2776
    column => [$model->column('table2' => [qw/key1 key3/])],
2777
    where => {'table1.key1' => 1}
2778
);
2779
is_deeply($result->one,
2780
          {'table2.key1' => 1, 'table2.key3' => 3});
2781

            
2782

            
2783
test 'type_rule from';
cleanup test
Yuki Kimoto authored on 2011-08-06
2784
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2785
$dbi->type_rule(
2786
    from1 => {
2787
        date => sub { uc $_[0] }
2788
    }
2789
);
2790
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2791
$dbi->insert({key1 => 'a'}, table => 'table1');
2792
$result = $dbi->select(table => 'table1');
2793
is($result->fetch_first->[0], 'A');
2794

            
2795
$result = $dbi->select(table => 'table1');
2796
is($result->one->{key1}, 'A');
2797

            
2798

            
2799
test 'type_rule into';
cleanup test
Yuki Kimoto authored on 2011-08-06
2800
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2801
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2802
$dbi->type_rule(
2803
    into1 => {
2804
        date => sub { uc $_[0] }
2805
    }
2806
);
2807
$dbi->insert({key1 => 'a'}, table => 'table1');
2808
$result = $dbi->select(table => 'table1');
2809
is($result->one->{key1}, 'A');
2810

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2811
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2812
$dbi->execute("create table table1 (key1 date, key2 datetime)");
2813
$dbi->type_rule(
2814
    into1 => [
2815
         [qw/date datetime/] => sub { uc $_[0] }
2816
    ]
2817
);
2818
$dbi->insert({key1 => 'a', key2 => 'b'}, table => 'table1');
2819
$result = $dbi->select(table => 'table1');
2820
$row = $result->one;
2821
is($row->{key1}, 'A');
2822
is($row->{key2}, 'B');
2823

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2824
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2825
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2826
$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1');
2827
$dbi->type_rule(
2828
    into1 => [
2829
        [qw/date datetime/] => sub { uc $_[0] }
2830
    ]
2831
);
2832
$result = $dbi->execute(
2833
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
2834
    param => {key1 => 'a', 'table1.key2' => 'b'}
2835
);
2836
$row = $result->one;
2837
is($row->{key1}, 'a');
2838
is($row->{key2}, 'B');
2839

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2840
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2841
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2842
$dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1');
2843
$dbi->type_rule(
2844
    into1 => [
2845
        [qw/date datetime/] => sub { uc $_[0] }
2846
    ]
2847
);
2848
$result = $dbi->execute(
2849
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
2850
    param => {key1 => 'a', 'table1.key2' => 'b'},
2851
    table => 'table1'
2852
);
2853
$row = $result->one;
2854
is($row->{key1}, 'A');
2855
is($row->{key2}, 'B');
2856

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2857
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2858
$dbi->execute("create table table1 (key1 date, key2 datetime)");
2859
$dbi->register_filter(twice => sub { $_[0] * 2 });
2860
$dbi->type_rule(
2861
    from1 => {
2862
        date => 'twice',
2863
    },
2864
    into1 => {
2865
        date => 'twice',
2866
    }
2867
);
2868
$dbi->insert({key1 => 2}, table => 'table1');
2869
$result = $dbi->select(table => 'table1');
2870
is($result->fetch->[0], 8);
2871

            
2872
test 'type_rule and filter order';
cleanup test
Yuki Kimoto authored on 2011-08-06
2873
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2874
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2875
$dbi->type_rule(
2876
    into1 => {
2877
        date => sub { $_[0] . 'b' }
2878
    },
2879
    into2 => {
2880
        date => sub { $_[0] . 'c' }
2881
    },
2882
    from1 => {
2883
        date => sub { $_[0] . 'd' }
2884
    },
2885
    from2 => {
2886
        date => sub { $_[0] . 'e' }
2887
    }
2888
);
2889
$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }});
2890
$result = $dbi->select(table => 'table1');
2891
$result->filter(key1 => sub { $_[0] . 'f' });
2892
is($result->fetch_first->[0], '1abcdef');
2893

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2894
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2895
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2896
$dbi->type_rule(
2897
    from1 => {
2898
        date => sub { $_[0] . 'p' }
2899
    },
2900
    from2 => {
2901
        date => sub { $_[0] . 'q' }
2902
    },
2903
);
2904
$dbi->insert({key1 => '1'}, table => 'table1');
2905
$result = $dbi->select(table => 'table1');
2906
$result->type_rule(
2907
    from1 => {
2908
        date => sub { $_[0] . 'd' }
2909
    },
2910
    from2 => {
2911
        date => sub { $_[0] . 'e' }
2912
    }
2913
);
2914
$result->filter(key1 => sub { $_[0] . 'f' });
2915
is($result->fetch_first->[0], '1def');
2916

            
2917
test 'type_rule_off';
cleanup test
Yuki Kimoto authored on 2011-08-06
2918
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2919
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2920
$dbi->type_rule(
2921
    from1 => {
2922
        date => sub { $_[0] * 2 },
2923
    },
2924
    into1 => {
2925
        date => sub { $_[0] * 2 },
2926
    }
2927
);
2928
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
2929
$result = $dbi->select(table => 'table1', type_rule_off => 1);
2930
is($result->type_rule_off->fetch->[0], 2);
2931

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2932
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2933
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2934
$dbi->type_rule(
2935
    from1 => {
2936
        date => sub { $_[0] * 2 },
2937
    },
2938
    into1 => {
2939
        date => sub { $_[0] * 3 },
2940
    }
2941
);
2942
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
2943
$result = $dbi->select(table => 'table1', type_rule_off => 1);
2944
is($result->one->{key1}, 4);
2945

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2946
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2947
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2948
$dbi->type_rule(
2949
    from1 => {
2950
        date => sub { $_[0] * 2 },
2951
    },
2952
    into1 => {
2953
        date => sub { $_[0] * 3 },
2954
    }
2955
);
2956
$dbi->insert({key1 => 2}, table => 'table1');
2957
$result = $dbi->select(table => 'table1');
2958
is($result->one->{key1}, 12);
2959

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2960
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2961
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2962
$dbi->type_rule(
2963
    from1 => {
2964
        date => sub { $_[0] * 2 },
2965
    },
2966
    into1 => {
2967
        date => sub { $_[0] * 3 },
2968
    }
2969
);
2970
$dbi->insert({key1 => 2}, table => 'table1');
2971
$result = $dbi->select(table => 'table1');
2972
is($result->fetch->[0], 12);
2973

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2974
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2975
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2976
$dbi->register_filter(ppp => sub { uc $_[0] });
2977
$dbi->type_rule(
2978
    into1 => {
2979
        date => 'ppp'
2980
    }
2981
);
2982
$dbi->insert({key1 => 'a'}, table => 'table1');
2983
$result = $dbi->select(table => 'table1');
2984
is($result->one->{key1}, 'A');
2985

            
2986
eval{$dbi->type_rule(
2987
    into1 => {
2988
        date => 'pp'
2989
    }
2990
)};
2991
like($@, qr/not registered/);
2992

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2993
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2994
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2995
eval {
2996
    $dbi->type_rule(
2997
        from1 => {
2998
            Date => sub { $_[0] * 2 },
2999
        }
3000
    );
3001
};
3002
like($@, qr/lower/);
3003

            
3004
eval {
3005
    $dbi->type_rule(
3006
        into1 => {
3007
            Date => sub { $_[0] * 2 },
3008
        }
3009
    );
3010
};
3011
like($@, qr/lower/);
3012

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3013
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3014
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3015
$dbi->type_rule(
3016
    from1 => {
3017
        date => sub { $_[0] * 2 },
3018
    },
3019
    into1 => {
3020
        date => sub { $_[0] * 3 },
3021
    }
3022
);
3023
$dbi->insert({key1 => 2}, table => 'table1');
3024
$result = $dbi->select(table => 'table1');
3025
$result->type_rule_off;
3026
is($result->one->{key1}, 6);
3027

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3028
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3029
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3030
$dbi->type_rule(
3031
    from1 => {
3032
        date => sub { $_[0] * 2 },
3033
        datetime => sub { $_[0] * 4 },
3034
    },
3035
);
3036
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
3037
$result = $dbi->select(table => 'table1');
3038
$result->type_rule(
3039
    from1 => {
3040
        date => sub { $_[0] * 3 }
3041
    }
3042
);
3043
$row = $result->one;
3044
is($row->{key1}, 6);
3045
is($row->{key2}, 2);
3046

            
3047
$result = $dbi->select(table => 'table1');
3048
$result->type_rule(
3049
    from1 => {
3050
        date => sub { $_[0] * 3 }
3051
    }
3052
);
3053
$row = $result->one;
3054
is($row->{key1}, 6);
3055
is($row->{key2}, 2);
3056

            
3057
$result = $dbi->select(table => 'table1');
3058
$result->type_rule(
3059
    from1 => {
3060
        date => sub { $_[0] * 3 }
3061
    }
3062
);
3063
$row = $result->one;
3064
is($row->{key1}, 6);
3065
is($row->{key2}, 2);
3066
$result = $dbi->select(table => 'table1');
3067
$result->type_rule(
3068
    from1 => [date => sub { $_[0] * 3 }]
3069
);
3070
$row = $result->one;
3071
is($row->{key1}, 6);
3072
is($row->{key2}, 2);
3073
$dbi->register_filter(fivetimes => sub { $_[0] * 5});
3074
$result = $dbi->select(table => 'table1');
3075
$result->type_rule(
3076
    from1 => [date => 'fivetimes']
3077
);
3078
$row = $result->one;
3079
is($row->{key1}, 10);
3080
is($row->{key2}, 2);
3081
$result = $dbi->select(table => 'table1');
3082
$result->type_rule(
3083
    from1 => [date => undef]
3084
);
3085
$row = $result->one;
3086
is($row->{key1}, 2);
3087
is($row->{key2}, 2);
3088

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3089
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3090
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3091
$dbi->type_rule(
3092
    from1 => {
3093
        date => sub { $_[0] * 2 },
3094
    },
3095
);
3096
$dbi->insert({key1 => 2}, table => 'table1');
3097
$result = $dbi->select(table => 'table1');
3098
$result->filter(key1 => sub { $_[0] * 3 });
3099
is($result->one->{key1}, 12);
3100

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3101
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3102
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3103
$dbi->type_rule(
3104
    from1 => {
3105
        date => sub { $_[0] * 2 },
3106
    },
3107
);
3108
$dbi->insert({key1 => 2}, table => 'table1');
3109
$result = $dbi->select(table => 'table1');
3110
$result->filter(key1 => sub { $_[0] * 3 });
3111
is($result->fetch->[0], 12);
3112

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3113
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3114
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3115
$dbi->type_rule(
3116
    into1 => {
3117
        date => sub { $_[0] . 'b' }
3118
    },
3119
    into2 => {
3120
        date => sub { $_[0] . 'c' }
3121
    },
3122
    from1 => {
3123
        date => sub { $_[0] . 'd' }
3124
    },
3125
    from2 => {
3126
        date => sub { $_[0] . 'e' }
3127
    }
3128
);
3129
$dbi->insert({key1 => '1'}, table => 'table1', type_rule_off => 1);
3130
$result = $dbi->select(table => 'table1');
3131
is($result->type_rule_off->fetch_first->[0], '1');
3132
$result = $dbi->select(table => 'table1');
3133
is($result->type_rule_on->fetch_first->[0], '1de');
3134

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3135
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3136
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3137
$dbi->type_rule(
3138
    into1 => {
3139
        date => sub { $_[0] . 'b' }
3140
    },
3141
    into2 => {
3142
        date => sub { $_[0] . 'c' }
3143
    },
3144
    from1 => {
3145
        date => sub { $_[0] . 'd' }
3146
    },
3147
    from2 => {
3148
        date => sub { $_[0] . 'e' }
3149
    }
3150
);
3151
$dbi->insert({key1 => '1'}, table => 'table1', type_rule1_off => 1);
3152
$result = $dbi->select(table => 'table1');
3153
is($result->type_rule1_off->fetch_first->[0], '1ce');
3154
$result = $dbi->select(table => 'table1');
3155
is($result->type_rule1_on->fetch_first->[0], '1cde');
3156

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3157
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3158
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3159
$dbi->type_rule(
3160
    into1 => {
3161
        date => sub { $_[0] . 'b' }
3162
    },
3163
    into2 => {
3164
        date => sub { $_[0] . 'c' }
3165
    },
3166
    from1 => {
3167
        date => sub { $_[0] . 'd' }
3168
    },
3169
    from2 => {
3170
        date => sub { $_[0] . 'e' }
3171
    }
3172
);
3173
$dbi->insert({key1 => '1'}, table => 'table1', type_rule2_off => 1);
3174
$result = $dbi->select(table => 'table1');
3175
is($result->type_rule2_off->fetch_first->[0], '1bd');
3176
$result = $dbi->select(table => 'table1');
3177
is($result->type_rule2_on->fetch_first->[0], '1bde');
3178

            
3179
test 'separator';
cleanup test
Yuki Kimoto authored on 2011-08-06
3180
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
3181
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
3182
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
3183

            
3184
$dbi->create_model(
3185
    table => 'table1',
3186
    join => [
3187
       'left outer join table2 on table1.key1 = table2.key1'
3188
    ],
3189
    primary_key => ['key1'],
3190
);
3191
$model2 = $dbi->create_model(
3192
    table => 'table2',
3193
);
3194
$dbi->setup_model;
3195
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3196
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
3197
$model = $dbi->model('table1');
3198
$result = $model->select(
3199
    column => [
3200
        $model->mycolumn,
3201
        {table2 => [qw/key1 key3/]}
3202
    ],
3203
    where => {'table1.key1' => 1}
3204
);
3205
is_deeply($result->one,
3206
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
3207
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3208

            
3209
$dbi->separator('__');
3210
$model = $dbi->model('table1');
3211
$result = $model->select(
3212
    column => [
3213
        $model->mycolumn,
3214
        {table2 => [qw/key1 key3/]}
3215
    ],
3216
    where => {'table1.key1' => 1}
3217
);
3218
is_deeply($result->one,
3219
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
3220
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3221

            
3222
$dbi->separator('-');
3223
$model = $dbi->model('table1');
3224
$result = $model->select(
3225
    column => [
3226
        $model->mycolumn,
3227
        {table2 => [qw/key1 key3/]}
3228
    ],
3229
    where => {'table1.key1' => 1}
3230
);
3231
is_deeply($result->one,
3232
          {key1 => 1, key2 => 2, 'table2-key1' => 1, 'table2-key3' => 3});
3233
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3234

            
3235

            
3236
test 'filter_off';
cleanup test
Yuki Kimoto authored on 2011-08-06
3237
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
3238
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
3239
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
3240

            
3241
$dbi->create_model(
3242
    table => 'table1',
3243
    join => [
3244
       'left outer join table2 on table1.key1 = table2.key1'
3245
    ],
3246
    primary_key => ['key1'],
3247
);
3248
$dbi->setup_model;
3249
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3250
$model = $dbi->model('table1');
3251
$result = $model->select(column => 'key1');
3252
$result->filter(key1 => sub { $_[0] * 2 });
3253
is_deeply($result->one, {key1 => 2});
3254

            
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
3255
test 'available_datetype';
cleanup test
Yuki Kimoto authored on 2011-08-06
3256
$dbi = DBIx::Custom->connect(%memory);
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
3257
ok($dbi->can('available_datatype'));
cleanup test
Yuki Kimoto authored on 2011-08-06
3258

            
3259

            
3260
test 'select prefix option';
cleanup test
Yuki Kimoto authored on 2011-08-06
3261
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
3262
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
3263
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3264
$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all;
3265
is_deeply($rows, [{key1 => 1, key2 => 2}], "table");
3266

            
3267

            
3268
test 'separator';
cleanup test
Yuki Kimoto authored on 2011-08-06
3269
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3270
is($dbi->separator, '.');
3271
$dbi->separator('-');
3272
is($dbi->separator, '-');
3273
$dbi->separator('__');
3274
is($dbi->separator, '__');
3275
eval { $dbi->separator('?') };
3276
like($@, qr/Separator/);
3277

            
3278

            
3279
test 'map_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
3280
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3281
$param = $dbi->map_param(
3282
    {id => 1, author => 'Ken', price => 1900},
3283
    id => 'book.id',
3284
    author => ['book.author', sub { '%' . $_[0] . '%' }],
3285
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
3286
);
3287
is_deeply($param, {'book.id' => 1, 'book.author' => '%Ken%',
3288
  'book.price' => 1900});
3289

            
3290
$param = $dbi->map_param(
3291
    {id => 0, author => 0, price => 0},
3292
    id => 'book.id',
3293
    author => ['book.author', sub { '%' . $_[0] . '%' }],
3294
    price => ['book.price', sub { '%' . $_[0] . '%' },
3295
      {if => sub { $_[0] eq 0 }}]
3296
);
3297
is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
3298

            
3299
$param = $dbi->map_param(
3300
    {id => '', author => '', price => ''},
3301
    id => 'book.id',
3302
    author => ['book.author', sub { '%' . $_[0] . '%' }],
3303
    price => ['book.price', sub { '%' . $_[0] . '%' },
3304
      {if => sub { $_[0] eq 1 }}]
3305
);
3306
is_deeply($param, {});
3307

            
3308
$param = $dbi->map_param(
3309
    {id => undef, author => undef, price => undef},
3310
    id => 'book.id',
3311
    price => ['book.price', {if => 'exists'}]
3312
);
3313
is_deeply($param, {'book.price' => undef});
3314

            
3315
$param = $dbi->map_param(
3316
    {price => 'a'},
3317
    id => ['book.id', {if => 'exists'}],
3318
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
3319
);
3320
is_deeply($param, {'book.price' => '%a'});
3321

            
3322

            
3323
test 'table_alias';
cleanup test
Yuki Kimoto authored on 2011-08-06
3324
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3325
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3326
$dbi->type_rule(
3327
    into1 => {
3328
        date => sub { uc $_[0] }
3329
    }
3330
);
3331
$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'},
3332
  table_alias => {table2 => 'table1'});
3333
$result = $dbi->select(table => 'table1');
3334
is($result->one->{key1}, 'A');
3335

            
3336

            
3337
test 'order';
cleanup test
Yuki Kimoto authored on 2011-08-06
3338
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3339
$dbi->execute("create table table1 (key1, key2)");
3340
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
3341
$dbi->insert({key1 => 1, key2 => 3}, table => 'table1');
3342
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
3343
$dbi->insert({key1 => 2, key2 => 4}, table => 'table1');
3344
my $order = $dbi->order;
3345
$order->prepend('key1', 'key2 desc');
3346
$result = $dbi->select(table => 'table1', append => "$order");
3347
is_deeply($result->all, [{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1},
3348
  {key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}]);
3349
$order->prepend('key1 desc');
3350
$result = $dbi->select(table => 'table1', append => "$order");
3351
is_deeply($result->all, [{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2},
3352
  {key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}]);
3353

            
3354
$order = $dbi->order;
3355
$order->prepend(['table1-key1'], [qw/table1-key2 desc/]);
3356
$result = $dbi->select(table => 'table1',
3357
  column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']],
3358
  append => "$order");
3359
is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3},
3360
  {'table1-key1' => 1, 'table1-key2' => 1},
3361
  {'table1-key1' => 2, 'table1-key2' => 4},
3362
  {'table1-key1' => 2, 'table1-key2' => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
3363

            
3364
test 'tag_parse';
cleanup test
Yuki Kimoto authored on 2011-08-06
3365
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3366
$dbi->tag_parse(0);
cleanup test
Yuki Kimoto authored on 2011-08-06
3367
$dbi->execute("create table table1 (key1, key2)");
3368
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
3369
eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})};
3370
ok($@);
cleanup test
Yuki Kimoto authored on 2011-08-06
3371

            
3372
test 'last_sql';
cleanup test
Yuki Kimoto authored on 2011-08-06
3373
$dbi = DBIx::Custom->connect(%memory);
3374
$dbi->execute("create table table1 (key1, key2)");
3375
$dbi->execute('select * from table1');
3376
is($dbi->last_sql, 'select * from table1;');
3377

            
3378
eval{$dbi->execute("aaa")};
3379
is($dbi->last_sql, 'aaa;');
cleanup test
Yuki Kimoto authored on 2011-08-06
3380

            
3381
test 'DBIx::Custom header';
cleanup test
Yuki Kimoto authored on 2011-08-06
3382
$dbi = DBIx::Custom->connect(%memory);
3383
$dbi->execute("create table table1 (key1, key2)");
3384
$result = $dbi->execute('select key1 as h1, key2 as h2 from table1');
3385
is_deeply($result->header, [qw/h1 h2/]);
cleanup test
Yuki Kimoto authored on 2011-08-06
3386

            
3387
test 'Named placeholder :name(operater) syntax';
cleanup test
Yuki Kimoto authored on 2011-08-06
3388
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3389
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
3390
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3391
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3392

            
3393
$source = "select * from table1 where :key1{=} and :key2{=}";
3394
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
3395
$rows = $result->all;
3396
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3397

            
3398
$source = "select * from table1 where :key1{ = } and :key2{=}";
3399
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
3400
$rows = $result->all;
3401
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3402

            
3403
$source = "select * from table1 where :key1{<} and :key2{=}";
3404
$result = $dbi->execute($source, param => {key1 => 5, key2 => 2});
3405
$rows = $result->all;
3406
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3407

            
3408
$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}";
3409
$result = $dbi->execute(
3410
    $source,
3411
    param => {'table1.key1' => 1, 'table1.key2' => 1},
3412
    filter => {'table1.key2' => sub { $_[0] * 2 }}
3413
);
3414
$rows = $result->all;
3415
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3416

            
3417
test 'high perfomance way';
cleanup test
Yuki Kimoto authored on 2011-08-06
3418
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
3419
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
3420
$rows = [
3421
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3422
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3423
];
3424
{
3425
    my $query;
3426
    foreach my $row (@$rows) {
3427
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
3428
      $dbi->execute($query, $row, filter => {ab => sub { $_[0] * 2 }});
3429
    }
3430
    is_deeply($dbi->select(table => 'table1')->all,
3431
      [
3432
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3433
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3434
      ]
3435
    );
3436
}
3437

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3438
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
3439
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
3440
$rows = [
3441
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3442
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3443
];
3444
{
3445
    my $query;
3446
    my $sth;
3447
    foreach my $row (@$rows) {
3448
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
3449
      $sth ||= $query->sth;
3450
      $sth->execute(map { $row->{$_} } sort keys %$row);
3451
    }
3452
    is_deeply($dbi->select(table => 'table1')->all,
3453
      [
3454
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3455
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3456
      ]
3457
    );
3458
}
cleanup test
Yuki Kimoto authored on 2011-08-06
3459

            
clenup test
Yuki Kimoto authored on 2011-08-06
3460
test 'result';
3461
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
3462
$dbi->execute($create_table1);
clenup test
Yuki Kimoto authored on 2011-08-06
3463
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3464
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3465

            
3466
$result = $dbi->select(table => 'table1');
3467
@rows = ();
3468
while (my $row = $result->fetch) {
3469
    push @rows, [@$row];
3470
}
3471
is_deeply(\@rows, [[1, 2], [3, 4]]);
3472

            
3473
$result = $dbi->select(table => 'table1');
3474
@rows = ();
3475
while (my $row = $result->fetch_hash) {
3476
    push @rows, {%$row};
3477
}
3478
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
3479

            
3480
$result = $dbi->select(table => 'table1');
3481
$row = $result->fetch_first;
3482
is_deeply($row, [1, 2], "row");
3483
$row = $result->fetch;
3484
ok(!$row, "finished");
3485

            
3486
$result = $dbi->select(table => 'table1');
3487
$row = $result->fetch_hash_first;
3488
is_deeply($row, {key1 => 1, key2 => 2}, "row");
3489
$row = $result->fetch_hash;
3490
ok(!$row, "finished");
3491

            
3492
$dbi->execute('create table table2 (key1, key2);');
3493
$result = $dbi->select(table => 'table2');
3494
$row = $result->fetch_hash_first;
3495
ok(!$row, "no row fetch");
3496

            
3497
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
3498
$dbi->execute($create_table1);
clenup test
Yuki Kimoto authored on 2011-08-06
3499
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3500
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3501
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1');
3502
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1');
3503
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1');
3504
$result = $dbi->select(table => 'table1');
3505
$rows = $result->fetch_multi(2);
3506
is_deeply($rows, [[1, 2],
3507
                  [3, 4]], "fetch_multi first");
3508
$rows = $result->fetch_multi(2);
3509
is_deeply($rows, [[5, 6],
3510
                  [7, 8]], "fetch_multi secound");
3511
$rows = $result->fetch_multi(2);
3512
is_deeply($rows, [[9, 10]], "fetch_multi third");
3513
$rows = $result->fetch_multi(2);
3514
ok(!$rows);
3515

            
3516
$result = $dbi->select(table => 'table1');
3517
eval {$result->fetch_multi};
3518
like($@, qr/Row count must be specified/, "Not specified row count");
3519

            
3520
$result = $dbi->select(table => 'table1');
3521
$rows = $result->fetch_hash_multi(2);
3522
is_deeply($rows, [{key1 => 1, key2 => 2},
3523
                  {key1 => 3, key2 => 4}], "fetch_multi first");
3524
$rows = $result->fetch_hash_multi(2);
3525
is_deeply($rows, [{key1 => 5, key2 => 6},
3526
                  {key1 => 7, key2 => 8}], "fetch_multi secound");
3527
$rows = $result->fetch_hash_multi(2);
3528
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third");
3529
$rows = $result->fetch_hash_multi(2);
3530
ok(!$rows);
3531

            
3532
$result = $dbi->select(table => 'table1');
3533
eval {$result->fetch_hash_multi};
3534
like($@, qr/Row count must be specified/, "Not specified row count");
3535

            
3536
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
3537
$dbi->execute($create_table1);
clenup test
Yuki Kimoto authored on 2011-08-06
3538
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3539
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3540

            
3541
test 'fetch_all';
3542
$result = $dbi->select(table => 'table1');
3543
$rows = $result->fetch_all;
3544
is_deeply($rows, [[1, 2], [3, 4]]);
3545

            
3546
$result = $dbi->select(table => 'table1');
3547
$rows = $result->fetch_hash_all;
3548
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
3549

            
3550
$result = $dbi->select(table => 'table1');
3551
$result->dbi->filters({three_times => sub { $_[0] * 3}});
3552
$result->filter({key1 => 'three_times'});
3553

            
3554
$rows = $result->fetch_all;
3555
is_deeply($rows, [[3, 2], [9, 4]], "array");
3556

            
3557
$result = $dbi->select(table => 'table1');
3558
$result->dbi->filters({three_times => sub { $_[0] * 3}});
3559
$result->filter({key1 => 'three_times'});
3560
$rows = $result->fetch_hash_all;
3561
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash");
3562

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3563
test "query_builder";
3564
$datas = [
3565
    # Basic tests
3566
    {   name            => 'placeholder basic',
3567
        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
3568
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;",
3569
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
3570
    },
3571
    {
3572
        name            => 'placeholder in',
3573
        source            => "{in k1 3};",
3574
        sql_expected    => "k1 in (?, ?, ?);",
3575
        columns_expected   => [qw/k1 k1 k1/]
3576
    },
3577
    
3578
    # Table name
3579
    {
3580
        name            => 'placeholder with table name',
3581
        source            => "{= a.k1} {= a.k2}",
3582
        sql_expected    => "a.k1 = ? a.k2 = ?;",
3583
        columns_expected  => [qw/a.k1 a.k2/]
3584
    },
3585
    {   
3586
        name            => 'placeholder in with table name',
3587
        source            => "{in a.k1 2} {in b.k2 2}",
3588
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?);",
3589
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
3590
    },
3591
    {
3592
        name            => 'not contain tag',
3593
        source            => "aaa",
3594
        sql_expected    => "aaa;",
3595
        columns_expected  => [],
3596
    }
3597
];
3598

            
3599
for (my $i = 0; $i < @$datas; $i++) {
3600
    my $data = $datas->[$i];
3601
    my $builder = DBIx::Custom->new->query_builder;
3602
    my $query = $builder->build_query($data->{source});
3603
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
3604
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
3605
}
3606

            
3607
$builder = DBIx::Custom->new->query_builder;
3608
$ret_val = $builder->register_tag(
3609
    p => sub {
3610
        my @args = @_;
3611
        
3612
        my $expand    = "? $args[0] $args[1]";
3613
        my $columns = [2];
3614
        return [$expand, $columns];
3615
    }
3616
);
3617

            
3618
$query = $builder->build_query("{p a b}");
3619
is($query->{sql}, "? a b;", "register_tag sql");
3620
is_deeply($query->{columns}, [2], "register_tag columns");
3621
isa_ok($ret_val, 'DBIx::Custom::QueryBuilder');
3622

            
3623
$builder = DBIx::Custom->new->query_builder;
3624

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

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

            
3631
$builder->register_tag({
3632
    q => 'string'
3633
});
3634

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

            
3638
$builder->register_tag({
3639
   r => sub {} 
3640
});
3641

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

            
3645
$builder->register_tag({
3646
   s => sub { return ["a", ""]} 
3647
});
3648

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

            
3652
$builder->register_tag(
3653
    t => sub {return ["a", []]}
3654
);
3655

            
3656

            
3657
test 'General error case';
3658
$builder = DBIx::Custom->new->query_builder;
3659
$builder->register_tag(
3660
    a => sub {
3661
        return ["? ? ?", ['']];
3662
    }
3663
);
3664
eval{$builder->build_query("{a}")};
3665
like($@, qr/\QPlaceholder count/, "placeholder count is invalid");
3666

            
3667

            
3668
test 'Default tag Error case';
3669
eval{$builder->build_query("{= }")};
3670
like($@, qr/Column name must be specified in tag "{= }"/, "basic '=' : key not exist");
3671

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

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

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

            
3683
test 'variouse source';
3684
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;";
3685
$query = $builder->build_query($source);
3686
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1");
3687

            
3688
$source = "abc;";
3689
$query = $builder->build_query($source);
3690
is($query->sql, 'abc;', "basic : 2");
3691

            
3692
$source = "{= a}";
3693
$query = $builder->build_query($source);
3694
is($query->sql, 'a = ?;', "only tag");
3695

            
3696
$source = "000;";
3697
$query = $builder->build_query($source);
3698
is($query->sql, '000;', "contain 0 value");
3699

            
3700
$source = "a {= b} }";
3701
eval{$builder->build_query($source)};
3702
like($@, qr/unexpected "}"/, "error : 1");
3703

            
3704
$source = "a {= {}";
3705
eval{$builder->build_query($source)};
3706
like($@, qr/unexpected "{"/, "error : 2");
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
3707