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

            
937
test 'DBIx::Custom::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');
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
1229
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1230
$row = $result->all;
1231
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1232

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

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

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

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

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

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

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

            
1287
test 'dbi_option default';
1288
$dbi = DBIx::Custom->new;
1289
is_deeply($dbi->dbi_option, {});
1290

            
1291
test 'register_tag_processor';
cleanup test
Yuki Kimoto authored on 2011-08-06
1292
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1293
$dbi->register_tag_processor(
1294
    a => sub { 1 }
1295
);
1296
is($dbi->query_builder->tag_processors->{a}->(), 1);
1297

            
1298
test 'register_tag';
cleanup test
Yuki Kimoto authored on 2011-08-06
1299
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1300
$dbi->register_tag(
1301
    b => sub { 2 }
1302
);
1303
is($dbi->query_builder->tags->{b}->(), 2);
1304

            
1305
test 'table not specify exception';
cleanup test
Yuki Kimoto authored on 2011-08-06
1306
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1307
eval {$dbi->insert};
1308
like($@, qr/table/);
1309
eval {$dbi->update};
1310
like($@, qr/table/);
1311
eval {$dbi->delete};
1312
like($@, qr/table/);
1313
eval {$dbi->select};
1314
like($@, qr/table/);
1315

            
1316

            
1317
test 'more tests';
cleanup test
Yuki Kimoto authored on 2011-08-06
1318
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1319
eval{$dbi->apply_filter('table', 'column', [])};
1320
like($@, qr/apply_filter/);
1321

            
1322
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
1323
like($@, qr/apply_filter/);
1324

            
1325
$dbi->apply_filter(
1326

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1345
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1346
eval {$dbi->apply_filter('table1', 'key2', {out => 'no'})};
1347
like($@, qr/not registered/);
1348
eval {$dbi->apply_filter('table1', 'key2', {in => 'no'})};
1349
like($@, qr/not registered/);
1350
$dbi->method({one => sub { 1 }});
1351
is($dbi->one, 1);
1352

            
1353
eval{DBIx::Custom->connect()};
1354
like($@, qr/_connect/);
1355

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

            
1367
$dbi->register_filter(one => sub { });
1368
$dbi->default_fetch_filter('one');
1369
ok($dbi->default_fetch_filter);
1370
$dbi->default_bind_filter('one');
1371
ok($dbi->default_bind_filter);
1372
eval{$dbi->default_fetch_filter('no')};
1373
like($@, qr/not registered/);
1374
eval{$dbi->default_bind_filter('no')};
1375
like($@, qr/not registered/);
1376
$dbi->default_bind_filter(undef);
1377
ok(!defined $dbi->default_bind_filter);
1378
$dbi->default_fetch_filter(undef);
1379
ok(!defined $dbi->default_fetch_filter);
1380
eval {$dbi->execute('select * from table1 {} {= author') };
1381
like($@, qr/Tag not finished/);
1382

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

            
1396
test 'dbi_option';
cleanup test
Yuki Kimoto authored on 2011-08-06
1397
$dbi = DBIx::Custom->connect(%memory,
cleanup test
Yuki Kimoto authored on 2011-08-06
1398
                             dbi_option => {PrintError => 1});
1399
ok($dbi->dbh->{PrintError});
cleanup test
Yuki Kimoto authored on 2011-08-06
1400
$dbi = DBIx::Custom->connect(%memory,
cleanup test
Yuki Kimoto authored on 2011-08-06
1401
                             dbi_options => {PrintError => 1});
1402
ok($dbi->dbh->{PrintError});
1403

            
1404
test 'DBIx::Custom::Result stash()';
1405
$result = DBIx::Custom::Result->new;
1406
is_deeply($result->stash, {}, 'default');
1407
$result->stash->{foo} = 1;
1408
is($result->stash->{foo}, 1, 'get and set');
1409

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

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

            
1421
$result = $dbi->select(
1422
    table => ['company', 'location'], relation => {'company.location_id' => 'location.id'},
1423
    column => ['location.name as location__name']
1424
);
1425
is($result->fetch_first->[0], 'B');
1426

            
1427
$result = $dbi->select(
1428
    table => 'company', 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
test 'Model class';
1440
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1441
$dbi = MyDBI1->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1442
$dbi->execute("create table book (title, author)");
1443
$model = $dbi->model('book');
1444
$model->insert({title => 'a', author => 'b'});
1445
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
1446
$dbi->execute("create table company (name)");
1447
$model = $dbi->model('company');
1448
$model->insert({name => 'a'});
1449
is_deeply($model->list->all, [{name => 'a'}], 'basic');
1450
is($dbi->models->{'book'}, $dbi->model('book'));
1451
is($dbi->models->{'company'}, $dbi->model('company'));
1452

            
1453
{
1454
    package MyDBI4;
1455

            
1456
    use strict;
1457
    use warnings;
1458

            
1459
    use base 'DBIx::Custom';
1460

            
1461
    sub connect {
1462
        my $self = shift->SUPER::connect(@_);
1463
        
1464
        $self->include_model(
1465
            MyModel2 => [
1466
                'book',
1467
                {class => 'Company', name => 'company'}
1468
            ]
1469
        );
1470
    }
1471

            
1472
    package MyModel2::Base1;
1473

            
1474
    use strict;
1475
    use warnings;
1476

            
1477
    use base 'DBIx::Custom::Model';
1478

            
1479
    package MyModel2::book;
1480

            
1481
    use strict;
1482
    use warnings;
1483

            
1484
    use base 'MyModel2::Base1';
1485

            
1486
    sub insert {
1487
        my ($self, $param) = @_;
1488
        
1489
        return $self->SUPER::insert(param => $param);
1490
    }
1491

            
1492
    sub list { shift->select; }
1493

            
1494
    package MyModel2::Company;
1495

            
1496
    use strict;
1497
    use warnings;
1498

            
1499
    use base 'MyModel2::Base1';
1500

            
1501
    sub insert {
1502
        my ($self, $param) = @_;
1503
        
1504
        return $self->SUPER::insert(param => $param);
1505
    }
1506

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

            
1519
{
1520
     package MyDBI5;
1521

            
1522
    use strict;
1523
    use warnings;
1524

            
1525
    use base 'DBIx::Custom';
1526

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

            
1543
test 'primary_key';
1544
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1545
$dbi = MyDBI1->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1546
$model = $dbi->model('book');
1547
$model->primary_key(['id', 'number']);
1548
is_deeply($model->primary_key, ['id', 'number']);
1549

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

            
1557
test 'setup_model';
1558
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1559
$dbi = MyDBI1->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1560
$dbi->execute('create table book (id)');
1561
$dbi->execute('create table company (id, name);');
1562
$dbi->execute('create table test (id, name, primary key (id, name));');
1563
$dbi->setup_model;
1564
is_deeply($dbi->model('book')->columns, ['id']);
1565
is_deeply($dbi->model('company')->columns, ['id', 'name']);
1566

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

            
1578
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1579
$dbi->delete_at(
1580
    table => 'table1',
1581
    primary_key => 'key1',
1582
    where => 1,
1583
);
1584
is_deeply($dbi->select(table => 'table1')->all, []);
1585

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

            
1599
$dbi->delete_all(table => 'table1');
1600
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1601
$dbi->insert_at(
1602
    primary_key => 'key1', 
1603
    table => 'table1',
1604
    where => 1,
1605
    param => {key2 => 2, key3 => 3}
1606
);
1607

            
1608
is($dbi->select(table => 'table1')->one->{key1}, 1);
1609
is($dbi->select(table => 'table1')->one->{key2}, 2);
1610
is($dbi->select(table => 'table1')->one->{key3}, 3);
1611

            
1612
eval {
1613
    $dbi->insert_at(
1614
        table => 'table1',
1615
        primary_key => ['key1', 'key2'],
1616
        where => {},
1617
        param => {key1 => 1, key2 => 2, key3 => 3},
1618
    );
1619
};
1620
like($@, qr/must be/);
1621

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1622
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1623
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1624
$dbi->insert_at(
1625
    {key3 => 3},
1626
    primary_key => ['key1', 'key2'], 
1627
    table => 'table1',
1628
    where => [1, 2],
1629
);
1630
is($dbi->select(table => 'table1')->one->{key1}, 1);
1631
is($dbi->select(table => 'table1')->one->{key2}, 2);
1632
is($dbi->select(table => 'table1')->one->{key3}, 3);
1633

            
1634
test 'update_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1635
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1636
$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
1637
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1638
$dbi->update_at(
1639
    table => 'table1',
1640
    primary_key => ['key1', 'key2'],
1641
    where => [1, 2],
1642
    param => {key3 => 4}
1643
);
1644
is($dbi->select(table => 'table1')->one->{key1}, 1);
1645
is($dbi->select(table => 'table1')->one->{key2}, 2);
1646
is($dbi->select(table => 'table1')->one->{key3}, 4);
1647

            
1648
$dbi->delete_all(table => 'table1');
1649
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1650
$dbi->update_at(
1651
    table => 'table1',
1652
    primary_key => 'key1',
1653
    where => 1,
1654
    param => {key3 => 4}
1655
);
1656
is($dbi->select(table => 'table1')->one->{key1}, 1);
1657
is($dbi->select(table => 'table1')->one->{key2}, 2);
1658
is($dbi->select(table => 'table1')->one->{key3}, 4);
1659

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

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

            
1687
$dbi->delete_all(table => 'table1');
1688
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1689
$result = $dbi->select_at(
1690
    table => 'table1',
1691
    primary_key => 'key1',
1692
    where => 1,
1693
);
1694
$row = $result->one;
1695
is($row->{key1}, 1);
1696
is($row->{key2}, 2);
1697
is($row->{key3}, 3);
1698

            
1699
$dbi->delete_all(table => 'table1');
1700
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1701
$result = $dbi->select_at(
1702
    table => 'table1',
1703
    primary_key => ['key1', 'key2'],
1704
    where => [1, 2]
1705
);
1706
$row = $result->one;
1707
is($row->{key1}, 1);
1708
is($row->{key2}, 2);
1709
is($row->{key3}, 3);
1710

            
1711
eval {
1712
    $result = $dbi->select_at(
1713
        table => 'table1',
1714
        primary_key => ['key1', 'key2'],
1715
        where => {},
1716
    );
1717
};
1718
like($@, qr/must be/);
1719

            
1720
eval {
1721
    $result = $dbi->select_at(
1722
        table => 'table1',
1723
        primary_key => ['key1', 'key2'],
1724
        where => [1],
1725
    );
1726
};
1727
like($@, qr/same/);
1728

            
1729
eval {
1730
    $result = $dbi->update_at(
1731
        table => 'table1',
1732
        primary_key => ['key1', 'key2'],
1733
        where => {},
1734
        param => {key1 => 1, key2 => 2},
1735
    );
1736
};
1737
like($@, qr/must be/);
1738

            
1739
eval {
1740
    $result = $dbi->delete_at(
1741
        table => 'table1',
1742
        primary_key => ['key1', 'key2'],
1743
        where => {},
1744
    );
1745
};
1746
like($@, qr/must be/);
1747

            
1748
test 'columns';
1749
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1750
$dbi = MyDBI1->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1751
$model = $dbi->model('book');
1752

            
1753

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

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

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

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

            
1819

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

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

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

            
1869

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

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

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

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

            
1906
            
1907
eval { $dbi->update_param({";" => 1}) };
1908
like($@, qr/not safety/);
1909

            
1910

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

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

            
1930

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

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

            
1955
eval { $dbi->insert_param({";" => 1}) };
1956
like($@, qr/not safety/);
1957

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

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

            
1976
$rows = $dbi->select(
1977
    table => 'table1',
1978
    where   => {'key1' => 1},
1979
    join  => ['left outer join table2 on table1.key1 = table2.key1']
1980
)->all;
1981
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1982

            
1983
eval {
1984
    $rows = $dbi->select(
1985
        table => 'table1',
1986
        column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
1987
        where   => {'table1.key2' => 2},
1988
        join  => {'table1.key1' => 'table2.key1'}
1989
    );
1990
};
1991
like ($@, qr/array/);
1992

            
1993
$rows = $dbi->select(
1994
    table => 'table1',
1995
    where   => {'key1' => 1},
1996
    join  => ['left outer join table2 on table1.key1 = table2.key1',
1997
              'left outer join table3 on table2.key3 = table3.key3']
1998
)->all;
1999
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2000

            
2001
$rows = $dbi->select(
2002
    column => 'table3.key4 as table3__key4',
2003
    table => 'table1',
2004
    where   => {'table1.key1' => 1},
2005
    join  => ['left outer join table2 on table1.key1 = table2.key1',
2006
              'left outer join table3 on table2.key3 = table3.key3']
2007
)->all;
2008
is_deeply($rows, [{table3__key4 => 4}]);
2009

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

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

            
2034
{
2035
    package MyDBI8;
2036
    
2037
    use base 'DBIx::Custom';
2038
    
2039
    sub connect {
2040
        my $self = shift->SUPER::connect(@_);
2041
        
2042
        $self->include_model('MyModel7');
2043
        
2044
        return $self;
2045
    }
2046
}
cleanup test
Yuki Kimoto authored on 2011-08-06
2047

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

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

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

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

            
2133
$result = $model->select_at(
2134
    column => [
2135
        $model->mycolumn(['key1']),
2136
        $model->column(table2 => ['key1'])
2137
    ]
2138
);
2139
is_deeply($result->one,
2140
          {key1 => 1, 'table2.key1' => 1});
2141
$result = $model->select_at(
2142
    column => [
2143
        $model->mycolumn(['key1']),
2144
        {table2 => ['key1']}
2145
    ]
2146
);
2147
is_deeply($result->one,
2148
          {key1 => 1, 'table2.key1' => 1});
2149

            
2150
$result = $model->select_at(
2151
    column => [
2152
        $model->mycolumn(['key1']),
2153
        ['table2.key1', as => 'table2.key1']
2154
    ]
2155
);
2156
is_deeply($result->one,
2157
          {key1 => 1, 'table2.key1' => 1});
2158

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

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

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

            
2205
$dbi->separator('__');
2206
$result = $model->select(
2207
    column => [
2208
        $model->column('table2', {alias => 'table2_alias'})
2209
    ],
2210
    where => {'table2_alias.key3' => 4}
2211
);
2212
is_deeply($result->one, 
2213
          {'table2_alias__key1' => 1, 'table2_alias__key3' => 4});
2214

            
2215
$dbi->separator('-');
2216
$result = $model->select(
2217
    column => [
2218
        $model->column('table2', {alias => 'table2_alias'})
2219
    ],
2220
    where => {'table2_alias.key3' => 4}
2221
);
2222
is_deeply($result->one, 
2223
          {'table2_alias-key1' => 1, 'table2_alias-key3' => 4});
2224

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

            
2242
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [['key1'] => DBI::SQL_BLOB]);
2243
$result = $dbi->select(table => 'table1');
2244
$row   = $result->one;
2245
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2246
$result = $dbi->execute('select length(key1) as key1_length from table1');
2247
$row = $result->one;
2248
is($row->{key1_length}, length $binary);
2249

            
2250

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

            
2268
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, bind_type => [['key1'] => DBI::SQL_BLOB]);
2269
$result = $dbi->select(table => 'table1');
2270
$row   = $result->one;
2271
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2272
$result = $dbi->execute('select length(key1) as key1_length from table1');
2273
$row = $result->one;
2274
is($row->{key1_length}, length $binary);
2275

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

            
2294
test 'create_model';
cleanup test
Yuki Kimoto authored on 2011-08-06
2295
$dbi = DBIx::Custom->connect(%memory);
2296
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2297
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2298

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

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

            
2338
test 'merge_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
2339
$dbi = DBIx::Custom->new;
2340
$params = [
2341
    {key1 => 1, key2 => 2, key3 => 3},
2342
    {key1 => 1, key2 => 2},
2343
    {key1 => 1}
2344
];
2345
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]);
2346
is_deeply($param, {key1 => [1, 1, 1], key2 => [2, 2], key3 => 3});
2347

            
2348
$params = [
2349
    {key1 => [1, 2], key2 => 1, key3 => [1, 2]},
2350
    {key1 => [3, 4], key2 => [2, 3], key3 => 3}
2351
];
2352
$param = $dbi->merge_param($params->[0], $params->[1]);
2353
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
2354

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

            
2373

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

            
2386
eval {
2387
$dbi->select(
2388
    table => 'table1',
2389
    column => 'key1',
2390
    wrap => 'select * from ('
2391
)
2392
};
2393
like($@, qr/array/);
2394

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

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

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

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

            
2447

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

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

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

            
2488
$dbi->delete_all(table => 'table1');
2489
$dbi->insert(
2490
    primary_key => 'key1', 
2491
    table => 'table1',
2492
    id => 0,
2493
    param => {key2 => 2, key3 => 3}
2494
);
2495

            
2496
is($dbi->select(table => 'table1')->one->{key1}, 0);
2497
is($dbi->select(table => 'table1')->one->{key2}, 2);
2498
is($dbi->select(table => 'table1')->one->{key3}, 3);
2499

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

            
2512

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

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

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

            
2552
$dbi->delete_all(table => 'table1');
2553
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2554
$dbi->update(
2555
    table => 'table1',
2556
    primary_key => 'key1',
2557
    id => 0,
2558
    param => {key3 => 4}
2559
);
2560
is($dbi->select(table => 'table1')->one->{key1}, 0);
2561
is($dbi->select(table => 'table1')->one->{key2}, 2);
2562
is($dbi->select(table => 'table1')->one->{key3}, 4);
2563

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

            
2577

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

            
2592

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

            
2604
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2605
$dbi->delete(
2606
    table => 'table1',
2607
    primary_key => 'key1',
2608
    id => 0,
2609
);
2610
is_deeply($dbi->select(table => 'table1')->all, []);
2611

            
2612

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

            
2628

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

            
2643
$dbi->delete_all(table => 'table1');
2644
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2645
$result = $dbi->select(
2646
    table => 'table1',
2647
    primary_key => 'key1',
2648
    id => 0,
2649
);
2650
$row = $result->one;
2651
is($row->{key1}, 0);
2652
is($row->{key2}, 2);
2653
is($row->{key3}, 3);
2654

            
2655
$dbi->delete_all(table => 'table1');
2656
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2657
$result = $dbi->select(
2658
    table => 'table1',
2659
    primary_key => ['key1', 'key2'],
2660
    id => [1, 2]
2661
);
2662
$row = $result->one;
2663
is($row->{key1}, 1);
2664
is($row->{key2}, 2);
2665
is($row->{key3}, 3);
2666

            
2667

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

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

            
2693
$result = $model->select(
2694
    column => [$model->column('table2' => [qw/key1 key3/])],
2695
    where => {'table1.key1' => 1}
2696
);
2697
is_deeply($result->one,
2698
          {'table2.key1' => 1, 'table2.key3' => 3});
