DBIx-Custom / t / sqlite.t /
Newer Older
3771 lines | 118.679kb
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-10
63
my $binary;
cleanup test
Yuki Kimoto authored on 2011-08-06
64

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

            
68
test 'update';
test cleanup
Yuki Kimoto authored on 2011-08-10
69
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
70
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
71
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
72
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
73
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1});
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 => 11, key3 => 3, key4 => 4, key5 => 5},
77
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
78
                  "basic");
79
                  
80
$dbi->execute("delete from table1");
81
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
82
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
83
$dbi->update(table => 'table1', param => {key2 => 12}, where => {key2 => 2, key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-06
84
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
85
$rows   = $result->all;
86
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
87
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
88
                  "update key same as search key");
89

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

            
97
$dbi->execute("delete from table1");
98
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
99
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
100
$dbi->register_filter(twice => sub { $_[0] * 2 });
101
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1},
102
              filter => {key2 => sub { $_[0] * 2 }});
test cleanup
Yuki Kimoto authored on 2011-08-06
103
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
104
$rows   = $result->all;
105
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5},
106
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
107
                  "filter");
108

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

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

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
117
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
118
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
119
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
120
$where = $dbi->where;
121
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
122
$where->param({key1 => 1, key2 => 2});
123
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
124
$result = $dbi->select(table => 'table1');
125
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
126

            
test cleanup
Yuki Kimoto authored on 2011-08-10
127
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
128
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
129
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
130
$dbi->update(
131
    table => 'table1',
132
    param => {key1 => 3},
133
    where => [
134
        ['and', 'key1 = :key1', 'key2 = :key2'],
135
        {key1 => 1, key2 => 2}
136
    ]
137
);
138
$result = $dbi->select(table => 'table1');
139
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
140

            
test cleanup
Yuki Kimoto authored on 2011-08-10
141
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
142
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
143
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
144
$where = $dbi->where;
145
$where->clause(['and', 'key2 = :key2']);
146
$where->param({key2 => 2});
147
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
148
$result = $dbi->select(table => 'table1');
149
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
150

            
151
eval{$dbi->update(table => 'table1', param => {';' => 1})};
152
like($@, qr/safety/);
153

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
157
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
158
$dbi->quote('"');
cleanup test
Yuki Kimoto authored on 2011-08-10
159
eval { $dbi->execute("drop table ${q}table$p") };
160
$dbi->execute("create table ${q}table$p (${q}select$p, ${q}update$p)");
cleanup test
Yuki Kimoto authored on 2011-08-06
161
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
162
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
163
$dbi->insert(table => 'table', param => {select => 1});
164
$dbi->update(table => 'table', where => {select => 1}, param => {update => 2});
cleanup test
Yuki Kimoto authored on 2011-08-10
165
$result = $dbi->execute("select * from ${q}table$p");
cleanup test
Yuki Kimoto authored on 2011-08-06
166
$rows   = $result->all;
167
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
168

            
169
eval {$dbi->update_all(table => 'table', param => {';' => 2}) };
170
like($@, qr/safety/);
171

            
test cleanup
Yuki Kimoto authored on 2011-08-10
172
eval { $dbi->execute("drop table ${q}table$p") };
cleanup test
Yuki Kimoto authored on 2011-08-06
173
$dbi->reserved_word_quote('"');
cleanup test
Yuki Kimoto authored on 2011-08-10
174
$dbi->execute("create table ${q}table$p (${q}select$p, ${q}update$p)");
cleanup test
Yuki Kimoto authored on 2011-08-06
175
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
176
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
177
$dbi->insert(table => 'table', param => {select => 1});
178
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
cleanup test
Yuki Kimoto authored on 2011-08-10
179
$result = $dbi->execute("select * from ${q}table$p");
cleanup test
Yuki Kimoto authored on 2011-08-06
180
$rows   = $result->all;
181
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
182

            
test cleanup
Yuki Kimoto authored on 2011-08-10
183
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
184
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
185
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
186
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
187
$dbi->update({key2 => 11}, table => 'table1', where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
188
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
189
$rows   = $result->all;
190
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
191
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
192
                  "basic");
193

            
test cleanup
Yuki Kimoto authored on 2011-08-10
194
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
195
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
196
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
197
$dbi->update(table => 'table1', param => {key2 => 4},
198
  where => {key1 => 1}, prefix => 'or replace');
test cleanup
Yuki Kimoto authored on 2011-08-06
199
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
200
$rows   = $result->all;
201
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
202

            
test cleanup
Yuki Kimoto authored on 2011-08-10
203
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
204
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
205
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
206
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
207
$dbi->update(table => 'table1', param => {key2 => \"'11'"}, where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
208
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
209
$rows   = $result->all;
210
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
211
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
212
                  "basic");
213

            
214
test 'update_all';
test cleanup
Yuki Kimoto authored on 2011-08-10
215
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
216
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
217
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
218
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
219
$dbi->register_filter(twice => sub { $_[0] * 2 });
220
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'});
test cleanup
Yuki Kimoto authored on 2011-08-06
221
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
222
$rows   = $result->all;
223
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
224
                  {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
225
                  "filter");
226

            
227

            
228
test 'delete';
test cleanup
Yuki Kimoto authored on 2011-08-10
229
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
230
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
231
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
232
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
233
$dbi->delete(table => 'table1', where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
234
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
235
$rows   = $result->all;
236
is_deeply($rows, [{key1 => 3, key2 => 4}], "basic");
237

            
238
$dbi->execute("delete from table1;");
239
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
240
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
241
$dbi->register_filter(twice => sub { $_[0] * 2 });
242
$dbi->delete(table => 'table1', where => {key2 => 1}, filter => {key2 => 'twice'});
test cleanup
Yuki Kimoto authored on 2011-08-06
243
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
244
$rows   = $result->all;
245
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter");
246

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

            
249
$dbi->delete_all(table => 'table1');
250
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
251
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
252
$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2});
253
$rows = $dbi->select(table => 'table1')->all;
254
is_deeply($rows, [{key1 => 3, key2 => 4}], "delete multi key");
255

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
259
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
260
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
261
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
262
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
263
$where = $dbi->where;
264
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
265
$where->param({ke1 => 1, key2 => 2});
266
$dbi->delete(table => 'table1', where => $where);
267
$result = $dbi->select(table => 'table1');
268
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
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);
cleanup test
Yuki Kimoto authored on 2011-08-06
272
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
273
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
274
$dbi->delete(
275
    table => 'table1',
276
    where => [
277
        ['and', 'key1 = :key1', 'key2 = :key2'],
278
        {ke1 => 1, key2 => 2}
279
    ]
280
);
281
$result = $dbi->select(table => 'table1');
282
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
283

            
test cleanup
Yuki Kimoto authored on 2011-08-10
284
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
285
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
286
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
287
$dbi->delete(table => 'table1', where => {key1 => 1}, prefix => '    ');
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, [], "basic");
291

            
292
test 'delete error';
test cleanup
Yuki Kimoto authored on 2011-08-10
293
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
294
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
295
eval{$dbi->delete(table => 'table1')};
296
like($@, qr/"where" must be specified/,
297
         "where key-value pairs not specified");
298

            
299
eval{$dbi->delete(table => 'table1', where => {';' => 1})};
300
like($@, qr/safety/);
301

            
test cleanup
Yuki Kimoto authored on 2011-08-10
302
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
303
$dbi->quote('"');
cleanup test
Yuki Kimoto authored on 2011-08-10
304
eval { $dbi->execute("drop table ${q}table$p") };
305
$dbi->execute("create table ${q}table$p (${q}select$p, ${q}update$p)");
cleanup test
Yuki Kimoto authored on 2011-08-06
306
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
307
$dbi->insert(table => 'table', param => {select => 1});
308
$dbi->delete(table => 'table', where => {select => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
309
$result = $dbi->execute("select * from ${q}table$p");
cleanup test
Yuki Kimoto authored on 2011-08-06
310
$rows   = $result->all;
311
is_deeply($rows, [], "reserved word");
312

            
313
test 'delete_all';
test cleanup
Yuki Kimoto authored on 2011-08-10
314
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
315
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
316
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
317
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
318
$dbi->delete_all(table => 'table1');
test cleanup
Yuki Kimoto authored on 2011-08-06
319
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
320
$rows   = $result->all;
321
is_deeply($rows, [], "basic");
322

            
323

            
324
test 'select';
test cleanup
Yuki Kimoto authored on 2011-08-10
325
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
326
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
327
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
328
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
329
$rows = $dbi->select(table => 'table1')->all;
330
is_deeply($rows, [{key1 => 1, key2 => 2},
331
                  {key1 => 3, key2 => 4}], "table");
332

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

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

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

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

            
345
$dbi->register_filter(decrement => sub { $_[0] - 1 });
346
$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'})
347
            ->all;
348
is_deeply($rows, [{key1 => 1, key2 => 2}], "filter");
349

            
cleanup test
Yuki Kimoto authored on 2011-08-06
350
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
351
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
352
$rows = $dbi->select(
353
    table => [qw/table1 table2/],
354
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
355
    where   => {'table1.key2' => 2},
356
    relation  => {'table1.key1' => 'table2.key1'}
357
)->all;
358
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : exists where");
359

            
360
$rows = $dbi->select(
361
    table => [qw/table1 table2/],
362
    column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
363
    relation  => {'table1.key1' => 'table2.key1'}
364
)->all;
365
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : no exists where");
366

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

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

            
379
test 'fetch filter';
test cleanup
Yuki Kimoto authored on 2011-08-10
380
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
381
$dbi->register_filter(
382
    twice       => sub { $_[0] * 2 },
383
    three_times => sub { $_[0] * 3 }
384
);
385
$dbi->default_fetch_filter('twice');
cleanup test
Yuki Kimoto authored on 2011-08-10
386
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
387
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
388
$result = $dbi->select(table => 'table1');
389
$result->filter({key1 => 'three_times'});
390
$row = $result->one;
391
is_deeply($row, {key1 => 3, key2 => 4}, "default_fetch_filter and filter");
392

            
393
test 'filters';
394
$dbi = DBIx::Custom->new;
395

            
396
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')),
397
   'あ', "decode_utf8");
398

            
399
is($dbi->filters->{encode_utf8}->('あ'),
400
   encode_utf8('あ'), "encode_utf8");
401

            
402
test 'transaction';
test cleanup
Yuki Kimoto authored on 2011-08-10
403
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
404
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
405
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
406
$dbi->dbh->begin_work;
407
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
408
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
409
$dbi->dbh->commit;
410
$result = $dbi->select(table => 'table1');
411
is_deeply(scalar $result->all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}],
412
          "commit");
