DBIx-Custom / t / sqlite.t /
Newer Older
3751 lines | 117.468kb
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
cleanup test
Yuki Kimoto authored on 2011-08-10
28
my $create_table1 = 'create table table1 (key1 char(255), key2 char(255));';
29
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
30
my $q = '"';
31
my $p = '"';
cleanup test
Yuki Kimoto authored on 2011-08-06
32

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

            
64
# Prepare table
test cleanup
Yuki Kimoto authored on 2011-08-10
65
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
66

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
294

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

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

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

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

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

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

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

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

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

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

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

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

            
389

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
468
test 'transaction';
test cleanup
Yuki Kimoto authored on 2011-08-10
469
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
470
eval { $dbi->execute('drop table table1') };
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

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

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

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

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

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

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

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

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

            
540

            
541
test 'transaction';
test cleanup
Yuki Kimoto authored on 2011-08-10
542
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
543
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
544
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
545

            
546
$dbi->begin_work;
547

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

            
554
$dbi->rollback if $@;
555

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

            
560
$dbi->begin_work;
561

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

            
567
$dbi->commit unless $@;
568

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

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

            
578

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

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

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

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

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

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

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

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

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

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

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

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

            
722
$result = $dbi->select(
723
     table => ['table1', 'table2'],
724
     column => ['key2', 'key3'],
725
     where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
726

            
727
$result->filter({'key2' => 'twice'});
728
$rows   = $result->all;
729
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join : omit");
730

            
731
test 'each_column';
test cleanup
Yuki Kimoto authored on 2011-08-10
732
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
733
eval { $dbi->execute('drop table table1') };
734
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-06
735
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
736
$dbi->execute('create table table1 (key1 Date, key2 datetime);');
cleanup test
Yuki Kimoto authored on 2011-08-06
737

            
738
$infos = [];
739
$dbi->each_column(sub {
740
    my ($self, $table, $column, $cinfo) = @_;
741
    
742
    if ($table =~ /^table/) {
743
         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
744
         push @$infos, $info;
745
    }
746
});
747
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
748
is_deeply($infos, 
749
    [
750
        ['table1', 'key1', 'key1'],
751
        ['table1', 'key2', 'key2'],
752
        ['table2', 'key1', 'key1'],
753
        ['table2', 'key3', 'key3']
754
    ]
755
    
756
);
757
test 'each_table';
test cleanup
Yuki Kimoto authored on 2011-08-10
758
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
759
eval { $dbi->execute('drop table table1') };
760
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-06
761
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
762
$dbi->execute('create table table1 (key1 Date, key2 datetime);');
cleanup test
Yuki Kimoto authored on 2011-08-06
763

            
764
$infos = [];
765
$dbi->each_table(sub {
766
    my ($self, $table, $table_info) = @_;
767
    
768
    if ($table =~ /^table/) {
769
         my $info = [$table, $table_info->{TABLE_NAME}];
770
         push @$infos, $info;
771
    }
772
});
773
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
774
is_deeply($infos, 
775
    [
776
        ['table1', 'table1'],
777
        ['table2', 'table2'],
778
    ]
779
);
780

            
781
test 'limit';
test cleanup
Yuki Kimoto authored on 2011-08-10
782
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
783
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
784
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
785
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
786
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
787
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
788
$dbi->register_tag(
789
    limit => sub {
790
        my ($count, $offset) = @_;
791
        
792
        my $s = '';
793
        $s .= "limit $count";
794
        $s .= " offset $offset" if defined $offset;
795
        
796
        return [$s, []];
797
    }
798
);
799
$rows = $dbi->select(
800
  table => 'table1',
801
  where => {key1 => 1},
802
  append => "order by key2 {limit 1 0}"
803
)->all;
804
is_deeply($rows, [{key1 => 1, key2 => 2}]);
805
$rows = $dbi->select(
806
  table => 'table1',
807
  where => {key1 => 1},
808
  append => "order by key2 {limit 2 1}"
809
)->all;
810
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
811
$rows = $dbi->select(
812
  table => 'table1',
813
  where => {key1 => 1},
814
  append => "order by key2 {limit 1}"
815
)->all;
816
is_deeply($rows, [{key1 => 1, key2 => 2}]);
817

            
818
test 'connect super';
819
{
820
    package MyDBI;
821
    
822
    use base 'DBIx::Custom';
823
    sub connect {
824
        my $self = shift->SUPER::connect(@_);
825
        
826
        return $self;
827
    }
828
    
829
    sub new {
830
        my $self = shift->SUPER::new(@_);
831
        
832
        return $self;
833
    }
834
}
835

            
test cleanup
Yuki Kimoto authored on 2011-08-10
836
$dbi = MyDBI->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
837
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
838
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
839
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
840
is($dbi->select(table => 'table1')->one->{key1}, 1);
841

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

            
849
{
850
    package MyDBI2;
851
    
852
    use base 'DBIx::Custom';
853
    sub connect {
854
        my $self = shift->SUPER::new(@_);
855
        $self->connect;
856
        
857
        return $self;
858
    }
859
}
860

            
test cleanup
Yuki Kimoto authored on 2011-08-10
861
$dbi = MyDBI->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
862
eval { $dbi->execute('drop table table1') };
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
is($dbi->select(table => 'table1')->one->{key1}, 1);
866

            
867
test 'end_filter';
test cleanup
Yuki Kimoto authored on 2011-08-10
868
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
869
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
870
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
871
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
872
$result = $dbi->select(table => 'table1');
873
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
874
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 });
875
$row = $result->fetch_first;
876
is_deeply($row, [6, 40]);
877

            
test cleanup
Yuki Kimoto authored on 2011-08-10
878
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
879
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
880
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
881
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
882
$result = $dbi->select(table => 'table1');
883
$result->filter([qw/key1 key2/] => sub { $_[0] * 2 });
884
$result->end_filter([[qw/key1 key2/] => sub { $_[0] * 3 }]);
885
$row = $result->fetch_first;
886
is_deeply($row, [6, 12]);
887

            
test cleanup
Yuki Kimoto authored on 2011-08-10
888
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
889
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
890
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
891
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
892
$result = $dbi->select(table => 'table1');
893
$result->filter([[qw/key1 key2/] => sub { $_[0] * 2 }]);
894
$result->end_filter([qw/key1 key2/] => sub { $_[0] * 3 });
895
$row = $result->fetch_first;
896
is_deeply($row, [6, 12]);
897

            
898
$dbi->register_filter(five_times => sub { $_[0] * 5 });
899
$result = $dbi->select(table => 'table1');
900
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
901
$result->end_filter({key1 => sub { $_[0] * 3 }, key2 => 'five_times' });
902
$row = $result->one;
903
is_deeply($row, {key1 => 6, key2 => 40});
904

            
905
$dbi->register_filter(five_times => sub { $_[0] * 5 });
906
$dbi->apply_filter('table1',
907
    key1 => {end => sub { $_[0] * 3 } },
908
    key2 => {end => 'five_times'}
909
);
910
$result = $dbi->select(table => 'table1');
911
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
912
$row = $result->one;
913
is_deeply($row, {key1 => 6, key2 => 40}, 'apply_filter');
914

            
915
$dbi->register_filter(five_times => sub { $_[0] * 5 });
916
$dbi->apply_filter('table1',
917
    key1 => {end => sub { $_[0] * 3 } },
918
    key2 => {end => 'five_times'}
919
);
920
$result = $dbi->select(table => 'table1');
921
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
922
$result->filter(key1 => undef);
923
$result->end_filter(key1 => undef);
924
$row = $result->one;
925
is_deeply($row, {key1 => 1, key2 => 40}, 'apply_filter overwrite');
926

            
927
test 'remove_end_filter and remove_filter';
test cleanup
Yuki Kimoto authored on 2011-08-10
928
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
929
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
930
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
931
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
932
$result = $dbi->select(table => 'table1');
933
$row = $result
934
       ->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 })
935
       ->remove_filter
936
       ->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 })
937
       ->remove_end_filter
938
       ->fetch_first;
939
is_deeply($row, [1, 2]);
940

            
941
test 'empty where select';
test cleanup
Yuki Kimoto authored on 2011-08-10
942
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
943
eval { $dbi->execute('drop table table1') };
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
$result = $dbi->select(table => 'table1', where => {});
947
$row = $result->one;
948
is_deeply($row, {key1 => 1, key2 => 2});
949

            
950
test 'select query option';
test cleanup
Yuki Kimoto authored on 2011-08-10
951
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
952
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
953
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
954
$query = $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, query => 1);
955
is(ref $query, 'DBIx::Custom::Query');
956
$query = $dbi->update(table => 'table1', where => {key1 => 1}, param => {key2 => 2}, query => 1);
957
is(ref $query, 'DBIx::Custom::Query');
958
$query = $dbi->delete(table => 'table1', where => {key1 => 1}, query => 1);
959
is(ref $query, 'DBIx::Custom::Query');
960
$query = $dbi->select(table => 'table1', where => {key1 => 1, key2 => 2}, query => 1);
961
is(ref $query, 'DBIx::Custom::Query');
962

            
map cleanup
Yuki Kimoto authored on 2011-08-09
963
test 'where';
test cleanup
Yuki Kimoto authored on 2011-08-10
964
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
965
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
966
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
967
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
968
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
969
$where = $dbi->where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
970
is("$where", "where ( key1 = :key1 and key2 = :key2 )", 'no param');
971

            
972
$where = $dbi->where
973
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
974
             ->param({key1 => 1});
975

            
976
$result = $dbi->select(
977
    table => 'table1',
978
    where => $where
979
);
980
$row = $result->all;
981
is_deeply($row, [{key1 => 1, key2 => 2}]);
982

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

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

            
1003
$where = $dbi->where
1004
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
1005
             ->param({});
1006
$result = $dbi->select(
1007
    table => 'table1',
1008
    where => $where,
1009
);
1010
$row = $result->all;
1011
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1012

            
1013
$where = $dbi->where
1014
             ->clause(['and', ['or', 'key1 > :key1', 'key1 < :key1'], 'key2 = :key2'])
1015
             ->param({key1 => [0, 3], key2 => 2});
1016
$result = $dbi->select(
1017
    table => 'table1',
1018
    where => $where,
1019
); 
1020
$row = $result->all;
1021
is_deeply($row, [{key1 => 1, key2 => 2}]);
1022

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

            
1031
eval {
1032
$where = $dbi->where
1033
             ->clause(['uuu']);
1034
$result = $dbi->select(
1035
    table => 'table1',
1036
    where => $where
1037
);
1038
};
1039
ok($@);
1040

            
1041
$where = $dbi->where;
1042
is("$where", '');
1043

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

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

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

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

            
1084
$where = $dbi->where
1085
             ->clause('key1 = :key1 key2 = :key2')
1086
             ->param({key1 => 1});
1087
eval{$where->to_string};
1088
like($@, qr/one column/);
1089

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

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

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

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

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

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

            
1150
$where = $dbi->where
1151
             ->clause(['or', ('key1 = :key1') x 3])
