DBIx-Custom / t / sqlite.t /
Newer Older
3838 lines | 121.215kb
cleanup test
Yuki Kimoto authored on 2011-08-06
1
use Test::More;
2
use strict;
3
use warnings;
4
use utf8;
5
use Encode qw/encode_utf8 decode_utf8/;
test cleanup
Yuki Kimoto authored on 2011-08-06
6
use FindBin;
7
use lib "$FindBin::Bin/basic";
cleanup test
Yuki Kimoto authored on 2011-08-06
8

            
9
BEGIN {
10
    eval { require DBD::SQLite; 1 }
11
        or plan skip_all => 'DBD::SQLite required';
12
    eval { DBD::SQLite->VERSION >= 1.25 }
13
        or plan skip_all => 'DBD::SQLite >= 1.25 required';
14

            
15
    plan 'no_plan';
16
    use_ok('DBIx::Custom');
17
}
18

            
test cleanup
Yuki Kimoto authored on 2011-08-06
19
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/};
cleanup test
Yuki Kimoto authored on 2011-08-06
20
sub test { print "# $_[0]\n" }
21

            
test cleanup
Yuki Kimoto authored on 2011-08-10
22
{
23
    package DBIx::Custom;
24
    has dsn => sub { 'dbi:SQLite:dbname=:memory:' }
25
}
26

            
cleanup test
Yuki Kimoto authored on 2011-08-06
27
# Constant
cleanup test
Yuki Kimoto authored on 2011-08-10
28
my $create_table1 = 'create table table1 (key1 char(255), key2 char(255));';
29
my $create_table1_2 = 'create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));';
test cleanup
Yuki Kimoto authored on 2011-08-10
30
my $q = '"';
31
my $p = '"';
cleanup test
Yuki Kimoto authored on 2011-08-06
32

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

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

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

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

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

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

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

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

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

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

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

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
135
=cut
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('"');
228
$dbi->execute('create table "table" ("select", "update")');
229
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
230
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
231
$dbi->insert(table => 'table', param => {select => 1});
232
$dbi->update(table => 'table', where => {select => 1}, param => {update => 2});
233
$result = $dbi->execute('select * from "table"');
234
$rows   = $result->all;
235
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
236

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

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

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

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

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

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

            
295

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

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

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

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

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

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

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

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

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

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

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

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

            
390

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
541

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

            
547
$dbi->begin_work;
548

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

            
555
$dbi->rollback if $@;
556

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

            
561
$dbi->begin_work;
562

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

            
568
$dbi->commit unless $@;
569

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

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

            
579

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1234

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1428

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

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

            
1437
$dbi->apply_filter(
1438

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1570
{
1571
    package MyDBI4;
1572

            
1573
    use strict;
1574
    use warnings;
1575

            
1576
    use base 'DBIx::Custom';
1577

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

            
1589
    package MyModel2::Base1;
1590

            
1591
    use strict;
1592
    use warnings;
1593

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

            
1596
    package MyModel2::book;
1597

            
1598
    use strict;
1599
    use warnings;
1600

            
1601
    use base 'MyModel2::Base1';
1602

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

            
1609
    sub list { shift->select; }
1610

            
1611
    package MyModel2::Company;
1612

            
1613
    use strict;
1614
    use warnings;
1615

            
1616
    use base 'MyModel2::Base1';
1617

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

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

            
1637
{
1638
     package MyDBI5;
1639

            
1640
    use strict;
1641
    use warnings;
1642

            
1643
    use base 'DBIx::Custom';
1644

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1883

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

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

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

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

            
1955

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

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

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

            
2008

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

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

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

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

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

            
2051

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

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

            
2072

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2405

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

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

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

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

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

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

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

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

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

            
2532

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

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

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

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

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

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

            
2611

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

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

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

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

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

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

            
2680

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

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

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

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

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

            
2749

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

            
2765

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

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

            
2786

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

            
2805

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

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

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

            
2845

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

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

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

            
2881

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

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

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

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

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

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

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

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

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

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

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

            
2976

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

            
2987

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

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

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

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

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

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

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

            
3046

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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