413

            
test cleanup
Yuki Kimoto authored on 2011-08-10
414
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
415
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
416
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
417
$dbi->dbh->begin_work(0);
418
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
419
$dbi->dbh->rollback;
420

            
421
$result = $dbi->select(table => 'table1');
422
ok(! $result->fetch_first, "rollback");
423

            
424
test 'cache';
test cleanup
Yuki Kimoto authored on 2011-08-10
425
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
426
$dbi->cache(1);
cleanup test
Yuki Kimoto authored on 2011-08-10
427
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
428
$source = 'select * from table1 where key1 = :key1 and key2 = :key2;';
429
$dbi->execute($source, {}, query => 1);
430
is_deeply($dbi->{_cached}->{$source}, 
431
          {sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2'], tables => []}, "cache");
432

            
test cleanup
Yuki Kimoto authored on 2011-08-10
433
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
434
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
435
$dbi->{_cached} = {};
436
$dbi->cache(0);
437
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
438
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
439

            
440
test 'execute';
test cleanup
Yuki Kimoto authored on 2011-08-10
441
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
442
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
443
{
444
    local $Carp::Verbose = 0;
445
    eval{$dbi->execute('select * frm table1')};
446
    like($@, qr/\Qselect * frm table1;/, "fail prepare");
447
    like($@, qr/\.t /, "fail : not verbose");
448
}
449
{
450
    local $Carp::Verbose = 1;
451
    eval{$dbi->execute('select * frm table1')};
452
    like($@, qr/Custom.*\.t /s, "fail : verbose");
453
}
454

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

            
458
$query = $dbi->execute('select * from table1 where key1 = :key1', {}, query => 1);
459
$dbi->dbh->disconnect;
460
eval{$dbi->execute($query, param => {key1 => {a => 1}})};
461
ok($@, "execute fail");
462

            
463
{
464
    local $Carp::Verbose = 0;
465
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
466
    like($@, qr/\Q.t /, "caller spec : not vebose");
467
}
468
{
469
    local $Carp::Verbose = 1;
470
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
471
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
472
}
473

            
474

            
475
test 'transaction';
test cleanup
Yuki Kimoto authored on 2011-08-10
476
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
477
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
478
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
479

            
480
$dbi->begin_work;
481

            
482
eval {
483
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
484
    die "Error";
485
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
486
};
487

            
488
$dbi->rollback if $@;
489

            
490
$result = $dbi->select(table => 'table1');
491
$rows = $result->all;
492
is_deeply($rows, [], "rollback");
493

            
494
$dbi->begin_work;
495

            
496
eval {
497
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
498
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
499
};
500

            
501
$dbi->commit unless $@;
502

            
503
$result = $dbi->select(table => 'table1');
504
$rows = $result->all;
505
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "commit");
506

            
507
$dbi->dbh->{AutoCommit} = 0;
508
eval{ $dbi->begin_work };
509
ok($@, "exception");
510
$dbi->dbh->{AutoCommit} = 1;
511

            
512

            
513
test 'method';
514
$dbi->method(
515
    one => sub { 1 }
516
);
517
$dbi->method(
518
    two => sub { 2 }
519
);
520
$dbi->method({
521
    twice => sub {
522
        my $self = shift;
523
        return $_[0] * 2;
524
    }
525
});
526

            
527
is($dbi->one, 1, "first");
528
is($dbi->two, 2, "second");
529
is($dbi->twice(5), 10 , "second");
530

            
531
eval {$dbi->XXXXXX};
532
ok($@, "not exists");
533

            
534
test 'out filter';
test cleanup
Yuki Kimoto authored on 2011-08-10
535
$dbi = DBIx::Custom->connect;
536
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
537
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
538
$dbi->register_filter(twice => sub { $_[0] * 2 });
539
$dbi->register_filter(three_times => sub { $_[0] * 3});
540
$dbi->apply_filter(
541
    'table1', 'key1' => {out => 'twice', in => 'three_times'}, 
542
              'key2' => {out => 'three_times', in => 'twice'});
543
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-06
544
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
545
$row   = $result->fetch_hash_first;
546
is_deeply($row, {key1 => 2, key2 => 6}, "insert");
547
$result = $dbi->select(table => 'table1');
548
$row   = $result->one;
549
is_deeply($row, {key1 => 6, key2 => 12}, "insert");
550

            
test cleanup
Yuki Kimoto authored on 2011-08-10
551
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
552
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
553
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
554
$dbi->register_filter(twice => sub { $_[0] * 2 });
555
$dbi->register_filter(three_times => sub { $_[0] * 3});
556
$dbi->apply_filter(
557
    'table1', 'key1' => {out => 'twice', in => 'three_times'}, 
558
              'key2' => {out => 'three_times', in => 'twice'});
559
$dbi->apply_filter(
560
    'table1', 'key1' => {out => undef}
561
); 
562
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-06
563
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
564
$row   = $result->one;
565
is_deeply($row, {key1 => 1, key2 => 6}, "insert");
566

            
test cleanup
Yuki Kimoto authored on 2011-08-10
567
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
568
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
569
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
570
$dbi->register_filter(twice => sub { $_[0] * 2 });
571
$dbi->apply_filter(
572
    'table1', 'key1' => {out => 'twice', in => 'twice'}
573
);
574
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => undef});
575
$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-06
576
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
577
$row   = $result->one;
578
is_deeply($row, {key1 => 4, key2 => 2}, "update");
579

            
test cleanup
Yuki Kimoto authored on 2011-08-10
580
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
581
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
582
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
583
$dbi->register_filter(twice => sub { $_[0] * 2 });
584
$dbi->apply_filter(
585
    'table1', 'key1' => {out => 'twice', in => 'twice'}
586
);
587
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1=> undef});
588
$dbi->delete(table => 'table1', where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
589
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
590
$rows   = $result->all;
591
is_deeply($rows, [], "delete");
592

            
test cleanup
Yuki Kimoto authored on 2011-08-10
593
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
594
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
595
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
596
$dbi->register_filter(twice => sub { $_[0] * 2 });
597
$dbi->apply_filter(
598
    'table1', 'key1' => {out => 'twice', in => 'twice'}
599
);
600
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
601
$result = $dbi->select(table => 'table1', where => {key1 => 1});
602
$result->filter({'key2' => 'twice'});
603
$rows   = $result->all;
604
is_deeply($rows, [{key1 => 4, key2 => 4}], "select");
605

            
test cleanup
Yuki Kimoto authored on 2011-08-10
606
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
607
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
608
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
609
$dbi->register_filter(twice => sub { $_[0] * 2 });
610
$dbi->apply_filter(
611
    'table1', 'key1' => {out => 'twice', in => 'twice'}
612
);
613
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
614
$result = $dbi->execute("select * from table1 where key1 = :key1 and key2 = :key2;",
615
                        param => {key1 => 1, key2 => 2},
616
                        table => ['table1']);
617
$rows   = $result->all;
618
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute");
619

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
633
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
634
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
635
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
636
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
637
$dbi->register_filter(twice => sub { $_[0] * 2 });
638
$dbi->register_filter(three_times => sub { $_[0] * 3 });
639
$dbi->apply_filter(
640
    'table1', 'key2' => {out => 'twice', in => 'twice'}
641
);
642
$dbi->apply_filter(
643
    'table2', 'key3' => {out => 'three_times', in => 'three_times'}
644
);
645
$dbi->insert(table => 'table1', param => {key1 => 5, key2 => 2}, filter => {key2 => undef});
646
$dbi->insert(table => 'table2', param => {key1 => 5, key3 => 6}, filter => {key3 => undef});
647
$result = $dbi->select(
648
     table => ['table1', 'table2'],
649
     column => ['key2', 'key3'],
650
     where => {'table1.key2' => 1, 'table2.key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
651

            
652
$result->filter({'key2' => 'twice'});
653
$rows   = $result->all;
654
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join");
655

            
656
$result = $dbi->select(
657
     table => ['table1', 'table2'],
658
     column => ['key2', 'key3'],
659
     where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
660

            
661
$result->filter({'key2' => 'twice'});
662
$rows   = $result->all;
663
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join : omit");
664

            
665
test 'each_column';
test cleanup
Yuki Kimoto authored on 2011-08-10
666
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
667
eval { $dbi->execute('drop table table1') };
668
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-06
669
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
670
$dbi->execute('create table table1 (key1 Date, key2 datetime);');
cleanup test
Yuki Kimoto authored on 2011-08-06
671

            
672
$infos = [];
673
$dbi->each_column(sub {
674
    my ($self, $table, $column, $cinfo) = @_;
675
    
676
    if ($table =~ /^table/) {
677
         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
678
         push @$infos, $info;
679
    }
680
});
681
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
682
is_deeply($infos, 
683
    [
684
        ['table1', 'key1', 'key1'],
685
        ['table1', 'key2', 'key2'],
686
        ['table2', 'key1', 'key1'],
687
        ['table2', 'key3', 'key3']
688
    ]
689
    
690
);
691
test 'each_table';
test cleanup
Yuki Kimoto authored on 2011-08-10
692
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
693
eval { $dbi->execute('drop table table1') };
694
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-06
695
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
696
$dbi->execute('create table table1 (key1 Date, key2 datetime);');
cleanup test
Yuki Kimoto authored on 2011-08-06
697

            
698
$infos = [];
699
$dbi->each_table(sub {
700
    my ($self, $table, $table_info) = @_;
701
    
702
    if ($table =~ /^table/) {
703
         my $info = [$table, $table_info->{TABLE_NAME}];
704
         push @$infos, $info;
705
    }
706
});
707
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
708
is_deeply($infos, 
709
    [
710
        ['table1', 'table1'],
711
        ['table2', 'table2'],
712
    ]
713
);
714

            
715
test 'limit';
test cleanup
Yuki Kimoto authored on 2011-08-10
716
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
717
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
718
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
719
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
720
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
721
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
722
$dbi->register_tag(
723
    limit => sub {
724
        my ($count, $offset) = @_;
725
        
726
        my $s = '';
727
        $s .= "limit $count";
728
        $s .= " offset $offset" if defined $offset;
729
        
730
        return [$s, []];
731
    }
732
);
733
$rows = $dbi->select(
734
  table => 'table1',
735
  where => {key1 => 1},
736
  append => "order by key2 {limit 1 0}"
737
)->all;
738
is_deeply($rows, [{key1 => 1, key2 => 2}]);
739
$rows = $dbi->select(
740
  table => 'table1',
741
  where => {key1 => 1},
742
  append => "order by key2 {limit 2 1}"
743
)->all;
744
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
745
$rows = $dbi->select(
746
  table => 'table1',
747
  where => {key1 => 1},
748
  append => "order by key2 {limit 1}"
749
)->all;
750
is_deeply($rows, [{key1 => 1, key2 => 2}]);
751

            
752
test 'connect super';
753
{
754
    package MyDBI;
755
    
756
    use base 'DBIx::Custom';
757
    sub connect {
758
        my $self = shift->SUPER::connect(@_);
759
        
760
        return $self;
761
    }
762
    
763
    sub new {
764
        my $self = shift->SUPER::new(@_);
765
        
766
        return $self;
767
    }
768
}
769

            
test cleanup
Yuki Kimoto authored on 2011-08-10
770
$dbi = MyDBI->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
771
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
772
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
773
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
774
is($dbi->select(table => 'table1')->one->{key1}, 1);
775

            
test cleanup
Yuki Kimoto authored on 2011-08-10
776
$dbi = MyDBI->new;
cleanup test
Yuki Kimoto authored on 2011-08-06
777
$dbi->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
778
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
779
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
780
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
781
is($dbi->select(table => 'table1')->one->{key1}, 1);
782

            
783
{
784
    package MyDBI2;
785
    
786
    use base 'DBIx::Custom';
787
    sub connect {
788
        my $self = shift->SUPER::new(@_);
789
        $self->connect;
790
        
791
        return $self;
792
    }
793
}
794

            
test cleanup
Yuki Kimoto authored on 2011-08-10
795
$dbi = MyDBI->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
796
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
797
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
798
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
799
is($dbi->select(table => 'table1')->one->{key1}, 1);
800

            
801
test 'end_filter';
test cleanup
Yuki Kimoto authored on 2011-08-10
802
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
803
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
804
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
805
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
806
$result = $dbi->select(table => 'table1');
807
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
808
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 });
809
$row = $result->fetch_first;
810
is_deeply($row, [6, 40]);
811

            
test cleanup
Yuki Kimoto authored on 2011-08-10
812
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
813
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
814
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
815
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
816
$result = $dbi->select(table => 'table1');
817
$result->filter([qw/key1 key2/] => sub { $_[0] * 2 });
818
$result->end_filter([[qw/key1 key2/] => sub { $_[0] * 3 }]);
819
$row = $result->fetch_first;
820
is_deeply($row, [6, 12]);
821

            
test cleanup
Yuki Kimoto authored on 2011-08-10
822
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
823
eval { $dbi->execute('drop table table1') };
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
$result = $dbi->select(table => 'table1');
827
$result->filter([[qw/key1 key2/] => sub { $_[0] * 2 }]);
828
$result->end_filter([qw/key1 key2/] => sub { $_[0] * 3 });
829
$row = $result->fetch_first;
830
is_deeply($row, [6, 12]);
831

            
832
$dbi->register_filter(five_times => sub { $_[0] * 5 });
833
$result = $dbi->select(table => 'table1');
834
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
835
$result->end_filter({key1 => sub { $_[0] * 3 }, key2 => 'five_times' });
836
$row = $result->one;
837
is_deeply($row, {key1 => 6, key2 => 40});
838

            
839
$dbi->register_filter(five_times => sub { $_[0] * 5 });
840
$dbi->apply_filter('table1',
841
    key1 => {end => sub { $_[0] * 3 } },
842
    key2 => {end => 'five_times'}
843
);
844
$result = $dbi->select(table => 'table1');
845
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
846
$row = $result->one;
847
is_deeply($row, {key1 => 6, key2 => 40}, 'apply_filter');
848

            
849
$dbi->register_filter(five_times => sub { $_[0] * 5 });
850
$dbi->apply_filter('table1',
851
    key1 => {end => sub { $_[0] * 3 } },
852
    key2 => {end => 'five_times'}
853
);
854
$result = $dbi->select(table => 'table1');
855
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
856
$result->filter(key1 => undef);
857
$result->end_filter(key1 => undef);
858
$row = $result->one;
859
is_deeply($row, {key1 => 1, key2 => 40}, 'apply_filter overwrite');
860

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

            
875
test 'empty where select';
test cleanup
Yuki Kimoto authored on 2011-08-10
876
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
877
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
878
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
879
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
880
$result = $dbi->select(table => 'table1', where => {});
881
$row = $result->one;
882
is_deeply($row, {key1 => 1, key2 => 2});
883

            
884
test 'select query option';
test cleanup
Yuki Kimoto authored on 2011-08-10
885
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
886
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
887
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
888
$query = $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, query => 1);
889
is(ref $query, 'DBIx::Custom::Query');
890
$query = $dbi->update(table => 'table1', where => {key1 => 1}, param => {key2 => 2}, query => 1);
891
is(ref $query, 'DBIx::Custom::Query');
892
$query = $dbi->delete(table => 'table1', where => {key1 => 1}, query => 1);
893
is(ref $query, 'DBIx::Custom::Query');
894
$query = $dbi->select(table => 'table1', where => {key1 => 1, key2 => 2}, query => 1);
895
is(ref $query, 'DBIx::Custom::Query');
896

            
map cleanup
Yuki Kimoto authored on 2011-08-09
897
test 'where';
test cleanup
Yuki Kimoto authored on 2011-08-10
898
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
899
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
900
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
901
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
902
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
903
$where = $dbi->where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
904
is("$where", "where ( key1 = :key1 and key2 = :key2 )", 'no param');
905

            
906
$where = $dbi->where
907
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
908
             ->param({key1 => 1});
909

            
910
$result = $dbi->select(
911
    table => 'table1',
912
    where => $where
913
);
914
$row = $result->all;
915
is_deeply($row, [{key1 => 1, key2 => 2}]);
916

            
917
$result = $dbi->select(
918
    table => 'table1',
919
    where => [
920
        ['and', 'key1 = :key1', 'key2 = :key2'],
921
        {key1 => 1}
922
    ]
923
);
924
$row = $result->all;
925
is_deeply($row, [{key1 => 1, key2 => 2}]);
926

            
927
$where = $dbi->where
928
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
929
             ->param({key1 => 1, key2 => 2});
930
$result = $dbi->select(
931
    table => 'table1',
932
    where => $where
933
);
934
$row = $result->all;
935
is_deeply($row, [{key1 => 1, key2 => 2}]);
936

            
937
$where = $dbi->where
938
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
939
             ->param({});
940
$result = $dbi->select(
941
    table => 'table1',
942
    where => $where,
943
);
944
$row = $result->all;
945
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
946

            
947
$where = $dbi->where
948
             ->clause(['and', ['or', 'key1 > :key1', 'key1 < :key1'], 'key2 = :key2'])
949
             ->param({key1 => [0, 3], key2 => 2});
950
$result = $dbi->select(
951
    table => 'table1',
952
    where => $where,
953
); 
954
$row = $result->all;
955
is_deeply($row, [{key1 => 1, key2 => 2}]);
956

            
957
$where = $dbi->where;
958
$result = $dbi->select(
959
    table => 'table1',
960
    where => $where
961
);
962
$row = $result->all;
963
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
964

            
965
eval {
966
$where = $dbi->where
967
             ->clause(['uuu']);
968
$result = $dbi->select(
969
    table => 'table1',
970
    where => $where
971
);
972
};
973
ok($@);
974

            
975
$where = $dbi->where;
976
is("$where", '');
977

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

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

            
998
$where = $dbi->where
999
             ->clause(['or', ('key1 = :key1') x 2])
1000
             ->param({key1 => 1});
1001
$result = $dbi->select(
1002
    table => 'table1',
1003
    where => $where,
1004
);
1005
$row = $result->all;
1006
is_deeply($row, [{key1 => 1, key2 => 2}]);
1007

            
1008
$where = $dbi->where
1009
             ->clause('key1 = :key1')
1010
             ->param({key1 => 1});
1011
$result = $dbi->select(
1012
    table => 'table1',
1013
    where => $where,
1014
);
1015
$row = $result->all;
1016
is_deeply($row, [{key1 => 1, key2 => 2}]);
1017

            
1018
$where = $dbi->where
1019
             ->clause('key1 = :key1 key2 = :key2')
1020
             ->param({key1 => 1});
1021
eval{$where->to_string};
1022
like($@, qr/one column/);
1023

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

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

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

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

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

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

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

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

            
1104
$where = $dbi->where
1105
             ->clause(['and', '{> key1}', '{< key1}' ])
1106
             ->param({key1 => [2, $dbi->not_exists]});
1107
$result = $dbi->select(
1108
    table => 'table1',
1109
    where => $where,
1110
);
1111
$row = $result->all;
1112
is_deeply($row, [{key1 => 3, key2 => 4}], 'not_exists');
1113

            
1114
$where = $dbi->where
1115
             ->clause(['and', '{> key1}', '{< key1}' ])
1116
             ->param({key1 => [$dbi->not_exists, 2]});
1117
$result = $dbi->select(
1118
    table => 'table1',
1119
    where => $where,
1120
);
1121
$row = $result->all;
1122
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1123

            
1124
$where = $dbi->where
1125
             ->clause(['and', '{> key1}', '{< key1}' ])
1126
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists]});
1127
$result = $dbi->select(
1128
    table => 'table1',
1129
    where => $where,
1130
);
1131
$row = $result->all;
1132
is_deeply($row, [{key1 => 1, key2 => 2},{key1 => 3, key2 => 4}], 'not_exists');
1133

            
1134
$where = $dbi->where
1135
             ->clause(['and', '{> key1}', '{< key1}' ])
