DBIx-Custom / t / basic.t /
Newer Older
3515 lines | 122.423kb
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

            
22
# Constant varialbes for test
23
my $CREATE_TABLE = {
24
    3 => 'create table table1 (key1 Date, key2 datetime);',
25
    4 => 'create table table3 (key3 int, key4 int);'
26
};
27

            
28
# Variables
29
my $dbi;
30
my $sth;
31
my $source;
32
my @sources;
33
my $select_SOURCE;
34
my $insert_SOURCE;
35
my $update_SOURCE;
36
my $param;
37
my $params;
38
my $sql;
39
my $result;
40
my $row;
41
my @rows;
42
my $rows;
43
my $query;
44
my @queries;
45
my $select_query;
46
my $insert_query;
47
my $update_query;
48
my $ret_val;
49
my $infos;
50
my $model;
51
my $model2;
52
my $where;
53
my $update_param;
54
my $insert_param;
55
my $join;
56

            
57
# Prepare table
cleanup test
Yuki Kimoto authored on 2011-08-06
58
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
59
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
60
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
61
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
62

            
63
test 'DBIx::Custom::Result test';
64
$source = "select key1, key2 from table1";
65
$query = $dbi->create_query($source);
66
$result = $dbi->execute($query);
67

            
68
@rows = ();
69
while (my $row = $result->fetch) {
70
    push @rows, [@$row];
71
}
72
is_deeply(\@rows, [[1, 2], [3, 4]], "fetch");
73

            
74
$result = $dbi->execute($query);
75
@rows = ();
76
while (my $row = $result->fetch_hash) {
77
    push @rows, {%$row};
78
}
79
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "fetch_hash");
80

            
81
$result = $dbi->execute($query);
82
$rows = $result->fetch_all;
83
is_deeply($rows, [[1, 2], [3, 4]], "fetch_all");
84

            
85
$result = $dbi->execute($query);
86
$rows = $result->fetch_hash_all;
87
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "all");
88

            
89
test 'Insert query return value';
cleanup test
Yuki Kimoto authored on 2011-08-06
90
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
91
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
92
$source = "insert into table1 {insert_param key1 key2}";
93
$query = $dbi->execute($source, {}, query => 1);
94
$ret_val = $dbi->execute($query, param => {key1 => 1, key2 => 2});
95
ok($ret_val);
96

            
97

            
98
test 'Direct query';
cleanup test
Yuki Kimoto authored on 2011-08-06
99
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
100
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
101
$insert_SOURCE = "insert into table1 {insert_param key1 key2}";
102
$dbi->execute($insert_SOURCE, param => {key1 => 1, key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-06
103
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
104
$rows = $result->all;
105
is_deeply($rows, [{key1 => 1, key2 => 2}]);
106

            
107
test 'Filter basic';
cleanup test
Yuki Kimoto authored on 2011-08-06
108
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
109
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
110
$dbi->register_filter(twice       => sub { $_[0] * 2}, 
111
                    three_times => sub { $_[0] * 3});
112

            
113
$insert_SOURCE  = "insert into table1 {insert_param key1 key2};";
114
$insert_query = $dbi->execute($insert_SOURCE, {}, query => 1);
115
$insert_query->filter({key1 => 'twice'});
116
$dbi->execute($insert_query, param => {key1 => 1, key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-06
117
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
118
$rows = $result->filter({key2 => 'three_times'})->all;
119
is_deeply($rows, [{key1 => 2, key2 => 6}], "filter fetch_filter");
cleanup test
Yuki Kimoto authored on 2011-08-06
120
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
121

            
122
test 'Filter in';
cleanup test
Yuki Kimoto authored on 2011-08-06
123
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
124
$insert_SOURCE  = "insert into table1 {insert_param key1 key2};";
125
$insert_query = $dbi->execute($insert_SOURCE, {}, query => 1);
126
$dbi->execute($insert_query, param => {key1 => 2, key2 => 4});
127
$select_SOURCE = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}";
128
$select_query = $dbi->execute($select_SOURCE,{}, query => 1);
129
$select_query->filter({'table1.key1' => 'twice'});
130
$result = $dbi->execute($select_query, param => {'table1.key1' => [1,5], 'table1.key2' => [2,4]});
131
$rows = $result->all;
132
is_deeply($rows, [{key1 => 2, key2 => 4}], "filter");
133

            
134
test 'DBIx::Custom::SQLTemplate basic tag';
cleanup test
Yuki Kimoto authored on 2011-08-06
135
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
136
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
137
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
138
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
139

            
140
$source = "select * from table1 where key1 = :key1 and {<> key2} and {< key3} and {> key4} and {>= key5};";
141
$query = $dbi->execute($source, {}, query => 1);
142
$result = $dbi->execute($query, param => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
143
$rows = $result->all;
144
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1");
145

            
146
$source = "select * from table1 where key1 = :key1 and {<> key2} and {< key3} and {> key4} and {>= key5};";
147
$query = $dbi->execute($source, {}, query => 1);
148
$result = $dbi->execute($query, {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
149
$rows = $result->all;
150
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1");
151

            
152
$source = "select * from table1 where {<= key1} and {like key2};";
153
$query = $dbi->execute($source, {}, query => 1);
154
$result = $dbi->execute($query, param => {key1 => 1, key2 => '%2%'});
155
$rows = $result->all;
156
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag2");
157

            
158
test 'DIB::Custom::SQLTemplate in tag';
cleanup test
Yuki Kimoto authored on 2011-08-06
159
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
160
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
161
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
162
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
163

            
164
$source = "select * from table1 where {in key1 2};";
165
$query = $dbi->execute($source, {}, query => 1);
166
$result = $dbi->execute($query, param => {key1 => [9, 1]});
167
$rows = $result->all;
168
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic");
169

            
170
test 'DBIx::Custom::SQLTemplate insert tag';
171
$dbi->execute("delete from table1");
172
$insert_SOURCE = 'insert into table1 {insert_param key1 key2 key3 key4 key5}';
173
$dbi->execute($insert_SOURCE, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
174

            
test cleanup
Yuki Kimoto authored on 2011-08-06
175
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
176
$rows = $result->all;
177
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic");
178

            
179
test 'DBIx::Custom::SQLTemplate update tag';
180
$dbi->execute("delete from table1");
181
$insert_SOURCE = "insert into table1 {insert_param key1 key2 key3 key4 key5}";
182
$dbi->execute($insert_SOURCE, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
183
$dbi->execute($insert_SOURCE, param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
184

            
185
$update_SOURCE = 'update table1 {update_param key1 key2 key3 key4} where {= key5}';
186
$dbi->execute($update_SOURCE, param => {key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5});
187

            
test cleanup
Yuki Kimoto authored on 2011-08-06
188
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
189
$rows = $result->all;
190
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5},
191
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "basic");
192

            
193

            
194
test 'Named placeholder';
cleanup test
Yuki Kimoto authored on 2011-08-06
195
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
196
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
197
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
198
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
199

            
200
$source = "select * from table1 where key1 = :key1 and key2 = :key2";
201
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
202
$rows = $result->all;
203
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
204

            
205
$source = "select * from table1 where key1 = \n:key1\n and key2 = :key2";
206
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
207
$rows = $result->all;
208
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
209

            
210
$source = "select * from table1 where key1 = :key1 or key1 = :key1";
211
$result = $dbi->execute($source, param => {key1 => [1, 2]});
212
$rows = $result->all;
213
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
214

            
215
$source = "select * from table1 where key1 = :table1.key1 and key2 = :table1.key2";
216
$result = $dbi->execute(
217
    $source,
218
    param => {'table1.key1' => 1, 'table1.key2' => 1},
219
    filter => {'table1.key2' => sub { $_[0] * 2 }}
220
);
221
$rows = $result->all;
222
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
223

            
cleanup test
Yuki Kimoto authored on 2011-08-06
224
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
225
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
226
$dbi->insert(table => 'table1', param => {key1 => '2011-10-14 12:19:18', key2 => 2});
227
$source = "select * from table1 where key1 = '2011-10-14 12:19:18' and key2 = :key2";
228
$result = $dbi->execute(
229
    $source,
230
    param => {'key2' => 2},
231
);
232

            
233
$rows = $result->all;
234
is_deeply($rows, [{key1 => '2011-10-14 12:19:18', key2 => 2}]);
235

            
cleanup test
Yuki Kimoto authored on 2011-08-06
236
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
237
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
238
$dbi->insert(table => 'table1', param => {key1 => 'a:b c:d', key2 => 2});
239
$source = "select * from table1 where key1 = 'a\\:b c\\:d' and key2 = :key2";
240
$result = $dbi->execute(
241
    $source,
242
    param => {'key2' => 2},
243
);
244
$rows = $result->all;
245
is_deeply($rows, [{key1 => 'a:b c:d', key2 => 2}]);
246

            
247

            
248
test 'Error case';
249
eval {DBIx::Custom->connect(dsn => 'dbi:SQLit')};
250
ok($@, "connect error");
251

            
cleanup test
Yuki Kimoto authored on 2011-08-06
252
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
253
eval{$dbi->execute("{p }", {}, query => 1)};
254
ok($@, "create_query invalid SQL template");
255

            
256
test 'insert';
cleanup test
Yuki Kimoto authored on 2011-08-06
257
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
258
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
259
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
260
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-06
261
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
262
$rows   = $result->all;
263
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
264

            
265
$dbi->execute('delete from table1');
266
$dbi->register_filter(
267
    twice       => sub { $_[0] * 2 },
268
    three_times => sub { $_[0] * 3 }
269
);
270
$dbi->default_bind_filter('twice');
271
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => 'three_times'});
test cleanup
Yuki Kimoto authored on 2011-08-06
272
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
273
$rows   = $result->all;
274
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter");
275
$dbi->default_bind_filter(undef);
276

            
cleanup test
Yuki Kimoto authored on 2011-08-06
277
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
278
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
279
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, append => '   ');
280
$rows = $dbi->select(table => 'table1')->all;
281
is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append');
282

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

            
286
eval{$dbi->insert(table => 'table', param => {';' => 1})};
287
like($@, qr/safety/);
288

            
cleanup test
Yuki Kimoto authored on 2011-08-06
289
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
290
$dbi->quote('"');
291
$dbi->execute('create table "table" ("select")');
292
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
293
$dbi->insert(table => 'table', param => {select => 1});
294
$result = $dbi->execute('select * from "table"');
295
$rows   = $result->all;
296
is_deeply($rows, [{select => 2}], "reserved word");
297

            
cleanup test
Yuki Kimoto authored on 2011-08-06
298
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
299
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
300
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
301
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
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 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
305

            
cleanup test
Yuki Kimoto authored on 2011-08-06
306
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
307
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
308
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
309
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace');
test cleanup
Yuki Kimoto authored on 2011-08-06
310
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
311
$rows   = $result->all;
312
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
313

            
cleanup test
Yuki Kimoto authored on 2011-08-06
314
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
315
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
316
$dbi->insert(table => 'table1', param => {key1 => \"'1'", key2 => 2});
317
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-06
318
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
319
$rows   = $result->all;
320
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
321

            
322
test 'update';
cleanup test
Yuki Kimoto authored on 2011-08-06
323
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
324
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
325
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
326
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
327
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
328
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
329
$rows   = $result->all;
330
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
331
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
332
                  "basic");
333
                  
334
$dbi->execute("delete from table1");
335
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
336
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
337
$dbi->update(table => 'table1', param => {key2 => 12}, where => {key2 => 2, key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-06
338
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
339
$rows   = $result->all;
340
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
341
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
342
                  "update key same as search key");
343

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

            
351
$dbi->execute("delete from table1");
352
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
353
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
354
$dbi->register_filter(twice => sub { $_[0] * 2 });
355
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1},
356
              filter => {key2 => sub { $_[0] * 2 }});
test cleanup
Yuki Kimoto authored on 2011-08-06
357
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
358
$rows   = $result->all;
359
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5},
360
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
361
                  "filter");
362

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
371
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
372
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
373
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
374
$where = $dbi->where;
375
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
376
$where->param({key1 => 1, key2 => 2});
377
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
378
$result = $dbi->select(table => 'table1');
379
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
380

            
cleanup test
Yuki Kimoto authored on 2011-08-06
381
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
382
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
383
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
384
$dbi->update(
385
    table => 'table1',
386
    param => {key1 => 3},
387
    where => [
388
        ['and', 'key1 = :key1', 'key2 = :key2'],
389
        {key1 => 1, key2 => 2}
390
    ]
391
);
392
$result = $dbi->select(table => 'table1');
393
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
394

            
cleanup test
Yuki Kimoto authored on 2011-08-06
395
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
396
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
397
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
398
$where = $dbi->where;
399
$where->clause(['and', 'key2 = :key2']);
400
$where->param({key2 => 2});
401
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
402
$result = $dbi->select(table => 'table1');
403
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
404

            
405
eval{$dbi->update(table => 'table1', param => {';' => 1})};
406
like($@, qr/safety/);
407

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
411
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
412
$dbi->quote('"');
413
$dbi->execute('create table "table" ("select", "update")');
414
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
415
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
416
$dbi->insert(table => 'table', param => {select => 1});
417
$dbi->update(table => 'table', where => {select => 1}, param => {update => 2});
418
$result = $dbi->execute('select * from "table"');
419
$rows   = $result->all;
420
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
421

            
422
eval {$dbi->update_all(table => 'table', param => {';' => 2}) };
423
like($@, qr/safety/);
424

            
cleanup test
Yuki Kimoto authored on 2011-08-06
425
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
426
$dbi->reserved_word_quote('"');
427
$dbi->execute('create table "table" ("select", "update")');
428
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
429
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
430
$dbi->insert(table => 'table', param => {select => 1});
431
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
432
$result = $dbi->execute('select * from "table"');
433
$rows   = $result->all;
434
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
435

            
cleanup test
Yuki Kimoto authored on 2011-08-06
436
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
437
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
438
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
439
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
440
$dbi->update({key2 => 11}, table => 'table1', where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
441
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
442
$rows   = $result->all;
443
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
444
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
445
                  "basic");
