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

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

            
1293
test 'dbi_option default';
1294
$dbi = DBIx::Custom->new;
1295
is_deeply($dbi->dbi_option, {});
1296

            
1297
test 'register_tag_processor';
cleanup test
Yuki Kimoto authored on 2011-08-06
1298
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1299
$dbi->register_tag_processor(
1300
    a => sub { 1 }
1301
);
1302
is($dbi->query_builder->tag_processors->{a}->(), 1);
1303

            
1304
test 'register_tag';
cleanup test
Yuki Kimoto authored on 2011-08-06
1305
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1306
$dbi->register_tag(
1307
    b => sub { 2 }
1308
);
1309
is($dbi->query_builder->tags->{b}->(), 2);
1310

            
1311
test 'table not specify exception';
cleanup test
Yuki Kimoto authored on 2011-08-06
1312
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1313
eval {$dbi->insert};
1314
like($@, qr/table/);
1315
eval {$dbi->update};
1316
like($@, qr/table/);
1317
eval {$dbi->delete};
1318
like($@, qr/table/);
1319
eval {$dbi->select};
1320
like($@, qr/table/);
1321

            
1322

            
1323
test 'more tests';
cleanup test
Yuki Kimoto authored on 2011-08-06
1324
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1325
eval{$dbi->apply_filter('table', 'column', [])};
1326
like($@, qr/apply_filter/);
1327

            
1328
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
1329
like($@, qr/apply_filter/);
1330

            
1331
$dbi->apply_filter(
1332

            
1333
);
cleanup test
Yuki Kimoto authored on 2011-08-06
1334
$dbi = DBIx::Custom->connect(%memory);
1335
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
1336
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1337
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1338
$dbi->apply_filter('table1', 'key2', 
1339
                   {in => sub { $_[0] * 3 }, out => sub { $_[0] * 2 }});
1340
$rows = $dbi->select(table => 'table1', where => {key2 => 1})->all;
1341
is_deeply($rows, [{key1 => 1, key2 => 6}]);
1342

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1343
$dbi = DBIx::Custom->connect(%memory);
1344
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
1345
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1346
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1347
$dbi->apply_filter('table1', 'key2', {});
1348
$rows = $dbi->select(table => 'table1', where => {key2 => 2})->all;
1349
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1350

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1351
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1352
eval {$dbi->apply_filter('table1', 'key2', {out => 'no'})};
1353
like($@, qr/not registered/);
1354
eval {$dbi->apply_filter('table1', 'key2', {in => 'no'})};
1355
like($@, qr/not registered/);
1356
$dbi->method({one => sub { 1 }});
1357
is($dbi->one, 1);
1358

            
1359
eval{DBIx::Custom->connect()};
1360
like($@, qr/_connect/);
1361

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1362
$dbi = DBIx::Custom->connect(%memory);
1363
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
1364
$dbi->register_filter(twice => sub { $_[0] * 2 });
1365
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
1366
             filter => {key1 => 'twice'});
1367
$row = $dbi->select(table => 'table1')->one;
1368
is_deeply($row, {key1 => 2, key2 => 2});
1369
eval {$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
1370
             filter => {key1 => 'no'}) };