1136
             ->param({key1 => [0, 2]});
1137
$result = $dbi->select(
1138
    table => 'table1',
1139
    where => $where,
1140
);
1141
$row = $result->all;
1142
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1143

            
1144
$where = $dbi->where
1145
             ->clause(['and', 'key1 is not null', 'key2 is not null' ]);
1146
$result = $dbi->select(
1147
    table => 'table1',
1148
    where => $where,
1149
);
1150
$row = $result->all;
1151
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1152

            
1153
eval {$dbi->where(ppp => 1) };
1154
like($@, qr/invalid/);
1155

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

            
1167

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

            
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
1179
$where = $dbi->where;
1180
$where->clause(['and', ':key1{=}']);
1181
$where->param({key1 => undef});
1182
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1183
$row = $result->all;
1184
is_deeply($row, [{key1 => 1, key2 => 2}]);
1185

            
1186
$where = $dbi->where;
1187
$where->clause(['and', ':key1{=}']);
1188
$where->param({key1 => undef});
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
1189
$where->if('defined');
map cleanup
Yuki Kimoto authored on 2011-08-09
1190
$where->map;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
1191
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1192
$row = $result->all;
1193
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1194

            
1195
$where = $dbi->where;
1196
$where->clause(['or', ':key1{=}', ':key1{=}']);
1197
$where->param({key1 => [undef, undef]});
1198
$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]});
1199
$row = $result->all;
1200
is_deeply($row, [{key1 => 1, key2 => 2}]);
1201
$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]});
1202
$row = $result->all;
1203
is_deeply($row, [{key1 => 1, key2 => 2}]);
1204

            
1205
$where = $dbi->where;
1206
$where->clause(['and', ':key1{=}']);
1207
$where->param({key1 => [undef, undef]});
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
1208
$where->if('defined');
map cleanup
Yuki Kimoto authored on 2011-08-09
1209
$where->map;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
1210
$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]});
1211
$row = $result->all;
1212
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1213
$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]});
1214
$row = $result->all;
1215
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1216

            
1217
$where = $dbi->where;
1218
$where->clause(['and', ':key1{=}']);
1219
$where->param({key1 => 0});
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
1220
$where->if('length');
map cleanup
Yuki Kimoto authored on 2011-08-09
1221
$where->map;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
1222
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1223
$row = $result->all;
1224
is_deeply($row, [{key1 => 1, key2 => 2}]);
1225

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

            
1235
$where = $dbi->where;
1236
$where->clause(['and', ':key1{=}']);
1237
$where->param({key1 => 5});
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
1238
$where->if(sub { ($_[0] || '') eq 5 });
map cleanup
Yuki Kimoto authored on 2011-08-09
1239
$where->map;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
1240
$result = $dbi->execute("select * from table1 $where", {key1 => 1});
1241
$row = $result->all;
1242
is_deeply($row, [{key1 => 1, key2 => 2}]);
1243

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

            
added tests
Yuki Kimoto authored on 2011-08-09
1253
$where = $dbi->where;
1254
$where->param({id => 1, author => 'Ken', price => 1900});
1255
$where->map(id => 'book.id',
1256
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1257
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1258
);
1259
is_deeply($where->param, {'book.id' => 1, 'book.author' => '%Ken%',
1260
  'book.price' => 1900});
1261

            
1262
$where = $dbi->where;
1263
$where->param({id => 0, author => 0, price => 0});
1264
$where->map(
1265
    id => 'book.id',
1266
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1267
    price => ['book.price', sub { '%' . $_[0] . '%' },
1268
      {if => sub { $_[0] eq 0 }}]
1269
);
1270
is_deeply($where->param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
1271

            
1272
$where = $dbi->where;
1273
$where->param({id => '', author => '', price => ''});
1274
$where->if('length');
1275
$where->map(
1276
    id => 'book.id',
1277
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1278
    price => ['book.price', sub { '%' . $_[0] . '%' },
1279
      {if => sub { $_[0] eq 1 }}]
1280
);
1281
is_deeply($where->param, {});
1282

            
1283
$where = $dbi->where;
1284
$where->param({id => undef, author => undef, price => undef});
1285
$where->if('length');
1286
$where->map(
1287
    id => 'book.id',
1288
    price => ['book.price', {if => 'exists'}]
1289
);
1290
is_deeply($where->param, {'book.price' => undef});
1291

            
1292
$where = $dbi->where;
1293
$where->param({price => 'a'});
1294
$where->if('length');
1295
$where->map(
1296
    id => ['book.id', {if => 'exists'}],
1297
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
1298
);
1299
is_deeply($where->param, {'book.price' => '%a'});
1300

            
fixed if is not converted to...
Yuki Kimoto authored on 2011-08-09
1301
$where = $dbi->where;
1302
$where->param({id => [1, 2], author => 'Ken', price => 1900});
1303
$where->map(
1304
    id => 'book.id',
1305
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1306
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1307
);
1308
is_deeply($where->param, {'book.id' => [1, 2], 'book.author' => '%Ken%',
1309
  'book.price' => 1900});
1310

            
1311
$where = $dbi->where;
1312
$where->if('length');
1313
$where->param({id => ['', ''], author => 'Ken', price => 1900});
1314
$where->map(
1315
    id => 'book.id',
1316
    author => ['book.author', sub { '%' . $_[0] . '%' }],
1317
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1318
);
1319
is_deeply($where->param, {'book.id' => [$dbi->not_exists, $dbi->not_exists], 'book.author' => '%Ken%',
1320
  'book.price' => 1900});
1321

            
1322
$where = $dbi->where;
1323
$where->param({id => ['', ''], author => 'Ken', price => 1900});
1324
$where->map(
1325
    id => ['book.id', {if => 'length'}],
1326
    author => ['book.author', sub { '%' . $_[0] . '%' }, {if => 'defined'}],
1327
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1328
);
1329
is_deeply($where->param, {'book.id' => [$dbi->not_exists, $dbi->not_exists], 'book.author' => '%Ken%',
1330
  'book.price' => 1900});
cleanup test
Yuki Kimoto authored on 2011-08-06
1331

            
1332
test 'dbi_option default';
1333
$dbi = DBIx::Custom->new;
1334
is_deeply($dbi->dbi_option, {});
1335

            
1336
test 'register_tag_processor';
test cleanup
Yuki Kimoto authored on 2011-08-10
1337
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1338
$dbi->register_tag_processor(
1339
    a => sub { 1 }
1340
);
1341
is($dbi->query_builder->tag_processors->{a}->(), 1);
1342

            
1343
test 'register_tag';
test cleanup
Yuki Kimoto authored on 2011-08-10
1344
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1345
$dbi->register_tag(
1346
    b => sub { 2 }
1347
);
1348
is($dbi->query_builder->tags->{b}->(), 2);
1349

            
1350
test 'table not specify exception';
test cleanup
Yuki Kimoto authored on 2011-08-10
1351
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1352
eval {$dbi->insert};
1353
like($@, qr/table/);
1354
eval {$dbi->update};
1355
like($@, qr/table/);
1356
eval {$dbi->delete};
1357
like($@, qr/table/);
1358
eval {$dbi->select};
1359
like($@, qr/table/);
1360

            
1361

            
1362
test 'more tests';
test cleanup
Yuki Kimoto authored on 2011-08-10
1363
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1364
eval{$dbi->apply_filter('table', 'column', [])};
1365
like($@, qr/apply_filter/);
1366

            
1367
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
1368
like($@, qr/apply_filter/);
1369

            
1370
$dbi->apply_filter(
1371

            
1372
);
test cleanup
Yuki Kimoto authored on 2011-08-10
1373
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1374
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1375
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1376
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1377
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1378
$dbi->apply_filter('table1', 'key2', 
1379
                   {in => sub { $_[0] * 3 }, out => sub { $_[0] * 2 }});
1380
$rows = $dbi->select(table => 'table1', where => {key2 => 1})->all;
1381
is_deeply($rows, [{key1 => 1, key2 => 6}]);
1382

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1383
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1384
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1385
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1386
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1387
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1388
$dbi->apply_filter('table1', 'key2', {});
1389
$rows = $dbi->select(table => 'table1', where => {key2 => 2})->all;
1390
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1391

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1392
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1393
eval {$dbi->apply_filter('table1', 'key2', {out => 'no'})};
1394
like($@, qr/not registered/);
1395
eval {$dbi->apply_filter('table1', 'key2', {in => 'no'})};
1396
like($@, qr/not registered/);
1397
$dbi->method({one => sub { 1 }});
1398
is($dbi->one, 1);
1399

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1403
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1404
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1405
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1406
$dbi->register_filter(twice => sub { $_[0] * 2 });
1407
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
1408
             filter => {key1 => 'twice'});