446

            
cleanup test
Yuki Kimoto authored on 2011-08-06
447
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
448
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
449
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
450
$dbi->update(table => 'table1', param => {key2 => 4},
451
  where => {key1 => 1}, prefix => 'or replace');
test cleanup
Yuki Kimoto authored on 2011-08-06
452
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
453
$rows   = $result->all;
454
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
455

            
cleanup test
Yuki Kimoto authored on 2011-08-06
456
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
457
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
458
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
459
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
460
$dbi->update(table => 'table1', param => {key2 => \"'11'"}, where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
461
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
462
$rows   = $result->all;
463
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
464
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
465
                  "basic");
466

            
467
test 'update_all';
cleanup test
Yuki Kimoto authored on 2011-08-06
468
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
469
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
470
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
471
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
472
$dbi->register_filter(twice => sub { $_[0] * 2 });
473
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'});
test cleanup
Yuki Kimoto authored on 2011-08-06
474
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
475
$rows   = $result->all;
476
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
477
                  {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
478
                  "filter");
479

            
480

            
481
test 'delete';
cleanup test
Yuki Kimoto authored on 2011-08-06
482
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
483
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
484
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
485
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
486
$dbi->delete(table => 'table1', where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
487
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
488
$rows   = $result->all;
489
is_deeply($rows, [{key1 => 3, key2 => 4}], "basic");
490

            
491
$dbi->execute("delete from table1;");
492
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
493
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
494
$dbi->register_filter(twice => sub { $_[0] * 2 });
495
$dbi->delete(table => 'table1', where => {key2 => 1}, filter => {key2 => 'twice'});
test cleanup
Yuki Kimoto authored on 2011-08-06
496
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
497
$rows   = $result->all;
498
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter");
499

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

            
502
$dbi->delete_all(table => 'table1');
503
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
504
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
505
$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2});
506
$rows = $dbi->select(table => 'table1')->all;
507
is_deeply($rows, [{key1 => 3, key2 => 4}], "delete multi key");
508

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
512
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
513
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
514
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
515
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
516
$where = $dbi->where;
517
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
518
$where->param({ke1 => 1, key2 => 2});
519
$dbi->delete(table => 'table1', where => $where);
520
$result = $dbi->select(table => 'table1');
521
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
522

            
cleanup test
Yuki Kimoto authored on 2011-08-06
523
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
524
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
525
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
526
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
527
$dbi->delete(
528
    table => 'table1',
529
    where => [
530
        ['and', 'key1 = :key1', 'key2 = :key2'],
531
        {ke1 => 1, key2 => 2}
532
    ]
533
);
534
$result = $dbi->select(table => 'table1');
535
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
536

            
cleanup test
Yuki Kimoto authored on 2011-08-06
537
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
538
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
539
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
540
$dbi->delete(table => 'table1', where => {key1 => 1}, prefix => '    ');
test cleanup
Yuki Kimoto authored on 2011-08-06
541
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
542
$rows   = $result->all;
543
is_deeply($rows, [], "basic");
544

            
545
test 'delete error';
cleanup test
Yuki Kimoto authored on 2011-08-06
546
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
547
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
548
eval{$dbi->delete(table => 'table1')};
549
like($@, qr/"where" must be specified/,
550
         "where key-value pairs not specified");
551

            
552
eval{$dbi->delete(table => 'table1', where => {';' => 1})};
553
like($@, qr/safety/);
554

            
cleanup test
Yuki Kimoto authored on 2011-08-06
555
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
556
$dbi->quote('"');
557
$dbi->execute('create table "table" ("select", "update")');
558
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
559
$dbi->insert(table => 'table', param => {select => 1});
560
$dbi->delete(table => 'table', where => {select => 1});
561
$result = $dbi->execute('select * from "table"');
562
$rows   = $result->all;
563
is_deeply($rows, [], "reserved word");
564

            
565
test 'delete_all';
cleanup test
Yuki Kimoto authored on 2011-08-06
566
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
567
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
568
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
569
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
570
$dbi->delete_all(table => 'table1');
test cleanup
Yuki Kimoto authored on 2011-08-06
571
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
572
$rows   = $result->all;
573
is_deeply($rows, [], "basic");
574

            
575

            
576
test 'select';
cleanup test
Yuki Kimoto authored on 2011-08-06
577
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
578
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
579
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
580
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
581
$rows = $dbi->select(table => 'table1')->all;
582
is_deeply($rows, [{key1 => 1, key2 => 2},
583
                  {key1 => 3, key2 => 4}], "table");
584

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

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

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

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

            
597
$dbi->register_filter(decrement => sub { $_[0] - 1 });
598
$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'})
599
            ->all;
600
is_deeply($rows, [{key1 => 1, key2 => 2}], "filter");
601

            
cleanup test
Yuki Kimoto authored on 2011-08-06
602
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
603
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
604
$rows = $dbi->select(
605
    table => [qw/table1 table2/],
606
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
607
    where   => {'table1.key2' => 2},
608
    relation  => {'table1.key1' => 'table2.key1'}
609
)->all;
610
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : exists where");
611

            
612
$rows = $dbi->select(
613
    table => [qw/table1 table2/],
614
    column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
615
    relation  => {'table1.key1' => 'table2.key1'}
616
)->all;
617
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : no exists where");
618

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
622
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
623
$dbi->quote('"');
624
$dbi->execute('create table "table" ("select", "update")');
625
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
626
$dbi->insert(table => 'table', param => {select => 1, update => 2});
627
$result = $dbi->select(table => 'table', where => {select => 1});
628
$rows   = $result->all;
629
is_deeply($rows, [{select => 2, update => 2}], "reserved word");
630

            
631
test 'fetch filter';
cleanup test
Yuki Kimoto authored on 2011-08-06
632
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
633
$dbi->register_filter(
634
    twice       => sub { $_[0] * 2 },
635
    three_times => sub { $_[0] * 3 }
636
);
637
$dbi->default_fetch_filter('twice');
cleanup test
Yuki Kimoto authored on 2011-08-06
638
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
639
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
640
$result = $dbi->select(table => 'table1');
641
$result->filter({key1 => 'three_times'});
642
$row = $result->one;
643
is_deeply($row, {key1 => 3, key2 => 4}, "default_fetch_filter and filter");
644

            
645
test 'filters';
646
$dbi = DBIx::Custom->new;
647

            
648
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')),
649
   'あ', "decode_utf8");
650

            
651
is($dbi->filters->{encode_utf8}->('あ'),
652
   encode_utf8('あ'), "encode_utf8");
653

            
654
test 'transaction';
cleanup test
Yuki Kimoto authored on 2011-08-06
655
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
656
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
657
$dbi->dbh->begin_work;
658
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
659
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
660
$dbi->dbh->commit;
661
$result = $dbi->select(table => 'table1');
662
is_deeply(scalar $result->all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}],
663
          "commit");
