DBIx-Custom / t / basic.t /
Newer Older
3509 lines | 122.342kb
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
# Variables
23
my $dbi;
24
my $sth;
25
my $source;
26
my @sources;
27
my $select_SOURCE;
28
my $insert_SOURCE;
29
my $update_SOURCE;
30
my $param;
31
my $params;
32
my $sql;
33
my $result;
34
my $row;
35
my @rows;
36
my $rows;
37
my $query;
38
my @queries;
39
my $select_query;
40
my $insert_query;
41
my $update_query;
42
my $ret_val;
43
my $infos;
44
my $model;
45
my $model2;
46
my $where;
47
my $update_param;
48
my $insert_param;
cleanup test
Yuki Kimoto authored on 2011-08-06
49
my $join;
cleanup test
Yuki Kimoto authored on 2011-08-06
50

            
51
# Prepare table
cleanup test
Yuki Kimoto authored on 2011-08-06
52
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
53
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
54
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
55
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
56

            
57
test 'DBIx::Custom::Result test';
58
$source = "select key1, key2 from table1";
59
$query = $dbi->create_query($source);
60
$result = $dbi->execute($query);
61

            
62
@rows = ();
63
while (my $row = $result->fetch) {
64
    push @rows, [@$row];
65
}
66
is_deeply(\@rows, [[1, 2], [3, 4]], "fetch");
67

            
68
$result = $dbi->execute($query);
69
@rows = ();
70
while (my $row = $result->fetch_hash) {
71
    push @rows, {%$row};
72
}
73
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "fetch_hash");
74

            
75
$result = $dbi->execute($query);
76
$rows = $result->fetch_all;
77
is_deeply($rows, [[1, 2], [3, 4]], "fetch_all");
78

            
79
$result = $dbi->execute($query);
80
$rows = $result->fetch_hash_all;
81
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "all");
82

            
83
test 'Insert query return value';
cleanup test
Yuki Kimoto authored on 2011-08-06
84
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
85
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
86
$source = "insert into table1 {insert_param key1 key2}";
87
$query = $dbi->execute($source, {}, query => 1);
88
$ret_val = $dbi->execute($query, param => {key1 => 1, key2 => 2});
89
ok($ret_val);
90

            
91

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

            
101
test 'Filter basic';
cleanup test
Yuki Kimoto authored on 2011-08-06
102
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
103
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
104
$dbi->register_filter(twice       => sub { $_[0] * 2}, 
105
                    three_times => sub { $_[0] * 3});
106

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

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

            
128
test 'DBIx::Custom::SQLTemplate basic tag';
cleanup test
Yuki Kimoto authored on 2011-08-06
129
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
130
$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
131
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
132
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
133

            
134
$source = "select * from table1 where key1 = :key1 and {<> key2} and {< key3} and {> key4} and {>= key5};";
135
$query = $dbi->execute($source, {}, query => 1);
136
$result = $dbi->execute($query, param => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
137
$rows = $result->all;
138
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1");
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, {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} and {like key2};";
147
$query = $dbi->execute($source, {}, query => 1);
148
$result = $dbi->execute($query, param => {key1 => 1, key2 => '%2%'});
149
$rows = $result->all;
150
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag2");
151

            
152
test 'DIB::Custom::SQLTemplate in tag';
cleanup test
Yuki Kimoto authored on 2011-08-06
153
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
154
$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
155
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
156
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
157

            
158
$source = "select * from table1 where {in key1 2};";
159
$query = $dbi->execute($source, {}, query => 1);
160
$result = $dbi->execute($query, param => {key1 => [9, 1]});
161
$rows = $result->all;
162
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic");
163

            
164
test 'DBIx::Custom::SQLTemplate insert tag';
165
$dbi->execute("delete from table1");
166
$insert_SOURCE = 'insert into table1 {insert_param key1 key2 key3 key4 key5}';
167
$dbi->execute($insert_SOURCE, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
168

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

            
173
test 'DBIx::Custom::SQLTemplate update tag';
174
$dbi->execute("delete from table1");
175
$insert_SOURCE = "insert into table1 {insert_param key1 key2 key3 key4 key5}";
176
$dbi->execute($insert_SOURCE, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
177
$dbi->execute($insert_SOURCE, param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
178

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

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

            
187

            
188
test 'Named placeholder';
cleanup test
Yuki Kimoto authored on 2011-08-06
189
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
190
$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
191
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
192
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
193

            
194
$source = "select * from table1 where key1 = :key1 and key2 = :key2";
195
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
196
$rows = $result->all;
197
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
198

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

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

            
209
$source = "select * from table1 where key1 = :table1.key1 and key2 = :table1.key2";
210
$result = $dbi->execute(
211
    $source,
212
    param => {'table1.key1' => 1, 'table1.key2' => 1},
213
    filter => {'table1.key2' => sub { $_[0] * 2 }}
214
);
215
$rows = $result->all;
216
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
217

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

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

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

            
241

            
242
test 'Error case';
243
eval {DBIx::Custom->connect(dsn => 'dbi:SQLit')};
244
ok($@, "connect error");
245

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
271
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
272
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
273
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, append => '   ');
274
$rows = $dbi->select(table => 'table1')->all;
275
is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append');
276

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

            
280
eval{$dbi->insert(table => 'table', param => {';' => 1})};
281
like($@, qr/safety/);
282

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
292
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
293
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
294
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
295
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
test cleanup
Yuki Kimoto authored on 2011-08-06
296
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
297
$rows   = $result->all;
298
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
299

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

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

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

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

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

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

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

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

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

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

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

            
399
eval{$dbi->update(table => 'table1', param => {';' => 1})};
400
like($@, qr/safety/);
401

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

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

            
416
eval {$dbi->update_all(table => 'table', param => {';' => 2}) };
417
like($@, qr/safety/);
418

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
430
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
431
$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
432
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
433
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
434
$dbi->update({key2 => 11}, table => 'table1', where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
435
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
436
$rows   = $result->all;
437
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
438
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
439
                  "basic");
440

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
450
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
451
$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
452
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
453
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
454
$dbi->update(table => 'table1', param => {key2 => \"'11'"}, where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
455
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
456
$rows   = $result->all;
457
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
458
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
459
                  "basic");
460

            
461
test 'update_all';
cleanup test
Yuki Kimoto authored on 2011-08-06
462
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
463
$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
464
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
465
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
466
$dbi->register_filter(twice => sub { $_[0] * 2 });
467
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'});
test cleanup
Yuki Kimoto authored on 2011-08-06
468
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
469
$rows   = $result->all;
470
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
471
                  {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
472
                  "filter");
473

            
474

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

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

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

            
496
$dbi->delete_all(table => 'table1');
497
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
498
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
499
$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2});
500
$rows = $dbi->select(table => 'table1')->all;
501
is_deeply($rows, [{key1 => 3, key2 => 4}], "delete multi key");
502

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

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

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

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

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

            
546
eval{$dbi->delete(table => 'table1', where => {';' => 1})};
547
like($@, qr/safety/);
548

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

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

            
569

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

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

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

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

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

            
591
$dbi->register_filter(decrement => sub { $_[0] - 1 });
592
$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'})
593
            ->all;
594
is_deeply($rows, [{key1 => 1, key2 => 2}], "filter");
595

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

            
606
$rows = $dbi->select(
607
    table => [qw/table1 table2/],
608
    column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
609
    relation  => {'table1.key1' => 'table2.key1'}
610
)->all;
611
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : no exists where");
612

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

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

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

            
639
test 'filters';
640
$dbi = DBIx::Custom->new;
641

            
642
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')),
643
   'あ', "decode_utf8");