1371
like($@, qr//);
1372

            
1373
$dbi->register_filter(one => sub { });
1374
$dbi->default_fetch_filter('one');
1375
ok($dbi->default_fetch_filter);
1376
$dbi->default_bind_filter('one');
1377
ok($dbi->default_bind_filter);
1378
eval{$dbi->default_fetch_filter('no')};
1379
like($@, qr/not registered/);
1380
eval{$dbi->default_bind_filter('no')};
1381
like($@, qr/not registered/);
1382
$dbi->default_bind_filter(undef);
1383
ok(!defined $dbi->default_bind_filter);
1384
$dbi->default_fetch_filter(undef);
1385
ok(!defined $dbi->default_fetch_filter);
1386
eval {$dbi->execute('select * from table1 {} {= author') };
1387
like($@, qr/Tag not finished/);
1388

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1389
$dbi = DBIx::Custom->connect(%memory);
1390
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
1391
$dbi->register_filter(one => sub { 1 });
1392
$result = $dbi->select(table => 'table1');
1393
eval {$result->filter(key1 => 'no')};
1394
like($@, qr/not registered/);
1395
eval {$result->end_filter(key1 => 'no')};
1396
like($@, qr/not registered/);
1397
$result->default_filter(undef);
1398
ok(!defined $result->default_filter);
1399
$result->default_filter('one');
1400
is($result->default_filter->(), 1);
1401

            
1402
test 'dbi_option';
cleanup test
Yuki Kimoto authored on 2011-08-06
1403
$dbi = DBIx::Custom->connect(%memory,
cleanup test
Yuki Kimoto authored on 2011-08-06
1404
                             dbi_option => {PrintError => 1});
1405
ok($dbi->dbh->{PrintError});
cleanup test
Yuki Kimoto authored on 2011-08-06
1406
$dbi = DBIx::Custom->connect(%memory,
cleanup test
Yuki Kimoto authored on 2011-08-06
1407
                             dbi_options => {PrintError => 1});
1408
ok($dbi->dbh->{PrintError});
1409

            
1410
test 'DBIx::Custom::Result stash()';
1411
$result = DBIx::Custom::Result->new;
1412
is_deeply($result->stash, {}, 'default');
1413
$result->stash->{foo} = 1;
1414
is($result->stash->{foo}, 1, 'get and set');
1415

            
1416
test 'filter __ expression';
cleanup test
Yuki Kimoto authored on 2011-08-06
1417
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1418
$dbi->execute('create table company (id, name, location_id)');
1419
$dbi->execute('create table location (id, name)');
1420
$dbi->apply_filter('location',
1421
  name => {in => sub { uc $_[0] } }
1422
);
1423

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

            
1427
$result = $dbi->select(
1428
    table => ['company', 'location'], relation => {'company.location_id' => 'location.id'},
1429
    column => ['location.name as location__name']
1430
);
1431
is($result->fetch_first->[0], 'B');
1432

            
1433
$result = $dbi->select(
1434
    table => 'company', relation => {'company.location_id' => 'location.id'},
1435
    column => ['location.name as location__name']
1436
);
1437
is($result->fetch_first->[0], 'B');
1438

            
1439
$result = $dbi->select(
1440
    table => 'company', relation => {'company.location_id' => 'location.id'},
1441
    column => ['location.name as "location.name"']
1442
);
1443
is($result->fetch_first->[0], 'B');
1444

            
1445
test 'Model class';
1446
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1447
$dbi = MyDBI1->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1448
$dbi->execute("create table book (title, author)");
1449
$model = $dbi->model('book');
1450
$model->insert({title => 'a', author => 'b'});
1451
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
1452
$dbi->execute("create table company (name)");
1453
$model = $dbi->model('company');
1454
$model->insert({name => 'a'});
1455
is_deeply($model->list->all, [{name => 'a'}], 'basic');
1456
is($dbi->models->{'book'}, $dbi->model('book'));
1457
is($dbi->models->{'company'}, $dbi->model('company'));
1458

            
1459
{
1460
    package MyDBI4;
1461

            
1462
    use strict;
1463
    use warnings;
1464

            
1465
    use base 'DBIx::Custom';
1466

            
1467
    sub connect {
1468
        my $self = shift->SUPER::connect(@_);
1469
        
1470
        $self->include_model(
1471
            MyModel2 => [
1472
                'book',
1473
                {class => 'Company', name => 'company'}
1474
            ]
1475
        );
1476
    }
1477

            
1478
    package MyModel2::Base1;
1479

            
1480
    use strict;
1481
    use warnings;
1482

            
1483
    use base 'DBIx::Custom::Model';
1484

            
1485
    package MyModel2::book;
1486

            
1487
    use strict;
1488
    use warnings;
1489

            
1490
    use base 'MyModel2::Base1';
1491

            
1492
    sub insert {
1493
        my ($self, $param) = @_;
1494
        
1495
        return $self->SUPER::insert(param => $param);
1496
    }
1497

            
1498
    sub list { shift->select; }
1499

            
1500
    package MyModel2::Company;
1501

            
1502
    use strict;
1503
    use warnings;
1504

            
1505
    use base 'MyModel2::Base1';
1506

            
1507
    sub insert {
1508
        my ($self, $param) = @_;
1509
        
1510
        return $self->SUPER::insert(param => $param);
1511
    }
1512

            
1513
    sub list { shift->select; }
1514
}
cleanup test
Yuki Kimoto authored on 2011-08-06
1515
$dbi = MyDBI4->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1516
$dbi->execute("create table book (title, author)");
1517
$model = $dbi->model('book');
1518
$model->insert({title => 'a', author => 'b'});
1519
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
1520
$dbi->execute("create table company (name)");
1521
$model = $dbi->model('company');
1522
$model->insert({name => 'a'});
1523
is_deeply($model->list->all, [{name => 'a'}], 'basic');
1524

            
1525
{
1526
     package MyDBI5;
1527

            
1528
    use strict;
1529
    use warnings;
1530

            
1531
    use base 'DBIx::Custom';
1532

            
1533
    sub connect {
1534
        my $self = shift->SUPER::connect(@_);
1535
        
1536
        $self->include_model('MyModel4');
1537
    }
1538
}
cleanup test
Yuki Kimoto authored on 2011-08-06
1539
$dbi = MyDBI5->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1540
$dbi->execute("create table company (name)");
1541
$dbi->execute("create table table1 (key1)");
1542
$model = $dbi->model('company');
1543
$model->insert({name => 'a'});
1544
is_deeply($model->list->all, [{name => 'a'}], 'include all model');
1545
$dbi->insert(table => 'table1', param => {key1 => 1});
1546
$model = $dbi->model('book');
1547
is_deeply($model->list->all, [{key1 => 1}], 'include all model');
1548

            
1549
test 'primary_key';
1550
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1551
$dbi = MyDBI1->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1552
$model = $dbi->model('book');
1553
$model->primary_key(['id', 'number']);
1554
is_deeply($model->primary_key, ['id', 'number']);
1555

            
1556
test 'columns';
1557
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1558
$dbi = MyDBI1->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1559
$model = $dbi->model('book');
1560
$model->columns(['id', 'number']);
1561
is_deeply($model->columns, ['id', 'number']);
1562

            
1563
test 'setup_model';
1564
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1565
$dbi = MyDBI1->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1566
$dbi->execute('create table book (id)');
1567
$dbi->execute('create table company (id, name);');
1568
$dbi->execute('create table test (id, name, primary key (id, name));');
1569
$dbi->setup_model;
1570
is_deeply($dbi->model('book')->columns, ['id']);
1571
is_deeply($dbi->model('company')->columns, ['id', 'name']);
1572

            
1573
test 'delete_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1574
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1575
$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
1576
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1577
$dbi->delete_at(
1578
    table => 'table1',
1579
    primary_key => ['key1', 'key2'],
1580
    where => [1, 2],
1581
);
1582
is_deeply($dbi->select(table => 'table1')->all, []);
1583

            
1584
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1585
$dbi->delete_at(
1586
    table => 'table1',
1587
    primary_key => 'key1',
1588
    where => 1,
1589
);
1590
is_deeply($dbi->select(table => 'table1')->all, []);
1591

            
1592
test 'insert_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1593
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1594
$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
1595
$dbi->insert_at(
1596
    primary_key => ['key1', 'key2'], 
1597
    table => 'table1',
1598
    where => [1, 2],
1599
    param => {key3 => 3}
1600
);
1601
is($dbi->select(table => 'table1')->one->{key1}, 1);
1602
is($dbi->select(table => 'table1')->one->{key2}, 2);
1603
is($dbi->select(table => 'table1')->one->{key3}, 3);
1604

            
1605
$dbi->delete_all(table => 'table1');
1606
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1607
$dbi->insert_at(
1608
    primary_key => 'key1', 
1609
    table => 'table1',
1610
    where => 1,
1611
    param => {key2 => 2, key3 => 3}
1612
);
1613

            
1614
is($dbi->select(table => 'table1')->one->{key1}, 1);
1615
is($dbi->select(table => 'table1')->one->{key2}, 2);
1616
is($dbi->select(table => 'table1')->one->{key3}, 3);
1617

            
1618
eval {
1619
    $dbi->insert_at(
1620
        table => 'table1',
1621
        primary_key => ['key1', 'key2'],
1622
        where => {},
1623
        param => {key1 => 1, key2 => 2, key3 => 3},
1624
    );
1625
};
1626
like($@, qr/must be/);
1627

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1628
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1629
$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
1630
$dbi->insert_at(
1631
    {key3 => 3},
1632
    primary_key => ['key1', 'key2'], 
1633
    table => 'table1',
1634
    where => [1, 2],
1635
);
1636
is($dbi->select(table => 'table1')->one->{key1}, 1);
1637
is($dbi->select(table => 'table1')->one->{key2}, 2);
1638
is($dbi->select(table => 'table1')->one->{key3}, 3);
1639

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

            
1654
$dbi->delete_all(table => 'table1');
1655
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1656
$dbi->update_at(
1657
    table => 'table1',
1658
    primary_key => 'key1',
1659
    where => 1,
1660
    param => {key3 => 4}
1661
);
1662
is($dbi->select(table => 'table1')->one->{key1}, 1);
1663
is($dbi->select(table => 'table1')->one->{key2}, 2);
1664
is($dbi->select(table => 'table1')->one->{key3}, 4);
1665

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

            
1679
test 'select_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1680
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1681
$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
1682
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1683
$result = $dbi->select_at(
1684
    table => 'table1',
1685
    primary_key => ['key1', 'key2'],
1686
    where => [1, 2]
1687
);
1688
$row = $result->one;
1689
is($row->{key1}, 1);
1690
is($row->{key2}, 2);
1691
is($row->{key3}, 3);
1692

            
1693
$dbi->delete_all(table => 'table1');
1694
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1695
$result = $dbi->select_at(
1696
    table => 'table1',
1697
    primary_key => 'key1',
1698
    where => 1,
1699
);
1700
$row = $result->one;
1701
is($row->{key1}, 1);
1702
is($row->{key2}, 2);
1703
is($row->{key3}, 3);
1704

            
1705
$dbi->delete_all(table => 'table1');
1706
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1707
$result = $dbi->select_at(
1708
    table => 'table1',
1709
    primary_key => ['key1', 'key2'],
1710
    where => [1, 2]
1711
);
1712
$row = $result->one;
1713
is($row->{key1}, 1);
1714
is($row->{key2}, 2);
1715
is($row->{key3}, 3);
1716

            
1717
eval {
1718
    $result = $dbi->select_at(
1719
        table => 'table1',
1720
        primary_key => ['key1', 'key2'],
1721
        where => {},
1722
    );
1723
};
1724
like($@, qr/must be/);
1725

            
1726
eval {
1727
    $result = $dbi->select_at(
1728
        table => 'table1',
1729
        primary_key => ['key1', 'key2'],
1730
        where => [1],
1731
    );
1732
};
1733
like($@, qr/same/);
1734

            
1735
eval {
1736
    $result = $dbi->update_at(
1737
        table => 'table1',
1738
        primary_key => ['key1', 'key2'],
1739
        where => {},
1740
        param => {key1 => 1, key2 => 2},
1741
    );
1742
};
1743
like($@, qr/must be/);
1744

            
1745
eval {
1746
    $result = $dbi->delete_at(
1747
        table => 'table1',
1748
        primary_key => ['key1', 'key2'],
1749
        where => {},
1750
    );
1751
};
1752
like($@, qr/must be/);
1753

            
1754
test 'columns';
1755
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1756
$dbi = MyDBI1->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1757
$model = $dbi->model('book');
1758

            
1759

            
1760
test 'model delete_at';
1761
{
1762
    package MyDBI6;
1763
    
1764
    use base 'DBIx::Custom';
1765
    
1766
    sub connect {
1767
        my $self = shift->SUPER::connect(@_);
1768
        
1769
        $self->include_model('MyModel5');
1770
        
1771
        return $self;
1772
    }
1773
}
cleanup test
Yuki Kimoto authored on 2011-08-06
1774
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1775
$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
1776
$dbi->execute("create table table2 (key1, key2, key3)");
1777
$dbi->execute("create table table3 (key1, key2, key3)");
1778
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1779
$dbi->model('table1')->delete_at(where => [1, 2]);
1780
is_deeply($dbi->select(table => 'table1')->all, []);
1781
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
1782
$dbi->model('table1_1')->delete_at(where => [1, 2]);
1783
is_deeply($dbi->select(table => 'table1')->all, []);
1784
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
1785
$dbi->model('table1_3')->delete_at(where => [1, 2]);
1786
is_deeply($dbi->select(table => 'table1')->all, []);
1787

            
1788
test 'model insert_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1789
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1790
$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
1791
$dbi->model('table1')->insert_at(
1792
    where => [1, 2],
1793
    param => {key3 => 3}
1794
);
1795
$result = $dbi->model('table1')->select;
1796
$row = $result->one;
1797
is($row->{key1}, 1);
1798
is($row->{key2}, 2);
1799
is($row->{key3}, 3);
1800

            
1801
test 'model update_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1802
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1803
$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
1804
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1805
$dbi->model('table1')->update_at(
1806
    where => [1, 2],
1807
    param => {key3 => 4}
1808
);
1809
$result = $dbi->model('table1')->select;
1810
$row = $result->one;
1811
is($row->{key1}, 1);
1812
is($row->{key2}, 2);
1813
is($row->{key3}, 4);
1814

            
1815
test 'model select_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1816
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1817
$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
1818
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1819
$result = $dbi->model('table1')->select_at(where => [1, 2]);
1820
$row = $result->one;
1821
is($row->{key1}, 1);
1822
is($row->{key2}, 2);
1823
is($row->{key3}, 3);
1824

            
1825

            
1826
test 'mycolumn and column';
1827
{
1828
    package MyDBI7;
1829
    
1830
    use base 'DBIx::Custom';
1831
    
1832
    sub connect {
1833
        my $self = shift->SUPER::connect(@_);
1834
        
1835
        $self->include_model('MyModel6');
1836
        
1837
        
1838
        return $self;
1839
    }
1840
}
cleanup test
Yuki Kimoto authored on 2011-08-06
1841
$dbi = MyDBI7->connect(%memory);
1842
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
1843
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1844
$dbi->separator('__');
1845
$dbi->setup_model;
1846
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1847
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1848
$model = $dbi->model('table1');
1849
$result = $model->select(
1850
    column => [$model->mycolumn, $model->column('table2')],
1851
    where => {'table1.key1' => 1}
1852
);
1853
is_deeply($result->one,
1854
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
1855

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

            
1862
$param = {key2 => 11};
1863
$update_param = $dbi->update_param($param);
1864
$sql = <<"EOS";
1865
update table1 $update_param
1866
where key1 = 1
1867
EOS
1868
$dbi->execute($sql, param => $param);
test cleanup
Yuki Kimoto authored on 2011-08-06
1869
$result = $dbi->execute('select * from table1;', table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
1870
$rows   = $result->all;
1871
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
1872
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1873
                  "basic");
1874

            
1875

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

            
1881
$param = {key2 => 11, key3 => 33};
1882
$update_param = $dbi->update_param($param);
1883
$sql = <<"EOS";
1884
update table1 $update_param
1885
where key1 = 1
1886
EOS
1887
$dbi->execute($sql, param => $param);
test cleanup
Yuki Kimoto authored on 2011-08-06
1888
$result = $dbi->execute('select * from table1;', table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
1889
$rows   = $result->all;
1890
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
1891
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1892
                  "basic");
1893

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1894
$dbi = DBIx::Custom->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, key4 => 4, key5 => 5});
1897
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1898

            
1899
$param = {key2 => 11, key3 => 33};
1900
$update_param = $dbi->update_param($param, {no_set => 1});
1901
$sql = <<"EOS";
1902
update table1 set $update_param
1903
where key1 = 1
1904
EOS
1905
$dbi->execute($sql, param => $param);
test cleanup
Yuki Kimoto authored on 2011-08-06
1906
$result = $dbi->execute('select * from table1;', table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
1907
$rows   = $result->all;
1908
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
1909
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1910
                  "update param no_set");
