DBIx-Custom / t / sqlite.t /
Newer Older
3837 lines | 121.425kb
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 'insert';
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);
cleanup test
Yuki Kimoto authored on 2011-08-06
71
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
72
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-06
73
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
74
$rows   = $result->all;
75
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
76

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

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

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

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

            
101
$dbi->quote('"');
cleanup test
Yuki Kimoto authored on 2011-08-10
102
eval { $dbi->execute("drop table ${q}table$p") };
103
$dbi->execute("create table ${q}table$p (${q}select$p)");
cleanup test
Yuki Kimoto authored on 2011-08-06
104
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
105
$dbi->insert(table => 'table', param => {select => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
106
$result = $dbi->execute("select * from ${q}table$p");
cleanup test
Yuki Kimoto authored on 2011-08-06
107
$rows   = $result->all;
108
is_deeply($rows, [{select => 2}], "reserved word");
109

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

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

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

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

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

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

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

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

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

            
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);
cleanup test
Yuki Kimoto authored on 2011-08-06
185
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
186
$where = $dbi->where;
187
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
188
$where->param({key1 => 1, key2 => 2});
189
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
190
$result = $dbi->select(table => 'table1');
191
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
192

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

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

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

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

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

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

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

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

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

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

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

            
293

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

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

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

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

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

            
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
$where = $dbi->where;
330
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
331
$where->param({ke1 => 1, key2 => 2});
332
$dbi->delete(table => 'table1', where => $where);
333
$result = $dbi->select(table => 'table1');
334
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
335

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

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

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

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

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

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

            
389

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
540

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

            
546
$dbi->begin_work;
547

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

            
554
$dbi->rollback if $@;
555

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

            
560
$dbi->begin_work;
561

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

            
567
$dbi->commit unless $@;
568

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

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

            
578

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
861
$dbi = MyDBI->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
862
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
863
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
864
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
865
is($dbi->select(table => 'table1')->one->{key1}, 1);
866

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

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

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

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

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

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

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

            
941
test 'empty where select';
test cleanup
Yuki Kimoto authored on 2011-08-10
942
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
943
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
944
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
945
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
946
$result = $dbi->select(table => 'table1', where => {});
947
$row = $result->one;
948
is_deeply($row, {key1 => 1, key2 => 2});
949

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1233

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

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

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

            
1261
$where = $dbi->where;
1262
$where->clause(['or', ':key1{=}', ':key1{=}']);
1263
$where->param({key1 => [undef, undef]});
1264
$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]});
1265
$row = $result->all;
1266
is_deeply($row, [{key1 => 1, key2 => 2}]);
1267
$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]});
1268
$row = $result->all;
1269
is_deeply($row, [{key1 => 1, key2 => 2}]);
1270

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1427

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

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

            
1436
$dbi->apply_filter(
1437

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1569
{
1570
    package MyDBI4;
1571

            
1572
    use strict;
1573
    use warnings;
1574

            
1575
    use base 'DBIx::Custom';
1576

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

            
1588
    package MyModel2::Base1;
1589

            
1590
    use strict;
1591
    use warnings;
1592

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

            
1595
    package MyModel2::book;
1596

            
1597
    use strict;
1598
    use warnings;
1599

            
1600
    use base 'MyModel2::Base1';
1601

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

            
1608
    sub list { shift->select; }
1609

            
1610
    package MyModel2::Company;
1611

            
1612
    use strict;
1613
    use warnings;
1614

            
1615
    use base 'MyModel2::Base1';
1616

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

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

            
1636
{
1637
     package MyDBI5;
1638

            
1639
    use strict;
1640
    use warnings;
1641

            
1642
    use base 'DBIx::Custom';
1643

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

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

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

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

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

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

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

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

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

            
1733
is($dbi->select(table => 'table1')->one->{key1}, 1);
1734
is($dbi->select(table => 'table1')->one->{key2}, 2);
1735
is($dbi->select(table => 'table1')->one->{key3}, 3);
1736

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1882

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

            
1914
test 'model insert_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
1915
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1916
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1917
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1918
$dbi->model('table1')->insert_at(
1919
    where => [1, 2],
1920
    param => {key3 => 3}
1921
);
1922
$result = $dbi->model('table1')->select;
1923
$row = $result->one;
1924
is($row->{key1}, 1);
1925
is($row->{key2}, 2);
1926
is($row->{key3}, 3);
1927

            
1928
test 'model update_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
1929
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1930
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1931
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1932
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1933
$dbi->model('table1')->update_at(
1934
    where => [1, 2],
1935
    param => {key3 => 4}
1936
);
1937
$result = $dbi->model('table1')->select;
1938
$row = $result->one;
1939
is($row->{key1}, 1);
1940
is($row->{key2}, 2);
1941
is($row->{key3}, 4);
1942

            
1943
test 'model select_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
1944
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
1945
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
1946
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
1947
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1948
$result = $dbi->model('table1')->select_at(where => [1, 2]);
1949
$row = $result->one;
1950
is($row->{key1}, 1);
1951
is($row->{key2}, 2);
1952
is($row->{key3}, 3);
1953

            
1954

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

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

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

            
2007

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

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

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

            
2033
$param = {key2 => 11, key3 => 33};
2034
$update_param = $dbi->update_param($param, {no_set => 1});
2035
$sql = <<"EOS";
2036
update table1 set $update_param
2037
where key1 = 1
2038
EOS
2039
$dbi->execute($sql, param => $param);
test cleanup
Yuki Kimoto authored on 2011-08-06
2040
$result = $dbi->execute('select * from table1;', table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
2041
$rows   = $result->all;
2042
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
2043
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
2044
                  "update param no_set");
2045

            
2046
            
2047
eval { $dbi->update_param({";" => 1}) };
2048
like($@, qr/not safety/);
2049

            
2050

            
2051
test 'update_param';
test cleanup
Yuki Kimoto authored on 2011-08-10
2052
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2053
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2054
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2055
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
2056
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
2057

            
2058
$param = {key2 => 11};
2059
$update_param = $dbi->assign_param($param);
2060
$sql = <<"EOS";
2061
update table1 set $update_param
2062
where key1 = 1
2063
EOS
2064
$dbi->execute($sql, param => $param, table => 'table1');
test cleanup
Yuki Kimoto authored on 2011-08-06
2065
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
2066
$rows   = $result->all;
2067
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
2068
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
2069
                  "basic");
2070

            
2071

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2085
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
2086
$dbi->quote('"');
cleanup test
Yuki Kimoto authored on 2011-08-10
2087
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2088
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2089
$param = {key1 => 1, key2 => 2};
2090
$insert_param = $dbi->insert_param($param);
2091
$sql = <<"EOS";
2092
insert into table1 $insert_param
2093
EOS
2094
$dbi->execute($sql, param => $param, table => 'table1');
2095
is($dbi->select(table => 'table1')->one->{key1}, 1);
2096
is($dbi->select(table => 'table1')->one->{key2}, 2);
2097

            
2098
eval { $dbi->insert_param({";" => 1}) };
2099
like($@, qr/not safety/);
2100

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

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

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

            
2127
eval {
2128
    $rows = $dbi->select(
2129
        table => 'table1',
2130
        column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
2131
        where   => {'table1.key2' => 2},
2132
        join  => {'table1.key1' => 'table2.key1'}
2133
    );
2134
};
2135
like ($@, qr/array/);
2136

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

            
2145
$rows = $dbi->select(
2146
    column => 'table3.key4 as table3__key4',
2147
    table => 'table1',
2148
    where   => {'table1.key1' => 1},
2149
    join  => ['left outer join table2 on table1.key1 = table2.key1',
2150
              'left outer join table3 on table2.key3 = table3.key3']
2151
)->all;
2152
is_deeply($rows, [{table3__key4 => 4}]);
2153

            
2154
$rows = $dbi->select(
2155
    column => 'table1.key1 as table1__key1',
2156
    table => 'table1',
2157
    where   => {'table3.key4' => 4},
2158
    join  => ['left outer join table2 on table1.key1 = table2.key1',
2159
              'left outer join table3 on table2.key3 = table3.key3']
2160
)->all;
2161
is_deeply($rows, [{table1__key1 => 1}]);
2162

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2163
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
2164
$dbi->quote('"');
cleanup test
Yuki Kimoto authored on 2011-08-10
2165
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2166
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2167
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-06
2168
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2169
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
2170
$rows = $dbi->select(
2171
    table => 'table1',
2172
    column => '"table1"."key1" as "table1_key1", "table2"."key1" as "table2_key1", "key2", "key3"',
2173
    where   => {'table1.key2' => 2},
2174
    join  => ['left outer join "table2" on "table1"."key1" = "table2"."key1"'],
2175
)->all;
2176
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}],
2177
          'quote');
