DBIx-Custom / t / sqlite.t /
Newer Older
3702 lines | 119.773kb
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:');
24
my $create_table_default = 'create table table1 (key1 char(255), key2 char(255));';
25

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

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

            
60

            
61
test 'insert';
cleanup test
Yuki Kimoto authored on 2011-08-06
62
$dbi = DBIx::Custom->connect(%memory);
63
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
64
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
65
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-06
66
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
67
$rows   = $result->all;
68
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
69

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
82
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
83
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
84
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, append => '   ');
85
$rows = $dbi->select(table => 'table1')->all;
86
is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append');
87

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

            
91
eval{$dbi->insert(table => 'table', param => {';' => 1})};
92
like($@, qr/safety/);
93

            
cleanup test
Yuki Kimoto authored on 2011-08-06
94
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
95
$dbi->quote('"');
96
$dbi->execute('create table "table" ("select")');
97
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
98
$dbi->insert(table => 'table', param => {select => 1});
99
$result = $dbi->execute('select * from "table"');
100
$rows   = $result->all;
101
is_deeply($rows, [{select => 2}], "reserved word");
102

            
cleanup test
Yuki Kimoto authored on 2011-08-06
103
$dbi = DBIx::Custom->connect(%memory);
104
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
105
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
106
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
test cleanup
Yuki Kimoto authored on 2011-08-06
107
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
108
$rows   = $result->all;
109
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
110

            
cleanup test
Yuki Kimoto authored on 2011-08-06
111
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
112
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
113
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
114
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace');
test cleanup
Yuki Kimoto authored on 2011-08-06
115
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
116
$rows   = $result->all;
117
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
118

            
cleanup test
Yuki Kimoto authored on 2011-08-06
119
$dbi = DBIx::Custom->connect(%memory);
120
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
121
$dbi->insert(table => 'table1', param => {key1 => \"'1'", key2 => 2});
122
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-06
123
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
124
$rows   = $result->all;
125
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
126

            
127
test 'update';
cleanup test
Yuki Kimoto authored on 2011-08-06
128
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
129
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
130
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
131
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
132
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
133
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
134
$rows   = $result->all;
135
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
136
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
137
                  "basic");
138
                  
139
$dbi->execute("delete from table1");
140
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
141
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
142
$dbi->update(table => 'table1', param => {key2 => 12}, where => {key2 => 2, key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-06
143
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
144
$rows   = $result->all;
145
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
146
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
147
                  "update key same as search key");
148

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

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

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
176
$dbi = DBIx::Custom->connect(%memory);
177
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
178
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
179
$where = $dbi->where;
180
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
181
$where->param({key1 => 1, key2 => 2});
182
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
183
$result = $dbi->select(table => 'table1');
184
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
185

            
cleanup test
Yuki Kimoto authored on 2011-08-06
186
$dbi = DBIx::Custom->connect(%memory);
187
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
188
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
189
$dbi->update(
190
    table => 'table1',
191
    param => {key1 => 3},
192
    where => [
193
        ['and', 'key1 = :key1', 'key2 = :key2'],
194
        {key1 => 1, key2 => 2}
195
    ]
196
);
197
$result = $dbi->select(table => 'table1');
198
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
199

            
cleanup test
Yuki Kimoto authored on 2011-08-06
200
$dbi = DBIx::Custom->connect(%memory);
201
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
202
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
203
$where = $dbi->where;
204
$where->clause(['and', 'key2 = :key2']);
205
$where->param({key2 => 2});
206
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
207
$result = $dbi->select(table => 'table1');
208
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
209

            
210
eval{$dbi->update(table => 'table1', param => {';' => 1})};
211
like($@, qr/safety/);
212

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
216
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
217
$dbi->quote('"');
218
$dbi->execute('create table "table" ("select", "update")');
219
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
220
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
221
$dbi->insert(table => 'table', param => {select => 1});
222
$dbi->update(table => 'table', where => {select => 1}, param => {update => 2});
223
$result = $dbi->execute('select * from "table"');
224
$rows   = $result->all;
225
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
226

            
227
eval {$dbi->update_all(table => 'table', param => {';' => 2}) };
228
like($@, qr/safety/);
229

            
cleanup test
Yuki Kimoto authored on 2011-08-06
230
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
231
$dbi->reserved_word_quote('"');
232
$dbi->execute('create table "table" ("select", "update")');
233
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
234
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
235
$dbi->insert(table => 'table', param => {select => 1});
236
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
237
$result = $dbi->execute('select * from "table"');
238
$rows   = $result->all;
239
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
240

            
cleanup test
Yuki Kimoto authored on 2011-08-06
241
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
242
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
243
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
244
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
245
$dbi->update({key2 => 11}, table => 'table1', where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
246
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
247
$rows   = $result->all;
248
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
249
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
250
                  "basic");
251

            
cleanup test
Yuki Kimoto authored on 2011-08-06
252
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
253
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
254
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
255
$dbi->update(table => 'table1', param => {key2 => 4},
256
  where => {key1 => 1}, prefix => 'or replace');
test cleanup
Yuki Kimoto authored on 2011-08-06
257
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
258
$rows   = $result->all;
259
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
260

            
cleanup test
Yuki Kimoto authored on 2011-08-06
261
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
262
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
263
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
264
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
265
$dbi->update(table => 'table1', param => {key2 => \"'11'"}, where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
266
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
267
$rows   = $result->all;
268
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
269
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
270
                  "basic");
271

            
272
test 'update_all';
cleanup test
Yuki Kimoto authored on 2011-08-06
273
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
274
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
275
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
276
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
277
$dbi->register_filter(twice => sub { $_[0] * 2 });
278
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'});
test cleanup
Yuki Kimoto authored on 2011-08-06
279
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
280
$rows   = $result->all;
281
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
282
                  {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
283
                  "filter");
284

            
285

            
286
test 'delete';
cleanup test
Yuki Kimoto authored on 2011-08-06
287
$dbi = DBIx::Custom->connect(%memory);
288
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
289
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
290
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
291
$dbi->delete(table => 'table1', where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
292
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
293
$rows   = $result->all;
294
is_deeply($rows, [{key1 => 3, key2 => 4}], "basic");
295

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

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

            
307
$dbi->delete_all(table => 'table1');
308
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
309
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
310
$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2});
311
$rows = $dbi->select(table => 'table1')->all;
312
is_deeply($rows, [{key1 => 3, key2 => 4}], "delete multi key");
313

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
317
$dbi = DBIx::Custom->connect(%memory);
318
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
319
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
320
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
321
$where = $dbi->where;
322
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
323
$where->param({ke1 => 1, key2 => 2});
324
$dbi->delete(table => 'table1', where => $where);
325
$result = $dbi->select(table => 'table1');
326
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
327

            
cleanup test
Yuki Kimoto authored on 2011-08-06
328
$dbi = DBIx::Custom->connect(%memory);
329
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
330
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
331
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
332
$dbi->delete(
333
    table => 'table1',
334
    where => [
335
        ['and', 'key1 = :key1', 'key2 = :key2'],
336
        {ke1 => 1, key2 => 2}
337
    ]
338
);
339
$result = $dbi->select(table => 'table1');
340
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
341

            
cleanup test
Yuki Kimoto authored on 2011-08-06
342
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
343
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
344
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
345
$dbi->delete(table => 'table1', where => {key1 => 1}, prefix => '    ');
test cleanup
Yuki Kimoto authored on 2011-08-06
346
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
347
$rows   = $result->all;
348
is_deeply($rows, [], "basic");
349

            
350
test 'delete error';
cleanup test
Yuki Kimoto authored on 2011-08-06
351
$dbi = DBIx::Custom->connect(%memory);
352
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
353
eval{$dbi->delete(table => 'table1')};
354
like($@, qr/"where" must be specified/,
355
         "where key-value pairs not specified");