2699

            
2700

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

            
2713
$result = $dbi->select(table => 'table1');
2714
is($result->one->{key1}, 'A');
2715

            
2716

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

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

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

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

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

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

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

            
2835
test 'type_rule_off';
cleanup test
Yuki Kimoto authored on 2011-08-06
2836
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2837
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2838
$dbi->type_rule(
2839
    from1 => {
2840
        date => sub { $_[0] * 2 },
2841
    },
2842
    into1 => {
2843
        date => sub { $_[0] * 2 },
2844
    }
2845
);
2846
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
2847
$result = $dbi->select(table => 'table1', type_rule_off => 1);
2848
is($result->type_rule_off->fetch->[0], 2);
2849

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

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

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

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

            
2904
eval{$dbi->type_rule(
2905
    into1 => {
2906
        date => 'pp'
2907
    }
2908
)};
2909
like($@, qr/not registered/);
2910

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

            
2922
eval {
2923
    $dbi->type_rule(
2924
        into1 => {
2925
            Date => sub { $_[0] * 2 },
2926
        }
2927
    );
2928
};
2929
like($@, qr/lower/);
2930

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

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

            
2965
$result = $dbi->select(table => 'table1');
2966
$result->type_rule(
2967
    from1 => {
2968
        date => sub { $_[0] * 3 }
2969
    }
2970
);
2971
$row = $result->one;
2972
is($row->{key1}, 6);
2973
is($row->{key2}, 2);
2974

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

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

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

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

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

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

            
3097
test 'separator';
cleanup test
Yuki Kimoto authored on 2011-08-06
3098
$dbi = DBIx::Custom->connect(%memory);
3099
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
3100
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
3101

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

            
3127
$dbi->separator('__');
3128
$model = $dbi->model('table1');
3129
$result = $model->select(
3130
    column => [
3131
        $model->mycolumn,
3132
        {table2 => [qw/key1 key3/]}
3133
    ],
3134
    where => {'table1.key1' => 1}
3135
);
3136
is_deeply($result->one,
3137
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
3138
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3139

            
3140
$dbi->separator('-');
3141
$model = $dbi->model('table1');
3142
$result = $model->select(
3143
    column => [
3144
        $model->mycolumn,
3145
        {table2 => [qw/key1 key3/]}
3146
    ],
3147
    where => {'table1.key1' => 1}
3148
);
3149
is_deeply($result->one,
3150
          {key1 => 1, key2 => 2, 'table2-key1' => 1, 'table2-key3' => 3});
3151
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3152

            
3153

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

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

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

            
3177

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

            
3185

            
3186
test 'separator';
cleanup test
Yuki Kimoto authored on 2011-08-06
3187
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3188
is($dbi->separator, '.');
3189
$dbi->separator('-');
3190
is($dbi->separator, '-');
3191
$dbi->separator('__');
3192
is($dbi->separator, '__');
3193
eval { $dbi->separator('?') };
3194
like($@, qr/Separator/);
3195

            
3196

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

            
3208
$param = $dbi->map_param(
3209
    {id => 0, author => 0, price => 0},
3210
    id => 'book.id',
3211
    author => ['book.author', sub { '%' . $_[0] . '%' }],
3212
    price => ['book.price', sub { '%' . $_[0] . '%' },
3213
      {if => sub { $_[0] eq 0 }}]
3214
);
3215
is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
3216

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

            
3226
$param = $dbi->map_param(
3227
    {id => undef, author => undef, price => undef},
3228
    id => 'book.id',
3229
    price => ['book.price', {if => 'exists'}]
3230
);
3231
is_deeply($param, {'book.price' => undef});
3232

            
3233
$param = $dbi->map_param(
3234
    {price => 'a'},
3235
    id => ['book.id', {if => 'exists'}],
3236
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
3237
);
3238
is_deeply($param, {'book.price' => '%a'});
3239

            
3240

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

            
3254

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

            
3272
$order = $dbi->order;
3273
$order->prepend(['table1-key1'], [qw/table1-key2 desc/]);
3274
$result = $dbi->select(table => 'table1',
3275
  column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']],
3276
  append => "$order");
3277
is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3},
3278
  {'table1-key1' => 1, 'table1-key2' => 1},
