DBIx-Custom / t / sqlite.t /
Newer Older
3711 lines | 115.715kb
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;
cleanup test
Yuki Kimoto authored on 2011-08-10
470
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
471
$dbi->dbh->begin_work;
472
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
473
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
474
$dbi->dbh->commit;
475
$result = $dbi->select(table => 'table1');
476
is_deeply(scalar $result->all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}],
477
          "commit");
478

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

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

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

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

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

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

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

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

            
538

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

            
543
$dbi->begin_work;
544

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

            
551
$dbi->rollback if $@;
552

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

            
557
$dbi->begin_work;
558

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

            
564
$dbi->commit unless $@;
565

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

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

            
575

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1021
$where = $dbi->where
1022
             ->clause(['or', ('key1 = :key1') x 2])
1023
             ->param({key1 => [1, 3]});
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
$where = $dbi->where
1032
             ->clause(['or', ('key1 = :key1') x 2])
1033
             ->param({key1 => [1]});
1034
$result = $dbi->select(
1035
    table => 'table1',
1036
    where => $where,
1037
);
1038
$row = $result->all;
1039
is_deeply($row, [{key1 => 1, key2 => 2}]);
1040

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1210

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1404

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

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

            
1413
$dbi->apply_filter(
1414

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1539
{
1540
    package MyDBI4;
1541

            
1542
    use strict;
1543
    use warnings;
1544

            
1545
    use base 'DBIx::Custom';
1546

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

            
1558
    package MyModel2::Base1;
1559

            
1560
    use strict;
1561
    use warnings;
1562

            
1563
    use base 'DBIx::Custom::Model';
1564

            
1565
    package MyModel2::book;
1566

            
1567
    use strict;
1568
    use warnings;
1569

            
1570
    use base 'MyModel2::Base1';
1571

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

            
1578
    sub list { shift->select; }
1579

            
1580
    package MyModel2::Company;
1581

            
1582
    use strict;
1583
    use warnings;
1584

            
1585
    use base 'MyModel2::Base1';
1586

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

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

            
1605
{
1606
     package MyDBI5;
1607

            
1608
    use strict;
1609
    use warnings;
1610

            
1611
    use base 'DBIx::Custom';
1612

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1834
test 'columns';
1835
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
1836
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1837
$model = $dbi->model('book');
1838

            
1839

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

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

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

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

            
1905

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

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

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

            
1955

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

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

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

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

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

            
1996

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

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

            
2016

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2336

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

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

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

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

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

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

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

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

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

            
2459

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

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

            
2481
test 'select() string where';
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});
2486
$rows = $dbi->select(
2487
    table => 'table1',
2488
    where => 'key1 = :key1 and key2 = :key2',
2489
    where_param => {key1 => 1, key2 => 2}
2490
)->all;
2491
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2492

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

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

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

            
2533

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

            
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->update(
2551
    table => 'table1',
2552
    param => {key1 => 5},
2553
    where => [
2554
        'key1 = :key1 and key2 = :key2',
2555
        {key1 => 1, key2 => 2}
2556
    ]
2557
);
2558
$rows = $dbi->select(table => 'table1')->all;
2559
is_deeply($rows, [{key1 => 5, key2 => 2}]);
2560

            
2561
test 'insert id and primary_key option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2562
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2563
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2564
$dbi->insert(
2565
    primary_key => ['key1', 'key2'], 
2566
    table => 'table1',
2567
    id => [1, 2],
2568
    param => {key3 => 3}
2569
);
2570
is($dbi->select(table => 'table1')->one->{key1}, 1);
2571
is($dbi->select(table => 'table1')->one->{key2}, 2);
2572
is($dbi->select(table => 'table1')->one->{key3}, 3);
2573

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

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

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

            
2598

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

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

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

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

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

            
2663

            
2664
test 'model update and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2665
$dbi = MyDBI6->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->model('table1')->update(
2669
    id => [1, 2],
2670
    param => {key3 => 4}
2671
);
2672
$result = $dbi->model('table1')->select;
2673
$row = $result->one;
2674
is($row->{key1}, 1);
2675
is($row->{key2}, 2);
2676
is($row->{key3}, 4);
2677

            
2678

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

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

            
2698

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

            
2714

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

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

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

            
2753

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

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

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

            
2786

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

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

            
2802

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

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

            
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->execute("create table table1 (key1 Date, key2 datetime)");
2830
$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1');
2831
$dbi->type_rule(
2832
    into1 => [
2833
        [qw/date datetime/] => sub { uc $_[0] }
2834
    ]
2835
);
2836
$result = $dbi->execute(
2837
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
2838
    param => {key1 => 'a', 'table1.key2' => 'b'}
2839
);
2840
$row = $result->one;
2841
is($row->{key1}, 'a');
2842
is($row->{key2}, 'B');
2843

            
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->insert({key1 => 'A', key2 => 'B'}, table => 'table1');
2847
$dbi->type_rule(
2848
    into1 => [
2849
        [qw/date datetime/] => sub { uc $_[0] }
2850
    ]
2851
);
2852
$result = $dbi->execute(
2853
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
2854
    param => {key1 => 'a', 'table1.key2' => 'b'},
2855
    table => 'table1'
2856
);
2857
$row = $result->one;
2858
is($row->{key1}, 'A');
2859
is($row->{key2}, 'B');
2860

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
3183
test 'separator';
test cleanup
Yuki Kimoto authored on 2011-08-10
3184
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3185
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
3186
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
3187

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

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

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

            
3239

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

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

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

            
3263

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

            
3271

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

            
3282

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

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

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

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

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

            
3326

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

            
3340

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
3627
$builder = DBIx::Custom->new->query_builder;
3628

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

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

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

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

            
3642
$builder->register_tag({
3643
   r => sub {} 
3644
});
3645

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

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

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

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

            
3660

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

            
3671

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

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

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

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

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

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

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

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

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

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