664

            
cleanup test
Yuki Kimoto authored on 2011-08-06
665
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
666
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
667
$dbi->dbh->begin_work(0);
668
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
669
$dbi->dbh->rollback;
670

            
671
$result = $dbi->select(table => 'table1');
672
ok(! $result->fetch_first, "rollback");
673

            
674
test 'cache';
cleanup test
Yuki Kimoto authored on 2011-08-06
675
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
676
$dbi->cache(1);
cleanup test
Yuki Kimoto authored on 2011-08-06
677
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
678
$source = 'select * from table1 where key1 = :key1 and key2 = :key2;';
679
$dbi->execute($source, {}, query => 1);
680
is_deeply($dbi->{_cached}->{$source}, 
681
          {sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2'], tables => []}, "cache");
682

            
cleanup test
Yuki Kimoto authored on 2011-08-06
683
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
684
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
685
$dbi->{_cached} = {};
686
$dbi->cache(0);
687
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
688
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
689

            
690
test 'execute';
cleanup test
Yuki Kimoto authored on 2011-08-06
691
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
692
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
693
{
694
    local $Carp::Verbose = 0;
695
    eval{$dbi->execute('select * frm table1')};
696
    like($@, qr/\Qselect * frm table1;/, "fail prepare");
697
    like($@, qr/\.t /, "fail : not verbose");
698
}
699
{
700
    local $Carp::Verbose = 1;
701
    eval{$dbi->execute('select * frm table1')};
702
    like($@, qr/Custom.*\.t /s, "fail : verbose");
703
}
704

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

            
708
$query = $dbi->execute('select * from table1 where key1 = :key1', {}, query => 1);
709
$dbi->dbh->disconnect;
710
eval{$dbi->execute($query, param => {key1 => {a => 1}})};
711
ok($@, "execute fail");
712

            
713
{
714
    local $Carp::Verbose = 0;
715
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
716
    like($@, qr/\Q.t /, "caller spec : not vebose");
717
}
718
{
719
    local $Carp::Verbose = 1;
720
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
721
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
722
}
723

            
724

            
725
test 'transaction';
cleanup test
Yuki Kimoto authored on 2011-08-06
726
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
727
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
728

            
729
$dbi->begin_work;
730

            
731
eval {
732
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
733
    die "Error";
734
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
735
};
736

            
737
$dbi->rollback if $@;
738

            
739
$result = $dbi->select(table => 'table1');
740
$rows = $result->all;
741
is_deeply($rows, [], "rollback");
742

            
743
$dbi->begin_work;
744

            
745
eval {
746
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
747
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
748
};
749

            
750
$dbi->commit unless $@;
751

            
752
$result = $dbi->select(table => 'table1');
753
$rows = $result->all;
754
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "commit");
755

            
756
$dbi->dbh->{AutoCommit} = 0;
757
eval{ $dbi->begin_work };
758
ok($@, "exception");
759
$dbi->dbh->{AutoCommit} = 1;
760

            
761

            
762
test 'method';
cleanup test
Yuki Kimoto authored on 2011-08-06
763
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
764
$dbi->method(
765
    one => sub { 1 }
766
);
767
$dbi->method(
768
    two => sub { 2 }
769
);
770
$dbi->method({
771
    twice => sub {
772
        my $self = shift;
773
        return $_[0] * 2;
774
    }
775
});
776

            
777
is($dbi->one, 1, "first");
778
is($dbi->two, 2, "second");
779
is($dbi->twice(5), 10 , "second");
780

            
781
eval {$dbi->XXXXXX};
782
ok($@, "not exists");
783

            
784
test 'out filter';
cleanup test
Yuki Kimoto authored on 2011-08-06
785
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
786
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
787
$dbi->register_filter(twice => sub { $_[0] * 2 });
788
$dbi->register_filter(three_times => sub { $_[0] * 3});
789
$dbi->apply_filter(
790
    'table1', 'key1' => {out => 'twice', in => 'three_times'}, 
791
              'key2' => {out => 'three_times', in => 'twice'});
792
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-06
793
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
794
$row   = $result->fetch_hash_first;
795
is_deeply($row, {key1 => 2, key2 => 6}, "insert");
796
$result = $dbi->select(table => 'table1');
797
$row   = $result->one;
798
is_deeply($row, {key1 => 6, key2 => 12}, "insert");
799

            
cleanup test
Yuki Kimoto authored on 2011-08-06
800
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
801
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
802
$dbi->register_filter(twice => sub { $_[0] * 2 });
803
$dbi->register_filter(three_times => sub { $_[0] * 3});
804
$dbi->apply_filter(
805
    'table1', 'key1' => {out => 'twice', in => 'three_times'}, 
806
              'key2' => {out => 'three_times', in => 'twice'});
807
$dbi->apply_filter(
808
    'table1', 'key1' => {out => undef}
809
); 
810
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-06
811
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
812
$row   = $result->one;
813
is_deeply($row, {key1 => 1, key2 => 6}, "insert");
814

            
cleanup test
Yuki Kimoto authored on 2011-08-06
815
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
816
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
817
$dbi->register_filter(twice => sub { $_[0] * 2 });
818
$dbi->apply_filter(
819
    'table1', 'key1' => {out => 'twice', in => 'twice'}
820
);
821
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => undef});
822
$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-06
823
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
824
$row   = $result->one;
825
is_deeply($row, {key1 => 4, key2 => 2}, "update");
826

            
cleanup test
Yuki Kimoto authored on 2011-08-06
827
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
828
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
829
$dbi->register_filter(twice => sub { $_[0] * 2 });
830
$dbi->apply_filter(
831
    'table1', 'key1' => {out => 'twice', in => 'twice'}
832
);
833
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1=> undef});
834
$dbi->delete(table => 'table1', where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
835
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
836
$rows   = $result->all;
837
is_deeply($rows, [], "delete");
838

            
cleanup test
Yuki Kimoto authored on 2011-08-06
839
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
840
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
841
$dbi->register_filter(twice => sub { $_[0] * 2 });
842
$dbi->apply_filter(
843
    'table1', 'key1' => {out => 'twice', in => 'twice'}
844
);
845
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
846
$result = $dbi->select(table => 'table1', where => {key1 => 1});
847
$result->filter({'key2' => 'twice'});
848
$rows   = $result->all;
849
is_deeply($rows, [{key1 => 4, key2 => 4}], "select");
850

            
cleanup test
Yuki Kimoto authored on 2011-08-06
851
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
852
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
853
$dbi->register_filter(twice => sub { $_[0] * 2 });
854
$dbi->apply_filter(
855
    'table1', 'key1' => {out => 'twice', in => 'twice'}
856
);
857
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
858
$result = $dbi->execute("select * from table1 where key1 = :key1 and key2 = :key2;",
859
                        param => {key1 => 1, key2 => 2},
860
                        table => ['table1']);
861
$rows   = $result->all;
862
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute");
863

            
cleanup test
Yuki Kimoto authored on 2011-08-06
864
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
865
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
866
$dbi->register_filter(twice => sub { $_[0] * 2 });
867
$dbi->apply_filter(
868
    'table1', 'key1' => {out => 'twice', in => 'twice'}
869
);
870
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
871
$result = $dbi->execute("select * from {table table1} where key1 = :key1 and key2 = :key2;",
872
                        param => {key1 => 1, key2 => 2});
873
$rows   = $result->all;
874
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute table tag");
875

            
cleanup test
Yuki Kimoto authored on 2011-08-06
876
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
877
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
878
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
879
$dbi->register_filter(twice => sub { $_[0] * 2 });
880
$dbi->register_filter(three_times => sub { $_[0] * 3 });
881
$dbi->apply_filter(
882
    'table1', 'key2' => {out => 'twice', in => 'twice'}
883
);
884
$dbi->apply_filter(
885
    'table2', 'key3' => {out => 'three_times', in => 'three_times'}
886
);
887
$dbi->insert(table => 'table1', param => {key1 => 5, key2 => 2}, filter => {key2 => undef});
888
$dbi->insert(table => 'table2', param => {key1 => 5, key3 => 6}, filter => {key3 => undef});
889
$result = $dbi->select(
890
     table => ['table1', 'table2'],
891
     column => ['key2', 'key3'],
892
     where => {'table1.key2' => 1, 'table2.key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
893

            
894
$result->filter({'key2' => 'twice'});
895
$rows   = $result->all;
896
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join");
897

            
898
$result = $dbi->select(
899
     table => ['table1', 'table2'],
900
     column => ['key2', 'key3'],
901
     where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
902

            
903
$result->filter({'key2' => 'twice'});
904
$rows   = $result->all;
905
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join : omit");
906

            
907
test 'each_column';
cleanup test
Yuki Kimoto authored on 2011-08-06
908
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
909
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
910
$dbi->execute($CREATE_TABLE->{3});
911

            
912
$infos = [];
913
$dbi->each_column(sub {
914
    my ($self, $table, $column, $cinfo) = @_;
915
    
916
    if ($table =~ /^table/) {
917
         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
918
         push @$infos, $info;
919
    }
920
});
921
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
922
is_deeply($infos, 
923
    [
924
        ['table1', 'key1', 'key1'],
925
        ['table1', 'key2', 'key2'],
926
        ['table2', 'key1', 'key1'],
927
        ['table2', 'key3', 'key3']
928
    ]
929
    
930
);
931
test 'each_table';
cleanup test
Yuki Kimoto authored on 2011-08-06
932
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
933
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
934
$dbi->execute($CREATE_TABLE->{3});
935

            
936
$infos = [];
937
$dbi->each_table(sub {
938
    my ($self, $table, $table_info) = @_;
939
    
940
    if ($table =~ /^table/) {
941
         my $info = [$table, $table_info->{TABLE_NAME}];
942
         push @$infos, $info;
943
    }
944
});
945
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
946
is_deeply($infos, 
947
    [
948
        ['table1', 'table1'],
949
        ['table2', 'table2'],
950
    ]
951
);
952

            
953
test 'limit';
cleanup test
Yuki Kimoto authored on 2011-08-06
954
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
955
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
956
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
957
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
958
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
959
$dbi->register_tag(
960
    limit => sub {
961
        my ($count, $offset) = @_;
962
        
963
        my $s = '';
964
        $s .= "limit $count";
965
        $s .= " offset $offset" if defined $offset;
966
        
967
        return [$s, []];
968
    }
969
);
970
$rows = $dbi->select(
971
  table => 'table1',
972
  where => {key1 => 1},
973
  append => "order by key2 {limit 1 0}"
974
)->all;
975
is_deeply($rows, [{key1 => 1, key2 => 2}]);
976
$rows = $dbi->select(
977
  table => 'table1',
978
  where => {key1 => 1},
979
  append => "order by key2 {limit 2 1}"
980
)->all;
981
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
982
$rows = $dbi->select(
983
  table => 'table1',
984
  where => {key1 => 1},
985
  append => "order by key2 {limit 1}"
986
)->all;
987
is_deeply($rows, [{key1 => 1, key2 => 2}]);
988

            
989
test 'connect super';
990
{
991
    package MyDBI;
992
    
993
    use base 'DBIx::Custom';
994
    sub connect {
995
        my $self = shift->SUPER::connect(@_);
996
        
997
        return $self;
998
    }
999
    
1000
    sub new {
1001
        my $self = shift->SUPER::new(@_);
1002
        
1003
        return $self;
1004
    }
1005
}
1006

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1007
$dbi = MyDBI->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1008
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1009
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1010
is($dbi->select(table => 'table1')->one->{key1}, 1);
1011

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1012
$dbi = MyDBI->new(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1013
$dbi->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
1014
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1015
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1016
is($dbi->select(table => 'table1')->one->{key1}, 1);
1017

            
1018
{
1019
    package MyDBI2;
1020
    
1021
    use base 'DBIx::Custom';
1022
    sub connect {
1023
        my $self = shift->SUPER::new(@_);
1024
        $self->connect;
1025
        
1026
        return $self;
1027
    }
1028
}
1029

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1030
$dbi = MyDBI->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1031
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1032
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1033
is($dbi->select(table => 'table1')->one->{key1}, 1);
1034

            
1035
test 'end_filter';
cleanup test
Yuki Kimoto authored on 2011-08-06
1036
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1037
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1038
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1039
$result = $dbi->select(table => 'table1');
1040
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1041
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 });
1042
$row = $result->fetch_first;
1043
is_deeply($row, [6, 40]);
1044

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1045
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1046
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1047
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1048
$result = $dbi->select(table => 'table1');
1049
$result->filter([qw/key1 key2/] => sub { $_[0] * 2 });
1050
$result->end_filter([[qw/key1 key2/] => sub { $_[0] * 3 }]);
1051
$row = $result->fetch_first;
1052
is_deeply($row, [6, 12]);
1053

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1054
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1055
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1056
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1057
$result = $dbi->select(table => 'table1');
1058
$result->filter([[qw/key1 key2/] => sub { $_[0] * 2 }]);
1059
$result->end_filter([qw/key1 key2/] => sub { $_[0] * 3 });
1060
$row = $result->fetch_first;
1061
is_deeply($row, [6, 12]);
1062

            
1063
$dbi->register_filter(five_times => sub { $_[0] * 5 });
1064
$result = $dbi->select(table => 'table1');
1065
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1066
$result->end_filter({key1 => sub { $_[0] * 3 }, key2 => 'five_times' });
1067
$row = $result->one;
1068
is_deeply($row, {key1 => 6, key2 => 40});
1069

            
1070
$dbi->register_filter(five_times => sub { $_[0] * 5 });
1071
$dbi->apply_filter('table1',
1072
    key1 => {end => sub { $_[0] * 3 } },
1073
    key2 => {end => 'five_times'}
1074
);
1075
$result = $dbi->select(table => 'table1');
1076
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1077
$row = $result->one;
1078
is_deeply($row, {key1 => 6, key2 => 40}, 'apply_filter');
1079

            
1080
$dbi->register_filter(five_times => sub { $_[0] * 5 });
1081
$dbi->apply_filter('table1',
1082
    key1 => {end => sub { $_[0] * 3 } },
1083
    key2 => {end => 'five_times'}
1084
);
1085
$result = $dbi->select(table => 'table1');
1086
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1087
$result->filter(key1 => undef);
1088
$result->end_filter(key1 => undef);
1089
$row = $result->one;
1090
is_deeply($row, {key1 => 1, key2 => 40}, 'apply_filter overwrite');
1091

            
1092
test 'remove_end_filter and remove_filter';
cleanup test
Yuki Kimoto authored on 2011-08-06
1093
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1094
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1095
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1096
$result = $dbi->select(table => 'table1');
1097
$row = $result
1098
       ->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 })
1099
       ->remove_filter
1100
       ->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 })
1101
       ->remove_end_filter
1102
       ->fetch_first;
1103
is_deeply($row, [1, 2]);
1104

            
1105
test 'empty where select';
cleanup test
Yuki Kimoto authored on 2011-08-06
1106
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1107
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1108
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1109
$result = $dbi->select(table => 'table1', where => {});
1110
$row = $result->one;
1111
is_deeply($row, {key1 => 1, key2 => 2});
1112

            
1113
test 'select query option';
cleanup test
Yuki Kimoto authored on 2011-08-06
1114
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1115
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1116
$query = $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, query => 1);
1117
is(ref $query, 'DBIx::Custom::Query');
1118
$query = $dbi->update(table => 'table1', where => {key1 => 1}, param => {key2 => 2}, query => 1);
1119
is(ref $query, 'DBIx::Custom::Query');
1120
$query = $dbi->delete(table => 'table1', where => {key1 => 1}, query => 1);
1121
is(ref $query, 'DBIx::Custom::Query');
1122
$query = $dbi->select(table => 'table1', where => {key1 => 1, key2 => 2}, query => 1);
1123
is(ref $query, 'DBIx::Custom::Query');
1124

            
1125
test 'DBIx::Custom::Where';
cleanup test
Yuki Kimoto authored on 2011-08-06
1126
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1127
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1128
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1129
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1130
$where = $dbi->where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
1131
is("$where", "where ( key1 = :key1 and key2 = :key2 )", 'no param');
1132

            
1133
$where = $dbi->where
1134
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
1135
             ->param({key1 => 1});
1136

            
1137
$result = $dbi->select(
1138
    table => 'table1',
1139
    where => $where
1140
);
1141
$row = $result->all;
1142
is_deeply($row, [{key1 => 1, key2 => 2}]);
1143

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

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

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

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

            
1184
$where = $dbi->where;
1185
$result = $dbi->select(
1186
    table => 'table1',
1187
    where => $where
1188
);
1189
$row = $result->all;
1190
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1191

            
1192
eval {
1193
$where = $dbi->where
1194
             ->clause(['uuu']);
1195
$result = $dbi->select(
1196
    table => 'table1',
1197
    where => $where
1198
);
1199
};
1200
ok($@);
1201

            
1202
$where = $dbi->where;
1203
is("$where", '');
1204

            
1205
$where = $dbi->where
1206
             ->clause(['or', ('key1 = :key1') x 2])
1207
             ->param({key1 => [1, 3]});
1208
$result = $dbi->select(
1209
    table => 'table1',
1210
    where => $where,
1211
);
1212
$row = $result->all;
1213
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1214

            
1215
$where = $dbi->where
1216
             ->clause(['or', ('key1 = :key1') x 2])
1217
             ->param({key1 => [1]});
1218
$result = $dbi->select(
1219
    table => 'table1',
1220
    where => $where,
1221
);
1222
$row = $result->all;
1223
is_deeply($row, [{key1 => 1, key2 => 2}]);
1224

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

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

            
1245
$where = $dbi->where
1246
             ->clause('key1 = :key1 key2 = :key2')
1247
             ->param({key1 => 1});
1248
eval{$where->to_string};
1249
like($@, qr/one column/);
1250

            
1251
$where = $dbi->where
1252
             ->clause('key1 = :key1')
1253
             ->param([]);
1254
eval{$where->to_string};
1255
like($@, qr/Parameter/);
1256

            
1257
$where = $dbi->where
1258
             ->clause(['or', ('key1 = :key1') x 3])
1259
             ->param({key1 => [$dbi->not_exists, 1, 3]});