3279
  {'table1-key1' => 2, 'table1-key2' => 4},
3280
  {'table1-key1' => 2, 'table1-key2' => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
3281

            
3282
test 'tag_parse';
cleanup test
Yuki Kimoto authored on 2011-08-06
3283
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3284
$dbi->tag_parse(0);
cleanup test
Yuki Kimoto authored on 2011-08-06
3285
$dbi->execute("create table table1 (key1, key2)");
3286
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
3287
eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})};
3288
ok($@);
cleanup test
Yuki Kimoto authored on 2011-08-06
3289

            
3290
test 'last_sql';
cleanup test
Yuki Kimoto authored on 2011-08-06
3291
$dbi = DBIx::Custom->connect(%memory);
3292
$dbi->execute("create table table1 (key1, key2)");
3293
$dbi->execute('select * from table1');
3294
is($dbi->last_sql, 'select * from table1;');
3295

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

            
3299
test 'DBIx::Custom header';
cleanup test
Yuki Kimoto authored on 2011-08-06
3300
$dbi = DBIx::Custom->connect(%memory);
3301
$dbi->execute("create table table1 (key1, key2)");
3302
$result = $dbi->execute('select key1 as h1, key2 as h2 from table1');
3303
is_deeply($result->header, [qw/h1 h2/]);
cleanup test
Yuki Kimoto authored on 2011-08-06
3304

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

            
3311
$source = "select * from table1 where :key1{=} and :key2{=}";
3312
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
3313
$rows = $result->all;
3314
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3315

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

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

            
3326
$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}";
3327
$result = $dbi->execute(
3328
    $source,
3329
    param => {'table1.key1' => 1, 'table1.key2' => 1},
3330
    filter => {'table1.key2' => sub { $_[0] * 2 }}
3331
);
3332
$rows = $result->all;
3333
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3334

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

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

            
clenup test
Yuki Kimoto authored on 2011-08-06
3378
test 'result';
3379
$dbi = DBIx::Custom->connect(%memory);
3380
$dbi->execute($create_table_default);
3381
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3382
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3383

            
3384
$result = $dbi->select(table => 'table1');
3385
@rows = ();
3386
while (my $row = $result->fetch) {
3387
    push @rows, [@$row];
3388
}
3389
is_deeply(\@rows, [[1, 2], [3, 4]]);
3390

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

            
3398
$result = $dbi->select(table => 'table1');
3399
$row = $result->fetch_first;
3400
is_deeply($row, [1, 2], "row");
3401
$row = $result->fetch;
3402
ok(!$row, "finished");
3403

            
3404
$result = $dbi->select(table => 'table1');
3405
$row = $result->fetch_hash_first;
3406
is_deeply($row, {key1 => 1, key2 => 2}, "row");
3407
$row = $result->fetch_hash;
3408
ok(!$row, "finished");
3409

            
3410
$dbi->execute('create table table2 (key1, key2);');
3411
$result = $dbi->select(table => 'table2');
3412
$row = $result->fetch_hash_first;
3413
ok(!$row, "no row fetch");
3414

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

            
3434
$result = $dbi->select(table => 'table1');
3435
eval {$result->fetch_multi};
3436
like($@, qr/Row count must be specified/, "Not specified row count");
3437

            
3438
$result = $dbi->select(table => 'table1');
3439
$rows = $result->fetch_hash_multi(2);
3440
is_deeply($rows, [{key1 => 1, key2 => 2},
3441
                  {key1 => 3, key2 => 4}], "fetch_multi first");