356

            
357
eval{$dbi->delete(table => 'table1', where => {';' => 1})};
358
like($@, qr/safety/);
359

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

            
370
test 'delete_all';
cleanup test
Yuki Kimoto authored on 2011-08-06
371
$dbi = DBIx::Custom->connect(%memory);
372
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
373
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
374
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
375
$dbi->delete_all(table => 'table1');
test cleanup
Yuki Kimoto authored on 2011-08-06
376
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
377
$rows   = $result->all;
378
is_deeply($rows, [], "basic");
379

            
380

            
381
test 'select';
cleanup test
Yuki Kimoto authored on 2011-08-06
382
$dbi = DBIx::Custom->connect(%memory);
383
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
384
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
385
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
386
$rows = $dbi->select(table => 'table1')->all;
387
is_deeply($rows, [{key1 => 1, key2 => 2},
388
                  {key1 => 3, key2 => 4}], "table");
389

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

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

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

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

            
402
$dbi->register_filter(decrement => sub { $_[0] - 1 });
403
$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'})
404
            ->all;
405
is_deeply($rows, [{key1 => 1, key2 => 2}], "filter");
406

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

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

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

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

            
436
test 'fetch filter';
cleanup test
Yuki Kimoto authored on 2011-08-06
437
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
438
$dbi->register_filter(
439
    twice       => sub { $_[0] * 2 },
440
    three_times => sub { $_[0] * 3 }
441
);
442
$dbi->default_fetch_filter('twice');
cleanup test
Yuki Kimoto authored on 2011-08-06
443
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
444
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
445
$result = $dbi->select(table => 'table1');
446
$result->filter({key1 => 'three_times'});
447
$row = $result->one;
448
is_deeply($row, {key1 => 3, key2 => 4}, "default_fetch_filter and filter");
449

            
450
test 'filters';
451
$dbi = DBIx::Custom->new;
452

            
453
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')),
454
   'あ', "decode_utf8");
455

            
456
is($dbi->filters->{encode_utf8}->('あ'),
457
   encode_utf8('あ'), "encode_utf8");