644

            
645
is($dbi->filters->{encode_utf8}->('あ'),
646
   encode_utf8('あ'), "encode_utf8");
647

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
659
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
660
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
661
$dbi->dbh->begin_work(0);
662
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
663
$dbi->dbh->rollback;
664

            
665
$result = $dbi->select(table => 'table1');
666
ok(! $result->fetch_first, "rollback");
667

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

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

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

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

            
702
$query = $dbi->execute('select * from table1 where key1 = :key1', {}, query => 1);
703
$dbi->dbh->disconnect;
704
eval{$dbi->execute($query, param => {key1 => {a => 1}})};
705
ok($@, "execute fail");
706

            
707
{
708
    local $Carp::Verbose = 0;
709
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
710
    like($@, qr/\Q.t /, "caller spec : not vebose");
711
}
712
{
713
    local $Carp::Verbose = 1;
714
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
715
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
716
}
717

            
718

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

            
723
$dbi->begin_work;
724

            
725
eval {
726
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
727
    die "Error";
728
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
729
};
730

            
731
$dbi->rollback if $@;
732

            
733
$result = $dbi->select(table => 'table1');
734
$rows = $result->all;
735
is_deeply($rows, [], "rollback");
736

            
737
$dbi->begin_work;
738

            
739
eval {
740
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
741
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
742
};
743

            
744
$dbi->commit unless $@;
745

            
746
$result = $dbi->select(table => 'table1');
747
$rows = $result->all;
748
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "commit");
749

            
750
$dbi->dbh->{AutoCommit} = 0;
751
eval{ $dbi->begin_work };
752
ok($@, "exception");
753
$dbi->dbh->{AutoCommit} = 1;
754

            
755

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

            
771
is($dbi->one, 1, "first");
772
is($dbi->two, 2, "second");
773
is($dbi->twice(5), 10 , "second");
774

            
775
eval {$dbi->XXXXXX};
776
ok($@, "not exists");
777

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

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

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

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

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

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

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

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

            
888
$result->filter({'key2' => 'twice'});
889
$rows   = $result->all;
890
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join");
891

            
892
$result = $dbi->select(
893
     table => ['table1', 'table2'],
894
     column => ['key2', 'key3'],
895
     where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
896

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

            
901
test 'each_column';
cleanup test
Yuki Kimoto authored on 2011-08-06
902
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
903
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
904
$dbi->execute('create table table1 (key1 Date, key2 datetime);');
cleanup test
Yuki Kimoto authored on 2011-08-06
905

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

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

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

            
983
test 'connect super';
984
{
985
    package MyDBI;
986
    
987
    use base 'DBIx::Custom';
988
    sub connect {
989
        my $self = shift->SUPER::connect(@_);
990
        
991
        return $self;
992
    }
993
    
994
    sub new {
995
        my $self = shift->SUPER::new(@_);
996
        
997
        return $self;
998
    }
999
}
1000

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1006
$dbi = MyDBI->new(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1007
$dbi->connect;
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

            
1012
{
1013
    package MyDBI2;
1014
    
1015
    use base 'DBIx::Custom';
1016
    sub connect {
1017
        my $self = shift->SUPER::new(@_);
1018
        $self->connect;
1019
        
1020
        return $self;
1021
    }
1022
}
1023

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

            
1029
test 'end_filter';
cleanup test
Yuki Kimoto authored on 2011-08-06
1030
$dbi = DBIx::Custom->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
$result = $dbi->select(table => 'table1');
1034
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1035
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 });
1036
$row = $result->fetch_first;
1037
is_deeply($row, [6, 40]);
1038

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

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

            
1057
$dbi->register_filter(five_times => sub { $_[0] * 5 });
1058
$result = $dbi->select(table => 'table1');
1059
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1060
$result->end_filter({key1 => sub { $_[0] * 3 }, key2 => 'five_times' });
1061
$row = $result->one;
1062
is_deeply($row, {key1 => 6, key2 => 40});
1063

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

            
1074
$dbi->register_filter(five_times => sub { $_[0] * 5 });
1075
$dbi->apply_filter('table1',
1076
    key1 => {end => sub { $_[0] * 3 } },
1077
    key2 => {end => 'five_times'}
1078
);
1079
$result = $dbi->select(table => 'table1');
1080
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1081
$result->filter(key1 => undef);
1082
$result->end_filter(key1 => undef);
1083
$row = $result->one;
1084
is_deeply($row, {key1 => 1, key2 => 40}, 'apply_filter overwrite');
1085

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

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

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

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

            
1127
$where = $dbi->where
1128
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
1129
             ->param({key1 => 1});