1152
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists, $dbi->not_exists]});
1153
$result = $dbi->select(
1154
    table => 'table1',
1155
    where => $where,
1156
);
1157
$row = $result->all;
1158
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1159

            
1160
$where = $dbi->where
1161
             ->clause(['or', ('key1 = :key1') x 3])
1162
             ->param({key1 => []});
1163
$result = $dbi->select(
1164
    table => 'table1',
1165
    where => $where,
1166
);
1167
$row = $result->all;
1168
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1169

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

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

            
1190
$where = $dbi->where
1191
             ->clause(['and', '{> key1}', '{< key1}' ])
1192
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists]});
1193
$result = $dbi->select(
1194
    table => 'table1',
1195
    where => $where,
1196
);
1197
$row = $result->all;
1198
is_deeply($row, [{key1 => 1, key2 => 2},{key1 => 3, key2 => 4}], 'not_exists');
1199

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

            
1210
$where = $dbi->where
1211
             ->clause(['and', 'key1 is not null', 'key2 is not null' ]);
1212
$result = $dbi->select(
1213
    table => 'table1',
1214
    where => $where,
1215
);
1216
$row = $result->all;
1217
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1218

            
1219
eval {$dbi->where(ppp => 1) };
1220
like($@, qr/invalid/);
1221

            
1222
$where = $dbi->where(
1223
    clause => ['and', ['or'], ['and', 'key1 = :key1', 'key2 = :key2']],
1224
    param => {key1 => 1, key2 => 2}
1225
);
1226
$result = $dbi->select(
1227
    table => 'table1',
1228
    where => $where,
1229
);
1230
$row = $result->all;
1231
is_deeply($row, [{key1 => 1, key2 => 2}]);
1232

            
1233

            
1234
$where = $dbi->where(
1235
    clause => ['and', ['or'], ['or', ':key1', ':key2']],
1236
    param => {}
1237
);
1238
$result = $dbi->select(
1239
    table => 'table1',
1240
    where => $where,
1241
);
1242
$row = $result->all;
1243
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1244

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

            
1252
$where = $dbi->where;
1253
$where->clause(['and', ':key1{=}']);
1254
$where->param({key1 => undef});
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
1255
$where->if('defined');
map cleanup
Yuki Kimoto authored on 2011-08-09
1256
$where->map;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
1257
$result = $dbi->execute("select * from table1 $where", {key1 => 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(['or', ':key1{=}', ':key1{=}']);
1263
$where->param({key1 => [undef, undef]});
1264
$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]});
1265
$row = $result->all;
1266
is_deeply($row, [{key1 => 1, key2 => 2}]);
1267
$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]});
1268
$row = $result->all;
1269
is_deeply($row, [{key1 => 1, key2 => 2}]);
1270

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

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

            
1292
$where = $dbi->where;
1293
$where->clause(['and', ':key1{=}']);
1294
$where->param({key1 => ''});
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
1295
$where->if('length');
map cleanup
Yuki Kimoto authored on 2011-08-09
1296
$where->map;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
1297
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1298
$row = $result->all;
1299
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1300

            
1301
$where = $dbi->where;
1302
$where->clause(['and', ':key1{=}']);
1303
$where->param({key1 => 5});
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
1304
$where->if(sub { ($_[0] || '') eq 5 });
map cleanup
Yuki Kimoto authored on 2011-08-09
1305
$where->map;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
1306
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1307
$row = $result->all;
1308
is_deeply($row, [{key1 => 1, key2 => 2}]);
1309

            
1310
$where = $dbi->where;
1311
$where->clause(['and', ':key1{=}']);
1312
$where->param({key1 => 7});
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
1313
$where->if(sub { ($_[0] || '') eq 5 });
map cleanup
Yuki Kimoto authored on 2011-08-09
1314
$where->map;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
1315
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1316
$row = $result->all;
1317
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1318

            
added tests
Yuki Kimoto authored on 2011-08-09
1319
$where = $dbi->where;
1320
$where->param({id => 1, author => 'Ken', price => 1900});
1321
$where->map(id => 'book.id',
1322
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1323
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1324
);
1325
is_deeply($where->param, {'book.id' => 1, 'book.author' => '%Ken%',
1326
  'book.price' => 1900});
1327

            
1328
$where = $dbi->where;
1329
$where->param({id => 0, author => 0, price => 0});
1330
$where->map(
1331
    id => 'book.id',
1332
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1333
    price => ['book.price', sub { '%' . $_[0] . '%' },
1334
      {if => sub { $_[0] eq 0 }}]
1335
);
1336
is_deeply($where->param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
1337

            
1338
$where = $dbi->where;
1339
$where->param({id => '', author => '', price => ''});
1340
$where->if('length');
1341
$where->map(
1342
    id => 'book.id',
1343
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1344
    price => ['book.price', sub { '%' . $_[0] . '%' },
1345
      {if => sub { $_[0] eq 1 }}]
1346
);
1347
is_deeply($where->param, {});
1348

            
1349
$where = $dbi->where;
1350
$where->param({id => undef, author => undef, price => undef});
1351
$where->if('length');
1352
$where->map(
1353
    id => 'book.id',
1354
    price => ['book.price', {if => 'exists'}]
1355
);
1356
is_deeply($where->param, {'book.price' => undef});
1357

            
1358
$where = $dbi->where;
1359
$where->param({price => 'a'});
1360
$where->if('length');
1361
$where->map(
1362
    id => ['book.id', {if => 'exists'}],
1363
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
1364
);
1365
is_deeply($where->param, {'book.price' => '%a'});
1366

            
fixed if is not converted to...
Yuki Kimoto authored on 2011-08-09
1367
$where = $dbi->where;
1368
$where->param({id => [1, 2], author => 'Ken', price => 1900});
1369
$where->map(
1370
    id => 'book.id',
1371
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1372
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1373
);
1374
is_deeply($where->param, {'book.id' => [1, 2], 'book.author' => '%Ken%',
1375
  'book.price' => 1900});
1376

            
1377
$where = $dbi->where;
1378
$where->if('length');
1379
$where->param({id => ['', ''], author => 'Ken', price => 1900});
1380
$where->map(
1381
    id => 'book.id',
1382
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1383
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1384
);
1385
is_deeply($where->param, {'book.id' => [$dbi->not_exists, $dbi->not_exists], 'book.author' => '%Ken%',
1386
  'book.price' => 1900});
1387

            
1388
$where = $dbi->where;
1389
$where->param({id => ['', ''], author => 'Ken', price => 1900});
1390
$where->map(
1391
    id => ['book.id', {if => 'length'}],
1392
    author => ['book.author', sub { '%' . $_[0] . '%' }, {if => 'defined'}],
1393
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1394
);
1395
is_deeply($where->param, {'book.id' => [$dbi->not_exists, $dbi->not_exists], 'book.author' => '%Ken%',
1396
  'book.price' => 1900});
cleanup test
Yuki Kimoto authored on 2011-08-06
1397

            
1398
test 'dbi_option default';
1399
$dbi = DBIx::Custom->new;
1400
is_deeply($dbi->dbi_option, {});
1401

            
1402
test 'register_tag_processor';
test cleanup
Yuki Kimoto authored on 2011-08-10
1403
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1404
$dbi->register_tag_processor(
1405
    a => sub { 1 }
1406
);
1407
is($dbi->query_builder->tag_processors->{a}->(), 1);
1408

            
1409
test 'register_tag';
test cleanup
Yuki Kimoto authored on 2011-08-10
1410
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1411
$dbi->register_tag(
1412
    b => sub { 2 }
1413
);
1414
is($dbi->query_builder->tags->{b}->(), 2);
1415

            
1416
test 'table not specify exception';
test cleanup
Yuki Kimoto authored on 2011-08-10
1417
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1418
eval {$dbi->insert};
1419
like($@, qr/table/);
1420
eval {$dbi->update};
1421
like($@, qr/table/);
1422
eval {$dbi->delete};
1423
like($@, qr/table/);
1424
eval {$dbi->select};
1425
like($@, qr/table/);
1426

            
1427

            
1428
test 'more tests';
test cleanup
Yuki Kimoto authored on 2011-08-10
1429
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1430
eval{$dbi->apply_filter('table', 'column', [])};
1431
like($@, qr/apply_filter/);
1432

            
1433
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
1434
like($@, qr/apply_filter/);
1435

            
1436
$dbi->apply_filter(
1437

            
1438
);
test cleanup
Yuki Kimoto authored on 2011-08-10
1439
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1440
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1441
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1442
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1443
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1444
$dbi->apply_filter('table1', 'key2', 
1445
                   {in => sub { $_[0] * 3 }, out => sub { $_[0] * 2 }});
1446
$rows = $dbi->select(table => 'table1', where => {key2 => 1})->all;
1447
is_deeply($rows, [{key1 => 1, key2 => 6}]);
1448

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1449
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1450
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1451
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1452
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1453
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1454
$dbi->apply_filter('table1', 'key2', {});
1455
$rows = $dbi->select(table => 'table1', where => {key2 => 2})->all;
1456
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1457

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1458
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1459
eval {$dbi->apply_filter('table1', 'key2', {out => 'no'})};
1460
like($@, qr/not registered/);
1461
eval {$dbi->apply_filter('table1', 'key2', {in => 'no'})};
1462
like($@, qr/not registered/);
1463
$dbi->method({one => sub { 1 }});
1464
is($dbi->one, 1);
1465

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1469
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1470
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1471
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1472
$dbi->register_filter(twice => sub { $_[0] * 2 });
1473
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
1474
             filter => {key1 => 'twice'});
1475
$row = $dbi->select(table => 'table1')->one;
1476
is_deeply($row, {key1 => 2, key2 => 2});
1477
eval {$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
1478
             filter => {key1 => 'no'}) };