1911

            
1912
            
1913
eval { $dbi->update_param({";" => 1}) };
1914
like($@, qr/not safety/);
1915

            
1916

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

            
1923
$param = {key2 => 11};
1924
$update_param = $dbi->assign_param($param);
1925
$sql = <<"EOS";
1926
update table1 set $update_param
1927
where key1 = 1
1928
EOS
1929
$dbi->execute($sql, param => $param, table => 'table1');
test cleanup
Yuki Kimoto authored on 2011-08-06
1930
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
1931
$rows   = $result->all;
1932
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
1933
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1934
                  "basic");
1935

            
1936

            
1937
test 'insert_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
1938
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1939
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
1940
$param = {key1 => 1, key2 => 2};
1941
$insert_param = $dbi->insert_param($param);
1942
$sql = <<"EOS";
1943
insert into table1 $insert_param
1944
EOS
1945
$dbi->execute($sql, param => $param, table => 'table1');
1946
is($dbi->select(table => 'table1')->one->{key1}, 1);
1947
is($dbi->select(table => 'table1')->one->{key2}, 2);
1948

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1949
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1950
$dbi->quote('"');
1951
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
1952
$param = {key1 => 1, key2 => 2};
1953
$insert_param = $dbi->insert_param($param);
1954
$sql = <<"EOS";
1955
insert into table1 $insert_param
1956
EOS
1957
$dbi->execute($sql, param => $param, table => 'table1');
1958
is($dbi->select(table => 'table1')->one->{key1}, 1);
1959
is($dbi->select(table => 'table1')->one->{key2}, 2);
1960

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

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

            
1965
test 'join';
cleanup test
Yuki Kimoto authored on 2011-08-06
1966
$dbi = DBIx::Custom->connect(%memory);
1967
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
1968
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1969
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
cleanup test
Yuki Kimoto authored on 2011-08-06
1970
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1971
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
cleanup test
Yuki Kimoto authored on 2011-08-06
1972
$dbi->execute('create table table3 (key3 int, key4 int);');
cleanup test
Yuki Kimoto authored on 2011-08-06
1973
$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4});
1974
$rows = $dbi->select(
1975
    table => 'table1',
1976
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
1977
    where   => {'table1.key2' => 2},
1978
    join  => ['left outer join table2 on table1.key1 = table2.key1']
1979
)->all;
1980
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]);
1981

            
1982
$rows = $dbi->select(
1983
    table => 'table1',
1984
    where   => {'key1' => 1},
1985
    join  => ['left outer join table2 on table1.key1 = table2.key1']
1986
)->all;
1987
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1988

            
1989
eval {
1990
    $rows = $dbi->select(
1991
        table => 'table1',
1992
        column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
1993
        where   => {'table1.key2' => 2},
1994
        join  => {'table1.key1' => 'table2.key1'}
1995
    );
1996
};
1997
like ($@, qr/array/);
1998

            
1999
$rows = $dbi->select(
2000
    table => 'table1',
2001
    where   => {'key1' => 1},
2002
    join  => ['left outer join table2 on table1.key1 = table2.key1',
2003
              'left outer join table3 on table2.key3 = table3.key3']
2004
)->all;
2005
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2006

            
2007
$rows = $dbi->select(
2008
    column => 'table3.key4 as table3__key4',
2009
    table => 'table1',
2010
    where   => {'table1.key1' => 1},
2011
    join  => ['left outer join table2 on table1.key1 = table2.key1',
2012
              'left outer join table3 on table2.key3 = table3.key3']
2013
)->all;
2014
is_deeply($rows, [{table3__key4 => 4}]);
2015

            
2016
$rows = $dbi->select(
2017
    column => 'table1.key1 as table1__key1',
2018
    table => 'table1',
2019
    where   => {'table3.key4' => 4},
2020
    join  => ['left outer join table2 on table1.key1 = table2.key1',
2021
              'left outer join table3 on table2.key3 = table3.key3']
2022
)->all;
2023
is_deeply($rows, [{table1__key1 => 1}]);
2024

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2025
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2026
$dbi->quote('"');
cleanup test
Yuki Kimoto authored on 2011-08-06
2027
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2028
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-06
2029
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2030
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
2031
$rows = $dbi->select(
2032
    table => 'table1',
2033
    column => '"table1"."key1" as "table1_key1", "table2"."key1" as "table2_key1", "key2", "key3"',
2034
    where   => {'table1.key2' => 2},
2035
    join  => ['left outer join "table2" on "table1"."key1" = "table2"."key1"'],
2036
)->all;
2037
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}],
2038
          'quote');
