DBIx-Custom / t / sqlite.t /
Newer Older
3679 lines | 118.856kb
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
test 'Error case';
61
eval {DBIx::Custom->connect(dsn => 'dbi:SQLit')};
62
ok($@, "connect error");
63

            
cleanup test
Yuki Kimoto authored on 2011-08-06
64
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
65
eval{$dbi->execute("{p }", {}, query => 1)};
66
ok($@, "create_query invalid SQL template");
67

            
68
test 'insert';
cleanup test
Yuki Kimoto authored on 2011-08-06
69
$dbi = DBIx::Custom->connect(%memory);
70
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
71
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
72
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-06
73
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
74
$rows   = $result->all;
75
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
76

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

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

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

            
98
eval{$dbi->insert(table => 'table', param => {';' => 1})};
99
like($@, qr/safety/);
100

            
cleanup test
Yuki Kimoto authored on 2011-08-06
101
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
102
$dbi->quote('"');
103
$dbi->execute('create table "table" ("select")');
104
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
105
$dbi->insert(table => 'table', param => {select => 1});
106
$result = $dbi->execute('select * from "table"');
107
$rows   = $result->all;
108
is_deeply($rows, [{select => 2}], "reserved word");
109

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
126
$dbi = DBIx::Custom->connect(%memory);
127
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
128
$dbi->insert(table => 'table1', param => {key1 => \"'1'", key2 => 2});
129
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-06
130
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
131
$rows   = $result->all;
132
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
133

            
134
test 'update';
cleanup test
Yuki Kimoto authored on 2011-08-06
135
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
136
$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
137
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
138
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
139
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
140
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
141
$rows   = $result->all;
142
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
143
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
144
                  "basic");
145
                  
146
$dbi->execute("delete from table1");
147
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
148
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
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");
155

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

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

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

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

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
207
$dbi = DBIx::Custom->connect(%memory);
208
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
209
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
210
$where = $dbi->where;
211
$where->clause(['and', 'key2 = :key2']);
212
$where->param({key2 => 2});
213
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
214
$result = $dbi->select(table => 'table1');
215
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
216

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

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

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
248
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
249
$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
250
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
251
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
252
$dbi->update({key2 => 11}, table => 'table1', where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
253
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
254
$rows   = $result->all;
255
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
256
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
257
                  "basic");
258

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
268
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
269
$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
270
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
271
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
272
$dbi->update(table => 'table1', param => {key2 => \"'11'"}, where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
273
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
274
$rows   = $result->all;
275
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
276
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
277
                  "basic");
278

            
279
test 'update_all';
cleanup test
Yuki Kimoto authored on 2011-08-06
280
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
281
$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
282
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
283
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
284
$dbi->register_filter(twice => sub { $_[0] * 2 });
285
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'});
test cleanup
Yuki Kimoto authored on 2011-08-06
286
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
287
$rows   = $result->all;
288
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
289
                  {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
290
                  "filter");
291

            
292

            
293
test 'delete';
cleanup test
Yuki Kimoto authored on 2011-08-06
294
$dbi = DBIx::Custom->connect(%memory);
295
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
296
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
297
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
298
$dbi->delete(table => 'table1', where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
299
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
300
$rows   = $result->all;
301
is_deeply($rows, [{key1 => 3, key2 => 4}], "basic");
302

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

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

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

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

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

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

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

            
357
test 'delete error';
cleanup test
Yuki Kimoto authored on 2011-08-06
358
$dbi = DBIx::Custom->connect(%memory);
359
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
360
eval{$dbi->delete(table => 'table1')};
361
like($@, qr/"where" must be specified/,
362
         "where key-value pairs not specified");
363

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

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

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

            
387

            
388
test 'select';
cleanup test
Yuki Kimoto authored on 2011-08-06
389
$dbi = DBIx::Custom->connect(%memory);
390
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
391
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
392
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
393
$rows = $dbi->select(table => 'table1')->all;
394
is_deeply($rows, [{key1 => 1, key2 => 2},
395
                  {key1 => 3, key2 => 4}], "table");
396

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

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

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

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

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

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

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

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

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

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

            
457
test 'filters';
458
$dbi = DBIx::Custom->new;
459

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

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

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

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

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

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

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

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

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

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

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

            
536

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

            
541
$dbi->begin_work;
542

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

            
549
$dbi->rollback if $@;
550

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

            
555
$dbi->begin_work;
556

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

            
562
$dbi->commit unless $@;
563

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

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

            
573

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1014
$where = $dbi->where;
1015
is("$where", '');
1016

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1206

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1341
test 'dbi_option default';
1342
$dbi = DBIx::Custom->new;
1343
is_deeply($dbi->dbi_option, {});
1344

            
1345
test 'register_tag_processor';
cleanup test
Yuki Kimoto authored on 2011-08-06
1346
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1347
$dbi->register_tag_processor(
1348
    a => sub { 1 }
1349
);
1350
is($dbi->query_builder->tag_processors->{a}->(), 1);
1351

            
1352
test 'register_tag';
cleanup test
Yuki Kimoto authored on 2011-08-06
1353
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1354
$dbi->register_tag(
1355
    b => sub { 2 }
1356
);
1357
is($dbi->query_builder->tags->{b}->(), 2);
1358

            
1359
test 'table not specify exception';
cleanup test
Yuki Kimoto authored on 2011-08-06
1360
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1361
eval {$dbi->insert};
1362
like($@, qr/table/);
1363
eval {$dbi->update};
1364
like($@, qr/table/);
1365
eval {$dbi->delete};
1366
like($@, qr/table/);
1367
eval {$dbi->select};
1368
like($@, qr/table/);
1369

            
1370

            
1371
test 'more tests';
cleanup test
Yuki Kimoto authored on 2011-08-06
1372
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1373
eval{$dbi->apply_filter('table', 'column', [])};
1374
like($@, qr/apply_filter/);
1375

            
1376
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
1377
like($@, qr/apply_filter/);
1378

            
1379
$dbi->apply_filter(
1380

            
1381
);
cleanup test
Yuki Kimoto authored on 2011-08-06
1382
$dbi = DBIx::Custom->connect(%memory);
1383
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
1384
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1385
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1386
$dbi->apply_filter('table1', 'key2', 
1387
                   {in => sub { $_[0] * 3 }, out => sub { $_[0] * 2 }});
1388
$rows = $dbi->select(table => 'table1', where => {key2 => 1})->all;
1389
is_deeply($rows, [{key1 => 1, key2 => 6}]);
1390

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1391
$dbi = DBIx::Custom->connect(%memory);
1392
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
1393
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1394
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1395
$dbi->apply_filter('table1', 'key2', {});
1396
$rows = $dbi->select(table => 'table1', where => {key2 => 2})->all;
1397
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1398

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1399
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1400
eval {$dbi->apply_filter('table1', 'key2', {out => 'no'})};
1401
like($@, qr/not registered/);
1402
eval {$dbi->apply_filter('table1', 'key2', {in => 'no'})};
1403
like($@, qr/not registered/);
1404
$dbi->method({one => sub { 1 }});
1405
is($dbi->one, 1);
1406

            
1407
eval{DBIx::Custom->connect()};
1408
like($@, qr/_connect/);
1409

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1410
$dbi = DBIx::Custom->connect(%memory);
1411
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
1412
$dbi->register_filter(twice => sub { $_[0] * 2 });
1413
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
1414
             filter => {key1 => 'twice'});
1415
$row = $dbi->select(table => 'table1')->one;
1416
is_deeply($row, {key1 => 2, key2 => 2});
1417
eval {$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
1418
             filter => {key1 => 'no'}) };