1479
like($@, qr//);
1480

            
1481
$dbi->register_filter(one => sub { });
1482
$dbi->default_fetch_filter('one');
1483
ok($dbi->default_fetch_filter);
1484
$dbi->default_bind_filter('one');
1485
ok($dbi->default_bind_filter);
1486
eval{$dbi->default_fetch_filter('no')};
1487
like($@, qr/not registered/);
1488
eval{$dbi->default_bind_filter('no')};
1489
like($@, qr/not registered/);
1490
$dbi->default_bind_filter(undef);
1491
ok(!defined $dbi->default_bind_filter);
1492
$dbi->default_fetch_filter(undef);
1493
ok(!defined $dbi->default_fetch_filter);
1494
eval {$dbi->execute('select * from table1 {} {= author') };
1495
like($@, qr/Tag not finished/);
1496

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1497
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1498
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1499
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1500
$dbi->register_filter(one => sub { 1 });
1501
$result = $dbi->select(table => 'table1');
1502
eval {$result->filter(key1 => 'no')};
1503
like($@, qr/not registered/);
1504
eval {$result->end_filter(key1 => 'no')};
1505
like($@, qr/not registered/);
1506
$result->default_filter(undef);
1507
ok(!defined $result->default_filter);
1508
$result->default_filter('one');
1509
is($result->default_filter->(), 1);
1510

            
1511
test 'dbi_option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1512
$dbi = DBIx::Custom->connect(dbi_option => {PrintError => 1});
cleanup test
Yuki Kimoto authored on 2011-08-06
1513
ok($dbi->dbh->{PrintError});
test cleanup
Yuki Kimoto authored on 2011-08-10
1514
$dbi = DBIx::Custom->connect(dbi_options => {PrintError => 1});
cleanup test
Yuki Kimoto authored on 2011-08-06
1515
ok($dbi->dbh->{PrintError});
1516

            
1517
test 'DBIx::Custom::Result stash()';
1518
$result = DBIx::Custom::Result->new;
1519
is_deeply($result->stash, {}, 'default');
1520
$result->stash->{foo} = 1;
1521
is($result->stash->{foo}, 1, 'get and set');
1522

            
1523
test 'filter __ expression';
test cleanup
Yuki Kimoto authored on 2011-08-10
1524
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1525
eval { $dbi->execute('drop table company') };
1526
eval { $dbi->execute('drop table location') };
cleanup test
Yuki Kimoto authored on 2011-08-06
1527
$dbi->execute('create table company (id, name, location_id)');
1528
$dbi->execute('create table location (id, name)');
1529
$dbi->apply_filter('location',
1530
  name => {in => sub { uc $_[0] } }
1531
);
1532

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

            
1536
$result = $dbi->select(
1537
    table => ['company', 'location'], relation => {'company.location_id' => 'location.id'},
1538
    column => ['location.name as location__name']
1539
);
1540
is($result->fetch_first->[0], 'B');
1541

            
1542
$result = $dbi->select(
1543
    table => 'company', relation => {'company.location_id' => 'location.id'},
1544
    column => ['location.name as location__name']
1545
);
1546
is($result->fetch_first->[0], 'B');
1547

            
1548
$result = $dbi->select(
1549
    table => 'company', relation => {'company.location_id' => 'location.id'},
1550
    column => ['location.name as "location.name"']
1551
);
1552
is($result->fetch_first->[0], 'B');
1553

            
1554
test 'Model class';
1555
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
1556
$dbi = MyDBI1->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1557
eval { $dbi->execute('drop table book') };
cleanup test
Yuki Kimoto authored on 2011-08-06
1558
$dbi->execute("create table book (title, author)");
1559
$model = $dbi->model('book');
1560
$model->insert({title => 'a', author => 'b'});
1561
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
1562
$dbi->execute("create table company (name)");
1563
$model = $dbi->model('company');
1564
$model->insert({name => 'a'});
1565
is_deeply($model->list->all, [{name => 'a'}], 'basic');
1566
is($dbi->models->{'book'}, $dbi->model('book'));
1567
is($dbi->models->{'company'}, $dbi->model('company'));
1568

            
1569
{
1570
    package MyDBI4;
1571

            
1572
    use strict;
1573
    use warnings;
1574

            
1575
    use base 'DBIx::Custom';
1576

            
1577
    sub connect {
1578
        my $self = shift->SUPER::connect(@_);
1579
        
1580
        $self->include_model(
1581
            MyModel2 => [
1582
                'book',
1583
                {class => 'Company', name => 'company'}
1584
            ]
1585
        );
1586
    }
1587

            
1588
    package MyModel2::Base1;
1589

            
1590
    use strict;
1591
    use warnings;
1592

            
1593
    use base 'DBIx::Custom::Model';
1594

            
1595
    package MyModel2::book;
1596

            
1597
    use strict;
1598
    use warnings;
1599

            
1600
    use base 'MyModel2::Base1';
1601

            
1602
    sub insert {
1603
        my ($self, $param) = @_;
1604
        
1605
        return $self->SUPER::insert(param => $param);
1606
    }
1607

            
1608
    sub list { shift->select; }
1609

            
1610
    package MyModel2::Company;
1611

            
1612
    use strict;
1613
    use warnings;
1614

            
1615
    use base 'MyModel2::Base1';
1616

            
1617
    sub insert {
1618
        my ($self, $param) = @_;
1619
        
1620
        return $self->SUPER::insert(param => $param);
1621
    }
1622

            
1623
    sub list { shift->select; }
1624
}
test cleanup
Yuki Kimoto authored on 2011-08-10
1625
$dbi = MyDBI4->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1626
eval { $dbi->execute('drop table book') };
cleanup test
Yuki Kimoto authored on 2011-08-06
1627
$dbi->execute("create table book (title, author)");
1628
$model = $dbi->model('book');
1629
$model->insert({title => 'a', author => 'b'});
1630
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
1631
$dbi->execute("create table company (name)");
1632
$model = $dbi->model('company');
1633
$model->insert({name => 'a'});
1634
is_deeply($model->list->all, [{name => 'a'}], 'basic');
1635

            
1636
{
1637
     package MyDBI5;
1638

            
1639
    use strict;
1640
    use warnings;
1641

            
1642
    use base 'DBIx::Custom';
1643

            
1644
    sub connect {
1645
        my $self = shift->SUPER::connect(@_);
1646
        
1647
        $self->include_model('MyModel4');
1648
    }
1649
}
test cleanup
Yuki Kimoto authored on 2011-08-10
1650
$dbi = MyDBI5->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1651
eval { $dbi->execute('drop table company') };
1652
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
1653
$dbi->execute("create table company (name)");
1654
$dbi->execute("create table table1 (key1)");
1655
$model = $dbi->model('company');
1656
$model->insert({name => 'a'});
1657
is_deeply($model->list->all, [{name => 'a'}], 'include all model');
1658
$dbi->insert(table => 'table1', param => {key1 => 1});
1659
$model = $dbi->model('book');
1660
is_deeply($model->list->all, [{key1 => 1}], 'include all model');
1661

            
1662
test 'primary_key';
1663
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
1664
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1665
$model = $dbi->model('book');
1666
$model->primary_key(['id', 'number']);
1667
is_deeply($model->primary_key, ['id', 'number']);
1668

            
1669
test 'columns';
1670
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
1671
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1672
$model = $dbi->model('book');
1673
$model->columns(['id', 'number']);
1674
is_deeply($model->columns, ['id', 'number']);
1675

            
1676
test 'setup_model';
1677
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
1678
$dbi = MyDBI1->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1679
eval { $dbi->execute('drop table book') };
1680
eval { $dbi->execute('drop table company') };
1681
eval { $dbi->execute('drop table test') };
1682

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1683
$dbi->execute('create table book (id)');
1684
$dbi->execute('create table company (id, name);');
1685
$dbi->execute('create table test (id, name, primary key (id, name));');
1686
$dbi->setup_model;
1687
is_deeply($dbi->model('book')->columns, ['id']);
1688
is_deeply($dbi->model('company')->columns, ['id', 'name']);
1689

            
1690
test 'delete_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
1691
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1692
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1693
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1694
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1695
$dbi->delete_at(
1696
    table => 'table1',
1697
    primary_key => ['key1', 'key2'],
1698
    where => [1, 2],
1699
);
1700
is_deeply($dbi->select(table => 'table1')->all, []);
1701

            
1702
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1703
$dbi->delete_at(
1704
    table => 'table1',
1705
    primary_key => 'key1',
1706
    where => 1,
1707
);
1708
is_deeply($dbi->select(table => 'table1')->all, []);
1709

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

            
1724
$dbi->delete_all(table => 'table1');
1725
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1726
$dbi->insert_at(
1727
    primary_key => 'key1', 
1728
    table => 'table1',
1729
    where => 1,
1730
    param => {key2 => 2, key3 => 3}
1731
);
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}, 3);
1736

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

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

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

            
1774
$dbi->delete_all(table => 'table1');
1775
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1776
$dbi->update_at(
1777
    table => 'table1',
1778
    primary_key => 'key1',
1779
    where => 1,
1780
    param => {key3 => 4}
1781
);
1782
is($dbi->select(table => 'table1')->one->{key1}, 1);
1783
is($dbi->select(table => 'table1')->one->{key2}, 2);
1784
is($dbi->select(table => 'table1')->one->{key3}, 4);
1785

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1786
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1787
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1788
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1789
$dbi->update_at(
1790
    {key3 => 4},
1791
    table => 'table1',
1792
    primary_key => ['key1', 'key2'],
1793
    where => [1, 2]
1794
);
1795
is($dbi->select(table => 'table1')->one->{key1}, 1);
1796
is($dbi->select(table => 'table1')->one->{key2}, 2);
1797
is($dbi->select(table => 'table1')->one->{key3}, 4);
1798

            
1799
test 'select_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
1800
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1801
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1802
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1803
$result = $dbi->select_at(
1804
    table => 'table1',
1805
    primary_key => ['key1', 'key2'],
1806
    where => [1, 2]
1807
);
1808
$row = $result->one;
1809
is($row->{key1}, 1);
1810
is($row->{key2}, 2);
1811
is($row->{key3}, 3);
1812

            
1813
$dbi->delete_all(table => 'table1');
1814
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1815
$result = $dbi->select_at(
1816
    table => 'table1',
1817
    primary_key => 'key1',
1818
    where => 1,
1819
);
1820
$row = $result->one;
1821
is($row->{key1}, 1);
1822
is($row->{key2}, 2);
1823
is($row->{key3}, 3);
1824

            
1825
$dbi->delete_all(table => 'table1');
1826
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1827
$result = $dbi->select_at(
1828
    table => 'table1',
1829
    primary_key => ['key1', 'key2'],
1830
    where => [1, 2]
1831
);
1832
$row = $result->one;
1833
is($row->{key1}, 1);
1834
is($row->{key2}, 2);
1835
is($row->{key3}, 3);
1836

            
1837
eval {
1838
    $result = $dbi->select_at(
1839
        table => 'table1',
1840
        primary_key => ['key1', 'key2'],
1841
        where => {},
1842
    );
1843
};
1844
like($@, qr/must be/);
1845

            
1846
eval {
1847
    $result = $dbi->select_at(
1848
        table => 'table1',
1849
        primary_key => ['key1', 'key2'],
1850
        where => [1],
1851
    );
1852
};
1853
like($@, qr/same/);
1854

            
1855
eval {
1856
    $result = $dbi->update_at(
1857
        table => 'table1',
1858
        primary_key => ['key1', 'key2'],
1859
        where => {},
1860
        param => {key1 => 1, key2 => 2},
1861
    );
1862
};
1863
like($@, qr/must be/);
1864

            
1865
eval {
1866
    $result = $dbi->delete_at(
1867
        table => 'table1',
1868
        primary_key => ['key1', 'key2'],
1869
        where => {},
1870
    );
1871
};
1872
like($@, qr/must be/);
1873

            
1874
test 'columns';
1875
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
1876
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1877
$model = $dbi->model('book');
1878

            
1879

            
1880
test 'model delete_at';
1881
{
1882
    package MyDBI6;
1883
    
1884
    use base 'DBIx::Custom';
1885
    
1886
    sub connect {
1887
        my $self = shift->SUPER::connect(@_);
1888
        
1889
        $self->include_model('MyModel5');
1890
        
1891
        return $self;
1892
    }
1893
}
test cleanup
Yuki Kimoto authored on 2011-08-10
1894
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1895
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1896
$dbi->execute("create table table2 (key1, key2, key3)");
1897
$dbi->execute("create table table3 (key1, key2, key3)");
1898
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1899
$dbi->model('table1')->delete_at(where => [1, 2]);
1900
is_deeply($dbi->select(table => 'table1')->all, []);
1901
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
1902
$dbi->model('table1_1')->delete_at(where => [1, 2]);
1903
is_deeply($dbi->select(table => 'table1')->all, []);
1904
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
1905
$dbi->model('table1_3')->delete_at(where => [1, 2]);
1906
is_deeply($dbi->select(table => 'table1')->all, []);
1907

            
1908
test 'model insert_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
1909
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1910
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1911
$dbi->model('table1')->insert_at(
1912
    where => [1, 2],
1913
    param => {key3 => 3}
1914
);
1915
$result = $dbi->model('table1')->select;
1916
$row = $result->one;
1917
is($row->{key1}, 1);
1918
is($row->{key2}, 2);
1919
is($row->{key3}, 3);
1920

            
1921
test 'model update_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
1922
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1923
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1924
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1925
$dbi->model('table1')->update_at(
1926
    where => [1, 2],
1927
    param => {key3 => 4}
1928
);
1929
$result = $dbi->model('table1')->select;
1930
$row = $result->one;
1931
is($row->{key1}, 1);
1932
is($row->{key2}, 2);
1933
is($row->{key3}, 4);
1934

            
1935
test 'model select_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
1936
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1937
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1938
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1939
$result = $dbi->model('table1')->select_at(where => [1, 2]);
1940
$row = $result->one;
1941
is($row->{key1}, 1);
1942
is($row->{key2}, 2);
1943
is($row->{key3}, 3);
1944

            
1945

            
1946
test 'mycolumn and column';
1947
{
1948
    package MyDBI7;
1949
    
1950
    use base 'DBIx::Custom';
1951
    
1952
    sub connect {
1953
        my $self = shift->SUPER::connect(@_);
1954
        
1955
        $self->include_model('MyModel6');
1956
        
1957
        
1958
        return $self;
1959
    }
1960
}
test cleanup
Yuki Kimoto authored on 2011-08-10
1961
$dbi = MyDBI7->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1962
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1963
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1964
$dbi->separator('__');
1965
$dbi->setup_model;
1966
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1967
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1968
$model = $dbi->model('table1');
1969
$result = $model->select(
1970
    column => [$model->mycolumn, $model->column('table2')],
1971
    where => {'table1.key1' => 1}
1972
);
1973
is_deeply($result->one,
1974
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
1975

            
1976
test 'update_param';
test cleanup
Yuki Kimoto authored on 2011-08-10
1977
$dbi = DBIx::Custom->connect;
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};
1983
$update_param = $dbi->update_param($param);
1984
$sql = <<"EOS";
1985
update table1 $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 => 3, key4 => 4, key5 => 5},
1992
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1993
                  "basic");