2178

            
2179
{
2180
    package MyDBI8;
2181
    
2182
    use base 'DBIx::Custom';
2183
    
2184
    sub connect {
2185
        my $self = shift->SUPER::connect(@_);
2186
        
2187
        $self->include_model('MyModel7');
2188
        
2189
        return $self;
2190
    }
2191
}
cleanup test
Yuki Kimoto authored on 2011-08-06
2192

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2193
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2194
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2195
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2196
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2197
$sql = <<"EOS";
cleanup test
Yuki Kimoto authored on 2011-08-06
2198
left outer join (
2199
  select * from table1 as t1
2200
  where t1.key2 = (
2201
    select max(t2.key2) from table1 as t2
2202
    where t1.key1 = t2.key1
2203
  )
2204
) as latest_table1 on table1.key1 = latest_table1.key1
2205
EOS
cleanup test
Yuki Kimoto authored on 2011-08-06
2206
$join = [$sql];
2207
$rows = $dbi->select(
2208
    table => 'table1',
2209
    column => 'latest_table1.key1 as latest_table1__key1',
2210
    join  => $join
2211
)->all;
2212
is_deeply($rows, [{latest_table1__key1 => 1}]);
2213

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2246
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2247
eval { $dbi->execute('drop table table1') };
2248
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2249
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2250
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2251
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2252
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2253
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
2254
$result = $dbi->select(
2255
    table => 'table1',
2256
    column => [{table2 => ['key3']}],
2257
    join => [
2258
        {
2259
            clause => "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1",
2260
            table => ['table1', 'table2']
2261
        }
2262
    ]
2263
);
2264
is_deeply($result->all, [{'table2.key3' => 4}]);
2265

            
2266
test 'mycolumn';
test cleanup
Yuki Kimoto authored on 2011-08-10
2267
$dbi = MyDBI8->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2268
eval { $dbi->execute('drop table table1') };
2269
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2270
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2271
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2272
$dbi->setup_model;
2273
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2274
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2275
$model = $dbi->model('table1');
2276
$result = $model->select_at(
2277
    column => [
2278
        $model->mycolumn,
2279
        $model->column('table2')
2280
    ]
2281
);
2282
is_deeply($result->one,
2283
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
2284

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

            
2302
$result = $model->select_at(
2303
    column => [
2304
        $model->mycolumn(['key1']),
2305
        ['table2.key1', as => 'table2.key1']
2306
    ]
2307
);
2308
is_deeply($result->one,
2309
          {key1 => 1, 'table2.key1' => 1});
2310

            
2311
$result = $model->select_at(
2312
    column => [
2313
        $model->mycolumn(['key1']),
2314
        ['table2.key1' => 'table2.key1']
2315
    ]
2316
);
2317
is_deeply($result->one,
2318
          {key1 => 1, 'table2.key1' => 1});
2319

            
2320
test 'dbi method from model';
2321
{
2322
    package MyDBI9;
2323
    
2324
    use base 'DBIx::Custom';
2325
    
2326
    sub connect {
2327
        my $self = shift->SUPER::connect(@_);
2328
        
2329
        $self->include_model('MyModel8')->setup_model;
2330
        
2331
        return $self;
2332
    }
2333
}
test cleanup
Yuki Kimoto authored on 2011-08-10
2334
$dbi = MyDBI9->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2335
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2336
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2337
$model = $dbi->model('table1');
2338
eval{$model->execute('select * from table1')};
2339
ok(!$@);
2340

            
2341
test 'column table option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2342
$dbi = MyDBI9->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2343
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2344
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2345
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2346
$dbi->setup_model;
2347
$dbi->execute('insert into table1 (key1, key2) values (1, 2);');
2348
$dbi->execute('insert into table2 (key1, key3) values (1, 4);');
2349
$model = $dbi->model('table1');
2350
$result = $model->select(
2351
    column => [
2352
        $model->column('table2', {alias => 'table2_alias'})
2353
    ],
2354
    where => {'table2_alias.key3' => 4}
2355
);
2356
is_deeply($result->one, 
2357
          {'table2_alias.key1' => 1, 'table2_alias.key3' => 4});
2358

            
2359
$dbi->separator('__');
2360
$result = $model->select(
2361
    column => [
2362
        $model->column('table2', {alias => 'table2_alias'})
2363
    ],
2364
    where => {'table2_alias.key3' => 4}
2365
);
2366
is_deeply($result->one, 
2367
          {'table2_alias__key1' => 1, 'table2_alias__key3' => 4});
2368

            
2369
$dbi->separator('-');
2370
$result = $model->select(
2371
    column => [
2372
        $model->column('table2', {alias => 'table2_alias'})
2373
    ],
2374
    where => {'table2_alias.key3' => 4}
2375
);
2376
is_deeply($result->one, 
2377
          {'table2_alias-key1' => 1, 'table2_alias-key3' => 4});
2378

            
2379
test 'type option'; # DEPRECATED!
2380
$dbi = DBIx::Custom->connect(
2381
    dbi_option => {
2382
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
2383
    }
2384
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2385
$binary = pack("I3", 1, 2, 3);
2386
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
2387
$dbi->execute('create table table1(key1, key2)');
2388
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
2389
$result = $dbi->select(table => 'table1');
2390
$row   = $result->one;
2391
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2392
$result = $dbi->execute('select length(key1) as key1_length from table1');
2393
$row = $result->one;
2394
is($row->{key1_length}, length $binary);
2395

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

            
2404

            
2405
test 'bind_type option';
2406
$dbi = DBIx::Custom->connect(
2407
    dbi_option => {
2408
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
2409
    }
2410
);
2411
$binary = pack("I3", 1, 2, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2412
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
2413
$dbi->execute('create table table1(key1, key2)');
2414
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, bind_type => [key1 => DBI::SQL_BLOB]);
2415
$result = $dbi->select(table => 'table1');
2416
$row   = $result->one;
2417
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2418
$result = $dbi->execute('select length(key1) as key1_length from table1');
2419
$row = $result->one;
2420
is($row->{key1_length}, length $binary);
2421

            
2422
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, bind_type => [['key1'] => DBI::SQL_BLOB]);
2423
$result = $dbi->select(table => 'table1');
2424
$row   = $result->one;
2425
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2426
$result = $dbi->execute('select length(key1) as key1_length from table1');
2427
$row = $result->one;
2428
is($row->{key1_length}, length $binary);
2429

            
2430
test 'model type attribute';
2431
$dbi = DBIx::Custom->connect(
2432
    dbi_option => {
2433
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
2434
    }
2435
);
2436
$binary = pack("I3", 1, 2, 3);
cleanup test
Yuki Kimoto authored on 2011-08-10
2437
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-06
2438
$dbi->execute('create table table1(key1, key2)');
2439
$model = $dbi->create_model(table => 'table1', bind_type => [key1 => DBI::SQL_BLOB]);
2440
$model->insert(param => {key1 => $binary, key2 => 'あ'});
2441
$result = $dbi->select(table => 'table1');
2442
$row   = $result->one;
2443
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2444
$result = $dbi->execute('select length(key1) as key1_length from table1');
2445
$row = $result->one;
2446
is($row->{key1_length}, length $binary);
2447

            
2448
test 'create_model';
test cleanup
Yuki Kimoto authored on 2011-08-10
2449
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2450
eval { $dbi->execute('drop table table1') };
2451
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2452
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2453
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2454

            
2455
$dbi->create_model(
2456
    table => 'table1',
2457
    join => [
2458
       'left outer join table2 on table1.key1 = table2.key1'
2459
    ],
2460
    primary_key => ['key1']
2461
);
2462
$model2 = $dbi->create_model(
2463
    table => 'table2'
2464
);
2465
$dbi->create_model(
2466
    table => 'table3',
2467
    filter => [
2468
        key1 => {in => sub { uc $_[0] }}
2469
    ]
2470
);
2471
$dbi->setup_model;
2472
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2473
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2474
$model = $dbi->model('table1');
2475
$result = $model->select(
2476
    column => [$model->mycolumn, $model->column('table2')],
2477
    where => {'table1.key1' => 1}
2478
);
2479
is_deeply($result->one,
2480
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
2481
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
2482

            
2483
test 'model method';
2484
test 'create_model';
test cleanup
Yuki Kimoto authored on 2011-08-10
2485
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2486
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-06
2487
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2488
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2489
$model = $dbi->create_model(
2490
    table => 'table2'
2491
);
2492
$model->method(foo => sub { shift->select(@_) });
2493
is_deeply($model->foo->one, {key1 => 1, key3 => 3});
2494

            
2495
test 'merge_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
2496
$dbi = DBIx::Custom->new;
2497
$params = [
2498
    {key1 => 1, key2 => 2, key3 => 3},
2499
    {key1 => 1, key2 => 2},
2500
    {key1 => 1}
2501
];
2502
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]);
2503
is_deeply($param, {key1 => [1, 1, 1], key2 => [2, 2], key3 => 3});
2504

            
2505
$params = [
2506
    {key1 => [1, 2], key2 => 1, key3 => [1, 2]},
2507
    {key1 => [3, 4], key2 => [2, 3], key3 => 3}
2508
];
2509
$param = $dbi->merge_param($params->[0], $params->[1]);
2510
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
2511

            
2512
test 'select() param option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2513
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2514
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2515
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2516
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2517
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
2518
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2519
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2520
$dbi->insert(table => 'table2', param => {key1 => 2, key3 => 5});
2521
$rows = $dbi->select(
2522
    table => 'table1',
2523
    column => 'table1.key1 as table1_key1, key2, key3',
2524
    where   => {'table1.key2' => 3},
2525
    join  => ['inner join (select * from table2 where {= table2.key3})' . 
2526
              ' as table2 on table1.key1 = table2.key1'],
2527
    param => {'table2.key3' => 5}
2528
)->all;
2529
is_deeply($rows, [{table1_key1 => 2, key2 => 3, key3 => 5}]);
2530

            
2531

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

            
2545
eval {
2546
$dbi->select(
2547
    table => 'table1',
2548
    column => 'key1',
2549
    wrap => 'select * from ('
2550
)
2551
};
2552
like($@, qr/array/);
2553

            
2554
test 'select() string where';
test cleanup
Yuki Kimoto authored on 2011-08-10
2555
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2556
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2557
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2558
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2559
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2560
$rows = $dbi->select(
2561
    table => 'table1',
2562
    where => 'key1 = :key1 and key2 = :key2',
2563
    where_param => {key1 => 1, key2 => 2}
2564
)->all;
2565
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2566

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2567
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2568
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2569
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2570
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2571
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2572
$rows = $dbi->select(
2573
    table => 'table1',
2574
    where => [
2575
        'key1 = :key1 and key2 = :key2',
2576
        {key1 => 1, key2 => 2}
2577
    ]
2578
)->all;
2579
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2580

            
2581
test 'delete() string where';
test cleanup
Yuki Kimoto authored on 2011-08-10
2582
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2583
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2584
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2585
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2586
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2587
$dbi->delete(
2588
    table => 'table1',
2589
    where => 'key1 = :key1 and key2 = :key2',
2590
    where_param => {key1 => 1, key2 => 2}
2591
);
2592
$rows = $dbi->select(table => 'table1')->all;
2593
is_deeply($rows, [{key1 => 2, key2 => 3}]);
2594

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2595
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2596
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2597
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2598
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2599
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2600
$dbi->delete(
2601
    table => 'table1',
2602
    where => [
2603
        'key1 = :key1 and key2 = :key2',
2604
         {key1 => 1, key2 => 2}
2605
    ]
2606
);
2607
$rows = $dbi->select(table => 'table1')->all;
2608
is_deeply($rows, [{key1 => 2, key2 => 3}]);
2609

            
2610

            
2611
test 'update() string where';
test cleanup
Yuki Kimoto authored on 2011-08-10
2612
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2613
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2614
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2615
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2616
$dbi->update(
2617
    table => 'table1',
2618
    param => {key1 => 5},
2619
    where => 'key1 = :key1 and key2 = :key2',
2620
    where_param => {key1 => 1, key2 => 2}
2621
);
2622
$rows = $dbi->select(table => 'table1')->all;
2623
is_deeply($rows, [{key1 => 5, key2 => 2}]);
2624

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2625
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2626
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2627
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2628
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2629
$dbi->update(
2630
    table => 'table1',
2631
    param => {key1 => 5},
2632
    where => [
2633
        'key1 = :key1 and key2 = :key2',
2634
        {key1 => 1, key2 => 2}
2635
    ]
2636
);
2637
$rows = $dbi->select(table => 'table1')->all;
2638
is_deeply($rows, [{key1 => 5, key2 => 2}]);
2639

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

            
2654
$dbi->delete_all(table => 'table1');
2655
$dbi->insert(
2656
    primary_key => 'key1', 
2657
    table => 'table1',
2658
    id => 0,
2659
    param => {key2 => 2, key3 => 3}
2660
);
2661

            
2662
is($dbi->select(table => 'table1')->one->{key1}, 0);
2663
is($dbi->select(table => 'table1')->one->{key2}, 2);
2664
is($dbi->select(table => 'table1')->one->{key3}, 3);
2665

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

            
2679

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2694
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2695
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2696
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2697
$dbi->model('table1')->insert(
2698
    {key3 => 3},
2699
    id => [1, 2]
2700
);
2701
$result = $dbi->model('table1')->select;
2702
$row = $result->one;
2703
is($row->{key1}, 1);
2704
is($row->{key2}, 2);
2705
is($row->{key3}, 3);
2706

            
2707
test 'update and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2708
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2709
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2710
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2711
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2712
$dbi->update(
2713
    table => 'table1',
2714
    primary_key => ['key1', 'key2'],
2715
    id => [1, 2],
2716
    param => {key3 => 4}
2717
);
2718
is($dbi->select(table => 'table1')->one->{key1}, 1);
2719
is($dbi->select(table => 'table1')->one->{key2}, 2);
2720
is($dbi->select(table => 'table1')->one->{key3}, 4);
2721

            
2722
$dbi->delete_all(table => 'table1');
2723
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2724
$dbi->update(
2725
    table => 'table1',
2726
    primary_key => 'key1',
2727
    id => 0,
2728
    param => {key3 => 4}
2729
);
2730
is($dbi->select(table => 'table1')->one->{key1}, 0);
2731
is($dbi->select(table => 'table1')->one->{key2}, 2);
2732
is($dbi->select(table => 'table1')->one->{key3}, 4);
2733

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2734
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2735
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2736
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2737
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2738
$dbi->update(
2739
    {key3 => 4},
2740
    table => 'table1',
2741
    primary_key => ['key1', 'key2'],
2742
    id => [1, 2]
2743
);
2744
is($dbi->select(table => 'table1')->one->{key1}, 1);
2745
is($dbi->select(table => 'table1')->one->{key2}, 2);
2746
is($dbi->select(table => 'table1')->one->{key3}, 4);
2747

            
2748

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

            
2764

            
2765
test 'delete and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2766
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2767
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2768
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2769
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2770
$dbi->delete(
2771
    table => 'table1',
2772
    primary_key => ['key1', 'key2'],
2773
    id => [1, 2],
2774
);
2775
is_deeply($dbi->select(table => 'table1')->all, []);
2776

            
2777
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2778
$dbi->delete(
2779
    table => 'table1',
2780
    primary_key => 'key1',
2781
    id => 0,
2782
);
2783
is_deeply($dbi->select(table => 'table1')->all, []);
2784

            
2785

            
2786
test 'model delete and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2787
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2788
eval { $dbi->execute('drop table table1') };
2789
eval { $dbi->execute('drop table table2') };
2790
eval { $dbi->execute('drop table table3') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2791
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2792
$dbi->execute("create table table2 (key1, key2, key3)");
2793
$dbi->execute("create table table3 (key1, key2, key3)");
2794
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2795
$dbi->model('table1')->delete(id => [1, 2]);
2796
is_deeply($dbi->select(table => 'table1')->all, []);
2797
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
2798
$dbi->model('table1_1')->delete(id => [1, 2]);
2799
is_deeply($dbi->select(table => 'table1')->all, []);
2800
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
2801
$dbi->model('table1_3')->delete(id => [1, 2]);
2802
is_deeply($dbi->select(table => 'table1')->all, []);
2803

            
2804

            
2805
test 'select and id option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2806
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2807
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2808
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2809
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2810
$result = $dbi->select(
2811
    table => 'table1',
2812
    primary_key => ['key1', 'key2'],
2813
    id => [1, 2]
2814
);
2815
$row = $result->one;
2816
is($row->{key1}, 1);
2817
is($row->{key2}, 2);
2818
is($row->{key3}, 3);
2819

            
2820
$dbi->delete_all(table => 'table1');
2821
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2822
$result = $dbi->select(
2823
    table => 'table1',
2824
    primary_key => 'key1',
2825
    id => 0,
2826
);
2827
$row = $result->one;
2828
is($row->{key1}, 0);
2829
is($row->{key2}, 2);
2830
is($row->{key3}, 3);
2831

            
2832
$dbi->delete_all(table => 'table1');
2833
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2834
$result = $dbi->select(
2835
    table => 'table1',
2836
    primary_key => ['key1', 'key2'],
2837
    id => [1, 2]
2838
);
2839
$row = $result->one;
2840
is($row->{key1}, 1);
2841
is($row->{key2}, 2);
2842
is($row->{key3}, 3);
2843

            
2844

            
2845
test 'model select_at';
test cleanup
Yuki Kimoto authored on 2011-08-10
2846
$dbi = MyDBI6->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2847
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2848
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2849
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2850
$result = $dbi->model('table1')->select(id => [1, 2]);
2851
$row = $result->one;
2852
is($row->{key1}, 1);
2853
is($row->{key2}, 2);
2854
is($row->{key3}, 3);
2855

            
2856
test 'column separator is default .';
test cleanup
Yuki Kimoto authored on 2011-08-10
2857
$dbi = MyDBI7->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2858
eval { $dbi->execute('drop table table1') };
2859
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2860
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-06
2861
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2862
$dbi->setup_model;
2863
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2864
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2865
$model = $dbi->model('table1');
2866
$result = $model->select(
2867
    column => [$model->column('table2')],
2868
    where => {'table1.key1' => 1}
2869
);
2870
is_deeply($result->one,
2871
          {'table2.key1' => 1, 'table2.key3' => 3});
2872

            
2873
$result = $model->select(
2874
    column => [$model->column('table2' => [qw/key1 key3/])],
2875
    where => {'table1.key1' => 1}
2876
);
2877
is_deeply($result->one,
2878
          {'table2.key1' => 1, 'table2.key3' => 3});
2879

            
2880

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

            
2882
test 'separator';
test cleanup
Yuki Kimoto authored on 2011-08-10
2883
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2884
eval { $dbi->execute('drop table table1') };
2885
eval { $dbi->execute('drop table table2') };
2886
$dbi->execute($create_table1);
2887
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
2888

            
2889
$dbi->create_model(
2890
    table => 'table1',
2891
    join => [
2892
       'left outer join table2 on table1.key1 = table2.key1'
2893
    ],
2894
    primary_key => ['key1'],
cleanup test
Yuki Kimoto authored on 2011-08-06
2895
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2896
$model2 = $dbi->create_model(
2897
    table => 'table2',
2898
);
2899
$dbi->setup_model;
2900
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2901
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2902
$model = $dbi->model('table1');
2903
$result = $model->select(
2904
    column => [
2905
        $model->mycolumn,
2906
        {table2 => [qw/key1 key3/]}
2907
    ],
2908
    where => {'table1.key1' => 1}
2909
);
2910
is_deeply($result->one,
2911
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
2912
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
2913

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2914
$dbi->separator('__');
2915
$model = $dbi->model('table1');
2916
$result = $model->select(
2917
    column => [
2918
        $model->mycolumn,
2919
        {table2 => [qw/key1 key3/]}
2920
    ],
2921
    where => {'table1.key1' => 1}
2922
);
2923
is_deeply($result->one,
2924
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
2925
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
2926

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2927
$dbi->separator('-');
2928
$model = $dbi->model('table1');
2929
$result = $model->select(
2930
    column => [
2931
        $model->mycolumn,
2932
        {table2 => [qw/key1 key3/]}
2933
    ],
2934
    where => {'table1.key1' => 1}
2935
);
2936
is_deeply($result->one,
2937
          {key1 => 1, key2 => 2, 'table2-key1' => 1, 'table2-key3' => 3});
2938
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
2939

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

            
2941
test 'filter_off';
test cleanup
Yuki Kimoto authored on 2011-08-10
2942
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2943
eval { $dbi->execute('drop table table1') };
2944
eval { $dbi->execute('drop table table2') };
2945
$dbi->execute($create_table1);
2946
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
2947

            
2948
$dbi->create_model(
2949
    table => 'table1',
2950
    join => [
2951
       'left outer join table2 on table1.key1 = table2.key1'
2952
    ],
2953
    primary_key => ['key1'],
cleanup test
Yuki Kimoto authored on 2011-08-06
2954
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2955
$dbi->setup_model;
2956
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2957
$model = $dbi->model('table1');
2958
$result = $model->select(column => 'key1');
2959
$result->filter(key1 => sub { $_[0] * 2 });
2960
is_deeply($result->one, {key1 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-06
2961

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2962
test 'available_datetype';
test cleanup
Yuki Kimoto authored on 2011-08-10
2963
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2964
ok($dbi->can('available_datatype'));
2965

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2967
test 'select prefix option';
test cleanup
Yuki Kimoto authored on 2011-08-10
2968
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2969
eval { $dbi->execute('drop table table1') };
2970
$dbi->execute($create_table1);
2971
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2972
$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all;
2973
is_deeply($rows, [{key1 => 1, key2 => 2}], "table");
2974

            
2975

            
2976
test 'separator';
2977
$dbi = DBIx::Custom->connect;
2978
is($dbi->separator, '.');
2979
$dbi->separator('-');
2980
is($dbi->separator, '-');
2981
$dbi->separator('__');
2982
is($dbi->separator, '__');
2983
eval { $dbi->separator('?') };
2984
like($@, qr/Separator/);
2985

            
2986

            
2987
test 'map_param';
2988
$dbi = DBIx::Custom->connect;
2989
$param = $dbi->map_param(
2990
    {id => 1, author => 'Ken', price => 1900},
2991
    id => 'book.id',
2992
    author => ['book.author', sub { '%' . $_[0] . '%' }],
2993
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
cleanup test
Yuki Kimoto authored on 2011-08-06
2994
);
cleanup test
Yuki Kimoto authored on 2011-08-10
2995
is_deeply($param, {'book.id' => 1, 'book.author' => '%Ken%',
2996
  'book.price' => 1900});
2997

            
2998
$param = $dbi->map_param(
2999
    {id => 0, author => 0, price => 0},
3000
    id => 'book.id',
3001
    author => ['book.author', sub { '%' . $_[0] . '%' }],
3002
    price => ['book.price', sub { '%' . $_[0] . '%' },
3003
      {if => sub { $_[0] eq 0 }}]
cleanup test
Yuki Kimoto authored on 2011-08-06
3004
);
cleanup test
Yuki Kimoto authored on 2011-08-10
3005
is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
cleanup test
Yuki Kimoto authored on 2011-08-06
3006

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3007
$param = $dbi->map_param(
3008
    {id => '', author => '', price => ''},
3009
    id => 'book.id',
3010
    author => ['book.author', sub { '%' . $_[0] . '%' }],
3011
    price => ['book.price', sub { '%' . $_[0] . '%' },
3012
      {if => sub { $_[0] eq 1 }}]
cleanup test
Yuki Kimoto authored on 2011-08-06
3013
);
cleanup test
Yuki Kimoto authored on 2011-08-10
3014
is_deeply($param, {});
3015

            
3016
$param = $dbi->map_param(
3017
    {id => undef, author => undef, price => undef},
3018
    id => 'book.id',
3019
    price => ['book.price', {if => 'exists'}]
cleanup test
Yuki Kimoto authored on 2011-08-06
3020
);
cleanup test
Yuki Kimoto authored on 2011-08-10
3021
is_deeply($param, {'book.price' => undef});
3022

            
3023
$param = $dbi->map_param(
3024
    {price => 'a'},
3025
    id => ['book.id', {if => 'exists'}],
3026
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
3027
);
3028
is_deeply($param, {'book.price' => '%a'});
cleanup test
Yuki Kimoto authored on 2011-08-06
3029

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

            
3031
test 'table_alias';
test cleanup
Yuki Kimoto authored on 2011-08-10
3032
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3033
eval { $dbi->execute('drop table table1') };
3034
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
cleanup test
Yuki Kimoto authored on 2011-08-06
3035
$dbi->type_rule(
3036
    into1 => {
cleanup test
Yuki Kimoto authored on 2011-08-10
3037
        date => sub { uc $_[0] }
cleanup test
Yuki Kimoto authored on 2011-08-06
3038
    }
3039
);
cleanup test
Yuki Kimoto authored on 2011-08-10
3040
$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'},
3041
  table_alias => {table2 => 'table1'});
cleanup test
Yuki Kimoto authored on 2011-08-06
3042
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3043
is($result->one->{key1}, 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
3044

            
3045

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3046
test 'order';
test cleanup
Yuki Kimoto authored on 2011-08-10
3047
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3048
eval { $dbi->execute('drop table table1') };
3049
$dbi->execute("create table table1 (key1, key2)");
3050
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
3051
$dbi->insert({key1 => 1, key2 => 3}, table => 'table1');
3052
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
3053
$dbi->insert({key1 => 2, key2 => 4}, table => 'table1');
3054
my $order = $dbi->order;
3055
$order->prepend('key1', 'key2 desc');
3056
$result = $dbi->select(table => 'table1', append => "$order");
3057
is_deeply($result->all, [{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1},
3058
  {key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}]);
3059
$order->prepend('key1 desc');
3060
$result = $dbi->select(table => 'table1', append => "$order");
3061
is_deeply($result->all, [{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2},
3062
  {key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
3063

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3064
$order = $dbi->order;
3065
$order->prepend(['table1-key1'], [qw/table1-key2 desc/]);
3066
$result = $dbi->select(table => 'table1',
3067
  column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']],
3068
  append => "$order");
3069
is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3},
3070
  {'table1-key1' => 1, 'table1-key2' => 1},
3071
  {'table1-key1' => 2, 'table1-key2' => 4},
3072
  {'table1-key1' => 2, 'table1-key2' => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
3073

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3074
test 'tag_parse';
test cleanup
Yuki Kimoto authored on 2011-08-10
3075
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3076
$dbi->tag_parse(0);
3077
eval { $dbi->execute('drop table table1') };
3078
$dbi->execute("create table table1 (key1, key2)");
3079
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
3080
eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})};
3081
ok($@);
cleanup test
Yuki Kimoto authored on 2011-08-06
3082

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3083
test 'last_sql';
test cleanup
Yuki Kimoto authored on 2011-08-10
3084
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3085
eval { $dbi->execute('drop table table1') };
3086
$dbi->execute("create table table1 (key1, key2)");
3087
$dbi->execute('select * from table1');
3088
is($dbi->last_sql, 'select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
3089

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3093
test 'DBIx::Custom header';
test cleanup
Yuki Kimoto authored on 2011-08-10
3094
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3095
eval { $dbi->execute('drop table table1') };
3096
$dbi->execute("create table table1 (key1, key2)");
3097
$result = $dbi->execute('select key1 as h1, key2 as h2 from table1');
3098
is_deeply($result->header, [qw/h1 h2/]);
cleanup test
Yuki Kimoto authored on 2011-08-06
3099

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

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3121
$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}";
3122
$result = $dbi->execute(
3123
    $source,
3124
    param => {'table1.key1' => 1, 'table1.key2' => 1},
3125
    filter => {'table1.key2' => sub { $_[0] * 2 }}
cleanup test
Yuki Kimoto authored on 2011-08-06
3126
);
cleanup test
Yuki Kimoto authored on 2011-08-10
3127
$rows = $result->all;
3128
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
3129

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3130
test 'high perfomance way';
3131
$dbi->execute('drop table table1');
3132
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
3133
$rows = [
3134
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3135
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3136
];
3137
{
3138
    my $query;
3139
    foreach my $row (@$rows) {
3140
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
3141
      $dbi->execute($query, $row, filter => {ab => sub { $_[0] * 2 }});
cleanup test
Yuki Kimoto authored on 2011-08-06
3142
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3143
    is_deeply($dbi->select(table => 'table1')->all,
3144
      [
3145
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3146
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3147
      ]
3148
    );
3149
}
3150

            
3151
$dbi->execute('drop table table1');
3152
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
3153
$rows = [
3154
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3155
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3156
];
3157
{
3158
    my $query;
3159
    my $sth;
3160
    foreach my $row (@$rows) {
3161
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
3162
      $sth ||= $query->sth;
3163
      $sth->execute(map { $row->{$_} } sort keys %$row);
cleanup test
Yuki Kimoto authored on 2011-08-06
3164
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3165
    is_deeply($dbi->select(table => 'table1')->all,
3166
      [
3167
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3168
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3169
      ]
3170
    );
3171
}
cleanup test
Yuki Kimoto authored on 2011-08-06
3172

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3173
test 'result';
test cleanup
Yuki Kimoto authored on 2011-08-10
3174
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3175
eval { $dbi->execute('drop table table1') };
3176
$dbi->execute($create_table1);
3177
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3178
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3179

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3180
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3181
@rows = ();
3182
while (my $row = $result->fetch) {
3183
    push @rows, [@$row];
3184
}
3185
is_deeply(\@rows, [[1, 2], [3, 4]]);
cleanup test
Yuki Kimoto authored on 2011-08-06
3186

            
3187
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3188
@rows = ();
3189
while (my $row = $result->fetch_hash) {
3190
    push @rows, {%$row};
3191
}
3192
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
3193

            
3194
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3195
$row = $result->fetch_first;
3196
is_deeply($row, [1, 2], "row");
3197
$row = $result->fetch;
3198
ok(!$row, "finished");
3199

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3200
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3201
$row = $result->fetch_hash_first;
3202
is_deeply($row, {key1 => 1, key2 => 2}, "row");
3203
$row = $result->fetch_hash;
3204
ok(!$row, "finished");
3205

            
3206
$dbi->execute('create table table2 (key1, key2);');
3207
$result = $dbi->select(table => 'table2');
3208
$row = $result->fetch_hash_first;
3209
ok(!$row, "no row fetch");
cleanup test
Yuki Kimoto authored on 2011-08-06
3210

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3211
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3212
eval { $dbi->execute('drop table table1') };
3213
$dbi->execute($create_table1);
3214
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3215
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3216
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1');
3217
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1');
3218
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
3219
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3220
$rows = $result->fetch_multi(2);
3221
is_deeply($rows, [[1, 2],
3222
                  [3, 4]], "fetch_multi first");
3223
$rows = $result->fetch_multi(2);
3224
is_deeply($rows, [[5, 6],
3225
                  [7, 8]], "fetch_multi secound");
3226
$rows = $result->fetch_multi(2);
3227
is_deeply($rows, [[9, 10]], "fetch_multi third");
3228
$rows = $result->fetch_multi(2);
3229
ok(!$rows);
3230

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

            
3235
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3236
$rows = $result->fetch_hash_multi(2);
3237
is_deeply($rows, [{key1 => 1, key2 => 2},
3238
                  {key1 => 3, key2 => 4}], "fetch_multi first");
3239
$rows = $result->fetch_hash_multi(2);
3240
is_deeply($rows, [{key1 => 5, key2 => 6},
3241
                  {key1 => 7, key2 => 8}], "fetch_multi secound");
3242
$rows = $result->fetch_hash_multi(2);
3243
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third");
3244
$rows = $result->fetch_hash_multi(2);
3245
ok(!$rows);
3246

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3251
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3252
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
3253
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
3254
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3255
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
3256

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3257
test 'fetch_all';
3258
$result = $dbi->select(table => 'table1');
3259
$rows = $result->fetch_all;
3260
is_deeply($rows, [[1, 2], [3, 4]]);
cleanup test
Yuki Kimoto authored on 2011-08-06
3261

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3273
$result = $dbi->select(table => 'table1');
3274
$result->dbi->filters({three_times => sub { $_[0] * 3}});
3275
$result->filter({key1 => 'three_times'});
3276
$rows = $result->fetch_hash_all;
3277
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash");
3278

            
3279
test "query_builder";
3280
$datas = [
3281
    # Basic tests
3282
    {   name            => 'placeholder basic',
3283
        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
3284
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;",
3285
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
3286
    },
3287
    {
3288
        name            => 'placeholder in',
3289
        source            => "{in k1 3};",
3290
        sql_expected    => "k1 in (?, ?, ?);",
3291
        columns_expected   => [qw/k1 k1 k1/]
3292
    },
3293
    
3294
    # Table name
3295
    {
3296
        name            => 'placeholder with table name',
3297
        source            => "{= a.k1} {= a.k2}",
3298
        sql_expected    => "a.k1 = ? a.k2 = ?;",
3299
        columns_expected  => [qw/a.k1 a.k2/]
3300
    },
3301
    {   
3302
        name            => 'placeholder in with table name',
3303
        source            => "{in a.k1 2} {in b.k2 2}",
3304
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?);",
3305
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
3306
    },
3307
    {
3308
        name            => 'not contain tag',
3309
        source            => "aaa",
3310
        sql_expected    => "aaa;",
3311
        columns_expected  => [],
3312
    }
3313
];
3314

            
3315
for (my $i = 0; $i < @$datas; $i++) {
3316
    my $data = $datas->[$i];
3317
    my $builder = DBIx::Custom->new->query_builder;
3318
    my $query = $builder->build_query($data->{source});
3319
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
3320
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
3321
}
3322

            
3323
$builder = DBIx::Custom->new->query_builder;
3324
$ret_val = $builder->register_tag(
3325
    p => sub {
3326
        my @args = @_;
3327
        
3328
        my $expand    = "? $args[0] $args[1]";
3329
        my $columns = [2];
3330
        return [$expand, $columns];
3331
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
3332
);
3333

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

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3347
$builder->register_tag({
3348
    q => 'string'
3349
});
cleanup test
Yuki Kimoto authored on 2011-08-06
3350

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3354
$builder->register_tag({
3355
   r => sub {} 
3356
});
cleanup test
Yuki Kimoto authored on 2011-08-06
3357

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

            
3361
$builder->register_tag({
3362
   s => sub { return ["a", ""]} 
3363
});
3364

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

            
3368
$builder->register_tag(
3369
    t => sub {return ["a", []]}
cleanup test
Yuki Kimoto authored on 2011-08-06
3370
);
3371

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

            
3373
test 'General error case';
3374
$builder = DBIx::Custom->new->query_builder;
3375
$builder->register_tag(
3376
    a => sub {
3377
        return ["? ? ?", ['']];
3378
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
3379
);
cleanup test
Yuki Kimoto authored on 2011-08-10
3380
eval{$builder->build_query("{a}")};
3381
like($@, qr/\QPlaceholder count/, "placeholder count is invalid");
cleanup test
Yuki Kimoto authored on 2011-08-06
3382

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

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

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

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

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

            
3399
test 'variouse source';
3400
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;";
3401
$query = $builder->build_query($source);
3402
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1");
3403

            
3404
$source = "abc;";
3405
$query = $builder->build_query($source);
3406
is($query->sql, 'abc;', "basic : 2");
3407

            
3408
$source = "{= a}";
3409
$query = $builder->build_query($source);
3410
is($query->sql, 'a = ?;', "only tag");
3411

            
3412
$source = "000;";
3413
$query = $builder->build_query($source);
3414
is($query->sql, '000;', "contain 0 value");
3415

            
3416
$source = "a {= b} }";
3417
eval{$builder->build_query($source)};
3418
like($@, qr/unexpected "}"/, "error : 1");
3419

            
3420
$source = "a {= {}";
3421
eval{$builder->build_query($source)};
3422
like($@, qr/unexpected "{"/, "error : 2");
3423

            
3424
### SQLite test
3425
test 'type option'; # DEPRECATED!
3426
$dbi = DBIx::Custom->connect(
3427
    data_source => 'dbi:SQLite:dbname=:memory:',
3428
    dbi_option => {
3429
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
3430
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
3431
);
cleanup test
Yuki Kimoto authored on 2011-08-10
3432
$binary = pack("I3", 1, 2, 3);
3433
eval { $dbi->execute('drop table table1') };
3434
$dbi->execute('create table table1(key1, key2)');
3435
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
3436
$result = $dbi->select(table => 'table1');
3437
$row   = $result->one;
3438
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
3439
$result = $dbi->execute('select length(key1) as key1_length from table1');
3440
$row = $result->one;
3441
is($row->{key1_length}, length $binary);
cleanup test
Yuki Kimoto authored on 2011-08-06
3442

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3443
test 'type_rule from';
3444
$dbi = DBIx::Custom->connect;
3445
$dbi->type_rule(
3446
    from1 => {
3447
        date => sub { uc $_[0] }
3448
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
3449
);
cleanup test
Yuki Kimoto authored on 2011-08-10
3450
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3451
$dbi->insert({key1 => 'a'}, table => 'table1');
3452
$result = $dbi->select(table => 'table1');
3453
is($result->fetch_first->[0], 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
3454

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

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

            
3459
test 'type_rule into';
test cleanup
Yuki Kimoto authored on 2011-08-10
3460
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
3461
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3462
$dbi->type_rule(
3463
    into1 => {
3464
        date => sub { uc $_[0] }
3465
    }
3466
);
cleanup test
Yuki Kimoto authored on 2011-08-10
3467
$dbi->insert({key1 => 'a'}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
3468
$result = $dbi->select(table => 'table1');
3469
is($result->one->{key1}, 'A');
3470

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3471
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3472
$dbi->execute("create table table1 (key1 date, key2 datetime)");
3473
$dbi->type_rule(
3474
    into1 => [
3475
         [qw/date datetime/] => sub { uc $_[0] }
3476
    ]
3477
);
3478
$dbi->insert({key1 => 'a', key2 => 'b'}, table => 'table1');
3479
$result = $dbi->select(table => 'table1');
3480
$row = $result->one;
3481
is($row->{key1}, 'A');
3482
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
3483

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3484
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3485
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3486
$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1');
3487
$dbi->type_rule(
3488
    into1 => [
3489
        [qw/date datetime/] => sub { uc $_[0] }
3490
    ]
3491
);
3492
$result = $dbi->execute(
3493
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
3494
    param => {key1 => 'a', 'table1.key2' => 'b'}
3495
);
3496
$row = $result->one;
3497
is($row->{key1}, 'a');
3498
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
3499

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3500
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3501
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3502
$dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1');
3503
$dbi->type_rule(
3504
    into1 => [
3505
        [qw/date datetime/] => sub { uc $_[0] }
3506
    ]
3507
);
3508
$result = $dbi->execute(
3509
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
3510
    param => {key1 => 'a', 'table1.key2' => 'b'},
3511
    table => 'table1'
3512
);
3513
$row = $result->one;
3514
is($row->{key1}, 'A');
3515
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
3516

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3517
$dbi = DBIx::Custom->connect;
3518
$dbi->execute("create table table1 (key1 date, key2 datetime)");
3519
$dbi->register_filter(twice => sub { $_[0] * 2 });
3520
$dbi->type_rule(
3521
    from1 => {
3522
        date => 'twice',
3523
    },
3524
    into1 => {
3525
        date => 'twice',
3526
    }
3527
);
3528
$dbi->insert({key1 => 2}, table => 'table1');
3529
$result = $dbi->select(table => 'table1');
3530
is($result->fetch->[0], 8);
cleanup test
Yuki Kimoto authored on 2011-08-06
3531

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3532
test 'type_rule and filter order';
test cleanup
Yuki Kimoto authored on 2011-08-10
3533
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3534
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3535
$dbi->type_rule(
3536
    into1 => {
3537
        date => sub { $_[0] . 'b' }
3538
    },
3539
    into2 => {
3540
        date => sub { $_[0] . 'c' }
3541
    },
3542
    from1 => {
3543
        date => sub { $_[0] . 'd' }
3544
    },
3545
    from2 => {
3546
        date => sub { $_[0] . 'e' }
3547
    }
3548
);
3549
$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }});
3550
$result = $dbi->select(table => 'table1');
3551
$result->filter(key1 => sub { $_[0] . 'f' });
3552
is($result->fetch_first->[0], '1abcdef');
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] . 'p' }
3559
    },
3560
    from2 => {
3561
        date => sub { $_[0] . 'q' }
3562
    },
3563
);
3564
$dbi->insert({key1 => '1'}, table => 'table1');
3565
$result = $dbi->select(table => 'table1');
3566
$result->type_rule(
3567
    from1 => {
3568
        date => sub { $_[0] . 'd' }
3569
    },
3570
    from2 => {
3571
        date => sub { $_[0] . 'e' }
3572
    }
3573
);
3574
$result->filter(key1 => sub { $_[0] . 'f' });
3575
is($result->fetch_first->[0], '1def');
cleanup test
Yuki Kimoto authored on 2011-08-06
3576

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3577
test 'type_rule_off';
3578
$dbi = DBIx::Custom->connect;
3579
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3580
$dbi->type_rule(
3581
    from1 => {
3582
        date => sub { $_[0] * 2 },
3583
    },
3584
    into1 => {
3585
        date => sub { $_[0] * 2 },
3586
    }
3587
);
3588
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
3589
$result = $dbi->select(table => 'table1', type_rule_off => 1);
3590
is($result->type_rule_off->fetch->[0], 2);
cleanup test
Yuki Kimoto authored on 2011-08-06
3591

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3592
$dbi = DBIx::Custom->connect;
3593
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3594
$dbi->type_rule(
3595
    from1 => {
3596
        date => sub { $_[0] * 2 },
3597
    },
3598
    into1 => {
3599
        date => sub { $_[0] * 3 },
3600
    }
3601
);
3602
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
3603
$result = $dbi->select(table => 'table1', type_rule_off => 1);
3604
is($result->one->{key1}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-06
3605

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3620
$dbi = DBIx::Custom->connect;
3621
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3622
$dbi->type_rule(
3623
    from1 => {
3624
        date => sub { $_[0] * 2 },
3625
    },
3626
    into1 => {
3627
        date => sub { $_[0] * 3 },
3628
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
3629
);
cleanup test
Yuki Kimoto authored on 2011-08-10
3630
$dbi->insert({key1 => 2}, table => 'table1');
3631
$result = $dbi->select(table => 'table1');
3632
is($result->fetch->[0], 12);
cleanup test
Yuki Kimoto authored on 2011-08-06
3633

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3634
$dbi = DBIx::Custom->connect;
3635
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3636
$dbi->register_filter(ppp => sub { uc $_[0] });
3637
$dbi->type_rule(
3638
    into1 => {
3639
        date => 'ppp'
cleanup test
Yuki Kimoto authored on 2011-08-06
3640
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3641
);
3642
$dbi->insert({key1 => 'a'}, table => 'table1');
3643
$result = $dbi->select(table => 'table1');
3644
is($result->one->{key1}, 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
3645

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3646
eval{$dbi->type_rule(
3647
    into1 => {
3648
        date => 'pp'
cleanup test
Yuki Kimoto authored on 2011-08-06
3649
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
3650
)};
3651
like($@, qr/not registered/);
cleanup test
Yuki Kimoto authored on 2011-08-06
3652

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3653
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3654
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3655
eval {
3656
    $dbi->type_rule(
3657
        from1 => {
3658
            Date => sub { $_[0] * 2 },
3659
        }
3660
    );
3661
};
3662
like($@, qr/lower/);
clenup test
Yuki Kimoto authored on 2011-08-06
3663

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3664
eval {
3665
    $dbi->type_rule(
3666
        into1 => {
3667
            Date => sub { $_[0] * 2 },
3668
        }
3669
    );
3670
};
3671
like($@, qr/lower/);
clenup test
Yuki Kimoto authored on 2011-08-06
3672

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3673
$dbi = DBIx::Custom->connect;
3674
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3675
$dbi->type_rule(
3676
    from1 => {
3677
        date => sub { $_[0] * 2 },
3678
    },
3679
    into1 => {
3680
        date => sub { $_[0] * 3 },
3681
    }
3682
);
3683
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
3684
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3685
$result->type_rule_off;
3686
is($result->one->{key1}, 6);
clenup test
Yuki Kimoto authored on 2011-08-06
3687

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

            
3707
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3708
$result->type_rule(
3709
    from1 => {
3710
        date => sub { $_[0] * 3 }
3711
    }
3712
);
3713
$row = $result->one;
3714
is($row->{key1}, 6);
3715
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
3716

            
3717
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3718
$result->type_rule(
3719
    from1 => {
3720
        date => sub { $_[0] * 3 }
3721
    }
3722
);
3723
$row = $result->one;
3724
is($row->{key1}, 6);
3725
is($row->{key2}, 2);
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
$result->type_rule(
3728
    from1 => [date => sub { $_[0] * 3 }]
3729
);
3730
$row = $result->one;
3731
is($row->{key1}, 6);
3732
is($row->{key2}, 2);
3733
$dbi->register_filter(fivetimes => sub { $_[0] * 5});
clenup test
Yuki Kimoto authored on 2011-08-06
3734
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3735
$result->type_rule(
3736
    from1 => [date => 'fivetimes']
3737
);
3738
$row = $result->one;
3739
is($row->{key1}, 10);
3740
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
3741
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3742
$result->type_rule(
3743
    from1 => [date => undef]
3744
);
3745
$row = $result->one;
3746
is($row->{key1}, 2);
3747
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
3748

            
test cleanup
Yuki Kimoto authored on 2011-08-10
3749
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3750
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3751
$dbi->type_rule(
3752
    from1 => {
3753
        date => sub { $_[0] * 2 },
3754
    },
3755
);
3756
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
3757
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3758
$result->filter(key1 => sub { $_[0] * 3 });
3759
is($result->one->{key1}, 12);
clenup test
Yuki Kimoto authored on 2011-08-06
3760

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3761
$dbi = DBIx::Custom->connect;
3762
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3763
$dbi->type_rule(
3764
    from1 => {
3765
        date => sub { $_[0] * 2 },
3766
    },
3767
);
3768
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
3769
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3770
$result->filter(key1 => sub { $_[0] * 3 });
3771
is($result->fetch->[0], 12);
clenup test
Yuki Kimoto authored on 2011-08-06
3772

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3773
$dbi = DBIx::Custom->connect;
3774
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3775
$dbi->type_rule(
3776
    into1 => {
3777
        date => sub { $_[0] . 'b' }
3778
    },
3779
    into2 => {
3780
        date => sub { $_[0] . 'c' }
3781
    },
3782
    from1 => {
3783
        date => sub { $_[0] . 'd' }
3784
    },
3785
    from2 => {
3786
        date => sub { $_[0] . 'e' }
3787
    }
3788
);
3789
$dbi->insert({key1 => '1'}, table => 'table1', type_rule_off => 1);
clenup test
Yuki Kimoto authored on 2011-08-06
3790
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3791
is($result->type_rule_off->fetch_first->[0], '1');
clenup test
Yuki Kimoto authored on 2011-08-06
3792
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
3793
is($result->type_rule_on->fetch_first->[0], '1de');
clenup test
Yuki Kimoto authored on 2011-08-06
3794

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3795
$dbi = DBIx::Custom->connect;
3796
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3797
$dbi->type_rule(
3798
    into1 => {
3799
        date => sub { $_[0] . 'b' }
cleanup test
Yuki Kimoto authored on 2011-08-06
3800
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
3801
    into2 => {
3802
        date => sub { $_[0] . 'c' }
cleanup test
Yuki Kimoto authored on 2011-08-06
3803
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
3804
    from1 => {
3805
        date => sub { $_[0] . 'd' }
cleanup test
Yuki Kimoto authored on 2011-08-06
3806
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
3807
    from2 => {
3808
        date => sub { $_[0] . 'e' }
cleanup test
Yuki Kimoto authored on 2011-08-06
3809
    }
3810
);
cleanup test
Yuki Kimoto authored on 2011-08-10
3811
$dbi->insert({key1 => '1'}, table => 'table1', type_rule1_off => 1);
3812
$result = $dbi->select(table => 'table1');
3813
is($result->type_rule1_off->fetch_first->[0], '1ce');
3814
$result = $dbi->select(table => 'table1');
3815
is($result->type_rule1_on->fetch_first->[0], '1cde');
cleanup test
Yuki Kimoto authored on 2011-08-06
3816

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3817
$dbi = DBIx::Custom->connect;
3818
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3819
$dbi->type_rule(
3820
    into1 => {
3821
        date => sub { $_[0] . 'b' }
3822
    },
3823
    into2 => {
3824
        date => sub { $_[0] . 'c' }
3825
    },
3826
    from1 => {
3827
        date => sub { $_[0] . 'd' }
3828
    },
3829
    from2 => {
3830
        date => sub { $_[0] . 'e' }
cleanup test
Yuki Kimoto authored on 2011-08-06
3831
    }
3832
);
cleanup test
Yuki Kimoto authored on 2011-08-10
3833
$dbi->insert({key1 => '1'}, table => 'table1', type_rule2_off => 1);
3834
$result = $dbi->select(table => 'table1');
3835
is($result->type_rule2_off->fetch_first->[0], '1bd');
3836
$result = $dbi->select(table => 'table1');
3837
is($result->type_rule2_on->fetch_first->[0], '1bde');