DBIx-Custom / t / sqlite.t /
Newer Older
3709 lines | 119.997kb
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

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

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

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

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

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

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

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

            
1400

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

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

            
1409
$dbi->apply_filter(
1410

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1537
{
1538
    package MyDBI4;
1539

            
1540
    use strict;
1541
    use warnings;
1542

            
1543
    use base 'DBIx::Custom';
1544

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

            
1556
    package MyModel2::Base1;
1557

            
1558
    use strict;
1559
    use warnings;
1560

            
1561
    use base 'DBIx::Custom::Model';
1562

            
1563
    package MyModel2::book;
1564

            
1565
    use strict;
1566
    use warnings;
1567

            
1568
    use base 'MyModel2::Base1';
1569

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

            
1576
    sub list { shift->select; }
1577

            
1578
    package MyModel2::Company;
1579

            
1580
    use strict;
1581
    use warnings;
1582

            
1583
    use base 'MyModel2::Base1';
1584

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

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

            
1603
{
1604
     package MyDBI5;
1605

            
1606
    use strict;
1607
    use warnings;
1608

            
1609
    use base 'DBIx::Custom';
1610

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1837

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

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

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

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

            
1903

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

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

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

            
1953

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

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

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

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

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

            
1994

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

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

            
2014

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

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

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

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

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

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

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

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

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

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2132
$dbi = DBIx::Custom->connect(%memory);
2133
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2134
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2135
$sql = <<"EOS";
cleanup test
Yuki Kimoto authored on 2011-08-06
2136
left outer join (
2137
  select * from table1 as t1
2138
  where t1.key2 = (
2139
    select max(t2.key2) from table1 as t2
2140
    where t1.key1 = t2.key1
2141
  )
2142
) as latest_table1 on table1.key1 = latest_table1.key1
2143
EOS
cleanup test
Yuki Kimoto authored on 2011-08-06
2144
$join = [$sql];
2145
$rows = $dbi->select(
2146
    table => 'table1',
2147
    column => 'latest_table1.key1 as latest_table1__key1',
2148
    join  => $join
2149
)->all;
2150
is_deeply($rows, [{latest_table1__key1 => 1}]);
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
    join => [
2161
        "left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1"
2162
    ]
2163
);
2164
is_deeply($result->all, [{key1 => 1, key2 => 2}]);
2165
$result = $dbi->select(
2166
    table => 'table1',
2167
    column => [{table2 => ['key3']}],
2168
    join => [
2169
        "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1"
2170
    ]
2171
);
2172
is_deeply($result->all, [{'table2.key3' => 4}]);
2173
$result = $dbi->select(
2174
    table => 'table1',
2175
    column => [{table2 => ['key3']}],
2176
    join => [
2177
        "left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'"
2178
    ]
2179
);
2180
is_deeply($result->all, [{'table2.key3' => 4}]);
2181

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

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

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

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

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

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

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

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

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

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

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

            
2334

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

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

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

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

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

            
2411
test 'model method';
2412
test 'create_model';
cleanup test
Yuki Kimoto authored on 2011-08-06
2413
$dbi = DBIx::Custom->connect(%memory);
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 => 3});
2416
$model = $dbi->create_model(
2417
    table => 'table2'
2418
);
2419
$model->method(foo => sub { shift->select(@_) });
2420
is_deeply($model->foo->one, {key1 => 1, key3 => 3});
2421

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

            
2432
$params = [
2433
    {key1 => [1, 2], key2 => 1, key3 => [1, 2]},
2434
    {key1 => [3, 4], key2 => [2, 3], key3 => 3}
2435
];
2436
$param = $dbi->merge_param($params->[0], $params->[1]);
2437
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
2438

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

            
2457

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

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

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

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

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

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

            
2531

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

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

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

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

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

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

            
2596

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

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

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

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

            
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->update(
2652
    {key3 => 4},
2653
    table => 'table1',
2654
    primary_key => ['key1', 'key2'],
2655
    id => [1, 2]
2656
);
2657
is($dbi->select(table => 'table1')->one->{key1}, 1);
2658
is($dbi->select(table => 'table1')->one->{key2}, 2);
2659
is($dbi->select(table => 'table1')->one->{key3}, 4);
2660

            
2661

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

            
2676

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

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

            
2696

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

            
2712

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

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

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

            
2751

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

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

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

            
2784

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

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

            
2800

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
3237

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

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

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

            
3261

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

            
3269

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

            
3280

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

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

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

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

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

            
3324

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

            
3338

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

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

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

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

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

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

            
3389
test 'Named placeholder :name(operater) syntax';
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 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
3392
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3393
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3394

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
3538
$dbi = DBIx::Custom->connect(%memory);
3539
$dbi->execute($create_table_default);
3540
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3541
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3542

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

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

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

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

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

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

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

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

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

            
3625
$builder = DBIx::Custom->new->query_builder;
3626

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

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

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

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

            
3640
$builder->register_tag({
3641
   r => sub {} 
3642
});
3643

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

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

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

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

            
3658

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

            
3669

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

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

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

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

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

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

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

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

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

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