1994

            
1995

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

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2014
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2015
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2016
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
2017
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
2018

            
2019
$param = {key2 => 11, key3 => 33};
2020
$update_param = $dbi->update_param($param, {no_set => 1});
2021
$sql = <<"EOS";
2022
update table1 set $update_param
2023
where key1 = 1
2024
EOS
2025
$dbi->execute($sql, param => $param);
test cleanup
Yuki Kimoto authored on 2011-08-06
2026
$result = $dbi->execute('select * from table1;', table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
2027
$rows   = $result->all;
2028
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
2029
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
2030
                  "update param no_set");
2031

            
2032
            
2033
eval { $dbi->update_param({";" => 1}) };
2034
like($@, qr/not safety/);
2035

            
2036

            
2037
test 'update_param';
test cleanup
Yuki Kimoto authored on 2011-08-10
2038
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2039
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2040
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
2041
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
2042

            
2043
$param = {key2 => 11};
2044
$update_param = $dbi->assign_param($param);
2045
$sql = <<"EOS";
2046
update table1 set $update_param
2047
where key1 = 1
2048
EOS
2049
$dbi->execute($sql, param => $param, table => 'table1');
test cleanup
Yuki Kimoto authored on 2011-08-06
2050
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
2051
$rows   = $result->all;
2052
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
2053
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
2054
                  "basic");
2055

            
2056

            
2057
test 'insert_param';
test cleanup
Yuki Kimoto authored on 2011-08-10
2058
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2059
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2060
$param = {key1 => 1, key2 => 2};
2061
$insert_param = $dbi->insert_param($param);
2062
$sql = <<"EOS";
2063
insert into table1 $insert_param
2064
EOS
2065
$dbi->execute($sql, param => $param, table => 'table1');
2066
is($dbi->select(table => 'table1')->one->{key1}, 1);
2067
is($dbi->select(table => 'table1')->one->{key2}, 2);
2068

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2069
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
2070
$dbi->quote('"');
cleanup test
Yuki Kimoto authored on 2011-08-10
2071
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2072
$param = {key1 => 1, key2 => 2};
2073
$insert_param = $dbi->insert_param($param);
2074
$sql = <<"EOS";
2075
insert into table1 $insert_param
2076
EOS
2077
$dbi->execute($sql, param => $param, table => 'table1');
2078
is($dbi->select(table => 'table1')->one->{key1}, 1);
2079
is($dbi->select(table => 'table1')->one->{key2}, 2);
2080

            
2081
eval { $dbi->insert_param({";" => 1}) };
2082
like($@, qr/not safety/);
2083

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

            
2085
test 'join';
test cleanup
Yuki Kimoto authored on 2011-08-10
2086
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2087
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2088
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2089
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
cleanup test
Yuki Kimoto authored on 2011-08-06
2090
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2091
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
cleanup test
Yuki Kimoto authored on 2011-08-06
2092
$dbi->execute('create table table3 (key3 int, key4 int);');
cleanup test
Yuki Kimoto authored on 2011-08-06
2093
$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4});
2094
$rows = $dbi->select(
2095
    table => 'table1',
2096
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
2097
    where   => {'table1.key2' => 2},
2098
    join  => ['left outer join table2 on table1.key1 = table2.key1']
2099
)->all;
2100
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]);
2101

            
2102
$rows = $dbi->select(
2103
    table => 'table1',
2104
    where   => {'key1' => 1},
2105
    join  => ['left outer join table2 on table1.key1 = table2.key1']
2106
)->all;
2107
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2108

            
2109
eval {
2110
    $rows = $dbi->select(
2111
        table => 'table1',
2112
        column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
2113
        where   => {'table1.key2' => 2},
2114
        join  => {'table1.key1' => 'table2.key1'}
2115
    );
2116
};
2117
like ($@, qr/array/);
2118

            
2119
$rows = $dbi->select(
2120
    table => 'table1',
2121
    where   => {'key1' => 1},
2122
    join  => ['left outer join table2 on table1.key1 = table2.key1',
2123
              'left outer join table3 on table2.key3 = table3.key3']
2124
)->all;
2125
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2126

            
2127
$rows = $dbi->select(
2128
    column => 'table3.key4 as table3__key4',
2129
    table => 'table1',
2130
    where   => {'table1.key1' => 1},
2131
    join  => ['left outer join table2 on table1.key1 = table2.key1',
2132
              'left outer join table3 on table2.key3 = table3.key3']
2133
)->all;
2134
is_deeply($rows, [{table3__key4 => 4}]);
2135

            
2136
$rows = $dbi->select(
2137
    column => 'table1.key1 as table1__key1',
2138
    table => 'table1',
2139
    where   => {'table3.key4' => 4},
2140
    join  => ['left outer join table2 on table1.key1 = table2.key1',
2141
              'left outer join table3 on table2.key3 = table3.key3']
2142
)->all;
2143
is_deeply($rows, [{table1__key1 => 1}]);
2144

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2145
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
2146
$dbi->quote('"');
cleanup test
Yuki Kimoto authored on 2011-08-10
2147
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2148
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-06
2149
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2150
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
2151
$rows = $dbi->select(
2152
    table => 'table1',
2153
    column => '"table1"."key1" as "table1_key1", "table2"."key1" as "table2_key1", "key2", "key3"',
2154
    where   => {'table1.key2' => 2},
2155
    join  => ['left outer join "table2" on "table1"."key1" = "table2"."key1"'],
2156
)->all;
2157
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}],
2158
          'quote');