458

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
470
$dbi = DBIx::Custom->connect(%memory);
471
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
472
$dbi->dbh->begin_work(0);
473
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
474
$dbi->dbh->rollback;
475

            
476
$result = $dbi->select(table => 'table1');
477
ok(! $result->fetch_first, "rollback");
478

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
488
$dbi = DBIx::Custom->connect(%memory);
489
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
490
$dbi->{_cached} = {};
491
$dbi->cache(0);
492
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
493
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
494

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

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

            
513
$query = $dbi->execute('select * from table1 where key1 = :key1', {}, query => 1);
514
$dbi->dbh->disconnect;
515
eval{$dbi->execute($query, param => {key1 => {a => 1}})};
516
ok($@, "execute fail");
517

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

            
529

            
530
test 'transaction';
cleanup test
Yuki Kimoto authored on 2011-08-06
531
$dbi = DBIx::Custom->connect(%memory);
532
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
533

            
534
$dbi->begin_work;
535

            
536
eval {
537
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
538
    die "Error";
539
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
540
};
541

            
542
$dbi->rollback if $@;
543

            
544
$result = $dbi->select(table => 'table1');
545
$rows = $result->all;
546
is_deeply($rows, [], "rollback");
547

            
548
$dbi->begin_work;
549

            
550
eval {
551
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
552
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
553
};
554

            
555
$dbi->commit unless $@;
556

            
557
$result = $dbi->select(table => 'table1');
558
$rows = $result->all;
559
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "commit");
560

            
561
$dbi->dbh->{AutoCommit} = 0;
562
eval{ $dbi->begin_work };
563
ok($@, "exception");
564
$dbi->dbh->{AutoCommit} = 1;
565

            
566

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

            
582
is($dbi->one, 1, "first");
583
is($dbi->two, 2, "second");
584
is($dbi->twice(5), 10 , "second");
585

            
586
eval {$dbi->XXXXXX};
587
ok($@, "not exists");
588

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

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

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

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

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

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

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

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

            
699
$result->filter({'key2' => 'twice'});
700
$rows   = $result->all;
701
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join");
702

            
703
$result = $dbi->select(
704
     table => ['table1', 'table2'],
705
     column => ['key2', 'key3'],
706
     where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
707

            
708
$result->filter({'key2' => 'twice'});
709
$rows   = $result->all;
710
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join : omit");
711

            
712
test 'each_column';
cleanup test
Yuki Kimoto authored on 2011-08-06
713
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
714
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
715
$dbi->execute('create table table1 (key1 Date, key2 datetime);');
cleanup test
Yuki Kimoto authored on 2011-08-06
716

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

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
812
$dbi = MyDBI->connect(%memory);
813
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
814
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
815
is($dbi->select(table => 'table1')->one->{key1}, 1);
816

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

            
823
{
824
    package MyDBI2;
825
    
826
    use base 'DBIx::Custom';
827
    sub connect {
828
        my $self = shift->SUPER::new(@_);
829
        $self->connect;
830
        
831
        return $self;
832
    }
833
}
834

            
cleanup test
Yuki Kimoto authored on 2011-08-06
835
$dbi = MyDBI->connect(%memory);
836
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
837
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
838
is($dbi->select(table => 'table1')->one->{key1}, 1);
839

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

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

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

            
868
$dbi->register_filter(five_times => sub { $_[0] * 5 });
869
$result = $dbi->select(table => 'table1');
870
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
871
$result->end_filter({key1 => sub { $_[0] * 3 }, key2 => 'five_times' });
872
$row = $result->one;
873
is_deeply($row, {key1 => 6, key2 => 40});
874

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

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

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

            
910
test 'empty where select';
cleanup test
Yuki Kimoto authored on 2011-08-06
911
$dbi = DBIx::Custom->connect(%memory);
912
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
913
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
914
$result = $dbi->select(table => 'table1', where => {});
915
$row = $result->one;
916
is_deeply($row, {key1 => 1, key2 => 2});
917

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

            
map cleanup
Yuki Kimoto authored on 2011-08-09
930
test 'where';
cleanup test
Yuki Kimoto authored on 2011-08-06
931
$dbi = DBIx::Custom->connect(%memory);
932
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
933
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
934
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
935
$where = $dbi->where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
936
is("$where", "where ( key1 = :key1 and key2 = :key2 )", 'no param');
937

            
938
$where = $dbi->where
939
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
940
             ->param({key1 => 1});
941

            
942
$result = $dbi->select(
943
    table => 'table1',
944
    where => $where
945
);
946
$row = $result->all;
947
is_deeply($row, [{key1 => 1, key2 => 2}]);
948

            
949
$result = $dbi->select(
950
    table => 'table1',
951
    where => [
952
        ['and', 'key1 = :key1', 'key2 = :key2'],
953
        {key1 => 1}
954
    ]
955
);
956
$row = $result->all;
957
is_deeply($row, [{key1 => 1, key2 => 2}]);
958

            
959
$where = $dbi->where
960
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
961
             ->param({key1 => 1, key2 => 2});
962
$result = $dbi->select(
963
    table => 'table1',
964
    where => $where
965
);
966
$row = $result->all;
967
is_deeply($row, [{key1 => 1, key2 => 2}]);
968

            
969
$where = $dbi->where
970
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
971
             ->param({});
972
$result = $dbi->select(
973
    table => 'table1',
974
    where => $where,
975
);
976
$row = $result->all;
977
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
978

            
979
$where = $dbi->where
980
             ->clause(['and', ['or', 'key1 > :key1', 'key1 < :key1'], 'key2 = :key2'])
981
             ->param({key1 => [0, 3], key2 => 2});
982
$result = $dbi->select(
983
    table => 'table1',
984
    where => $where,
985
); 
986
$row = $result->all;
987
is_deeply($row, [{key1 => 1, key2 => 2}]);
988

            
989
$where = $dbi->where;
990
$result = $dbi->select(
991
    table => 'table1',
992
    where => $where
993
);
994
$row = $result->all;
995
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
996

            
997
eval {
998
$where = $dbi->where
999
             ->clause(['uuu']);
1000
$result = $dbi->select(
1001
    table => 'table1',
1002
    where => $where
1003
);
1004
};
1005
ok($@);
1006

            
1007
$where = $dbi->where;
1008
is("$where", '');
1009

            
1010
$where = $dbi->where
1011
             ->clause(['or', ('key1 = :key1') x 2])
1012
             ->param({key1 => [1, 3]});
1013
$result = $dbi->select(
1014
    table => 'table1',
1015
    where => $where,
1016
);
1017
$row = $result->all;
1018
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1019

            
1020
$where = $dbi->where
1021
             ->clause(['or', ('key1 = :key1') x 2])
1022
             ->param({key1 => [1]});
1023
$result = $dbi->select(
1024
    table => 'table1',
1025
    where => $where,
1026
);
1027
$row = $result->all;
1028
is_deeply($row, [{key1 => 1, key2 => 2}]);
1029

            
1030
$where = $dbi->where
1031
             ->clause(['or', ('key1 = :key1') x 2])
1032
             ->param({key1 => 1});
1033
$result = $dbi->select(
1034
    table => 'table1',
1035
    where => $where,
1036
);
1037
$row = $result->all;
1038
is_deeply($row, [{key1 => 1, key2 => 2}]);
1039

            
1040
$where = $dbi->where
1041
             ->clause('key1 = :key1')
1042
             ->param({key1 => 1});
1043
$result = $dbi->select(
1044
    table => 'table1',
1045
    where => $where,
1046
);
1047
$row = $result->all;
1048
is_deeply($row, [{key1 => 1, key2 => 2}]);
1049

            
1050
$where = $dbi->where
1051
             ->clause('key1 = :key1 key2 = :key2')
1052
             ->param({key1 => 1});
1053
eval{$where->to_string};
1054
like($@, qr/one column/);
1055

            
1056
$where = $dbi->where
1057
             ->clause(['or', ('key1 = :key1') x 3])
1058
             ->param({key1 => [$dbi->not_exists, 1, 3]});
1059
$result = $dbi->select(
1060
    table => 'table1',
1061
    where => $where,
1062
);
1063
$row = $result->all;
1064
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1065

            
1066
$where = $dbi->where
1067
             ->clause(['or', ('key1 = :key1') x 3])
1068
             ->param({key1 => [1, $dbi->not_exists, 3]});
1069
$result = $dbi->select(
1070
    table => 'table1',
1071
    where => $where,
1072
);
1073
$row = $result->all;
1074
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1075

            
1076
$where = $dbi->where
1077
             ->clause(['or', ('key1 = :key1') x 3])
1078
             ->param({key1 => [1, 3, $dbi->not_exists]});
1079
$result = $dbi->select(
1080
    table => 'table1',
1081
    where => $where,
1082
);
1083
$row = $result->all;
1084
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1085

            
1086
$where = $dbi->where
1087
             ->clause(['or', ('key1 = :key1') x 3])
1088
             ->param({key1 => [1, $dbi->not_exists, $dbi->not_exists]});
1089
$result = $dbi->select(
1090
    table => 'table1',
1091
    where => $where,
1092
);
1093
$row = $result->all;
1094
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1095

            
1096
$where = $dbi->where
1097
             ->clause(['or', ('key1 = :key1') x 3])
1098
             ->param({key1 => [$dbi->not_exists, 1, $dbi->not_exists]});
1099
$result = $dbi->select(
1100
    table => 'table1',
1101
    where => $where,
1102
);
1103
$row = $result->all;
1104
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1105

            
1106
$where = $dbi->where
1107
             ->clause(['or', ('key1 = :key1') x 3])
1108
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists, 1]});
1109
$result = $dbi->select(
1110
    table => 'table1',
1111
    where => $where,
1112
);
1113
$row = $result->all;
1114
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1115

            
1116
$where = $dbi->where
1117
             ->clause(['or', ('key1 = :key1') x 3])
1118
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists, $dbi->not_exists]});
1119
$result = $dbi->select(
1120
    table => 'table1',
1121
    where => $where,
1122
);
1123
$row = $result->all;
1124
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1125

            
1126
$where = $dbi->where
1127
             ->clause(['or', ('key1 = :key1') x 3])
1128
             ->param({key1 => []});
1129
$result = $dbi->select(
1130
    table => 'table1',
1131
    where => $where,
1132
);
1133
$row = $result->all;
1134
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1135

            
1136
$where = $dbi->where
1137
             ->clause(['and', '{> key1}', '{< key1}' ])
1138
             ->param({key1 => [2, $dbi->not_exists]});
1139
$result = $dbi->select(
1140
    table => 'table1',
1141
    where => $where,
1142
);
1143
$row = $result->all;
1144
is_deeply($row, [{key1 => 3, key2 => 4}], 'not_exists');
1145

            
1146
$where = $dbi->where
1147
             ->clause(['and', '{> key1}', '{< key1}' ])
1148
             ->param({key1 => [$dbi->not_exists, 2]});
1149
$result = $dbi->select(
1150
    table => 'table1',
1151
    where => $where,
1152
);
1153
$row = $result->all;
1154
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1155

            
1156
$where = $dbi->where
1157
             ->clause(['and', '{> key1}', '{< key1}' ])
1158
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists]});
1159
$result = $dbi->select(
1160
    table => 'table1',
1161
    where => $where,
1162
);
1163
$row = $result->all;
1164
is_deeply($row, [{key1 => 1, key2 => 2},{key1 => 3, key2 => 4}], 'not_exists');
1165

            
1166
$where = $dbi->where
1167
             ->clause(['and', '{> key1}', '{< key1}' ])