1130

            
1131
$result = $dbi->select(
1132
    table => 'table1',
1133
    where => $where
1134
);
1135
$row = $result->all;
1136
is_deeply($row, [{key1 => 1, key2 => 2}]);
1137

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

            
1148
$where = $dbi->where
1149
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
1150
             ->param({key1 => 1, key2 => 2});
1151
$result = $dbi->select(
1152
    table => 'table1',
1153
    where => $where
1154
);
1155
$row = $result->all;
1156
is_deeply($row, [{key1 => 1, key2 => 2}]);
1157

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

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

            
1178
$where = $dbi->where;
1179
$result = $dbi->select(
1180
    table => 'table1',
1181
    where => $where
1182
);
1183
$row = $result->all;
1184
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1185

            
1186
eval {
1187
$where = $dbi->where
1188
             ->clause(['uuu']);
1189
$result = $dbi->select(
1190
    table => 'table1',
1191
    where => $where
1192
);
1193
};
1194
ok($@);
1195

            
1196
$where = $dbi->where;
1197
is("$where", '');
1198

            
1199
$where = $dbi->where
1200
             ->clause(['or', ('key1 = :key1') x 2])
1201
             ->param({key1 => [1, 3]});
1202
$result = $dbi->select(
1203
    table => 'table1',
1204
    where => $where,
1205
);
1206
$row = $result->all;
1207
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1208

            
1209
$where = $dbi->where
1210
             ->clause(['or', ('key1 = :key1') x 2])
1211
             ->param({key1 => [1]});
1212
$result = $dbi->select(
1213
    table => 'table1',
1214
    where => $where,
1215
);
1216
$row = $result->all;
1217
is_deeply($row, [{key1 => 1, key2 => 2}]);
1218

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

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

            
1239
$where = $dbi->where
1240
             ->clause('key1 = :key1 key2 = :key2')
1241
             ->param({key1 => 1});
1242
eval{$where->to_string};
1243
like($@, qr/one column/);
1244

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

            
1251
$where = $dbi->where
1252
             ->clause(['or', ('key1 = :key1') x 3])
1253
             ->param({key1 => [$dbi->not_exists, 1, 3]});
1254
$result = $dbi->select(
1255
    table => 'table1',
1256
    where => $where,
1257
);
1258
$row = $result->all;
1259
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1260

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

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

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

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

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

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

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

            
1331
$where = $dbi->where
1332
             ->clause(['and', '{> key1}', '{< key1}' ])
1333
             ->param({key1 => [2, $dbi->not_exists]});
1334
$result = $dbi->select(
1335
    table => 'table1',
1336
    where => $where,
1337
);
1338
$row = $result->all;
1339
is_deeply($row, [{key1 => 3, key2 => 4}], 'not_exists');
1340

            
1341
$where = $dbi->where
1342
             ->clause(['and', '{> key1}', '{< key1}' ])
1343
             ->param({key1 => [$dbi->not_exists, 2]});
1344
$result = $dbi->select(
1345
    table => 'table1',
1346
    where => $where,
1347
);
1348
$row = $result->all;
1349
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1350

            
1351
$where = $dbi->where
1352
             ->clause(['and', '{> key1}', '{< key1}' ])
1353
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists]});
1354
$result = $dbi->select(
1355
    table => 'table1',
1356
    where => $where,
1357
);
1358
$row = $result->all;
1359
is_deeply($row, [{key1 => 1, key2 => 2},{key1 => 3, key2 => 4}], 'not_exists');
1360

            
1361
$where = $dbi->where
1362
             ->clause(['and', '{> key1}', '{< key1}' ])
1363
             ->param({key1 => [0, 2]});
1364
$result = $dbi->select(
1365
    table => 'table1',
1366
    where => $where,
1367
);
1368
$row = $result->all;
1369
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1370

            
1371
$where = $dbi->where
1372
             ->clause(['and', 'key1 is not null', 'key2 is not null' ]);