2159

            
2160
{
2161
    package MyDBI8;
2162
    
2163
    use base 'DBIx::Custom';
2164
    
2165
    sub connect {
2166
        my $self = shift->SUPER::connect(@_);
2167
        
2168
        $self->include_model('MyModel7');
2169
        
2170
        return $self;
2171
    }
2172
}
cleanup test
Yuki Kimoto authored on 2011-08-06
2173

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2174
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2175
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2176
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2177
$sql = <<"EOS";
cleanup test
Yuki Kimoto authored on 2011-08-06
2178
left outer join (
2179
  select * from table1 as t1
2180
  where t1.key2 = (
2181
    select max(t2.key2) from table1 as t2
2182
    where t1.key1 = t2.key1
2183
  )
2184
) as latest_table1 on table1.key1 = latest_table1.key1
2185
EOS
cleanup test
Yuki Kimoto authored on 2011-08-06
2186
$join = [$sql];
2187
$rows = $dbi->select(
2188
    table => 'table1',
2189
    column => 'latest_table1.key1 as latest_table1__key1',
2190
    join  => $join
2191
)->all;
2192
is_deeply($rows, [{latest_table1__key1 => 1}]);
2193

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2194
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2195
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2196
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2197
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2198
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2199
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
2200
$result = $dbi->select(
2201
    table => 'table1',
2202
    join => [
2203
        "left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1"
2204
    ]
2205
);
2206
is_deeply($result->all, [{key1 => 1, key2 => 2}]);
2207
$result = $dbi->select(
2208
    table => 'table1',
2209
    column => [{table2 => ['key3']}],
2210
    join => [
2211
        "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1"
2212
    ]
2213
);
2214
is_deeply($result->all, [{'table2.key3' => 4}]);
2215
$result = $dbi->select(
2216
    table => 'table1',
2217
    column => [{table2 => ['key3']}],
2218
    join => [
2219
        "left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'"
2220
    ]
2221
);
2222
is_deeply($result->all, [{'table2.key3' => 4}]);
2223

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2224
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2225
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2226
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2227
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2228
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2229
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
2230
$result = $dbi->select(
2231
    table => 'table1',
2232
    column => [{table2 => ['key3']}],
2233
    join => [
2234
        {
2235
            clause => "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1",
2236
            table => ['table1', 'table2']
2237
        }
2238
    ]
2239
);
2240
is_deeply($result->all, [{'table2.key3' => 4}]);
2241

            
2242
test 'mycolumn';
test cleanup
Yuki Kimoto authored on 2011-08-10
2243
$dbi = MyDBI8->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2244
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2245
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2246
$dbi->setup_model;
2247
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2248
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2249
$model = $dbi->model('table1');
2250
$result = $model->select_at(
2251
    column => [
2252
        $model->mycolumn,
2253
        $model->column('table2')
2254
    ]
2255
);
2256
is_deeply($result->one,
2257
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
2258

            
2259
$result = $model->select_at(
2260
    column => [
2261
        $model->mycolumn(['key1']),
2262
        $model->column(table2 => ['key1'])
2263
    ]
2264
);
2265
is_deeply($result->one,
2266
          {key1 => 1, 'table2.key1' => 1});
2267
$result = $model->select_at(
2268
    column => [
2269
        $model->mycolumn(['key1']),
2270
        {table2 => ['key1']}
2271
    ]
2272
);
2273
is_deeply($result->one,
2274
          {key1 => 1, 'table2.key1' => 1});
2275

            
2276
$result = $model->select_at(
2277
    column => [
2278
        $model->mycolumn(['key1']),
2279
        ['table2.key1', as => 'table2.key1']
2280
    ]
2281
);
2282
is_deeply($result->one,
2283
          {key1 => 1, 'table2.key1' => 1});
2284

            
2285
$result = $model->select_at(
2286
    column => [
2287
        $model->mycolumn(['key1']),
2288
        ['table2.key1' => 'table2.key1']
2289
    ]
2290
);
2291
is_deeply($result->one,
2292
          {key1 => 1, 'table2.key1' => 1});
2293

            
2294
test 'dbi method from model';
2295
{
2296
    package MyDBI9;
2297
    
2298
    use base 'DBIx::Custom';
2299
    
2300
    sub connect {
2301
        my $self = shift->SUPER::connect(@_);
2302
        
2303
        $self->include_model('MyModel8')->setup_model;
2304
        
2305
        return $self;
2306
    }
2307
}
test cleanup
Yuki Kimoto authored on 2011-08-10
2308
$dbi = MyDBI9->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2309
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2310
$model = $dbi->model('table1');
2311
eval{$model->execute('select * from table1')};
2312
ok(!$@);
2313

            
2314
test 'column table option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2315
$dbi = MyDBI9->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2316
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2317
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2318
$dbi->setup_model;
2319
$dbi->execute('insert into table1 (key1, key2) values (1, 2);');
2320
$dbi->execute('insert into table2 (key1, key3) values (1, 4);');
2321
$model = $dbi->model('table1');
2322
$result = $model->select(
2323
    column => [
2324
        $model->column('table2', {alias => 'table2_alias'})
2325
    ],
2326
    where => {'table2_alias.key3' => 4}
2327
);
2328
is_deeply($result->one, 
2329
          {'table2_alias.key1' => 1, 'table2_alias.key3' => 4});
2330

            
2331
$dbi->separator('__');
2332
$result = $model->select(
2333
    column => [
2334
        $model->column('table2', {alias => 'table2_alias'})
2335
    ],
2336
    where => {'table2_alias.key3' => 4}
2337
);
2338
is_deeply($result->one, 
2339
          {'table2_alias__key1' => 1, 'table2_alias__key3' => 4});
2340

            
2341
$dbi->separator('-');
2342
$result = $model->select(
2343
    column => [
2344
        $model->column('table2', {alias => 'table2_alias'})
2345
    ],
2346
    where => {'table2_alias.key3' => 4}
2347
);
2348
is_deeply($result->one, 
2349
          {'table2_alias-key1' => 1, 'table2_alias-key3' => 4});
2350

            
2351
test 'type option'; # DEPRECATED!
2352
$dbi = DBIx::Custom->connect(
2353
    data_source => 'dbi:SQLite:dbname=:memory:',
2354
    dbi_option => {
2355
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
2356
    }
2357
);
2358
my $binary = pack("I3", 1, 2, 3);
2359
$dbi->execute('create table table1(key1, key2)');
2360
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
2361
$result = $dbi->select(table => 'table1');
2362
$row   = $result->one;
2363
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2364
$result = $dbi->execute('select length(key1) as key1_length from table1');
2365
$row = $result->one;
2366
is($row->{key1_length}, length $binary);
2367

            
2368
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [['key1'] => DBI::SQL_BLOB]);
2369
$result = $dbi->select(table => 'table1');
2370
$row   = $result->one;
2371
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2372
$result = $dbi->execute('select length(key1) as key1_length from table1');
2373
$row = $result->one;
2374
is($row->{key1_length}, length $binary);
2375

            
2376

            
2377
test 'bind_type option';
2378
$dbi = DBIx::Custom->connect(
2379
    data_source => 'dbi:SQLite:dbname=:memory:',
2380
    dbi_option => {
2381
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
2382
    }
2383
);
2384
$binary = pack("I3", 1, 2, 3);
2385
$dbi->execute('create table table1(key1, key2)');
2386
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, bind_type => [key1 => DBI::SQL_BLOB]);
2387
$result = $dbi->select(table => 'table1');
2388
$row   = $result->one;
2389
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2390
$result = $dbi->execute('select length(key1) as key1_length from table1');
2391
$row = $result->one;
2392
is($row->{key1_length}, length $binary);
2393

            
2394
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, bind_type => [['key1'] => DBI::SQL_BLOB]);
2395
$result = $dbi->select(table => 'table1');
2396
$row   = $result->one;
2397
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2398
$result = $dbi->execute('select length(key1) as key1_length from table1');
2399
$row = $result->one;
2400
is($row->{key1_length}, length $binary);
2401

            
2402
test 'model type attribute';
2403
$dbi = DBIx::Custom->connect(
2404
    data_source => 'dbi:SQLite:dbname=:memory:',
2405
    dbi_option => {
2406
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
2407
    }
2408
);
2409
$binary = pack("I3", 1, 2, 3);
2410
$dbi->execute('create table table1(key1, key2)');
2411
$model = $dbi->create_model(table => 'table1', bind_type => [key1 => DBI::SQL_BLOB]);
2412
$model->insert(param => {key1 => $binary, key2 => 'あ'});
2413
$result = $dbi->select(table => 'table1');
2414
$row   = $result->one;
2415
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2416
$result = $dbi->execute('select length(key1) as key1_length from table1');
2417
$row = $result->one;
2418
is($row->{key1_length}, length $binary);
2419

            
2420
test 'create_model';
test cleanup
Yuki Kimoto authored on 2011-08-10
2421
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2422
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2423
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2424

            
2425
$dbi->create_model(
2426
    table => 'table1',
2427
    join => [
2428
       'left outer join table2 on table1.key1 = table2.key1'
2429
    ],
2430
    primary_key => ['key1']
2431
);
2432
$model2 = $dbi->create_model(
2433
    table => 'table2'
2434
);
2435
$dbi->create_model(
2436
    table => 'table3',
2437
    filter => [
2438
        key1 => {in => sub { uc $_[0] }}
2439
    ]
2440
);
2441
$dbi->setup_model;
2442
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2443
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2444
$model = $dbi->model('table1');
2445
$result = $model->select(
2446
    column => [$model->mycolumn, $model->column('table2')],
2447
    where => {'table1.key1' => 1}
2448
);
2449
is_deeply($result->one,
2450
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
2451
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
2452

            
2453
test 'model method';
2454
test 'create_model';
test cleanup
Yuki Kimoto authored on 2011-08-10
2455
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
2456
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2457
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2458
$model = $dbi->create_model(
2459
    table => 'table2'
2460
);
2461
$model->method(foo => sub { shift->select(@_) });
2462
is_deeply($model->foo->one, {key1 => 1, key3 => 3});
2463

            
2464
test 'merge_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
2465
$dbi = DBIx::Custom->new;
2466
$params = [
2467
    {key1 => 1, key2 => 2, key3 => 3},
2468
    {key1 => 1, key2 => 2},
2469
    {key1 => 1}
2470
];
2471
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]);
2472
is_deeply($param, {key1 => [1, 1, 1], key2 => [2, 2], key3 => 3});
2473

            
2474
$params = [
2475
    {key1 => [1, 2], key2 => 1, key3 => [1, 2]},
2476
    {key1 => [3, 4], key2 => [2, 3], key3 => 3}
2477
];
2478
$param = $dbi->merge_param($params->[0], $params->[1]);
2479
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
2480

            
2481
test 'select() param option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2482
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2483
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2484
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2485
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
2486
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2487
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2488
$dbi->insert(table => 'table2', param => {key1 => 2, key3 => 5});
2489
$rows = $dbi->select(
2490
    table => 'table1',
2491
    column => 'table1.key1 as table1_key1, key2, key3',
2492
    where   => {'table1.key2' => 3},
2493
    join  => ['inner join (select * from table2 where {= table2.key3})' . 
2494
              ' as table2 on table1.key1 = table2.key1'],
2495
    param => {'table2.key3' => 5}
2496
)->all;
2497
is_deeply($rows, [{table1_key1 => 2, key2 => 3, key3 => 5}]);
2498

            
2499

            
2500
test 'select() wrap option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2501
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2502
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2503
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2504
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2505
$rows = $dbi->select(
2506
    table => 'table1',
2507
    column => 'key1',
2508
    wrap => ['select * from (', ') as t where key1 = 1']
2509
)->all;
2510
is_deeply($rows, [{key1 => 1}]);
2511

            
2512
eval {
2513
$dbi->select(
2514
    table => 'table1',
2515
    column => 'key1',
2516
    wrap => 'select * from ('
2517
)
2518
};
2519
like($@, qr/array/);
2520

            
2521
test 'select() string where';
test cleanup
Yuki Kimoto authored on 2011-08-10
2522
$dbi = DBIx::Custom->connect;
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
$rows = $dbi->select(
2527
    table => 'table1',
2528
    where => 'key1 = :key1 and key2 = :key2',
2529
    where_param => {key1 => 1, key2 => 2}
2530
)->all;
2531
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2532

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

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2559
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2560
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2561
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2562
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2563
$dbi->delete(
2564
    table => 'table1',
2565
    where => [
2566
        'key1 = :key1 and key2 = :key2',
2567
         {key1 => 1, key2 => 2}
2568
    ]
2569
);
2570
$rows = $dbi->select(table => 'table1')->all;
2571
is_deeply($rows, [{key1 => 2, key2 => 3}]);
2572

            
2573

            
2574
test 'update() string where';
test cleanup
Yuki Kimoto authored on 2011-08-10
2575
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2576
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2577
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2578
$dbi->update(
2579
    table => 'table1',
2580
    param => {key1 => 5},
2581
    where => 'key1 = :key1 and key2 = :key2',
2582
    where_param => {key1 => 1, key2 => 2}
2583
);
2584
$rows = $dbi->select(table => 'table1')->all;
2585
is_deeply($rows, [{key1 => 5, key2 => 2}]);
2586

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2587
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2588
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2589
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2590
$dbi->update(
2591
    table => 'table1',
2592
    param => {key1 => 5},
2593
    where => [
2594
        'key1 = :key1 and key2 = :key2',
2595
        {key1 => 1, key2 => 2}
2596
    ]
2597
);
2598
$rows = $dbi->select(table => 'table1')->all;
2599
is_deeply($rows, [{key1 => 5, key2 => 2}]);
2600

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

            
2614
$dbi->delete_all(table => 'table1');
2615
$dbi->insert(
2616
    primary_key => 'key1', 
2617
    table => 'table1',
2618
    id => 0,
2619
    param => {key2 => 2, key3 => 3}
2620
);
2621

            
2622
is($dbi->select(table => 'table1')->one->{key1}, 0);
2623
is($dbi->select(table => 'table1')->one->{key2}, 2);
2624
is($dbi->select(table => 'table1')->one->{key3}, 3);
2625

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

            
2638

            
2639
test 'model insert id and primary_key option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2640
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2641
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2642
$dbi->model('table1')->insert(
2643
    id => [1, 2],
2644
    param => {key3 => 3}
2645
);
2646
$result = $dbi->model('table1')->select;
2647
$row = $result->one;
2648
is($row->{key1}, 1);
2649
is($row->{key2}, 2);
2650
is($row->{key3}, 3);
2651

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2652
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2653
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2654
$dbi->model('table1')->insert(
2655
    {key3 => 3},
2656
    id => [1, 2]
2657
);
2658
$result = $dbi->model('table1')->select;
2659
$row = $result->one;
2660
is($row->{key1}, 1);
2661
is($row->{key2}, 2);
2662
is($row->{key3}, 3);
2663

            
2664
test 'update and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2665
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2666
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2667
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2668
$dbi->update(
2669
    table => 'table1',
2670
    primary_key => ['key1', 'key2'],
2671
    id => [1, 2],
2672
    param => {key3 => 4}
2673
);
2674
is($dbi->select(table => 'table1')->one->{key1}, 1);
2675
is($dbi->select(table => 'table1')->one->{key2}, 2);
2676
is($dbi->select(table => 'table1')->one->{key3}, 4);
2677

            
2678
$dbi->delete_all(table => 'table1');
2679
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2680
$dbi->update(
2681
    table => 'table1',
2682
    primary_key => 'key1',
2683
    id => 0,
2684
    param => {key3 => 4}
2685
);
2686
is($dbi->select(table => 'table1')->one->{key1}, 0);
2687
is($dbi->select(table => 'table1')->one->{key2}, 2);
2688
is($dbi->select(table => 'table1')->one->{key3}, 4);
2689

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2690
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2691
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2692
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2693
$dbi->update(
2694
    {key3 => 4},
2695
    table => 'table1',
2696
    primary_key => ['key1', 'key2'],
2697
    id => [1, 2]
2698
);
2699
is($dbi->select(table => 'table1')->one->{key1}, 1);
2700
is($dbi->select(table => 'table1')->one->{key2}, 2);
2701
is($dbi->select(table => 'table1')->one->{key3}, 4);
2702

            
2703

            
2704
test 'model update and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2705
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2706
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2707
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2708
$dbi->model('table1')->update(
2709
    id => [1, 2],
2710
    param => {key3 => 4}
2711
);
2712
$result = $dbi->model('table1')->select;
2713
$row = $result->one;
2714
is($row->{key1}, 1);
2715
is($row->{key2}, 2);
2716
is($row->{key3}, 4);
2717

            
2718

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

            
2730
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2731
$dbi->delete(
2732
    table => 'table1',
2733
    primary_key => 'key1',
2734
    id => 0,
2735
);
2736
is_deeply($dbi->select(table => 'table1')->all, []);
2737

            
2738

            
2739
test 'model delete and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2740
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2741
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2742
$dbi->execute("create table table2 (key1, key2, key3)");
2743
$dbi->execute("create table table3 (key1, key2, key3)");
2744
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2745
$dbi->model('table1')->delete(id => [1, 2]);
2746
is_deeply($dbi->select(table => 'table1')->all, []);
2747
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
2748
$dbi->model('table1_1')->delete(id => [1, 2]);
2749
is_deeply($dbi->select(table => 'table1')->all, []);
2750
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
2751
$dbi->model('table1_3')->delete(id => [1, 2]);
2752
is_deeply($dbi->select(table => 'table1')->all, []);
2753

            
2754

            
2755
test 'select and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2756
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2757
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2758
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2759
$result = $dbi->select(
2760
    table => 'table1',
2761
    primary_key => ['key1', 'key2'],
2762
    id => [1, 2]
2763
);
2764
$row = $result->one;
2765
is($row->{key1}, 1);
2766
is($row->{key2}, 2);
2767
is($row->{key3}, 3);
2768

            
2769
$dbi->delete_all(table => 'table1');
2770
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2771
$result = $dbi->select(
2772
    table => 'table1',
2773
    primary_key => 'key1',
2774
    id => 0,
2775
);
2776
$row = $result->one;
2777
is($row->{key1}, 0);
2778
is($row->{key2}, 2);
2779
is($row->{key3}, 3);
2780

            
2781
$dbi->delete_all(table => 'table1');
2782
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2783
$result = $dbi->select(
2784
    table => 'table1',
2785
    primary_key => ['key1', 'key2'],
2786
    id => [1, 2]
2787
);
2788
$row = $result->one;
2789
is($row->{key1}, 1);
2790
is($row->{key2}, 2);
2791
is($row->{key3}, 3);
2792

            
2793

            
2794
test 'model select_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
2795
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2796
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2797
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2798
$result = $dbi->model('table1')->select(id => [1, 2]);
2799
$row = $result->one;
2800
is($row->{key1}, 1);
2801
is($row->{key2}, 2);
2802
is($row->{key3}, 3);
2803

            
2804
test 'column separator is default .';
test cleanup
Yuki Kimoto authored on 2011-08-10
2805
$dbi = MyDBI7->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2806
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2807
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2808
$dbi->setup_model;
2809
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2810
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2811
$model = $dbi->model('table1');
2812
$result = $model->select(
2813
    column => [$model->column('table2')],
2814
    where => {'table1.key1' => 1}
2815
);
2816
is_deeply($result->one,
2817
          {'table2.key1' => 1, 'table2.key3' => 3});
