DBIx-Custom / t / sqlite.t /
Newer Older
3886 lines | 122.851kb
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 $create_table2 = 'create table table2 (key1 char(255), key3 char(255));';
31
my $create_table_reserved = 'create table "table" ("select", "update")';
32

            
test cleanup
Yuki Kimoto authored on 2011-08-10
33
my $q = '"';
34
my $p = '"';
cleanup test
Yuki Kimoto authored on 2011-08-06
35

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

            
68
# Prepare table
test cleanup
Yuki Kimoto authored on 2011-08-10
69
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
70

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

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

            
92
$dbi->execute('drop table table1');
93
$dbi->execute($create_table1);
94
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, append => '   ');
95
$rows = $dbi->select(table => 'table1')->all;
96
is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append');
97

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

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

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

            
113
eval { $dbi->execute('drop table table1') };
114
$dbi->execute($create_table1);
115
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
116
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
117
$result = $dbi->execute('select * from table1;');
118
$rows   = $result->all;
119
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
120

            
121
eval { $dbi->execute('drop table table1') };
122
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
123
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
124
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace');
125
$result = $dbi->execute('select * from table1;');
126
$rows   = $result->all;
127
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
128

            
129
eval { $dbi->execute('drop table table1') };
130
$dbi->execute($create_table1);
131
$dbi->insert(table => 'table1', param => {key1 => \"'1'", key2 => 2});
132
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
133
$result = $dbi->execute('select * from table1;');
134
$rows   = $result->all;
135
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
136

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
296

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

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

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

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

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

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

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

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

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

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

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

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

            
392

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

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

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

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

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

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

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

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

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

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

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

            
462
test 'filters';
463
$dbi = DBIx::Custom->new;
464

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

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

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

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

            
490
$result = $dbi->select(table => 'table1');
491
ok(! $result->fetch_first, "rollback");
492

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

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

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

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

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

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

            
543

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

            
549
$dbi->begin_work;
550

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

            
557
$dbi->rollback if $@;
558

            
559
$result = $dbi->select(table => 'table1');
560
$rows = $result->all;
561
is_deeply($rows, [], "rollback");
562

            
563
$dbi->begin_work;
564

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

            
570
$dbi->commit unless $@;
571

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

            
576
$dbi->dbh->{AutoCommit} = 0;
577
eval{ $dbi->begin_work };
578
ok($@, "exception");
579
$dbi->dbh->{AutoCommit} = 1;
580

            
581

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

            
596
is($dbi->one, 1, "first");
597
is($dbi->two, 2, "second");
598
is($dbi->twice(5), 10 , "second");
599

            
600
eval {$dbi->XXXXXX};
601
ok($@, "not exists");
602

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
975
$where = $dbi->where
976
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
977
             ->param({key1 => 1});