2039

            
2040
{
2041
    package MyDBI8;
2042
    
2043
    use base 'DBIx::Custom';
2044
    
2045
    sub connect {
2046
        my $self = shift->SUPER::connect(@_);
2047
        
2048
        $self->include_model('MyModel7');
2049
        
2050
        return $self;
2051
    }
2052
}
cleanup test
Yuki Kimoto authored on 2011-08-06
2053

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2054
$dbi = DBIx::Custom->connect(%memory);
2055
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2056
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2057
$sql = <<"EOS";
cleanup test
Yuki Kimoto authored on 2011-08-06
2058
left outer join (
2059
  select * from table1 as t1
2060
  where t1.key2 = (
2061
    select max(t2.key2) from table1 as t2
2062
    where t1.key1 = t2.key1
2063
  )
2064
) as latest_table1 on table1.key1 = latest_table1.key1
2065
EOS
cleanup test
Yuki Kimoto authored on 2011-08-06
2066
$join = [$sql];
2067
$rows = $dbi->select(
2068
    table => 'table1',
2069
    column => 'latest_table1.key1 as latest_table1__key1',
2070
    join  => $join
2071
)->all;
2072
is_deeply($rows, [{latest_table1__key1 => 1}]);
2073

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2074
$dbi = DBIx::Custom->connect(%memory);
2075
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2076
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2077
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2078
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2079
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
2080
$result = $dbi->select(
2081
    table => 'table1',
2082
    join => [
2083
        "left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1"
2084
    ]
2085
);
2086
is_deeply($result->all, [{key1 => 1, key2 => 2}]);
2087
$result = $dbi->select(
2088
    table => 'table1',
2089
    column => [{table2 => ['key3']}],
2090
    join => [
2091
        "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1"
2092
    ]
2093
);
2094
is_deeply($result->all, [{'table2.key3' => 4}]);
2095
$result = $dbi->select(
2096
    table => 'table1',
2097
    column => [{table2 => ['key3']}],
2098
    join => [
2099
        "left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'"
2100
    ]
2101
);
2102
is_deeply($result->all, [{'table2.key3' => 4}]);
2103

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

            
2122
test 'mycolumn';
cleanup test
Yuki Kimoto authored on 2011-08-06
2123
$dbi = MyDBI8->connect(%memory);
2124
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2125
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2126
$dbi->setup_model;
2127
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2128
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2129
$model = $dbi->model('table1');
2130
$result = $model->select_at(
2131
    column => [
2132
        $model->mycolumn,
2133
        $model->column('table2')
2134
    ]
2135
);
2136
is_deeply($result->one,
2137
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
2138

            
2139
$result = $model->select_at(
2140
    column => [
2141
        $model->mycolumn(['key1']),
2142
        $model->column(table2 => ['key1'])
2143
    ]
2144
);
2145
is_deeply($result->one,
2146
          {key1 => 1, 'table2.key1' => 1});
2147
$result = $model->select_at(
2148
    column => [
2149
        $model->mycolumn(['key1']),
2150
        {table2 => ['key1']}
2151
    ]
2152
);
2153
is_deeply($result->one,
2154
          {key1 => 1, 'table2.key1' => 1});
2155

            
2156
$result = $model->select_at(
2157
    column => [
2158
        $model->mycolumn(['key1']),
2159
        ['table2.key1', as => 'table2.key1']
2160
    ]
2161
);
2162
is_deeply($result->one,
2163
          {key1 => 1, 'table2.key1' => 1});
2164

            
2165
$result = $model->select_at(
2166
    column => [
2167
        $model->mycolumn(['key1']),
2168
        ['table2.key1' => 'table2.key1']
2169
    ]
2170
);
2171
is_deeply($result->one,
2172
          {key1 => 1, 'table2.key1' => 1});
2173

            
2174
test 'dbi method from model';
2175
{
2176
    package MyDBI9;
2177
    
2178
    use base 'DBIx::Custom';
2179
    
2180
    sub connect {
2181
        my $self = shift->SUPER::connect(@_);
2182
        
2183
        $self->include_model('MyModel8')->setup_model;
2184
        
2185
        return $self;
2186
    }
2187
}
cleanup test
Yuki Kimoto authored on 2011-08-06
2188
$dbi = MyDBI9->connect(%memory);
2189
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2190
$model = $dbi->model('table1');
2191
eval{$model->execute('select * from table1')};
2192
ok(!$@);
2193

            
2194
test 'column table option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2195
$dbi = MyDBI9->connect(%memory);
2196
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2197
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2198
$dbi->setup_model;
2199
$dbi->execute('insert into table1 (key1, key2) values (1, 2);');
2200
$dbi->execute('insert into table2 (key1, key3) values (1, 4);');
2201
$model = $dbi->model('table1');
2202
$result = $model->select(
2203
    column => [
2204
        $model->column('table2', {alias => 'table2_alias'})
2205
    ],
2206
    where => {'table2_alias.key3' => 4}
2207
);
2208
is_deeply($result->one, 
2209
          {'table2_alias.key1' => 1, 'table2_alias.key3' => 4});
2210

            
2211
$dbi->separator('__');
2212
$result = $model->select(
2213
    column => [
2214
        $model->column('table2', {alias => 'table2_alias'})
2215
    ],
2216
    where => {'table2_alias.key3' => 4}
2217
);
2218
is_deeply($result->one, 
2219
          {'table2_alias__key1' => 1, 'table2_alias__key3' => 4});
2220

            
2221
$dbi->separator('-');
2222
$result = $model->select(
2223
    column => [
2224
        $model->column('table2', {alias => 'table2_alias'})
2225
    ],
2226
    where => {'table2_alias.key3' => 4}
2227
);
2228
is_deeply($result->one, 
2229
          {'table2_alias-key1' => 1, 'table2_alias-key3' => 4});
2230

            
2231
test 'type option'; # DEPRECATED!
2232
$dbi = DBIx::Custom->connect(
2233
    data_source => 'dbi:SQLite:dbname=:memory:',
2234
    dbi_option => {
2235
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
2236
    }
2237
);
2238
my $binary = pack("I3", 1, 2, 3);
2239
$dbi->execute('create table table1(key1, key2)');
2240
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
2241
$result = $dbi->select(table => 'table1');
2242
$row   = $result->one;
2243
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2244
$result = $dbi->execute('select length(key1) as key1_length from table1');
2245
$row = $result->one;
2246
is($row->{key1_length}, length $binary);
2247

            
2248
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [['key1'] => DBI::SQL_BLOB]);
2249
$result = $dbi->select(table => 'table1');
2250
$row   = $result->one;
2251
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2252
$result = $dbi->execute('select length(key1) as key1_length from table1');
2253
$row = $result->one;
2254
is($row->{key1_length}, length $binary);
2255

            
2256

            
2257
test 'bind_type option';
2258
$dbi = DBIx::Custom->connect(
2259
    data_source => 'dbi:SQLite:dbname=:memory:',
2260
    dbi_option => {
2261
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
2262
    }
2263
);
2264
$binary = pack("I3", 1, 2, 3);
2265
$dbi->execute('create table table1(key1, key2)');
2266
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, bind_type => [key1 => DBI::SQL_BLOB]);
2267
$result = $dbi->select(table => 'table1');
2268
$row   = $result->one;
2269
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2270
$result = $dbi->execute('select length(key1) as key1_length from table1');
2271
$row = $result->one;
2272
is($row->{key1_length}, length $binary);
2273

            
2274
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, bind_type => [['key1'] => DBI::SQL_BLOB]);
2275
$result = $dbi->select(table => 'table1');
2276
$row   = $result->one;
2277
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2278
$result = $dbi->execute('select length(key1) as key1_length from table1');
2279
$row = $result->one;
2280
is($row->{key1_length}, length $binary);
2281

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

            
2300
test 'create_model';
cleanup test
Yuki Kimoto authored on 2011-08-06
2301
$dbi = DBIx::Custom->connect(%memory);
2302
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2303
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2304

            
2305
$dbi->create_model(
2306
    table => 'table1',
2307
    join => [
2308
       'left outer join table2 on table1.key1 = table2.key1'
2309
    ],
2310
    primary_key => ['key1']
2311
);
2312
$model2 = $dbi->create_model(
2313
    table => 'table2'
2314
);
2315
$dbi->create_model(
2316
    table => 'table3',
2317
    filter => [
2318
        key1 => {in => sub { uc $_[0] }}
2319
    ]
2320
);
2321
$dbi->setup_model;
2322
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2323
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2324
$model = $dbi->model('table1');
2325
$result = $model->select(
2326
    column => [$model->mycolumn, $model->column('table2')],
2327
    where => {'table1.key1' => 1}
2328
);
2329
is_deeply($result->one,
2330
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
2331
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
2332

            
2333
test 'model method';
2334
test 'create_model';
cleanup test
Yuki Kimoto authored on 2011-08-06
2335
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2336
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2337
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2338
$model = $dbi->create_model(
2339
    table => 'table2'
2340
);
2341
$model->method(foo => sub { shift->select(@_) });
2342
is_deeply($model->foo->one, {key1 => 1, key3 => 3});
2343

            
2344
test 'merge_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
2345
$dbi = DBIx::Custom->new;
2346
$params = [
2347
    {key1 => 1, key2 => 2, key3 => 3},
2348
    {key1 => 1, key2 => 2},
2349
    {key1 => 1}
2350
];
2351
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]);
2352
is_deeply($param, {key1 => [1, 1, 1], key2 => [2, 2], key3 => 3});
2353

            
2354
$params = [
2355
    {key1 => [1, 2], key2 => 1, key3 => [1, 2]},
2356
    {key1 => [3, 4], key2 => [2, 3], key3 => 3}
2357
];
2358
$param = $dbi->merge_param($params->[0], $params->[1]);
2359
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
2360

            
2361
test 'select() param option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2362
$dbi = DBIx::Custom->connect(%memory);
2363
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2364
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2365
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
2366
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2367
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2368
$dbi->insert(table => 'table2', param => {key1 => 2, key3 => 5});
2369
$rows = $dbi->select(
2370
    table => 'table1',
2371
    column => 'table1.key1 as table1_key1, key2, key3',
2372
    where   => {'table1.key2' => 3},
2373
    join  => ['inner join (select * from table2 where {= table2.key3})' . 
2374
              ' as table2 on table1.key1 = table2.key1'],
2375
    param => {'table2.key3' => 5}
2376
)->all;
2377
is_deeply($rows, [{table1_key1 => 2, key2 => 3, key3 => 5}]);
2378

            
2379

            
2380
test 'select() wrap option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2381
$dbi = DBIx::Custom->connect(%memory);
2382
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2383
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2384
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2385
$rows = $dbi->select(
2386
    table => 'table1',
2387
    column => 'key1',
2388
    wrap => ['select * from (', ') as t where key1 = 1']
2389
)->all;
2390
is_deeply($rows, [{key1 => 1}]);
2391

            
2392
eval {
2393
$dbi->select(
2394
    table => 'table1',
2395
    column => 'key1',
2396
    wrap => 'select * from ('
2397
)
2398
};
2399
like($@, qr/array/);
2400

            
2401
test 'select() string where';
cleanup test
Yuki Kimoto authored on 2011-08-06
2402
$dbi = DBIx::Custom->connect(%memory);
2403
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2404
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2405
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2406
$rows = $dbi->select(
2407
    table => 'table1',
2408
    where => 'key1 = :key1 and key2 = :key2',
2409
    where_param => {key1 => 1, key2 => 2}
2410
)->all;
2411
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2412

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2413
$dbi = DBIx::Custom->connect(%memory);
2414
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2415
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2416
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2417
$rows = $dbi->select(
2418
    table => 'table1',
2419
    where => [
2420
        'key1 = :key1 and key2 = :key2',
2421
        {key1 => 1, key2 => 2}
2422
    ]
2423
)->all;
2424
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2425

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2439
$dbi = DBIx::Custom->connect(%memory);
2440
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2441
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2442
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2443
$dbi->delete(
2444
    table => 'table1',
2445
    where => [
2446
        'key1 = :key1 and key2 = :key2',
2447
         {key1 => 1, key2 => 2}
2448
    ]
2449
);
2450
$rows = $dbi->select(table => 'table1')->all;
2451
is_deeply($rows, [{key1 => 2, key2 => 3}]);
2452

            
2453

            
2454
test 'update() string where';
cleanup test
Yuki Kimoto authored on 2011-08-06
2455
$dbi = DBIx::Custom->connect(%memory);
2456
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2457
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2458
$dbi->update(
2459
    table => 'table1',
2460
    param => {key1 => 5},
2461
    where => 'key1 = :key1 and key2 = :key2',
2462
    where_param => {key1 => 1, key2 => 2}
2463
);
2464
$rows = $dbi->select(table => 'table1')->all;
2465
is_deeply($rows, [{key1 => 5, key2 => 2}]);
2466

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

            
2481
test 'insert id and primary_key option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2482
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2483
$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
2484
$dbi->insert(
2485
    primary_key => ['key1', 'key2'], 
2486
    table => 'table1',
2487
    id => [1, 2],
2488
    param => {key3 => 3}
2489
);
2490
is($dbi->select(table => 'table1')->one->{key1}, 1);
2491
is($dbi->select(table => 'table1')->one->{key2}, 2);
2492
is($dbi->select(table => 'table1')->one->{key3}, 3);
2493

            
2494
$dbi->delete_all(table => 'table1');
2495
$dbi->insert(
2496
    primary_key => 'key1', 
2497
    table => 'table1',
2498
    id => 0,
2499
    param => {key2 => 2, key3 => 3}
2500
);
2501

            
2502
is($dbi->select(table => 'table1')->one->{key1}, 0);
2503
is($dbi->select(table => 'table1')->one->{key2}, 2);
2504
is($dbi->select(table => 'table1')->one->{key3}, 3);
2505

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2506
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2507
$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
2508
$dbi->insert(
2509
    {key3 => 3},
2510
    primary_key => ['key1', 'key2'], 
2511
    table => 'table1',
2512
    id => [1, 2],
2513
);
2514
is($dbi->select(table => 'table1')->one->{key1}, 1);
2515
is($dbi->select(table => 'table1')->one->{key2}, 2);
2516
is($dbi->select(table => 'table1')->one->{key3}, 3);
2517

            
2518

            
2519
test 'model insert id and primary_key option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2520
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2521
$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
2522
$dbi->model('table1')->insert(
2523
    id => [1, 2],
2524
    param => {key3 => 3}
2525
);
2526
$result = $dbi->model('table1')->select;
2527
$row = $result->one;
2528
is($row->{key1}, 1);
2529
is($row->{key2}, 2);
2530
is($row->{key3}, 3);
2531

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2532
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2533
$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
2534
$dbi->model('table1')->insert(
2535
    {key3 => 3},
2536
    id => [1, 2]
2537
);
2538
$result = $dbi->model('table1')->select;
2539
$row = $result->one;
2540
is($row->{key1}, 1);
2541
is($row->{key2}, 2);
2542
is($row->{key3}, 3);
2543

            
2544
test 'update and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2545
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2546
$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
2547
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2548
$dbi->update(
2549
    table => 'table1',
2550
    primary_key => ['key1', 'key2'],
2551
    id => [1, 2],
2552
    param => {key3 => 4}
2553
);
2554
is($dbi->select(table => 'table1')->one->{key1}, 1);
2555
is($dbi->select(table => 'table1')->one->{key2}, 2);
2556
is($dbi->select(table => 'table1')->one->{key3}, 4);
2557

            
2558
$dbi->delete_all(table => 'table1');
2559
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2560
$dbi->update(
2561
    table => 'table1',
2562
    primary_key => 'key1',
2563
    id => 0,
2564
    param => {key3 => 4}
2565
);
2566
is($dbi->select(table => 'table1')->one->{key1}, 0);
2567
is($dbi->select(table => 'table1')->one->{key2}, 2);
2568
is($dbi->select(table => 'table1')->one->{key3}, 4);
2569

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2570
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2571
$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
2572
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2573
$dbi->update(
2574
    {key3 => 4},
2575
    table => 'table1',
2576
    primary_key => ['key1', 'key2'],
2577
    id => [1, 2]
2578
);
2579
is($dbi->select(table => 'table1')->one->{key1}, 1);
2580
is($dbi->select(table => 'table1')->one->{key2}, 2);
2581
is($dbi->select(table => 'table1')->one->{key3}, 4);
2582

            
2583

            
2584
test 'model update and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2585
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2586
$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
2587
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2588
$dbi->model('table1')->update(
2589
    id => [1, 2],
2590
    param => {key3 => 4}
2591
);
2592
$result = $dbi->model('table1')->select;
2593
$row = $result->one;
2594
is($row->{key1}, 1);
2595
is($row->{key2}, 2);
2596
is($row->{key3}, 4);
2597

            
2598

            
2599
test 'delete and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2600
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2601
$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
2602
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2603
$dbi->delete(
2604
    table => 'table1',
2605
    primary_key => ['key1', 'key2'],
2606
    id => [1, 2],
2607
);
2608
is_deeply($dbi->select(table => 'table1')->all, []);
2609

            
2610
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2611
$dbi->delete(
2612
    table => 'table1',
2613
    primary_key => 'key1',
2614
    id => 0,
2615
);
2616
is_deeply($dbi->select(table => 'table1')->all, []);
2617

            
2618

            
2619
test 'model delete and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2620
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2621
$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
2622
$dbi->execute("create table table2 (key1, key2, key3)");
2623
$dbi->execute("create table table3 (key1, key2, key3)");
2624
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2625
$dbi->model('table1')->delete(id => [1, 2]);
2626
is_deeply($dbi->select(table => 'table1')->all, []);
2627
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
2628
$dbi->model('table1_1')->delete(id => [1, 2]);
2629
is_deeply($dbi->select(table => 'table1')->all, []);
2630
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
2631
$dbi->model('table1_3')->delete(id => [1, 2]);
2632
is_deeply($dbi->select(table => 'table1')->all, []);
2633

            
2634

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

            
2649
$dbi->delete_all(table => 'table1');
2650
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2651
$result = $dbi->select(
2652
    table => 'table1',
2653
    primary_key => 'key1',
2654
    id => 0,
2655
);
2656
$row = $result->one;
2657
is($row->{key1}, 0);
2658
is($row->{key2}, 2);
2659
is($row->{key3}, 3);
2660

            
2661
$dbi->delete_all(table => 'table1');
2662
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2663
$result = $dbi->select(
2664
    table => 'table1',
2665
    primary_key => ['key1', 'key2'],
2666
    id => [1, 2]
2667
);
2668
$row = $result->one;
2669
is($row->{key1}, 1);
2670
is($row->{key2}, 2);
2671
is($row->{key3}, 3);
2672

            
2673

            
2674
test 'model select_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
2675
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2676
$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
2677
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2678
$result = $dbi->model('table1')->select(id => [1, 2]);
2679
$row = $result->one;
2680
is($row->{key1}, 1);
2681
is($row->{key2}, 2);
2682
is($row->{key3}, 3);
2683

            
2684
test 'column separator is default .';
cleanup test
Yuki Kimoto authored on 2011-08-06
2685
$dbi = MyDBI7->connect(%memory);
2686
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2687
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2688
$dbi->setup_model;
2689
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2690
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2691
$model = $dbi->model('table1');
2692
$result = $model->select(
2693
    column => [$model->column('table2')],
2694
    where => {'table1.key1' => 1}
2695
);
2696
is_deeply($result->one,
2697
          {'table2.key1' => 1, 'table2.key3' => 3});