1373
$result = $dbi->select(
1374
    table => 'table1',
1375
    where => $where,
1376
);
1377
$row = $result->all;
1378
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1379

            
1380
eval {$dbi->where(ppp => 1) };
1381
like($@, qr/invalid/);
1382

            
1383
$where = $dbi->where(
1384
    clause => ['and', ['or'], ['and', 'key1 = :key1', 'key2 = :key2']],
1385
    param => {key1 => 1, key2 => 2}
1386
);
1387
$result = $dbi->select(
1388
    table => 'table1',
1389
    where => $where,
1390
);
1391
$row = $result->all;
1392
is_deeply($row, [{key1 => 1, key2 => 2}]);
1393

            
1394

            
1395
$where = $dbi->where(
1396
    clause => ['and', ['or'], ['or', ':key1', ':key2']],
1397
    param => {}
1398
);
1399
$result = $dbi->select(
1400
    table => 'table1',
1401
    where => $where,
1402
);
1403
$row = $result->all;
1404
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1405

            
1406

            
1407
test 'dbi_option default';
1408
$dbi = DBIx::Custom->new;
1409
is_deeply($dbi->dbi_option, {});
1410

            
1411
test 'register_tag_processor';
cleanup test
Yuki Kimoto authored on 2011-08-06
1412
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1413
$dbi->register_tag_processor(
1414
    a => sub { 1 }
1415
);
1416
is($dbi->query_builder->tag_processors->{a}->(), 1);
1417

            
1418
test 'register_tag';
cleanup test
Yuki Kimoto authored on 2011-08-06
1419
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1420
$dbi->register_tag(
1421
    b => sub { 2 }
1422
);
1423
is($dbi->query_builder->tags->{b}->(), 2);
1424

            
1425
test 'table not specify exception';
cleanup test
Yuki Kimoto authored on 2011-08-06
1426
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1427
eval {$dbi->insert};
1428
like($@, qr/table/);
1429
eval {$dbi->update};
1430
like($@, qr/table/);
1431
eval {$dbi->delete};
1432
like($@, qr/table/);
1433
eval {$dbi->select};
1434
like($@, qr/table/);
1435

            
1436

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

            
1442
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
1443
like($@, qr/apply_filter/);
1444

            
1445
$dbi->apply_filter(
1446

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

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

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

            
1473
eval{DBIx::Custom->connect()};
1474
like($@, qr/_connect/);
1475

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

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

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

            
1516
test 'dbi_option';
1517
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:',
1518
                             dbi_option => {PrintError => 1});
1519
ok($dbi->dbh->{PrintError});
1520
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:',
1521
                             dbi_options => {PrintError => 1});