1260
$result = $dbi->select(
1261
    table => 'table1',
1262
    where => $where,
1263
);
1264
$row = $result->all;
1265
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1266

            
1267
$where = $dbi->where
1268
             ->clause(['or', ('key1 = :key1') x 3])
1269
             ->param({key1 => [1, $dbi->not_exists, 3]});
1270
$result = $dbi->select(
1271
    table => 'table1',
1272
    where => $where,
1273
);
1274
$row = $result->all;
1275
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1276

            
1277
$where = $dbi->where
1278
             ->clause(['or', ('key1 = :key1') x 3])
1279
             ->param({key1 => [1, 3, $dbi->not_exists]});
1280
$result = $dbi->select(
1281
    table => 'table1',
1282
    where => $where,
1283
);
1284
$row = $result->all;
1285
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1286

            
1287
$where = $dbi->where
1288
             ->clause(['or', ('key1 = :key1') x 3])
1289
             ->param({key1 => [1, $dbi->not_exists, $dbi->not_exists]});
1290
$result = $dbi->select(
1291
    table => 'table1',
1292
    where => $where,
1293
);
1294
$row = $result->all;
1295
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1296

            
1297
$where = $dbi->where
1298
             ->clause(['or', ('key1 = :key1') x 3])
1299
             ->param({key1 => [$dbi->not_exists, 1, $dbi->not_exists]});
1300
$result = $dbi->select(
1301
    table => 'table1',
1302
    where => $where,
1303
);
1304
$row = $result->all;
1305
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1306

            
1307
$where = $dbi->where
1308
             ->clause(['or', ('key1 = :key1') x 3])
1309
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists, 1]});
1310
$result = $dbi->select(
1311
    table => 'table1',
1312
    where => $where,
1313
);
1314
$row = $result->all;
1315
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1316

            
1317
$where = $dbi->where
1318
             ->clause(['or', ('key1 = :key1') x 3])
1319
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists, $dbi->not_exists]});
1320
$result = $dbi->select(
1321
    table => 'table1',
1322
    where => $where,
1323
);
1324
$row = $result->all;
1325
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1326

            
1327
$where = $dbi->where
1328
             ->clause(['or', ('key1 = :key1') x 3])
1329
             ->param({key1 => []});
1330
$result = $dbi->select(
1331
    table => 'table1',
1332
    where => $where,
1333
);
1334
$row = $result->all;
1335
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1336

            
1337
$where = $dbi->where
1338
             ->clause(['and', '{> key1}', '{< key1}' ])
1339
             ->param({key1 => [2, $dbi->not_exists]});
1340
$result = $dbi->select(
1341
    table => 'table1',
1342
    where => $where,
1343
);
1344
$row = $result->all;
1345
is_deeply($row, [{key1 => 3, key2 => 4}], 'not_exists');
1346

            
1347
$where = $dbi->where
1348
             ->clause(['and', '{> key1}', '{< key1}' ])
1349
             ->param({key1 => [$dbi->not_exists, 2]});
1350
$result = $dbi->select(
1351
    table => 'table1',
1352
    where => $where,
1353
);
1354
$row = $result->all;
1355
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1356

            
1357
$where = $dbi->where
1358
             ->clause(['and', '{> key1}', '{< key1}' ])
1359
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists]});
1360
$result = $dbi->select(
1361
    table => 'table1',
1362
    where => $where,
1363
);
1364
$row = $result->all;
1365
is_deeply($row, [{key1 => 1, key2 => 2},{key1 => 3, key2 => 4}], 'not_exists');
1366

            
1367
$where = $dbi->where
1368
             ->clause(['and', '{> key1}', '{< key1}' ])
1369
             ->param({key1 => [0, 2]});
1370
$result = $dbi->select(
1371
    table => 'table1',
1372
    where => $where,
1373
);
1374
$row = $result->all;
1375
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1376

            
1377
$where = $dbi->where
1378
             ->clause(['and', 'key1 is not null', 'key2 is not null' ]);
1379
$result = $dbi->select(
1380
    table => 'table1',
1381
    where => $where,
1382
);
1383
$row = $result->all;
1384
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1385

            
1386
eval {$dbi->where(ppp => 1) };
1387
like($@, qr/invalid/);
1388

            
1389
$where = $dbi->where(
1390
    clause => ['and', ['or'], ['and', 'key1 = :key1', 'key2 = :key2']],
1391
    param => {key1 => 1, key2 => 2}
1392
);
1393
$result = $dbi->select(
1394
    table => 'table1',
1395
    where => $where,
1396
);
1397
$row = $result->all;
1398
is_deeply($row, [{key1 => 1, key2 => 2}]);
1399

            
1400

            
1401
$where = $dbi->where(
1402
    clause => ['and', ['or'], ['or', ':key1', ':key2']],
1403
    param => {}
1404
);
1405
$result = $dbi->select(
1406
    table => 'table1',
1407
    where => $where,
1408
);
1409
$row = $result->all;
1410
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1411

            
1412

            
1413
test 'dbi_option default';
1414
$dbi = DBIx::Custom->new;
1415
is_deeply($dbi->dbi_option, {});
1416

            
1417
test 'register_tag_processor';
cleanup test
Yuki Kimoto authored on 2011-08-06
1418
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1419
$dbi->register_tag_processor(
1420
    a => sub { 1 }
1421
);
1422
is($dbi->query_builder->tag_processors->{a}->(), 1);
1423

            
1424
test 'register_tag';
cleanup test
Yuki Kimoto authored on 2011-08-06
1425
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1426
$dbi->register_tag(
1427
    b => sub { 2 }
1428
);
1429
is($dbi->query_builder->tags->{b}->(), 2);
1430

            
1431
test 'table not specify exception';
cleanup test
Yuki Kimoto authored on 2011-08-06
1432
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1433
eval {$dbi->insert};
1434
like($@, qr/table/);
1435
eval {$dbi->update};
1436
like($@, qr/table/);
1437
eval {$dbi->delete};
1438
like($@, qr/table/);
1439
eval {$dbi->select};
1440
like($@, qr/table/);
1441

            
1442

            
1443
test 'more tests';
cleanup test
Yuki Kimoto authored on 2011-08-06
1444
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1445
eval{$dbi->apply_filter('table', 'column', [])};
1446
like($@, qr/apply_filter/);
1447

            
1448
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
1449
like($@, qr/apply_filter/);
1450

            
1451
$dbi->apply_filter(
1452

            
1453
);
cleanup test
Yuki Kimoto authored on 2011-08-06
1454
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1455
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1456
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1457
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1458
$dbi->apply_filter('table1', 'key2', 
1459
                   {in => sub { $_[0] * 3 }, out => sub { $_[0] * 2 }});
1460
$rows = $dbi->select(table => 'table1', where => {key2 => 1})->all;
1461
is_deeply($rows, [{key1 => 1, key2 => 6}]);
1462

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1463
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1464
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1465
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1466
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1467
$dbi->apply_filter('table1', 'key2', {});
1468
$rows = $dbi->select(table => 'table1', where => {key2 => 2})->all;
1469
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1470

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1471
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1472
eval {$dbi->apply_filter('table1', 'key2', {out => 'no'})};
1473
like($@, qr/not registered/);
1474
eval {$dbi->apply_filter('table1', 'key2', {in => 'no'})};
1475
like($@, qr/not registered/);
1476
$dbi->method({one => sub { 1 }});
1477
is($dbi->one, 1);
1478

            
1479
eval{DBIx::Custom->connect()};
1480
like($@, qr/_connect/);
1481

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1482
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1483
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1484
$dbi->register_filter(twice => sub { $_[0] * 2 });
1485
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
1486
             filter => {key1 => 'twice'});
1487
$row = $dbi->select(table => 'table1')->one;
1488
is_deeply($row, {key1 => 2, key2 => 2});
1489
eval {$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
1490
             filter => {key1 => 'no'}) };
1491
like($@, qr//);
1492

            
1493
$dbi->register_filter(one => sub { });
1494
$dbi->default_fetch_filter('one');
1495
ok($dbi->default_fetch_filter);
1496
$dbi->default_bind_filter('one');
1497
ok($dbi->default_bind_filter);
1498
eval{$dbi->default_fetch_filter('no')};
1499
like($@, qr/not registered/);
1500
eval{$dbi->default_bind_filter('no')};
1501
like($@, qr/not registered/);
1502
$dbi->default_bind_filter(undef);
1503
ok(!defined $dbi->default_bind_filter);
1504
$dbi->default_fetch_filter(undef);
1505
ok(!defined $dbi->default_fetch_filter);
1506
eval {$dbi->execute('select * from table1 {} {= author') };
1507
like($@, qr/Tag not finished/);
1508

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1509
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1510
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1511
$dbi->register_filter(one => sub { 1 });
1512
$result = $dbi->select(table => 'table1');
1513
eval {$result->filter(key1 => 'no')};
1514
like($@, qr/not registered/);
1515
eval {$result->end_filter(key1 => 'no')};
1516
like($@, qr/not registered/);
1517
$result->default_filter(undef);
1518
ok(!defined $result->default_filter);
1519
$result->default_filter('one');
1520
is($result->default_filter->(), 1);
1521

            
1522
test 'dbi_option';
1523
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:',
1524
                             dbi_option => {PrintError => 1});
1525
ok($dbi->dbh->{PrintError});
1526
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:',
1527
                             dbi_options => {PrintError => 1});