2698

            
2699
$result = $model->select(
2700
    column => [$model->column('table2' => [qw/key1 key3/])],
2701
    where => {'table1.key1' => 1}
2702
);
2703
is_deeply($result->one,
2704
          {'table2.key1' => 1, 'table2.key3' => 3});
2705

            
2706

            
2707
test 'type_rule from';
cleanup test
Yuki Kimoto authored on 2011-08-06
2708
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2709
$dbi->type_rule(
2710
    from1 => {
2711
        date => sub { uc $_[0] }
2712
    }
2713
);
2714
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2715
$dbi->insert({key1 => 'a'}, table => 'table1');
2716
$result = $dbi->select(table => 'table1');
2717
is($result->fetch_first->[0], 'A');
2718

            
2719
$result = $dbi->select(table => 'table1');
2720
is($result->one->{key1}, 'A');
2721

            
2722

            
2723
test 'type_rule into';
cleanup test
Yuki Kimoto authored on 2011-08-06
2724
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2725
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2726
$dbi->type_rule(
2727
    into1 => {
2728
        date => sub { uc $_[0] }
2729
    }
2730
);
2731
$dbi->insert({key1 => 'a'}, table => 'table1');
2732
$result = $dbi->select(table => 'table1');
2733
is($result->one->{key1}, 'A');
2734

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2735
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2736
$dbi->execute("create table table1 (key1 date, key2 datetime)");
2737
$dbi->type_rule(
2738
    into1 => [
2739
         [qw/date datetime/] => sub { uc $_[0] }
2740
    ]
2741
);
2742
$dbi->insert({key1 => 'a', key2 => 'b'}, table => 'table1');
2743
$result = $dbi->select(table => 'table1');
2744
$row = $result->one;
2745
is($row->{key1}, 'A');
2746
is($row->{key2}, 'B');
2747

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2748
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2749
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2750
$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1');
2751
$dbi->type_rule(
2752
    into1 => [
2753
        [qw/date datetime/] => sub { uc $_[0] }
2754
    ]
2755
);
2756
$result = $dbi->execute(
2757
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
2758
    param => {key1 => 'a', 'table1.key2' => 'b'}
2759
);
2760
$row = $result->one;
2761
is($row->{key1}, 'a');
2762
is($row->{key2}, 'B');
2763

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2764
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2765
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2766
$dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1');
2767
$dbi->type_rule(
2768
    into1 => [
2769
        [qw/date datetime/] => sub { uc $_[0] }
2770
    ]
2771
);
2772
$result = $dbi->execute(
2773
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
2774
    param => {key1 => 'a', 'table1.key2' => 'b'},
2775
    table => 'table1'
2776
);
2777
$row = $result->one;
2778
is($row->{key1}, 'A');
2779
is($row->{key2}, 'B');
2780

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2781
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2782
$dbi->execute("create table table1 (key1 date, key2 datetime)");
2783
$dbi->register_filter(twice => sub { $_[0] * 2 });
2784
$dbi->type_rule(
2785
    from1 => {
2786
        date => 'twice',
2787
    },
2788
    into1 => {
2789
        date => 'twice',
2790
    }
2791
);
2792
$dbi->insert({key1 => 2}, table => 'table1');
2793
$result = $dbi->select(table => 'table1');
2794
is($result->fetch->[0], 8);
2795

            
2796
test 'type_rule and filter order';
cleanup test
Yuki Kimoto authored on 2011-08-06
2797
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2798
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2799
$dbi->type_rule(
2800
    into1 => {
2801
        date => sub { $_[0] . 'b' }
2802
    },
2803
    into2 => {
2804
        date => sub { $_[0] . 'c' }
2805
    },
2806
    from1 => {
2807
        date => sub { $_[0] . 'd' }
2808
    },
2809
    from2 => {
2810
        date => sub { $_[0] . 'e' }
2811
    }
2812
);
2813
$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }});
2814
$result = $dbi->select(table => 'table1');
2815
$result->filter(key1 => sub { $_[0] . 'f' });
2816
is($result->fetch_first->[0], '1abcdef');
2817

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2818
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2819
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2820
$dbi->type_rule(
2821
    from1 => {
2822
        date => sub { $_[0] . 'p' }
2823
    },
2824
    from2 => {
2825
        date => sub { $_[0] . 'q' }
2826
    },
2827
);
2828
$dbi->insert({key1 => '1'}, table => 'table1');
2829
$result = $dbi->select(table => 'table1');
2830
$result->type_rule(
2831
    from1 => {
2832
        date => sub { $_[0] . 'd' }
2833
    },
2834
    from2 => {
2835
        date => sub { $_[0] . 'e' }
2836
    }
2837
);
2838
$result->filter(key1 => sub { $_[0] . 'f' });
2839
is($result->fetch_first->[0], '1def');
2840

            
2841
test 'type_rule_off';
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->type_rule(
2845
    from1 => {
2846
        date => sub { $_[0] * 2 },
2847
    },
2848
    into1 => {
2849
        date => sub { $_[0] * 2 },
2850
    }
2851
);
2852
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
2853
$result = $dbi->select(table => 'table1', type_rule_off => 1);
2854
is($result->type_rule_off->fetch->[0], 2);
2855

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2870
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2871
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2872
$dbi->type_rule(
2873
    from1 => {
2874
        date => sub { $_[0] * 2 },
2875
    },
2876
    into1 => {
2877
        date => sub { $_[0] * 3 },
2878
    }
2879
);
2880
$dbi->insert({key1 => 2}, table => 'table1');
2881
$result = $dbi->select(table => 'table1');
2882
is($result->one->{key1}, 12);
2883

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2898
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2899
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2900
$dbi->register_filter(ppp => sub { uc $_[0] });
2901
$dbi->type_rule(
2902
    into1 => {
2903
        date => 'ppp'
2904
    }
2905
);
2906
$dbi->insert({key1 => 'a'}, table => 'table1');
2907
$result = $dbi->select(table => 'table1');
2908
is($result->one->{key1}, 'A');
2909

            
2910
eval{$dbi->type_rule(
2911
    into1 => {
2912
        date => 'pp'
2913
    }
2914
)};
2915
like($@, qr/not registered/);
2916

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2917
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2918
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2919
eval {
2920
    $dbi->type_rule(
2921
        from1 => {
2922
            Date => sub { $_[0] * 2 },
2923
        }
2924
    );
2925
};
2926
like($@, qr/lower/);
2927

            
2928
eval {
2929
    $dbi->type_rule(
2930
        into1 => {
2931
            Date => sub { $_[0] * 2 },
2932
        }
2933
    );
2934
};
2935
like($@, qr/lower/);
2936

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

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

            
2971
$result = $dbi->select(table => 'table1');
2972
$result->type_rule(
2973
    from1 => {
2974
        date => sub { $_[0] * 3 }
2975
    }
2976
);
2977
$row = $result->one;
2978
is($row->{key1}, 6);
2979
is($row->{key2}, 2);
2980

            
2981
$result = $dbi->select(table => 'table1');
2982
$result->type_rule(
2983
    from1 => {
2984
        date => sub { $_[0] * 3 }
2985
    }
2986
);
2987
$row = $result->one;
2988
is($row->{key1}, 6);
2989
is($row->{key2}, 2);
2990
$result = $dbi->select(table => 'table1');
2991
$result->type_rule(
2992
    from1 => [date => sub { $_[0] * 3 }]
2993
);
2994
$row = $result->one;
2995
is($row->{key1}, 6);
2996
is($row->{key2}, 2);
2997
$dbi->register_filter(fivetimes => sub { $_[0] * 5});
2998
$result = $dbi->select(table => 'table1');
2999
$result->type_rule(
3000
    from1 => [date => 'fivetimes']
3001
);
3002
$row = $result->one;
3003
is($row->{key1}, 10);
3004
is($row->{key2}, 2);
3005
$result = $dbi->select(table => 'table1');
3006
$result->type_rule(
3007
    from1 => [date => undef]
3008
);
3009
$row = $result->one;
3010
is($row->{key1}, 2);
3011
is($row->{key2}, 2);
3012

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3037
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3038
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3039
$dbi->type_rule(
3040
    into1 => {
3041
        date => sub { $_[0] . 'b' }
3042
    },
3043
    into2 => {
3044
        date => sub { $_[0] . 'c' }
3045
    },
3046
    from1 => {
3047
        date => sub { $_[0] . 'd' }
3048
    },
3049
    from2 => {
3050
        date => sub { $_[0] . 'e' }
3051
    }
3052
);
3053
$dbi->insert({key1 => '1'}, table => 'table1', type_rule_off => 1);
3054
$result = $dbi->select(table => 'table1');
3055
is($result->type_rule_off->fetch_first->[0], '1');
3056
$result = $dbi->select(table => 'table1');
3057
is($result->type_rule_on->fetch_first->[0], '1de');
3058

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3059
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3060
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3061
$dbi->type_rule(
3062
    into1 => {
3063
        date => sub { $_[0] . 'b' }
3064
    },
3065
    into2 => {
3066
        date => sub { $_[0] . 'c' }
3067
    },
3068
    from1 => {
3069
        date => sub { $_[0] . 'd' }
3070
    },
3071
    from2 => {
3072
        date => sub { $_[0] . 'e' }
3073
    }
3074
);
3075
$dbi->insert({key1 => '1'}, table => 'table1', type_rule1_off => 1);
3076
$result = $dbi->select(table => 'table1');
3077
is($result->type_rule1_off->fetch_first->[0], '1ce');
3078
$result = $dbi->select(table => 'table1');
3079
is($result->type_rule1_on->fetch_first->[0], '1cde');
3080

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

            
3103
test 'separator';
cleanup test
Yuki Kimoto authored on 2011-08-06
3104
$dbi = DBIx::Custom->connect(%memory);
3105
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
3106
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
3107

            
3108
$dbi->create_model(
3109
    table => 'table1',
3110
    join => [
3111
       'left outer join table2 on table1.key1 = table2.key1'
3112
    ],
3113
    primary_key => ['key1'],
3114
);
3115
$model2 = $dbi->create_model(
3116
    table => 'table2',
3117
);
3118
$dbi->setup_model;
3119
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3120
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
3121
$model = $dbi->model('table1');
3122
$result = $model->select(
3123
    column => [
3124
        $model->mycolumn,
3125
        {table2 => [qw/key1 key3/]}
3126
    ],
3127
    where => {'table1.key1' => 1}
3128
);
3129
is_deeply($result->one,
3130
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
3131
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3132

            
3133
$dbi->separator('__');
3134
$model = $dbi->model('table1');
3135
$result = $model->select(
3136
    column => [
3137
        $model->mycolumn,
3138
        {table2 => [qw/key1 key3/]}
3139
    ],
3140
    where => {'table1.key1' => 1}
3141
);
3142
is_deeply($result->one,
3143
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
3144
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3145

            
3146
$dbi->separator('-');
3147
$model = $dbi->model('table1');
3148
$result = $model->select(
3149
    column => [
3150
        $model->mycolumn,
3151
        {table2 => [qw/key1 key3/]}
3152
    ],
3153
    where => {'table1.key1' => 1}
3154
);
3155
is_deeply($result->one,
3156
          {key1 => 1, key2 => 2, 'table2-key1' => 1, 'table2-key3' => 3});
3157
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3158

            
3159

            
3160
test 'filter_off';
cleanup test
Yuki Kimoto authored on 2011-08-06
3161
$dbi = DBIx::Custom->connect(%memory);
3162
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
3163
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
3164

            
3165
$dbi->create_model(
3166
    table => 'table1',
3167
    join => [
3168
       'left outer join table2 on table1.key1 = table2.key1'
3169
    ],
3170
    primary_key => ['key1'],
3171
);
3172
$dbi->setup_model;
3173
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3174
$model = $dbi->model('table1');
3175
$result = $model->select(column => 'key1');
3176
$result->filter(key1 => sub { $_[0] * 2 });
3177
is_deeply($result->one, {key1 => 2});
3178

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

            
3183

            
3184
test 'select prefix option';
cleanup test
Yuki Kimoto authored on 2011-08-06
3185
$dbi = DBIx::Custom->connect(%memory);
3186
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
3187
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3188
$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all;
3189
is_deeply($rows, [{key1 => 1, key2 => 2}], "table");
3190

            
3191

            
3192
test 'separator';
cleanup test
Yuki Kimoto authored on 2011-08-06
3193
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3194
is($dbi->separator, '.');
3195
$dbi->separator('-');
3196
is($dbi->separator, '-');
3197
$dbi->separator('__');
3198
is($dbi->separator, '__');
3199
eval { $dbi->separator('?') };
3200
like($@, qr/Separator/);
3201

            
3202

            
3203
test 'map_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
3204
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3205
$param = $dbi->map_param(
3206
    {id => 1, author => 'Ken', price => 1900},
3207
    id => 'book.id',
3208
    author => ['book.author', sub { '%' . $_[0] . '%' }],
3209
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
3210
);
3211
is_deeply($param, {'book.id' => 1, 'book.author' => '%Ken%',
3212
  'book.price' => 1900});
3213

            
3214
$param = $dbi->map_param(
3215
    {id => 0, author => 0, price => 0},
3216
    id => 'book.id',
3217
    author => ['book.author', sub { '%' . $_[0] . '%' }],
3218
    price => ['book.price', sub { '%' . $_[0] . '%' },
3219
      {if => sub { $_[0] eq 0 }}]
3220
);
3221
is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
3222

            
3223
$param = $dbi->map_param(
3224
    {id => '', author => '', price => ''},
3225
    id => 'book.id',
3226
    author => ['book.author', sub { '%' . $_[0] . '%' }],
3227
    price => ['book.price', sub { '%' . $_[0] . '%' },
3228
      {if => sub { $_[0] eq 1 }}]
3229
);
3230
is_deeply($param, {});
3231

            
3232
$param = $dbi->map_param(
3233
    {id => undef, author => undef, price => undef},
3234
    id => 'book.id',
3235
    price => ['book.price', {if => 'exists'}]
3236
);
3237
is_deeply($param, {'book.price' => undef});
3238

            
3239
$param = $dbi->map_param(
3240
    {price => 'a'},
3241
    id => ['book.id', {if => 'exists'}],
3242
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
3243
);
3244
is_deeply($param, {'book.price' => '%a'});
3245

            
3246

            
3247
test 'table_alias';
cleanup test
Yuki Kimoto authored on 2011-08-06
3248
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3249
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3250
$dbi->type_rule(
3251
    into1 => {
3252
        date => sub { uc $_[0] }
3253
    }
3254
);
3255
$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'},
3256
  table_alias => {table2 => 'table1'});
3257
$result = $dbi->select(table => 'table1');
3258
is($result->one->{key1}, 'A');
3259

            
3260

            
3261
test 'order';
cleanup test
Yuki Kimoto authored on 2011-08-06
3262
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3263
$dbi->execute("create table table1 (key1, key2)");
3264
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
3265
$dbi->insert({key1 => 1, key2 => 3}, table => 'table1');
3266
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
3267
$dbi->insert({key1 => 2, key2 => 4}, table => 'table1');
3268
my $order = $dbi->order;
3269
$order->prepend('key1', 'key2 desc');
3270
$result = $dbi->select(table => 'table1', append => "$order");
3271
is_deeply($result->all, [{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1},
3272
  {key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}]);
3273
$order->prepend('key1 desc');
3274
$result = $dbi->select(table => 'table1', append => "$order");
3275
is_deeply($result->all, [{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2},
3276
  {key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}]);
3277

            
3278
$order = $dbi->order;
3279
$order->prepend(['table1-key1'], [qw/table1-key2 desc/]);
3280
$result = $dbi->select(table => 'table1',
3281
  column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']],
3282
  append => "$order");
3283
is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3},
3284
  {'table1-key1' => 1, 'table1-key2' => 1},