1419
like($@, qr//);
1420

            
1421
$dbi->register_filter(one => sub { });
1422
$dbi->default_fetch_filter('one');
1423
ok($dbi->default_fetch_filter);
1424
$dbi->default_bind_filter('one');
1425
ok($dbi->default_bind_filter);
1426
eval{$dbi->default_fetch_filter('no')};
1427
like($@, qr/not registered/);
1428
eval{$dbi->default_bind_filter('no')};
1429
like($@, qr/not registered/);
1430
$dbi->default_bind_filter(undef);
1431
ok(!defined $dbi->default_bind_filter);
1432
$dbi->default_fetch_filter(undef);
1433
ok(!defined $dbi->default_fetch_filter);
1434
eval {$dbi->execute('select * from table1 {} {= author') };
1435
like($@, qr/Tag not finished/);
1436

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1437
$dbi = DBIx::Custom->connect(%memory);
1438
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
1439
$dbi->register_filter(one => sub { 1 });
1440
$result = $dbi->select(table => 'table1');
1441
eval {$result->filter(key1 => 'no')};
1442
like($@, qr/not registered/);
1443
eval {$result->end_filter(key1 => 'no')};
1444
like($@, qr/not registered/);
1445
$result->default_filter(undef);
1446
ok(!defined $result->default_filter);
1447
$result->default_filter('one');
1448
is($result->default_filter->(), 1);
1449

            
1450
test 'dbi_option';
cleanup test
Yuki Kimoto authored on 2011-08-06
1451
$dbi = DBIx::Custom->connect(%memory,
cleanup test
Yuki Kimoto authored on 2011-08-06
1452
                             dbi_option => {PrintError => 1});
1453
ok($dbi->dbh->{PrintError});
cleanup test
Yuki Kimoto authored on 2011-08-06
1454
$dbi = DBIx::Custom->connect(%memory,
cleanup test
Yuki Kimoto authored on 2011-08-06
1455
                             dbi_options => {PrintError => 1});
1456
ok($dbi->dbh->{PrintError});
1457

            
1458
test 'DBIx::Custom::Result stash()';
1459
$result = DBIx::Custom::Result->new;
1460
is_deeply($result->stash, {}, 'default');
1461
$result->stash->{foo} = 1;
1462
is($result->stash->{foo}, 1, 'get and set');
1463

            
1464
test 'filter __ expression';
cleanup test
Yuki Kimoto authored on 2011-08-06
1465
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1466
$dbi->execute('create table company (id, name, location_id)');
1467
$dbi->execute('create table location (id, name)');
1468
$dbi->apply_filter('location',
1469
  name => {in => sub { uc $_[0] } }
1470
);
1471

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

            
1475
$result = $dbi->select(
1476
    table => ['company', 'location'], relation => {'company.location_id' => 'location.id'},
1477
    column => ['location.name as location__name']
1478
);
1479
is($result->fetch_first->[0], 'B');
1480

            
1481
$result = $dbi->select(
1482
    table => 'company', relation => {'company.location_id' => 'location.id'},
1483
    column => ['location.name as location__name']
1484
);
1485
is($result->fetch_first->[0], 'B');
1486

            
1487
$result = $dbi->select(
1488
    table => 'company', relation => {'company.location_id' => 'location.id'},
1489
    column => ['location.name as "location.name"']
1490
);
1491
is($result->fetch_first->[0], 'B');
1492

            
1493
test 'Model class';
1494
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1495
$dbi = MyDBI1->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1496
$dbi->execute("create table book (title, author)");
1497
$model = $dbi->model('book');
1498
$model->insert({title => 'a', author => 'b'});
1499
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
1500
$dbi->execute("create table company (name)");
1501
$model = $dbi->model('company');
1502
$model->insert({name => 'a'});
1503
is_deeply($model->list->all, [{name => 'a'}], 'basic');
1504
is($dbi->models->{'book'}, $dbi->model('book'));
1505
is($dbi->models->{'company'}, $dbi->model('company'));
1506

            
1507
{
1508
    package MyDBI4;
1509

            
1510
    use strict;
1511
    use warnings;
1512

            
1513
    use base 'DBIx::Custom';
1514

            
1515
    sub connect {
1516
        my $self = shift->SUPER::connect(@_);
1517
        
1518
        $self->include_model(
1519
            MyModel2 => [
1520
                'book',
1521
                {class => 'Company', name => 'company'}
1522
            ]
1523
        );
1524
    }
1525

            
1526
    package MyModel2::Base1;
1527

            
1528
    use strict;
1529
    use warnings;
1530

            
1531
    use base 'DBIx::Custom::Model';
1532

            
1533
    package MyModel2::book;
1534

            
1535
    use strict;
1536
    use warnings;
1537

            
1538
    use base 'MyModel2::Base1';
1539

            
1540
    sub insert {
1541
        my ($self, $param) = @_;
1542
        
1543
        return $self->SUPER::insert(param => $param);
1544
    }
1545

            
1546
    sub list { shift->select; }
1547

            
1548
    package MyModel2::Company;
1549

            
1550
    use strict;
1551
    use warnings;
1552

            
1553
    use base 'MyModel2::Base1';
1554

            
1555
    sub insert {
1556
        my ($self, $param) = @_;
1557
        
1558
        return $self->SUPER::insert(param => $param);
1559
    }
1560

            
1561
    sub list { shift->select; }
1562
}
cleanup test
Yuki Kimoto authored on 2011-08-06
1563
$dbi = MyDBI4->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1564
$dbi->execute("create table book (title, author)");
1565
$model = $dbi->model('book');
1566
$model->insert({title => 'a', author => 'b'});
1567
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
1568
$dbi->execute("create table company (name)");
1569
$model = $dbi->model('company');
1570
$model->insert({name => 'a'});
1571
is_deeply($model->list->all, [{name => 'a'}], 'basic');
1572

            
1573
{
1574
     package MyDBI5;
1575

            
1576
    use strict;
1577
    use warnings;
1578

            
1579
    use base 'DBIx::Custom';
1580

            
1581
    sub connect {
1582
        my $self = shift->SUPER::connect(@_);
1583
        
1584
        $self->include_model('MyModel4');
1585
    }
1586
}
cleanup test
Yuki Kimoto authored on 2011-08-06
1587
$dbi = MyDBI5->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1588
$dbi->execute("create table company (name)");
1589
$dbi->execute("create table table1 (key1)");
1590
$model = $dbi->model('company');
1591
$model->insert({name => 'a'});
1592
is_deeply($model->list->all, [{name => 'a'}], 'include all model');
1593
$dbi->insert(table => 'table1', param => {key1 => 1});
1594
$model = $dbi->model('book');
1595
is_deeply($model->list->all, [{key1 => 1}], 'include all model');
1596

            
1597
test 'primary_key';
1598
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1599
$dbi = MyDBI1->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1600
$model = $dbi->model('book');
1601
$model->primary_key(['id', 'number']);
1602
is_deeply($model->primary_key, ['id', 'number']);
1603

            
1604
test 'columns';
1605
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1606
$dbi = MyDBI1->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1607
$model = $dbi->model('book');
1608
$model->columns(['id', 'number']);
1609
is_deeply($model->columns, ['id', 'number']);
1610

            
1611
test 'setup_model';
1612
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1613
$dbi = MyDBI1->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1614
$dbi->execute('create table book (id)');
1615
$dbi->execute('create table company (id, name);');
1616
$dbi->execute('create table test (id, name, primary key (id, name));');
1617
$dbi->setup_model;
1618
is_deeply($dbi->model('book')->columns, ['id']);
1619
is_deeply($dbi->model('company')->columns, ['id', 'name']);
1620

            
1621
test 'delete_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1622
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1623
$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
1624
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1625
$dbi->delete_at(
1626
    table => 'table1',
1627
    primary_key => ['key1', 'key2'],
1628
    where => [1, 2],
1629
);
1630
is_deeply($dbi->select(table => 'table1')->all, []);
1631

            
1632
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1633
$dbi->delete_at(
1634
    table => 'table1',
1635
    primary_key => 'key1',
1636
    where => 1,
1637
);
1638
is_deeply($dbi->select(table => 'table1')->all, []);
1639

            
1640
test 'insert_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1641
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1642
$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
1643
$dbi->insert_at(
1644
    primary_key => ['key1', 'key2'], 
1645
    table => 'table1',
1646
    where => [1, 2],
1647
    param => {key3 => 3}
1648
);
1649
is($dbi->select(table => 'table1')->one->{key1}, 1);
1650
is($dbi->select(table => 'table1')->one->{key2}, 2);
1651
is($dbi->select(table => 'table1')->one->{key3}, 3);
1652

            
1653
$dbi->delete_all(table => 'table1');
1654
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1655
$dbi->insert_at(
1656
    primary_key => 'key1', 
1657
    table => 'table1',
1658
    where => 1,
1659
    param => {key2 => 2, key3 => 3}
1660
);
1661

            
1662
is($dbi->select(table => 'table1')->one->{key1}, 1);
1663
is($dbi->select(table => 'table1')->one->{key2}, 2);
1664
is($dbi->select(table => 'table1')->one->{key3}, 3);
1665

            
1666
eval {
1667
    $dbi->insert_at(
1668
        table => 'table1',
1669
        primary_key => ['key1', 'key2'],
1670
        where => {},
1671
        param => {key1 => 1, key2 => 2, key3 => 3},
1672
    );
1673
};
1674
like($@, qr/must be/);
1675

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1676
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1677
$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
1678
$dbi->insert_at(
1679
    {key3 => 3},
1680
    primary_key => ['key1', 'key2'], 
1681
    table => 'table1',
1682
    where => [1, 2],
1683
);
1684
is($dbi->select(table => 'table1')->one->{key1}, 1);
1685
is($dbi->select(table => 'table1')->one->{key2}, 2);
1686
is($dbi->select(table => 'table1')->one->{key3}, 3);
1687

            
1688
test 'update_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1689
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1690
$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
1691
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1692
$dbi->update_at(
1693
    table => 'table1',
1694
    primary_key => ['key1', 'key2'],
1695
    where => [1, 2],
1696
    param => {key3 => 4}
1697
);
1698
is($dbi->select(table => 'table1')->one->{key1}, 1);
1699
is($dbi->select(table => 'table1')->one->{key2}, 2);
1700
is($dbi->select(table => 'table1')->one->{key3}, 4);
1701

            
1702
$dbi->delete_all(table => 'table1');
1703
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1704
$dbi->update_at(
1705
    table => 'table1',
1706
    primary_key => 'key1',
1707
    where => 1,
1708
    param => {key3 => 4}
1709
);
1710
is($dbi->select(table => 'table1')->one->{key1}, 1);
1711
is($dbi->select(table => 'table1')->one->{key2}, 2);
1712
is($dbi->select(table => 'table1')->one->{key3}, 4);
1713

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

            
1727
test 'select_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1728
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1729
$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
1730
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1731
$result = $dbi->select_at(
1732
    table => 'table1',
1733
    primary_key => ['key1', 'key2'],
1734
    where => [1, 2]
1735
);
1736
$row = $result->one;
1737
is($row->{key1}, 1);
1738
is($row->{key2}, 2);
1739
is($row->{key3}, 3);
1740

            
1741
$dbi->delete_all(table => 'table1');
1742
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1743
$result = $dbi->select_at(
1744
    table => 'table1',
1745
    primary_key => 'key1',
1746
    where => 1,
1747
);
1748
$row = $result->one;
1749
is($row->{key1}, 1);
1750
is($row->{key2}, 2);
1751
is($row->{key3}, 3);
1752

            
1753
$dbi->delete_all(table => 'table1');
1754
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1755
$result = $dbi->select_at(
1756
    table => 'table1',
1757
    primary_key => ['key1', 'key2'],
1758
    where => [1, 2]
1759
);
1760
$row = $result->one;
1761
is($row->{key1}, 1);
1762
is($row->{key2}, 2);
1763
is($row->{key3}, 3);
1764

            
1765
eval {
1766
    $result = $dbi->select_at(
1767
        table => 'table1',
1768
        primary_key => ['key1', 'key2'],
1769
        where => {},
1770
    );
1771
};
1772
like($@, qr/must be/);
1773

            
1774
eval {
1775
    $result = $dbi->select_at(
1776
        table => 'table1',
1777
        primary_key => ['key1', 'key2'],
1778
        where => [1],
1779
    );
1780
};
1781
like($@, qr/same/);
1782

            
1783
eval {
1784
    $result = $dbi->update_at(
1785
        table => 'table1',
1786
        primary_key => ['key1', 'key2'],
1787
        where => {},
1788
        param => {key1 => 1, key2 => 2},
1789
    );
1790
};
1791
like($@, qr/must be/);
1792

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

            
1802
test 'columns';
1803
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1804
$dbi = MyDBI1->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1805
$model = $dbi->model('book');
1806

            
1807

            
1808
test 'model delete_at';
1809
{
1810
    package MyDBI6;
1811
    
1812
    use base 'DBIx::Custom';
1813
    
1814
    sub connect {
1815
        my $self = shift->SUPER::connect(@_);
1816
        
1817
        $self->include_model('MyModel5');
1818
        
1819
        return $self;
1820
    }
1821
}
cleanup test
Yuki Kimoto authored on 2011-08-06
1822
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1823
$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
1824
$dbi->execute("create table table2 (key1, key2, key3)");
1825
$dbi->execute("create table table3 (key1, key2, key3)");
1826
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1827
$dbi->model('table1')->delete_at(where => [1, 2]);
1828
is_deeply($dbi->select(table => 'table1')->all, []);
1829
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
1830
$dbi->model('table1_1')->delete_at(where => [1, 2]);
1831
is_deeply($dbi->select(table => 'table1')->all, []);
1832
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
1833
$dbi->model('table1_3')->delete_at(where => [1, 2]);
1834
is_deeply($dbi->select(table => 'table1')->all, []);
1835

            
1836
test 'model insert_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1837
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1838
$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
1839
$dbi->model('table1')->insert_at(
1840
    where => [1, 2],
1841
    param => {key3 => 3}
1842
);
1843
$result = $dbi->model('table1')->select;
1844
$row = $result->one;
1845
is($row->{key1}, 1);
1846
is($row->{key2}, 2);
1847
is($row->{key3}, 3);
1848

            
1849
test 'model update_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1850
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1851
$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
1852
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1853
$dbi->model('table1')->update_at(
1854
    where => [1, 2],
1855
    param => {key3 => 4}
1856
);
1857
$result = $dbi->model('table1')->select;
1858
$row = $result->one;
1859
is($row->{key1}, 1);
1860
is($row->{key2}, 2);
1861
is($row->{key3}, 4);
1862

            
1863
test 'model select_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1864
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1865
$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
1866
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1867
$result = $dbi->model('table1')->select_at(where => [1, 2]);
1868
$row = $result->one;
1869
is($row->{key1}, 1);
1870
is($row->{key2}, 2);
1871
is($row->{key3}, 3);
1872

            
1873

            
1874
test 'mycolumn and column';
1875
{
1876
    package MyDBI7;
1877
    
1878
    use base 'DBIx::Custom';
1879
    
1880
    sub connect {
1881
        my $self = shift->SUPER::connect(@_);
1882
        
1883
        $self->include_model('MyModel6');
1884
        
1885
        
1886
        return $self;
1887
    }
1888
}
cleanup test
Yuki Kimoto authored on 2011-08-06
1889
$dbi = MyDBI7->connect(%memory);
1890
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
1891
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1892
$dbi->separator('__');
1893
$dbi->setup_model;
1894
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1895
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1896
$model = $dbi->model('table1');
1897
$result = $model->select(
1898
    column => [$model->mycolumn, $model->column('table2')],
1899
    where => {'table1.key1' => 1}
1900
);
1901
is_deeply($result->one,
1902
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
1903

            
1904
test 'update_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
1905
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1906
$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
1907
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1908
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1909

            
1910
$param = {key2 => 11};
1911
$update_param = $dbi->update_param($param);
1912
$sql = <<"EOS";
1913
update table1 $update_param
1914
where key1 = 1
1915
EOS
1916
$dbi->execute($sql, param => $param);
test cleanup
Yuki Kimoto authored on 2011-08-06
1917
$result = $dbi->execute('select * from table1;', table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
1918
$rows   = $result->all;
1919
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
1920
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1921
                  "basic");
1922

            
1923

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1924
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1925
$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
1926
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1927
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1928

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1942
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1943
$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
1944
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1945
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1946

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

            
1960
            
1961
eval { $dbi->update_param({";" => 1}) };
1962
like($@, qr/not safety/);
1963

            
1964

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

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

            
1984

            
1985
test 'insert_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
1986
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1987
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
1988
$param = {key1 => 1, key2 => 2};
1989
$insert_param = $dbi->insert_param($param);
1990
$sql = <<"EOS";
1991
insert into table1 $insert_param
1992
EOS
1993
$dbi->execute($sql, param => $param, table => 'table1');
1994
is($dbi->select(table => 'table1')->one->{key1}, 1);
1995
is($dbi->select(table => 'table1')->one->{key2}, 2);
1996

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1997
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1998
$dbi->quote('"');
1999
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
2000
$param = {key1 => 1, key2 => 2};
2001
$insert_param = $dbi->insert_param($param);
2002
$sql = <<"EOS";
2003
insert into table1 $insert_param
2004
EOS
2005
$dbi->execute($sql, param => $param, table => 'table1');
2006
is($dbi->select(table => 'table1')->one->{key1}, 1);
2007
is($dbi->select(table => 'table1')->one->{key2}, 2);
2008

            
2009
eval { $dbi->insert_param({";" => 1}) };
2010
like($@, qr/not safety/);
2011

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

            
2013
test 'join';
cleanup test
Yuki Kimoto authored on 2011-08-06
2014
$dbi = DBIx::Custom->connect(%memory);
2015
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2016
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2017
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
cleanup test
Yuki Kimoto authored on 2011-08-06
2018
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2019
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
cleanup test
Yuki Kimoto authored on 2011-08-06
2020
$dbi->execute('create table table3 (key3 int, key4 int);');
cleanup test
Yuki Kimoto authored on 2011-08-06
2021
$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4});
2022
$rows = $dbi->select(
2023
    table => 'table1',
2024
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
2025
    where   => {'table1.key2' => 2},
2026
    join  => ['left outer join table2 on table1.key1 = table2.key1']
2027
)->all;
2028
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]);
2029

            
2030
$rows = $dbi->select(
2031
    table => 'table1',
2032
    where   => {'key1' => 1},
2033
    join  => ['left outer join table2 on table1.key1 = table2.key1']
2034
)->all;
2035
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2036

            
2037
eval {
2038
    $rows = $dbi->select(
2039
        table => 'table1',
2040
        column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
2041
        where   => {'table1.key2' => 2},
2042
        join  => {'table1.key1' => 'table2.key1'}
2043
    );
2044
};
2045
like ($@, qr/array/);
2046

            
2047
$rows = $dbi->select(
2048
    table => 'table1',
2049
    where   => {'key1' => 1},
2050
    join  => ['left outer join table2 on table1.key1 = table2.key1',
2051
              'left outer join table3 on table2.key3 = table3.key3']
2052
)->all;
2053
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2054

            
2055
$rows = $dbi->select(
2056
    column => 'table3.key4 as table3__key4',
2057
    table => 'table1',
2058
    where   => {'table1.key1' => 1},
2059
    join  => ['left outer join table2 on table1.key1 = table2.key1',
2060
              'left outer join table3 on table2.key3 = table3.key3']
2061
)->all;
2062
is_deeply($rows, [{table3__key4 => 4}]);
2063

            
2064
$rows = $dbi->select(
2065
    column => 'table1.key1 as table1__key1',
2066
    table => 'table1',
2067
    where   => {'table3.key4' => 4},
2068
    join  => ['left outer join table2 on table1.key1 = table2.key1',
2069
              'left outer join table3 on table2.key3 = table3.key3']
2070
)->all;
2071
is_deeply($rows, [{table1__key1 => 1}]);
2072

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2073
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2074
$dbi->quote('"');
cleanup test
Yuki Kimoto authored on 2011-08-06
2075
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2076
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-06
2077
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2078
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
2079
$rows = $dbi->select(
2080
    table => 'table1',
2081
    column => '"table1"."key1" as "table1_key1", "table2"."key1" as "table2_key1", "key2", "key3"',
2082
    where   => {'table1.key2' => 2},
2083
    join  => ['left outer join "table2" on "table1"."key1" = "table2"."key1"'],
2084
)->all;
2085
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}],
2086
          'quote');