1168
             ->param({key1 => [0, 2]});
1169
$result = $dbi->select(
1170
    table => 'table1',
1171
    where => $where,
1172
);
1173
$row = $result->all;
1174
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1175

            
1176
$where = $dbi->where
1177
             ->clause(['and', 'key1 is not null', 'key2 is not null' ]);
1178
$result = $dbi->select(
1179
    table => 'table1',
1180
    where => $where,
1181
);
1182
$row = $result->all;
1183
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1184

            
1185
eval {$dbi->where(ppp => 1) };
1186
like($@, qr/invalid/);
1187

            
1188
$where = $dbi->where(
1189
    clause => ['and', ['or'], ['and', 'key1 = :key1', 'key2 = :key2']],
1190
    param => {key1 => 1, key2 => 2}
1191
);
1192
$result = $dbi->select(
1193
    table => 'table1',
1194
    where => $where,
1195
);
1196
$row = $result->all;
1197
is_deeply($row, [{key1 => 1, key2 => 2}]);
1198

            
1199

            
1200
$where = $dbi->where(
1201
    clause => ['and', ['or'], ['or', ':key1', ':key2']],
1202
    param => {}
1203
);
1204
$result = $dbi->select(
1205
    table => 'table1',
1206
    where => $where,
1207
);
1208
$row = $result->all;
1209
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1210

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

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

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

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

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

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

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

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

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

            
1294
$where = $dbi->where;
1295
$where->param({id => 0, author => 0, price => 0});
1296
$where->map(
1297
    id => 'book.id',
1298
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1299
    price => ['book.price', sub { '%' . $_[0] . '%' },
1300
      {if => sub { $_[0] eq 0 }}]
1301
);
1302
is_deeply($where->param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
1303

            
1304
$where = $dbi->where;
1305
$where->param({id => '', author => '', price => ''});
1306
$where->if('length');
1307
$where->map(
1308
    id => 'book.id',
1309
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1310
    price => ['book.price', sub { '%' . $_[0] . '%' },
1311
      {if => sub { $_[0] eq 1 }}]
1312
);
1313
is_deeply($where->param, {});
1314

            
1315
$where = $dbi->where;
1316
$where->param({id => undef, author => undef, price => undef});
1317
$where->if('length');
1318
$where->map(
1319
    id => 'book.id',
1320
    price => ['book.price', {if => 'exists'}]
1321
);
1322
is_deeply($where->param, {'book.price' => undef});
1323

            
1324
$where = $dbi->where;
1325
$where->param({price => 'a'});
1326
$where->if('length');
1327
$where->map(
1328
    id => ['book.id', {if => 'exists'}],
1329
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
1330
);
1331
is_deeply($where->param, {'book.price' => '%a'});
1332

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

            
1343
$where = $dbi->where;
1344
$where->if('length');
1345
$where->param({id => ['', ''], author => 'Ken', price => 1900});
1346
$where->map(
1347
    id => 'book.id',
1348
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1349
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1350
);
1351
is_deeply($where->param, {'book.id' => [$dbi->not_exists, $dbi->not_exists], 'book.author' => '%Ken%',
1352
  'book.price' => 1900});
1353

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

            
1364
test 'dbi_option default';
1365
$dbi = DBIx::Custom->new;
1366
is_deeply($dbi->dbi_option, {});
1367

            
1368
test 'register_tag_processor';
cleanup test
Yuki Kimoto authored on 2011-08-06
1369
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1370
$dbi->register_tag_processor(
1371
    a => sub { 1 }
1372
);
1373
is($dbi->query_builder->tag_processors->{a}->(), 1);
1374

            
1375
test 'register_tag';
cleanup test
Yuki Kimoto authored on 2011-08-06
1376
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1377
$dbi->register_tag(
1378
    b => sub { 2 }
1379
);
1380
is($dbi->query_builder->tags->{b}->(), 2);
1381

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

            
1393

            
1394
test 'more tests';
cleanup test
Yuki Kimoto authored on 2011-08-06
1395
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1396
eval{$dbi->apply_filter('table', 'column', [])};
1397
like($@, qr/apply_filter/);
1398

            
1399
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
1400
like($@, qr/apply_filter/);
1401

            
1402
$dbi->apply_filter(
1403

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1414
$dbi = DBIx::Custom->connect(%memory);
1415
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
1416
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1417
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1418
$dbi->apply_filter('table1', 'key2', {});
1419
$rows = $dbi->select(table => 'table1', where => {key2 => 2})->all;
1420
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1421

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

            
1430
eval{DBIx::Custom->connect()};
1431
like($@, qr/_connect/);
1432

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

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

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

            
1473
test 'dbi_option';
cleanup test
Yuki Kimoto authored on 2011-08-06
1474
$dbi = DBIx::Custom->connect(%memory,
cleanup test
Yuki Kimoto authored on 2011-08-06
1475
                             dbi_option => {PrintError => 1});
1476
ok($dbi->dbh->{PrintError});
cleanup test
Yuki Kimoto authored on 2011-08-06
1477
$dbi = DBIx::Custom->connect(%memory,
cleanup test
Yuki Kimoto authored on 2011-08-06
1478
                             dbi_options => {PrintError => 1});
1479
ok($dbi->dbh->{PrintError});
1480

            
1481
test 'DBIx::Custom::Result stash()';
1482
$result = DBIx::Custom::Result->new;
1483
is_deeply($result->stash, {}, 'default');
1484
$result->stash->{foo} = 1;
1485
is($result->stash->{foo}, 1, 'get and set');
1486

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

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

            
1498
$result = $dbi->select(
1499
    table => ['company', 'location'], relation => {'company.location_id' => 'location.id'},
1500
    column => ['location.name as location__name']
1501
);
1502
is($result->fetch_first->[0], 'B');
1503

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

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

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

            
1530
{
1531
    package MyDBI4;
1532

            
1533
    use strict;
1534
    use warnings;
1535

            
1536
    use base 'DBIx::Custom';
1537

            
1538
    sub connect {
1539
        my $self = shift->SUPER::connect(@_);
1540
        
1541
        $self->include_model(
1542
            MyModel2 => [
1543
                'book',
1544
                {class => 'Company', name => 'company'}
1545
            ]
1546
        );
1547
    }
1548

            
1549
    package MyModel2::Base1;
1550

            
1551
    use strict;
1552
    use warnings;
1553

            
1554
    use base 'DBIx::Custom::Model';
1555

            
1556
    package MyModel2::book;
1557

            
1558
    use strict;
1559
    use warnings;
1560

            
1561
    use base 'MyModel2::Base1';
1562

            
1563
    sub insert {
1564
        my ($self, $param) = @_;
1565
        
1566
        return $self->SUPER::insert(param => $param);
1567
    }
1568

            
1569
    sub list { shift->select; }
1570

            
1571
    package MyModel2::Company;
1572

            
1573
    use strict;
1574
    use warnings;
1575

            
1576
    use base 'MyModel2::Base1';
1577

            
1578
    sub insert {
1579
        my ($self, $param) = @_;
1580
        
1581
        return $self->SUPER::insert(param => $param);
1582
    }
1583

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

            
1596
{
1597
     package MyDBI5;
1598

            
1599
    use strict;
1600
    use warnings;
1601

            
1602
    use base 'DBIx::Custom';
1603

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

            
1620
test 'primary_key';
1621
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1622
$dbi = MyDBI1->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1623
$model = $dbi->model('book');
1624
$model->primary_key(['id', 'number']);
1625
is_deeply($model->primary_key, ['id', 'number']);
1626

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

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

            
1644
test 'delete_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1645
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1646
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1647
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1648
$dbi->delete_at(
1649
    table => 'table1',
1650
    primary_key => ['key1', 'key2'],
1651
    where => [1, 2],
1652
);
1653
is_deeply($dbi->select(table => 'table1')->all, []);
1654

            
1655
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1656
$dbi->delete_at(
1657
    table => 'table1',
1658
    primary_key => 'key1',
1659
    where => 1,
1660
);
1661
is_deeply($dbi->select(table => 'table1')->all, []);
1662

            
1663
test 'insert_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1664
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1665
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1666
$dbi->insert_at(
1667
    primary_key => ['key1', 'key2'], 
1668
    table => 'table1',
1669
    where => [1, 2],
1670
    param => {key3 => 3}
1671
);
1672
is($dbi->select(table => 'table1')->one->{key1}, 1);
1673
is($dbi->select(table => 'table1')->one->{key2}, 2);
1674
is($dbi->select(table => 'table1')->one->{key3}, 3);
1675

            
1676
$dbi->delete_all(table => 'table1');
1677
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1678
$dbi->insert_at(
1679
    primary_key => 'key1', 
1680
    table => 'table1',
1681
    where => 1,
1682
    param => {key2 => 2, key3 => 3}
1683
);
1684

            
1685
is($dbi->select(table => 'table1')->one->{key1}, 1);
1686
is($dbi->select(table => 'table1')->one->{key2}, 2);
1687
is($dbi->select(table => 'table1')->one->{key3}, 3);
1688

            
1689
eval {
1690
    $dbi->insert_at(
1691
        table => 'table1',
1692
        primary_key => ['key1', 'key2'],
1693
        where => {},
1694
        param => {key1 => 1, key2 => 2, key3 => 3},
1695
    );
1696
};
1697
like($@, qr/must be/);
1698

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1699
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1700
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1701
$dbi->insert_at(
1702
    {key3 => 3},
1703
    primary_key => ['key1', 'key2'], 
1704
    table => 'table1',
1705
    where => [1, 2],
1706
);
1707
is($dbi->select(table => 'table1')->one->{key1}, 1);
1708
is($dbi->select(table => 'table1')->one->{key2}, 2);
1709
is($dbi->select(table => 'table1')->one->{key3}, 3);
1710

            
1711
test 'update_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1712
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1713
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1714
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1715
$dbi->update_at(
1716
    table => 'table1',
1717
    primary_key => ['key1', 'key2'],
1718
    where => [1, 2],
1719
    param => {key3 => 4}
1720
);
1721
is($dbi->select(table => 'table1')->one->{key1}, 1);
1722
is($dbi->select(table => 'table1')->one->{key2}, 2);
1723
is($dbi->select(table => 'table1')->one->{key3}, 4);
1724

            
1725
$dbi->delete_all(table => 'table1');
1726
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1727
$dbi->update_at(
1728
    table => 'table1',
1729
    primary_key => 'key1',
1730
    where => 1,
1731
    param => {key3 => 4}
1732
);
1733
is($dbi->select(table => 'table1')->one->{key1}, 1);
1734
is($dbi->select(table => 'table1')->one->{key2}, 2);
1735
is($dbi->select(table => 'table1')->one->{key3}, 4);
1736

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1737
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1738
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1739
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1740
$dbi->update_at(
1741
    {key3 => 4},
1742
    table => 'table1',
1743
    primary_key => ['key1', 'key2'],
1744
    where => [1, 2]
1745
);
1746
is($dbi->select(table => 'table1')->one->{key1}, 1);
1747
is($dbi->select(table => 'table1')->one->{key2}, 2);
1748
is($dbi->select(table => 'table1')->one->{key3}, 4);
1749

            
1750
test 'select_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1751
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1752
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1753
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1754
$result = $dbi->select_at(
1755
    table => 'table1',
1756
    primary_key => ['key1', 'key2'],
1757
    where => [1, 2]
1758
);
1759
$row = $result->one;
1760
is($row->{key1}, 1);
1761
is($row->{key2}, 2);
1762
is($row->{key3}, 3);
1763

            
1764
$dbi->delete_all(table => 'table1');
1765
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1766
$result = $dbi->select_at(
1767
    table => 'table1',
1768
    primary_key => 'key1',
1769
    where => 1,
1770
);
1771
$row = $result->one;
1772
is($row->{key1}, 1);
1773
is($row->{key2}, 2);
1774
is($row->{key3}, 3);
1775

            
1776
$dbi->delete_all(table => 'table1');
1777
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1778
$result = $dbi->select_at(
1779
    table => 'table1',
1780
    primary_key => ['key1', 'key2'],
1781
    where => [1, 2]
1782
);
1783
$row = $result->one;
1784
is($row->{key1}, 1);
1785
is($row->{key2}, 2);
1786
is($row->{key3}, 3);
1787

            
1788
eval {
1789
    $result = $dbi->select_at(
1790
        table => 'table1',
1791
        primary_key => ['key1', 'key2'],
1792
        where => {},
1793
    );
1794
};
1795
like($@, qr/must be/);
1796

            
1797
eval {
1798
    $result = $dbi->select_at(
1799
        table => 'table1',
1800
        primary_key => ['key1', 'key2'],
1801
        where => [1],
1802
    );
1803
};
1804
like($@, qr/same/);
1805

            
1806
eval {
1807
    $result = $dbi->update_at(
1808
        table => 'table1',
1809
        primary_key => ['key1', 'key2'],
1810
        where => {},
1811
        param => {key1 => 1, key2 => 2},
1812
    );
1813
};
1814
like($@, qr/must be/);
1815

            
1816
eval {
1817
    $result = $dbi->delete_at(
1818
        table => 'table1',
1819
        primary_key => ['key1', 'key2'],
1820
        where => {},
1821
    );
1822
};
1823
like($@, qr/must be/);
1824

            
1825
test 'columns';
1826
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1827
$dbi = MyDBI1->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1828
$model = $dbi->model('book');
1829

            
1830

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

            
1859
test 'model insert_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1860
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1861
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1862
$dbi->model('table1')->insert_at(
1863
    where => [1, 2],
1864
    param => {key3 => 3}
1865
);
1866
$result = $dbi->model('table1')->select;
1867
$row = $result->one;
1868
is($row->{key1}, 1);
1869
is($row->{key2}, 2);
1870
is($row->{key3}, 3);
1871

            
1872
test 'model update_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1873
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1874
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1875
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1876
$dbi->model('table1')->update_at(
1877
    where => [1, 2],
1878
    param => {key3 => 4}
1879
);
1880
$result = $dbi->model('table1')->select;
1881
$row = $result->one;
1882
is($row->{key1}, 1);
1883
is($row->{key2}, 2);
1884
is($row->{key3}, 4);
1885

            
1886
test 'model select_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1887
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1888
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1889
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1890
$result = $dbi->model('table1')->select_at(where => [1, 2]);
1891
$row = $result->one;
1892
is($row->{key1}, 1);
1893
is($row->{key2}, 2);
1894
is($row->{key3}, 3);
1895

            
1896

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

            
1927
test 'update_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
1928
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1929
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1930
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1931
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1932

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

            
1946

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1947
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1948
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1949
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1950
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1951

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1965
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1966
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1967
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1968
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1969

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

            
1983
            
1984
eval { $dbi->update_param({";" => 1}) };
1985
like($@, qr/not safety/);
1986

            
1987

            
1988
test 'update_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
1989
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1990
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1991
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1992
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1993

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

            
2007

            
2008
test 'insert_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
2009
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2010
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
2011
$param = {key1 => 1, key2 => 2};
2012
$insert_param = $dbi->insert_param($param);
2013
$sql = <<"EOS";
2014
insert into table1 $insert_param
2015
EOS
2016
$dbi->execute($sql, param => $param, table => 'table1');
2017
is($dbi->select(table => 'table1')->one->{key1}, 1);
2018
is($dbi->select(table => 'table1')->one->{key2}, 2);
2019

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2020
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2021
$dbi->quote('"');
2022
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
2023
$param = {key1 => 1, key2 => 2};
2024
$insert_param = $dbi->insert_param($param);
2025
$sql = <<"EOS";
2026
insert into table1 $insert_param
2027
EOS
2028
$dbi->execute($sql, param => $param, table => 'table1');
2029
is($dbi->select(table => 'table1')->one->{key1}, 1);
2030
is($dbi->select(table => 'table1')->one->{key2}, 2);
2031

            
2032
eval { $dbi->insert_param({";" => 1}) };
2033
like($@, qr/not safety/);
2034

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

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

            
2053
$rows = $dbi->select(
2054
    table => 'table1',
2055
    where   => {'key1' => 1},
2056
    join  => ['left outer join table2 on table1.key1 = table2.key1']
2057
)->all;
2058
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2059

            
2060
eval {
2061
    $rows = $dbi->select(
2062
        table => 'table1',
2063
        column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
2064
        where   => {'table1.key2' => 2},
2065
        join  => {'table1.key1' => 'table2.key1'}
2066
    );
2067
};
2068
like ($@, qr/array/);
2069

            
2070
$rows = $dbi->select(
2071
    table => 'table1',
2072
    where   => {'key1' => 1},
2073
    join  => ['left outer join table2 on table1.key1 = table2.key1',
2074
              'left outer join table3 on table2.key3 = table3.key3']
2075
)->all;
2076
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2077

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

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

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

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

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

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

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

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

            
2210
$result = $model->select_at(
2211
    column => [
2212
        $model->mycolumn(['key1']),
2213
        $model->column(table2 => ['key1'])
2214
    ]
2215
);
2216
is_deeply($result->one,
2217
          {key1 => 1, 'table2.key1' => 1});
2218
$result = $model->select_at(
2219
    column => [
2220
        $model->mycolumn(['key1']),
2221
        {table2 => ['key1']}
2222
    ]
2223
);
2224
is_deeply($result->one,
2225
          {key1 => 1, 'table2.key1' => 1});
2226

            
2227
$result = $model->select_at(
2228
    column => [
2229
        $model->mycolumn(['key1']),
2230
        ['table2.key1', as => 'table2.key1']
2231
    ]
2232
);
2233
is_deeply($result->one,
2234
          {key1 => 1, 'table2.key1' => 1});
2235

            
2236
$result = $model->select_at(
2237
    column => [
2238
        $model->mycolumn(['key1']),
2239
        ['table2.key1' => 'table2.key1']
2240
    ]
2241
);
2242
is_deeply($result->one,
2243
          {key1 => 1, 'table2.key1' => 1});
2244

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

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

            
2282
$dbi->separator('__');
2283
$result = $model->select(
2284
    column => [
2285
        $model->column('table2', {alias => 'table2_alias'})
2286
    ],
2287
    where => {'table2_alias.key3' => 4}
2288
);
2289
is_deeply($result->one, 
2290
          {'table2_alias__key1' => 1, 'table2_alias__key3' => 4});
2291

            
2292
$dbi->separator('-');
2293
$result = $model->select(
2294
    column => [
2295
        $model->column('table2', {alias => 'table2_alias'})
2296
    ],
2297
    where => {'table2_alias.key3' => 4}
2298
);
2299
is_deeply($result->one, 
2300
          {'table2_alias-key1' => 1, 'table2_alias-key3' => 4});
2301

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

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

            
2327

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

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

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

            
2371
test 'create_model';
cleanup test
Yuki Kimoto authored on 2011-08-06
2372
$dbi = DBIx::Custom->connect(%memory);
2373
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2374
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2375

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

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

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

            
2425
$params = [
2426
    {key1 => [1, 2], key2 => 1, key3 => [1, 2]},
2427
    {key1 => [3, 4], key2 => [2, 3], key3 => 3}
2428
];
2429
$param = $dbi->merge_param($params->[0], $params->[1]);
2430
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
2431

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

            
2450

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

            
2463
eval {
2464
$dbi->select(
2465
    table => 'table1',
2466
    column => 'key1',
2467
    wrap => 'select * from ('
2468
)
2469
};
2470
like($@, qr/array/);
2471

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

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

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

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

            
2524

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

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

            
2552
test 'insert id and primary_key option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2553
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2554
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2555
$dbi->insert(
2556
    primary_key => ['key1', 'key2'], 
2557
    table => 'table1',
2558
    id => [1, 2],
2559
    param => {key3 => 3}
2560
);
2561
is($dbi->select(table => 'table1')->one->{key1}, 1);
2562
is($dbi->select(table => 'table1')->one->{key2}, 2);
2563
is($dbi->select(table => 'table1')->one->{key3}, 3);
2564

            
2565
$dbi->delete_all(table => 'table1');
2566
$dbi->insert(
2567
    primary_key => 'key1', 
2568
    table => 'table1',
2569
    id => 0,
2570
    param => {key2 => 2, key3 => 3}
2571
);
2572

            
2573
is($dbi->select(table => 'table1')->one->{key1}, 0);
2574
is($dbi->select(table => 'table1')->one->{key2}, 2);
2575
is($dbi->select(table => 'table1')->one->{key3}, 3);
2576

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2577
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2578
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2579
$dbi->insert(
2580
    {key3 => 3},
2581
    primary_key => ['key1', 'key2'], 
2582
    table => 'table1',
2583
    id => [1, 2],
2584
);
2585
is($dbi->select(table => 'table1')->one->{key1}, 1);
2586
is($dbi->select(table => 'table1')->one->{key2}, 2);
2587
is($dbi->select(table => 'table1')->one->{key3}, 3);
2588

            
2589

            
2590
test 'model insert id and primary_key option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2591
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2592
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2593
$dbi->model('table1')->insert(
2594
    id => [1, 2],
2595
    param => {key3 => 3}
2596
);
2597
$result = $dbi->model('table1')->select;
2598
$row = $result->one;
2599
is($row->{key1}, 1);
2600
is($row->{key2}, 2);
2601
is($row->{key3}, 3);
2602

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2603
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2604
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2605
$dbi->model('table1')->insert(
2606
    {key3 => 3},
2607
    id => [1, 2]
2608
);
2609
$result = $dbi->model('table1')->select;
2610
$row = $result->one;
2611
is($row->{key1}, 1);
2612
is($row->{key2}, 2);
2613
is($row->{key3}, 3);
2614

            
2615
test 'update and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2616
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2617
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2618
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2619
$dbi->update(
2620
    table => 'table1',
2621
    primary_key => ['key1', 'key2'],
2622
    id => [1, 2],
2623
    param => {key3 => 4}
2624
);
2625
is($dbi->select(table => 'table1')->one->{key1}, 1);
2626
is($dbi->select(table => 'table1')->one->{key2}, 2);
2627
is($dbi->select(table => 'table1')->one->{key3}, 4);
2628

            
2629
$dbi->delete_all(table => 'table1');
2630
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2631
$dbi->update(
2632
    table => 'table1',
2633
    primary_key => 'key1',
2634
    id => 0,
2635
    param => {key3 => 4}
2636
);
2637
is($dbi->select(table => 'table1')->one->{key1}, 0);
2638
is($dbi->select(table => 'table1')->one->{key2}, 2);
2639
is($dbi->select(table => 'table1')->one->{key3}, 4);
2640

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2641
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2642
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2643
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2644
$dbi->update(
2645
    {key3 => 4},
2646
    table => 'table1',
2647
    primary_key => ['key1', 'key2'],
2648
    id => [1, 2]
2649
);
2650
is($dbi->select(table => 'table1')->one->{key1}, 1);
2651
is($dbi->select(table => 'table1')->one->{key2}, 2);
2652
is($dbi->select(table => 'table1')->one->{key3}, 4);
2653

            
2654

            
2655
test 'model update and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2656
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2657
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2658
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2659
$dbi->model('table1')->update(
2660
    id => [1, 2],
2661
    param => {key3 => 4}
2662
);
2663
$result = $dbi->model('table1')->select;
2664
$row = $result->one;
2665
is($row->{key1}, 1);
2666
is($row->{key2}, 2);
2667
is($row->{key3}, 4);
2668

            
2669

            
2670
test 'delete and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2671
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2672
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2673
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2674
$dbi->delete(
2675
    table => 'table1',
2676
    primary_key => ['key1', 'key2'],
2677
    id => [1, 2],
2678
);
2679
is_deeply($dbi->select(table => 'table1')->all, []);
2680

            
2681
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2682
$dbi->delete(
2683
    table => 'table1',
2684
    primary_key => 'key1',
2685
    id => 0,
2686
);
2687
is_deeply($dbi->select(table => 'table1')->all, []);
2688

            
2689

            
2690
test 'model delete and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2691
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2692
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2693
$dbi->execute("create table table2 (key1, key2, key3)");
2694
$dbi->execute("create table table3 (key1, key2, key3)");
2695
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2696
$dbi->model('table1')->delete(id => [1, 2]);
2697
is_deeply($dbi->select(table => 'table1')->all, []);
2698
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
2699
$dbi->model('table1_1')->delete(id => [1, 2]);
2700
is_deeply($dbi->select(table => 'table1')->all, []);
2701
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
2702
$dbi->model('table1_3')->delete(id => [1, 2]);
2703
is_deeply($dbi->select(table => 'table1')->all, []);
2704

            
2705

            
2706
test 'select and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2707
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2708
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2709
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2710
$result = $dbi->select(
2711
    table => 'table1',
2712
    primary_key => ['key1', 'key2'],
2713
    id => [1, 2]
2714
);
2715
$row = $result->one;
2716
is($row->{key1}, 1);
2717
is($row->{key2}, 2);
2718
is($row->{key3}, 3);
2719

            
2720
$dbi->delete_all(table => 'table1');
2721
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2722
$result = $dbi->select(
2723
    table => 'table1',
2724
    primary_key => 'key1',
2725
    id => 0,
2726
);
2727
$row = $result->one;
2728
is($row->{key1}, 0);
2729
is($row->{key2}, 2);
2730
is($row->{key3}, 3);
2731

            
2732
$dbi->delete_all(table => 'table1');
2733
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2734
$result = $dbi->select(
2735
    table => 'table1',
2736
    primary_key => ['key1', 'key2'],
2737
    id => [1, 2]
2738
);
2739
$row = $result->one;
2740
is($row->{key1}, 1);
2741
is($row->{key2}, 2);
2742
is($row->{key3}, 3);
2743

            
2744

            
2745
test 'model select_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
2746
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2747
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2748
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2749
$result = $dbi->model('table1')->select(id => [1, 2]);
2750
$row = $result->one;
2751
is($row->{key1}, 1);
2752
is($row->{key2}, 2);
2753
is($row->{key3}, 3);
2754

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

            
2770
$result = $model->select(
2771
    column => [$model->column('table2' => [qw/key1 key3/])],
2772
    where => {'table1.key1' => 1}
2773
);
2774
is_deeply($result->one,
2775
          {'table2.key1' => 1, 'table2.key3' => 3});
2776

            
2777

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

            
2790
$result = $dbi->select(table => 'table1');
2791
is($result->one->{key1}, 'A');
2792

            
2793

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

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

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

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

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

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

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

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

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

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

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

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

            
2981
eval{$dbi->type_rule(
2982
    into1 => {
2983
        date => 'pp'
2984
    }
2985
)};
2986
like($@, qr/not registered/);
2987

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

            
2999
eval {
3000
    $dbi->type_rule(
3001
        into1 => {
3002
            Date => sub { $_[0] * 2 },
3003
        }
3004
    );
3005
};
3006
like($@, qr/lower/);
3007

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

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

            
3042
$result = $dbi->select(table => 'table1');
3043
$result->type_rule(
3044
    from1 => {
3045
        date => sub { $_[0] * 3 }
3046
    }
3047
);
3048
$row = $result->one;
3049
is($row->{key1}, 6);
3050
is($row->{key2}, 2);
3051

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

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

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

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

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

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

            
3174
test 'separator';
cleanup test
Yuki Kimoto authored on 2011-08-06
3175
$dbi = DBIx::Custom->connect(%memory);
3176
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
3177
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
3178

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

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

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

            
3230

            
3231
test 'filter_off';
cleanup test
Yuki Kimoto authored on 2011-08-06
3232
$dbi = DBIx::Custom->connect(%memory);
3233
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
3234
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
3235

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

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

            
3254

            
3255
test 'select prefix option';
cleanup test
Yuki Kimoto authored on 2011-08-06
3256
$dbi = DBIx::Custom->connect(%memory);
3257
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
3258
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3259
$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all;
3260
is_deeply($rows, [{key1 => 1, key2 => 2}], "table");
3261

            
3262

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

            
3273

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

            
3285
$param = $dbi->map_param(
3286
    {id => 0, author => 0, price => 0},
3287
    id => 'book.id',
3288
    author => ['book.author', sub { '%' . $_[0] . '%' }],
3289
    price => ['book.price', sub { '%' . $_[0] . '%' },
3290
      {if => sub { $_[0] eq 0 }}]
3291
);
3292
is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
3293

            
3294
$param = $dbi->map_param(
3295
    {id => '', author => '', price => ''},
3296
    id => 'book.id',
3297
    author => ['book.author', sub { '%' . $_[0] . '%' }],
3298
    price => ['book.price', sub { '%' . $_[0] . '%' },
3299
      {if => sub { $_[0] eq 1 }}]
3300
);
3301
is_deeply($param, {});
3302

            
3303
$param = $dbi->map_param(
3304
    {id => undef, author => undef, price => undef},
3305
    id => 'book.id',
3306
    price => ['book.price', {if => 'exists'}]
3307
);
3308
is_deeply($param, {'book.price' => undef});
3309

            
3310
$param = $dbi->map_param(
3311
    {price => 'a'},
3312
    id => ['book.id', {if => 'exists'}],
3313
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
3314
);
3315
is_deeply($param, {'book.price' => '%a'});
3316

            
3317

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

            
3331

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

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

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

            
3367
test 'last_sql';
cleanup test
Yuki Kimoto authored on 2011-08-06
3368
$dbi = DBIx::Custom->connect(%memory);
3369
$dbi->execute("create table table1 (key1, key2)");
3370
$dbi->execute('select * from table1');
3371
is($dbi->last_sql, 'select * from table1;');
3372

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

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

            
3382
test 'Named placeholder :name(operater) syntax';
cleanup test
Yuki Kimoto authored on 2011-08-06
3383
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
3384
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
3385
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3386
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3387

            
3388
$source = "select * from table1 where :key1{=} and :key2{=}";
3389
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
3390
$rows = $result->all;
3391
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
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 => 5, 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 :table1.key1{=} and :table1.key2{=}";
3404
$result = $dbi->execute(
3405
    $source,
3406
    param => {'table1.key1' => 1, 'table1.key2' => 1},
3407
    filter => {'table1.key2' => sub { $_[0] * 2 }}
3408
);
3409
$rows = $result->all;
3410
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3411

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

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

            
clenup test
Yuki Kimoto authored on 2011-08-06
3455
test 'result';
3456
$dbi = DBIx::Custom->connect(%memory);
3457
$dbi->execute($create_table_default);
3458
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3459
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3460

            
3461
$result = $dbi->select(table => 'table1');
3462
@rows = ();
3463
while (my $row = $result->fetch) {
3464
    push @rows, [@$row];
3465
}
3466
is_deeply(\@rows, [[1, 2], [3, 4]]);
3467

            
3468
$result = $dbi->select(table => 'table1');
3469
@rows = ();
3470
while (my $row = $result->fetch_hash) {
3471
    push @rows, {%$row};
3472
}
3473
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
3474

            
3475
$result = $dbi->select(table => 'table1');
3476
$row = $result->fetch_first;
3477
is_deeply($row, [1, 2], "row");
3478
$row = $result->fetch;
3479
ok(!$row, "finished");
3480

            
3481
$result = $dbi->select(table => 'table1');
3482
$row = $result->fetch_hash_first;
3483
is_deeply($row, {key1 => 1, key2 => 2}, "row");
3484
$row = $result->fetch_hash;
3485
ok(!$row, "finished");
3486

            
3487
$dbi->execute('create table table2 (key1, key2);');
3488
$result = $dbi->select(table => 'table2');
3489
$row = $result->fetch_hash_first;
3490
ok(!$row, "no row fetch");
3491

            
3492
$dbi = DBIx::Custom->connect(%memory);
3493
$dbi->execute($create_table_default);
3494
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3495
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3496
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1');
3497
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1');
3498
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1');
3499
$result = $dbi->select(table => 'table1');
3500
$rows = $result->fetch_multi(2);
3501
is_deeply($rows, [[1, 2],
3502
                  [3, 4]], "fetch_multi first");
3503
$rows = $result->fetch_multi(2);
3504
is_deeply($rows, [[5, 6],
3505
                  [7, 8]], "fetch_multi secound");
3506
$rows = $result->fetch_multi(2);
3507
is_deeply($rows, [[9, 10]], "fetch_multi third");
3508
$rows = $result->fetch_multi(2);
3509
ok(!$rows);
3510

            
3511
$result = $dbi->select(table => 'table1');
3512
eval {$result->fetch_multi};
3513
like($@, qr/Row count must be specified/, "Not specified row count");
3514

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

            
3527
$result = $dbi->select(table => 'table1');
3528
eval {$result->fetch_hash_multi};
3529
like($@, qr/Row count must be specified/, "Not specified row count");
3530

            
3531
$dbi = DBIx::Custom->connect(%memory);
3532
$dbi->execute($create_table_default);
3533
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3534
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3535

            
3536
test 'fetch_all';
3537
$result = $dbi->select(table => 'table1');
3538
$rows = $result->fetch_all;
3539
is_deeply($rows, [[1, 2], [3, 4]]);
3540

            
3541
$result = $dbi->select(table => 'table1');
3542
$rows = $result->fetch_hash_all;
3543
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
3544

            
3545
$result = $dbi->select(table => 'table1');
3546
$result->dbi->filters({three_times => sub { $_[0] * 3}});
3547
$result->filter({key1 => 'three_times'});
3548

            
3549
$rows = $result->fetch_all;
3550
is_deeply($rows, [[3, 2], [9, 4]], "array");
3551

            
3552
$result = $dbi->select(table => 'table1');
3553
$result->dbi->filters({three_times => sub { $_[0] * 3}});
3554
$result->filter({key1 => 'three_times'});
3555
$rows = $result->fetch_hash_all;
3556
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash");
3557

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

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

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

            
3613
$query = $builder->build_query("{p a b}");
3614
is($query->{sql}, "? a b;", "register_tag sql");
3615
is_deeply($query->{columns}, [2], "register_tag columns");
3616
isa_ok($ret_val, 'DBIx::Custom::QueryBuilder');
3617

            
3618
$builder = DBIx::Custom->new->query_builder;
3619

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

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

            
3626
$builder->register_tag({
3627
    q => 'string'
3628
});
3629

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

            
3633
$builder->register_tag({
3634
   r => sub {} 
3635
});
3636

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

            
3640
$builder->register_tag({
3641
   s => sub { return ["a", ""]} 
3642
});
3643

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

            
3647
$builder->register_tag(
3648
    t => sub {return ["a", []]}
3649
);
3650

            
3651

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

            
3662

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

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

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

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

            
3678
test 'variouse source';
3679
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;";
3680
$query = $builder->build_query($source);
3681
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1");
3682

            
3683
$source = "abc;";
3684
$query = $builder->build_query($source);
3685
is($query->sql, 'abc;', "basic : 2");
3686

            
3687
$source = "{= a}";
3688
$query = $builder->build_query($source);
3689
is($query->sql, 'a = ?;', "only tag");
3690

            
3691
$source = "000;";
3692
$query = $builder->build_query($source);
3693
is($query->sql, '000;', "contain 0 value");
3694

            
3695
$source = "a {= b} }";
3696
eval{$builder->build_query($source)};
3697
like($@, qr/unexpected "}"/, "error : 1");
3698

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