3285
  {'table1-key1' => 2, 'table1-key2' => 4},
3286
  {'table1-key1' => 2, 'table1-key2' => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
3287

            
3288
test 'tag_parse';
cleanup test
Yuki Kimoto authored on 2011-08-06
3289
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3290
$dbi->tag_parse(0);
cleanup test
Yuki Kimoto authored on 2011-08-06
3291
$dbi->execute("create table table1 (key1, key2)");
3292
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
3293
eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})};
3294
ok($@);
cleanup test
Yuki Kimoto authored on 2011-08-06
3295

            
3296
test 'last_sql';
cleanup test
Yuki Kimoto authored on 2011-08-06
3297
$dbi = DBIx::Custom->connect(%memory);
3298
$dbi->execute("create table table1 (key1, key2)");
3299
$dbi->execute('select * from table1');
3300
is($dbi->last_sql, 'select * from table1;');
3301

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

            
3305
test 'DBIx::Custom header';
cleanup test
Yuki Kimoto authored on 2011-08-06
3306
$dbi = DBIx::Custom->connect(%memory);
3307
$dbi->execute("create table table1 (key1, key2)");
3308
$result = $dbi->execute('select key1 as h1, key2 as h2 from table1');
3309
is_deeply($result->header, [qw/h1 h2/]);
cleanup test
Yuki Kimoto authored on 2011-08-06
3310

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

            
3317
$source = "select * from table1 where :key1{=} and :key2{=}";
3318
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
3319
$rows = $result->all;
3320
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3321

            
3322
$source = "select * from table1 where :key1{ = } and :key2{=}";
3323
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
3324
$rows = $result->all;
3325
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3326

            
3327
$source = "select * from table1 where :key1{<} and :key2{=}";
3328
$result = $dbi->execute($source, param => {key1 => 5, key2 => 2});
3329
$rows = $result->all;
3330
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3331

            
3332
$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}";
3333
$result = $dbi->execute(
3334
    $source,
3335
    param => {'table1.key1' => 1, 'table1.key2' => 1},
3336
    filter => {'table1.key2' => sub { $_[0] * 2 }}
3337
);
3338
$rows = $result->all;
3339
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3340

            
3341
test 'high perfomance way';
cleanup test
Yuki Kimoto authored on 2011-08-06
3342
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
3343
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
3344
$rows = [
3345
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3346
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3347
];
3348
{
3349
    my $query;
3350
    foreach my $row (@$rows) {
3351
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
3352
      $dbi->execute($query, $row, filter => {ab => sub { $_[0] * 2 }});
3353
    }
3354
    is_deeply($dbi->select(table => 'table1')->all,
3355
      [
3356
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3357
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3358
      ]
3359
    );
3360
}
3361

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3362
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
3363
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
3364
$rows = [
3365
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3366
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3367
];
3368
{
3369
    my $query;
3370
    my $sth;
3371
    foreach my $row (@$rows) {
3372
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
3373
      $sth ||= $query->sth;
3374
      $sth->execute(map { $row->{$_} } sort keys %$row);
3375
    }
3376
    is_deeply($dbi->select(table => 'table1')->all,
3377
      [
3378
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3379
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3380
      ]
3381
    );
3382
}
cleanup test
Yuki Kimoto authored on 2011-08-06
3383

            
clenup test
Yuki Kimoto authored on 2011-08-06
3384
test 'result';
3385
$dbi = DBIx::Custom->connect(%memory);
3386
$dbi->execute($create_table_default);
3387
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3388
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3389

            
3390
$result = $dbi->select(table => 'table1');
3391
@rows = ();
3392
while (my $row = $result->fetch) {
3393
    push @rows, [@$row];
3394
}
3395
is_deeply(\@rows, [[1, 2], [3, 4]]);
3396

            
3397
$result = $dbi->select(table => 'table1');
3398
@rows = ();
3399
while (my $row = $result->fetch_hash) {
3400
    push @rows, {%$row};
3401
}
3402
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
3403

            
3404
$result = $dbi->select(table => 'table1');
3405
$row = $result->fetch_first;
3406
is_deeply($row, [1, 2], "row");
3407
$row = $result->fetch;
3408
ok(!$row, "finished");
3409

            
3410
$result = $dbi->select(table => 'table1');
3411
$row = $result->fetch_hash_first;
3412
is_deeply($row, {key1 => 1, key2 => 2}, "row");
3413
$row = $result->fetch_hash;
3414
ok(!$row, "finished");
3415

            
3416
$dbi->execute('create table table2 (key1, key2);');
3417
$result = $dbi->select(table => 'table2');
3418
$row = $result->fetch_hash_first;
3419
ok(!$row, "no row fetch");
3420

            
3421
$dbi = DBIx::Custom->connect(%memory);
3422
$dbi->execute($create_table_default);
3423
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3424
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3425
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1');
3426
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1');
3427
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1');
3428
$result = $dbi->select(table => 'table1');
3429
$rows = $result->fetch_multi(2);
3430
is_deeply($rows, [[1, 2],
3431
                  [3, 4]], "fetch_multi first");