1409
$row = $dbi->select(table => 'table1')->one;
1410
is_deeply($row, {key1 => 2, key2 => 2});
1411
eval {$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
1412
             filter => {key1 => 'no'}) };
1413
like($@, qr//);
1414

            
1415
$dbi->register_filter(one => sub { });
1416
$dbi->default_fetch_filter('one');
1417
ok($dbi->default_fetch_filter);
1418
$dbi->default_bind_filter('one');
1419
ok($dbi->default_bind_filter);
1420
eval{$dbi->default_fetch_filter('no')};
1421
like($@, qr/not registered/);
1422
eval{$dbi->default_bind_filter('no')};
1423
like($@, qr/not registered/);
1424
$dbi->default_bind_filter(undef);
1425
ok(!defined $dbi->default_bind_filter);
1426
$dbi->default_fetch_filter(undef);
1427
ok(!defined $dbi->default_fetch_filter);
1428
eval {$dbi->execute('select * from table1 {} {= author') };
1429
like($@, qr/Tag not finished/);
1430

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1431
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1432
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1433
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1434
$dbi->register_filter(one => sub { 1 });
1435
$result = $dbi->select(table => 'table1');
1436
eval {$result->filter(key1 => 'no')};
1437
like($@, qr/not registered/);
1438
eval {$result->end_filter(key1 => 'no')};
1439
like($@, qr/not registered/);
1440
$result->default_filter(undef);
1441
ok(!defined $result->default_filter);
1442
$result->default_filter('one');
1443
is($result->default_filter->(), 1);
1444

            
1445
test 'dbi_option';
test cleanup
Yuki Kimoto authored on 2011-08-10
1446
$dbi = DBIx::Custom->connect(dbi_option => {PrintError => 1});
cleanup test
Yuki Kimoto authored on 2011-08-06
1447
ok($dbi->dbh->{PrintError});
test cleanup
Yuki Kimoto authored on 2011-08-10
1448
$dbi = DBIx::Custom->connect(dbi_options => {PrintError => 1});
cleanup test
Yuki Kimoto authored on 2011-08-06
1449
ok($dbi->dbh->{PrintError});
1450

            
1451
test 'DBIx::Custom::Result stash()';
1452
$result = DBIx::Custom::Result->new;
1453
is_deeply($result->stash, {}, 'default');
1454
$result->stash->{foo} = 1;
1455
is($result->stash->{foo}, 1, 'get and set');
1456

            
1457
test 'filter __ expression';
test cleanup
Yuki Kimoto authored on 2011-08-10
1458
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1459
eval { $dbi->execute('drop table company') };
1460
eval { $dbi->execute('drop table location') };
cleanup test
Yuki Kimoto authored on 2011-08-06
1461
$dbi->execute('create table company (id, name, location_id)');
1462
$dbi->execute('create table location (id, name)');
1463
$dbi->apply_filter('location',
1464
  name => {in => sub { uc $_[0] } }
1465
);
1466

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

            
1470
$result = $dbi->select(
1471
    table => ['company', 'location'], relation => {'company.location_id' => 'location.id'},
1472
    column => ['location.name as location__name']
1473
);
1474
is($result->fetch_first->[0], 'B');
1475

            
1476
$result = $dbi->select(
1477
    table => 'company', relation => {'company.location_id' => 'location.id'},
1478
    column => ['location.name as location__name']
1479
);
1480
is($result->fetch_first->[0], 'B');
1481

            
1482
$result = $dbi->select(
1483
    table => 'company', relation => {'company.location_id' => 'location.id'},
1484
    column => ['location.name as "location.name"']
1485
);
1486
is($result->fetch_first->[0], 'B');
1487

            
1488
test 'Model class';
1489
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
1490
$dbi = MyDBI1->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1491
eval { $dbi->execute('drop table book') };
cleanup test
Yuki Kimoto authored on 2011-08-06
1492
$dbi->execute("create table book (title, author)");
1493
$model = $dbi->model('book');
1494
$model->insert({title => 'a', author => 'b'});
1495
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
1496
$dbi->execute("create table company (name)");
1497
$model = $dbi->model('company');
1498
$model->insert({name => 'a'});
1499
is_deeply($model->list->all, [{name => 'a'}], 'basic');
1500
is($dbi->models->{'book'}, $dbi->model('book'));
1501
is($dbi->models->{'company'}, $dbi->model('company'));
1502

            
1503
{
1504
    package MyDBI4;
1505

            
1506
    use strict;
1507
    use warnings;
1508

            
1509
    use base 'DBIx::Custom';
1510

            
1511
    sub connect {
1512
        my $self = shift->SUPER::connect(@_);
1513
        
1514
        $self->include_model(
1515
            MyModel2 => [
1516
                'book',
1517
                {class => 'Company', name => 'company'}
1518
            ]
1519
        );
1520
    }
1521

            
1522
    package MyModel2::Base1;
1523

            
1524
    use strict;
1525
    use warnings;
1526

            
1527
    use base 'DBIx::Custom::Model';
1528

            
1529
    package MyModel2::book;
1530

            
1531
    use strict;
1532
    use warnings;
1533

            
1534
    use base 'MyModel2::Base1';
1535

            
1536
    sub insert {
1537
        my ($self, $param) = @_;
1538
        
1539
        return $self->SUPER::insert(param => $param);
1540
    }
1541

            
1542
    sub list { shift->select; }
1543

            
1544
    package MyModel2::Company;
1545

            
1546
    use strict;
1547
    use warnings;
1548

            
1549
    use base 'MyModel2::Base1';
1550

            
1551
    sub insert {
1552
        my ($self, $param) = @_;
1553
        
1554
        return $self->SUPER::insert(param => $param);
1555
    }
1556

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

            
1570
{
1571
     package MyDBI5;
1572

            
1573
    use strict;
1574
    use warnings;
1575

            
1576
    use base 'DBIx::Custom';
1577

            
1578
    sub connect {
1579
        my $self = shift->SUPER::connect(@_);
1580
        
1581
        $self->include_model('MyModel4');
1582
    }
1583
}
test cleanup
Yuki Kimoto authored on 2011-08-10
1584
$dbi = MyDBI5->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1585
eval { $dbi->execute('drop table company') };
1586
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
1587
$dbi->execute("create table company (name)");
1588
$dbi->execute("create table table1 (key1)");
1589
$model = $dbi->model('company');
1590
$model->insert({name => 'a'});
1591
is_deeply($model->list->all, [{name => 'a'}], 'include all model');
1592
$dbi->insert(table => 'table1', param => {key1 => 1});
1593
$model = $dbi->model('book');
1594
is_deeply($model->list->all, [{key1 => 1}], 'include all model');
1595

            
1596
test 'primary_key';
1597
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
1598
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1599
$model = $dbi->model('book');
1600
$model->primary_key(['id', 'number']);
1601
is_deeply($model->primary_key, ['id', 'number']);
1602

            
1603
test 'columns';
1604
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
1605
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1606
$model = $dbi->model('book');
1607
$model->columns(['id', 'number']);
1608
is_deeply($model->columns, ['id', 'number']);
1609

            
1610
test 'setup_model';
1611
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
1612
$dbi = MyDBI1->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1613
eval { $dbi->execute('drop table book') };
1614
eval { $dbi->execute('drop table company') };
1615
eval { $dbi->execute('drop table test') };
1616

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1617
$dbi->execute('create table book (id)');
1618
$dbi->execute('create table company (id, name);');
1619
$dbi->execute('create table test (id, name, primary key (id, name));');
1620
$dbi->setup_model;
1621
is_deeply($dbi->model('book')->columns, ['id']);
1622
is_deeply($dbi->model('company')->columns, ['id', 'name']);
1623

            
1624
test 'delete_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
1625
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1626
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1627
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1628
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1629
$dbi->delete_at(
1630
    table => 'table1',
1631
    primary_key => ['key1', 'key2'],
1632
    where => [1, 2],
1633
);
1634
is_deeply($dbi->select(table => 'table1')->all, []);
1635

            
1636
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1637
$dbi->delete_at(
1638
    table => 'table1',
1639
    primary_key => 'key1',
1640
    where => 1,
1641
);
1642
is_deeply($dbi->select(table => 'table1')->all, []);
1643

            
1644
test 'insert_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
1645
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1646
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1647
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1648
$dbi->insert_at(
1649
    primary_key => ['key1', 'key2'], 
1650
    table => 'table1',
1651
    where => [1, 2],
1652
    param => {key3 => 3}
1653
);
1654
is($dbi->select(table => 'table1')->one->{key1}, 1);
1655
is($dbi->select(table => 'table1')->one->{key2}, 2);
1656
is($dbi->select(table => 'table1')->one->{key3}, 3);
1657

            
1658
$dbi->delete_all(table => 'table1');
1659
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1660
$dbi->insert_at(
1661
    primary_key => 'key1', 
1662
    table => 'table1',
1663
    where => 1,
1664
    param => {key2 => 2, key3 => 3}
1665
);
1666

            
1667
is($dbi->select(table => 'table1')->one->{key1}, 1);
1668
is($dbi->select(table => 'table1')->one->{key2}, 2);
1669
is($dbi->select(table => 'table1')->one->{key3}, 3);
1670

            
1671
eval {
1672
    $dbi->insert_at(
1673
        table => 'table1',
1674
        primary_key => ['key1', 'key2'],
1675
        where => {},
1676
        param => {key1 => 1, key2 => 2, key3 => 3},
1677
    );
1678
};
1679
like($@, qr/must be/);
1680

            
test cleanup
Yuki Kimoto authored on 2011-08-10
1681
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
1682
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1683
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1684
$dbi->insert_at(
1685
    {key3 => 3},
1686
    primary_key => ['key1', 'key2'], 
1687
    table => 'table1',
1688
    where => [1, 2],
1689
);
1690
is($dbi->select(table => 'table1')->one->{key1}, 1);
1691
is($dbi->select(table => 'table1')->one->{key2}, 2);
1692
is($dbi->select(table => 'table1')->one->{key3}, 3);
1693

            
1694
test 'update_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
1695
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1696
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1697
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1698
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1699
$dbi->update_at(
1700
    table => 'table1',
1701
    primary_key => ['key1', 'key2'],
1702
    where => [1, 2],
1703
    param => {key3 => 4}
1704
);
1705
is($dbi->select(table => 'table1')->one->{key1}, 1);
1706
is($dbi->select(table => 'table1')->one->{key2}, 2);
1707
is($dbi->select(table => 'table1')->one->{key3}, 4);
1708

            
1709
$dbi->delete_all(table => 'table1');
1710
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1711
$dbi->update_at(
1712
    table => 'table1',
1713
    primary_key => 'key1',
1714
    where => 1,
1715
    param => {key3 => 4}
1716
);
1717
is($dbi->select(table => 'table1')->one->{key1}, 1);
1718
is($dbi->select(table => 'table1')->one->{key2}, 2);
1719
is($dbi->select(table => 'table1')->one->{key3}, 4);
1720

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

            
1735
test 'select_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
1736
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1737
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1738
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1739
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1740
$result = $dbi->select_at(
1741
    table => 'table1',
1742
    primary_key => ['key1', 'key2'],
1743
    where => [1, 2]
1744
);
1745
$row = $result->one;
1746
is($row->{key1}, 1);
1747
is($row->{key2}, 2);
1748
is($row->{key3}, 3);
1749

            
1750
$dbi->delete_all(table => 'table1');
1751
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1752
$result = $dbi->select_at(
1753
    table => 'table1',
1754
    primary_key => 'key1',
1755
    where => 1,
1756
);
1757
$row = $result->one;
1758
is($row->{key1}, 1);
1759
is($row->{key2}, 2);
1760
is($row->{key3}, 3);
1761

            
1762
$dbi->delete_all(table => 'table1');
1763
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1764
$result = $dbi->select_at(
1765
    table => 'table1',
1766
    primary_key => ['key1', 'key2'],
1767
    where => [1, 2]
1768
);
1769
$row = $result->one;
1770
is($row->{key1}, 1);
1771
is($row->{key2}, 2);
1772
is($row->{key3}, 3);
1773

            
1774
eval {
1775
    $result = $dbi->select_at(
1776
        table => 'table1',
1777
        primary_key => ['key1', 'key2'],
1778
        where => {},
1779
    );
1780
};
1781
like($@, qr/must be/);
1782

            
1783
eval {
1784
    $result = $dbi->select_at(
1785
        table => 'table1',
1786
        primary_key => ['key1', 'key2'],
1787
        where => [1],
1788
    );
1789
};
1790
like($@, qr/same/);
1791

            
1792
eval {
1793
    $result = $dbi->update_at(
1794
        table => 'table1',
1795
        primary_key => ['key1', 'key2'],
1796
        where => {},
1797
        param => {key1 => 1, key2 => 2},
1798
    );
1799
};
1800
like($@, qr/must be/);
1801

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

            
1811
test 'columns';
1812
use MyDBI1;
test cleanup
Yuki Kimoto authored on 2011-08-10
1813
$dbi = MyDBI1->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1814
$model = $dbi->model('book');
1815

            
1816

            
1817
test 'model delete_at';
1818
{
1819
    package MyDBI6;
1820
    
1821
    use base 'DBIx::Custom';
1822
    
1823
    sub connect {
1824
        my $self = shift->SUPER::connect(@_);
1825
        
1826
        $self->include_model('MyModel5');
1827
        
1828
        return $self;
1829
    }
1830
}
test cleanup
Yuki Kimoto authored on 2011-08-10
1831
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1832
eval { $dbi->execute('drop table table1') };
1833
eval { $dbi->execute('drop table table2') };
1834
eval { $dbi->execute('drop table table3') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1835
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1836
$dbi->execute("create table table2 (key1, key2, key3)");
1837
$dbi->execute("create table table3 (key1, key2, key3)");
1838
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1839
$dbi->model('table1')->delete_at(where => [1, 2]);
1840
is_deeply($dbi->select(table => 'table1')->all, []);
1841
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
1842
$dbi->model('table1_1')->delete_at(where => [1, 2]);
1843
is_deeply($dbi->select(table => 'table1')->all, []);
1844
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
1845
$dbi->model('table1_3')->delete_at(where => [1, 2]);
1846
is_deeply($dbi->select(table => 'table1')->all, []);
1847

            
1848
test 'model insert_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
1849
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1850
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1851
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1852
$dbi->model('table1')->insert_at(
1853
    where => [1, 2],
1854
    param => {key3 => 3}
1855
);
1856
$result = $dbi->model('table1')->select;
1857
$row = $result->one;
1858
is($row->{key1}, 1);
1859
is($row->{key2}, 2);
1860
is($row->{key3}, 3);
1861

            
1862
test 'model update_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
1863
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1864
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1865
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1866
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1867
$dbi->model('table1')->update_at(
1868
    where => [1, 2],
1869
    param => {key3 => 4}
1870
);
1871
$result = $dbi->model('table1')->select;
1872
$row = $result->one;
1873
is($row->{key1}, 1);
1874
is($row->{key2}, 2);
1875
is($row->{key3}, 4);
1876

            
1877
test 'model select_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
1878
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1879
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1880
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1881
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1882
$result = $dbi->model('table1')->select_at(where => [1, 2]);
1883
$row = $result->one;
1884
is($row->{key1}, 1);
1885
is($row->{key2}, 2);
1886
is($row->{key3}, 3);
1887

            
1888

            
1889
test 'mycolumn and column';
1890
{
1891
    package MyDBI7;
1892
    
1893
    use base 'DBIx::Custom';
1894
    
1895
    sub connect {
1896
        my $self = shift->SUPER::connect(@_);
1897
        
1898
        $self->include_model('MyModel6');
1899
        
1900
        
1901
        return $self;
1902
    }
1903
}
test cleanup
Yuki Kimoto authored on 2011-08-10
1904
$dbi = MyDBI7->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1905
eval { $dbi->execute('drop table table1') };
1906
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1907
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
1908
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1909
$dbi->separator('__');
1910
$dbi->setup_model;
1911
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1912
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1913
$model = $dbi->model('table1');
1914
$result = $model->select(
1915
    column => [$model->mycolumn, $model->column('table2')],
1916
    where => {'table1.key1' => 1}
1917
);
1918
is_deeply($result->one,
1919
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
1920

            
1921
test 'update_param';
test cleanup
Yuki Kimoto authored on 2011-08-10
1922
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1923
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1924
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1925
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1926
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1927

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

            
1941

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

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

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

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

            
1980
            
1981
eval { $dbi->update_param({";" => 1}) };
1982
like($@, qr/not safety/);
1983

            
1984

            
1985
test 'update_param';
test cleanup
Yuki Kimoto authored on 2011-08-10
1986
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1987
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1988
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1989
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1990
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1991

            
1992
$param = {key2 => 11};
1993
$update_param = $dbi->assign_param($param);
1994
$sql = <<"EOS";
1995
update table1 set $update_param
1996
where key1 = 1
1997
EOS
1998
$dbi->execute($sql, param => $param, table => 'table1');
test cleanup
Yuki Kimoto authored on 2011-08-06
1999
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
2000
$rows   = $result->all;
2001
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
2002
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
2003
                  "basic");
2004

            
2005

            
2006
test 'insert_param';
test cleanup
Yuki Kimoto authored on 2011-08-10
2007
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2008
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2009
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2010
$param = {key1 => 1, key2 => 2};
2011
$insert_param = $dbi->insert_param($param);
2012
$sql = <<"EOS";
2013
insert into table1 $insert_param
2014
EOS
2015
$dbi->execute($sql, param => $param, table => 'table1');
2016
is($dbi->select(table => 'table1')->one->{key1}, 1);
2017
is($dbi->select(table => 'table1')->one->{key2}, 2);
2018

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

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

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

            
2036
test 'join';
test cleanup
Yuki Kimoto authored on 2011-08-10
2037
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2038
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2039
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2040
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2041
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
cleanup test
Yuki Kimoto authored on 2011-08-06
2042
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2043
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
cleanup test
Yuki Kimoto authored on 2011-08-06
2044
$dbi->execute('create table table3 (key3 int, key4 int);');
cleanup test
Yuki Kimoto authored on 2011-08-06
2045
$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4});
2046
$rows = $dbi->select(
2047
    table => 'table1',
2048
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
2049
    where   => {'table1.key2' => 2},
2050
    join  => ['left outer join table2 on table1.key1 = table2.key1']
2051
)->all;
2052
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]);
2053

            
2054
$rows = $dbi->select(
2055
    table => 'table1',
2056
    where   => {'key1' => 1},
2057
    join  => ['left outer join table2 on table1.key1 = table2.key1']
2058
)->all;
2059
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2060

            
2061
eval {
2062
    $rows = $dbi->select(
2063
        table => 'table1',
2064
        column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
2065
        where   => {'table1.key2' => 2},
2066
        join  => {'table1.key1' => 'table2.key1'}
2067
    );
2068
};
2069
like ($@, qr/array/);
2070

            
2071
$rows = $dbi->select(
2072
    table => 'table1',
2073
    where   => {'key1' => 1},
2074
    join  => ['left outer join table2 on table1.key1 = table2.key1',
2075
              'left outer join table3 on table2.key3 = table3.key3']
2076
)->all;
2077
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2078

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

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2097
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
2098
$dbi->quote('"');
cleanup test
Yuki Kimoto authored on 2011-08-10
2099
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2100
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2101
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-06
2102
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2103
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
2104
$rows = $dbi->select(
2105
    table => 'table1',
2106
    column => '"table1"."key1" as "table1_key1", "table2"."key1" as "table2_key1", "key2", "key3"',
2107
    where   => {'table1.key2' => 2},
2108
    join  => ['left outer join "table2" on "table1"."key1" = "table2"."key1"'],
2109
)->all;
2110
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}],
2111
          'quote');