1528
ok($dbi->dbh->{PrintError});
1529

            
1530
test 'DBIx::Custom::Result stash()';
1531
$result = DBIx::Custom::Result->new;
1532
is_deeply($result->stash, {}, 'default');
1533
$result->stash->{foo} = 1;
1534
is($result->stash->{foo}, 1, 'get and set');
1535

            
1536
test 'filter __ expression';
cleanup test
Yuki Kimoto authored on 2011-08-06
1537
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1538
$dbi->execute('create table company (id, name, location_id)');
1539
$dbi->execute('create table location (id, name)');
1540
$dbi->apply_filter('location',
1541
  name => {in => sub { uc $_[0] } }
1542
);
1543

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

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

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

            
1559
$result = $dbi->select(
1560
    table => 'company', relation => {'company.location_id' => 'location.id'},
1561
    column => ['location.name as "location.name"']
1562
);
1563
is($result->fetch_first->[0], 'B');
1564

            
1565
test 'Model class';
1566
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1567
$dbi = MyDBI1->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1568
$dbi->execute("create table book (title, author)");
1569
$model = $dbi->model('book');
1570
$model->insert({title => 'a', author => 'b'});
1571
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
1572
$dbi->execute("create table company (name)");
1573
$model = $dbi->model('company');
1574
$model->insert({name => 'a'});
1575
is_deeply($model->list->all, [{name => 'a'}], 'basic');
1576
is($dbi->models->{'book'}, $dbi->model('book'));
1577
is($dbi->models->{'company'}, $dbi->model('company'));
1578

            
1579
{
1580
    package MyDBI4;
1581

            
1582
    use strict;
1583
    use warnings;
1584

            
1585
    use base 'DBIx::Custom';
1586

            
1587
    sub connect {
1588
        my $self = shift->SUPER::connect(@_);
1589
        
1590
        $self->include_model(
1591
            MyModel2 => [
1592
                'book',
1593
                {class => 'Company', name => 'company'}
1594
            ]
1595
        );
1596
    }
1597

            
1598
    package MyModel2::Base1;
1599

            
1600
    use strict;
1601
    use warnings;
1602

            
1603
    use base 'DBIx::Custom::Model';
1604

            
1605
    package MyModel2::book;
1606

            
1607
    use strict;
1608
    use warnings;
1609

            
1610
    use base 'MyModel2::Base1';
1611

            
1612
    sub insert {
1613
        my ($self, $param) = @_;
1614
        
1615
        return $self->SUPER::insert(param => $param);
1616
    }
1617

            
1618
    sub list { shift->select; }
1619

            
1620
    package MyModel2::Company;
1621

            
1622
    use strict;
1623
    use warnings;
1624

            
1625
    use base 'MyModel2::Base1';
1626

            
1627
    sub insert {
1628
        my ($self, $param) = @_;
1629
        
1630
        return $self->SUPER::insert(param => $param);
1631
    }
1632

            
1633
    sub list { shift->select; }
1634
}
cleanup test
Yuki Kimoto authored on 2011-08-06
1635
$dbi = MyDBI4->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1636
$dbi->execute("create table book (title, author)");
1637
$model = $dbi->model('book');
1638
$model->insert({title => 'a', author => 'b'});
1639
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
1640
$dbi->execute("create table company (name)");
1641
$model = $dbi->model('company');
1642
$model->insert({name => 'a'});
1643
is_deeply($model->list->all, [{name => 'a'}], 'basic');
1644

            
1645
{
1646
     package MyDBI5;
1647

            
1648
    use strict;
1649
    use warnings;
1650

            
1651
    use base 'DBIx::Custom';
1652

            
1653
    sub connect {
1654
        my $self = shift->SUPER::connect(@_);
1655
        
1656
        $self->include_model('MyModel4');
1657
    }
1658
}
cleanup test
Yuki Kimoto authored on 2011-08-06
1659
$dbi = MyDBI5->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1660
$dbi->execute("create table company (name)");
1661
$dbi->execute("create table table1 (key1)");
1662
$model = $dbi->model('company');
1663
$model->insert({name => 'a'});
1664
is_deeply($model->list->all, [{name => 'a'}], 'include all model');
1665
$dbi->insert(table => 'table1', param => {key1 => 1});
1666
$model = $dbi->model('book');
1667
is_deeply($model->list->all, [{key1 => 1}], 'include all model');
1668

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

            
1676
test 'columns';
1677
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1678
$dbi = MyDBI1->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1679
$model = $dbi->model('book');
1680
$model->columns(['id', 'number']);
1681
is_deeply($model->columns, ['id', 'number']);
1682

            
1683
test 'setup_model';
1684
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1685
$dbi = MyDBI1->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1686
$dbi->execute('create table book (id)');
1687
$dbi->execute('create table company (id, name);');
1688
$dbi->execute('create table test (id, name, primary key (id, name));');
1689
$dbi->setup_model;
1690
is_deeply($dbi->model('book')->columns, ['id']);
1691
is_deeply($dbi->model('company')->columns, ['id', 'name']);
1692

            
1693
test 'delete_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1694
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1695
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1696
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1697
$dbi->delete_at(
1698
    table => 'table1',
1699
    primary_key => ['key1', 'key2'],
1700
    where => [1, 2],
1701
);
1702
is_deeply($dbi->select(table => 'table1')->all, []);
1703

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

            
1712
test 'insert_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1713
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1714
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1748
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1749
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1750
$dbi->insert_at(
1751
    {key3 => 3},
1752
    primary_key => ['key1', 'key2'], 
1753
    table => 'table1',
1754
    where => [1, 2],
1755
);
1756
is($dbi->select(table => 'table1')->one->{key1}, 1);
1757
is($dbi->select(table => 'table1')->one->{key2}, 2);
1758
is($dbi->select(table => 'table1')->one->{key3}, 3);
1759

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1786
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1787
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1788
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1789
$dbi->update_at(
1790
    {key3 => 4},
1791
    table => 'table1',
1792
    primary_key => ['key1', 'key2'],
1793
    where => [1, 2]
1794
);
1795
is($dbi->select(table => 'table1')->one->{key1}, 1);
1796
is($dbi->select(table => 'table1')->one->{key2}, 2);
1797
is($dbi->select(table => 'table1')->one->{key3}, 4);
1798

            
1799
test 'select_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1800
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1801
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1802
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1803
$result = $dbi->select_at(
1804
    table => 'table1',
1805
    primary_key => ['key1', 'key2'],
1806
    where => [1, 2]
1807
);
1808
$row = $result->one;
1809
is($row->{key1}, 1);
1810
is($row->{key2}, 2);
1811
is($row->{key3}, 3);
1812

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

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

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

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

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

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

            
1874
test 'columns';
1875
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1876
$dbi = MyDBI1->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1877
$model = $dbi->model('book');
1878

            
1879

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

            
1908
test 'model insert_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1909
$dbi = MyDBI6->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1910
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1911
$dbi->model('table1')->insert_at(
1912
    where => [1, 2],
1913
    param => {key3 => 3}
1914
);
1915
$result = $dbi->model('table1')->select;
1916
$row = $result->one;
1917
is($row->{key1}, 1);
1918
is($row->{key2}, 2);
1919
is($row->{key3}, 3);
1920

            
1921
test 'model update_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1922
$dbi = MyDBI6->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1923
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1924
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1925
$dbi->model('table1')->update_at(
1926
    where => [1, 2],
1927
    param => {key3 => 4}
1928
);
1929
$result = $dbi->model('table1')->select;
1930
$row = $result->one;
1931
is($row->{key1}, 1);
1932
is($row->{key2}, 2);
1933
is($row->{key3}, 4);
1934

            
1935
test 'model select_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1936
$dbi = MyDBI6->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1937
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1938
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1939
$result = $dbi->model('table1')->select_at(where => [1, 2]);
1940
$row = $result->one;
1941
is($row->{key1}, 1);
1942
is($row->{key2}, 2);
1943
is($row->{key3}, 3);
1944

            
1945

            
1946
test 'mycolumn and column';
1947
{
1948
    package MyDBI7;
1949
    
1950
    use base 'DBIx::Custom';
1951
    
1952
    sub connect {
1953
        my $self = shift->SUPER::connect(@_);
1954
        
1955
        $self->include_model('MyModel6');
1956
        
1957
        
1958
        return $self;
1959
    }
1960
}
cleanup test
Yuki Kimoto authored on 2011-08-06
1961
$dbi = MyDBI7->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1962
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1963
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1964
$dbi->separator('__');
1965
$dbi->setup_model;
1966
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1967
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1968
$model = $dbi->model('table1');
1969
$result = $model->select(
1970
    column => [$model->mycolumn, $model->column('table2')],
1971
    where => {'table1.key1' => 1}
1972
);
1973
is_deeply($result->one,
1974
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
1975

            
1976
test 'update_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
1977
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1978
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1979
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1980
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1981

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

            
1995

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2014
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2015
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2016
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
2017
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
2018

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

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

            
2036

            
2037
test 'update_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
2038
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2039
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2040
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
2041
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
2042

            
2043
$param = {key2 => 11};
2044
$update_param = $dbi->assign_param($param);
2045
$sql = <<"EOS";
2046
update table1 set $update_param
2047
where key1 = 1
2048
EOS
2049
$dbi->execute($sql, param => $param, table => 'table1');
test cleanup
Yuki Kimoto authored on 2011-08-06
2050
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
2051
$rows   = $result->all;
2052
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
2053
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
2054
                  "basic");
2055

            
2056

            
2057
test 'insert_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
2058
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2059
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2060
$param = {key1 => 1, key2 => 2};
2061
$insert_param = $dbi->insert_param($param);
2062
$sql = <<"EOS";
2063
insert into table1 $insert_param
2064
EOS
2065
$dbi->execute($sql, param => $param, table => 'table1');
2066
is($dbi->select(table => 'table1')->one->{key1}, 1);
2067
is($dbi->select(table => 'table1')->one->{key2}, 2);
2068

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2069
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2070
$dbi->quote('"');
cleanup test
Yuki Kimoto authored on 2011-08-06
2071
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2072
$param = {key1 => 1, key2 => 2};
2073
$insert_param = $dbi->insert_param($param);
2074
$sql = <<"EOS";
2075
insert into table1 $insert_param
2076
EOS
2077
$dbi->execute($sql, param => $param, table => 'table1');
2078
is($dbi->select(table => 'table1')->one->{key1}, 1);
2079
is($dbi->select(table => 'table1')->one->{key2}, 2);
2080

            
2081
eval { $dbi->insert_param({";" => 1}) };
2082
like($@, qr/not safety/);
2083

            
2084

            
2085
test 'join';
cleanup test
Yuki Kimoto authored on 2011-08-06
2086
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2087
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2088
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2089
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
cleanup test
Yuki Kimoto authored on 2011-08-06
2090
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2091
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
2092
$dbi->execute($CREATE_TABLE->{4});
2093
$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4});
2094
$rows = $dbi->select(
2095
    table => 'table1',
2096
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
2097
    where   => {'table1.key2' => 2},
2098
    join  => ['left outer join table2 on table1.key1 = table2.key1']
2099
)->all;
2100
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]);
2101

            
2102
$rows = $dbi->select(
2103
    table => 'table1',
2104
    where   => {'key1' => 1},
2105
    join  => ['left outer join table2 on table1.key1 = table2.key1']
2106
)->all;
2107
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2108

            
2109
eval {
2110
    $rows = $dbi->select(
2111
        table => 'table1',
2112
        column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
2113
        where   => {'table1.key2' => 2},
2114
        join  => {'table1.key1' => 'table2.key1'}
2115
    );
2116
};
2117
like ($@, qr/array/);
2118

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

            
2127
$rows = $dbi->select(
2128
    column => 'table3.key4 as table3__key4',
2129
    table => 'table1',
2130
    where   => {'table1.key1' => 1},
2131
    join  => ['left outer join table2 on table1.key1 = table2.key1',
2132
              'left outer join table3 on table2.key3 = table3.key3']
2133
)->all;
2134
is_deeply($rows, [{table3__key4 => 4}]);
2135

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2145
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2146
$dbi->quote('"');
cleanup test
Yuki Kimoto authored on 2011-08-06
2147
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2148
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-06
2149
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2150
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
2151
$rows = $dbi->select(
2152
    table => 'table1',
2153
    column => '"table1"."key1" as "table1_key1", "table2"."key1" as "table2_key1", "key2", "key3"',
2154
    where   => {'table1.key2' => 2},
2155
    join  => ['left outer join "table2" on "table1"."key1" = "table2"."key1"'],
2156
)->all;
2157
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}],
2158
          'quote');