3432
$rows = $result->fetch_multi(2);
3433
is_deeply($rows, [[5, 6],
3434
                  [7, 8]], "fetch_multi secound");
3435
$rows = $result->fetch_multi(2);
3436
is_deeply($rows, [[9, 10]], "fetch_multi third");
3437
$rows = $result->fetch_multi(2);
3438
ok(!$rows);
3439

            
3440
$result = $dbi->select(table => 'table1');
3441
eval {$result->fetch_multi};
3442
like($@, qr/Row count must be specified/, "Not specified row count");
3443

            
3444
$result = $dbi->select(table => 'table1');
3445
$rows = $result->fetch_hash_multi(2);
3446
is_deeply($rows, [{key1 => 1, key2 => 2},
3447
                  {key1 => 3, key2 => 4}], "fetch_multi first");
3448
$rows = $result->fetch_hash_multi(2);
3449
is_deeply($rows, [{key1 => 5, key2 => 6},
3450
                  {key1 => 7, key2 => 8}], "fetch_multi secound");
3451
$rows = $result->fetch_hash_multi(2);
3452
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third");
3453
$rows = $result->fetch_hash_multi(2);
3454
ok(!$rows);
3455

            
3456
$result = $dbi->select(table => 'table1');
3457
eval {$result->fetch_hash_multi};
3458
like($@, qr/Row count must be specified/, "Not specified row count");
3459

            
3460
$dbi = DBIx::Custom->connect(%memory);
3461
$dbi->execute($create_table_default);
3462
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3463
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3464

            
3465
test 'fetch_all';
3466
$result = $dbi->select(table => 'table1');
3467
$rows = $result->fetch_all;
3468
is_deeply($rows, [[1, 2], [3, 4]]);
3469

            
3470
$result = $dbi->select(table => 'table1');
3471
$rows = $result->fetch_hash_all;
3472
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
3473

            
3474
$result = $dbi->select(table => 'table1');
3475
$result->dbi->filters({three_times => sub { $_[0] * 3}});
3476
$result->filter({key1 => 'three_times'});
3477

            
3478
$rows = $result->fetch_all;
3479
is_deeply($rows, [[3, 2], [9, 4]], "array");
3480

            
3481
$result = $dbi->select(table => 'table1');
3482
$result->dbi->filters({three_times => sub { $_[0] * 3}});
3483
$result->filter({key1 => 'three_times'});
3484
$rows = $result->fetch_hash_all;
3485
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash");
3486

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3487
test "query_builder";
3488
$datas = [
3489
    # Basic tests
3490
    {   name            => 'placeholder basic',
3491
        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
3492
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;",
3493
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
3494
    },
3495
    {
3496
        name            => 'placeholder in',
3497
        source            => "{in k1 3};",
3498
        sql_expected    => "k1 in (?, ?, ?);",
3499
        columns_expected   => [qw/k1 k1 k1/]
3500
    },
3501
    
3502
    # Table name
3503
    {
3504
        name            => 'placeholder with table name',
3505
        source            => "{= a.k1} {= a.k2}",
3506
        sql_expected    => "a.k1 = ? a.k2 = ?;",
3507
        columns_expected  => [qw/a.k1 a.k2/]
3508
    },
3509
    {   
3510
        name            => 'placeholder in with table name',
3511
        source            => "{in a.k1 2} {in b.k2 2}",
3512
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?);",
3513
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
3514
    },