2112

            
2113
{
2114
    package MyDBI8;
2115
    
2116
    use base 'DBIx::Custom';
2117
    
2118
    sub connect {
2119
        my $self = shift->SUPER::connect(@_);
2120
        
2121
        $self->include_model('MyModel7');
2122
        
2123
        return $self;
2124
    }
2125
}
cleanup test
Yuki Kimoto authored on 2011-08-06
2126

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

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

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

            
2200
test 'mycolumn';
test cleanup
Yuki Kimoto authored on 2011-08-10
2201
$dbi = MyDBI8->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2202
eval { $dbi->execute('drop table table1') };
2203
eval { $dbi->execute('drop table table2') };
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
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2270
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2271
$model = $dbi->model('table1');
2272
eval{$model->execute('select * from table1')};
2273
ok(!$@);
2274

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

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

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

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

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

            
2338

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

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

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

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

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

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

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

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

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

            
2465

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

            
2479
eval {
2480
$dbi->select(
2481
    table => 'table1',
2482
    column => 'key1',
2483
    wrap => 'select * from ('
2484
)
2485
};
2486
like($@, qr/array/);
2487

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2501
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2502
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2503
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2504
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2505
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2506
$rows = $dbi->select(
2507
    table => 'table1',
2508
    where => [
2509
        'key1 = :key1 and key2 = :key2',
2510
        {key1 => 1, key2 => 2}
2511
    ]
2512
)->all;
2513
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2514

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

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

            
2544

            
2545
test 'update() string where';
test cleanup
Yuki Kimoto authored on 2011-08-10
2546
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2547
eval { $dbi->execute('drop table table1') };
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 => 'key1 = :key1 and key2 = :key2',
2554
    where_param => {key1 => 1, key2 => 2}