2087

            
2088
{
2089
    package MyDBI8;
2090
    
2091
    use base 'DBIx::Custom';
2092
    
2093
    sub connect {
2094
        my $self = shift->SUPER::connect(@_);
2095
        
2096
        $self->include_model('MyModel7');
2097
        
2098
        return $self;
2099
    }
2100
}
cleanup test
Yuki Kimoto authored on 2011-08-06
2101

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2102
$dbi = DBIx::Custom->connect(%memory);
2103
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2104
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2105
$sql = <<"EOS";
cleanup test
Yuki Kimoto authored on 2011-08-06
2106
left outer join (
2107
  select * from table1 as t1
2108
  where t1.key2 = (
2109
    select max(t2.key2) from table1 as t2
2110
    where t1.key1 = t2.key1
2111
  )
2112
) as latest_table1 on table1.key1 = latest_table1.key1
2113
EOS
cleanup test
Yuki Kimoto authored on 2011-08-06
2114
$join = [$sql];
2115
$rows = $dbi->select(
2116
    table => 'table1',
2117
    column => 'latest_table1.key1 as latest_table1__key1',
2118
    join  => $join
2119
)->all;
2120
is_deeply($rows, [{latest_table1__key1 => 1}]);
2121

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2122
$dbi = DBIx::Custom->connect(%memory);
2123
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2124
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2125
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2126
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2127
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
2128
$result = $dbi->select(
2129
    table => 'table1',
2130
    join => [
2131
        "left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1"
2132
    ]
2133
);
2134
is_deeply($result->all, [{key1 => 1, key2 => 2}]);
2135
$result = $dbi->select(
2136
    table => 'table1',
2137
    column => [{table2 => ['key3']}],
2138
    join => [
2139
        "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1"
2140
    ]
2141
);
2142
is_deeply($result->all, [{'table2.key3' => 4}]);
2143
$result = $dbi->select(
2144
    table => 'table1',
2145
    column => [{table2 => ['key3']}],
2146
    join => [
2147
        "left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'"
2148
    ]
2149
);
2150
is_deeply($result->all, [{'table2.key3' => 4}]);
2151

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

            
2170
test 'mycolumn';
cleanup test
Yuki Kimoto authored on 2011-08-06
2171
$dbi = MyDBI8->connect(%memory);
2172
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2173
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2174
$dbi->setup_model;
2175
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2176
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2177
$model = $dbi->model('table1');
2178
$result = $model->select_at(
2179
    column => [
2180
        $model->mycolumn,
2181
        $model->column('table2')
2182
    ]
2183
);
2184
is_deeply($result->one,
2185
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
2186

            
2187
$result = $model->select_at(
2188
    column => [
2189
        $model->mycolumn(['key1']),
2190
        $model->column(table2 => ['key1'])
2191
    ]
2192
);
2193
is_deeply($result->one,
2194
          {key1 => 1, 'table2.key1' => 1});
2195
$result = $model->select_at(
2196
    column => [
2197
        $model->mycolumn(['key1']),
2198
        {table2 => ['key1']}
2199
    ]
2200
);
2201
is_deeply($result->one,
2202
          {key1 => 1, 'table2.key1' => 1});
2203

            
2204
$result = $model->select_at(
2205
    column => [
2206
        $model->mycolumn(['key1']),
2207
        ['table2.key1', as => 'table2.key1']
2208
    ]
2209
);
2210
is_deeply($result->one,
2211
          {key1 => 1, 'table2.key1' => 1});
2212

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

            
2222
test 'dbi method from model';
2223
{
2224
    package MyDBI9;
2225
    
2226
    use base 'DBIx::Custom';
2227
    
2228
    sub connect {
2229
        my $self = shift->SUPER::connect(@_);
2230
        
2231
        $self->include_model('MyModel8')->setup_model;
2232
        
2233
        return $self;
2234
    }
2235
}
cleanup test
Yuki Kimoto authored on 2011-08-06
2236
$dbi = MyDBI9->connect(%memory);
2237
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2238
$model = $dbi->model('table1');
2239
eval{$model->execute('select * from table1')};
2240
ok(!$@);
2241

            
2242
test 'column table option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2243
$dbi = MyDBI9->connect(%memory);
2244
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2245
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2246
$dbi->setup_model;
2247
$dbi->execute('insert into table1 (key1, key2) values (1, 2);');
2248
$dbi->execute('insert into table2 (key1, key3) values (1, 4);');
2249
$model = $dbi->model('table1');
2250
$result = $model->select(
2251
    column => [
2252
        $model->column('table2', {alias => 'table2_alias'})
2253
    ],
2254
    where => {'table2_alias.key3' => 4}
2255
);
2256
is_deeply($result->one, 
2257
          {'table2_alias.key1' => 1, 'table2_alias.key3' => 4});
2258

            
2259
$dbi->separator('__');
2260
$result = $model->select(
2261
    column => [
2262
        $model->column('table2', {alias => 'table2_alias'})
2263
    ],
2264
    where => {'table2_alias.key3' => 4}
2265
);
2266
is_deeply($result->one, 
2267
          {'table2_alias__key1' => 1, 'table2_alias__key3' => 4});
2268

            
2269
$dbi->separator('-');
2270
$result = $model->select(
2271
    column => [
2272
        $model->column('table2', {alias => 'table2_alias'})
2273
    ],
2274
    where => {'table2_alias.key3' => 4}
2275
);
2276
is_deeply($result->one, 
2277
          {'table2_alias-key1' => 1, 'table2_alias-key3' => 4});
2278

            
2279
test 'type option'; # DEPRECATED!
2280
$dbi = DBIx::Custom->connect(
2281
    data_source => 'dbi:SQLite:dbname=:memory:',
2282
    dbi_option => {
2283
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
2284
    }
2285
);
2286
my $binary = pack("I3", 1, 2, 3);
2287
$dbi->execute('create table table1(key1, key2)');
2288
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
2289
$result = $dbi->select(table => 'table1');
2290
$row   = $result->one;
2291
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2292
$result = $dbi->execute('select length(key1) as key1_length from table1');
2293
$row = $result->one;
2294
is($row->{key1_length}, length $binary);
2295

            
2296
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [['key1'] => DBI::SQL_BLOB]);
2297
$result = $dbi->select(table => 'table1');
2298
$row   = $result->one;
2299
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2300
$result = $dbi->execute('select length(key1) as key1_length from table1');
2301
$row = $result->one;
2302
is($row->{key1_length}, length $binary);
2303

            
2304

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

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

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

            
2348
test 'create_model';
cleanup test
Yuki Kimoto authored on 2011-08-06
2349
$dbi = DBIx::Custom->connect(%memory);
2350
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2351
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2352

            
2353
$dbi->create_model(
2354
    table => 'table1',
2355
    join => [
2356
       'left outer join table2 on table1.key1 = table2.key1'
2357
    ],
2358
    primary_key => ['key1']
2359
);
2360
$model2 = $dbi->create_model(
2361
    table => 'table2'
2362
);
2363
$dbi->create_model(
2364
    table => 'table3',
2365
    filter => [
2366
        key1 => {in => sub { uc $_[0] }}
2367
    ]
2368
);
2369
$dbi->setup_model;
2370
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2371
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2372
$model = $dbi->model('table1');
2373
$result = $model->select(
2374
    column => [$model->mycolumn, $model->column('table2')],
2375
    where => {'table1.key1' => 1}
2376
);
2377
is_deeply($result->one,
2378
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
2379
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
2380

            
2381
test 'model method';
2382
test 'create_model';
cleanup test
Yuki Kimoto authored on 2011-08-06
2383
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2384
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2385
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2386
$model = $dbi->create_model(
2387
    table => 'table2'
2388
);
2389
$model->method(foo => sub { shift->select(@_) });
2390
is_deeply($model->foo->one, {key1 => 1, key3 => 3});
2391

            
2392
test 'merge_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
2393
$dbi = DBIx::Custom->new;
2394
$params = [
2395
    {key1 => 1, key2 => 2, key3 => 3},
2396
    {key1 => 1, key2 => 2},
2397
    {key1 => 1}
2398
];
2399
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]);
2400
is_deeply($param, {key1 => [1, 1, 1], key2 => [2, 2], key3 => 3});
2401

            
2402
$params = [
2403
    {key1 => [1, 2], key2 => 1, key3 => [1, 2]},
2404
    {key1 => [3, 4], key2 => [2, 3], key3 => 3}
2405
];
2406
$param = $dbi->merge_param($params->[0], $params->[1]);
2407
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
2408

            
2409
test 'select() param option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2410
$dbi = DBIx::Custom->connect(%memory);
2411
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2412
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2413
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
2414
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2415
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2416
$dbi->insert(table => 'table2', param => {key1 => 2, key3 => 5});
2417
$rows = $dbi->select(
2418
    table => 'table1',
2419
    column => 'table1.key1 as table1_key1, key2, key3',
2420
    where   => {'table1.key2' => 3},
2421
    join  => ['inner join (select * from table2 where {= table2.key3})' . 
2422
              ' as table2 on table1.key1 = table2.key1'],
2423
    param => {'table2.key3' => 5}
2424
)->all;
2425
is_deeply($rows, [{table1_key1 => 2, key2 => 3, key3 => 5}]);
2426

            
2427

            
2428
test 'select() wrap option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2429
$dbi = DBIx::Custom->connect(%memory);
2430
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2431
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2432
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2433
$rows = $dbi->select(
2434
    table => 'table1',
2435
    column => 'key1',
2436
    wrap => ['select * from (', ') as t where key1 = 1']
2437
)->all;
2438
is_deeply($rows, [{key1 => 1}]);
2439

            
2440
eval {
2441
$dbi->select(
2442
    table => 'table1',
2443
    column => 'key1',
2444
    wrap => 'select * from ('
2445
)
2446
};
2447
like($@, qr/array/);
2448

            
2449
test 'select() string where';
cleanup test
Yuki Kimoto authored on 2011-08-06
2450
$dbi = DBIx::Custom->connect(%memory);
2451
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2452
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2453
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2454
$rows = $dbi->select(
2455
    table => 'table1',
2456
    where => 'key1 = :key1 and key2 = :key2',
2457
    where_param => {key1 => 1, key2 => 2}
2458
)->all;
2459
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2460

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2461
$dbi = DBIx::Custom->connect(%memory);
2462
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2463
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2464
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2465
$rows = $dbi->select(
2466
    table => 'table1',
2467
    where => [
2468
        'key1 = :key1 and key2 = :key2',
2469
        {key1 => 1, key2 => 2}
2470
    ]
2471
)->all;
2472
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2473

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

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

            
2501

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

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

            
2529
test 'insert id and primary_key option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2530
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2531
$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
2532
$dbi->insert(
2533
    primary_key => ['key1', 'key2'], 
2534
    table => 'table1',
2535
    id => [1, 2],
2536
    param => {key3 => 3}
2537
);
2538
is($dbi->select(table => 'table1')->one->{key1}, 1);
2539
is($dbi->select(table => 'table1')->one->{key2}, 2);
2540
is($dbi->select(table => 'table1')->one->{key3}, 3);
2541

            
2542
$dbi->delete_all(table => 'table1');
2543
$dbi->insert(
2544
    primary_key => 'key1', 
2545
    table => 'table1',
2546
    id => 0,
2547
    param => {key2 => 2, key3 => 3}
2548
);
2549

            
2550
is($dbi->select(table => 'table1')->one->{key1}, 0);
2551
is($dbi->select(table => 'table1')->one->{key2}, 2);
2552
is($dbi->select(table => 'table1')->one->{key3}, 3);
2553

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2554
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2555
$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
2556
$dbi->insert(
2557
    {key3 => 3},
2558
    primary_key => ['key1', 'key2'], 
2559
    table => 'table1',
2560
    id => [1, 2],
2561
);
2562
is($dbi->select(table => 'table1')->one->{key1}, 1);
2563
is($dbi->select(table => 'table1')->one->{key2}, 2);
2564
is($dbi->select(table => 'table1')->one->{key3}, 3);
2565

            
2566

            
2567
test 'model insert id and primary_key option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2568
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2569
$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
2570
$dbi->model('table1')->insert(
2571
    id => [1, 2],
2572
    param => {key3 => 3}
2573
);
2574
$result = $dbi->model('table1')->select;
2575
$row = $result->one;
2576
is($row->{key1}, 1);
2577
is($row->{key2}, 2);
2578
is($row->{key3}, 3);
2579

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2580
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2581
$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
2582
$dbi->model('table1')->insert(
2583
    {key3 => 3},
2584
    id => [1, 2]
2585
);
2586
$result = $dbi->model('table1')->select;
2587
$row = $result->one;
2588
is($row->{key1}, 1);
2589
is($row->{key2}, 2);
2590
is($row->{key3}, 3);
2591

            
2592
test 'update and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2593
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2594
$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
2595
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2596
$dbi->update(
2597
    table => 'table1',
2598
    primary_key => ['key1', 'key2'],
2599
    id => [1, 2],
2600
    param => {key3 => 4}
2601
);
2602
is($dbi->select(table => 'table1')->one->{key1}, 1);
2603
is($dbi->select(table => 'table1')->one->{key2}, 2);
2604
is($dbi->select(table => 'table1')->one->{key3}, 4);
2605

            
2606
$dbi->delete_all(table => 'table1');
2607
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2608
$dbi->update(
2609
    table => 'table1',
2610
    primary_key => 'key1',
2611
    id => 0,
2612
    param => {key3 => 4}
2613
);
2614
is($dbi->select(table => 'table1')->one->{key1}, 0);
2615
is($dbi->select(table => 'table1')->one->{key2}, 2);
2616
is($dbi->select(table => 'table1')->one->{key3}, 4);
2617

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2618
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2619
$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
2620
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2621
$dbi->update(
2622
    {key3 => 4},
2623
    table => 'table1',
2624
    primary_key => ['key1', 'key2'],
2625
    id => [1, 2]
2626
);
2627
is($dbi->select(table => 'table1')->one->{key1}, 1);
2628
is($dbi->select(table => 'table1')->one->{key2}, 2);
2629
is($dbi->select(table => 'table1')->one->{key3}, 4);
2630

            
2631

            
2632
test 'model update and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2633
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2634
$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
2635
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2636
$dbi->model('table1')->update(
2637
    id => [1, 2],
2638
    param => {key3 => 4}
2639
);
2640
$result = $dbi->model('table1')->select;
2641
$row = $result->one;
2642
is($row->{key1}, 1);
2643
is($row->{key2}, 2);
2644
is($row->{key3}, 4);
2645

            
2646

            
2647
test 'delete and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2648
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2649
$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
2650
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2651
$dbi->delete(
2652
    table => 'table1',
2653
    primary_key => ['key1', 'key2'],
2654
    id => [1, 2],
2655
);
2656
is_deeply($dbi->select(table => 'table1')->all, []);
2657

            
2658
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2659
$dbi->delete(
2660
    table => 'table1',
2661
    primary_key => 'key1',
2662
    id => 0,
2663
);
2664
is_deeply($dbi->select(table => 'table1')->all, []);
2665

            
2666

            
2667
test 'model delete and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2668
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2669
$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
2670
$dbi->execute("create table table2 (key1, key2, key3)");
2671
$dbi->execute("create table table3 (key1, key2, key3)");
2672
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2673
$dbi->model('table1')->delete(id => [1, 2]);
2674
is_deeply($dbi->select(table => 'table1')->all, []);
2675
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
2676
$dbi->model('table1_1')->delete(id => [1, 2]);
2677
is_deeply($dbi->select(table => 'table1')->all, []);
2678
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
2679
$dbi->model('table1_3')->delete(id => [1, 2]);
2680
is_deeply($dbi->select(table => 'table1')->all, []);
2681

            
2682

            
2683
test 'select and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2684
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2685
$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
2686
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2687
$result = $dbi->select(
2688
    table => 'table1',
2689
    primary_key => ['key1', 'key2'],
2690
    id => [1, 2]
2691
);
2692
$row = $result->one;
2693
is($row->{key1}, 1);
2694
is($row->{key2}, 2);
2695
is($row->{key3}, 3);
2696

            
2697
$dbi->delete_all(table => 'table1');
2698
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2699
$result = $dbi->select(
2700
    table => 'table1',
2701
    primary_key => 'key1',
2702
    id => 0,
2703
);
2704
$row = $result->one;
2705
is($row->{key1}, 0);
2706
is($row->{key2}, 2);
2707
is($row->{key3}, 3);
2708

            
2709
$dbi->delete_all(table => 'table1');
2710
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2711
$result = $dbi->select(
2712
    table => 'table1',
2713
    primary_key => ['key1', 'key2'],
2714
    id => [1, 2]
2715
);
2716
$row = $result->one;
2717
is($row->{key1}, 1);
2718
is($row->{key2}, 2);
2719
is($row->{key3}, 3);
2720

            
2721

            
2722
test 'model select_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
2723
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2724
$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
2725
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2726
$result = $dbi->model('table1')->select(id => [1, 2]);
2727
$row = $result->one;
2728
is($row->{key1}, 1);
2729
is($row->{key2}, 2);
2730
is($row->{key3}, 3);
2731

            
2732
test 'column separator is default .';
cleanup test
Yuki Kimoto authored on 2011-08-06
2733
$dbi = MyDBI7->connect(%memory);
2734
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2735
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2736
$dbi->setup_model;
2737
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2738
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2739
$model = $dbi->model('table1');
2740
$result = $model->select(
2741
    column => [$model->column('table2')],
2742
    where => {'table1.key1' => 1}
2743
);
2744
is_deeply($result->one,
2745
          {'table2.key1' => 1, 'table2.key3' => 3});