2818

            
2819
$result = $model->select(
2820
    column => [$model->column('table2' => [qw/key1 key3/])],
2821
    where => {'table1.key1' => 1}
2822
);
2823
is_deeply($result->one,
2824
          {'table2.key1' => 1, 'table2.key3' => 3});
2825

            
2826

            
2827
test 'type_rule from';
test cleanup
Yuki Kimoto authored on 2011-08-10
2828
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
2829
$dbi->type_rule(
2830
    from1 => {
2831
        date => sub { uc $_[0] }
2832
    }
2833
);
2834
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2835
$dbi->insert({key1 => 'a'}, table => 'table1');
2836
$result = $dbi->select(table => 'table1');
2837
is($result->fetch_first->[0], 'A');
2838

            
2839
$result = $dbi->select(table => 'table1');
2840
is($result->one->{key1}, 'A');
2841

            
2842

            
2843
test 'type_rule into';
test cleanup
Yuki Kimoto authored on 2011-08-10
2844
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
2845
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2846
$dbi->type_rule(
2847
    into1 => {
2848
        date => sub { uc $_[0] }
2849
    }
2850
);
2851
$dbi->insert({key1 => 'a'}, table => 'table1');
2852
$result = $dbi->select(table => 'table1');
2853
is($result->one->{key1}, 'A');
2854

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2855
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
2856
$dbi->execute("create table table1 (key1 date, key2 datetime)");
2857
$dbi->type_rule(
2858
    into1 => [
2859
         [qw/date datetime/] => sub { uc $_[0] }
2860
    ]
2861
);
2862
$dbi->insert({key1 => 'a', key2 => 'b'}, table => 'table1');
2863
$result = $dbi->select(table => 'table1');
2864
$row = $result->one;
2865
is($row->{key1}, 'A');
2866
is($row->{key2}, 'B');
2867

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2868
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
2869
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2870
$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1');
2871
$dbi->type_rule(
2872
    into1 => [
2873
        [qw/date datetime/] => sub { uc $_[0] }
2874
    ]
2875
);
2876
$result = $dbi->execute(
2877
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
2878
    param => {key1 => 'a', 'table1.key2' => 'b'}
2879
);
2880
$row = $result->one;
2881
is($row->{key1}, 'a');
2882
is($row->{key2}, 'B');
2883

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2884
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
2885
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2886
$dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1');
2887
$dbi->type_rule(
2888
    into1 => [
2889
        [qw/date datetime/] => sub { uc $_[0] }
2890
    ]
2891
);
2892
$result = $dbi->execute(
2893
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
2894
    param => {key1 => 'a', 'table1.key2' => 'b'},
2895
    table => 'table1'
2896
);
2897
$row = $result->one;
2898
is($row->{key1}, 'A');
2899
is($row->{key2}, 'B');
2900

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2901
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
2902
$dbi->execute("create table table1 (key1 date, key2 datetime)");
2903
$dbi->register_filter(twice => sub { $_[0] * 2 });
2904
$dbi->type_rule(
2905
    from1 => {
2906
        date => 'twice',
2907
    },
2908
    into1 => {
2909
        date => 'twice',
2910
    }
2911
);
2912
$dbi->insert({key1 => 2}, table => 'table1');
2913
$result = $dbi->select(table => 'table1');
2914
is($result->fetch->[0], 8);
2915

            
2916
test 'type_rule and filter order';
test cleanup
Yuki Kimoto authored on 2011-08-10
2917
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
2918
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2919
$dbi->type_rule(
2920
    into1 => {
2921
        date => sub { $_[0] . 'b' }
2922
    },
2923
    into2 => {
2924
        date => sub { $_[0] . 'c' }
2925
    },
2926
    from1 => {
2927
        date => sub { $_[0] . 'd' }
2928
    },
2929
    from2 => {
2930
        date => sub { $_[0] . 'e' }
2931
    }
2932
);
2933
$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }});
2934
$result = $dbi->select(table => 'table1');
2935
$result->filter(key1 => sub { $_[0] . 'f' });
2936
is($result->fetch_first->[0], '1abcdef');
2937

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2938
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
2939
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2940
$dbi->type_rule(
2941
    from1 => {
2942
        date => sub { $_[0] . 'p' }
2943
    },
2944
    from2 => {
2945
        date => sub { $_[0] . 'q' }
2946
    },
2947
);
2948
$dbi->insert({key1 => '1'}, table => 'table1');
2949
$result = $dbi->select(table => 'table1');
2950
$result->type_rule(
2951
    from1 => {
2952
        date => sub { $_[0] . 'd' }
2953
    },
2954
    from2 => {
2955
        date => sub { $_[0] . 'e' }
2956
    }
2957
);
2958
$result->filter(key1 => sub { $_[0] . 'f' });
2959
is($result->fetch_first->[0], '1def');
2960

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2976
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
2977
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2978
$dbi->type_rule(
2979
    from1 => {
2980
        date => sub { $_[0] * 2 },
2981
    },
2982
    into1 => {
2983
        date => sub { $_[0] * 3 },
2984
    }
2985
);
2986
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
2987
$result = $dbi->select(table => 'table1', type_rule_off => 1);
2988
is($result->one->{key1}, 4);
2989

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

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3018
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
3019
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3020
$dbi->register_filter(ppp => sub { uc $_[0] });
3021
$dbi->type_rule(
3022
    into1 => {
3023
        date => 'ppp'
3024
    }
3025
);
3026
$dbi->insert({key1 => 'a'}, table => 'table1');
3027
$result = $dbi->select(table => 'table1');
3028
is($result->one->{key1}, 'A');
3029

            
3030
eval{$dbi->type_rule(
3031
    into1 => {
3032
        date => 'pp'
3033
    }
3034
)};
3035
like($@, qr/not registered/);
3036

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3037
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
3038
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3039
eval {
3040
    $dbi->type_rule(
3041
        from1 => {
3042
            Date => sub { $_[0] * 2 },
3043
        }
3044
    );
3045
};
3046
like($@, qr/lower/);
3047

            
3048
eval {
3049
    $dbi->type_rule(
3050
        into1 => {
3051
            Date => sub { $_[0] * 2 },
3052
        }
3053
    );
3054
};
3055
like($@, qr/lower/);
3056

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3072
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
3073
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3074
$dbi->type_rule(
3075
    from1 => {
3076
        date => sub { $_[0] * 2 },
3077
        datetime => sub { $_[0] * 4 },
3078
    },
3079
);
3080
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
3081
$result = $dbi->select(table => 'table1');
3082
$result->type_rule(
3083
    from1 => {
3084
        date => sub { $_[0] * 3 }
3085
    }
3086
);
3087
$row = $result->one;
3088
is($row->{key1}, 6);
3089
is($row->{key2}, 2);
3090

            
3091
$result = $dbi->select(table => 'table1');
3092
$result->type_rule(
3093
    from1 => {
3094
        date => sub { $_[0] * 3 }
3095
    }
3096
);
3097
$row = $result->one;
3098
is($row->{key1}, 6);
3099
is($row->{key2}, 2);
3100

            
3101
$result = $dbi->select(table => 'table1');
3102
$result->type_rule(
3103
    from1 => {
3104
        date => sub { $_[0] * 3 }
3105
    }
3106
);
3107
$row = $result->one;
3108
is($row->{key1}, 6);
3109
is($row->{key2}, 2);
3110
$result = $dbi->select(table => 'table1');
3111
$result->type_rule(
3112
    from1 => [date => sub { $_[0] * 3 }]
3113
);
3114
$row = $result->one;
3115
is($row->{key1}, 6);
3116
is($row->{key2}, 2);
3117
$dbi->register_filter(fivetimes => sub { $_[0] * 5});
3118
$result = $dbi->select(table => 'table1');
3119
$result->type_rule(
3120
    from1 => [date => 'fivetimes']
3121
);
3122
$row = $result->one;
3123
is($row->{key1}, 10);
3124
is($row->{key2}, 2);
3125
$result = $dbi->select(table => 'table1');
3126
$result->type_rule(
3127
    from1 => [date => undef]
3128
);
3129
$row = $result->one;
3130
is($row->{key1}, 2);
3131
is($row->{key2}, 2);
3132

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3133
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
3134
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3135
$dbi->type_rule(
3136
    from1 => {
3137
        date => sub { $_[0] * 2 },
3138
    },
3139
);
3140
$dbi->insert({key1 => 2}, table => 'table1');
3141
$result = $dbi->select(table => 'table1');
3142
$result->filter(key1 => sub { $_[0] * 3 });
3143
is($result->one->{key1}, 12);
3144

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3145
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
3146
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3147
$dbi->type_rule(
3148
    from1 => {
3149
        date => sub { $_[0] * 2 },
3150
    },
3151
);
3152
$dbi->insert({key1 => 2}, table => 'table1');
3153
$result = $dbi->select(table => 'table1');
3154
$result->filter(key1 => sub { $_[0] * 3 });
3155
is($result->fetch->[0], 12);
3156

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3179
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
3180
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3181
$dbi->type_rule(
3182
    into1 => {
3183
        date => sub { $_[0] . 'b' }
3184
    },
3185
    into2 => {
3186
        date => sub { $_[0] . 'c' }
3187
    },
3188
    from1 => {
3189
        date => sub { $_[0] . 'd' }
3190
    },
3191
    from2 => {
3192
        date => sub { $_[0] . 'e' }
3193
    }
3194
);
3195
$dbi->insert({key1 => '1'}, table => 'table1', type_rule1_off => 1);
3196
$result = $dbi->select(table => 'table1');
3197
is($result->type_rule1_off->fetch_first->[0], '1ce');
3198
$result = $dbi->select(table => 'table1');
3199
is($result->type_rule1_on->fetch_first->[0], '1cde');
3200

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3201
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
3202
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3203
$dbi->type_rule(
3204
    into1 => {
3205
        date => sub { $_[0] . 'b' }
3206
    },
3207
    into2 => {
3208
        date => sub { $_[0] . 'c' }
3209
    },
3210
    from1 => {
3211
        date => sub { $_[0] . 'd' }
3212
    },
3213
    from2 => {
3214
        date => sub { $_[0] . 'e' }
3215
    }
3216
);
3217
$dbi->insert({key1 => '1'}, table => 'table1', type_rule2_off => 1);
3218
$result = $dbi->select(table => 'table1');
3219
is($result->type_rule2_off->fetch_first->[0], '1bd');
3220
$result = $dbi->select(table => 'table1');
3221
is($result->type_rule2_on->fetch_first->[0], '1bde');
3222

            
3223
test 'separator';
test cleanup
Yuki Kimoto authored on 2011-08-10
3224
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3225
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
3226
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
3227

            
3228
$dbi->create_model(
3229
    table => 'table1',
3230
    join => [
3231
       'left outer join table2 on table1.key1 = table2.key1'
3232
    ],
3233
    primary_key => ['key1'],
3234
);
3235
$model2 = $dbi->create_model(
3236
    table => 'table2',
3237
);
3238
$dbi->setup_model;
3239
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3240
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
3241
$model = $dbi->model('table1');
3242
$result = $model->select(
3243
    column => [
3244
        $model->mycolumn,
3245
        {table2 => [qw/key1 key3/]}
3246
    ],
3247
    where => {'table1.key1' => 1}
3248
);
3249
is_deeply($result->one,
3250
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
3251
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3252

            
3253
$dbi->separator('__');
3254
$model = $dbi->model('table1');
3255
$result = $model->select(
3256
    column => [
3257
        $model->mycolumn,
3258
        {table2 => [qw/key1 key3/]}
3259
    ],
3260
    where => {'table1.key1' => 1}
3261
);
3262
is_deeply($result->one,
3263
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
3264
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3265

            
3266
$dbi->separator('-');
3267
$model = $dbi->model('table1');
3268
$result = $model->select(
3269
    column => [
3270
        $model->mycolumn,
3271
        {table2 => [qw/key1 key3/]}
3272
    ],
3273
    where => {'table1.key1' => 1}
3274
);
3275
is_deeply($result->one,
3276
          {key1 => 1, key2 => 2, 'table2-key1' => 1, 'table2-key3' => 3});
3277
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3278

            
3279

            
3280
test 'filter_off';
test cleanup
Yuki Kimoto authored on 2011-08-10
3281
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3282
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
3283
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
3284

            
3285
$dbi->create_model(
3286
    table => 'table1',
3287
    join => [
3288
       'left outer join table2 on table1.key1 = table2.key1'
3289
    ],
3290
    primary_key => ['key1'],
3291
);
3292
$dbi->setup_model;
3293
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3294
$model = $dbi->model('table1');
3295
$result = $model->select(column => 'key1');
3296
$result->filter(key1 => sub { $_[0] * 2 });
3297
is_deeply($result->one, {key1 => 2});
3298

            
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
3299
test 'available_datetype';
test cleanup
Yuki Kimoto authored on 2011-08-10
3300
$dbi = DBIx::Custom->connect;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
3301
ok($dbi->can('available_datatype'));
cleanup test
Yuki Kimoto authored on 2011-08-06
3302

            
3303

            
3304
test 'select prefix option';
test cleanup
Yuki Kimoto authored on 2011-08-10
3305
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3306
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
3307
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3308
$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all;
3309
is_deeply($rows, [{key1 => 1, key2 => 2}], "table");
3310

            
3311

            
3312
test 'separator';
test cleanup
Yuki Kimoto authored on 2011-08-10
3313
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
3314
is($dbi->separator, '.');
3315
$dbi->separator('-');
3316
is($dbi->separator, '-');
3317
$dbi->separator('__');
3318
is($dbi->separator, '__');
3319
eval { $dbi->separator('?') };
3320
like($@, qr/Separator/);
3321

            
3322

            
3323
test 'map_param';
test cleanup
Yuki Kimoto authored on 2011-08-10
3324
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
3325
$param = $dbi->map_param(
3326
    {id => 1, author => 'Ken', price => 1900},
3327
    id => 'book.id',
3328
    author => ['book.author', sub { '%' . $_[0] . '%' }],
3329
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
3330
);
3331
is_deeply($param, {'book.id' => 1, 'book.author' => '%Ken%',
3332
  'book.price' => 1900});
3333

            
3334
$param = $dbi->map_param(
3335
    {id => 0, author => 0, price => 0},
3336
    id => 'book.id',
3337
    author => ['book.author', sub { '%' . $_[0] . '%' }],
3338
    price => ['book.price', sub { '%' . $_[0] . '%' },
3339
      {if => sub { $_[0] eq 0 }}]
3340
);
3341
is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
3342

            
3343
$param = $dbi->map_param(
3344
    {id => '', author => '', price => ''},
3345
    id => 'book.id',
3346
    author => ['book.author', sub { '%' . $_[0] . '%' }],
3347
    price => ['book.price', sub { '%' . $_[0] . '%' },
3348
      {if => sub { $_[0] eq 1 }}]
3349
);
3350
is_deeply($param, {});
3351

            
3352
$param = $dbi->map_param(
3353
    {id => undef, author => undef, price => undef},
3354
    id => 'book.id',
3355
    price => ['book.price', {if => 'exists'}]
3356
);
3357
is_deeply($param, {'book.price' => undef});
3358

            
3359
$param = $dbi->map_param(
3360
    {price => 'a'},
3361
    id => ['book.id', {if => 'exists'}],
3362
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
3363
);
3364
is_deeply($param, {'book.price' => '%a'});
3365

            
3366

            
3367
test 'table_alias';
test cleanup
Yuki Kimoto authored on 2011-08-10
3368
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
3369
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3370
$dbi->type_rule(
3371
    into1 => {
3372
        date => sub { uc $_[0] }
3373
    }
3374
);
3375
$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'},
3376
  table_alias => {table2 => 'table1'});
3377
$result = $dbi->select(table => 'table1');
3378
is($result->one->{key1}, 'A');
3379

            
3380

            
3381
test 'order';
test cleanup
Yuki Kimoto authored on 2011-08-10
3382
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
3383
$dbi->execute("create table table1 (key1, key2)");
3384
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
3385
$dbi->insert({key1 => 1, key2 => 3}, table => 'table1');
3386
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
3387
$dbi->insert({key1 => 2, key2 => 4}, table => 'table1');
3388
my $order = $dbi->order;
3389
$order->prepend('key1', 'key2 desc');
3390
$result = $dbi->select(table => 'table1', append => "$order");
3391
is_deeply($result->all, [{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1},
3392
  {key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}]);
3393
$order->prepend('key1 desc');
3394
$result = $dbi->select(table => 'table1', append => "$order");
3395
is_deeply($result->all, [{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2},
3396
  {key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}]);
3397

            
3398
$order = $dbi->order;
3399
$order->prepend(['table1-key1'], [qw/table1-key2 desc/]);
3400
$result = $dbi->select(table => 'table1',
3401
  column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']],
3402
  append => "$order");
3403
is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3},
3404
  {'table1-key1' => 1, 'table1-key2' => 1},