1522
ok($dbi->dbh->{PrintError});
1523

            
1524
test 'DBIx::Custom::Result stash()';
1525
$result = DBIx::Custom::Result->new;
1526
is_deeply($result->stash, {}, 'default');
1527
$result->stash->{foo} = 1;
1528
is($result->stash->{foo}, 1, 'get and set');
1529

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

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

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

            
1547
$result = $dbi->select(
1548
    table => 'company', 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
test 'Model class';
1560
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1561
$dbi = MyDBI1->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1562
$dbi->execute("create table book (title, author)");
1563
$model = $dbi->model('book');
1564
$model->insert({title => 'a', author => 'b'});
1565
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
1566
$dbi->execute("create table company (name)");
1567
$model = $dbi->model('company');
1568
$model->insert({name => 'a'});
1569
is_deeply($model->list->all, [{name => 'a'}], 'basic');
1570
is($dbi->models->{'book'}, $dbi->model('book'));
1571
is($dbi->models->{'company'}, $dbi->model('company'));
1572

            
1573
{
1574
    package MyDBI4;
1575

            
1576
    use strict;
1577
    use warnings;
1578

            
1579
    use base 'DBIx::Custom';
1580

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

            
1592
    package MyModel2::Base1;
1593

            
1594
    use strict;
1595
    use warnings;
1596

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

            
1599
    package MyModel2::book;
1600

            
1601
    use strict;
1602
    use warnings;
1603

            
1604
    use base 'MyModel2::Base1';
1605

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

            
1612
    sub list { shift->select; }
1613

            
1614
    package MyModel2::Company;
1615

            
1616
    use strict;
1617
    use warnings;
1618

            
1619
    use base 'MyModel2::Base1';
1620

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

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

            
1639
{
1640
     package MyDBI5;
1641

            
1642
    use strict;
1643
    use warnings;
1644

            
1645
    use base 'DBIx::Custom';
1646

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

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

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

            
1677
test 'setup_model';
1678
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1679
$dbi = MyDBI1->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1680
$dbi->execute('create table book (id)');
1681
$dbi->execute('create table company (id, name);');
1682
$dbi->execute('create table test (id, name, primary key (id, name));');
1683
$dbi->setup_model;
1684
is_deeply($dbi->model('book')->columns, ['id']);
1685
is_deeply($dbi->model('company')->columns, ['id', 'name']);
1686

            
1687
test 'delete_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1688
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1689
$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
1690
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1691
$dbi->delete_at(
1692
    table => 'table1',
1693
    primary_key => ['key1', 'key2'],
1694
    where => [1, 2],
1695
);
1696
is_deeply($dbi->select(table => 'table1')->all, []);
1697

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

            
1706
test 'insert_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1707
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1708
$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
1709
$dbi->insert_at(
1710
    primary_key => ['key1', 'key2'], 
1711
    table => 'table1',
1712
    where => [1, 2],
1713
    param => {key3 => 3}
1714
);
1715
is($dbi->select(table => 'table1')->one->{key1}, 1);
1716
is($dbi->select(table => 'table1')->one->{key2}, 2);
1717
is($dbi->select(table => 'table1')->one->{key3}, 3);
1718

            
1719
$dbi->delete_all(table => 'table1');
1720
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1721
$dbi->insert_at(
1722
    primary_key => 'key1', 
1723
    table => 'table1',
1724
    where => 1,
1725
    param => {key2 => 2, key3 => 3}
1726
);
1727

            
1728
is($dbi->select(table => 'table1')->one->{key1}, 1);
1729
is($dbi->select(table => 'table1')->one->{key2}, 2);
1730
is($dbi->select(table => 'table1')->one->{key3}, 3);
1731

            
1732
eval {
1733
    $dbi->insert_at(
1734
        table => 'table1',
1735
        primary_key => ['key1', 'key2'],
1736
        where => {},
1737
        param => {key1 => 1, key2 => 2, key3 => 3},
1738
    );
1739
};
1740
like($@, qr/must be/);
1741

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1742
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1743
$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
1744
$dbi->insert_at(
1745
    {key3 => 3},
1746
    primary_key => ['key1', 'key2'], 
1747
    table => 'table1',
1748
    where => [1, 2],
1749
);
1750
is($dbi->select(table => 'table1')->one->{key1}, 1);
1751
is($dbi->select(table => 'table1')->one->{key2}, 2);
1752
is($dbi->select(table => 'table1')->one->{key3}, 3);
1753

            
1754
test 'update_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1755
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1756
$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
1757
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1758
$dbi->update_at(
1759
    table => 'table1',
1760
    primary_key => ['key1', 'key2'],
1761
    where => [1, 2],
1762
    param => {key3 => 4}
1763
);
1764
is($dbi->select(table => 'table1')->one->{key1}, 1);
1765
is($dbi->select(table => 'table1')->one->{key2}, 2);
1766
is($dbi->select(table => 'table1')->one->{key3}, 4);
1767

            
1768
$dbi->delete_all(table => 'table1');
1769
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1770
$dbi->update_at(
1771
    table => 'table1',
1772
    primary_key => 'key1',
1773
    where => 1,
1774
    param => {key3 => 4}
1775
);
1776
is($dbi->select(table => 'table1')->one->{key1}, 1);
1777
is($dbi->select(table => 'table1')->one->{key2}, 2);
1778
is($dbi->select(table => 'table1')->one->{key3}, 4);
1779

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1780
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1781
$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
1782
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1783
$dbi->update_at(
1784
    {key3 => 4},
1785
    table => 'table1',
1786
    primary_key => ['key1', 'key2'],
1787
    where => [1, 2]
1788
);
1789
is($dbi->select(table => 'table1')->one->{key1}, 1);
1790
is($dbi->select(table => 'table1')->one->{key2}, 2);
1791
is($dbi->select(table => 'table1')->one->{key3}, 4);
1792

            
1793
test 'select_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1794
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1795
$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
1796
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1797
$result = $dbi->select_at(
1798
    table => 'table1',
1799
    primary_key => ['key1', 'key2'],
1800
    where => [1, 2]
1801
);
1802
$row = $result->one;
1803
is($row->{key1}, 1);
1804
is($row->{key2}, 2);
1805
is($row->{key3}, 3);
1806

            
1807
$dbi->delete_all(table => 'table1');
1808
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1809
$result = $dbi->select_at(
1810
    table => 'table1',
1811
    primary_key => 'key1',
1812
    where => 1,
1813
);
1814
$row = $result->one;
1815
is($row->{key1}, 1);
1816
is($row->{key2}, 2);
1817
is($row->{key3}, 3);
1818

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

            
1831
eval {
1832
    $result = $dbi->select_at(
1833
        table => 'table1',
1834
        primary_key => ['key1', 'key2'],
1835
        where => {},
1836
    );
1837
};
1838
like($@, qr/must be/);
1839

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

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

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

            
1868
test 'columns';
1869
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1870
$dbi = MyDBI1->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1871
$model = $dbi->model('book');
1872

            
1873

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

            
1902
test 'model insert_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1903
$dbi = MyDBI6->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1904
$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
1905
$dbi->model('table1')->insert_at(
1906
    where => [1, 2],
1907
    param => {key3 => 3}
1908
);
1909
$result = $dbi->model('table1')->select;
1910
$row = $result->one;
1911
is($row->{key1}, 1);
1912
is($row->{key2}, 2);
1913
is($row->{key3}, 3);
1914

            
1915
test 'model update_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1916
$dbi = MyDBI6->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1917
$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
1918
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1919
$dbi->model('table1')->update_at(
1920
    where => [1, 2],
1921
    param => {key3 => 4}
1922
);
1923
$result = $dbi->model('table1')->select;
1924
$row = $result->one;
1925
is($row->{key1}, 1);
1926
is($row->{key2}, 2);
1927
is($row->{key3}, 4);
1928

            
1929
test 'model select_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1930
$dbi = MyDBI6->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1931
$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
1932
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1933
$result = $dbi->model('table1')->select_at(where => [1, 2]);
1934
$row = $result->one;
1935
is($row->{key1}, 1);
1936
is($row->{key2}, 2);
1937
is($row->{key3}, 3);
1938

            
1939

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

            
1970
test 'update_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
1971
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1972
$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
1973
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1974
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1975

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

            
1989

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1990
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
1991
$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
1992
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1993
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1994

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2008
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2009
$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
2010
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
2011
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
2012

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

            
2026
            
2027
eval { $dbi->update_param({";" => 1}) };
2028
like($@, qr/not safety/);
2029

            
2030

            
2031
test 'update_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
2032
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2033
$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
2034
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
2035
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
2036

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

            
2050

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

            
2063
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2064
$dbi->quote('"');
2065
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
2066
$param = {key1 => 1, key2 => 2};
2067
$insert_param = $dbi->insert_param($param);
2068
$sql = <<"EOS";
2069
insert into table1 $insert_param
2070
EOS
2071
$dbi->execute($sql, param => $param, table => 'table1');
2072
is($dbi->select(table => 'table1')->one->{key1}, 1);
2073
is($dbi->select(table => 'table1')->one->{key2}, 2);
2074

            
2075
eval { $dbi->insert_param({";" => 1}) };
2076
like($@, qr/not safety/);
2077

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

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

            
2096
$rows = $dbi->select(
2097
    table => 'table1',
2098
    where   => {'key1' => 1},
2099
    join  => ['left outer join table2 on table1.key1 = table2.key1']
2100
)->all;
2101
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2102

            
2103
eval {
2104
    $rows = $dbi->select(
2105
        table => 'table1',
2106
        column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
2107
        where   => {'table1.key2' => 2},
2108
        join  => {'table1.key1' => 'table2.key1'}
2109
    );
2110
};
2111
like ($@, qr/array/);
2112

            
2113
$rows = $dbi->select(
2114
    table => 'table1',
2115
    where   => {'key1' => 1},
2116
    join  => ['left outer join table2 on table1.key1 = table2.key1',
2117
              'left outer join table3 on table2.key3 = table3.key3']
2118
)->all;
2119
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2120

            
2121
$rows = $dbi->select(
2122
    column => 'table3.key4 as table3__key4',
2123
    table => 'table1',
2124
    where   => {'table1.key1' => 1},
2125
    join  => ['left outer join table2 on table1.key1 = table2.key1',
2126
              'left outer join table3 on table2.key3 = table3.key3']
2127
)->all;
2128
is_deeply($rows, [{table3__key4 => 4}]);
2129

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

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

            
2154
{
2155
    package MyDBI8;
2156
    
2157
    use base 'DBIx::Custom';
2158
    
2159
    sub connect {
2160
        my $self = shift->SUPER::connect(@_);
2161
        
2162
        $self->include_model('MyModel7');
2163
        
2164
        return $self;
2165
    }
2166
}
cleanup test
Yuki Kimoto authored on 2011-08-06
2167

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

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

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

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

            
2253
$result = $model->select_at(
2254
    column => [
2255
        $model->mycolumn(['key1']),
2256
        $model->column(table2 => ['key1'])
2257
    ]
2258
);
2259
is_deeply($result->one,
2260
          {key1 => 1, 'table2.key1' => 1});
2261
$result = $model->select_at(
2262
    column => [
2263
        $model->mycolumn(['key1']),
2264
        {table2 => ['key1']}
2265
    ]
2266
);
2267
is_deeply($result->one,
2268
          {key1 => 1, 'table2.key1' => 1});
2269

            
2270
$result = $model->select_at(
2271
    column => [
2272
        $model->mycolumn(['key1']),
2273
        ['table2.key1', as => 'table2.key1']
2274
    ]
2275
);
2276
is_deeply($result->one,
2277
          {key1 => 1, 'table2.key1' => 1});
2278

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

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

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

            
2325
$dbi->separator('__');
2326
$result = $model->select(
2327
    column => [
2328
        $model->column('table2', {alias => 'table2_alias'})
2329
    ],
2330
    where => {'table2_alias.key3' => 4}
2331
);
2332
is_deeply($result->one, 
2333
          {'table2_alias__key1' => 1, 'table2_alias__key3' => 4});
2334

            
2335
$dbi->separator('-');
2336
$result = $model->select(
2337
    column => [
2338
        $model->column('table2', {alias => 'table2_alias'})
2339
    ],
2340
    where => {'table2_alias.key3' => 4}
2341
);
2342
is_deeply($result->one, 
2343
          {'table2_alias-key1' => 1, 'table2_alias-key3' => 4});
2344

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

            
2362
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [['key1'] => DBI::SQL_BLOB]);
2363
$result = $dbi->select(table => 'table1');
2364
$row   = $result->one;
2365
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2366
$result = $dbi->execute('select length(key1) as key1_length from table1');
2367
$row = $result->one;
2368
is($row->{key1_length}, length $binary);
2369

            
2370

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

            
2388
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, bind_type => [['key1'] => DBI::SQL_BLOB]);
2389
$result = $dbi->select(table => 'table1');
2390
$row   = $result->one;
2391
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2392
$result = $dbi->execute('select length(key1) as key1_length from table1');
2393
$row = $result->one;
2394
is($row->{key1_length}, length $binary);
2395

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

            
2414
test 'create_model';
cleanup test
Yuki Kimoto authored on 2011-08-06
2415
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2416
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2417
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2418

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

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

            
2458
test 'merge_param';
2459
{
2460
    my $dbi = DBIx::Custom->new;
2461
    my $param1 = {key1 => 1, key2 => 2, key3 => 3};
2462
    my $param2 = {key1 => 1, key2 => 2};
2463
    my $param3 = {key1 => 1};
2464
    my $param = $dbi->merge_param($param1, $param2, $param3);
2465
    is_deeply($param, {key1 => [1, 1, 1], key2 => [2, 2], key3 => 3});
2466
}
2467

            
2468
{
2469
    my $dbi = DBIx::Custom->new;
2470
    my $param1 = {key1 => [1, 2], key2 => 1, key3 => [1, 2]};
2471
    my $param2 = {key1 => [3, 4], key2 => [2, 3], key3 => 3};
2472
    my $param = $dbi->merge_param($param1, $param2);
2473
    is_deeply($param, {key1 => [1, 2, 3, 4], key2 => [1, 2, 3], key3 => [1, 2, 3]});
2474
}
2475

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

            
2494

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

            
2507
eval {
2508
$dbi->select(
2509
    table => 'table1',
2510
    column => 'key1',
2511
    wrap => 'select * from ('
2512
)
2513
};
2514
like($@, qr/array/);
2515

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

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

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

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

            
2568

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

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

            
2596
test 'insert id and primary_key option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2597
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2598
$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
2599
$dbi->insert(
2600
    primary_key => ['key1', 'key2'], 
2601
    table => 'table1',
2602
    id => [1, 2],
2603
    param => {key3 => 3}
2604
);
2605
is($dbi->select(table => 'table1')->one->{key1}, 1);
2606
is($dbi->select(table => 'table1')->one->{key2}, 2);
2607
is($dbi->select(table => 'table1')->one->{key3}, 3);
2608

            
2609
$dbi->delete_all(table => 'table1');
2610
$dbi->insert(
2611
    primary_key => 'key1', 
2612
    table => 'table1',
2613
    id => 0,
2614
    param => {key2 => 2, key3 => 3}
2615
);
2616

            
2617
is($dbi->select(table => 'table1')->one->{key1}, 0);
2618
is($dbi->select(table => 'table1')->one->{key2}, 2);
2619
is($dbi->select(table => 'table1')->one->{key3}, 3);
2620

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2621
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2622
$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
2623
$dbi->insert(
2624
    {key3 => 3},
2625
    primary_key => ['key1', 'key2'], 
2626
    table => 'table1',
2627
    id => [1, 2],
2628
);
2629
is($dbi->select(table => 'table1')->one->{key1}, 1);
2630
is($dbi->select(table => 'table1')->one->{key2}, 2);
2631
is($dbi->select(table => 'table1')->one->{key3}, 3);
2632

            
2633

            
2634
test 'model insert id and primary_key option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2635
$dbi = MyDBI6->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2636
$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
2637
$dbi->model('table1')->insert(
2638
    id => [1, 2],
2639
    param => {key3 => 3}
2640
);
2641
$result = $dbi->model('table1')->select;
2642
$row = $result->one;
2643
is($row->{key1}, 1);
2644
is($row->{key2}, 2);
2645
is($row->{key3}, 3);
2646

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2647
$dbi = MyDBI6->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2648
$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
2649
$dbi->model('table1')->insert(
2650
    {key3 => 3},
2651
    id => [1, 2]
2652
);
2653
$result = $dbi->model('table1')->select;
2654
$row = $result->one;
2655
is($row->{key1}, 1);
2656
is($row->{key2}, 2);
2657
is($row->{key3}, 3);
2658

            
2659
test 'update and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2660
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2661
$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
2662
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2663
$dbi->update(
2664
    table => 'table1',
2665
    primary_key => ['key1', 'key2'],
2666
    id => [1, 2],
2667
    param => {key3 => 4}
2668
);
2669
is($dbi->select(table => 'table1')->one->{key1}, 1);
2670
is($dbi->select(table => 'table1')->one->{key2}, 2);
2671
is($dbi->select(table => 'table1')->one->{key3}, 4);
2672

            
2673
$dbi->delete_all(table => 'table1');
2674
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2675
$dbi->update(
2676
    table => 'table1',
2677
    primary_key => 'key1',
2678
    id => 0,
2679
    param => {key3 => 4}
2680
);
2681
is($dbi->select(table => 'table1')->one->{key1}, 0);
2682
is($dbi->select(table => 'table1')->one->{key2}, 2);
2683
is($dbi->select(table => 'table1')->one->{key3}, 4);
2684

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2685
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2686
$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
2687
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2688
$dbi->update(
2689
    {key3 => 4},
2690
    table => 'table1',
2691
    primary_key => ['key1', 'key2'],
2692
    id => [1, 2]
2693
);
2694
is($dbi->select(table => 'table1')->one->{key1}, 1);
2695
is($dbi->select(table => 'table1')->one->{key2}, 2);
2696
is($dbi->select(table => 'table1')->one->{key3}, 4);
2697

            
2698

            
2699
test 'model update and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2700
$dbi = MyDBI6->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2701
$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
2702
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2703
$dbi->model('table1')->update(
2704
    id => [1, 2],
2705
    param => {key3 => 4}
2706
);
2707
$result = $dbi->model('table1')->select;
2708
$row = $result->one;
2709
is($row->{key1}, 1);
2710
is($row->{key2}, 2);
2711
is($row->{key3}, 4);
2712

            
2713

            
2714
test 'delete and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2715
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2716
$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
2717
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2718
$dbi->delete(
2719
    table => 'table1',
2720
    primary_key => ['key1', 'key2'],
2721
    id => [1, 2],
2722
);
2723
is_deeply($dbi->select(table => 'table1')->all, []);
2724

            
2725
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2726
$dbi->delete(
2727
    table => 'table1',
2728
    primary_key => 'key1',
2729
    id => 0,
2730
);
2731
is_deeply($dbi->select(table => 'table1')->all, []);
2732

            
2733

            
2734
test 'model delete and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2735
$dbi = MyDBI6->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2736
$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
2737
$dbi->execute("create table table2 (key1, key2, key3)");
2738
$dbi->execute("create table table3 (key1, key2, key3)");
2739
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2740
$dbi->model('table1')->delete(id => [1, 2]);
2741
is_deeply($dbi->select(table => 'table1')->all, []);
2742
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
2743
$dbi->model('table1_1')->delete(id => [1, 2]);
2744
is_deeply($dbi->select(table => 'table1')->all, []);
2745
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
2746
$dbi->model('table1_3')->delete(id => [1, 2]);
2747
is_deeply($dbi->select(table => 'table1')->all, []);
2748

            
2749

            
2750
test 'select and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2751
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2752
$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
2753
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2754
$result = $dbi->select(
2755
    table => 'table1',
2756
    primary_key => ['key1', 'key2'],
2757
    id => [1, 2]
2758
);
2759
$row = $result->one;
2760
is($row->{key1}, 1);
2761
is($row->{key2}, 2);
2762
is($row->{key3}, 3);
2763

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

            
2776
$dbi->delete_all(table => 'table1');
2777
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2778
$result = $dbi->select(
2779
    table => 'table1',
2780
    primary_key => ['key1', 'key2'],
2781
    id => [1, 2]
2782
);
2783
$row = $result->one;
2784
is($row->{key1}, 1);
2785
is($row->{key2}, 2);
2786
is($row->{key3}, 3);
2787

            
2788

            
2789
test 'model select_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
2790
$dbi = MyDBI6->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2791
$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
2792
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2793
$result = $dbi->model('table1')->select(id => [1, 2]);
2794
$row = $result->one;
2795
is($row->{key1}, 1);
2796
is($row->{key2}, 2);
2797
is($row->{key3}, 3);
2798

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

            
2814
$result = $model->select(
2815
    column => [$model->column('table2' => [qw/key1 key3/])],
2816
    where => {'table1.key1' => 1}
2817
);
2818
is_deeply($result->one,
2819
          {'table2.key1' => 1, 'table2.key3' => 3});
2820

            
2821

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

            
2834
$result = $dbi->select(table => 'table1');
2835
is($result->one->{key1}, 'A');
2836

            
2837

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

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

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

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

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

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

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

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

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

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

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

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

            
3025
eval{$dbi->type_rule(
3026
    into1 => {
3027
        date => 'pp'
3028
    }
3029
)};
3030
like($@, qr/not registered/);
3031

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

            
3043
eval {
3044
    $dbi->type_rule(
3045
        into1 => {
3046
            Date => sub { $_[0] * 2 },
3047
        }
3048
    );
3049
};
3050
like($@, qr/lower/);
3051

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

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

            
3086
$result = $dbi->select(table => 'table1');
3087
$result->type_rule(
3088
    from1 => {
3089
        date => sub { $_[0] * 3 }
3090
    }
3091
);
3092
$row = $result->one;
3093
is($row->{key1}, 6);
3094
is($row->{key2}, 2);
3095

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

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

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

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

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

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

            
3218
test 'separator';
cleanup test
Yuki Kimoto authored on 2011-08-06
3219
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
3220
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
3221
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
3222

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

            
3248
$dbi->separator('__');
3249
$model = $dbi->model('table1');
3250
$result = $model->select(
3251
    column => [
3252
        $model->mycolumn,
3253
        {table2 => [qw/key1 key3/]}
3254
    ],
3255
    where => {'table1.key1' => 1}
3256
);
3257
is_deeply($result->one,
3258
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
3259
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3260

            
3261
$dbi->separator('-');
3262
$model = $dbi->model('table1');
3263
$result = $model->select(
3264
    column => [
3265
        $model->mycolumn,
3266
        {table2 => [qw/key1 key3/]}
3267
    ],
3268
    where => {'table1.key1' => 1}
3269
);
3270
is_deeply($result->one,
3271
          {key1 => 1, key2 => 2, 'table2-key1' => 1, 'table2-key3' => 3});
3272
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3273

            
3274

            
3275
test 'filter_off';
cleanup test
Yuki Kimoto authored on 2011-08-06
3276
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
3277
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
3278
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
3279

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

            
3294
test 'available_date_type';
cleanup test
Yuki Kimoto authored on 2011-08-06
3295
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
3296
ok($dbi->can('available_data_type'));
3297

            
3298

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

            
3306

            
3307
test 'separator';
cleanup test
Yuki Kimoto authored on 2011-08-06
3308
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
3309
is($dbi->separator, '.');
3310
$dbi->separator('-');
3311
is($dbi->separator, '-');
3312
$dbi->separator('__');
3313
is($dbi->separator, '__');
3314
eval { $dbi->separator('?') };
3315
like($@, qr/Separator/);
3316

            
3317

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

            
3329
$param = $dbi->map_param(
3330
    {id => 0, author => 0, price => 0},
3331
    id => 'book.id',
3332
    author => ['book.author', sub { '%' . $_[0] . '%' }],
3333
    price => ['book.price', sub { '%' . $_[0] . '%' },
3334
      {if => sub { $_[0] eq 0 }}]
3335
);
3336
is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
3337

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

            
3347
$param = $dbi->map_param(
3348
    {id => undef, author => undef, price => undef},
3349
    id => 'book.id',
3350
    price => ['book.price', {if => 'exists'}]
3351
);
3352
is_deeply($param, {'book.price' => undef});
3353

            
3354
$param = $dbi->map_param(
3355
    {price => 'a'},
3356
    id => ['book.id', {if => 'exists'}],
3357
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
3358
);
3359
is_deeply($param, {'book.price' => '%a'});
3360

            
3361

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

            
3375

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

            
3394
    $order = $dbi->order;
3395
    $order->prepend(['table1-key1'], [qw/table1-key2 desc/]);
3396
    $result = $dbi->select(table => 'table1',
3397
      column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']],
3398
      append => "$order");
3399
    is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3},
3400
      {'table1-key1' => 1, 'table1-key2' => 1},
3401
      {'table1-key1' => 2, 'table1-key2' => 4},
3402
      {'table1-key1' => 2, 'table1-key2' => 2}]);