2746

            
2747
$result = $model->select(
2748
    column => [$model->column('table2' => [qw/key1 key3/])],
2749
    where => {'table1.key1' => 1}
2750
);
2751
is_deeply($result->one,
2752
          {'table2.key1' => 1, 'table2.key3' => 3});
2753

            
2754

            
2755
test 'type_rule from';
cleanup test
Yuki Kimoto authored on 2011-08-06
2756
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2757
$dbi->type_rule(
2758
    from1 => {
2759
        date => sub { uc $_[0] }
2760
    }
2761
);
2762
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2763
$dbi->insert({key1 => 'a'}, table => 'table1');
2764
$result = $dbi->select(table => 'table1');
2765
is($result->fetch_first->[0], 'A');
2766

            
2767
$result = $dbi->select(table => 'table1');
2768
is($result->one->{key1}, 'A');
2769

            
2770

            
2771
test 'type_rule into';
cleanup test
Yuki Kimoto authored on 2011-08-06
2772
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2773
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2774
$dbi->type_rule(
2775
    into1 => {
2776
        date => sub { uc $_[0] }
2777
    }
2778
);
2779
$dbi->insert({key1 => 'a'}, table => 'table1');
2780
$result = $dbi->select(table => 'table1');
2781
is($result->one->{key1}, 'A');
2782

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2796
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2797
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2798
$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1');
2799
$dbi->type_rule(
2800
    into1 => [
2801
        [qw/date datetime/] => sub { uc $_[0] }
2802
    ]
2803
);
2804
$result = $dbi->execute(
2805
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
2806
    param => {key1 => 'a', 'table1.key2' => 'b'}
2807
);
2808
$row = $result->one;
2809
is($row->{key1}, 'a');
2810
is($row->{key2}, 'B');
2811

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2829
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2830
$dbi->execute("create table table1 (key1 date, key2 datetime)");
2831
$dbi->register_filter(twice => sub { $_[0] * 2 });
2832
$dbi->type_rule(
2833
    from1 => {
2834
        date => 'twice',
2835
    },
2836
    into1 => {
2837
        date => 'twice',
2838
    }
2839
);
2840
$dbi->insert({key1 => 2}, table => 'table1');
2841
$result = $dbi->select(table => 'table1');
2842
is($result->fetch->[0], 8);
2843

            
2844
test 'type_rule and filter order';
cleanup test
Yuki Kimoto authored on 2011-08-06
2845
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2846
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2847
$dbi->type_rule(
2848
    into1 => {
2849
        date => sub { $_[0] . 'b' }
2850
    },
2851
    into2 => {
2852
        date => sub { $_[0] . 'c' }
2853
    },
2854
    from1 => {
2855
        date => sub { $_[0] . 'd' }
2856
    },
2857
    from2 => {
2858
        date => sub { $_[0] . 'e' }
2859
    }
2860
);
2861
$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }});
2862
$result = $dbi->select(table => 'table1');
2863
$result->filter(key1 => sub { $_[0] . 'f' });
2864
is($result->fetch_first->[0], '1abcdef');
2865

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

            
2889
test 'type_rule_off';
cleanup test
Yuki Kimoto authored on 2011-08-06
2890
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2891
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2892
$dbi->type_rule(
2893
    from1 => {
2894
        date => sub { $_[0] * 2 },
2895
    },
2896
    into1 => {
2897
        date => sub { $_[0] * 2 },
2898
    }
2899
);
2900
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
2901
$result = $dbi->select(table => 'table1', type_rule_off => 1);
2902
is($result->type_rule_off->fetch->[0], 2);
2903

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2904
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2905
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2906
$dbi->type_rule(
2907
    from1 => {
2908
        date => sub { $_[0] * 2 },
2909
    },
2910
    into1 => {
2911
        date => sub { $_[0] * 3 },
2912
    }
2913
);
2914
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
2915
$result = $dbi->select(table => 'table1', type_rule_off => 1);
2916
is($result->one->{key1}, 4);
2917

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

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

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

            
2958
eval{$dbi->type_rule(
2959
    into1 => {
2960
        date => 'pp'
2961
    }
2962
)};
2963
like($@, qr/not registered/);
2964

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2965
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2966
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2967
eval {
2968
    $dbi->type_rule(
2969
        from1 => {
2970
            Date => sub { $_[0] * 2 },
2971
        }
2972
    );
2973
};
2974
like($@, qr/lower/);
2975

            
2976
eval {
2977
    $dbi->type_rule(
2978
        into1 => {
2979
            Date => sub { $_[0] * 2 },
2980
        }
2981
    );
2982
};
2983
like($@, qr/lower/);
2984

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2985
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2986
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2987
$dbi->type_rule(
2988
    from1 => {
2989
        date => sub { $_[0] * 2 },
2990
    },
2991
    into1 => {
2992
        date => sub { $_[0] * 3 },
2993
    }
2994
);
2995
$dbi->insert({key1 => 2}, table => 'table1');
2996
$result = $dbi->select(table => 'table1');
2997
$result->type_rule_off;
2998
is($result->one->{key1}, 6);
2999

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

            
3019
$result = $dbi->select(table => 'table1');
3020
$result->type_rule(
3021
    from1 => {
3022
        date => sub { $_[0] * 3 }
3023
    }
3024
);
3025
$row = $result->one;
3026
is($row->{key1}, 6);
3027
is($row->{key2}, 2);
3028

            
3029
$result = $dbi->select(table => 'table1');
3030
$result->type_rule(
3031
    from1 => {
3032
        date => sub { $_[0] * 3 }
3033
    }
3034
);
3035
$row = $result->one;
3036
is($row->{key1}, 6);
3037
is($row->{key2}, 2);
3038
$result = $dbi->select(table => 'table1');
3039
$result->type_rule(
3040
    from1 => [date => sub { $_[0] * 3 }]
3041
);
3042
$row = $result->one;
3043
is($row->{key1}, 6);
3044
is($row->{key2}, 2);
3045
$dbi->register_filter(fivetimes => sub { $_[0] * 5});
3046
$result = $dbi->select(table => 'table1');
3047
$result->type_rule(
3048
    from1 => [date => 'fivetimes']
3049
);
3050
$row = $result->one;
3051
is($row->{key1}, 10);
3052
is($row->{key2}, 2);
3053
$result = $dbi->select(table => 'table1');
3054
$result->type_rule(
3055
    from1 => [date => undef]
3056
);
3057
$row = $result->one;
3058
is($row->{key1}, 2);
3059
is($row->{key2}, 2);
3060

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3061
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3062
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3063
$dbi->type_rule(
3064
    from1 => {
3065
        date => sub { $_[0] * 2 },
3066
    },
3067
);
3068
$dbi->insert({key1 => 2}, table => 'table1');
3069
$result = $dbi->select(table => 'table1');
3070
$result->filter(key1 => sub { $_[0] * 3 });
3071
is($result->one->{key1}, 12);
3072

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3073
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3074
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3075
$dbi->type_rule(
3076
    from1 => {
3077
        date => sub { $_[0] * 2 },
3078
    },
3079
);
3080
$dbi->insert({key1 => 2}, table => 'table1');
3081
$result = $dbi->select(table => 'table1');
3082
$result->filter(key1 => sub { $_[0] * 3 });
3083
is($result->fetch->[0], 12);
3084

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3085
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3086
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3087
$dbi->type_rule(
3088
    into1 => {
3089
        date => sub { $_[0] . 'b' }
3090
    },
3091
    into2 => {
3092
        date => sub { $_[0] . 'c' }
3093
    },
3094
    from1 => {
3095
        date => sub { $_[0] . 'd' }
3096
    },
3097
    from2 => {
3098
        date => sub { $_[0] . 'e' }
3099
    }
3100
);
3101
$dbi->insert({key1 => '1'}, table => 'table1', type_rule_off => 1);
3102
$result = $dbi->select(table => 'table1');
3103
is($result->type_rule_off->fetch_first->[0], '1');
3104
$result = $dbi->select(table => 'table1');
3105
is($result->type_rule_on->fetch_first->[0], '1de');
3106

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

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

            
3151
test 'separator';
cleanup test
Yuki Kimoto authored on 2011-08-06
3152
$dbi = DBIx::Custom->connect(%memory);
3153
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
3154
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
3155

            
3156
$dbi->create_model(
3157
    table => 'table1',
3158
    join => [
3159
       'left outer join table2 on table1.key1 = table2.key1'
3160
    ],
3161
    primary_key => ['key1'],
3162
);
3163
$model2 = $dbi->create_model(
3164
    table => 'table2',
3165
);
3166
$dbi->setup_model;
3167
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3168
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
3169
$model = $dbi->model('table1');
3170
$result = $model->select(
3171
    column => [
3172
        $model->mycolumn,
3173
        {table2 => [qw/key1 key3/]}
3174
    ],
3175
    where => {'table1.key1' => 1}
3176
);
3177
is_deeply($result->one,
3178
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
3179
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3180

            
3181
$dbi->separator('__');
3182
$model = $dbi->model('table1');
3183
$result = $model->select(
3184
    column => [
3185
        $model->mycolumn,
3186
        {table2 => [qw/key1 key3/]}
3187
    ],
3188
    where => {'table1.key1' => 1}
3189
);
3190
is_deeply($result->one,
3191
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
3192
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3193

            
3194
$dbi->separator('-');
3195
$model = $dbi->model('table1');
3196
$result = $model->select(
3197
    column => [
3198
        $model->mycolumn,
3199
        {table2 => [qw/key1 key3/]}
3200
    ],
3201
    where => {'table1.key1' => 1}
3202
);
3203
is_deeply($result->one,
3204
          {key1 => 1, key2 => 2, 'table2-key1' => 1, 'table2-key3' => 3});
3205
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3206

            
3207

            
3208
test 'filter_off';
cleanup test
Yuki Kimoto authored on 2011-08-06
3209
$dbi = DBIx::Custom->connect(%memory);
3210
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
3211
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
3212

            
3213
$dbi->create_model(
3214
    table => 'table1',
3215
    join => [
3216
       'left outer join table2 on table1.key1 = table2.key1'
3217
    ],
3218
    primary_key => ['key1'],
3219
);
3220
$dbi->setup_model;
3221
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3222
$model = $dbi->model('table1');
3223
$result = $model->select(column => 'key1');
3224
$result->filter(key1 => sub { $_[0] * 2 });
3225
is_deeply($result->one, {key1 => 2});
3226

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

            
3231

            
3232
test 'select prefix option';
cleanup test
Yuki Kimoto authored on 2011-08-06
3233
$dbi = DBIx::Custom->connect(%memory);
3234
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
3235
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3236
$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all;
3237
is_deeply($rows, [{key1 => 1, key2 => 2}], "table");
3238

            
3239

            
3240
test 'separator';
cleanup test
Yuki Kimoto authored on 2011-08-06
3241
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3242
is($dbi->separator, '.');
3243
$dbi->separator('-');
3244
is($dbi->separator, '-');
3245
$dbi->separator('__');
3246
is($dbi->separator, '__');
3247
eval { $dbi->separator('?') };
3248
like($@, qr/Separator/);
3249

            
3250

            
3251
test 'map_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
3252
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3253
$param = $dbi->map_param(
3254
    {id => 1, author => 'Ken', price => 1900},
3255
    id => 'book.id',
3256
    author => ['book.author', sub { '%' . $_[0] . '%' }],
3257
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
3258
);
3259
is_deeply($param, {'book.id' => 1, 'book.author' => '%Ken%',
3260
  'book.price' => 1900});
3261

            
3262
$param = $dbi->map_param(
3263
    {id => 0, author => 0, price => 0},
3264
    id => 'book.id',
3265
    author => ['book.author', sub { '%' . $_[0] . '%' }],
3266
    price => ['book.price', sub { '%' . $_[0] . '%' },
3267
      {if => sub { $_[0] eq 0 }}]
3268
);
3269
is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
3270

            
3271
$param = $dbi->map_param(
3272
    {id => '', author => '', price => ''},
3273
    id => 'book.id',
3274
    author => ['book.author', sub { '%' . $_[0] . '%' }],
3275
    price => ['book.price', sub { '%' . $_[0] . '%' },
3276
      {if => sub { $_[0] eq 1 }}]
3277
);
3278
is_deeply($param, {});
3279

            
3280
$param = $dbi->map_param(
3281
    {id => undef, author => undef, price => undef},
3282
    id => 'book.id',
3283
    price => ['book.price', {if => 'exists'}]
3284
);
3285
is_deeply($param, {'book.price' => undef});
3286

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

            
3294

            
3295
test 'table_alias';
cleanup test
Yuki Kimoto authored on 2011-08-06
3296
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3297
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3298
$dbi->type_rule(
3299
    into1 => {
3300
        date => sub { uc $_[0] }
3301
    }
3302
);
3303
$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'},
3304
  table_alias => {table2 => 'table1'});
3305
$result = $dbi->select(table => 'table1');
3306
is($result->one->{key1}, 'A');
3307

            
3308

            
3309
test 'order';
cleanup test
Yuki Kimoto authored on 2011-08-06
3310
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3311
$dbi->execute("create table table1 (key1, key2)");
3312
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
3313
$dbi->insert({key1 => 1, key2 => 3}, table => 'table1');
3314
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
3315
$dbi->insert({key1 => 2, key2 => 4}, table => 'table1');
3316
my $order = $dbi->order;
3317
$order->prepend('key1', 'key2 desc');
3318
$result = $dbi->select(table => 'table1', append => "$order");
3319
is_deeply($result->all, [{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1},
3320
  {key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}]);
3321
$order->prepend('key1 desc');
3322
$result = $dbi->select(table => 'table1', append => "$order");
3323
is_deeply($result->all, [{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2},
3324
  {key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}]);
3325

            
3326
$order = $dbi->order;
3327
$order->prepend(['table1-key1'], [qw/table1-key2 desc/]);
3328
$result = $dbi->select(table => 'table1',
3329
  column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']],
3330
  append => "$order");
3331
is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3},
3332
  {'table1-key1' => 1, 'table1-key2' => 1},
