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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
22
{
23
    package DBIx::Custom;
24
    has dsn => sub { 'dbi:SQLite:dbname=:memory:' }
25
}
26

            
cleanup test
Yuki Kimoto authored on 2011-08-06
27
# Constant
28
my %memory = (dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-10
29
my $create_table1 = 'create table table1 (key1 char(255), key2 char(255));';
30
my $create_table1_2 = 'create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));';
test cleanup
Yuki Kimoto authored on 2011-08-10
31
my $q = '"';
32
my $p = '"';
cleanup test
Yuki Kimoto authored on 2011-08-06
33

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

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
68
=pod
cleanup test
Yuki Kimoto authored on 2011-08-06
69

            
70
test 'insert';
test cleanup
Yuki Kimoto authored on 2011-08-10
71
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
72
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
73
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
74
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-06
75
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
76
$rows   = $result->all;
77
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
78

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

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

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

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

            
103
$dbi->quote('"');
104
$dbi->execute('create table "table" ("select")');
105
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
106
$dbi->insert(table => 'table', param => {select => 1});
107
$result = $dbi->execute('select * from "table"');
108
$rows   = $result->all;
109
is_deeply($rows, [{select => 2}], "reserved word");
110

            
test cleanup
Yuki Kimoto authored on 2011-08-10
111
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
112
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
113
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
114
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
test cleanup
Yuki Kimoto authored on 2011-08-06
115
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
116
$rows   = $result->all;
117
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
118

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
127
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
128
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
129
$dbi->insert(table => 'table1', param => {key1 => \"'1'", key2 => 2});
130
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-06
131
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
132
$rows   = $result->all;
133
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
134

            
test cleanup
Yuki Kimoto authored on 2011-08-10
135
=cut
136

            
cleanup test
Yuki Kimoto authored on 2011-08-06
137
test 'update';
test cleanup
Yuki Kimoto authored on 2011-08-10
138
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
139
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
140
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
141
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
142
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
143
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
144
$rows   = $result->all;
145
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
146
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
147
                  "basic");
148
                  