3442
$rows = $result->fetch_hash_multi(2);
3443
is_deeply($rows, [{key1 => 5, key2 => 6},
3444
                  {key1 => 7, key2 => 8}], "fetch_multi secound");
3445
$rows = $result->fetch_hash_multi(2);
3446
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third");
3447
$rows = $result->fetch_hash_multi(2);
3448
ok(!$rows);
3449

            
3450
$result = $dbi->select(table => 'table1');
3451
eval {$result->fetch_hash_multi};
3452
like($@, qr/Row count must be specified/, "Not specified row count");
3453

            
3454
$dbi = DBIx::Custom->connect(%memory);
3455
$dbi->execute($create_table_default);
3456
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3457
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3458

            
3459
test 'fetch_all';
3460
$result = $dbi->select(table => 'table1');
3461
$rows = $result->fetch_all;
3462
is_deeply($rows, [[1, 2], [3, 4]]);
3463

            
3464
$result = $dbi->select(table => 'table1');
3465
$rows = $result->fetch_hash_all;
3466
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
3467

            
3468
$result = $dbi->select(table => 'table1');
3469
$result->dbi->filters({three_times => sub { $_[0] * 3}});
3470
$result->filter({key1 => 'three_times'});
3471

            
3472
$rows = $result->fetch_all;
3473
is_deeply($rows, [[3, 2], [9, 4]], "array");
3474

            
3475
$result = $dbi->select(table => 'table1');
3476
$result->dbi->filters({three_times => sub { $_[0] * 3}});
3477
$result->filter({key1 => 'three_times'});
3478
$rows = $result->fetch_hash_all;
3479
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash");
3480

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

            
3517
for (my $i = 0; $i < @$datas; $i++) {
3518
    my $data = $datas->[$i];
3519
    my $builder = DBIx::Custom->new->query_builder;
3520
    my $query = $builder->build_query($data->{source});
3521
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
3522
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
3523
}
3524

            
3525
$builder = DBIx::Custom->new->query_builder;
3526
$ret_val = $builder->register_tag(
3527
    p => sub {
3528
        my @args = @_;
3529
        
3530
        my $expand    = "? $args[0] $args[1]";
3531
        my $columns = [2];
3532
        return [$expand, $columns];
3533
    }
3534
);
3535

            
3536
$query = $builder->build_query("{p a b}");
3537
is($query->{sql}, "? a b;", "register_tag sql");
3538
is_deeply($query->{columns}, [2], "register_tag columns");
3539
isa_ok($ret_val, 'DBIx::Custom::QueryBuilder');
3540

            
3541
$builder = DBIx::Custom->new->query_builder;
3542

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

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

            
3549
$builder->register_tag({
3550
    q => 'string'
3551
});
3552

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

            
3556
$builder->register_tag({
3557
   r => sub {} 
3558
});
3559

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

            
3563
$builder->register_tag({
3564
   s => sub { return ["a", ""]} 
3565
});
3566

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

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

            
3574

            
3575
test 'General error case';
3576
$builder = DBIx::Custom->new->query_builder;
3577
$builder->register_tag(
3578
    a => sub {
3579
        return ["? ? ?", ['']];
3580
    }
3581
);
3582
eval{$builder->build_query("{a}")};
3583
like($@, qr/\QPlaceholder count/, "placeholder count is invalid");
3584

            
3585

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

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

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

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

            
3601
test 'variouse source';
3602
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;";
3603
$query = $builder->build_query($source);
3604
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1");
3605

            
3606
$source = "abc;";
3607
$query = $builder->build_query($source);
3608
is($query->sql, 'abc;', "basic : 2");
3609

            
3610
$source = "{= a}";
3611
$query = $builder->build_query($source);
3612
is($query->sql, 'a = ?;', "only tag");
3613

            
3614
$source = "000;";
3615
$query = $builder->build_query($source);
3616
is($query->sql, '000;', "contain 0 value");
3617

            
3618
$source = "a {= b} }";
3619
eval{$builder->build_query($source)};
3620
like($@, qr/unexpected "}"/, "error : 1");
3621

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