3333
  {'table1-key1' => 2, 'table1-key2' => 4},
3334
  {'table1-key1' => 2, 'table1-key2' => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
3335

            
3336
test 'tag_parse';
cleanup test
Yuki Kimoto authored on 2011-08-06
3337
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3338
$dbi->tag_parse(0);
cleanup test
Yuki Kimoto authored on 2011-08-06
3339
$dbi->execute("create table table1 (key1, key2)");
3340
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
3341
eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})};
3342
ok($@);
cleanup test
Yuki Kimoto authored on 2011-08-06
3343

            
3344
test 'last_sql';
cleanup test
Yuki Kimoto authored on 2011-08-06
3345
$dbi = DBIx::Custom->connect(%memory);
3346
$dbi->execute("create table table1 (key1, key2)");
3347
$dbi->execute('select * from table1');
3348
is($dbi->last_sql, 'select * from table1;');
3349

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

            
3353
test 'DBIx::Custom header';
cleanup test
Yuki Kimoto authored on 2011-08-06
3354
$dbi = DBIx::Custom->connect(%memory);
3355
$dbi->execute("create table table1 (key1, key2)");
3356
$result = $dbi->execute('select key1 as h1, key2 as h2 from table1');
3357
is_deeply($result->header, [qw/h1 h2/]);
cleanup test
Yuki Kimoto authored on 2011-08-06
3358

            
3359
test 'Named placeholder :name(operater) syntax';
cleanup test
Yuki Kimoto authored on 2011-08-06
3360
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
3361
$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
3362
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3363
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3364

            
3365
$source = "select * from table1 where :key1{=} and :key2{=}";
3366
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
3367
$rows = $result->all;
3368
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3369

            
3370
$source = "select * from table1 where :key1{ = } and :key2{=}";
3371
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
3372
$rows = $result->all;
3373
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3374

            
3375
$source = "select * from table1 where :key1{<} and :key2{=}";
3376
$result = $dbi->execute($source, param => {key1 => 5, key2 => 2});
3377
$rows = $result->all;
3378
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3379

            
3380
$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}";
3381
$result = $dbi->execute(
3382
    $source,
3383
    param => {'table1.key1' => 1, 'table1.key2' => 1},
3384
    filter => {'table1.key2' => sub { $_[0] * 2 }}
3385
);
3386
$rows = $result->all;
3387
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3388

            
3389
test 'high perfomance way';
cleanup test
Yuki Kimoto authored on 2011-08-06
3390
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
3391
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
3392
$rows = [
3393
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3394
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3395
];
3396
{
3397
    my $query;
3398
    foreach my $row (@$rows) {
3399
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
3400
      $dbi->execute($query, $row, filter => {ab => sub { $_[0] * 2 }});
3401
    }
3402
    is_deeply($dbi->select(table => 'table1')->all,
3403
      [
3404
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3405
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3406
      ]
3407
    );
3408
}
3409

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

            
clenup test
Yuki Kimoto authored on 2011-08-06
3432
test 'result';
3433
$dbi = DBIx::Custom->connect(%memory);
3434
$dbi->execute($create_table_default);
3435
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3436
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3437

            
3438
$result = $dbi->select(table => 'table1');
3439
@rows = ();
3440
while (my $row = $result->fetch) {
3441
    push @rows, [@$row];
3442
}
3443
is_deeply(\@rows, [[1, 2], [3, 4]]);
3444

            
3445
$result = $dbi->select(table => 'table1');
3446
@rows = ();
3447
while (my $row = $result->fetch_hash) {
3448
    push @rows, {%$row};
3449
}
3450
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
3451

            
3452
$result = $dbi->select(table => 'table1');
3453
$row = $result->fetch_first;
3454
is_deeply($row, [1, 2], "row");
3455
$row = $result->fetch;
3456
ok(!$row, "finished");
3457

            
3458
$result = $dbi->select(table => 'table1');
3459
$row = $result->fetch_hash_first;
3460
is_deeply($row, {key1 => 1, key2 => 2}, "row");
3461
$row = $result->fetch_hash;
3462
ok(!$row, "finished");
3463

            
3464
$dbi->execute('create table table2 (key1, key2);');
3465
$result = $dbi->select(table => 'table2');
3466
$row = $result->fetch_hash_first;
3467
ok(!$row, "no row fetch");
3468

            
3469
$dbi = DBIx::Custom->connect(%memory);
3470
$dbi->execute($create_table_default);
3471
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3472
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3473
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1');
3474
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1');
3475
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1');
3476
$result = $dbi->select(table => 'table1');
3477
$rows = $result->fetch_multi(2);
3478
is_deeply($rows, [[1, 2],
3479
                  [3, 4]], "fetch_multi first");