149
$dbi->execute("delete from table1");
150
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
151
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
152
$dbi->update(table => 'table1', param => {key2 => 12}, where => {key2 => 2, key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-06
153
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
154
$rows   = $result->all;
155
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
156
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
157
                  "update key same as search key");
158

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

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

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

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

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
186
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
187
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
188
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
189
$where = $dbi->where;
190
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
191
$where->param({key1 => 1, key2 => 2});
192
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
193
$result = $dbi->select(table => 'table1');
194
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
195

            
test cleanup
Yuki Kimoto authored on 2011-08-10
196
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
197
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
198
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
199
$dbi->update(
200
    table => 'table1',
201
    param => {key1 => 3},
202
    where => [
203
        ['and', 'key1 = :key1', 'key2 = :key2'],
204
        {key1 => 1, key2 => 2}
205
    ]
206
);
207
$result = $dbi->select(table => 'table1');
208
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
209

            
test cleanup
Yuki Kimoto authored on 2011-08-10
210
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
211
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
212
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
213
$where = $dbi->where;
214
$where->clause(['and', 'key2 = :key2']);
215
$where->param({key2 => 2});
216
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
217
$result = $dbi->select(table => 'table1');
218
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
219

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

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
226
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
227
$dbi->quote('"');
228
$dbi->execute('create table "table" ("select", "update")');
229
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
230
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
231
$dbi->insert(table => 'table', param => {select => 1});
232
$dbi->update(table => 'table', where => {select => 1}, param => {update => 2});
233
$result = $dbi->execute('select * from "table"');
234
$rows   = $result->all;
235
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
236

            
237
eval {$dbi->update_all(table => 'table', param => {';' => 2}) };
238
like($@, qr/safety/);
239

            
test cleanup
Yuki Kimoto authored on 2011-08-10
240
eval { $dbi->execute("drop table ${q}table$p") };
cleanup test
Yuki Kimoto authored on 2011-08-06
241
$dbi->reserved_word_quote('"');
242
$dbi->execute('create table "table" ("select", "update")');
243
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
244
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
245
$dbi->insert(table => 'table', param => {select => 1});
246
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
247
$result = $dbi->execute('select * from "table"');
248
$rows   = $result->all;
249
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
250

            
test cleanup
Yuki Kimoto authored on 2011-08-10
251
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
252
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
253
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
254
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
255
$dbi->update({key2 => 11}, table => 'table1', where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
256
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
257
$rows   = $result->all;
258
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
259
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
260
                  "basic");
261

            
test cleanup
Yuki Kimoto authored on 2011-08-10
262
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
263
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
264
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
265
$dbi->update(table => 'table1', param => {key2 => 4},
266
  where => {key1 => 1}, prefix => 'or replace');
test cleanup
Yuki Kimoto authored on 2011-08-06
267
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
268
$rows   = $result->all;
269
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
270

            
test cleanup
Yuki Kimoto authored on 2011-08-10
271
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
272
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
273
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
274
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
275
$dbi->update(table => 'table1', param => {key2 => \"'11'"}, where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
276
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
277
$rows   = $result->all;
278
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
279
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
280
                  "basic");
281

            
282
test 'update_all';
test cleanup
Yuki Kimoto authored on 2011-08-10
283
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
284
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
285
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
286
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
287
$dbi->register_filter(twice => sub { $_[0] * 2 });
288
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'});
test cleanup
Yuki Kimoto authored on 2011-08-06
289
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
290
$rows   = $result->all;
291
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
292
                  {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
293
                  "filter");
294

            
295

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

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

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

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

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
327
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
328
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
329
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
330
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
331
$where = $dbi->where;
332
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
333
$where->param({ke1 => 1, key2 => 2});
334
$dbi->delete(table => 'table1', where => $where);
335
$result = $dbi->select(table => 'table1');
336
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
337

            
test cleanup
Yuki Kimoto authored on 2011-08-10
338
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
339
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
340
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
341
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
342
$dbi->delete(
343
    table => 'table1',
344
    where => [
345
        ['and', 'key1 = :key1', 'key2 = :key2'],
346
        {ke1 => 1, key2 => 2}
347
    ]
348
);
349
$result = $dbi->select(table => 'table1');
350
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
351

            
test cleanup
Yuki Kimoto authored on 2011-08-10
352
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
353
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
354
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
355
$dbi->delete(table => 'table1', where => {key1 => 1}, prefix => '    ');
test cleanup
Yuki Kimoto authored on 2011-08-06
356
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
357
$rows   = $result->all;
358
is_deeply($rows, [], "basic");
359

            
360
test 'delete error';
test cleanup
Yuki Kimoto authored on 2011-08-10
361
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
362
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
363
eval{$dbi->delete(table => 'table1')};
364
like($@, qr/"where" must be specified/,
365
         "where key-value pairs not specified");
366

            
367
eval{$dbi->delete(table => 'table1', where => {';' => 1})};
368
like($@, qr/safety/);
369

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

            
380
test 'delete_all';
test cleanup
Yuki Kimoto authored on 2011-08-10
381
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
382
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
383
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
384
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
385
$dbi->delete_all(table => 'table1');
test cleanup
Yuki Kimoto authored on 2011-08-06
386
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
387
$rows   = $result->all;
388
is_deeply($rows, [], "basic");
389

            
390

            
391
test 'select';
test cleanup
Yuki Kimoto authored on 2011-08-10
392
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
393
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
394
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
395
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
396
$rows = $dbi->select(table => 'table1')->all;
397
is_deeply($rows, [{key1 => 1, key2 => 2},
398
                  {key1 => 3, key2 => 4}], "table");
399

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

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

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

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

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

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

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

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

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

            
446
test 'fetch filter';
test cleanup
Yuki Kimoto authored on 2011-08-10
447
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
448
$dbi->register_filter(
449
    twice       => sub { $_[0] * 2 },
450
    three_times => sub { $_[0] * 3 }
451
);
452
$dbi->default_fetch_filter('twice');
cleanup test
Yuki Kimoto authored on 2011-08-10
453
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
454
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
455
$result = $dbi->select(table => 'table1');
456
$result->filter({key1 => 'three_times'});
457
$row = $result->one;
458
is_deeply($row, {key1 => 3, key2 => 4}, "default_fetch_filter and filter");
459

            
460
test 'filters';
461
$dbi = DBIx::Custom->new;
462

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

            
466
is($dbi->filters->{encode_utf8}->('あ'),
467
   encode_utf8('あ'), "encode_utf8");
468

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

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

            
486
$result = $dbi->select(table => 'table1');
487
ok(! $result->fetch_first, "rollback");
488

            
489
test 'cache';
test cleanup
Yuki Kimoto authored on 2011-08-10
490
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
491
$dbi->cache(1);
cleanup test
Yuki Kimoto authored on 2011-08-10
492
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
493
$source = 'select * from table1 where key1 = :key1 and key2 = :key2;';
494
$dbi->execute($source, {}, query => 1);
495
is_deeply($dbi->{_cached}->{$source}, 
496
          {sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2'], tables => []}, "cache");
497

            
test cleanup
Yuki Kimoto authored on 2011-08-10
498
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
499
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
500
$dbi->{_cached} = {};
501
$dbi->cache(0);
502
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
503
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
504

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

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

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

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

            
539

            
540
test 'transaction';
cleanup test
Yuki Kimoto authored on 2011-08-06
541
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
542
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
543

            
544
$dbi->begin_work;
545

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

            
552
$dbi->rollback if $@;
553

            
554
$result = $dbi->select(table => 'table1');
555
$rows = $result->all;
556
is_deeply($rows, [], "rollback");
557

            
558
$dbi->begin_work;
559

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

            
565
$dbi->commit unless $@;
566

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

            
571
$dbi->dbh->{AutoCommit} = 0;
572
eval{ $dbi->begin_work };
573
ok($@, "exception");
574
$dbi->dbh->{AutoCommit} = 1;
575

            
576

            
577
test 'method';
578
$dbi->method(
579
    one => sub { 1 }
580
);
581
$dbi->method(
582
    two => sub { 2 }
583
);
584
$dbi->method({
585
    twice => sub {
586
        my $self = shift;
587
        return $_[0] * 2;
588
    }
589
});
590

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

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

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

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

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

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

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

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

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

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

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

            
715
$result = $dbi->select(
716
     table => ['table1', 'table2'],
717
     column => ['key2', 'key3'],
718
     where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
719

            
720
$result->filter({'key2' => 'twice'});
721
$rows   = $result->all;
722
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join : omit");
723

            
724
test 'each_column';
cleanup test
Yuki Kimoto authored on 2011-08-06
725
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
726
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
727
$dbi->execute('create table table1 (key1 Date, key2 datetime);');
cleanup test
Yuki Kimoto authored on 2011-08-06
728

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

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

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
829
$dbi = MyDBI->new(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
830
$dbi->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
831
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
832
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
833
is($dbi->select(table => 'table1')->one->{key1}, 1);
834

            
835
{
836
    package MyDBI2;
837
    
838
    use base 'DBIx::Custom';
839
    sub connect {
840
        my $self = shift->SUPER::new(@_);
841
        $self->connect;
842
        
843
        return $self;
844
    }
845
}
846

            
cleanup test
Yuki Kimoto authored on 2011-08-06
847
$dbi = MyDBI->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
848
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
849
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
850
is($dbi->select(table => 'table1')->one->{key1}, 1);
851

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

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

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

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

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

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

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

            
922
test 'empty where select';
cleanup test
Yuki Kimoto authored on 2011-08-06
923
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
924
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
925
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
926
$result = $dbi->select(table => 'table1', where => {});
927
$row = $result->one;
928
is_deeply($row, {key1 => 1, key2 => 2});
929

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

            
map cleanup
Yuki Kimoto authored on 2011-08-09
942
test 'where';
cleanup test
Yuki Kimoto authored on 2011-08-06
943
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
944
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
945
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
946
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
947
$where = $dbi->where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
948
is("$where", "where ( key1 = :key1 and key2 = :key2 )", 'no param');
949

            
950
$where = $dbi->where
951
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
952
             ->param({key1 => 1});
953

            
954
$result = $dbi->select(
955
    table => 'table1',
956
    where => $where
957
);
958
$row = $result->all;
959
is_deeply($row, [{key1 => 1, key2 => 2}]);
960

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

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

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

            
991
$where = $dbi->where
992
             ->clause(['and', ['or', 'key1 > :key1', 'key1 < :key1'], 'key2 = :key2'])
993
             ->param({key1 => [0, 3], key2 => 2});
994
$result = $dbi->select(
995
    table => 'table1',
996
    where => $where,
997
); 
998
$row = $result->all;
999
is_deeply($row, [{key1 => 1, key2 => 2}]);
1000

            
1001
$where = $dbi->where;
1002
$result = $dbi->select(
1003
    table => 'table1',
1004
    where => $where
1005
);
1006
$row = $result->all;
1007
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1008

            
1009
eval {
1010
$where = $dbi->where
1011
             ->clause(['uuu']);
1012
$result = $dbi->select(
1013
    table => 'table1',
1014
    where => $where
1015
);
1016
};
1017
ok($@);
1018

            
1019
$where = $dbi->where;
1020
is("$where", '');
1021

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

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

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

            
1052
$where = $dbi->where
1053
             ->clause('key1 = :key1')
1054
             ->param({key1 => 1});
1055
$result = $dbi->select(
1056
    table => 'table1',
1057
    where => $where,
1058
);
1059
$row = $result->all;
1060
is_deeply($row, [{key1 => 1, key2 => 2}]);
1061

            
1062
$where = $dbi->where
1063
             ->clause('key1 = :key1 key2 = :key2')
1064
             ->param({key1 => 1});
1065
eval{$where->to_string};
1066
like($@, qr/one column/);
1067

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

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

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

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

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

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

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

            
1138
$where = $dbi->where
1139
             ->clause(['or', ('key1 = :key1') x 3])
1140
             ->param({key1 => []});
1141
$result = $dbi->select(
1142
    table => 'table1',
1143
    where => $where,
1144
);
1145
$row = $result->all;
1146
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1147

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

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

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

            
1178
$where = $dbi->where
1179
             ->clause(['and', '{> key1}', '{< key1}' ])
1180
             ->param({key1 => [0, 2]});
1181
$result = $dbi->select(
1182
    table => 'table1',
1183
    where => $where,
1184
);
1185
$row = $result->all;
1186
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1187

            
1188
$where = $dbi->where
1189
             ->clause(['and', 'key1 is not null', 'key2 is not null' ]);
1190
$result = $dbi->select(
1191
    table => 'table1',
1192
    where => $where,
1193
);
1194
$row = $result->all;
1195
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1196

            
1197
eval {$dbi->where(ppp => 1) };
1198
like($@, qr/invalid/);
1199

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

            
1211

            
1212
$where = $dbi->where(
1213
    clause => ['and', ['or'], ['or', ':key1', ':key2']],
1214
    param => {}
1215
);
1216
$result = $dbi->select(
1217
    table => 'table1',
1218
    where => $where,
1219
);
1220
$row = $result->all;
1221
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1222

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

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

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

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

            
1261
$where = $dbi->where;
1262
$where->clause(['and', ':key1{=}']);
1263
$where->param({key1 => 0});
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
1264
$where->if('length');
map cleanup
Yuki Kimoto authored on 2011-08-09
1265
$where->map;
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}]);
1269

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

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

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

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

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

            
1316
$where = $dbi->where;
1317
$where->param({id => '', author => '', price => ''});
1318
$where->if('length');
1319
$where->map(
1320
    id => 'book.id',
1321
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1322
    price => ['book.price', sub { '%' . $_[0] . '%' },
1323
      {if => sub { $_[0] eq 1 }}]
1324
);
1325
is_deeply($where->param, {});
1326

            
1327
$where = $dbi->where;
1328
$where->param({id => undef, author => undef, price => undef});
1329
$where->if('length');
1330
$where->map(
1331
    id => 'book.id',
1332
    price => ['book.price', {if => 'exists'}]
1333
);
1334
is_deeply($where->param, {'book.price' => undef});
1335

            
1336
$where = $dbi->where;
1337
$where->param({price => 'a'});
1338
$where->if('length');
1339
$where->map(
1340
    id => ['book.id', {if => 'exists'}],
1341
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
1342
);
1343
is_deeply($where->param, {'book.price' => '%a'});
1344

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

            
1355
$where = $dbi->where;
1356
$where->if('length');
1357
$where->param({id => ['', ''], author => 'Ken', price => 1900});
1358
$where->map(
1359
    id => 'book.id',
1360
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1361
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1362
);
1363
is_deeply($where->param, {'book.id' => [$dbi->not_exists, $dbi->not_exists], 'book.author' => '%Ken%',
1364
  'book.price' => 1900});
1365

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

            
1376
test 'dbi_option default';
1377
$dbi = DBIx::Custom->new;
1378
is_deeply($dbi->dbi_option, {});
1379

            
1380
test 'register_tag_processor';
cleanup test
Yuki Kimoto authored on 2011-08-06
1381
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1382
$dbi->register_tag_processor(
1383
    a => sub { 1 }
1384
);
1385
is($dbi->query_builder->tag_processors->{a}->(), 1);
1386

            
1387
test 'register_tag';
cleanup test
Yuki Kimoto authored on 2011-08-06
1388
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1389
$dbi->register_tag(
1390
    b => sub { 2 }
1391
);
1392
is($dbi->query_builder->tags->{b}->(), 2);
1393

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

            
1405

            
1406
test 'more tests';
cleanup test
Yuki Kimoto authored on 2011-08-06
1407
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1408
eval{$dbi->apply_filter('table', 'column', [])};
1409
like($@, qr/apply_filter/);
1410

            
1411
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
1412
like($@, qr/apply_filter/);
1413

            
1414
$dbi->apply_filter(
1415

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1426
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1427
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1428
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1429
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1430
$dbi->apply_filter('table1', 'key2', {});
1431
$rows = $dbi->select(table => 'table1', where => {key2 => 2})->all;
1432
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1433

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1442
eval{DBIx::Custom->connect(dsn => undef)};
cleanup test
Yuki Kimoto authored on 2011-08-06
1443
like($@, qr/_connect/);
1444

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

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

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

            
1485
test 'dbi_option';
cleanup test
Yuki Kimoto authored on 2011-08-06
1486
$dbi = DBIx::Custom->connect(%memory,
cleanup test
Yuki Kimoto authored on 2011-08-06
1487
                             dbi_option => {PrintError => 1});
1488
ok($dbi->dbh->{PrintError});
cleanup test
Yuki Kimoto authored on 2011-08-06
1489
$dbi = DBIx::Custom->connect(%memory,
cleanup test
Yuki Kimoto authored on 2011-08-06
1490
                             dbi_options => {PrintError => 1});
1491
ok($dbi->dbh->{PrintError});
1492

            
1493
test 'DBIx::Custom::Result stash()';
1494
$result = DBIx::Custom::Result->new;
1495
is_deeply($result->stash, {}, 'default');
1496
$result->stash->{foo} = 1;
1497
is($result->stash->{foo}, 1, 'get and set');
1498

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

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

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

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

            
1522
$result = $dbi->select(
1523
    table => 'company', relation => {'company.location_id' => 'location.id'},
1524
    column => ['location.name as "location.name"']
1525
);
1526
is($result->fetch_first->[0], 'B');
1527

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

            
1542
{
1543
    package MyDBI4;
1544

            
1545
    use strict;
1546
    use warnings;
1547

            
1548
    use base 'DBIx::Custom';
1549

            
1550
    sub connect {
1551
        my $self = shift->SUPER::connect(@_);
1552
        
1553
        $self->include_model(
1554
            MyModel2 => [
1555
                'book',
1556
                {class => 'Company', name => 'company'}
1557
            ]
1558
        );
1559
    }
1560

            
1561
    package MyModel2::Base1;
1562

            
1563
    use strict;
1564
    use warnings;
1565

            
1566
    use base 'DBIx::Custom::Model';
1567

            
1568
    package MyModel2::book;
1569

            
1570
    use strict;
1571
    use warnings;
1572

            
1573
    use base 'MyModel2::Base1';
1574

            
1575
    sub insert {
1576
        my ($self, $param) = @_;
1577
        
1578
        return $self->SUPER::insert(param => $param);
1579
    }
1580

            
1581
    sub list { shift->select; }
1582

            
1583
    package MyModel2::Company;
1584

            
1585
    use strict;
1586
    use warnings;
1587

            
1588
    use base 'MyModel2::Base1';
1589

            
1590
    sub insert {
1591
        my ($self, $param) = @_;
1592
        
1593
        return $self->SUPER::insert(param => $param);
1594
    }
1595

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

            
1608
{
1609
     package MyDBI5;
1610

            
1611
    use strict;
1612
    use warnings;
1613

            
1614
    use base 'DBIx::Custom';
1615

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

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

            
1639
test 'columns';
1640
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1641
$dbi = MyDBI1->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1642
$model = $dbi->model('book');
1643
$model->columns(['id', 'number']);
1644
is_deeply($model->columns, ['id', 'number']);
1645

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

            
1656
test 'delete_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1657
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1658
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1659
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1660
$dbi->delete_at(
1661
    table => 'table1',
1662
    primary_key => ['key1', 'key2'],
1663
    where => [1, 2],
1664
);
1665
is_deeply($dbi->select(table => 'table1')->all, []);
1666

            
1667
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1668
$dbi->delete_at(
1669
    table => 'table1',
1670
    primary_key => 'key1',
1671
    where => 1,
1672
);
1673
is_deeply($dbi->select(table => 'table1')->all, []);
1674

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

            
1688
$dbi->delete_all(table => 'table1');
1689
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1690
$dbi->insert_at(
1691
    primary_key => 'key1', 
1692
    table => 'table1',
1693
    where => 1,
1694
    param => {key2 => 2, key3 => 3}
1695
);
1696

            
1697
is($dbi->select(table => 'table1')->one->{key1}, 1);
1698
is($dbi->select(table => 'table1')->one->{key2}, 2);
1699
is($dbi->select(table => 'table1')->one->{key3}, 3);
1700

            
1701
eval {
1702
    $dbi->insert_at(
1703
        table => 'table1',
1704
        primary_key => ['key1', 'key2'],
1705
        where => {},
1706
        param => {key1 => 1, key2 => 2, key3 => 3},
1707
    );
1708
};
1709
like($@, qr/must be/);
1710

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1711
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1712
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1713
$dbi->insert_at(
1714
    {key3 => 3},
1715
    primary_key => ['key1', 'key2'], 
1716
    table => 'table1',
1717
    where => [1, 2],
1718
);
1719
is($dbi->select(table => 'table1')->one->{key1}, 1);
1720
is($dbi->select(table => 'table1')->one->{key2}, 2);
1721
is($dbi->select(table => 'table1')->one->{key3}, 3);
1722

            
1723
test 'update_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1724
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1725
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1726
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1727
$dbi->update_at(
1728
    table => 'table1',
1729
    primary_key => ['key1', 'key2'],
1730
    where => [1, 2],
1731
    param => {key3 => 4}
1732
);
1733
is($dbi->select(table => 'table1')->one->{key1}, 1);
1734
is($dbi->select(table => 'table1')->one->{key2}, 2);
1735
is($dbi->select(table => 'table1')->one->{key3}, 4);
1736

            
1737
$dbi->delete_all(table => 'table1');
1738
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1739
$dbi->update_at(
1740
    table => 'table1',
1741
    primary_key => 'key1',
1742
    where => 1,
1743
    param => {key3 => 4}
1744
);
1745
is($dbi->select(table => 'table1')->one->{key1}, 1);
1746
is($dbi->select(table => 'table1')->one->{key2}, 2);
1747
is($dbi->select(table => 'table1')->one->{key3}, 4);
1748

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1749
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1750
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1751
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1752
$dbi->update_at(
1753
    {key3 => 4},
1754
    table => 'table1',
1755
    primary_key => ['key1', 'key2'],
1756
    where => [1, 2]
1757
);
1758
is($dbi->select(table => 'table1')->one->{key1}, 1);
1759
is($dbi->select(table => 'table1')->one->{key2}, 2);
1760
is($dbi->select(table => 'table1')->one->{key3}, 4);
1761

            
1762
test 'select_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1763
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1764
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1765
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1766
$result = $dbi->select_at(
1767
    table => 'table1',
1768
    primary_key => ['key1', 'key2'],
1769
    where => [1, 2]
1770
);
1771
$row = $result->one;
1772
is($row->{key1}, 1);
1773
is($row->{key2}, 2);
1774
is($row->{key3}, 3);
1775

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

            
1788
$dbi->delete_all(table => 'table1');
1789
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1790
$result = $dbi->select_at(
1791
    table => 'table1',
1792
    primary_key => ['key1', 'key2'],
1793
    where => [1, 2]
1794
);
1795
$row = $result->one;
1796
is($row->{key1}, 1);
1797
is($row->{key2}, 2);
1798
is($row->{key3}, 3);
1799

            
1800
eval {
1801
    $result = $dbi->select_at(
1802
        table => 'table1',
1803
        primary_key => ['key1', 'key2'],
1804
        where => {},
1805
    );
1806
};
1807
like($@, qr/must be/);
1808

            
1809
eval {
1810
    $result = $dbi->select_at(
1811
        table => 'table1',
1812
        primary_key => ['key1', 'key2'],
1813
        where => [1],
1814
    );
1815
};
1816
like($@, qr/same/);
1817

            
1818
eval {
1819
    $result = $dbi->update_at(
1820
        table => 'table1',
1821
        primary_key => ['key1', 'key2'],
1822
        where => {},
1823
        param => {key1 => 1, key2 => 2},
1824
    );
1825
};
1826
like($@, qr/must be/);
1827

            
1828
eval {
1829
    $result = $dbi->delete_at(
1830
        table => 'table1',
1831
        primary_key => ['key1', 'key2'],
1832
        where => {},
1833
    );
1834
};
1835
like($@, qr/must be/);
1836

            
1837
test 'columns';
1838
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1839
$dbi = MyDBI1->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1840
$model = $dbi->model('book');
1841

            
1842

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

            
1871
test 'model insert_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1872
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1873
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1874
$dbi->model('table1')->insert_at(
1875
    where => [1, 2],
1876
    param => {key3 => 3}
1877
);
1878
$result = $dbi->model('table1')->select;
1879
$row = $result->one;
1880
is($row->{key1}, 1);
1881
is($row->{key2}, 2);
1882
is($row->{key3}, 3);
1883

            
1884
test 'model update_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1885
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1886
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1887
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1888
$dbi->model('table1')->update_at(
1889
    where => [1, 2],
1890
    param => {key3 => 4}
1891
);
1892
$result = $dbi->model('table1')->select;
1893
$row = $result->one;
1894
is($row->{key1}, 1);
1895
is($row->{key2}, 2);
1896
is($row->{key3}, 4);
1897

            
1898
test 'model select_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1899
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1900
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1901
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1902
$result = $dbi->model('table1')->select_at(where => [1, 2]);
1903
$row = $result->one;
1904
is($row->{key1}, 1);
1905
is($row->{key2}, 2);
1906
is($row->{key3}, 3);
1907

            
1908

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

            
1939
test 'update_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
1940
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1941
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1942
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1943
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1944

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

            
1958

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1959
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1960
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1961
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1962
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1963

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1977
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
1978
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1979
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1980
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1981

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

            
1995
            
1996
eval { $dbi->update_param({";" => 1}) };
1997
like($@, qr/not safety/);
1998

            
1999

            
2000
test 'update_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
2001
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2002
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2003
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
2004
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
2005

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

            
2019

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2032
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2033
$dbi->quote('"');
cleanup test
Yuki Kimoto authored on 2011-08-10
2034
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2035
$param = {key1 => 1, key2 => 2};
2036
$insert_param = $dbi->insert_param($param);
2037
$sql = <<"EOS";
2038
insert into table1 $insert_param
2039
EOS
2040
$dbi->execute($sql, param => $param, table => 'table1');
2041
is($dbi->select(table => 'table1')->one->{key1}, 1);
2042
is($dbi->select(table => 'table1')->one->{key2}, 2);
2043

            
2044
eval { $dbi->insert_param({";" => 1}) };
2045
like($@, qr/not safety/);
2046

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

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

            
2065
$rows = $dbi->select(
2066
    table => 'table1',
2067
    where   => {'key1' => 1},
2068
    join  => ['left outer join table2 on table1.key1 = table2.key1']
2069
)->all;
2070
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2071

            
2072
eval {
2073
    $rows = $dbi->select(
2074
        table => 'table1',
2075
        column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
2076
        where   => {'table1.key2' => 2},
2077
        join  => {'table1.key1' => 'table2.key1'}
2078
    );
2079
};
2080
like ($@, qr/array/);
2081

            
2082
$rows = $dbi->select(
2083
    table => 'table1',
2084
    where   => {'key1' => 1},
2085
    join  => ['left outer join table2 on table1.key1 = table2.key1',
2086
              'left outer join table3 on table2.key3 = table3.key3']
2087
)->all;
2088
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2089

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

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2137
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2138
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2139
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2140
$sql = <<"EOS";
cleanup test
Yuki Kimoto authored on 2011-08-06
2141
left outer join (
2142
  select * from table1 as t1
2143
  where t1.key2 = (
2144
    select max(t2.key2) from table1 as t2
2145
    where t1.key1 = t2.key1
2146
  )
2147
) as latest_table1 on table1.key1 = latest_table1.key1
2148
EOS
cleanup test
Yuki Kimoto authored on 2011-08-06
2149
$join = [$sql];
2150
$rows = $dbi->select(
2151
    table => 'table1',
2152
    column => 'latest_table1.key1 as latest_table1__key1',
2153
    join  => $join
2154
)->all;
2155
is_deeply($rows, [{latest_table1__key1 => 1}]);
2156

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2157
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2158
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2159
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2160
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2161
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2162
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
2163
$result = $dbi->select(
2164
    table => 'table1',
2165
    join => [
2166
        "left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1"
2167
    ]
2168
);
2169
is_deeply($result->all, [{key1 => 1, key2 => 2}]);
2170
$result = $dbi->select(
2171
    table => 'table1',
2172
    column => [{table2 => ['key3']}],
2173
    join => [
2174
        "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1"
2175
    ]
2176
);
2177
is_deeply($result->all, [{'table2.key3' => 4}]);
2178
$result = $dbi->select(
2179
    table => 'table1',
2180
    column => [{table2 => ['key3']}],
2181
    join => [
2182
        "left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'"
2183
    ]
2184
);
2185
is_deeply($result->all, [{'table2.key3' => 4}]);
2186

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

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

            
2222
$result = $model->select_at(
2223
    column => [
2224
        $model->mycolumn(['key1']),
2225
        $model->column(table2 => ['key1'])
2226
    ]
2227
);
2228
is_deeply($result->one,
2229
          {key1 => 1, 'table2.key1' => 1});
2230
$result = $model->select_at(
2231
    column => [
2232
        $model->mycolumn(['key1']),
2233
        {table2 => ['key1']}
2234
    ]
2235
);
2236
is_deeply($result->one,
2237
          {key1 => 1, 'table2.key1' => 1});
2238

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

            
2248
$result = $model->select_at(
2249
    column => [
2250
        $model->mycolumn(['key1']),
2251
        ['table2.key1' => 'table2.key1']
2252
    ]
2253
);
2254
is_deeply($result->one,
2255
          {key1 => 1, 'table2.key1' => 1});
2256

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

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

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

            
2304
$dbi->separator('-');
2305
$result = $model->select(
2306
    column => [
2307
        $model->column('table2', {alias => 'table2_alias'})
2308
    ],
2309
    where => {'table2_alias.key3' => 4}
2310
);
2311
is_deeply($result->one, 
2312
          {'table2_alias-key1' => 1, 'table2_alias-key3' => 4});
2313

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

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

            
2339

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

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

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

            
2383
test 'create_model';
cleanup test
Yuki Kimoto authored on 2011-08-06
2384
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2385
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2386
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2387

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

            
2416
test 'model method';
2417
test 'create_model';
cleanup test
Yuki Kimoto authored on 2011-08-06
2418
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2419
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2420
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2421
$model = $dbi->create_model(
2422
    table => 'table2'
2423
);
2424
$model->method(foo => sub { shift->select(@_) });
2425
is_deeply($model->foo->one, {key1 => 1, key3 => 3});
2426

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

            
2437
$params = [
2438
    {key1 => [1, 2], key2 => 1, key3 => [1, 2]},
2439
    {key1 => [3, 4], key2 => [2, 3], key3 => 3}
2440
];
2441
$param = $dbi->merge_param($params->[0], $params->[1]);
2442
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
2443

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

            
2462

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

            
2475
eval {
2476
$dbi->select(
2477
    table => 'table1',
2478
    column => 'key1',
2479
    wrap => 'select * from ('
2480
)
2481
};
2482
like($@, qr/array/);
2483

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

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

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

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

            
2536

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

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

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

            
2577
$dbi->delete_all(table => 'table1');
2578
$dbi->insert(
2579
    primary_key => 'key1', 
2580
    table => 'table1',
2581
    id => 0,
2582
    param => {key2 => 2, key3 => 3}
2583
);
2584

            
2585
is($dbi->select(table => 'table1')->one->{key1}, 0);
2586
is($dbi->select(table => 'table1')->one->{key2}, 2);
2587
is($dbi->select(table => 'table1')->one->{key3}, 3);
2588

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2589
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2590
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2591
$dbi->insert(
2592
    {key3 => 3},
2593
    primary_key => ['key1', 'key2'], 
2594
    table => 'table1',
2595
    id => [1, 2],
2596
);
2597
is($dbi->select(table => 'table1')->one->{key1}, 1);
2598
is($dbi->select(table => 'table1')->one->{key2}, 2);
2599
is($dbi->select(table => 'table1')->one->{key3}, 3);
2600

            
2601

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2615
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2616
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2617
$dbi->model('table1')->insert(
2618
    {key3 => 3},
2619
    id => [1, 2]
2620
);
2621
$result = $dbi->model('table1')->select;
2622
$row = $result->one;
2623
is($row->{key1}, 1);
2624
is($row->{key2}, 2);
2625
is($row->{key3}, 3);
2626

            
2627
test 'update and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2628
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2629
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2630
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2631
$dbi->update(
2632
    table => 'table1',
2633
    primary_key => ['key1', 'key2'],
2634
    id => [1, 2],
2635
    param => {key3 => 4}
2636
);
2637
is($dbi->select(table => 'table1')->one->{key1}, 1);
2638
is($dbi->select(table => 'table1')->one->{key2}, 2);
2639
is($dbi->select(table => 'table1')->one->{key3}, 4);
2640

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2653
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2654
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2655
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2656
$dbi->update(
2657
    {key3 => 4},
2658
    table => 'table1',
2659
    primary_key => ['key1', 'key2'],
2660
    id => [1, 2]
2661
);
2662
is($dbi->select(table => 'table1')->one->{key1}, 1);
2663
is($dbi->select(table => 'table1')->one->{key2}, 2);
2664
is($dbi->select(table => 'table1')->one->{key3}, 4);
2665

            
2666

            
2667
test 'model update and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2668
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2669
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2670
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2671
$dbi->model('table1')->update(
2672
    id => [1, 2],
2673
    param => {key3 => 4}
2674
);
2675
$result = $dbi->model('table1')->select;
2676
$row = $result->one;
2677
is($row->{key1}, 1);
2678
is($row->{key2}, 2);
2679
is($row->{key3}, 4);
2680

            
2681

            
2682
test 'delete and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2683
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2684
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2685
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2686
$dbi->delete(
2687
    table => 'table1',
2688
    primary_key => ['key1', 'key2'],
2689
    id => [1, 2],
2690
);
2691
is_deeply($dbi->select(table => 'table1')->all, []);
2692

            
2693
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2694
$dbi->delete(
2695
    table => 'table1',
2696
    primary_key => 'key1',
2697
    id => 0,
2698
);
2699
is_deeply($dbi->select(table => 'table1')->all, []);
2700

            
2701

            
2702
test 'model delete and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2703
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2704
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2705
$dbi->execute("create table table2 (key1, key2, key3)");
2706
$dbi->execute("create table table3 (key1, key2, key3)");
2707
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2708
$dbi->model('table1')->delete(id => [1, 2]);
2709
is_deeply($dbi->select(table => 'table1')->all, []);
2710
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
2711
$dbi->model('table1_1')->delete(id => [1, 2]);
2712
is_deeply($dbi->select(table => 'table1')->all, []);
2713
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
2714
$dbi->model('table1_3')->delete(id => [1, 2]);
2715
is_deeply($dbi->select(table => 'table1')->all, []);
2716

            
2717

            
2718
test 'select and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2719
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2720
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2721
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2722
$result = $dbi->select(
2723
    table => 'table1',
2724
    primary_key => ['key1', 'key2'],
2725
    id => [1, 2]
2726
);
2727
$row = $result->one;
2728
is($row->{key1}, 1);
2729
is($row->{key2}, 2);
2730
is($row->{key3}, 3);
2731

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

            
2744
$dbi->delete_all(table => 'table1');
2745
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2746
$result = $dbi->select(
2747
    table => 'table1',
2748
    primary_key => ['key1', 'key2'],
2749
    id => [1, 2]
2750
);
2751
$row = $result->one;
2752
is($row->{key1}, 1);
2753
is($row->{key2}, 2);
2754
is($row->{key3}, 3);
2755

            
2756

            
2757
test 'model select_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
2758
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
2759
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2760
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2761
$result = $dbi->model('table1')->select(id => [1, 2]);
2762
$row = $result->one;
2763
is($row->{key1}, 1);
2764
is($row->{key2}, 2);
2765
is($row->{key3}, 3);
2766

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

            
2782
$result = $model->select(
2783
    column => [$model->column('table2' => [qw/key1 key3/])],
2784
    where => {'table1.key1' => 1}
2785
);
2786
is_deeply($result->one,
2787
          {'table2.key1' => 1, 'table2.key3' => 3});
2788

            
2789

            
2790
test 'type_rule from';
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->type_rule(
2793
    from1 => {
2794
        date => sub { uc $_[0] }
2795
    }
2796
);
2797
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2798
$dbi->insert({key1 => 'a'}, table => 'table1');
2799
$result = $dbi->select(table => 'table1');
2800
is($result->fetch_first->[0], 'A');
2801

            
2802
$result = $dbi->select(table => 'table1');
2803
is($result->one->{key1}, 'A');
2804

            
2805

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2847
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2848
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2849
$dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1');
2850
$dbi->type_rule(
2851
    into1 => [
2852
        [qw/date datetime/] => sub { uc $_[0] }
2853
    ]
2854
);
2855
$result = $dbi->execute(
2856
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
2857
    param => {key1 => 'a', 'table1.key2' => 'b'},
2858
    table => 'table1'
2859
);
2860
$row = $result->one;
2861
is($row->{key1}, 'A');
2862
is($row->{key2}, 'B');
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->register_filter(twice => sub { $_[0] * 2 });
2867
$dbi->type_rule(
2868
    from1 => {
2869
        date => 'twice',
2870
    },
2871
    into1 => {
2872
        date => 'twice',
2873
    }
2874
);
2875
$dbi->insert({key1 => 2}, table => 'table1');
2876
$result = $dbi->select(table => 'table1');
2877
is($result->fetch->[0], 8);
2878

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

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

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

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

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

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

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

            
2993
eval{$dbi->type_rule(
2994
    into1 => {
2995
        date => 'pp'
2996
    }
2997
)};
2998
like($@, qr/not registered/);
2999

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

            
3011
eval {
3012
    $dbi->type_rule(
3013
        into1 => {
3014
            Date => sub { $_[0] * 2 },
3015
        }
3016
    );
3017
};
3018
like($@, qr/lower/);
3019

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

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

            
3054
$result = $dbi->select(table => 'table1');
3055
$result->type_rule(
3056
    from1 => {
3057
        date => sub { $_[0] * 3 }
3058
    }
3059
);
3060
$row = $result->one;
3061
is($row->{key1}, 6);
3062
is($row->{key2}, 2);
3063

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

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

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3164
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3165
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3166
$dbi->type_rule(
3167
    into1 => {
3168
        date => sub { $_[0] . 'b' }
3169
    },
3170
    into2 => {
3171
        date => sub { $_[0] . 'c' }
3172
    },
3173
    from1 => {
3174
        date => sub { $_[0] . 'd' }
3175
    },
3176
    from2 => {
3177
        date => sub { $_[0] . 'e' }
3178
    }
3179
);
3180
$dbi->insert({key1 => '1'}, table => 'table1', type_rule2_off => 1);
3181
$result = $dbi->select(table => 'table1');
3182
is($result->type_rule2_off->fetch_first->[0], '1bd');
3183
$result = $dbi->select(table => 'table1');
3184
is($result->type_rule2_on->fetch_first->[0], '1bde');
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-10
3188
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
3189
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
3190

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

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

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

            
3242

            
3243
test 'filter_off';
cleanup test
Yuki Kimoto authored on 2011-08-06
3244
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
3245
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
3246
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
3247

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

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

            
3266

            
3267
test 'select prefix option';
cleanup test
Yuki Kimoto authored on 2011-08-06
3268
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
3269
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
3270
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3271
$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all;
3272
is_deeply($rows, [{key1 => 1, key2 => 2}], "table");
3273

            
3274

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

            
3285

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

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

            
3306
$param = $dbi->map_param(
3307
    {id => '', author => '', price => ''},
3308
    id => 'book.id',
3309
    author => ['book.author', sub { '%' . $_[0] . '%' }],
3310
    price => ['book.price', sub { '%' . $_[0] . '%' },
3311
      {if => sub { $_[0] eq 1 }}]
3312
);
3313
is_deeply($param, {});
3314

            
3315
$param = $dbi->map_param(
3316
    {id => undef, author => undef, price => undef},
3317
    id => 'book.id',
3318
    price => ['book.price', {if => 'exists'}]
3319
);
3320
is_deeply($param, {'book.price' => undef});
3321

            
3322
$param = $dbi->map_param(
3323
    {price => 'a'},
3324
    id => ['book.id', {if => 'exists'}],
3325
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
3326
);
3327
is_deeply($param, {'book.price' => '%a'});
3328

            
3329

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

            
3343

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

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

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

            
3379
test 'last_sql';
cleanup test
Yuki Kimoto authored on 2011-08-06
3380
$dbi = DBIx::Custom->connect(%memory);
3381
$dbi->execute("create table table1 (key1, key2)");
3382
$dbi->execute('select * from table1');
3383
is($dbi->last_sql, 'select * from table1;');
3384

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

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

            
3394
test 'Named placeholder :name(operater) syntax';
cleanup test
Yuki Kimoto authored on 2011-08-06
3395
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3396
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
3397
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3398
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3399

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

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

            
3410
$source = "select * from table1 where :key1{<} and :key2{=}";
3411
$result = $dbi->execute($source, param => {key1 => 5, key2 => 2});
3412
$rows = $result->all;
3413
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3414

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

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

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

            
clenup test
Yuki Kimoto authored on 2011-08-06
3467
test 'result';
3468
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
3469
$dbi->execute($create_table1);
clenup test
Yuki Kimoto authored on 2011-08-06
3470
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3471
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3472

            
3473
$result = $dbi->select(table => 'table1');
3474
@rows = ();
3475
while (my $row = $result->fetch) {
3476
    push @rows, [@$row];
3477
}
3478
is_deeply(\@rows, [[1, 2], [3, 4]]);
3479

            
3480
$result = $dbi->select(table => 'table1');
3481
@rows = ();
3482
while (my $row = $result->fetch_hash) {
3483
    push @rows, {%$row};
3484
}
3485
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
3486

            
3487
$result = $dbi->select(table => 'table1');
3488
$row = $result->fetch_first;
3489
is_deeply($row, [1, 2], "row");
3490
$row = $result->fetch;
3491
ok(!$row, "finished");
3492

            
3493
$result = $dbi->select(table => 'table1');
3494
$row = $result->fetch_hash_first;
3495
is_deeply($row, {key1 => 1, key2 => 2}, "row");
3496
$row = $result->fetch_hash;
3497
ok(!$row, "finished");
3498

            
3499
$dbi->execute('create table table2 (key1, key2);');
3500
$result = $dbi->select(table => 'table2');
3501
$row = $result->fetch_hash_first;
3502
ok(!$row, "no row fetch");
3503

            
3504
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
3505
$dbi->execute($create_table1);
clenup test
Yuki Kimoto authored on 2011-08-06
3506
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3507
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3508
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1');
3509
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1');
3510
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1');
3511
$result = $dbi->select(table => 'table1');
3512
$rows = $result->fetch_multi(2);
3513
is_deeply($rows, [[1, 2],
3514
                  [3, 4]], "fetch_multi first");
3515
$rows = $result->fetch_multi(2);
3516
is_deeply($rows, [[5, 6],
3517
                  [7, 8]], "fetch_multi secound");
3518
$rows = $result->fetch_multi(2);
3519
is_deeply($rows, [[9, 10]], "fetch_multi third");
3520
$rows = $result->fetch_multi(2);
3521
ok(!$rows);
3522

            
3523
$result = $dbi->select(table => 'table1');
3524
eval {$result->fetch_multi};
3525
like($@, qr/Row count must be specified/, "Not specified row count");
3526

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

            
3539
$result = $dbi->select(table => 'table1');
3540
eval {$result->fetch_hash_multi};
3541
like($@, qr/Row count must be specified/, "Not specified row count");
3542

            
3543
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-10
3544
$dbi->execute($create_table1);
clenup test
Yuki Kimoto authored on 2011-08-06
3545
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3546
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3547

            
3548
test 'fetch_all';
3549
$result = $dbi->select(table => 'table1');
3550
$rows = $result->fetch_all;
3551
is_deeply($rows, [[1, 2], [3, 4]]);
3552

            
3553
$result = $dbi->select(table => 'table1');
3554
$rows = $result->fetch_hash_all;
3555
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
3556

            
3557
$result = $dbi->select(table => 'table1');
3558
$result->dbi->filters({three_times => sub { $_[0] * 3}});
3559
$result->filter({key1 => 'three_times'});
3560

            
3561
$rows = $result->fetch_all;
3562
is_deeply($rows, [[3, 2], [9, 4]], "array");
3563

            
3564
$result = $dbi->select(table => 'table1');
3565
$result->dbi->filters({three_times => sub { $_[0] * 3}});
3566
$result->filter({key1 => 'three_times'});
3567
$rows = $result->fetch_hash_all;
3568
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash");
3569

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

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

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

            
3625
$query = $builder->build_query("{p a b}");
3626
is($query->{sql}, "? a b;", "register_tag sql");
3627
is_deeply($query->{columns}, [2], "register_tag columns");
3628
isa_ok($ret_val, 'DBIx::Custom::QueryBuilder');
3629

            
3630
$builder = DBIx::Custom->new->query_builder;
3631

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

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

            
3638
$builder->register_tag({
3639
    q => 'string'
3640
});
3641

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

            
3645
$builder->register_tag({
3646
   r => sub {} 
3647
});
3648

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

            
3652
$builder->register_tag({
3653
   s => sub { return ["a", ""]} 
3654
});
3655

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

            
3659
$builder->register_tag(
3660
    t => sub {return ["a", []]}
3661
);
3662

            
3663

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

            
3674

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

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

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

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

            
3690
test 'variouse source';
3691
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;";
3692
$query = $builder->build_query($source);
3693
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1");
3694

            
3695
$source = "abc;";
3696
$query = $builder->build_query($source);
3697
is($query->sql, 'abc;', "basic : 2");
3698

            
3699
$source = "{= a}";
3700
$query = $builder->build_query($source);
3701
is($query->sql, 'a = ?;', "only tag");
3702

            
3703
$source = "000;";
3704
$query = $builder->build_query($source);
3705
is($query->sql, '000;', "contain 0 value");
3706

            
3707
$source = "a {= b} }";
3708
eval{$builder->build_query($source)};
3709
like($@, qr/unexpected "}"/, "error : 1");
3710

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