3515
    {
3516
        name            => 'not contain tag',
3517
        source            => "aaa",
3518
        sql_expected    => "aaa;",
3519
        columns_expected  => [],
3520
    }
3521
];
3522

            
3523
for (my $i = 0; $i < @$datas; $i++) {
3524
    my $data = $datas->[$i];
3525
    my $builder = DBIx::Custom->new->query_builder;
3526
    my $query = $builder->build_query($data->{source});
3527
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
3528
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
3529
}
3530

            
3531
$builder = DBIx::Custom->new->query_builder;
3532
$ret_val = $builder->register_tag(
3533
    p => sub {
3534
        my @args = @_;
3535
        
3536
        my $expand    = "? $args[0] $args[1]";
3537
        my $columns = [2];
3538
        return [$expand, $columns];
3539
    }
3540
);
3541

            
3542
$query = $builder->build_query("{p a b}");
3543
is($query->{sql}, "? a b;", "register_tag sql");
3544
is_deeply($query->{columns}, [2], "register_tag columns");
3545
isa_ok($ret_val, 'DBIx::Custom::QueryBuilder');
3546

            
3547
$builder = DBIx::Custom->new->query_builder;
3548

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

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

            
3555
$builder->register_tag({
3556
    q => 'string'
3557
});
3558

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

            
3562
$builder->register_tag({
3563
   r => sub {} 
3564
});
3565

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

            
3569
$builder->register_tag({
3570
   s => sub { return ["a", ""]} 
3571
});
3572

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

            
3576
$builder->register_tag(
3577
    t => sub {return ["a", []]}
3578
);
3579

            
3580

            
3581
test 'General error case';
3582
$builder = DBIx::Custom->new->query_builder;
3583
$builder->register_tag(
3584
    a => sub {
3585
        return ["? ? ?", ['']];
3586
    }
3587
);
3588
eval{$builder->build_query("{a}")};
3589
like($@, qr/\QPlaceholder count/, "placeholder count is invalid");
3590

            
3591

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

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

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

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

            
3607
test 'variouse source';
3608
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;";
3609
$query = $builder->build_query($source);
3610
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1");
3611

            
3612
$source = "abc;";
3613
$query = $builder->build_query($source);
3614
is($query->sql, 'abc;', "basic : 2");
3615

            
3616
$source = "{= a}";
3617
$query = $builder->build_query($source);
3618
is($query->sql, 'a = ?;', "only tag");
3619

            
3620
$source = "000;";
3621
$query = $builder->build_query($source);
3622
is($query->sql, '000;', "contain 0 value");
3623

            
3624
$source = "a {= b} }";
3625
eval{$builder->build_query($source)};
3626
like($@, qr/unexpected "}"/, "error : 1");
3627

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