DBIx-Custom / t / basic.t /
Newer Older
3506 lines | 122.449kb
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';
test cleanup
Yuki Kimoto authored on 2011-08-06
2051
{
2052
    $dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2053
    $dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
2054
    $param = {key1 => 1, key2 => 2};
2055
    my $insert_param = $dbi->insert_param($param);
2056
    $sql = "insert into table1 $insert_param";
2057
    $dbi->execute($sql, param => $param, table => 'table1');
2058
    is($dbi->select(table => 'table1')->one->{key1}, 1);
2059
    is($dbi->select(table => 'table1')->one->{key2}, 2);
2060
}
2061
{
2062
    $dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2063
    $dbi->quote('"');
2064
    $dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
2065
    $param = {key1 => 1, key2 => 2};
2066
    my $insert_param = $dbi->insert_param($param);
2067
    $sql = "insert into table1 $insert_param";
2068
    $dbi->execute($sql, param => $param, table => 'table1');
2069
    is($dbi->select(table => 'table1')->one->{key1}, 1);
2070
    is($dbi->select(table => 'table1')->one->{key2}, 2);
2071

            
2072
    eval { $dbi->insert_param({";" => 1}) };
2073
    like($@, qr/not safety/);
2074
}
cleanup test
Yuki Kimoto authored on 2011-08-06
2075

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2367

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

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

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

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

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

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

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

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

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

            
2491

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

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

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

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

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

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

            
2565

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

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

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

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

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

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

            
2630

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

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

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

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

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

            
2695

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

            
2710

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

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

            
2730

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

            
2746

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

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

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

            
2785

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

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

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

            
2818

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

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

            
2834

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
3271

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

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

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

            
3295

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

            
3303

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

            
3314

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

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

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

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

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

            
3358

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

            
3372

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

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

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

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

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

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

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

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

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

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

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

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