3405
  {'table1-key1' => 2, 'table1-key2' => 4},
3406
  {'table1-key1' => 2, 'table1-key2' => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
3407

            
3408
test 'tag_parse';
test cleanup
Yuki Kimoto authored on 2011-08-10
3409
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
3410
$dbi->tag_parse(0);
cleanup test
Yuki Kimoto authored on 2011-08-06
3411
$dbi->execute("create table table1 (key1, key2)");
3412
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
3413
eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})};
3414
ok($@);
cleanup test
Yuki Kimoto authored on 2011-08-06
3415

            
3416
test 'last_sql';
test cleanup
Yuki Kimoto authored on 2011-08-10
3417
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
3418
$dbi->execute("create table table1 (key1, key2)");
3419
$dbi->execute('select * from table1');
3420
is($dbi->last_sql, 'select * from table1;');
3421

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

            
3425
test 'DBIx::Custom header';
test cleanup
Yuki Kimoto authored on 2011-08-10
3426
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
3427
$dbi->execute("create table table1 (key1, key2)");
3428
$result = $dbi->execute('select key1 as h1, key2 as h2 from table1');
3429
is_deeply($result->header, [qw/h1 h2/]);
cleanup test
Yuki Kimoto authored on 2011-08-06
3430

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

            
3437
$source = "select * from table1 where :key1{=} and :key2{=}";
3438
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
3439
$rows = $result->all;
3440
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3441

            
3442
$source = "select * from table1 where :key1{ = } and :key2{=}";
3443
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
3444
$rows = $result->all;
3445
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3446

            
3447
$source = "select * from table1 where :key1{<} and :key2{=}";
3448
$result = $dbi->execute($source, param => {key1 => 5, key2 => 2});
3449
$rows = $result->all;
3450
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3451

            
3452
$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}";
3453
$result = $dbi->execute(
3454
    $source,
3455
    param => {'table1.key1' => 1, 'table1.key2' => 1},
3456
    filter => {'table1.key2' => sub { $_[0] * 2 }}
3457
);
3458
$rows = $result->all;
3459
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3460

            
3461
test 'high perfomance way';
cleanup test
Yuki Kimoto authored on 2011-08-06
3462
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
3463
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
3464
$rows = [
3465
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3466
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3467
];
3468
{
3469
    my $query;
3470
    foreach my $row (@$rows) {
3471
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
3472
      $dbi->execute($query, $row, filter => {ab => sub { $_[0] * 2 }});
3473
    }
3474
    is_deeply($dbi->select(table => 'table1')->all,
3475
      [
3476
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3477
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3478
      ]
3479
    );
3480
}
3481

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3482
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
3483
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
3484
$rows = [
3485
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3486
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3487
];
3488
{
3489
    my $query;
3490
    my $sth;
3491
    foreach my $row (@$rows) {
3492
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
3493
      $sth ||= $query->sth;
3494
      $sth->execute(map { $row->{$_} } sort keys %$row);
3495
    }
3496
    is_deeply($dbi->select(table => 'table1')->all,
3497
      [
3498
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3499
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3500
      ]
3501
    );
3502
}
cleanup test
Yuki Kimoto authored on 2011-08-06
3503

            
clenup test
Yuki Kimoto authored on 2011-08-06
3504
test 'result';
test cleanup
Yuki Kimoto authored on 2011-08-10
3505
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3506
$dbi->execute($create_table1);
clenup test
Yuki Kimoto authored on 2011-08-06
3507
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3508
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3509

            
3510
$result = $dbi->select(table => 'table1');
3511
@rows = ();
3512
while (my $row = $result->fetch) {
3513
    push @rows, [@$row];
3514
}
3515
is_deeply(\@rows, [[1, 2], [3, 4]]);
3516

            
3517
$result = $dbi->select(table => 'table1');
3518
@rows = ();
3519
while (my $row = $result->fetch_hash) {
3520
    push @rows, {%$row};
3521
}
3522
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
3523

            
3524
$result = $dbi->select(table => 'table1');
3525
$row = $result->fetch_first;
3526
is_deeply($row, [1, 2], "row");
3527
$row = $result->fetch;
3528
ok(!$row, "finished");
3529

            
3530
$result = $dbi->select(table => 'table1');
3531
$row = $result->fetch_hash_first;
3532
is_deeply($row, {key1 => 1, key2 => 2}, "row");
3533
$row = $result->fetch_hash;
3534
ok(!$row, "finished");
3535

            
3536
$dbi->execute('create table table2 (key1, key2);');
3537
$result = $dbi->select(table => 'table2');
3538
$row = $result->fetch_hash_first;
3539
ok(!$row, "no row fetch");
3540

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3541
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3542
$dbi->execute($create_table1);
clenup test
Yuki Kimoto authored on 2011-08-06
3543
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3544
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3545
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1');
3546
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1');
3547
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1');
3548
$result = $dbi->select(table => 'table1');
3549
$rows = $result->fetch_multi(2);
3550
is_deeply($rows, [[1, 2],
3551
                  [3, 4]], "fetch_multi first");