2159

            
2160
{
2161
    package MyDBI8;
2162
    
2163
    use base 'DBIx::Custom';
2164
    
2165
    sub connect {
2166
        my $self = shift->SUPER::connect(@_);
2167
        
2168
        $self->include_model('MyModel7');
2169
        
2170
        return $self;
2171
    }
2172
}
2173

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2174
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2175
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2176
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2177
$sql = <<"EOS";
2178
left outer join (
2179
  select * from table1 as t1
2180
  where t1.key2 = (
2181
    select max(t2.key2) from table1 as t2
2182
    where t1.key1 = t2.key1
2183
  )
2184
) as latest_table1 on table1.key1 = latest_table1.key1
2185
EOS
2186
$join = [$sql];
2187
$rows = $dbi->select(
2188
    table => 'table1',
2189
    column => 'latest_table1.key1 as latest_table1__key1',
2190
    join  => $join
2191
)->all;
2192
is_deeply($rows, [{latest_table1__key1 => 1}]);
2193

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2194
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2195
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2196
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2197
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2198
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2199
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
2200
$result = $dbi->select(
2201
    table => 'table1',
2202
    join => [
2203
        "left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1"
2204
    ]
2205
);
2206
is_deeply($result->all, [{key1 => 1, key2 => 2}]);
2207
$result = $dbi->select(
2208
    table => 'table1',
2209
    column => [{table2 => ['key3']}],
2210
    join => [
2211
        "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1"
2212
    ]
2213
);
2214
is_deeply($result->all, [{'table2.key3' => 4}]);
2215
$result = $dbi->select(
2216
    table => 'table1',
2217
    column => [{table2 => ['key3']}],
2218
    join => [
2219
        "left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'"
2220
    ]
2221
);
2222
is_deeply($result->all, [{'table2.key3' => 4}]);
2223

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2224
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2225
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2226
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2227
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2228
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2229
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
2230
$result = $dbi->select(
2231
    table => 'table1',
2232
    column => [{table2 => ['key3']}],
2233
    join => [
2234
        {
2235
            clause => "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1",
2236
            table => ['table1', 'table2']
2237
        }
2238
    ]
2239
);
2240
is_deeply($result->all, [{'table2.key3' => 4}]);
2241

            
2242
test 'mycolumn';
cleanup test
Yuki Kimoto authored on 2011-08-06
2243
$dbi = MyDBI8->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2244
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2245
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2246
$dbi->setup_model;
2247
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2248
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2249
$model = $dbi->model('table1');
2250
$result = $model->select_at(
2251
    column => [
2252
        $model->mycolumn,
2253
        $model->column('table2')
2254
    ]
2255
);
2256
is_deeply($result->one,
2257
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
2258

            
2259
$result = $model->select_at(
2260
    column => [
2261
        $model->mycolumn(['key1']),
2262
        $model->column(table2 => ['key1'])
2263
    ]
2264
);
2265
is_deeply($result->one,
2266
          {key1 => 1, 'table2.key1' => 1});
2267
$result = $model->select_at(
2268
    column => [
2269
        $model->mycolumn(['key1']),
2270
        {table2 => ['key1']}
2271
    ]
2272
);
2273
is_deeply($result->one,
2274
          {key1 => 1, 'table2.key1' => 1});
2275

            
2276
$result = $model->select_at(
2277
    column => [
2278
        $model->mycolumn(['key1']),
2279
        ['table2.key1', as => 'table2.key1']
2280
    ]
2281
);
2282
is_deeply($result->one,
2283
          {key1 => 1, 'table2.key1' => 1});
2284

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

            
2294
test 'dbi method from model';
2295
{
2296
    package MyDBI9;
2297
    
2298
    use base 'DBIx::Custom';
2299
    
2300
    sub connect {
2301
        my $self = shift->SUPER::connect(@_);
2302
        
2303
        $self->include_model('MyModel8')->setup_model;
2304
        
2305
        return $self;
2306
    }
2307
}
cleanup test
Yuki Kimoto authored on 2011-08-06
2308
$dbi = MyDBI9->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2309
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2310
$model = $dbi->model('table1');
2311
eval{$model->execute('select * from table1')};
2312
ok(!$@);
2313

            
2314
test 'column table option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2315
$dbi = MyDBI9->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2316
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2317
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2318
$dbi->setup_model;
2319
$dbi->execute('insert into table1 (key1, key2) values (1, 2);');
2320
$dbi->execute('insert into table2 (key1, key3) values (1, 4);');
2321
$model = $dbi->model('table1');
2322
$result = $model->select(
2323
    column => [
2324
        $model->column('table2', {alias => 'table2_alias'})
2325
    ],
2326
    where => {'table2_alias.key3' => 4}
2327
);
2328
is_deeply($result->one, 
2329
          {'table2_alias.key1' => 1, 'table2_alias.key3' => 4});
2330

            
2331
$dbi->separator('__');
2332
$result = $model->select(
2333
    column => [
2334
        $model->column('table2', {alias => 'table2_alias'})
2335
    ],
2336
    where => {'table2_alias.key3' => 4}
2337
);
2338
is_deeply($result->one, 
2339
          {'table2_alias__key1' => 1, 'table2_alias__key3' => 4});
2340

            
2341
$dbi->separator('-');
2342
$result = $model->select(
2343
    column => [
2344
        $model->column('table2', {alias => 'table2_alias'})
2345
    ],
2346
    where => {'table2_alias.key3' => 4}
2347
);
2348
is_deeply($result->one, 
2349
          {'table2_alias-key1' => 1, 'table2_alias-key3' => 4});
2350

            
2351
test 'type option'; # DEPRECATED!
2352
$dbi = DBIx::Custom->connect(
2353
    data_source => 'dbi:SQLite:dbname=:memory:',
2354
    dbi_option => {
2355
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
2356
    }
2357
);
2358
my $binary = pack("I3", 1, 2, 3);
2359
$dbi->execute('create table table1(key1, key2)');
2360
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
2361
$result = $dbi->select(table => 'table1');
2362
$row   = $result->one;
2363
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2364
$result = $dbi->execute('select length(key1) as key1_length from table1');
2365
$row = $result->one;
2366
is($row->{key1_length}, length $binary);
2367

            
2368
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [['key1'] => DBI::SQL_BLOB]);
2369
$result = $dbi->select(table => 'table1');
2370
$row   = $result->one;
2371
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2372
$result = $dbi->execute('select length(key1) as key1_length from table1');
2373
$row = $result->one;
2374
is($row->{key1_length}, length $binary);
2375

            
2376

            
2377
test 'bind_type option';
2378
$dbi = DBIx::Custom->connect(
2379
    data_source => 'dbi:SQLite:dbname=:memory:',
2380
    dbi_option => {
2381
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
2382
    }
2383
);
2384
$binary = pack("I3", 1, 2, 3);
2385
$dbi->execute('create table table1(key1, key2)');
2386
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, bind_type => [key1 => DBI::SQL_BLOB]);
2387
$result = $dbi->select(table => 'table1');
2388
$row   = $result->one;
2389
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2390
$result = $dbi->execute('select length(key1) as key1_length from table1');
2391
$row = $result->one;
2392
is($row->{key1_length}, length $binary);
2393

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

            
2402
test 'model type attribute';
2403
$dbi = DBIx::Custom->connect(
2404
    data_source => 'dbi:SQLite:dbname=:memory:',
2405
    dbi_option => {
2406
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
2407
    }
2408
);
2409
$binary = pack("I3", 1, 2, 3);
2410
$dbi->execute('create table table1(key1, key2)');
2411
$model = $dbi->create_model(table => 'table1', bind_type => [key1 => DBI::SQL_BLOB]);
2412
$model->insert(param => {key1 => $binary, key2 => 'あ'});
2413
$result = $dbi->select(table => 'table1');
2414
$row   = $result->one;
2415
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2416
$result = $dbi->execute('select length(key1) as key1_length from table1');
2417
$row = $result->one;
2418
is($row->{key1_length}, length $binary);
2419

            
2420
test 'create_model';
cleanup test
Yuki Kimoto authored on 2011-08-06
2421
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2422
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2423
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2424

            
2425
$dbi->create_model(
2426
    table => 'table1',
2427
    join => [
2428
       'left outer join table2 on table1.key1 = table2.key1'
2429
    ],
2430
    primary_key => ['key1']
2431
);
2432
$model2 = $dbi->create_model(
2433
    table => 'table2'
2434
);
2435
$dbi->create_model(
2436
    table => 'table3',
2437
    filter => [
2438
        key1 => {in => sub { uc $_[0] }}
2439
    ]
2440
);
2441
$dbi->setup_model;
2442
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2443
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2444
$model = $dbi->model('table1');
2445
$result = $model->select(
2446
    column => [$model->mycolumn, $model->column('table2')],
2447
    where => {'table1.key1' => 1}
2448
);
2449
is_deeply($result->one,
2450
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
2451
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
2452

            
2453
test 'model method';
2454
test 'create_model';
cleanup test
Yuki Kimoto authored on 2011-08-06
2455
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2456
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2457
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2458
$model = $dbi->create_model(
2459
    table => 'table2'
2460
);
2461
$model->method(foo => sub { shift->select(@_) });
2462
is_deeply($model->foo->one, {key1 => 1, key3 => 3});
2463

            
2464
test 'merge_param';
2465
{
2466
    my $dbi = DBIx::Custom->new;
2467
    my $param1 = {key1 => 1, key2 => 2, key3 => 3};
2468
    my $param2 = {key1 => 1, key2 => 2};
2469
    my $param3 = {key1 => 1};
2470
    my $param = $dbi->merge_param($param1, $param2, $param3);
2471
    is_deeply($param, {key1 => [1, 1, 1], key2 => [2, 2], key3 => 3});
2472
}
2473

            
2474
{
2475
    my $dbi = DBIx::Custom->new;
2476
    my $param1 = {key1 => [1, 2], key2 => 1, key3 => [1, 2]};
2477
    my $param2 = {key1 => [3, 4], key2 => [2, 3], key3 => 3};
2478
    my $param = $dbi->merge_param($param1, $param2);
2479
    is_deeply($param, {key1 => [1, 2, 3, 4], key2 => [1, 2, 3], key3 => [1, 2, 3]});
2480
}
2481

            
2482
test 'select() param option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2483
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2484
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2485
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2486
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
2487
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2488
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2489
$dbi->insert(table => 'table2', param => {key1 => 2, key3 => 5});
2490
$rows = $dbi->select(
2491
    table => 'table1',
2492
    column => 'table1.key1 as table1_key1, key2, key3',
2493
    where   => {'table1.key2' => 3},
2494
    join  => ['inner join (select * from table2 where {= table2.key3})' . 
2495
              ' as table2 on table1.key1 = table2.key1'],
2496
    param => {'table2.key3' => 5}
2497
)->all;
2498
is_deeply($rows, [{table1_key1 => 2, key2 => 3, key3 => 5}]);
2499

            
2500

            
2501
test 'select() wrap option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2502
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2503
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2504
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2505
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2506
$rows = $dbi->select(
2507
    table => 'table1',
2508
    column => 'key1',
2509
    wrap => ['select * from (', ') as t where key1 = 1']
2510
)->all;
2511
is_deeply($rows, [{key1 => 1}]);
2512

            
2513
eval {
2514
$dbi->select(
2515
    table => 'table1',
2516
    column => 'key1',
2517
    wrap => 'select * from ('
2518
)
2519
};
2520
like($@, qr/array/);
2521

            
2522
test 'select() string where';
cleanup test
Yuki Kimoto authored on 2011-08-06
2523
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2524
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2525
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2526
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2527
$rows = $dbi->select(
2528
    table => 'table1',
2529
    where => 'key1 = :key1 and key2 = :key2',
2530
    where_param => {key1 => 1, key2 => 2}
2531
)->all;
2532
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2533

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2534
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2535
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2536
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2537
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2538
$rows = $dbi->select(
2539
    table => 'table1',
2540
    where => [
2541
        'key1 = :key1 and key2 = :key2',
2542
        {key1 => 1, key2 => 2}
2543
    ]
2544
)->all;
2545
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2546

            
2547
test 'delete() string where';
cleanup test
Yuki Kimoto authored on 2011-08-06
2548
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2549
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2550
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2551
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2552
$dbi->delete(
2553
    table => 'table1',
2554
    where => 'key1 = :key1 and key2 = :key2',
2555
    where_param => {key1 => 1, key2 => 2}
2556
);
2557
$rows = $dbi->select(table => 'table1')->all;
2558
is_deeply($rows, [{key1 => 2, key2 => 3}]);
2559

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2560
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2561
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2562
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2563
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2564
$dbi->delete(
2565
    table => 'table1',
2566
    where => [
2567
        'key1 = :key1 and key2 = :key2',
2568
         {key1 => 1, key2 => 2}
2569
    ]
2570
);
2571
$rows = $dbi->select(table => 'table1')->all;
2572
is_deeply($rows, [{key1 => 2, key2 => 3}]);
2573

            
2574

            
2575
test 'update() string where';
cleanup test
Yuki Kimoto authored on 2011-08-06
2576
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2577
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2578
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2579
$dbi->update(
2580
    table => 'table1',
2581
    param => {key1 => 5},
2582
    where => 'key1 = :key1 and key2 = :key2',
2583
    where_param => {key1 => 1, key2 => 2}
2584
);
2585
$rows = $dbi->select(table => 'table1')->all;
2586
is_deeply($rows, [{key1 => 5, key2 => 2}]);
2587

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2588
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2589
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2590
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2591
$dbi->update(
2592
    table => 'table1',
2593
    param => {key1 => 5},
2594
    where => [
2595
        'key1 = :key1 and key2 = :key2',
2596
        {key1 => 1, key2 => 2}
2597
    ]
2598
);
2599
$rows = $dbi->select(table => 'table1')->all;
2600
is_deeply($rows, [{key1 => 5, key2 => 2}]);
2601

            
2602
test 'insert id and primary_key option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2603
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2604
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2605
$dbi->insert(
2606
    primary_key => ['key1', 'key2'], 
2607
    table => 'table1',
2608
    id => [1, 2],
2609
    param => {key3 => 3}
2610
);
2611
is($dbi->select(table => 'table1')->one->{key1}, 1);
2612
is($dbi->select(table => 'table1')->one->{key2}, 2);
2613
is($dbi->select(table => 'table1')->one->{key3}, 3);
2614

            
2615
$dbi->delete_all(table => 'table1');
2616
$dbi->insert(
2617
    primary_key => 'key1', 
2618
    table => 'table1',
2619
    id => 0,
2620
    param => {key2 => 2, key3 => 3}
2621
);
2622

            
2623
is($dbi->select(table => 'table1')->one->{key1}, 0);
2624
is($dbi->select(table => 'table1')->one->{key2}, 2);
2625
is($dbi->select(table => 'table1')->one->{key3}, 3);
2626

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2627
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2628
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2629
$dbi->insert(
2630
    {key3 => 3},
2631
    primary_key => ['key1', 'key2'], 
2632
    table => 'table1',
2633
    id => [1, 2],
2634
);
2635
is($dbi->select(table => 'table1')->one->{key1}, 1);
2636
is($dbi->select(table => 'table1')->one->{key2}, 2);
2637
is($dbi->select(table => 'table1')->one->{key3}, 3);
2638

            
2639

            
2640
test 'model insert id and primary_key option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2641
$dbi = MyDBI6->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2642
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2643
$dbi->model('table1')->insert(
2644
    id => [1, 2],
2645
    param => {key3 => 3}
2646
);
2647
$result = $dbi->model('table1')->select;
2648
$row = $result->one;
2649
is($row->{key1}, 1);
2650
is($row->{key2}, 2);
2651
is($row->{key3}, 3);
2652

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2653
$dbi = MyDBI6->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2654
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2655
$dbi->model('table1')->insert(
2656
    {key3 => 3},
2657
    id => [1, 2]
2658
);
2659
$result = $dbi->model('table1')->select;
2660
$row = $result->one;
2661
is($row->{key1}, 1);
2662
is($row->{key2}, 2);
2663
is($row->{key3}, 3);
2664

            
2665
test 'update and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2666
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2667
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2668
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2669
$dbi->update(
2670
    table => 'table1',
2671
    primary_key => ['key1', 'key2'],
2672
    id => [1, 2],
2673
    param => {key3 => 4}
2674
);
2675
is($dbi->select(table => 'table1')->one->{key1}, 1);
2676
is($dbi->select(table => 'table1')->one->{key2}, 2);
2677
is($dbi->select(table => 'table1')->one->{key3}, 4);
2678

            
2679
$dbi->delete_all(table => 'table1');
2680
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2681
$dbi->update(
2682
    table => 'table1',
2683
    primary_key => 'key1',
2684
    id => 0,
2685
    param => {key3 => 4}
2686
);
2687
is($dbi->select(table => 'table1')->one->{key1}, 0);
2688
is($dbi->select(table => 'table1')->one->{key2}, 2);
2689
is($dbi->select(table => 'table1')->one->{key3}, 4);
2690

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2691
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2692
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2693
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2694
$dbi->update(
2695
    {key3 => 4},
2696
    table => 'table1',
2697
    primary_key => ['key1', 'key2'],
2698
    id => [1, 2]
2699
);
2700
is($dbi->select(table => 'table1')->one->{key1}, 1);
2701
is($dbi->select(table => 'table1')->one->{key2}, 2);
2702
is($dbi->select(table => 'table1')->one->{key3}, 4);
2703

            
2704

            
2705
test 'model update and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2706
$dbi = MyDBI6->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2707
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2708
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2709
$dbi->model('table1')->update(
2710
    id => [1, 2],
2711
    param => {key3 => 4}
2712
);
2713
$result = $dbi->model('table1')->select;
2714
$row = $result->one;
2715
is($row->{key1}, 1);
2716
is($row->{key2}, 2);
2717
is($row->{key3}, 4);
2718

            
2719

            
2720
test 'delete and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2721
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2722
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2723
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2724
$dbi->delete(
2725
    table => 'table1',
2726
    primary_key => ['key1', 'key2'],
2727
    id => [1, 2],
2728
);
2729
is_deeply($dbi->select(table => 'table1')->all, []);
2730

            
2731
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2732
$dbi->delete(
2733
    table => 'table1',
2734
    primary_key => 'key1',
2735
    id => 0,
2736
);
2737
is_deeply($dbi->select(table => 'table1')->all, []);
2738

            
2739

            
2740
test 'model delete and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2741
$dbi = MyDBI6->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2742
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2743
$dbi->execute("create table table2 (key1, key2, key3)");
2744
$dbi->execute("create table table3 (key1, key2, key3)");
2745
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2746
$dbi->model('table1')->delete(id => [1, 2]);
2747
is_deeply($dbi->select(table => 'table1')->all, []);
2748
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
2749
$dbi->model('table1_1')->delete(id => [1, 2]);
2750
is_deeply($dbi->select(table => 'table1')->all, []);
2751
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
2752
$dbi->model('table1_3')->delete(id => [1, 2]);
2753
is_deeply($dbi->select(table => 'table1')->all, []);
2754

            
2755

            
2756
test 'select and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2757
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2758
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2759
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2760
$result = $dbi->select(
2761
    table => 'table1',
2762
    primary_key => ['key1', 'key2'],
2763
    id => [1, 2]
2764
);
2765
$row = $result->one;
2766
is($row->{key1}, 1);
2767
is($row->{key2}, 2);
2768
is($row->{key3}, 3);
2769

            
2770
$dbi->delete_all(table => 'table1');
2771
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2772
$result = $dbi->select(
2773
    table => 'table1',
2774
    primary_key => 'key1',
2775
    id => 0,
2776
);
2777
$row = $result->one;
2778
is($row->{key1}, 0);
2779
is($row->{key2}, 2);
2780
is($row->{key3}, 3);
2781

            
2782
$dbi->delete_all(table => 'table1');
2783
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2784
$result = $dbi->select(
2785
    table => 'table1',
2786
    primary_key => ['key1', 'key2'],
2787
    id => [1, 2]
2788
);
2789
$row = $result->one;
2790
is($row->{key1}, 1);
2791
is($row->{key2}, 2);
2792
is($row->{key3}, 3);
2793

            
2794

            
2795
test 'model select_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
2796
$dbi = MyDBI6->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2797
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2798
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2799
$result = $dbi->model('table1')->select(id => [1, 2]);
2800
$row = $result->one;
2801
is($row->{key1}, 1);
2802
is($row->{key2}, 2);
2803
is($row->{key3}, 3);
2804

            
2805
test 'column separator is default .';
cleanup test
Yuki Kimoto authored on 2011-08-06
2806
$dbi = MyDBI7->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2807
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2808
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2809
$dbi->setup_model;
2810
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2811
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2812
$model = $dbi->model('table1');
2813
$result = $model->select(
2814
    column => [$model->column('table2')],
2815
    where => {'table1.key1' => 1}
2816
);
2817
is_deeply($result->one,
2818
          {'table2.key1' => 1, 'table2.key3' => 3});
2819

            
2820
$result = $model->select(
2821
    column => [$model->column('table2' => [qw/key1 key3/])],
2822
    where => {'table1.key1' => 1}
2823
);
2824
is_deeply($result->one,
2825
          {'table2.key1' => 1, 'table2.key3' => 3});
2826

            
2827

            
2828
test 'type_rule from';
2829
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2830
$dbi->type_rule(
2831
    from1 => {
2832
        date => sub { uc $_[0] }
2833
    }
2834
);
2835
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2836
$dbi->insert({key1 => 'a'}, table => 'table1');
2837
$result = $dbi->select(table => 'table1');
2838
is($result->fetch_first->[0], 'A');
2839

            
2840
$result = $dbi->select(table => 'table1');
2841
is($result->one->{key1}, 'A');
2842

            
2843

            
2844
test 'type_rule into';
2845
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2846
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2847
$dbi->type_rule(
2848
    into1 => {
2849
        date => sub { uc $_[0] }
2850
    }
2851
);
2852
$dbi->insert({key1 => 'a'}, table => 'table1');
2853
$result = $dbi->select(table => 'table1');
2854
is($result->one->{key1}, 'A');
2855

            
2856
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2857
$dbi->execute("create table table1 (key1 date, key2 datetime)");
2858
$dbi->type_rule(
2859
    into1 => [
2860
         [qw/date datetime/] => sub { uc $_[0] }
2861
    ]
2862
);
2863
$dbi->insert({key1 => 'a', key2 => 'b'}, table => 'table1');
2864
$result = $dbi->select(table => 'table1');
2865
$row = $result->one;
2866
is($row->{key1}, 'A');
2867
is($row->{key2}, 'B');
2868

            
2869
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2870
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2871
$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1');
2872
$dbi->type_rule(
2873
    into1 => [
2874
        [qw/date datetime/] => sub { uc $_[0] }
2875
    ]
2876
);
2877
$result = $dbi->execute(
2878
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
2879
    param => {key1 => 'a', 'table1.key2' => 'b'}
2880
);
2881
$row = $result->one;
2882
is($row->{key1}, 'a');
2883
is($row->{key2}, 'B');
2884

            
2885
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2886
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2887
$dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1');
2888
$dbi->type_rule(
2889
    into1 => [
2890
        [qw/date datetime/] => sub { uc $_[0] }
2891
    ]
2892
);
2893
$result = $dbi->execute(
2894
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
2895
    param => {key1 => 'a', 'table1.key2' => 'b'},
2896
    table => 'table1'
2897
);
2898
$row = $result->one;
2899
is($row->{key1}, 'A');
2900
is($row->{key2}, 'B');
2901

            
2902
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2903
$dbi->execute("create table table1 (key1 date, key2 datetime)");
2904
$dbi->register_filter(twice => sub { $_[0] * 2 });
2905
$dbi->type_rule(
2906
    from1 => {
2907
        date => 'twice',
2908
    },
2909
    into1 => {
2910
        date => 'twice',
2911
    }
2912
);
2913
$dbi->insert({key1 => 2}, table => 'table1');
2914
$result = $dbi->select(table => 'table1');
2915
is($result->fetch->[0], 8);
2916

            
2917
test 'type_rule and filter order';
2918
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2919
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2920
$dbi->type_rule(
2921
    into1 => {
2922
        date => sub { $_[0] . 'b' }
2923
    },
2924
    into2 => {
2925
        date => sub { $_[0] . 'c' }
2926
    },
2927
    from1 => {
2928
        date => sub { $_[0] . 'd' }
2929
    },
2930
    from2 => {
2931
        date => sub { $_[0] . 'e' }
2932
    }
2933
);
2934
$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }});
2935
$result = $dbi->select(table => 'table1');
2936
$result->filter(key1 => sub { $_[0] . 'f' });
2937
is($result->fetch_first->[0], '1abcdef');
2938

            
2939
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2940
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2941
$dbi->type_rule(
2942
    from1 => {
2943
        date => sub { $_[0] . 'p' }
2944
    },
2945
    from2 => {
2946
        date => sub { $_[0] . 'q' }
2947
    },
2948
);
2949
$dbi->insert({key1 => '1'}, table => 'table1');
2950
$result = $dbi->select(table => 'table1');
2951
$result->type_rule(
2952
    from1 => {
2953
        date => sub { $_[0] . 'd' }
2954
    },
2955
    from2 => {
2956
        date => sub { $_[0] . 'e' }
2957
    }
2958
);
2959
$result->filter(key1 => sub { $_[0] . 'f' });
2960
is($result->fetch_first->[0], '1def');
2961

            
2962
test 'type_rule_off';
2963
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2964
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2965
$dbi->type_rule(
2966
    from1 => {
2967
        date => sub { $_[0] * 2 },
2968
    },
2969
    into1 => {
2970
        date => sub { $_[0] * 2 },
2971
    }
2972
);
2973
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
2974
$result = $dbi->select(table => 'table1', type_rule_off => 1);
2975
is($result->type_rule_off->fetch->[0], 2);
2976

            
2977
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2978
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2979
$dbi->type_rule(
2980
    from1 => {
2981
        date => sub { $_[0] * 2 },
2982
    },
2983
    into1 => {
2984
        date => sub { $_[0] * 3 },
2985
    }
2986
);
2987
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
2988
$result = $dbi->select(table => 'table1', type_rule_off => 1);
2989
is($result->one->{key1}, 4);
2990

            
2991
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2992
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2993
$dbi->type_rule(
2994
    from1 => {
2995
        date => sub { $_[0] * 2 },
2996
    },
2997
    into1 => {
2998
        date => sub { $_[0] * 3 },
2999
    }
3000
);
3001
$dbi->insert({key1 => 2}, table => 'table1');
3002
$result = $dbi->select(table => 'table1');
3003
is($result->one->{key1}, 12);
3004

            
3005
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
3006
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3007
$dbi->type_rule(
3008
    from1 => {
3009
        date => sub { $_[0] * 2 },
3010
    },
3011
    into1 => {
3012
        date => sub { $_[0] * 3 },
3013
    }
3014
);
3015
$dbi->insert({key1 => 2}, table => 'table1');
3016
$result = $dbi->select(table => 'table1');
3017
is($result->fetch->[0], 12);
3018

            
3019
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
3020
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3021
$dbi->register_filter(ppp => sub { uc $_[0] });
3022
$dbi->type_rule(
3023
    into1 => {
3024
        date => 'ppp'
3025
    }
3026
);
3027
$dbi->insert({key1 => 'a'}, table => 'table1');
3028
$result = $dbi->select(table => 'table1');
3029
is($result->one->{key1}, 'A');
3030

            
3031
eval{$dbi->type_rule(
3032
    into1 => {
3033
        date => 'pp'
3034
    }
3035
)};
3036
like($@, qr/not registered/);
3037

            
3038
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
3039
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3040
eval {
3041
    $dbi->type_rule(
3042
        from1 => {
3043
            Date => sub { $_[0] * 2 },
3044
        }
3045
    );
3046
};
3047
like($@, qr/lower/);
3048

            
3049
eval {
3050
    $dbi->type_rule(
3051
        into1 => {
3052
            Date => sub { $_[0] * 2 },
3053
        }
3054
    );
3055
};
3056
like($@, qr/lower/);
3057

            
3058
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
3059
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3060
$dbi->type_rule(
3061
    from1 => {
3062
        date => sub { $_[0] * 2 },
3063
    },
3064
    into1 => {
3065
        date => sub { $_[0] * 3 },
3066
    }
3067
);
3068
$dbi->insert({key1 => 2}, table => 'table1');
3069
$result = $dbi->select(table => 'table1');
3070
$result->type_rule_off;
3071
is($result->one->{key1}, 6);
3072

            
3073
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
3074
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3075
$dbi->type_rule(
3076
    from1 => {
3077
        date => sub { $_[0] * 2 },
3078
        datetime => sub { $_[0] * 4 },
3079
    },
3080
);
3081
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
3082
$result = $dbi->select(table => 'table1');
3083
$result->type_rule(
3084
    from1 => {
3085
        date => sub { $_[0] * 3 }
3086
    }
3087
);
3088
$row = $result->one;
3089
is($row->{key1}, 6);
3090
is($row->{key2}, 2);
3091

            
3092
$result = $dbi->select(table => 'table1');
3093
$result->type_rule(
3094
    from1 => {
3095
        date => sub { $_[0] * 3 }
3096
    }
3097
);
3098
$row = $result->one;
3099
is($row->{key1}, 6);
3100
is($row->{key2}, 2);
3101

            
3102
$result = $dbi->select(table => 'table1');
3103
$result->type_rule(
3104
    from1 => {
3105
        date => sub { $_[0] * 3 }
3106
    }
3107
);
3108
$row = $result->one;
3109
is($row->{key1}, 6);
3110
is($row->{key2}, 2);
3111
$result = $dbi->select(table => 'table1');
3112
$result->type_rule(
3113
    from1 => [date => sub { $_[0] * 3 }]
3114
);
3115
$row = $result->one;
3116
is($row->{key1}, 6);
3117
is($row->{key2}, 2);
3118
$dbi->register_filter(fivetimes => sub { $_[0] * 5});
3119
$result = $dbi->select(table => 'table1');
3120
$result->type_rule(
3121
    from1 => [date => 'fivetimes']
3122
);
3123
$row = $result->one;
3124
is($row->{key1}, 10);
3125
is($row->{key2}, 2);
3126
$result = $dbi->select(table => 'table1');
3127
$result->type_rule(
3128
    from1 => [date => undef]
3129
);
3130
$row = $result->one;
3131
is($row->{key1}, 2);
3132
is($row->{key2}, 2);
3133

            
3134
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
3135
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3136
$dbi->type_rule(
3137
    from1 => {
3138
        date => sub { $_[0] * 2 },
3139
    },
3140
);
3141
$dbi->insert({key1 => 2}, table => 'table1');
3142
$result = $dbi->select(table => 'table1');
3143
$result->filter(key1 => sub { $_[0] * 3 });
3144
is($result->one->{key1}, 12);
3145

            
3146
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
3147
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3148
$dbi->type_rule(
3149
    from1 => {
3150
        date => sub { $_[0] * 2 },
3151
    },
3152
);
3153
$dbi->insert({key1 => 2}, table => 'table1');
3154
$result = $dbi->select(table => 'table1');
3155
$result->filter(key1 => sub { $_[0] * 3 });
3156
is($result->fetch->[0], 12);
3157

            
3158
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
3159
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3160
$dbi->type_rule(
3161
    into1 => {
3162
        date => sub { $_[0] . 'b' }
3163
    },
3164
    into2 => {
3165
        date => sub { $_[0] . 'c' }
3166
    },
3167
    from1 => {
3168
        date => sub { $_[0] . 'd' }
3169
    },
3170
    from2 => {
3171
        date => sub { $_[0] . 'e' }
3172
    }
3173
);
3174
$dbi->insert({key1 => '1'}, table => 'table1', type_rule_off => 1);
3175
$result = $dbi->select(table => 'table1');
3176
is($result->type_rule_off->fetch_first->[0], '1');
3177
$result = $dbi->select(table => 'table1');
3178
is($result->type_rule_on->fetch_first->[0], '1de');
3179

            
3180
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
3181
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3182
$dbi->type_rule(
3183
    into1 => {
3184
        date => sub { $_[0] . 'b' }
3185
    },
3186
    into2 => {
3187
        date => sub { $_[0] . 'c' }
3188
    },
3189
    from1 => {
3190
        date => sub { $_[0] . 'd' }
3191
    },
3192
    from2 => {
3193
        date => sub { $_[0] . 'e' }
3194
    }
3195
);
3196
$dbi->insert({key1 => '1'}, table => 'table1', type_rule1_off => 1);
3197
$result = $dbi->select(table => 'table1');
3198
is($result->type_rule1_off->fetch_first->[0], '1ce');
3199
$result = $dbi->select(table => 'table1');
3200
is($result->type_rule1_on->fetch_first->[0], '1cde');
3201

            
3202
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
3203
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3204
$dbi->type_rule(
3205
    into1 => {
3206
        date => sub { $_[0] . 'b' }
3207
    },
3208
    into2 => {
3209
        date => sub { $_[0] . 'c' }
3210
    },
3211
    from1 => {
3212
        date => sub { $_[0] . 'd' }
3213
    },
3214
    from2 => {
3215
        date => sub { $_[0] . 'e' }
3216
    }
3217
);
3218
$dbi->insert({key1 => '1'}, table => 'table1', type_rule2_off => 1);
3219
$result = $dbi->select(table => 'table1');
3220
is($result->type_rule2_off->fetch_first->[0], '1bd');
3221
$result = $dbi->select(table => 'table1');
3222
is($result->type_rule2_on->fetch_first->[0], '1bde');
3223

            
3224
test 'separator';
cleanup test
Yuki Kimoto authored on 2011-08-06
3225
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
3226
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
3227
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
3228

            
3229
$dbi->create_model(
3230
    table => 'table1',
3231
    join => [
3232
       'left outer join table2 on table1.key1 = table2.key1'
3233
    ],
3234
    primary_key => ['key1'],
3235
);
3236
$model2 = $dbi->create_model(
3237
    table => 'table2',
3238
);
3239
$dbi->setup_model;
3240
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3241
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
3242
$model = $dbi->model('table1');
3243
$result = $model->select(
3244
    column => [
3245
        $model->mycolumn,
3246
        {table2 => [qw/key1 key3/]}
3247
    ],
3248
    where => {'table1.key1' => 1}
3249
);
3250
is_deeply($result->one,
3251
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
3252
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3253

            
3254
$dbi->separator('__');
3255
$model = $dbi->model('table1');
3256
$result = $model->select(
3257
    column => [
3258
        $model->mycolumn,
3259
        {table2 => [qw/key1 key3/]}
3260
    ],
3261
    where => {'table1.key1' => 1}
3262
);
3263
is_deeply($result->one,
3264
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
3265
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3266

            
3267
$dbi->separator('-');
3268
$model = $dbi->model('table1');
3269
$result = $model->select(
3270
    column => [
3271
        $model->mycolumn,
3272
        {table2 => [qw/key1 key3/]}
3273
    ],
3274
    where => {'table1.key1' => 1}
3275
);
3276
is_deeply($result->one,
3277
          {key1 => 1, key2 => 2, 'table2-key1' => 1, 'table2-key3' => 3});
3278
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3279

            
3280

            
3281
test 'filter_off';
cleanup test
Yuki Kimoto authored on 2011-08-06
3282
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
3283
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
3284
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
3285

            
3286
$dbi->create_model(
3287
    table => 'table1',
3288
    join => [
3289
       'left outer join table2 on table1.key1 = table2.key1'
3290
    ],
3291
    primary_key => ['key1'],
3292
);
3293
$dbi->setup_model;
3294
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3295
$model = $dbi->model('table1');
3296
$result = $model->select(column => 'key1');
3297
$result->filter(key1 => sub { $_[0] * 2 });
3298
is_deeply($result->one, {key1 => 2});
3299

            
3300
test 'available_date_type';
cleanup test
Yuki Kimoto authored on 2011-08-06
3301
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
3302
ok($dbi->can('available_data_type'));
3303

            
3304

            
3305
test 'select prefix option';
cleanup test
Yuki Kimoto authored on 2011-08-06
3306
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
3307
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
3308
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3309
$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all;
3310
is_deeply($rows, [{key1 => 1, key2 => 2}], "table");
3311

            
3312

            
3313
test 'separator';
cleanup test
Yuki Kimoto authored on 2011-08-06
3314
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
3315
is($dbi->separator, '.');
3316
$dbi->separator('-');
3317
is($dbi->separator, '-');
3318
$dbi->separator('__');
3319
is($dbi->separator, '__');
3320
eval { $dbi->separator('?') };
3321
like($@, qr/Separator/);
3322

            
3323

            
3324
test 'map_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
3325
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
3326
$param = $dbi->map_param(
3327
    {id => 1, author => 'Ken', price => 1900},
3328
    id => 'book.id',
3329
    author => ['book.author', sub { '%' . $_[0] . '%' }],
3330
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
3331
);
3332
is_deeply($param, {'book.id' => 1, 'book.author' => '%Ken%',
3333
  'book.price' => 1900});
3334

            
3335
$param = $dbi->map_param(
3336
    {id => 0, author => 0, price => 0},
3337
    id => 'book.id',
3338
    author => ['book.author', sub { '%' . $_[0] . '%' }],
3339
    price => ['book.price', sub { '%' . $_[0] . '%' },
3340
      {if => sub { $_[0] eq 0 }}]
3341
);
3342
is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
3343

            
3344
$param = $dbi->map_param(
3345
    {id => '', author => '', price => ''},
3346
    id => 'book.id',
3347
    author => ['book.author', sub { '%' . $_[0] . '%' }],
3348
    price => ['book.price', sub { '%' . $_[0] . '%' },
3349
      {if => sub { $_[0] eq 1 }}]
3350
);
3351
is_deeply($param, {});
3352

            
3353
$param = $dbi->map_param(
3354
    {id => undef, author => undef, price => undef},
3355
    id => 'book.id',
3356
    price => ['book.price', {if => 'exists'}]
3357
);
3358
is_deeply($param, {'book.price' => undef});
3359

            
3360
$param = $dbi->map_param(
3361
    {price => 'a'},
3362
    id => ['book.id', {if => 'exists'}],
3363
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
3364
);
3365
is_deeply($param, {'book.price' => '%a'});
3366

            
3367

            
3368
test 'table_alias';
3369
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
3370
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3371
$dbi->type_rule(
3372
    into1 => {
3373
        date => sub { uc $_[0] }
3374
    }
3375
);
3376
$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'},
3377
  table_alias => {table2 => 'table1'});
3378
$result = $dbi->select(table => 'table1');
3379
is($result->one->{key1}, 'A');
3380

            
3381

            
3382
test 'order';
3383
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
3384
{
3385
    $dbi->execute("create table table1 (key1, key2)");
3386
    $dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
3387
    $dbi->insert({key1 => 1, key2 => 3}, table => 'table1');
3388
    $dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
3389
    $dbi->insert({key1 => 2, key2 => 4}, table => 'table1');
3390
    my $order = $dbi->order;
3391
    $order->prepend('key1', 'key2 desc');
3392
    $result = $dbi->select(table => 'table1', append => "$order");
3393
    is_deeply($result->all, [{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1},
3394
      {key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}]);
3395
    $order->prepend('key1 desc');
3396
    $result = $dbi->select(table => 'table1', append => "$order");
3397
    is_deeply($result->all, [{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2},
3398
      {key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}]);
3399

            
3400
    $order = $dbi->order;
3401
    $order->prepend(['table1-key1'], [qw/table1-key2 desc/]);
3402
    $result = $dbi->select(table => 'table1',
3403
      column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']],
3404
      append => "$order");
3405
    is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3},
3406
      {'table1-key1' => 1, 'table1-key2' => 1},
3407
      {'table1-key1' => 2, 'table1-key2' => 4},
3408
      {'table1-key1' => 2, 'table1-key2' => 2}]);