3480
$rows = $result->fetch_multi(2);
3481
is_deeply($rows, [[5, 6],
3482
                  [7, 8]], "fetch_multi secound");
3483
$rows = $result->fetch_multi(2);
3484
is_deeply($rows, [[9, 10]], "fetch_multi third");
3485
$rows = $result->fetch_multi(2);
3486
ok(!$rows);
3487

            
3488
$result = $dbi->select(table => 'table1');
3489
eval {$result->fetch_multi};
3490
like($@, qr/Row count must be specified/, "Not specified row count");
3491

            
3492
$result = $dbi->select(table => 'table1');
3493
$rows = $result->fetch_hash_multi(2);
3494
is_deeply($rows, [{key1 => 1, key2 => 2},
3495
                  {key1 => 3, key2 => 4}], "fetch_multi first");
3496
$rows = $result->fetch_hash_multi(2);
3497
is_deeply($rows, [{key1 => 5, key2 => 6},
3498
                  {key1 => 7, key2 => 8}], "fetch_multi secound");
3499
$rows = $result->fetch_hash_multi(2);
3500
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third");
3501
$rows = $result->fetch_hash_multi(2);
3502
ok(!$rows);
3503

            
3504
$result = $dbi->select(table => 'table1');
3505
eval {$result->fetch_hash_multi};
3506
like($@, qr/Row count must be specified/, "Not specified row count");
3507

            
3508
$dbi = DBIx::Custom->connect(%memory);
3509
$dbi->execute($create_table_default);
3510
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3511
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3512

            
3513
test 'fetch_all';
3514
$result = $dbi->select(table => 'table1');
3515
$rows = $result->fetch_all;
3516
is_deeply($rows, [[1, 2], [3, 4]]);
3517

            
3518
$result = $dbi->select(table => 'table1');
3519
$rows = $result->fetch_hash_all;
3520
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
3521

            
3522
$result = $dbi->select(table => 'table1');
3523
$result->dbi->filters({three_times => sub { $_[0] * 3}});
3524
$result->filter({key1 => 'three_times'});
3525

            
3526
$rows = $result->fetch_all;
3527
is_deeply($rows, [[3, 2], [9, 4]], "array");
3528

            
3529
$result = $dbi->select(table => 'table1');
3530
$result->dbi->filters({three_times => sub { $_[0] * 3}});
3531
$result->filter({key1 => 'three_times'});
3532
$rows = $result->fetch_hash_all;
3533
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash");
3534

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3535
test "query_builder";
3536
$datas = [
3537
    # Basic tests
3538
    {   name            => 'placeholder basic',
3539
        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
3540
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;",
3541
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
3542
    },
3543
    {
3544
        name            => 'placeholder in',
3545
        source            => "{in k1 3};",
3546
        sql_expected    => "k1 in (?, ?, ?);",
3547
        columns_expected   => [qw/k1 k1 k1/]
3548
    },
3549
    
3550
    # Table name
3551
    {
3552
        name            => 'placeholder with table name',
3553
        source            => "{= a.k1} {= a.k2}",
3554
        sql_expected    => "a.k1 = ? a.k2 = ?;",
3555
        columns_expected  => [qw/a.k1 a.k2/]
3556
    },
3557
    {   
3558
        name            => 'placeholder in with table name',
3559
        source            => "{in a.k1 2} {in b.k2 2}",
3560
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?);",
3561
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
3562
    },