3552
$rows = $result->fetch_multi(2);
3553
is_deeply($rows, [[5, 6],
3554
                  [7, 8]], "fetch_multi secound");
3555
$rows = $result->fetch_multi(2);
3556
is_deeply($rows, [[9, 10]], "fetch_multi third");
3557
$rows = $result->fetch_multi(2);
3558
ok(!$rows);
3559

            
3560
$result = $dbi->select(table => 'table1');
3561
eval {$result->fetch_multi};
3562
like($@, qr/Row count must be specified/, "Not specified row count");
3563

            
3564
$result = $dbi->select(table => 'table1');
3565
$rows = $result->fetch_hash_multi(2);
3566
is_deeply($rows, [{key1 => 1, key2 => 2},
3567
                  {key1 => 3, key2 => 4}], "fetch_multi first");
3568
$rows = $result->fetch_hash_multi(2);
3569
is_deeply($rows, [{key1 => 5, key2 => 6},
3570
                  {key1 => 7, key2 => 8}], "fetch_multi secound");
3571
$rows = $result->fetch_hash_multi(2);
3572
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third");
3573
$rows = $result->fetch_hash_multi(2);
3574
ok(!$rows);
3575

            
3576
$result = $dbi->select(table => 'table1');
3577
eval {$result->fetch_hash_multi};
3578
like($@, qr/Row count must be specified/, "Not specified row count");
3579

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3580
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3581
$dbi->execute($create_table1);
clenup test
Yuki Kimoto authored on 2011-08-06
3582
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3583
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3584

            
3585
test 'fetch_all';
3586
$result = $dbi->select(table => 'table1');
3587
$rows = $result->fetch_all;
3588
is_deeply($rows, [[1, 2], [3, 4]]);
3589

            
3590
$result = $dbi->select(table => 'table1');
3591
$rows = $result->fetch_hash_all;
3592
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
3593

            
3594
$result = $dbi->select(table => 'table1');
3595
$result->dbi->filters({three_times => sub { $_[0] * 3}});
3596
$result->filter({key1 => 'three_times'});
3597

            
3598
$rows = $result->fetch_all;
3599
is_deeply($rows, [[3, 2], [9, 4]], "array");
3600

            
3601
$result = $dbi->select(table => 'table1');
3602
$result->dbi->filters({three_times => sub { $_[0] * 3}});
3603
$result->filter({key1 => 'three_times'});
3604
$rows = $result->fetch_hash_all;
3605
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash");
3606

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3607
test "query_builder";
3608
$datas = [
3609
    # Basic tests
3610
    {   name            => 'placeholder basic',
3611
        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
3612
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;",
3613
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
3614
    },
3615
    {
3616
        name            => 'placeholder in',
3617
        source            => "{in k1 3};",
3618
        sql_expected    => "k1 in (?, ?, ?);",
3619
        columns_expected   => [qw/k1 k1 k1/]
3620
    },
3621
    
3622
    # Table name
3623
    {
3624
        name            => 'placeholder with table name',
3625
        source            => "{= a.k1} {= a.k2}",
3626
        sql_expected    => "a.k1 = ? a.k2 = ?;",
3627
        columns_expected  => [qw/a.k1 a.k2/]
3628
    },
3629
    {   
3630
        name            => 'placeholder in with table name',
3631
        source            => "{in a.k1 2} {in b.k2 2}",
3632
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?);",
3633
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
3634
    },
3635
    {
3636
        name            => 'not contain tag',
3637
        source            => "aaa",
3638
        sql_expected    => "aaa;",
3639
        columns_expected  => [],
3640
    }
3641
];
3642

            
3643
for (my $i = 0; $i < @$datas; $i++) {
3644
    my $data = $datas->[$i];
3645
    my $builder = DBIx::Custom->new->query_builder;
3646
    my $query = $builder->build_query($data->{source});
3647
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
3648
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
3649
}
3650

            
3651
$builder = DBIx::Custom->new->query_builder;
3652
$ret_val = $builder->register_tag(
3653
    p => sub {
3654
        my @args = @_;
3655
        
3656
        my $expand    = "? $args[0] $args[1]";
3657
        my $columns = [2];
3658
        return [$expand, $columns];
3659
    }
3660
);
3661

            
3662
$query = $builder->build_query("{p a b}");
3663
is($query->{sql}, "? a b;", "register_tag sql");
3664
is_deeply($query->{columns}, [2], "register_tag columns");
3665
isa_ok($ret_val, 'DBIx::Custom::QueryBuilder');
3666

            
3667
$builder = DBIx::Custom->new->query_builder;
3668

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

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

            
3675
$builder->register_tag({
3676
    q => 'string'
3677
});
3678

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

            
3682
$builder->register_tag({
3683
   r => sub {} 
3684
});
3685

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

            
3689
$builder->register_tag({
3690
   s => sub { return ["a", ""]} 
3691
});
3692

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

            
3696
$builder->register_tag(
3697
    t => sub {return ["a", []]}
3698
);
3699

            
3700

            
3701
test 'General error case';
3702
$builder = DBIx::Custom->new->query_builder;
3703
$builder->register_tag(
3704
    a => sub {
3705
        return ["? ? ?", ['']];
3706
    }
3707
);
3708
eval{$builder->build_query("{a}")};
3709
like($@, qr/\QPlaceholder count/, "placeholder count is invalid");
3710

            
3711

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

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

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

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

            
3727
test 'variouse source';
3728
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;";
3729
$query = $builder->build_query($source);
3730
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1");
3731

            
3732
$source = "abc;";
3733
$query = $builder->build_query($source);
3734
is($query->sql, 'abc;', "basic : 2");
3735

            
3736
$source = "{= a}";
3737
$query = $builder->build_query($source);
3738
is($query->sql, 'a = ?;', "only tag");
3739

            
3740
$source = "000;";
3741
$query = $builder->build_query($source);
3742
is($query->sql, '000;', "contain 0 value");
3743

            
3744
$source = "a {= b} }";
3745
eval{$builder->build_query($source)};
3746
like($@, qr/unexpected "}"/, "error : 1");
3747

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