2555
);
2556
$rows = $dbi->select(table => 'table1')->all;
2557
is_deeply($rows, [{key1 => 5, key2 => 2}]);
2558

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

            
2574
test 'insert id and primary_key option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2575
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2576
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2577
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2578
$dbi->insert(
2579
    primary_key => ['key1', 'key2'], 
2580
    table => 'table1',
2581
    id => [1, 2],
2582
    param => {key3 => 3}
2583
);
2584
is($dbi->select(table => 'table1')->one->{key1}, 1);
2585
is($dbi->select(table => 'table1')->one->{key2}, 2);
2586
is($dbi->select(table => 'table1')->one->{key3}, 3);
2587

            
2588
$dbi->delete_all(table => 'table1');
2589
$dbi->insert(
2590
    primary_key => 'key1', 
2591
    table => 'table1',
2592
    id => 0,
2593
    param => {key2 => 2, key3 => 3}
2594
);
2595

            
2596
is($dbi->select(table => 'table1')->one->{key1}, 0);
2597
is($dbi->select(table => 'table1')->one->{key2}, 2);
2598
is($dbi->select(table => 'table1')->one->{key3}, 3);
2599

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

            
2613

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2628
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2629
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2630
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2631
$dbi->model('table1')->insert(
2632
    {key3 => 3},
2633
    id => [1, 2]
2634
);
2635
$result = $dbi->model('table1')->select;
2636
$row = $result->one;
2637
is($row->{key1}, 1);
2638
is($row->{key2}, 2);
2639
is($row->{key3}, 3);
2640

            
2641
test 'update and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2642
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2643
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2644
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2645
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2646
$dbi->update(
2647
    table => 'table1',
2648
    primary_key => ['key1', 'key2'],
2649
    id => [1, 2],
2650
    param => {key3 => 4}
2651
);
2652
is($dbi->select(table => 'table1')->one->{key1}, 1);
2653
is($dbi->select(table => 'table1')->one->{key2}, 2);
2654
is($dbi->select(table => 'table1')->one->{key3}, 4);
2655

            
2656
$dbi->delete_all(table => 'table1');
2657
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2658
$dbi->update(
2659
    table => 'table1',
2660
    primary_key => 'key1',
2661
    id => 0,
2662
    param => {key3 => 4}
2663
);
2664
is($dbi->select(table => 'table1')->one->{key1}, 0);
2665
is($dbi->select(table => 'table1')->one->{key2}, 2);
2666
is($dbi->select(table => 'table1')->one->{key3}, 4);
2667

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

            
2682

            
2683
test 'model update and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2684
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2685
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2686
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2687
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2688
$dbi->model('table1')->update(
2689
    id => [1, 2],
2690
    param => {key3 => 4}
2691
);
2692
$result = $dbi->model('table1')->select;
2693
$row = $result->one;
2694
is($row->{key1}, 1);
2695
is($row->{key2}, 2);
2696
is($row->{key3}, 4);
2697

            
2698

            
2699
test 'delete and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2700
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2701
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2702
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2703
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2704
$dbi->delete(
2705
    table => 'table1',
2706
    primary_key => ['key1', 'key2'],
2707
    id => [1, 2],
2708
);
2709
is_deeply($dbi->select(table => 'table1')->all, []);
2710

            
2711
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2712
$dbi->delete(
2713
    table => 'table1',
2714
    primary_key => 'key1',
2715
    id => 0,
2716
);
2717
is_deeply($dbi->select(table => 'table1')->all, []);
2718

            
2719

            
2720
test 'model delete and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2721
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2722
eval { $dbi->execute('drop table table1') };
2723
eval { $dbi->execute('drop table table2') };
2724
eval { $dbi->execute('drop table table3') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2725
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2726
$dbi->execute("create table table2 (key1, key2, key3)");
2727
$dbi->execute("create table table3 (key1, key2, key3)");
2728
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2729
$dbi->model('table1')->delete(id => [1, 2]);
2730
is_deeply($dbi->select(table => 'table1')->all, []);
2731
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
2732
$dbi->model('table1_1')->delete(id => [1, 2]);
2733
is_deeply($dbi->select(table => 'table1')->all, []);
2734
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
2735
$dbi->model('table1_3')->delete(id => [1, 2]);
2736
is_deeply($dbi->select(table => 'table1')->all, []);
2737

            
2738

            
2739
test 'select and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2740
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2741
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2742
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2743
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2744
$result = $dbi->select(
2745
    table => 'table1',
2746
    primary_key => ['key1', 'key2'],
2747
    id => [1, 2]
2748
);
2749
$row = $result->one;
2750
is($row->{key1}, 1);
2751
is($row->{key2}, 2);
2752
is($row->{key3}, 3);
2753

            
2754
$dbi->delete_all(table => 'table1');
2755
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2756
$result = $dbi->select(
2757
    table => 'table1',
2758
    primary_key => 'key1',
2759
    id => 0,
2760
);
2761
$row = $result->one;
2762
is($row->{key1}, 0);
2763
is($row->{key2}, 2);
2764
is($row->{key3}, 3);
2765

            
2766
$dbi->delete_all(table => 'table1');
2767
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2768
$result = $dbi->select(
2769
    table => 'table1',
2770
    primary_key => ['key1', 'key2'],
2771
    id => [1, 2]
2772
);
2773
$row = $result->one;
2774
is($row->{key1}, 1);
2775
is($row->{key2}, 2);
2776
is($row->{key3}, 3);
2777

            
2778

            
2779
test 'model select_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
2780
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2781
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2782
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2783
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2784
$result = $dbi->model('table1')->select(id => [1, 2]);
2785
$row = $result->one;
2786
is($row->{key1}, 1);
2787
is($row->{key2}, 2);
2788
is($row->{key3}, 3);
2789

            
2790
test 'column separator is default .';
test cleanup
Yuki Kimoto authored on 2011-08-10
2791
$dbi = MyDBI7->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2792
eval { $dbi->execute('drop table table1') };
2793
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2794
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2795
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2796
$dbi->setup_model;
2797
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2798
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2799
$model = $dbi->model('table1');
2800
$result = $model->select(
2801
    column => [$model->column('table2')],
2802
    where => {'table1.key1' => 1}
2803
);
2804
is_deeply($result->one,
2805
          {'table2.key1' => 1, 'table2.key3' => 3});
2806

            
2807
$result = $model->select(
2808
    column => [$model->column('table2' => [qw/key1 key3/])],
2809
    where => {'table1.key1' => 1}
2810
);
2811
is_deeply($result->one,
2812
          {'table2.key1' => 1, 'table2.key3' => 3});
2813

            
2814

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2815

            
2816
test 'separator';
test cleanup
Yuki Kimoto authored on 2011-08-10
2817
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2818
eval { $dbi->execute('drop table table1') };
2819
eval { $dbi->execute('drop table table2') };
2820
$dbi->execute($create_table1);
2821
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
2822

            
2823
$dbi->create_model(
2824
    table => 'table1',
2825
    join => [
2826
       'left outer join table2 on table1.key1 = table2.key1'
2827
    ],
2828
    primary_key => ['key1'],
cleanup test
Yuki Kimoto authored on 2011-08-06
2829
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2830
$model2 = $dbi->create_model(
2831
    table => 'table2',
2832
);
2833
$dbi->setup_model;
2834
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2835
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2836
$model = $dbi->model('table1');
2837
$result = $model->select(
2838
    column => [
2839
        $model->mycolumn,
2840
        {table2 => [qw/key1 key3/]}
2841
    ],
2842
    where => {'table1.key1' => 1}
2843
);
2844
is_deeply($result->one,
2845
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
2846
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
2847

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2848
$dbi->separator('__');
2849
$model = $dbi->model('table1');
2850
$result = $model->select(
2851
    column => [
2852
        $model->mycolumn,
2853
        {table2 => [qw/key1 key3/]}
2854
    ],
2855
    where => {'table1.key1' => 1}
2856
);
2857
is_deeply($result->one,
2858
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
2859
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
2860

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2861
$dbi->separator('-');
2862
$model = $dbi->model('table1');
2863
$result = $model->select(
2864
    column => [
2865
        $model->mycolumn,
2866
        {table2 => [qw/key1 key3/]}
2867
    ],
2868
    where => {'table1.key1' => 1}
2869
);
2870
is_deeply($result->one,
2871
          {key1 => 1, key2 => 2, 'table2-key1' => 1, 'table2-key3' => 3});
2872
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
2873

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2874

            
2875
test 'filter_off';
test cleanup
Yuki Kimoto authored on 2011-08-10
2876
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2877
eval { $dbi->execute('drop table table1') };
2878
eval { $dbi->execute('drop table table2') };
2879
$dbi->execute($create_table1);
2880
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
2881

            
2882
$dbi->create_model(
2883
    table => 'table1',
2884
    join => [
2885
       'left outer join table2 on table1.key1 = table2.key1'
2886
    ],
2887
    primary_key => ['key1'],
cleanup test
Yuki Kimoto authored on 2011-08-06
2888
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2889
$dbi->setup_model;
2890
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2891
$model = $dbi->model('table1');
2892
$result = $model->select(column => 'key1');
2893
$result->filter(key1 => sub { $_[0] * 2 });
2894
is_deeply($result->one, {key1 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-06
2895

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2896
test 'available_datetype';
test cleanup
Yuki Kimoto authored on 2011-08-10
2897
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2898
ok($dbi->can('available_datatype'));
2899

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2901
test 'select prefix option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2902
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2903
eval { $dbi->execute('drop table table1') };
2904
$dbi->execute($create_table1);
2905
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2906
$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all;
2907
is_deeply($rows, [{key1 => 1, key2 => 2}], "table");
2908

            
2909

            
2910
test 'separator';
2911
$dbi = DBIx::Custom->connect;
2912
is($dbi->separator, '.');
2913
$dbi->separator('-');
2914
is($dbi->separator, '-');
2915
$dbi->separator('__');
2916
is($dbi->separator, '__');
2917
eval { $dbi->separator('?') };
2918
like($@, qr/Separator/);
2919

            
2920

            
2921
test 'map_param';
2922
$dbi = DBIx::Custom->connect;
2923
$param = $dbi->map_param(
2924
    {id => 1, author => 'Ken', price => 1900},
2925
    id => 'book.id',
2926
    author => ['book.author', sub { '%' . $_[0] . '%' }],
2927
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
cleanup test
Yuki Kimoto authored on 2011-08-06
2928
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2929
is_deeply($param, {'book.id' => 1, 'book.author' => '%Ken%',
2930
  'book.price' => 1900});
2931

            
2932
$param = $dbi->map_param(
2933
    {id => 0, author => 0, price => 0},
2934
    id => 'book.id',
2935
    author => ['book.author', sub { '%' . $_[0] . '%' }],
2936
    price => ['book.price', sub { '%' . $_[0] . '%' },
2937
      {if => sub { $_[0] eq 0 }}]
cleanup test
Yuki Kimoto authored on 2011-08-06
2938
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2939
is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
cleanup test
Yuki Kimoto authored on 2011-08-06
2940

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2941
$param = $dbi->map_param(
2942
    {id => '', author => '', price => ''},
2943
    id => 'book.id',
2944
    author => ['book.author', sub { '%' . $_[0] . '%' }],
2945
    price => ['book.price', sub { '%' . $_[0] . '%' },
2946
      {if => sub { $_[0] eq 1 }}]
cleanup test
Yuki Kimoto authored on 2011-08-06
2947
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2948
is_deeply($param, {});
2949

            
2950
$param = $dbi->map_param(
2951
    {id => undef, author => undef, price => undef},
2952
    id => 'book.id',
2953
    price => ['book.price', {if => 'exists'}]
cleanup test
Yuki Kimoto authored on 2011-08-06
2954
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2955
is_deeply($param, {'book.price' => undef});
2956

            
2957
$param = $dbi->map_param(
2958
    {price => 'a'},
2959
    id => ['book.id', {if => 'exists'}],
2960
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
2961
);
2962
is_deeply($param, {'book.price' => '%a'});
cleanup test
Yuki Kimoto authored on 2011-08-06
2963

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2964

            
2965
test 'table_alias';
test cleanup
Yuki Kimoto authored on 2011-08-10
2966
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2967
eval { $dbi->execute('drop table table1') };
2968
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
cleanup test
Yuki Kimoto authored on 2011-08-06
2969
$dbi->type_rule(
2970
    into1 => {
cleanup test
Yuki Kimoto authored on 2011-08-10
2971
        date => sub { uc $_[0] }
cleanup test
Yuki Kimoto authored on 2011-08-06
2972
    }
2973
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2974
$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'},
2975
  table_alias => {table2 => 'table1'});
cleanup test
Yuki Kimoto authored on 2011-08-06
2976
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
2977
is($result->one->{key1}, 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
2978

            
2979

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2980
test 'order';
test cleanup
Yuki Kimoto authored on 2011-08-10
2981
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2982
eval { $dbi->execute('drop table table1') };
2983
$dbi->execute("create table table1 (key1, key2)");
2984
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
2985
$dbi->insert({key1 => 1, key2 => 3}, table => 'table1');
2986
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
2987
$dbi->insert({key1 => 2, key2 => 4}, table => 'table1');
2988
my $order = $dbi->order;
2989
$order->prepend('key1', 'key2 desc');
2990
$result = $dbi->select(table => 'table1', append => "$order");
2991
is_deeply($result->all, [{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1},
2992
  {key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}]);
2993
$order->prepend('key1 desc');
2994
$result = $dbi->select(table => 'table1', append => "$order");
2995
is_deeply($result->all, [{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2},
2996
  {key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
2997

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2998
$order = $dbi->order;
2999
$order->prepend(['table1-key1'], [qw/table1-key2 desc/]);
3000
$result = $dbi->select(table => 'table1',
3001
  column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']],
3002
  append => "$order");
3003
is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3},
3004
  {'table1-key1' => 1, 'table1-key2' => 1},
3005
  {'table1-key1' => 2, 'table1-key2' => 4},
3006
  {'table1-key1' => 2, 'table1-key2' => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
3007

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3008
test 'tag_parse';
test cleanup
Yuki Kimoto authored on 2011-08-10
3009
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3010
$dbi->tag_parse(0);
3011
eval { $dbi->execute('drop table table1') };
3012
$dbi->execute("create table table1 (key1, key2)");
3013
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
3014
eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})};
3015
ok($@);
cleanup test
Yuki Kimoto authored on 2011-08-06
3016

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3017
test 'last_sql';
test cleanup
Yuki Kimoto authored on 2011-08-10
3018
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3019
eval { $dbi->execute('drop table table1') };
3020
$dbi->execute("create table table1 (key1, key2)");
3021
$dbi->execute('select * from table1');
3022
is($dbi->last_sql, 'select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
3023

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3024
eval{$dbi->execute("aaa")};
3025
is($dbi->last_sql, 'aaa;');
cleanup test
Yuki Kimoto authored on 2011-08-06
3026

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3027
test 'DBIx::Custom header';
test cleanup
Yuki Kimoto authored on 2011-08-10
3028
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3029
eval { $dbi->execute('drop table table1') };
3030
$dbi->execute("create table table1 (key1, key2)");
3031
$result = $dbi->execute('select key1 as h1, key2 as h2 from table1');
3032
is_deeply($result->header, [qw/h1 h2/]);
cleanup test
Yuki Kimoto authored on 2011-08-06
3033

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3040
$source = "select * from table1 where :key1{=} and :key2{=}";
3041
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
3042
$rows = $result->all;
3043
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
3044

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3045
$source = "select * from table1 where :key1{ = } and :key2{=}";
3046
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
3047
$rows = $result->all;
3048
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
3049

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3050
$source = "select * from table1 where :key1{<} and :key2{=}";
3051
$result = $dbi->execute($source, param => {key1 => 5, key2 => 2});
3052
$rows = $result->all;
3053
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
3054

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3055
$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}";
3056
$result = $dbi->execute(
3057
    $source,
3058
    param => {'table1.key1' => 1, 'table1.key2' => 1},
3059
    filter => {'table1.key2' => sub { $_[0] * 2 }}
cleanup test
Yuki Kimoto authored on 2011-08-06
3060
);
cleanup test
Yuki Kimoto authored on 2011-08-10
3061
$rows = $result->all;
3062
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
3063

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3064
test 'high perfomance way';
3065
$dbi->execute('drop table table1');
3066
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
3067
$rows = [
3068
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3069
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3070
];
3071
{
3072
    my $query;
3073
    foreach my $row (@$rows) {
3074
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
3075
      $dbi->execute($query, $row, filter => {ab => sub { $_[0] * 2 }});
cleanup test
Yuki Kimoto authored on 2011-08-06
3076
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3077
    is_deeply($dbi->select(table => 'table1')->all,
3078
      [
3079
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3080
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3081
      ]
3082
    );
3083
}
3084

            
3085
$dbi->execute('drop table table1');
3086
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
3087
$rows = [
3088
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3089
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3090
];
3091
{
3092
    my $query;
3093
    my $sth;
3094
    foreach my $row (@$rows) {
3095
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
3096
      $sth ||= $query->sth;
3097
      $sth->execute(map { $row->{$_} } sort keys %$row);
cleanup test
Yuki Kimoto authored on 2011-08-06
3098
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3099
    is_deeply($dbi->select(table => 'table1')->all,
3100
      [
3101
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3102
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3103
      ]
3104
    );
3105
}
cleanup test
Yuki Kimoto authored on 2011-08-06
3106

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3107
test 'result';
test cleanup
Yuki Kimoto authored on 2011-08-10
3108
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3109
eval { $dbi->execute('drop table table1') };
3110
$dbi->execute($create_table1);
3111
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3112
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3113

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3114
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3115
@rows = ();
3116
while (my $row = $result->fetch) {
3117
    push @rows, [@$row];
3118
}
3119
is_deeply(\@rows, [[1, 2], [3, 4]]);
cleanup test
Yuki Kimoto authored on 2011-08-06
3120

            
3121
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3122
@rows = ();
3123
while (my $row = $result->fetch_hash) {
3124
    push @rows, {%$row};
3125
}
3126
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
3127

            
3128
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3129
$row = $result->fetch_first;
3130
is_deeply($row, [1, 2], "row");
3131
$row = $result->fetch;
3132
ok(!$row, "finished");
3133

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3134
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3135
$row = $result->fetch_hash_first;
3136
is_deeply($row, {key1 => 1, key2 => 2}, "row");
3137
$row = $result->fetch_hash;
3138
ok(!$row, "finished");
3139

            
3140
$dbi->execute('create table table2 (key1, key2);');
3141
$result = $dbi->select(table => 'table2');
3142
$row = $result->fetch_hash_first;
3143
ok(!$row, "no row fetch");
cleanup test
Yuki Kimoto authored on 2011-08-06
3144

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3145
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3146
eval { $dbi->execute('drop table table1') };
3147
$dbi->execute($create_table1);
3148
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3149
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3150
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1');
3151
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1');
3152
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
3153
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3154
$rows = $result->fetch_multi(2);
3155
is_deeply($rows, [[1, 2],
3156
                  [3, 4]], "fetch_multi first");
3157
$rows = $result->fetch_multi(2);
3158
is_deeply($rows, [[5, 6],
3159
                  [7, 8]], "fetch_multi secound");
3160
$rows = $result->fetch_multi(2);
3161
is_deeply($rows, [[9, 10]], "fetch_multi third");
3162
$rows = $result->fetch_multi(2);
3163
ok(!$rows);
3164

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3165
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3166
eval {$result->fetch_multi};
3167
like($@, qr/Row count must be specified/, "Not specified row count");
cleanup test
Yuki Kimoto authored on 2011-08-06
3168

            
3169
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3170
$rows = $result->fetch_hash_multi(2);
3171
is_deeply($rows, [{key1 => 1, key2 => 2},
3172
                  {key1 => 3, key2 => 4}], "fetch_multi first");
3173
$rows = $result->fetch_hash_multi(2);
3174
is_deeply($rows, [{key1 => 5, key2 => 6},
3175
                  {key1 => 7, key2 => 8}], "fetch_multi secound");
3176
$rows = $result->fetch_hash_multi(2);
3177
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third");
3178
$rows = $result->fetch_hash_multi(2);
3179
ok(!$rows);
3180

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3181
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3182
eval {$result->fetch_hash_multi};
3183
like($@, qr/Row count must be specified/, "Not specified row count");
cleanup test
Yuki Kimoto authored on 2011-08-06
3184

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3185
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3186
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
3187
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
3188
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3189
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
3190

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3191
test 'fetch_all';
3192
$result = $dbi->select(table => 'table1');
3193
$rows = $result->fetch_all;
3194
is_deeply($rows, [[1, 2], [3, 4]]);
cleanup test
Yuki Kimoto authored on 2011-08-06
3195

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3196
$result = $dbi->select(table => 'table1');
3197
$rows = $result->fetch_hash_all;
3198
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
3199

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3200
$result = $dbi->select(table => 'table1');
3201
$result->dbi->filters({three_times => sub { $_[0] * 3}});
3202
$result->filter({key1 => 'three_times'});
cleanup test
Yuki Kimoto authored on 2011-08-06
3203

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3204
$rows = $result->fetch_all;
3205
is_deeply($rows, [[3, 2], [9, 4]], "array");
cleanup test
Yuki Kimoto authored on 2011-08-06
3206

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3207
$result = $dbi->select(table => 'table1');
3208
$result->dbi->filters({three_times => sub { $_[0] * 3}});
3209
$result->filter({key1 => 'three_times'});
3210
$rows = $result->fetch_hash_all;
3211
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash");
3212

            
3213
test "query_builder";
3214
$datas = [
3215
    # Basic tests
3216
    {   name            => 'placeholder basic',
3217
        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
3218
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;",
3219
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
3220
    },
3221
    {
3222
        name            => 'placeholder in',
3223
        source            => "{in k1 3};",
3224
        sql_expected    => "k1 in (?, ?, ?);",
3225
        columns_expected   => [qw/k1 k1 k1/]
3226
    },
3227
    
3228
    # Table name
3229
    {
3230
        name            => 'placeholder with table name',
3231
        source            => "{= a.k1} {= a.k2}",
3232
        sql_expected    => "a.k1 = ? a.k2 = ?;",
3233
        columns_expected  => [qw/a.k1 a.k2/]
3234
    },
3235
    {   
3236
        name            => 'placeholder in with table name',
3237
        source            => "{in a.k1 2} {in b.k2 2}",
3238
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?);",
3239
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
3240
    },
3241
    {
3242
        name            => 'not contain tag',
3243
        source            => "aaa",
3244
        sql_expected    => "aaa;",
3245
        columns_expected  => [],
3246
    }
3247
];
3248

            
3249
for (my $i = 0; $i < @$datas; $i++) {
3250
    my $data = $datas->[$i];
3251
    my $builder = DBIx::Custom->new->query_builder;
3252
    my $query = $builder->build_query($data->{source});
3253
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
3254
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
3255
}
3256

            
3257
$builder = DBIx::Custom->new->query_builder;
3258
$ret_val = $builder->register_tag(
3259
    p => sub {
3260
        my @args = @_;
3261
        
3262
        my $expand    = "? $args[0] $args[1]";
3263
        my $columns = [2];
3264
        return [$expand, $columns];
3265
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
3266
);
3267

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3268
$query = $builder->build_query("{p a b}");
3269
is($query->{sql}, "? a b;", "register_tag sql");
3270
is_deeply($query->{columns}, [2], "register_tag columns");
3271
isa_ok($ret_val, 'DBIx::Custom::QueryBuilder');
cleanup test
Yuki Kimoto authored on 2011-08-06
3272

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3273
$builder = DBIx::Custom->new->query_builder;
cleanup test
Yuki Kimoto authored on 2011-08-06
3274

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3275
eval{$builder->build_query('{? }')};
3276
like($@, qr/\QColumn name must be specified in tag "{? }"/, "? not arguments");
cleanup test
Yuki Kimoto authored on 2011-08-06
3277

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3278
eval{$builder->build_query("{a }")};
3279
like($@, qr/\QTag "a" is not registered/, "tag not exist");
cleanup test
Yuki Kimoto authored on 2011-08-06
3280

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3281
$builder->register_tag({
3282
    q => 'string'
3283
});
cleanup test
Yuki Kimoto authored on 2011-08-06
3284

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3285
eval{$builder->build_query("{q}", {})};
3286
like($@, qr/Tag "q" must be sub reference/, "tag not code ref");
cleanup test
Yuki Kimoto authored on 2011-08-06
3287

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3288
$builder->register_tag({
3289
   r => sub {} 
3290
});
cleanup test
Yuki Kimoto authored on 2011-08-06
3291

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3292
eval{$builder->build_query("{r}")};
3293
like($@, qr/\QTag "r" must return [STRING, ARRAY_REFERENCE]/, "tag return noting");
3294

            
3295
$builder->register_tag({
3296
   s => sub { return ["a", ""]} 
3297
});
3298

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

            
3302
$builder->register_tag(
3303
    t => sub {return ["a", []]}
cleanup test
Yuki Kimoto authored on 2011-08-06
3304
);
3305

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3306

            
3307
test 'General error case';
3308
$builder = DBIx::Custom->new->query_builder;
3309
$builder->register_tag(
3310
    a => sub {
3311
        return ["? ? ?", ['']];
3312
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
3313
);
cleanup test
Yuki Kimoto authored on 2011-08-10
3314
eval{$builder->build_query("{a}")};
3315
like($@, qr/\QPlaceholder count/, "placeholder count is invalid");
cleanup test
Yuki Kimoto authored on 2011-08-06
3316

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3317

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

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

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

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

            
3333
test 'variouse source';
3334
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;";
3335
$query = $builder->build_query($source);
3336
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1");
3337

            
3338
$source = "abc;";
3339
$query = $builder->build_query($source);
3340
is($query->sql, 'abc;', "basic : 2");
3341

            
3342
$source = "{= a}";
3343
$query = $builder->build_query($source);
3344
is($query->sql, 'a = ?;', "only tag");
3345

            
3346
$source = "000;";
3347
$query = $builder->build_query($source);
3348
is($query->sql, '000;', "contain 0 value");
3349

            
3350
$source = "a {= b} }";
3351
eval{$builder->build_query($source)};
3352
like($@, qr/unexpected "}"/, "error : 1");
3353

            
3354
$source = "a {= {}";
3355
eval{$builder->build_query($source)};
3356
like($@, qr/unexpected "{"/, "error : 2");
3357

            
3358
### SQLite test
3359
test 'type option'; # DEPRECATED!
3360
$dbi = DBIx::Custom->connect(
3361
    data_source => 'dbi:SQLite:dbname=:memory:',
3362
    dbi_option => {
3363
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
3364
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
3365
);
cleanup test
Yuki Kimoto authored on 2011-08-10
3366
$binary = pack("I3", 1, 2, 3);
3367
eval { $dbi->execute('drop table table1') };
3368
$dbi->execute('create table table1(key1, key2)');
3369
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
3370
$result = $dbi->select(table => 'table1');
3371
$row   = $result->one;
3372
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
3373
$result = $dbi->execute('select length(key1) as key1_length from table1');
3374
$row = $result->one;
3375
is($row->{key1_length}, length $binary);
cleanup test
Yuki Kimoto authored on 2011-08-06
3376

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3377
test 'type_rule from';
3378
$dbi = DBIx::Custom->connect;
3379
$dbi->type_rule(
3380
    from1 => {
3381
        date => sub { uc $_[0] }
3382
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
3383
);
cleanup test
Yuki Kimoto authored on 2011-08-10
3384
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3385
$dbi->insert({key1 => 'a'}, table => 'table1');
3386
$result = $dbi->select(table => 'table1');
3387
is($result->fetch_first->[0], 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
3388

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3389
$result = $dbi->select(table => 'table1');
3390
is($result->one->{key1}, 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
3391

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3392

            
3393
test 'type_rule into';
test cleanup
Yuki Kimoto authored on 2011-08-10
3394
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
3395
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3396
$dbi->type_rule(
3397
    into1 => {
3398
        date => sub { uc $_[0] }
3399
    }
3400
);
cleanup test
Yuki Kimoto authored on 2011-08-10
3401
$dbi->insert({key1 => 'a'}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
3402
$result = $dbi->select(table => 'table1');
3403
is($result->one->{key1}, 'A');
3404

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3405
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3406
$dbi->execute("create table table1 (key1 date, key2 datetime)");
3407
$dbi->type_rule(
3408
    into1 => [
3409
         [qw/date datetime/] => sub { uc $_[0] }
3410
    ]
3411
);
3412
$dbi->insert({key1 => 'a', key2 => 'b'}, table => 'table1');
3413
$result = $dbi->select(table => 'table1');
3414
$row = $result->one;
3415
is($row->{key1}, 'A');
3416
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
3417

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3418
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3419
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3420
$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1');
3421
$dbi->type_rule(
3422
    into1 => [
3423
        [qw/date datetime/] => sub { uc $_[0] }
3424
    ]
3425
);
3426
$result = $dbi->execute(
3427
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
3428
    param => {key1 => 'a', 'table1.key2' => 'b'}
3429
);
3430
$row = $result->one;
3431
is($row->{key1}, 'a');
3432
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
3433

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3434
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3435
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3436
$dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1');
3437
$dbi->type_rule(
3438
    into1 => [
3439
        [qw/date datetime/] => sub { uc $_[0] }
3440
    ]
3441
);
3442
$result = $dbi->execute(
3443
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
3444
    param => {key1 => 'a', 'table1.key2' => 'b'},
3445
    table => 'table1'
3446
);
3447
$row = $result->one;
3448
is($row->{key1}, 'A');
3449
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
3450

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3451
$dbi = DBIx::Custom->connect;
3452
$dbi->execute("create table table1 (key1 date, key2 datetime)");
3453
$dbi->register_filter(twice => sub { $_[0] * 2 });
3454
$dbi->type_rule(
3455
    from1 => {
3456
        date => 'twice',
3457
    },
3458
    into1 => {
3459
        date => 'twice',
3460
    }
3461
);
3462
$dbi->insert({key1 => 2}, table => 'table1');
3463
$result = $dbi->select(table => 'table1');
3464
is($result->fetch->[0], 8);
cleanup test
Yuki Kimoto authored on 2011-08-06
3465

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3466
test 'type_rule and filter order';
test cleanup
Yuki Kimoto authored on 2011-08-10
3467
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3468
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3469
$dbi->type_rule(
3470
    into1 => {
3471
        date => sub { $_[0] . 'b' }
3472
    },
3473
    into2 => {
3474
        date => sub { $_[0] . 'c' }
3475
    },
3476
    from1 => {
3477
        date => sub { $_[0] . 'd' }
3478
    },
3479
    from2 => {
3480
        date => sub { $_[0] . 'e' }
3481
    }
3482
);
3483
$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }});
3484
$result = $dbi->select(table => 'table1');
3485
$result->filter(key1 => sub { $_[0] . 'f' });
3486
is($result->fetch_first->[0], '1abcdef');
cleanup test
Yuki Kimoto authored on 2011-08-06
3487

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3488
$dbi = DBIx::Custom->connect;
3489
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3490
$dbi->type_rule(
3491
    from1 => {
3492
        date => sub { $_[0] . 'p' }
3493
    },
3494
    from2 => {
3495
        date => sub { $_[0] . 'q' }
3496
    },
3497
);
3498
$dbi->insert({key1 => '1'}, table => 'table1');
3499
$result = $dbi->select(table => 'table1');
3500
$result->type_rule(
3501
    from1 => {
3502
        date => sub { $_[0] . 'd' }
3503
    },
3504
    from2 => {
3505
        date => sub { $_[0] . 'e' }
3506
    }
3507
);
3508
$result->filter(key1 => sub { $_[0] . 'f' });
3509
is($result->fetch_first->[0], '1def');
cleanup test
Yuki Kimoto authored on 2011-08-06
3510

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3511
test 'type_rule_off';
3512
$dbi = DBIx::Custom->connect;
3513
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3514
$dbi->type_rule(
3515
    from1 => {
3516
        date => sub { $_[0] * 2 },
3517
    },
3518
    into1 => {
3519
        date => sub { $_[0] * 2 },
3520
    }
3521
);
3522
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
3523
$result = $dbi->select(table => 'table1', type_rule_off => 1);
3524
is($result->type_rule_off->fetch->[0], 2);
cleanup test
Yuki Kimoto authored on 2011-08-06
3525

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3526
$dbi = DBIx::Custom->connect;
3527
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3528
$dbi->type_rule(
3529
    from1 => {
3530
        date => sub { $_[0] * 2 },
3531
    },
3532
    into1 => {
3533
        date => sub { $_[0] * 3 },
3534
    }
3535
);
3536
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
3537
$result = $dbi->select(table => 'table1', type_rule_off => 1);
3538
is($result->one->{key1}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-06
3539

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3540
$dbi = DBIx::Custom->connect;
3541
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3542
$dbi->type_rule(
3543
    from1 => {
3544
        date => sub { $_[0] * 2 },
3545
    },
3546
    into1 => {
3547
        date => sub { $_[0] * 3 },
3548
    }
3549
);
3550
$dbi->insert({key1 => 2}, table => 'table1');
3551
$result = $dbi->select(table => 'table1');
3552
is($result->one->{key1}, 12);
cleanup test
Yuki Kimoto authored on 2011-08-06
3553

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3554
$dbi = DBIx::Custom->connect;
3555
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3556
$dbi->type_rule(
3557
    from1 => {
3558
        date => sub { $_[0] * 2 },
3559
    },
3560
    into1 => {
3561
        date => sub { $_[0] * 3 },
3562
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
3563
);
cleanup test
Yuki Kimoto authored on 2011-08-10
3564
$dbi->insert({key1 => 2}, table => 'table1');
3565
$result = $dbi->select(table => 'table1');
3566
is($result->fetch->[0], 12);
cleanup test
Yuki Kimoto authored on 2011-08-06
3567

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3568
$dbi = DBIx::Custom->connect;
3569
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3570
$dbi->register_filter(ppp => sub { uc $_[0] });
3571
$dbi->type_rule(
3572
    into1 => {
3573
        date => 'ppp'
cleanup test
Yuki Kimoto authored on 2011-08-06
3574
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3575
);
3576
$dbi->insert({key1 => 'a'}, table => 'table1');
3577
$result = $dbi->select(table => 'table1');
3578
is($result->one->{key1}, 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
3579

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3580
eval{$dbi->type_rule(
3581
    into1 => {
3582
        date => 'pp'
cleanup test
Yuki Kimoto authored on 2011-08-06
3583
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3584
)};
3585
like($@, qr/not registered/);
cleanup test
Yuki Kimoto authored on 2011-08-06
3586

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3587
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3588
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3589
eval {
3590
    $dbi->type_rule(
3591
        from1 => {
3592
            Date => sub { $_[0] * 2 },
3593
        }
3594
    );
3595
};
3596
like($@, qr/lower/);
clenup test
Yuki Kimoto authored on 2011-08-06
3597

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3598
eval {
3599
    $dbi->type_rule(
3600
        into1 => {
3601
            Date => sub { $_[0] * 2 },
3602
        }
3603
    );
3604
};
3605
like($@, qr/lower/);
clenup test
Yuki Kimoto authored on 2011-08-06
3606

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3607
$dbi = DBIx::Custom->connect;
3608
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3609
$dbi->type_rule(
3610
    from1 => {
3611
        date => sub { $_[0] * 2 },
3612
    },
3613
    into1 => {
3614
        date => sub { $_[0] * 3 },
3615
    }
3616
);
3617
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
3618
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3619
$result->type_rule_off;
3620
is($result->one->{key1}, 6);
clenup test
Yuki Kimoto authored on 2011-08-06
3621

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3622
$dbi = DBIx::Custom->connect;
3623
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3624
$dbi->type_rule(
3625
    from1 => {
3626
        date => sub { $_[0] * 2 },
3627
        datetime => sub { $_[0] * 4 },
3628
    },
3629
);
3630
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
3631
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3632
$result->type_rule(
3633
    from1 => {
3634
        date => sub { $_[0] * 3 }
3635
    }
3636
);
3637
$row = $result->one;
3638
is($row->{key1}, 6);
3639
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
3640

            
3641
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3642
$result->type_rule(
3643
    from1 => {
3644
        date => sub { $_[0] * 3 }
3645
    }
3646
);
3647
$row = $result->one;
3648
is($row->{key1}, 6);
3649
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
3650

            
3651
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3652
$result->type_rule(
3653
    from1 => {
3654
        date => sub { $_[0] * 3 }
3655
    }
3656
);
3657
$row = $result->one;
3658
is($row->{key1}, 6);
3659
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
3660
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3661
$result->type_rule(
3662
    from1 => [date => sub { $_[0] * 3 }]
3663
);
3664
$row = $result->one;
3665
is($row->{key1}, 6);
3666
is($row->{key2}, 2);
3667
$dbi->register_filter(fivetimes => sub { $_[0] * 5});
clenup test
Yuki Kimoto authored on 2011-08-06
3668
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3669
$result->type_rule(
3670
    from1 => [date => 'fivetimes']
3671
);
3672
$row = $result->one;
3673
is($row->{key1}, 10);
3674
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
3675
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3676
$result->type_rule(
3677
    from1 => [date => undef]
3678
);
3679
$row = $result->one;
3680
is($row->{key1}, 2);
3681
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
3682

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3683
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3684
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3685
$dbi->type_rule(
3686
    from1 => {
3687
        date => sub { $_[0] * 2 },
3688
    },
3689
);
3690
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
3691
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3692
$result->filter(key1 => sub { $_[0] * 3 });
3693
is($result->one->{key1}, 12);
clenup test
Yuki Kimoto authored on 2011-08-06
3694

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3695
$dbi = DBIx::Custom->connect;
3696
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3697
$dbi->type_rule(
3698
    from1 => {
3699
        date => sub { $_[0] * 2 },
3700
    },
3701
);
3702
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
3703
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3704
$result->filter(key1 => sub { $_[0] * 3 });
3705
is($result->fetch->[0], 12);
clenup test
Yuki Kimoto authored on 2011-08-06
3706

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3707
$dbi = DBIx::Custom->connect;
3708
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3709
$dbi->type_rule(
3710
    into1 => {
3711
        date => sub { $_[0] . 'b' }
3712
    },
3713
    into2 => {
3714
        date => sub { $_[0] . 'c' }
3715
    },
3716
    from1 => {
3717
        date => sub { $_[0] . 'd' }
3718
    },
3719
    from2 => {
3720
        date => sub { $_[0] . 'e' }
3721
    }
3722
);
3723
$dbi->insert({key1 => '1'}, table => 'table1', type_rule_off => 1);
clenup test
Yuki Kimoto authored on 2011-08-06
3724
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3725
is($result->type_rule_off->fetch_first->[0], '1');
clenup test
Yuki Kimoto authored on 2011-08-06
3726
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3727
is($result->type_rule_on->fetch_first->[0], '1de');
clenup test
Yuki Kimoto authored on 2011-08-06
3728

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3729
$dbi = DBIx::Custom->connect;
3730
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3731
$dbi->type_rule(
3732
    into1 => {
3733
        date => sub { $_[0] . 'b' }
cleanup test
Yuki Kimoto authored on 2011-08-06
3734
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
3735
    into2 => {
3736
        date => sub { $_[0] . 'c' }
cleanup test
Yuki Kimoto authored on 2011-08-06
3737
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
3738
    from1 => {
3739
        date => sub { $_[0] . 'd' }
cleanup test
Yuki Kimoto authored on 2011-08-06
3740
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
3741
    from2 => {
3742
        date => sub { $_[0] . 'e' }
cleanup test
Yuki Kimoto authored on 2011-08-06
3743
    }
3744
);
cleanup test
Yuki Kimoto authored on 2011-08-10
3745
$dbi->insert({key1 => '1'}, table => 'table1', type_rule1_off => 1);
3746
$result = $dbi->select(table => 'table1');
3747
is($result->type_rule1_off->fetch_first->[0], '1ce');
3748
$result = $dbi->select(table => 'table1');
3749
is($result->type_rule1_on->fetch_first->[0], '1cde');
cleanup test
Yuki Kimoto authored on 2011-08-06
3750

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3751
$dbi = DBIx::Custom->connect;
3752
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3753
$dbi->type_rule(
3754
    into1 => {
3755
        date => sub { $_[0] . 'b' }
3756
    },
3757
    into2 => {
3758
        date => sub { $_[0] . 'c' }
3759
    },
3760
    from1 => {
3761
        date => sub { $_[0] . 'd' }
3762
    },
3763
    from2 => {
3764
        date => sub { $_[0] . 'e' }
cleanup test
Yuki Kimoto authored on 2011-08-06
3765
    }
3766
);
cleanup test
Yuki Kimoto authored on 2011-08-10
3767
$dbi->insert({key1 => '1'}, table => 'table1', type_rule2_off => 1);
3768
$result = $dbi->select(table => 'table1');
3769
is($result->type_rule2_off->fetch_first->[0], '1bd');
3770
$result = $dbi->select(table => 'table1');
3771
is($result->type_rule2_on->fetch_first->[0], '1bde');