3563
    {
3564
        name            => 'not contain tag',
3565
        source            => "aaa",
3566
        sql_expected    => "aaa;",
3567
        columns_expected  => [],
3568
    }
3569
];
3570

            
3571
for (my $i = 0; $i < @$datas; $i++) {
3572
    my $data = $datas->[$i];
3573
    my $builder = DBIx::Custom->new->query_builder;
3574
    my $query = $builder->build_query($data->{source});
3575
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
3576
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
3577
}
3578

            
3579
$builder = DBIx::Custom->new->query_builder;
3580
$ret_val = $builder->register_tag(
3581
    p => sub {
3582
        my @args = @_;
3583
        
3584
        my $expand    = "? $args[0] $args[1]";
3585
        my $columns = [2];
3586
        return [$expand, $columns];
3587
    }
3588
);
3589

            
3590
$query = $builder->build_query("{p a b}");
3591
is($query->{sql}, "? a b;", "register_tag sql");
3592
is_deeply($query->{columns}, [2], "register_tag columns");
3593
isa_ok($ret_val, 'DBIx::Custom::QueryBuilder');
3594

            
3595
$builder = DBIx::Custom->new->query_builder;
3596

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

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

            
3603
$builder->register_tag({
3604
    q => 'string'
3605
});
3606

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

            
3610
$builder->register_tag({
3611
   r => sub {} 
3612
});
3613

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

            
3617
$builder->register_tag({
3618
   s => sub { return ["a", ""]} 
3619
});
3620

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

            
3624
$builder->register_tag(
3625
    t => sub {return ["a", []]}
3626
);
3627

            
3628

            
3629
test 'General error case';
3630
$builder = DBIx::Custom->new->query_builder;
3631
$builder->register_tag(
3632
    a => sub {
3633
        return ["? ? ?", ['']];
3634
    }
3635
);
3636
eval{$builder->build_query("{a}")};
3637
like($@, qr/\QPlaceholder count/, "placeholder count is invalid");
3638

            
3639

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

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

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

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

            
3655
test 'variouse source';
3656
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;";
3657
$query = $builder->build_query($source);
3658
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1");
3659

            
3660
$source = "abc;";
3661
$query = $builder->build_query($source);
3662
is($query->sql, 'abc;', "basic : 2");
3663

            
3664
$source = "{= a}";
3665
$query = $builder->build_query($source);
3666
is($query->sql, 'a = ?;', "only tag");
3667

            
3668
$source = "000;";
3669
$query = $builder->build_query($source);
3670
is($query->sql, '000;', "contain 0 value");
3671

            
3672
$source = "a {= b} }";
3673
eval{$builder->build_query($source)};
3674
like($@, qr/unexpected "}"/, "error : 1");
3675

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