3409
}
3410

            
3411
test 'tag_parse';
3412
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
3413
$dbi->tag_parse(0);
3414
{
3415
    $dbi->execute("create table table1 (key1, key2)");
3416
    $dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
3417
    eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})};
3418
    ok($@);
3419
}
3420

            
3421
test 'last_sql';
3422
{
3423
    my $dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
3424
    $dbi->execute("create table table1 (key1, key2)");
3425
    $dbi->execute('select * from table1');
3426
    is($dbi->last_sql, 'select * from table1;');
3427
    
3428
    eval{$dbi->execute("aaa")};
3429
    is($dbi->last_sql, 'aaa;');
3430
    
3431
}
3432

            
3433
test 'DBIx::Custom header';
3434
{
3435
    my $dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
3436
    $dbi->execute("create table table1 (key1, key2)");
3437
    my $result = $dbi->execute('select key1 as h1, key2 as h2 from table1');
3438
    
3439
    is_deeply($result->header, [qw/h1 h2/]);
3440
    
3441
}
3442

            
3443
test 'Named placeholder :name(operater) syntax';
cleanup test
Yuki Kimoto authored on 2011-08-06
3444
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
3445
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
3446
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3447
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3448

            
3449
$source = "select * from table1 where :key1{=} and :key2{=}";
3450
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
3451
$rows = $result->all;
3452
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3453

            
3454
$source = "select * from table1 where :key1{ = } and :key2{=}";
3455
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
3456
$rows = $result->all;
3457
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3458

            
3459
$source = "select * from table1 where :key1{<} and :key2{=}";
3460
$result = $dbi->execute($source, param => {key1 => 5, key2 => 2});
3461
$rows = $result->all;
3462
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3463

            
3464
$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}";
3465
$result = $dbi->execute(
3466
    $source,
3467
    param => {'table1.key1' => 1, 'table1.key2' => 1},
3468
    filter => {'table1.key2' => sub { $_[0] * 2 }}
3469
);
3470
$rows = $result->all;
3471
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3472

            
3473
test 'high perfomance way';
cleanup test
Yuki Kimoto authored on 2011-08-06
3474
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
3475
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
3476
$rows = [
3477
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3478
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3479
];
3480
{
3481
    my $query;
3482
    foreach my $row (@$rows) {
3483
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
3484
      $dbi->execute($query, $row, filter => {ab => sub { $_[0] * 2 }});
3485
    }
3486
    is_deeply($dbi->select(table => 'table1')->all,
3487
      [
3488
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3489
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3490
      ]
3491
    );
3492
}
3493

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3494
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
3495
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
3496
$rows = [
3497
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3498
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3499
];
3500
{
3501
    my $query;
3502
    my $sth;
3503
    foreach my $row (@$rows) {
3504
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
3505
      $sth ||= $query->sth;
3506
      $sth->execute(map { $row->{$_} } sort keys %$row);
3507
    }
3508
    is_deeply($dbi->select(table => 'table1')->all,
3509
      [
3510
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3511
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3512
      ]
3513
    );
3514
}
3515
=cut