DBIx-Custom / t / basic.t /
Newer Older
3508 lines | 122.381kb
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;
49

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

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

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

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

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

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

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

            
90

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

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

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

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

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

            
133
$source = "select * from table1 where key1 = :key1 and {<> key2} and {< key3} and {> key4} and {>= key5};";
134
$query = $dbi->execute($source, {}, query => 1);
135
$result = $dbi->execute($query, param => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
136
$rows = $result->all;
137
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1");
138

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

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

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

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

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

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

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

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

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

            
186

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

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

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

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

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

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

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

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

            
240

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
473

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

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

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

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

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

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

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

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

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

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

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

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

            
568

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
717

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

            
722
$dbi->begin_work;
723

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

            
730
$dbi->rollback if $@;
731

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

            
736
$dbi->begin_work;
737

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

            
743
$dbi->commit unless $@;
744

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

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

            
754

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1393

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

            
1405

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

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

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

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

            
1435

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

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

            
1444
$dbi->apply_filter(
1445

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1572
{
1573
    package MyDBI4;
1574

            
1575
    use strict;
1576
    use warnings;
1577

            
1578
    use base 'DBIx::Custom';
1579

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

            
1591
    package MyModel2::Base1;
1592

            
1593
    use strict;
1594
    use warnings;
1595

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

            
1598
    package MyModel2::book;
1599

            
1600
    use strict;
1601
    use warnings;
1602

            
1603
    use base 'MyModel2::Base1';
1604

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

            
1611
    sub list { shift->select; }
1612

            
1613
    package MyModel2::Company;
1614

            
1615
    use strict;
1616
    use warnings;
1617

            
1618
    use base 'MyModel2::Base1';
1619

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

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

            
1638
{
1639
     package MyDBI5;
1640

            
1641
    use strict;
1642
    use warnings;
1643

            
1644
    use base 'DBIx::Custom';
1645

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1872

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

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

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

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

            
1938

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

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

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

            
1988

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

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

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

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

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

            
2029

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

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

            
2049

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2062
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2063
$dbi->quote('"');
cleanup test
Yuki Kimoto authored on 2011-08-06
2064
$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
2065
$param = {key1 => 1, key2 => 2};
2066
$insert_param = $dbi->insert_param($param);
2067
$sql = <<"EOS";
2068
insert into table1 $insert_param
2069
EOS
2070
$dbi->execute($sql, param => $param, table => 'table1');
2071
is($dbi->select(table => 'table1')->one->{key1}, 1);
2072
is($dbi->select(table => 'table1')->one->{key2}, 2);
2073

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

            
2077

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

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

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

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

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

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

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

            
2153
{
2154
    package MyDBI8;
2155
    
2156
    use base 'DBIx::Custom';
2157
    
2158
    sub connect {
2159
        my $self = shift->SUPER::connect(@_);
2160
        
2161
        $self->include_model('MyModel7');
2162
        
2163
        return $self;
2164
    }
2165
}
cleanup test
Yuki Kimoto authored on 2011-08-06
2166
{
2167
    $dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2168
    $dbi->execute('create table table1 (key1 char(255), key2 char(255));');
2169
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2170
    $sql = <<"EOS";
cleanup test
Yuki Kimoto authored on 2011-08-06
2171
left outer join (
2172
  select * from table1 as t1
2173
  where t1.key2 = (
2174
    select max(t2.key2) from table1 as t2
2175
    where t1.key1 = t2.key1
2176
  )
2177
) as latest_table1 on table1.key1 = latest_table1.key1
2178
EOS
cleanup test
Yuki Kimoto authored on 2011-08-06
2179
    my $join = [$sql];
2180
    $rows = $dbi->select(
2181
        table => 'table1',
2182
        column => 'latest_table1.key1 as latest_table1__key1',
2183
        join  => $join
2184
    )->all;
2185
    is_deeply($rows, [{latest_table1__key1 => 1}]);
2186
}
cleanup test
Yuki Kimoto authored on 2011-08-06
2187
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
cleanup test
Yuki Kimoto authored on 2011-08-06
2188
$dbi->execute('create table table1 (key1 char(255), key2 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2189
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2190
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2191
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2192
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
2193
$result = $dbi->select(
2194
    table => 'table1',
2195
    join => [
2196
        "left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1"
2197
    ]
2198
);
2199
is_deeply($result->all, [{key1 => 1, key2 => 2}]);
2200
$result = $dbi->select(
2201
    table => 'table1',
2202
    column => [{table2 => ['key3']}],
2203
    join => [
2204
        "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1"
2205
    ]
2206
);
2207
is_deeply($result->all, [{'table2.key3' => 4}]);
2208
$result = $dbi->select(
2209
    table => 'table1',
2210
    column => [{table2 => ['key3']}],
2211
    join => [
2212
        "left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'"
2213
    ]
2214
);
2215
is_deeply($result->all, [{'table2.key3' => 4}]);
2216

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

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

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

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

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

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

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

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

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

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

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

            
2369

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

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

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

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

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

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

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

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

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

            
2493

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

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

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

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

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

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

            
2567

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

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

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

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

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

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

            
2632

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

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

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

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

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

            
2697

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

            
2712

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

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

            
2732

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

            
2748

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

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

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

            
2787

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

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

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

            
2820

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

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

            
2836

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
3273

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

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

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

            
3297

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

            
3305

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

            
3316

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

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

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

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

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

            
3360

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

            
3374

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

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

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

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

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

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

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

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

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

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

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

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