3403
}
3404

            
3405
test 'tag_parse';
3406
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
3407
$dbi->tag_parse(0);
3408
{
3409
    $dbi->execute("create table table1 (key1, key2)");
3410
    $dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
3411
    eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})};
3412
    ok($@);
3413
}
3414

            
3415
test 'last_sql';
3416
{
3417
    my $dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
3418
    $dbi->execute("create table table1 (key1, key2)");
3419
    $dbi->execute('select * from table1');
3420
    is($dbi->last_sql, 'select * from table1;');
3421
    
3422
    eval{$dbi->execute("aaa")};
3423
    is($dbi->last_sql, 'aaa;');
3424
    
3425
}
3426

            
3427
test 'DBIx::Custom header';
3428
{
3429
    my $dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
3430
    $dbi->execute("create table table1 (key1, key2)");
3431
    my $result = $dbi->execute('select key1 as h1, key2 as h2 from table1');
3432
    
3433
    is_deeply($result->header, [qw/h1 h2/]);
3434
    
3435
}
3436

            
3437
test 'Named placeholder :name(operater) syntax';
cleanup test
Yuki Kimoto authored on 2011-08-06
3438
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
3439
$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
3440
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3441
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3442

            
3443
$source = "select * from table1 where :key1{=} and :key2{=}";
3444
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
3445
$rows = $result->all;
3446
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3447

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

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

            
3458
$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}";
3459
$result = $dbi->execute(
3460
    $source,
3461
    param => {'table1.key1' => 1, 'table1.key2' => 1},
3462
    filter => {'table1.key2' => sub { $_[0] * 2 }}
3463
);
3464
$rows = $result->all;
3465
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3466

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

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