978

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

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

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

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

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

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

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

            
1044
$where = $dbi->where;
1045
is("$where", '');
1046

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1222
eval {$dbi->where(ppp => 1) };
1223
like($@, qr/invalid/);
1224

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

            
1236

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1401
test 'dbi_option default';
1402
$dbi = DBIx::Custom->new;
1403
is_deeply($dbi->dbi_option, {});
1404

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

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

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

            
1430

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

            
1436
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
1437
like($@, qr/apply_filter/);
1438

            
1439
$dbi->apply_filter(
1440

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1572
{
1573
    package MyDBI4;
1574

            
1575
    use strict;
1576
    use warnings;
1577

            
1578
    use base 'DBIx::Custom';
1579

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

            
1591
    package MyModel2::Base1;
1592

            
1593
    use strict;
1594
    use warnings;
1595

            
1596
    use base 'DBIx::Custom::Model';
1597

            
1598
    package MyModel2::book;
1599

            
1600
    use strict;
1601
    use warnings;
1602

            
1603
    use base 'MyModel2::Base1';
1604

            
1605
    sub insert {
1606
        my ($self, $param) = @_;
1607
        
1608
        return $self->SUPER::insert(param => $param);
1609
    }
1610

            
1611
    sub list { shift->select; }
1612

            
1613
    package MyModel2::Company;
1614

            
1615
    use strict;
1616
    use warnings;
1617

            
1618
    use base 'MyModel2::Base1';
1619

            
1620
    sub insert {
1621
        my ($self, $param) = @_;
1622
        
1623
        return $self->SUPER::insert(param => $param);
1624
    }
1625

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

            
1639
{
1640
     package MyDBI5;
1641

            
1642
    use strict;
1643
    use warnings;
1644

            
1645
    use base 'DBIx::Custom';
1646

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1885

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

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

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

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

            
1957

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

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

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

            
2010

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

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

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

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

            
2049
            
2050
eval { $dbi->update_param({";" => 1}) };
2051
like($@, qr/not safety/);
2052

            
2053

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

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

            
2074

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

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

            
2101
eval { $dbi->insert_param({";" => 1}) };
2102
like($@, qr/not safety/);
2103

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2407

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

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

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

            
2451
test 'create_model';
test cleanup
Yuki Kimoto authored on 2011-08-10
2452
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2453
eval { $dbi->execute('drop table table1') };
2454
eval { $dbi->execute('drop table table2') };
cleanup test
Yuki Kimoto authored on 2011-08-10
2455
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2456
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-06
2457

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

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

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

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

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

            
2534

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

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

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

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

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

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

            
2613

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

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

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

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

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

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

            
2682

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

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

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

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

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

            
2751

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

            
2767

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

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

            
2788

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

            
2807

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

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

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

            
2847

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

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

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

            
2883

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

            
2885
test 'separator';
test cleanup
Yuki Kimoto authored on 2011-08-10
2886
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2887
eval { $dbi->execute('drop table table1') };
2888
eval { $dbi->execute('drop table table2') };
2889
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2890
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-10
2891

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

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

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

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

            
2944
test 'filter_off';
test cleanup
Yuki Kimoto authored on 2011-08-10
2945
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
2946
eval { $dbi->execute('drop table table1') };
2947
eval { $dbi->execute('drop table table2') };
2948
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
2949
$dbi->execute($create_table2);
cleanup test
Yuki Kimoto authored on 2011-08-10
2950

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

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

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

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

            
2978

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

            
2989

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

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

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

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

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

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

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

            
3048

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
3364
$builder->register_tag({
3365
   s => sub { return ["a", ""]} 
3366
});
3367

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

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

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

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

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

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

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

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

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

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

            
3407
$source = "abc;";
3408
$query = $builder->build_query($source);
3409
is($query->sql, 'abc;', "basic : 2");
3410

            
3411
$source = "{= a}";
3412
$query = $builder->build_query($source);
3413
is($query->sql, 'a = ?;', "only tag");
3414

            
3415
$source = "000;";
3416
$query = $builder->build_query($source);
3417
is($query->sql, '000;', "contain 0 value");
3418

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

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

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

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

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

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

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

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

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3535
test 'type_rule and filter order';
test cleanup
Yuki Kimoto authored on 2011-08-10
3536
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
3537
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3538
$dbi->type_rule(
3539
    into1 => {
3540
        date => sub { $_[0] . 'b' }
3541
    },
3542
    into2 => {
3543
        date => sub { $_[0] . 'c' }
3544
    },
3545
    from1 => {
3546
        date => sub { $_[0] . 'd' }
3547
    },
3548
    from2 => {
3549
        date => sub { $_[0] . 'e' }
3550
    }
3551
);
3552
$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }});
3553
$result = $dbi->select(table => 'table1');
3554
$result->filter(key1 => sub { $_[0] . 'f' });
3555
is($result->fetch_first->[0], '1abcdef');
cleanup test
Yuki Kimoto authored on 2011-08-06
3556

            
cleanup test
Yuki Kimoto authored on 2011-08-10
3557
$dbi = DBIx::Custom->connect;
3558
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3559
$dbi->type_rule(
3560
    from1 => {
3561
        date => sub { $_[0] . 'p' }
3562
    },
3563
    from2 => {
3564
        date => sub { $_[0] . 'q' }
3565
    },
3566
);
3567
$dbi->insert({key1 => '1'}, table => 'table1');
3568
$result = $dbi->select(table => 'table1');
3569
$result->type_rule(
3570
    from1 => {
3571
        date => sub { $_[0] . 'd' }
3572
    },
3573
    from2 => {
3574
        date => sub { $_[0] . 'e' }
3575
    }
3576
);
3577
$result->filter(key1 => sub { $_[0] . 'f' });
3578
is($result->fetch_first->[0], '1def');
cleanup test
Yuki Kimoto authored on 2011-08-06
3579

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
3842
test 'prefix';
3843
$dbi = DBIx::Custom->connect;
3844
eval { $dbi->execute('drop table table1') };
3845
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
3846
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3847
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace');
3848
$result = $dbi->execute('select * from table1;');
3849
$rows   = $result->all;
3850
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
3851

            
3852
$dbi = DBIx::Custom->connect;
3853
eval { $dbi->execute('drop table table1') };
3854
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
3855
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3856
$dbi->update(table => 'table1', param => {key2 => 4},
3857
  where => {key1 => 1}, prefix => 'or replace');
3858
$result = $dbi->execute('select * from table1;');
3859
$rows   = $result->all;
3860
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
3861

            
3862

            
3863
test 'reserved_word_quote';
3864
$dbi = DBIx::Custom->connect;
3865
eval { $dbi->execute("drop table ${q}table$p") };
3866
$dbi->reserved_word_quote('"');
3867
$dbi->execute($create_table_reserved);
3868
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
3869
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
3870
$dbi->insert(table => 'table', param => {select => 1});
3871
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
3872
$result = $dbi->execute("select * from ${q}table$p");
3873
$rows   = $result->all;
3874
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
3875

            
3876
test 'quote';
3877
$dbi = DBIx::Custom->connect;
3878
$dbi->quote('"');
3879
eval { $dbi->execute("drop table ${q}table$p") };
3880
$dbi->execute($create_table_reserved);
3881
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
3882
$dbi->insert(table => 'table', param => {select => 1});
3883
$dbi->delete(table => 'table', where => {select => 1});
3884
$result = $dbi->execute("select * from ${q}table$p");
3885
$rows   = $result->all;
3886
is_deeply($rows, [], "reserved word");